| 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" |
| 11 #include "SkMallocPixelRef.h" | 13 #include "SkMallocPixelRef.h" |
| 12 #include "SkOSFile.h" | 14 #include "SkOSFile.h" |
| 13 #include "SkPictureRecorder.h" | 15 #include "SkPictureRecorder.h" |
| 14 #include "SkTableColorFilter.h" | 16 #include "SkTableColorFilter.h" |
| 15 #include "SkTemplates.h" | 17 #include "SkTemplates.h" |
| 16 #include "SkTypeface.h" | 18 #include "SkTypeface.h" |
| 17 #include "SkWriteBuffer.h" | 19 #include "SkWriteBuffer.h" |
| 18 #include "SkValidatingReadBuffer.h" | 20 #include "SkValidatingReadBuffer.h" |
| 19 #include "SkXfermodeImageFilter.h" | 21 #include "SkXfermodeImageFilter.h" |
| 20 #include "Test.h" | 22 #include "Test.h" |
| (...skipping 287 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 308 | 310 |
| 309 int pixelErrors = 0; | 311 int pixelErrors = 0; |
| 310 for (int y = 0; y < b2.height(); ++y) { | 312 for (int y = 0; y < b2.height(); ++y) { |
| 311 for (int x = 0; x < b2.width(); ++x) { | 313 for (int x = 0; x < b2.width(); ++x) { |
| 312 if (b1.getColor(x, y) != b2.getColor(x, y)) | 314 if (b1.getColor(x, y) != b2.getColor(x, y)) |
| 313 ++pixelErrors; | 315 ++pixelErrors; |
| 314 } | 316 } |
| 315 } | 317 } |
| 316 REPORTER_ASSERT(reporter, 0 == pixelErrors); | 318 REPORTER_ASSERT(reporter, 0 == pixelErrors); |
| 317 } | 319 } |
| 318 | 320 static void serialize_and_compare_typeface(SkTypeface* typeface, const char* tex
t, |
| 319 static void TestPictureTypefaceSerialization(skiatest::Reporter* reporter) { | 321 skiatest::Reporter* reporter) |
| 320 // Load typeface form file to test CreateFromFile with index. | 322 { |
| 321 SkString filename = GetResourcePath("/fonts/test.ttc"); | 323 // Create a paint with the typeface. |
| 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. | |
| 329 SkPaint paint; | 324 SkPaint paint; |
| 330 paint.setColor(SK_ColorGRAY); | 325 paint.setColor(SK_ColorGRAY); |
| 331 paint.setTextSize(SkIntToScalar(30)); | 326 paint.setTextSize(SkIntToScalar(30)); |
| 332 SkSafeUnref(paint.setTypeface(typeface)); | 327 paint.setTypeface(typeface); |
| 333 | 328 |
| 334 // Paint some text. | 329 // Paint some text. |
| 335 SkPictureRecorder recorder; | 330 SkPictureRecorder recorder; |
| 336 SkIRect canvasRect = SkIRect::MakeWH(kBitmapSize, kBitmapSize); | 331 SkIRect canvasRect = SkIRect::MakeWH(kBitmapSize, kBitmapSize); |
| 337 SkCanvas* canvas = recorder.beginRecording(SkIntToScalar(canvasRect.width())
, | 332 SkCanvas* canvas = recorder.beginRecording(SkIntToScalar(canvasRect.width())
, |
| 338 SkIntToScalar(canvasRect.height()
), | 333 SkIntToScalar(canvasRect.height()
), |
| 339 NULL, 0); | 334 NULL, 0); |
| 340 canvas->drawColor(SK_ColorWHITE); | 335 canvas->drawColor(SK_ColorWHITE); |
| 341 canvas->drawText("A!", 2, 24, 32, paint); | 336 canvas->drawText(text, 2, 24, 32, paint); |
| 342 SkAutoTUnref<SkPicture> picture(recorder.endRecording()); | 337 SkAutoTUnref<SkPicture> picture(recorder.endRecording()); |
| 343 | 338 |
| 344 // Serlialize picture and create its clone from stream. | 339 // Serlialize picture and create its clone from stream. |
| 345 SkDynamicMemoryWStream stream; | 340 SkDynamicMemoryWStream stream; |
| 346 picture->serialize(&stream); | 341 picture->serialize(&stream); |
| 347 SkAutoTDelete<SkStream> inputStream(stream.detachAsStream()); | 342 SkAutoTDelete<SkStream> inputStream(stream.detachAsStream()); |
| 348 SkAutoTUnref<SkPicture> loadedPicture(SkPicture::CreateFromStream(inputStrea
m.get())); | 343 SkAutoTUnref<SkPicture> loadedPicture(SkPicture::CreateFromStream(inputStrea
m.get())); |
| 349 | 344 |
| 350 // Draw both original and clone picture and compare bitmaps -- they should b
e identical. | 345 // Draw both original and clone picture and compare bitmaps -- they should b
e identical. |
| 351 SkBitmap origBitmap = draw_picture(*picture); | 346 SkBitmap origBitmap = draw_picture(*picture); |
| 352 SkBitmap destBitmap = draw_picture(*loadedPicture); | 347 SkBitmap destBitmap = draw_picture(*loadedPicture); |
| 353 compare_bitmaps(reporter, origBitmap, destBitmap); | 348 compare_bitmaps(reporter, origBitmap, destBitmap); |
| 354 } | 349 } |
| 355 | 350 |
| 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 |
| 356 static void setup_bitmap_for_canvas(SkBitmap* bitmap) { | 381 static void setup_bitmap_for_canvas(SkBitmap* bitmap) { |
| 357 bitmap->allocN32Pixels(kBitmapSize, kBitmapSize); | 382 bitmap->allocN32Pixels(kBitmapSize, kBitmapSize); |
| 358 } | 383 } |
| 359 | 384 |
| 360 static void make_checkerboard_bitmap(SkBitmap& bitmap) { | 385 static void make_checkerboard_bitmap(SkBitmap& bitmap) { |
| 361 setup_bitmap_for_canvas(&bitmap); | 386 setup_bitmap_for_canvas(&bitmap); |
| 362 | 387 |
| 363 SkCanvas canvas(bitmap); | 388 SkCanvas canvas(bitmap); |
| 364 canvas.clear(0x00000000); | 389 canvas.clear(0x00000000); |
| 365 SkPaint darkPaint; | 390 SkPaint darkPaint; |
| (...skipping 145 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 511 | 536 |
| 512 // Deserialize picture | 537 // Deserialize picture |
| 513 SkValidatingReadBuffer reader(static_cast<void*>(data.get()), size); | 538 SkValidatingReadBuffer reader(static_cast<void*>(data.get()), size); |
| 514 SkAutoTUnref<SkPicture> readPict( | 539 SkAutoTUnref<SkPicture> readPict( |
| 515 SkPicture::CreateFromBuffer(reader)); | 540 SkPicture::CreateFromBuffer(reader)); |
| 516 REPORTER_ASSERT(reporter, readPict.get()); | 541 REPORTER_ASSERT(reporter, readPict.get()); |
| 517 } | 542 } |
| 518 | 543 |
| 519 TestPictureTypefaceSerialization(reporter); | 544 TestPictureTypefaceSerialization(reporter); |
| 520 } | 545 } |
| OLD | NEW |