OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2010 The Android Open Source Project | 2 * Copyright 2010 The Android Open Source Project |
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 "SkBitmap.h" | 9 #include "SkBitmap.h" |
10 #include "SkCanvas.h" | 10 #include "SkCanvas.h" |
11 #include "SkData.h" | 11 #include "SkData.h" |
12 #include "SkDocument.h" | 12 #include "SkDocument.h" |
13 #include "SkDeflate.h" | 13 #include "SkDeflate.h" |
14 #include "SkImageEncoder.h" | 14 #include "SkImageEncoder.h" |
15 #include "SkMatrix.h" | 15 #include "SkMatrix.h" |
16 #include "SkPDFCanon.h" | 16 #include "SkPDFCanon.h" |
17 #include "SkPDFDevice.h" | 17 #include "SkPDFDevice.h" |
18 #include "SkPDFFont.h" | 18 #include "SkPDFFont.h" |
19 #include "SkPDFStream.h" | 19 #include "SkPDFStream.h" |
20 #include "SkPDFTypes.h" | 20 #include "SkPDFTypes.h" |
| 21 #include "SkPDFUtils.h" |
21 #include "SkReadBuffer.h" | 22 #include "SkReadBuffer.h" |
22 #include "SkScalar.h" | 23 #include "SkScalar.h" |
23 #include "SkStream.h" | 24 #include "SkStream.h" |
24 #include "SkTypes.h" | 25 #include "SkTypes.h" |
25 #include "Test.h" | 26 #include "Test.h" |
26 #include "sk_tool_utils.h" | 27 #include "sk_tool_utils.h" |
27 | 28 |
28 #define DUMMY_TEXT "DCT compessed stream." | 29 #define DUMMY_TEXT "DCT compessed stream." |
29 | 30 |
30 namespace { | 31 namespace { |
(...skipping 303 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
334 dict->insertString("n6", "A String"); | 335 dict->insertString("n6", "A String"); |
335 ASSERT_EMIT_EQ(reporter, *dict, "<</n1 24\n/n2 99\n/n3 0.5\n/n4 /AName\n" | 336 ASSERT_EMIT_EQ(reporter, *dict, "<</n1 24\n/n2 99\n/n3 0.5\n/n4 /AName\n" |
336 "/n5 /AnotherName\n/n6 (A String)>>"); | 337 "/n5 /AnotherName\n/n6 (A String)>>"); |
337 | 338 |
338 dict->insertString("n7", SkString("Another String")); | 339 dict->insertString("n7", SkString("Another String")); |
339 ASSERT_EMIT_EQ(reporter, *dict, "<</n1 24\n/n2 99\n/n3 0.5\n/n4 /AName\n" | 340 ASSERT_EMIT_EQ(reporter, *dict, "<</n1 24\n/n2 99\n/n3 0.5\n/n4 /AName\n" |
340 "/n5 /AnotherName\n/n6 (A String)\n/n7 (Another String)>>"); | 341 "/n5 /AnotherName\n/n6 (A String)\n/n7 (Another String)>>"); |
341 | 342 |
342 dict.reset(new SkPDFDict("DType")); | 343 dict.reset(new SkPDFDict("DType")); |
343 ASSERT_EMIT_EQ(reporter, *dict, "<</Type /DType>>"); | 344 ASSERT_EMIT_EQ(reporter, *dict, "<</Type /DType>>"); |
344 | 345 |
345 SkAutoTUnref<SkPDFArray> referencedArray(new SkPDFArray); | 346 SkAutoTUnref<SkPDFArray> referencedArray(new SkPDFArray); |
346 Catalog catalog; | 347 Catalog catalog; |
347 catalog.numbers.addObject(referencedArray.get()); | 348 catalog.numbers.addObject(referencedArray.get()); |
348 REPORTER_ASSERT(reporter, catalog.numbers.getObjectNumber( | 349 REPORTER_ASSERT(reporter, catalog.numbers.getObjectNumber( |
349 referencedArray.get()) == 1); | 350 referencedArray.get()) == 1); |
350 dict->insertObjRef("n1", referencedArray.detach()); | 351 dict->insertObjRef("n1", referencedArray.detach()); |
351 SkString result = emit_to_string(*dict, &catalog); | 352 SkString result = emit_to_string(*dict, &catalog); |
352 ASSERT_EQ(reporter, result, "<</Type /DType\n/n1 1 0 R>>"); | 353 ASSERT_EQ(reporter, result, "<</Type /DType\n/n1 1 0 R>>"); |
353 } | 354 } |
354 | 355 |
(...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
428 SkAutoTUnref<SkTypeface> noEmbedTypeface(GetResourceAsTypeface(resource)); | 429 SkAutoTUnref<SkTypeface> noEmbedTypeface(GetResourceAsTypeface(resource)); |
429 if (noEmbedTypeface) { | 430 if (noEmbedTypeface) { |
430 REPORTER_ASSERT(reporter, | 431 REPORTER_ASSERT(reporter, |
431 !SkPDFFont::CanEmbedTypeface(noEmbedTypeface, &canon)); | 432 !SkPDFFont::CanEmbedTypeface(noEmbedTypeface, &canon)); |
432 } | 433 } |
433 SkAutoTUnref<SkTypeface> portableTypeface( | 434 SkAutoTUnref<SkTypeface> portableTypeface( |
434 sk_tool_utils::create_portable_typeface(NULL, SkTypeface::kNormal)); | 435 sk_tool_utils::create_portable_typeface(NULL, SkTypeface::kNormal)); |
435 REPORTER_ASSERT(reporter, | 436 REPORTER_ASSERT(reporter, |
436 SkPDFFont::CanEmbedTypeface(portableTypeface, &canon)); | 437 SkPDFFont::CanEmbedTypeface(portableTypeface, &canon)); |
437 } | 438 } |
| 439 |
| 440 |
| 441 // test to see that all finite scalars round trip via scanf(). |
| 442 static void check_pdf_scalar_serialization(skiatest::Reporter* reporter, |
| 443 float inputFloat) { |
| 444 SkDynamicMemoryWStream buffer; |
| 445 SkPDFUtils::AppendScalar(inputFloat, &buffer); |
| 446 char floatString[2048]; |
| 447 if (buffer.bytesWritten() >= sizeof(floatString)) { |
| 448 ERRORF(reporter, "absurdly long output."); |
| 449 return; |
| 450 } |
| 451 buffer.copyTo(floatString); |
| 452 floatString[buffer.bytesWritten()] = '\0'; |
| 453 if (reporter->verbose()) { |
| 454 SkDebugf("%.9g = \"%s\"\n", inputFloat, floatString); |
| 455 } |
| 456 float roundTripFloat; |
| 457 if (1 != sscanf(floatString, "%f", &roundTripFloat)) { |
| 458 ERRORF(reporter, "unscannable result"); |
| 459 return; |
| 460 } |
| 461 if (isfinite(inputFloat)) { |
| 462 REPORTER_ASSERT(reporter, roundTripFloat == inputFloat); |
| 463 } |
| 464 } |
| 465 |
| 466 // Test SkPDFUtils::AppendScalar for accuracy. |
| 467 DEF_TEST(PDFPrimitives_Scalar, reporter) { |
| 468 const int kRandomSeed = 0xBEEF; |
| 469 int iterationCount = 500; |
| 470 SkRandom random(kRandomSeed); |
| 471 while (iterationCount-- > 0) { |
| 472 union { |
| 473 float inputFloat; |
| 474 uint32_t u; |
| 475 }; |
| 476 static_assert(sizeof(float) == sizeof(uint32_t), ""); |
| 477 u = random.nextU(); // Pick any random float. |
| 478 check_pdf_scalar_serialization(reporter, inputFloat); |
| 479 } |
| 480 float alwaysCheck[] = { 0.0f, -0.0f, 1.0f, -1.0f, FLT_MIN, FLT_MAX, |
| 481 -FLT_MIN, -FLT_MAX, SK_FloatInfinity, |
| 482 SK_FloatNegativeInfinity, SK_FloatNaN, SK_ScalarPI, |
| 483 static_cast<float>(1.0 / 10.0)}; |
| 484 for (float inputFloat: alwaysCheck) { |
| 485 check_pdf_scalar_serialization(reporter, inputFloat); |
| 486 } |
| 487 } |
OLD | NEW |