| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright 2013 Google Inc. | 2 * Copyright 2013 Google Inc. |
| 3 * | 3 * |
| 4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
| 5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
| 6 */ | 6 */ |
| 7 | 7 |
| 8 #include "Resources.h" | 8 #include "Resources.h" |
| 9 #include "SkBitmapSource.h" | 9 #include "SkBitmapSource.h" |
| 10 #include "SkCanvas.h" | 10 #include "SkCanvas.h" |
| 11 #include "SkFixed.h" | |
| 12 #include "SkFontDescriptor.h" | |
| 13 #include "SkMallocPixelRef.h" | 11 #include "SkMallocPixelRef.h" |
| 14 #include "SkOSFile.h" | 12 #include "SkOSFile.h" |
| 15 #include "SkPictureRecorder.h" | 13 #include "SkPictureRecorder.h" |
| 16 #include "SkTableColorFilter.h" | 14 #include "SkTableColorFilter.h" |
| 17 #include "SkTemplates.h" | 15 #include "SkTemplates.h" |
| 18 #include "SkTypeface.h" | 16 #include "SkTypeface.h" |
| 19 #include "SkWriteBuffer.h" | 17 #include "SkWriteBuffer.h" |
| 20 #include "SkValidatingReadBuffer.h" | 18 #include "SkValidatingReadBuffer.h" |
| 21 #include "SkXfermodeImageFilter.h" | 19 #include "SkXfermodeImageFilter.h" |
| 22 #include "Test.h" | 20 #include "Test.h" |
| (...skipping 287 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 310 | 308 |
| 311 int pixelErrors = 0; | 309 int pixelErrors = 0; |
| 312 for (int y = 0; y < b2.height(); ++y) { | 310 for (int y = 0; y < b2.height(); ++y) { |
| 313 for (int x = 0; x < b2.width(); ++x) { | 311 for (int x = 0; x < b2.width(); ++x) { |
| 314 if (b1.getColor(x, y) != b2.getColor(x, y)) | 312 if (b1.getColor(x, y) != b2.getColor(x, y)) |
| 315 ++pixelErrors; | 313 ++pixelErrors; |
| 316 } | 314 } |
| 317 } | 315 } |
| 318 REPORTER_ASSERT(reporter, 0 == pixelErrors); | 316 REPORTER_ASSERT(reporter, 0 == pixelErrors); |
| 319 } | 317 } |
| 320 static void serialize_and_compare_typeface(SkTypeface* typeface, const char* tex
t, | 318 |
| 321 skiatest::Reporter* reporter) | 319 static void TestPictureTypefaceSerialization(skiatest::Reporter* reporter) { |
| 322 { | 320 // Load typeface form file to test CreateFromFile with index. |
| 323 // Create a paint with the typeface. | 321 SkString filename = GetResourcePath("/fonts/test.ttc"); |
| 322 SkTypeface* typeface = SkTypeface::CreateFromFile(filename.c_str(), 1); |
| 323 if (!typeface) { |
| 324 SkDebugf("Could not run fontstream test because test.ttc not found."); |
| 325 return; |
| 326 } |
| 327 |
| 328 // Create a paint with the typeface we loaded. |
| 324 SkPaint paint; | 329 SkPaint paint; |
| 325 paint.setColor(SK_ColorGRAY); | 330 paint.setColor(SK_ColorGRAY); |
| 326 paint.setTextSize(SkIntToScalar(30)); | 331 paint.setTextSize(SkIntToScalar(30)); |
| 327 paint.setTypeface(typeface); | 332 SkSafeUnref(paint.setTypeface(typeface)); |
| 328 | 333 |
| 329 // Paint some text. | 334 // Paint some text. |
| 330 SkPictureRecorder recorder; | 335 SkPictureRecorder recorder; |
| 331 SkIRect canvasRect = SkIRect::MakeWH(kBitmapSize, kBitmapSize); | 336 SkIRect canvasRect = SkIRect::MakeWH(kBitmapSize, kBitmapSize); |
| 332 SkCanvas* canvas = recorder.beginRecording(SkIntToScalar(canvasRect.width())
, | 337 SkCanvas* canvas = recorder.beginRecording(SkIntToScalar(canvasRect.width())
, |
| 333 SkIntToScalar(canvasRect.height()
), | 338 SkIntToScalar(canvasRect.height()
), |
| 334 NULL, 0); | 339 NULL, 0); |
| 335 canvas->drawColor(SK_ColorWHITE); | 340 canvas->drawColor(SK_ColorWHITE); |
| 336 canvas->drawText(text, 2, 24, 32, paint); | 341 canvas->drawText("A!", 2, 24, 32, paint); |
| 337 SkAutoTUnref<SkPicture> picture(recorder.endRecording()); | 342 SkAutoTUnref<SkPicture> picture(recorder.endRecording()); |
| 338 | 343 |
| 339 // Serlialize picture and create its clone from stream. | 344 // Serlialize picture and create its clone from stream. |
| 340 SkDynamicMemoryWStream stream; | 345 SkDynamicMemoryWStream stream; |
| 341 picture->serialize(&stream); | 346 picture->serialize(&stream); |
| 342 SkAutoTDelete<SkStream> inputStream(stream.detachAsStream()); | 347 SkAutoTDelete<SkStream> inputStream(stream.detachAsStream()); |
| 343 SkAutoTUnref<SkPicture> loadedPicture(SkPicture::CreateFromStream(inputStrea
m.get())); | 348 SkAutoTUnref<SkPicture> loadedPicture(SkPicture::CreateFromStream(inputStrea
m.get())); |
| 344 | 349 |
| 345 // Draw both original and clone picture and compare bitmaps -- they should b
e identical. | 350 // Draw both original and clone picture and compare bitmaps -- they should b
e identical. |
| 346 SkBitmap origBitmap = draw_picture(*picture); | 351 SkBitmap origBitmap = draw_picture(*picture); |
| 347 SkBitmap destBitmap = draw_picture(*loadedPicture); | 352 SkBitmap destBitmap = draw_picture(*loadedPicture); |
| 348 compare_bitmaps(reporter, origBitmap, destBitmap); | 353 compare_bitmaps(reporter, origBitmap, destBitmap); |
| 349 } | 354 } |
| 350 | 355 |
| 351 static void TestPictureTypefaceSerialization(skiatest::Reporter* reporter) { | |
| 352 { | |
| 353 // Load typeface from file to test CreateFromFile with index. | |
| 354 SkString filename = GetResourcePath("/fonts/test.ttc"); | |
| 355 SkAutoTUnref<SkTypeface> typeface(SkTypeface::CreateFromFile(filename.c_
str(), 1)); | |
| 356 if (!typeface) { | |
| 357 SkDebugf("Could not run fontstream test because test.ttc not found."
); | |
| 358 } else { | |
| 359 serialize_and_compare_typeface(typeface, "A!", reporter); | |
| 360 } | |
| 361 } | |
| 362 | |
| 363 { | |
| 364 // Load typeface as stream to create with axis settings. | |
| 365 SkAutoTDelete<SkStreamAsset> distortable(GetResourceAsStream("/fonts/Dis
tortable.ttf")); | |
| 366 if (!distortable) { | |
| 367 SkDebugf("Could not run fontstream test because Distortable.ttf not
found."); | |
| 368 } else { | |
| 369 SkFixed axis = SK_FixedSqrt2; | |
| 370 SkAutoTUnref<SkTypeface> typeface(SkTypeface::CreateFromFontData( | |
| 371 new SkFontData(distortable.detach(), 0, &axis, 1))); | |
| 372 if (!typeface) { | |
| 373 SkDebugf("Could not run fontstream test because Distortable.ttf
not created."); | |
| 374 } else { | |
| 375 serialize_and_compare_typeface(typeface, "abc", reporter); | |
| 376 } | |
| 377 } | |
| 378 } | |
| 379 } | |
| 380 | |
| 381 static void setup_bitmap_for_canvas(SkBitmap* bitmap) { | 356 static void setup_bitmap_for_canvas(SkBitmap* bitmap) { |
| 382 bitmap->allocN32Pixels(kBitmapSize, kBitmapSize); | 357 bitmap->allocN32Pixels(kBitmapSize, kBitmapSize); |
| 383 } | 358 } |
| 384 | 359 |
| 385 static void make_checkerboard_bitmap(SkBitmap& bitmap) { | 360 static void make_checkerboard_bitmap(SkBitmap& bitmap) { |
| 386 setup_bitmap_for_canvas(&bitmap); | 361 setup_bitmap_for_canvas(&bitmap); |
| 387 | 362 |
| 388 SkCanvas canvas(bitmap); | 363 SkCanvas canvas(bitmap); |
| 389 canvas.clear(0x00000000); | 364 canvas.clear(0x00000000); |
| 390 SkPaint darkPaint; | 365 SkPaint darkPaint; |
| (...skipping 145 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 536 | 511 |
| 537 // Deserialize picture | 512 // Deserialize picture |
| 538 SkValidatingReadBuffer reader(static_cast<void*>(data.get()), size); | 513 SkValidatingReadBuffer reader(static_cast<void*>(data.get()), size); |
| 539 SkAutoTUnref<SkPicture> readPict( | 514 SkAutoTUnref<SkPicture> readPict( |
| 540 SkPicture::CreateFromBuffer(reader)); | 515 SkPicture::CreateFromBuffer(reader)); |
| 541 REPORTER_ASSERT(reporter, readPict.get()); | 516 REPORTER_ASSERT(reporter, readPict.get()); |
| 542 } | 517 } |
| 543 | 518 |
| 544 TestPictureTypefaceSerialization(reporter); | 519 TestPictureTypefaceSerialization(reporter); |
| 545 } | 520 } |
| OLD | NEW |