vandebo@chromium.org | f66025d | 2010-10-01 23:26:55 +0000 | [diff] [blame] | 1 | /* |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 2 | * Copyright 2010 The Android Open Source Project |
vandebo@chromium.org | f66025d | 2010-10-01 23:26:55 +0000 | [diff] [blame] | 3 | * |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
vandebo@chromium.org | f66025d | 2010-10-01 23:26:55 +0000 | [diff] [blame] | 6 | */ |
| 7 | |
Hal Canary | 43fb7a0 | 2016-12-30 13:09:03 -0500 | [diff] [blame] | 8 | #include "Test.h" |
| 9 | |
| 10 | #ifdef SK_SUPPORT_PDF |
| 11 | |
halcanary | 66a82f3 | 2015-10-12 13:05:04 -0700 | [diff] [blame] | 12 | #include "Resources.h" |
edisonn@google.com | d9dfa18 | 2013-04-24 13:01:01 +0000 | [diff] [blame] | 13 | #include "SkBitmap.h" |
vandebo@chromium.org | 4e1cc6a | 2013-01-25 19:27:23 +0000 | [diff] [blame] | 14 | #include "SkCanvas.h" |
Hal Canary | 1521c8a | 2018-03-28 09:51:00 -0700 | [diff] [blame] | 15 | #include "SkClusterator.h" |
reed@google.com | 8a85d0c | 2011-06-24 19:12:12 +0000 | [diff] [blame] | 16 | #include "SkData.h" |
halcanary | 3d32d50 | 2015-03-01 06:55:20 -0800 | [diff] [blame] | 17 | #include "SkDocument.h" |
halcanary | d9e5715 | 2015-08-12 11:24:40 -0700 | [diff] [blame] | 18 | #include "SkDeflate.h" |
edisonn@google.com | d9dfa18 | 2013-04-24 13:01:01 +0000 | [diff] [blame] | 19 | #include "SkImageEncoder.h" |
Cary Clark | 60aaeb2 | 2017-11-03 08:06:09 -0400 | [diff] [blame] | 20 | #include "SkImageFilterPriv.h" |
halcanary | 022c2bd | 2016-09-02 11:29:46 -0700 | [diff] [blame] | 21 | #include "SkMakeUnique.h" |
edisonn@google.com | d9dfa18 | 2013-04-24 13:01:01 +0000 | [diff] [blame] | 22 | #include "SkMatrix.h" |
halcanary | a1f1ee9 | 2015-02-20 06:17:26 -0800 | [diff] [blame] | 23 | #include "SkPDFCanon.h" |
vandebo@chromium.org | 4e1cc6a | 2013-01-25 19:27:23 +0000 | [diff] [blame] | 24 | #include "SkPDFDevice.h" |
halcanary | 66a82f3 | 2015-10-12 13:05:04 -0700 | [diff] [blame] | 25 | #include "SkPDFFont.h" |
vandebo@chromium.org | 8459d4e | 2010-09-24 22:25:30 +0000 | [diff] [blame] | 26 | #include "SkPDFTypes.h" |
halcanary | 8e9f5e3 | 2016-02-24 15:46:46 -0800 | [diff] [blame] | 27 | #include "SkPDFUtils.h" |
senorblanco | b0e89dc | 2014-10-20 14:03:12 -0700 | [diff] [blame] | 28 | #include "SkReadBuffer.h" |
vandebo@chromium.org | 8459d4e | 2010-09-24 22:25:30 +0000 | [diff] [blame] | 29 | #include "SkScalar.h" |
robertphillips | 4ba94e2 | 2016-04-04 12:07:47 -0700 | [diff] [blame] | 30 | #include "SkSpecialImage.h" |
vandebo@chromium.org | 8459d4e | 2010-09-24 22:25:30 +0000 | [diff] [blame] | 31 | #include "SkStream.h" |
vandebo@chromium.org | 421d644 | 2011-07-20 17:39:01 +0000 | [diff] [blame] | 32 | #include "SkTypes.h" |
halcanary | 66a82f3 | 2015-10-12 13:05:04 -0700 | [diff] [blame] | 33 | #include "sk_tool_utils.h" |
vandebo@chromium.org | 8459d4e | 2010-09-24 22:25:30 +0000 | [diff] [blame] | 34 | |
halcanary | 56586b6 | 2016-07-25 13:59:30 -0700 | [diff] [blame] | 35 | #include <cstdlib> |
| 36 | #include <cmath> |
| 37 | |
commit-bot@chromium.org | 608ea65 | 2013-10-03 19:29:21 +0000 | [diff] [blame] | 38 | #define DUMMY_TEXT "DCT compessed stream." |
| 39 | |
halcanary | f7a169e | 2015-05-01 07:35:45 -0700 | [diff] [blame] | 40 | template <typename T> |
halcanary | 530032a | 2016-08-18 14:22:52 -0700 | [diff] [blame] | 41 | static SkString emit_to_string(T& obj, SkPDFObjNumMap* catPtr = nullptr) { |
| 42 | SkPDFObjNumMap catalog; |
halcanary | 6a14434 | 2015-01-23 11:45:10 -0800 | [diff] [blame] | 43 | SkDynamicMemoryWStream buffer; |
halcanary | f7a169e | 2015-05-01 07:35:45 -0700 | [diff] [blame] | 44 | if (!catPtr) { |
| 45 | catPtr = &catalog; |
halcanary | 130444f | 2015-04-25 06:45:07 -0700 | [diff] [blame] | 46 | } |
halcanary | 530032a | 2016-08-18 14:22:52 -0700 | [diff] [blame] | 47 | obj.emitObject(&buffer, *catPtr); |
halcanary | 56586b6 | 2016-07-25 13:59:30 -0700 | [diff] [blame] | 48 | SkString tmp(buffer.bytesWritten()); |
| 49 | buffer.copyTo(tmp.writable_str()); |
halcanary | f7a169e | 2015-05-01 07:35:45 -0700 | [diff] [blame] | 50 | return tmp; |
vandebo@chromium.org | 8459d4e | 2010-09-24 22:25:30 +0000 | [diff] [blame] | 51 | } |
| 52 | |
halcanary | f7a169e | 2015-05-01 07:35:45 -0700 | [diff] [blame] | 53 | static bool eq(const SkString& str, const char* strPtr, size_t len) { |
| 54 | return len == str.size() && 0 == memcmp(str.c_str(), strPtr, len); |
vandebo@chromium.org | 421d644 | 2011-07-20 17:39:01 +0000 | [diff] [blame] | 55 | } |
| 56 | |
halcanary | 56586b6 | 2016-07-25 13:59:30 -0700 | [diff] [blame] | 57 | static void assert_eql(skiatest::Reporter* reporter, |
| 58 | const SkString& skString, |
| 59 | const char* str, |
| 60 | size_t len) { |
| 61 | if (!eq(skString, str, len)) { |
| 62 | REPORT_FAILURE(reporter, "", SkStringPrintf( |
| 63 | "'%*s' != '%s'", len, str, skString.c_str())); |
| 64 | } |
| 65 | } |
halcanary | f7a169e | 2015-05-01 07:35:45 -0700 | [diff] [blame] | 66 | |
halcanary | 56586b6 | 2016-07-25 13:59:30 -0700 | [diff] [blame] | 67 | static void assert_eq(skiatest::Reporter* reporter, |
| 68 | const SkString& skString, |
| 69 | const char* str) { |
| 70 | assert_eql(reporter, skString, str, strlen(str)); |
| 71 | } |
halcanary | f7a169e | 2015-05-01 07:35:45 -0700 | [diff] [blame] | 72 | |
| 73 | |
halcanary | 56586b6 | 2016-07-25 13:59:30 -0700 | [diff] [blame] | 74 | template <typename T> |
| 75 | static void assert_emit_eq(skiatest::Reporter* reporter, |
| 76 | T& object, |
| 77 | const char* string) { |
| 78 | SkString result = emit_to_string(object); |
| 79 | assert_eq(reporter, result, string); |
| 80 | } |
halcanary | f7a169e | 2015-05-01 07:35:45 -0700 | [diff] [blame] | 81 | |
vandebo@chromium.org | 421d644 | 2011-07-20 17:39:01 +0000 | [diff] [blame] | 82 | static void TestPDFStream(skiatest::Reporter* reporter) { |
| 83 | char streamBytes[] = "Test\nFoo\tBar"; |
halcanary | 022c2bd | 2016-09-02 11:29:46 -0700 | [diff] [blame] | 84 | auto streamData = skstd::make_unique<SkMemoryStream>( |
| 85 | streamBytes, strlen(streamBytes), true); |
halcanary | ac0e00d | 2016-07-27 11:12:23 -0700 | [diff] [blame] | 86 | auto stream = sk_make_sp<SkPDFStream>(std::move(streamData)); |
halcanary | 56586b6 | 2016-07-25 13:59:30 -0700 | [diff] [blame] | 87 | assert_emit_eq(reporter, |
halcanary | f7a169e | 2015-05-01 07:35:45 -0700 | [diff] [blame] | 88 | *stream, |
| 89 | "<</Length 12>> stream\nTest\nFoo\tBar\nendstream"); |
halcanary | fa25106 | 2016-07-29 10:13:18 -0700 | [diff] [blame] | 90 | stream->dict()->insertInt("Attribute", 42); |
halcanary | 56586b6 | 2016-07-25 13:59:30 -0700 | [diff] [blame] | 91 | assert_emit_eq(reporter, |
halcanary | f7a169e | 2015-05-01 07:35:45 -0700 | [diff] [blame] | 92 | *stream, |
| 93 | "<</Length 12\n/Attribute 42>> stream\n" |
| 94 | "Test\nFoo\tBar\nendstream"); |
vandebo@chromium.org | 421d644 | 2011-07-20 17:39:01 +0000 | [diff] [blame] | 95 | |
halcanary | 91d1d62 | 2015-02-17 14:43:06 -0800 | [diff] [blame] | 96 | { |
vandebo@chromium.org | 421d644 | 2011-07-20 17:39:01 +0000 | [diff] [blame] | 97 | char streamBytes2[] = "This is a longer string, so that compression " |
| 98 | "can do something with it. With shorter strings, " |
| 99 | "the short circuit logic cuts in and we end up " |
| 100 | "with an uncompressed string."; |
halcanary | fe8f0e0 | 2016-07-27 14:14:04 -0700 | [diff] [blame] | 101 | auto stream = sk_make_sp<SkPDFStream>( |
| 102 | SkData::MakeWithCopy(streamBytes2, strlen(streamBytes2))); |
vandebo@chromium.org | 421d644 | 2011-07-20 17:39:01 +0000 | [diff] [blame] | 103 | |
| 104 | SkDynamicMemoryWStream compressedByteStream; |
halcanary | 48305e8 | 2015-08-18 13:30:25 -0700 | [diff] [blame] | 105 | SkDeflateWStream deflateWStream(&compressedByteStream); |
| 106 | deflateWStream.write(streamBytes2, strlen(streamBytes2)); |
| 107 | deflateWStream.finalize(); |
vandebo@chromium.org | 421d644 | 2011-07-20 17:39:01 +0000 | [diff] [blame] | 108 | |
mtklein | cabc08c | 2015-02-19 08:29:24 -0800 | [diff] [blame] | 109 | SkDynamicMemoryWStream expected; |
halcanary | 37c46ca | 2015-03-31 12:30:20 -0700 | [diff] [blame] | 110 | expected.writeText("<</Filter /FlateDecode\n/Length 116>> stream\n"); |
halcanary | 48305e8 | 2015-08-18 13:30:25 -0700 | [diff] [blame] | 111 | compressedByteStream.writeToStream(&expected); |
| 112 | compressedByteStream.reset(); |
mtklein | cabc08c | 2015-02-19 08:29:24 -0800 | [diff] [blame] | 113 | expected.writeText("\nendstream"); |
reed | 42943c8 | 2016-09-12 12:01:44 -0700 | [diff] [blame] | 114 | sk_sp<SkData> expectedResultData2(expected.detachAsData()); |
halcanary | f7a169e | 2015-05-01 07:35:45 -0700 | [diff] [blame] | 115 | SkString result = emit_to_string(*stream); |
halcanary | 18e0cbc | 2016-08-03 14:17:53 -0700 | [diff] [blame] | 116 | #ifndef SK_PDF_LESS_COMPRESSION |
halcanary | 56586b6 | 2016-07-25 13:59:30 -0700 | [diff] [blame] | 117 | assert_eql(reporter, |
halcanary | f7a169e | 2015-05-01 07:35:45 -0700 | [diff] [blame] | 118 | result, |
| 119 | (const char*)expectedResultData2->data(), |
| 120 | expectedResultData2->size()); |
halcanary | 18e0cbc | 2016-08-03 14:17:53 -0700 | [diff] [blame] | 121 | #endif |
vandebo@chromium.org | 421d644 | 2011-07-20 17:39:01 +0000 | [diff] [blame] | 122 | } |
| 123 | } |
| 124 | |
halcanary | f7a169e | 2015-05-01 07:35:45 -0700 | [diff] [blame] | 125 | static void TestObjectNumberMap(skiatest::Reporter* reporter) { |
| 126 | SkPDFObjNumMap objNumMap; |
halcanary | 8103a34 | 2016-03-08 15:10:16 -0800 | [diff] [blame] | 127 | sk_sp<SkPDFArray> a1(new SkPDFArray); |
| 128 | sk_sp<SkPDFArray> a2(new SkPDFArray); |
| 129 | sk_sp<SkPDFArray> a3(new SkPDFArray); |
vandebo@chromium.org | 8459d4e | 2010-09-24 22:25:30 +0000 | [diff] [blame] | 130 | |
Hal Canary | c5b9498 | 2017-10-03 09:38:43 -0400 | [diff] [blame] | 131 | objNumMap.addObjectRecursively(a1.get()); |
| 132 | objNumMap.addObjectRecursively(a2.get()); |
| 133 | objNumMap.addObjectRecursively(a3.get()); |
vandebo@chromium.org | 8459d4e | 2010-09-24 22:25:30 +0000 | [diff] [blame] | 134 | |
halcanary | f7a169e | 2015-05-01 07:35:45 -0700 | [diff] [blame] | 135 | // The objects should be numbered in the order they are added, |
| 136 | // starting with 1. |
| 137 | REPORTER_ASSERT(reporter, objNumMap.getObjectNumber(a1.get()) == 1); |
| 138 | REPORTER_ASSERT(reporter, objNumMap.getObjectNumber(a2.get()) == 2); |
| 139 | REPORTER_ASSERT(reporter, objNumMap.getObjectNumber(a3.get()) == 3); |
| 140 | // Assert that repeated calls to get the object number return |
| 141 | // consistent result. |
| 142 | REPORTER_ASSERT(reporter, objNumMap.getObjectNumber(a1.get()) == 1); |
vandebo@chromium.org | 8459d4e | 2010-09-24 22:25:30 +0000 | [diff] [blame] | 143 | } |
| 144 | |
| 145 | static void TestObjectRef(skiatest::Reporter* reporter) { |
halcanary | 8103a34 | 2016-03-08 15:10:16 -0800 | [diff] [blame] | 146 | sk_sp<SkPDFArray> a1(new SkPDFArray); |
| 147 | sk_sp<SkPDFArray> a2(new SkPDFArray); |
| 148 | a2->appendObjRef(a1); |
vandebo@chromium.org | 8459d4e | 2010-09-24 22:25:30 +0000 | [diff] [blame] | 149 | |
halcanary | 530032a | 2016-08-18 14:22:52 -0700 | [diff] [blame] | 150 | SkPDFObjNumMap catalog; |
Hal Canary | c5b9498 | 2017-10-03 09:38:43 -0400 | [diff] [blame] | 151 | catalog.addObjectRecursively(a1.get()); |
halcanary | 530032a | 2016-08-18 14:22:52 -0700 | [diff] [blame] | 152 | REPORTER_ASSERT(reporter, catalog.getObjectNumber(a1.get()) == 1); |
vandebo@chromium.org | 8459d4e | 2010-09-24 22:25:30 +0000 | [diff] [blame] | 153 | |
halcanary | f7a169e | 2015-05-01 07:35:45 -0700 | [diff] [blame] | 154 | SkString result = emit_to_string(*a2, &catalog); |
| 155 | // If appendObjRef misbehaves, then the result would |
| 156 | // be [[]], not [1 0 R]. |
halcanary | 56586b6 | 2016-07-25 13:59:30 -0700 | [diff] [blame] | 157 | assert_eq(reporter, result, "[1 0 R]"); |
vandebo@chromium.org | 8459d4e | 2010-09-24 22:25:30 +0000 | [diff] [blame] | 158 | } |
| 159 | |
vandebo@chromium.org | 4e1cc6a | 2013-01-25 19:27:23 +0000 | [diff] [blame] | 160 | // This test used to assert without the fix submitted for |
| 161 | // http://code.google.com/p/skia/issues/detail?id=1083. |
| 162 | // SKP files might have invalid glyph ids. This test ensures they are ignored, |
| 163 | // and there is no assert on input data in Debug mode. |
sugoi@google.com | 54f0d1b | 2013-02-27 19:17:41 +0000 | [diff] [blame] | 164 | static void test_issue1083() { |
halcanary | 3d32d50 | 2015-03-01 06:55:20 -0800 | [diff] [blame] | 165 | SkDynamicMemoryWStream outStream; |
halcanary | 4b65666 | 2016-04-27 07:45:18 -0700 | [diff] [blame] | 166 | sk_sp<SkDocument> doc(SkDocument::MakePDF(&outStream)); |
halcanary | 3d32d50 | 2015-03-01 06:55:20 -0800 | [diff] [blame] | 167 | SkCanvas* canvas = doc->beginPage(100.0f, 100.0f); |
vandebo@chromium.org | 4e1cc6a | 2013-01-25 19:27:23 +0000 | [diff] [blame] | 168 | SkPaint paint; |
| 169 | paint.setTextEncoding(SkPaint::kGlyphID_TextEncoding); |
| 170 | |
| 171 | uint16_t glyphID = 65000; |
halcanary | 3d32d50 | 2015-03-01 06:55:20 -0800 | [diff] [blame] | 172 | canvas->drawText(&glyphID, 2, 0, 0, paint); |
vandebo@chromium.org | 4e1cc6a | 2013-01-25 19:27:23 +0000 | [diff] [blame] | 173 | |
halcanary | 3d32d50 | 2015-03-01 06:55:20 -0800 | [diff] [blame] | 174 | doc->close(); |
vandebo@chromium.org | 4e1cc6a | 2013-01-25 19:27:23 +0000 | [diff] [blame] | 175 | } |
| 176 | |
halcanary | 56586b6 | 2016-07-25 13:59:30 -0700 | [diff] [blame] | 177 | static void assert_emit_eq_number(skiatest::Reporter* reporter, float number) { |
| 178 | SkPDFUnion pdfUnion = SkPDFUnion::Scalar(number); |
| 179 | SkString result = emit_to_string(pdfUnion); |
| 180 | float value = static_cast<float>(std::atof(result.c_str())); |
| 181 | if (value != number) { |
| 182 | ERRORF(reporter, "%.9g != %s", number, result.c_str()); |
| 183 | } |
| 184 | } |
| 185 | |
| 186 | |
halcanary | f7a169e | 2015-05-01 07:35:45 -0700 | [diff] [blame] | 187 | static void TestPDFUnion(skiatest::Reporter* reporter) { |
| 188 | SkPDFUnion boolTrue = SkPDFUnion::Bool(true); |
halcanary | 56586b6 | 2016-07-25 13:59:30 -0700 | [diff] [blame] | 189 | assert_emit_eq(reporter, boolTrue, "true"); |
vandebo@chromium.org | 8459d4e | 2010-09-24 22:25:30 +0000 | [diff] [blame] | 190 | |
halcanary | f7a169e | 2015-05-01 07:35:45 -0700 | [diff] [blame] | 191 | SkPDFUnion boolFalse = SkPDFUnion::Bool(false); |
halcanary | 56586b6 | 2016-07-25 13:59:30 -0700 | [diff] [blame] | 192 | assert_emit_eq(reporter, boolFalse, "false"); |
vandebo@chromium.org | 8459d4e | 2010-09-24 22:25:30 +0000 | [diff] [blame] | 193 | |
halcanary | f7a169e | 2015-05-01 07:35:45 -0700 | [diff] [blame] | 194 | SkPDFUnion int42 = SkPDFUnion::Int(42); |
halcanary | 56586b6 | 2016-07-25 13:59:30 -0700 | [diff] [blame] | 195 | assert_emit_eq(reporter, int42, "42"); |
halcanary | f7a169e | 2015-05-01 07:35:45 -0700 | [diff] [blame] | 196 | |
halcanary | 56586b6 | 2016-07-25 13:59:30 -0700 | [diff] [blame] | 197 | assert_emit_eq_number(reporter, SK_ScalarHalf); |
| 198 | assert_emit_eq_number(reporter, 110999.75f); // bigScalar |
| 199 | assert_emit_eq_number(reporter, 50000000.1f); // biggerScalar |
| 200 | assert_emit_eq_number(reporter, 1.0f / 65536); // smallScalar |
vandebo@chromium.org | 094316b | 2011-03-04 03:15:13 +0000 | [diff] [blame] | 201 | |
halcanary | f7a169e | 2015-05-01 07:35:45 -0700 | [diff] [blame] | 202 | SkPDFUnion stringSimple = SkPDFUnion::String("test ) string ( foo"); |
halcanary | 56586b6 | 2016-07-25 13:59:30 -0700 | [diff] [blame] | 203 | assert_emit_eq(reporter, stringSimple, "(test \\) string \\( foo)"); |
vandebo@chromium.org | 8459d4e | 2010-09-24 22:25:30 +0000 | [diff] [blame] | 204 | |
halcanary | f7a169e | 2015-05-01 07:35:45 -0700 | [diff] [blame] | 205 | SkString stringComplexInput("\ttest ) string ( foo"); |
| 206 | SkPDFUnion stringComplex = SkPDFUnion::String(stringComplexInput); |
halcanary | 56586b6 | 2016-07-25 13:59:30 -0700 | [diff] [blame] | 207 | assert_emit_eq(reporter, stringComplex, "(\\011test \\) string \\( foo)"); |
halcanary | ee41b75 | 2016-06-23 14:08:11 -0700 | [diff] [blame] | 208 | |
| 209 | SkString binaryStringInput("\1\2\3\4\5\6\7\10\11\12\13\14\15\16\17\20"); |
| 210 | SkPDFUnion binaryString = SkPDFUnion::String(binaryStringInput); |
halcanary | 56586b6 | 2016-07-25 13:59:30 -0700 | [diff] [blame] | 211 | assert_emit_eq(reporter, binaryString, "<0102030405060708090A0B0C0D0E0F10>"); |
vandebo@chromium.org | 8459d4e | 2010-09-24 22:25:30 +0000 | [diff] [blame] | 212 | |
halcanary | f7a169e | 2015-05-01 07:35:45 -0700 | [diff] [blame] | 213 | SkString nameInput("Test name\twith#tab"); |
| 214 | SkPDFUnion name = SkPDFUnion::Name(nameInput); |
halcanary | 56586b6 | 2016-07-25 13:59:30 -0700 | [diff] [blame] | 215 | assert_emit_eq(reporter, name, "/Test#20name#09with#23tab"); |
halcanary | f7a169e | 2015-05-01 07:35:45 -0700 | [diff] [blame] | 216 | |
| 217 | SkString nameInput2("A#/%()<>[]{}B"); |
| 218 | SkPDFUnion name2 = SkPDFUnion::Name(nameInput2); |
halcanary | 56586b6 | 2016-07-25 13:59:30 -0700 | [diff] [blame] | 219 | assert_emit_eq(reporter, name2, "/A#23#2F#25#28#29#3C#3E#5B#5D#7B#7DB"); |
halcanary | f7a169e | 2015-05-01 07:35:45 -0700 | [diff] [blame] | 220 | |
| 221 | SkPDFUnion name3 = SkPDFUnion::Name("SimpleNameWithOnlyPrintableASCII"); |
halcanary | 56586b6 | 2016-07-25 13:59:30 -0700 | [diff] [blame] | 222 | assert_emit_eq(reporter, name3, "/SimpleNameWithOnlyPrintableASCII"); |
vandebo@chromium.org | 251a766 | 2012-09-21 17:50:50 +0000 | [diff] [blame] | 223 | |
vandebo@chromium.org | c0376fe | 2012-03-05 18:44:33 +0000 | [diff] [blame] | 224 | // Test that we correctly handle characters with the high-bit set. |
halcanary | f7a169e | 2015-05-01 07:35:45 -0700 | [diff] [blame] | 225 | SkString highBitString("\xDE\xAD" "be\xEF"); |
| 226 | SkPDFUnion highBitName = SkPDFUnion::Name(highBitString); |
halcanary | 56586b6 | 2016-07-25 13:59:30 -0700 | [diff] [blame] | 227 | assert_emit_eq(reporter, highBitName, "/#DE#ADbe#EF"); |
halcanary | f7a169e | 2015-05-01 07:35:45 -0700 | [diff] [blame] | 228 | } |
vandebo@chromium.org | c0376fe | 2012-03-05 18:44:33 +0000 | [diff] [blame] | 229 | |
halcanary | f7a169e | 2015-05-01 07:35:45 -0700 | [diff] [blame] | 230 | static void TestPDFArray(skiatest::Reporter* reporter) { |
halcanary | 8103a34 | 2016-03-08 15:10:16 -0800 | [diff] [blame] | 231 | sk_sp<SkPDFArray> array(new SkPDFArray); |
halcanary | 56586b6 | 2016-07-25 13:59:30 -0700 | [diff] [blame] | 232 | assert_emit_eq(reporter, *array, "[]"); |
vandebo@chromium.org | 8459d4e | 2010-09-24 22:25:30 +0000 | [diff] [blame] | 233 | |
halcanary | f7a169e | 2015-05-01 07:35:45 -0700 | [diff] [blame] | 234 | array->appendInt(42); |
halcanary | 56586b6 | 2016-07-25 13:59:30 -0700 | [diff] [blame] | 235 | assert_emit_eq(reporter, *array, "[42]"); |
halcanary | f7a169e | 2015-05-01 07:35:45 -0700 | [diff] [blame] | 236 | |
| 237 | array->appendScalar(SK_ScalarHalf); |
halcanary | 56586b6 | 2016-07-25 13:59:30 -0700 | [diff] [blame] | 238 | assert_emit_eq(reporter, *array, "[42 .5]"); |
halcanary | f7a169e | 2015-05-01 07:35:45 -0700 | [diff] [blame] | 239 | |
| 240 | array->appendInt(0); |
halcanary | 56586b6 | 2016-07-25 13:59:30 -0700 | [diff] [blame] | 241 | assert_emit_eq(reporter, *array, "[42 .5 0]"); |
halcanary | f7a169e | 2015-05-01 07:35:45 -0700 | [diff] [blame] | 242 | |
| 243 | array->appendBool(true); |
halcanary | 56586b6 | 2016-07-25 13:59:30 -0700 | [diff] [blame] | 244 | assert_emit_eq(reporter, *array, "[42 .5 0 true]"); |
halcanary | f7a169e | 2015-05-01 07:35:45 -0700 | [diff] [blame] | 245 | |
| 246 | array->appendName("ThisName"); |
halcanary | 56586b6 | 2016-07-25 13:59:30 -0700 | [diff] [blame] | 247 | assert_emit_eq(reporter, *array, "[42 .5 0 true /ThisName]"); |
halcanary | f7a169e | 2015-05-01 07:35:45 -0700 | [diff] [blame] | 248 | |
| 249 | array->appendName(SkString("AnotherName")); |
halcanary | 56586b6 | 2016-07-25 13:59:30 -0700 | [diff] [blame] | 250 | assert_emit_eq(reporter, *array, "[42 .5 0 true /ThisName /AnotherName]"); |
halcanary | f7a169e | 2015-05-01 07:35:45 -0700 | [diff] [blame] | 251 | |
| 252 | array->appendString("This String"); |
halcanary | 56586b6 | 2016-07-25 13:59:30 -0700 | [diff] [blame] | 253 | assert_emit_eq(reporter, *array, |
halcanary | 8e9f5e3 | 2016-02-24 15:46:46 -0800 | [diff] [blame] | 254 | "[42 .5 0 true /ThisName /AnotherName (This String)]"); |
halcanary | f7a169e | 2015-05-01 07:35:45 -0700 | [diff] [blame] | 255 | |
| 256 | array->appendString(SkString("Another String")); |
halcanary | 56586b6 | 2016-07-25 13:59:30 -0700 | [diff] [blame] | 257 | assert_emit_eq(reporter, *array, |
halcanary | 8e9f5e3 | 2016-02-24 15:46:46 -0800 | [diff] [blame] | 258 | "[42 .5 0 true /ThisName /AnotherName (This String) " |
halcanary | f7a169e | 2015-05-01 07:35:45 -0700 | [diff] [blame] | 259 | "(Another String)]"); |
| 260 | |
halcanary | 8103a34 | 2016-03-08 15:10:16 -0800 | [diff] [blame] | 261 | sk_sp<SkPDFArray> innerArray(new SkPDFArray); |
halcanary | f7a169e | 2015-05-01 07:35:45 -0700 | [diff] [blame] | 262 | innerArray->appendInt(-1); |
halcanary | 8103a34 | 2016-03-08 15:10:16 -0800 | [diff] [blame] | 263 | array->appendObject(std::move(innerArray)); |
halcanary | 56586b6 | 2016-07-25 13:59:30 -0700 | [diff] [blame] | 264 | assert_emit_eq(reporter, *array, |
halcanary | 8e9f5e3 | 2016-02-24 15:46:46 -0800 | [diff] [blame] | 265 | "[42 .5 0 true /ThisName /AnotherName (This String) " |
halcanary | f7a169e | 2015-05-01 07:35:45 -0700 | [diff] [blame] | 266 | "(Another String) [-1]]"); |
| 267 | |
halcanary | 8103a34 | 2016-03-08 15:10:16 -0800 | [diff] [blame] | 268 | sk_sp<SkPDFArray> referencedArray(new SkPDFArray); |
halcanary | 530032a | 2016-08-18 14:22:52 -0700 | [diff] [blame] | 269 | SkPDFObjNumMap catalog; |
Hal Canary | c5b9498 | 2017-10-03 09:38:43 -0400 | [diff] [blame] | 270 | catalog.addObjectRecursively(referencedArray.get()); |
halcanary | 530032a | 2016-08-18 14:22:52 -0700 | [diff] [blame] | 271 | REPORTER_ASSERT(reporter, catalog.getObjectNumber( |
halcanary | f7a169e | 2015-05-01 07:35:45 -0700 | [diff] [blame] | 272 | referencedArray.get()) == 1); |
halcanary | 8103a34 | 2016-03-08 15:10:16 -0800 | [diff] [blame] | 273 | array->appendObjRef(std::move(referencedArray)); |
halcanary | f7a169e | 2015-05-01 07:35:45 -0700 | [diff] [blame] | 274 | |
| 275 | SkString result = emit_to_string(*array, &catalog); |
halcanary | 56586b6 | 2016-07-25 13:59:30 -0700 | [diff] [blame] | 276 | assert_eq(reporter, result, |
halcanary | 8e9f5e3 | 2016-02-24 15:46:46 -0800 | [diff] [blame] | 277 | "[42 .5 0 true /ThisName /AnotherName (This String) " |
halcanary | f7a169e | 2015-05-01 07:35:45 -0700 | [diff] [blame] | 278 | "(Another String) [-1] 1 0 R]"); |
| 279 | } |
| 280 | |
| 281 | static void TestPDFDict(skiatest::Reporter* reporter) { |
halcanary | 8103a34 | 2016-03-08 15:10:16 -0800 | [diff] [blame] | 282 | sk_sp<SkPDFDict> dict(new SkPDFDict); |
halcanary | 56586b6 | 2016-07-25 13:59:30 -0700 | [diff] [blame] | 283 | assert_emit_eq(reporter, *dict, "<<>>"); |
halcanary | f7a169e | 2015-05-01 07:35:45 -0700 | [diff] [blame] | 284 | |
| 285 | dict->insertInt("n1", SkToSizeT(42)); |
halcanary | 56586b6 | 2016-07-25 13:59:30 -0700 | [diff] [blame] | 286 | assert_emit_eq(reporter, *dict, "<</n1 42>>"); |
halcanary | f7a169e | 2015-05-01 07:35:45 -0700 | [diff] [blame] | 287 | |
| 288 | dict.reset(new SkPDFDict); |
halcanary | 56586b6 | 2016-07-25 13:59:30 -0700 | [diff] [blame] | 289 | assert_emit_eq(reporter, *dict, "<<>>"); |
halcanary | f7a169e | 2015-05-01 07:35:45 -0700 | [diff] [blame] | 290 | |
| 291 | dict->insertInt("n1", 42); |
halcanary | 56586b6 | 2016-07-25 13:59:30 -0700 | [diff] [blame] | 292 | assert_emit_eq(reporter, *dict, "<</n1 42>>"); |
halcanary | f7a169e | 2015-05-01 07:35:45 -0700 | [diff] [blame] | 293 | |
| 294 | dict->insertScalar("n2", SK_ScalarHalf); |
| 295 | |
halcanary | 130444f | 2015-04-25 06:45:07 -0700 | [diff] [blame] | 296 | SkString n3("n3"); |
halcanary | 8103a34 | 2016-03-08 15:10:16 -0800 | [diff] [blame] | 297 | sk_sp<SkPDFArray> innerArray(new SkPDFArray); |
halcanary | f7a169e | 2015-05-01 07:35:45 -0700 | [diff] [blame] | 298 | innerArray->appendInt(-100); |
halcanary | 8103a34 | 2016-03-08 15:10:16 -0800 | [diff] [blame] | 299 | dict->insertObject(n3, std::move(innerArray)); |
halcanary | 56586b6 | 2016-07-25 13:59:30 -0700 | [diff] [blame] | 300 | assert_emit_eq(reporter, *dict, "<</n1 42\n/n2 .5\n/n3 [-100]>>"); |
vandebo@chromium.org | 8459d4e | 2010-09-24 22:25:30 +0000 | [diff] [blame] | 301 | |
halcanary | f7a169e | 2015-05-01 07:35:45 -0700 | [diff] [blame] | 302 | dict.reset(new SkPDFDict); |
halcanary | 56586b6 | 2016-07-25 13:59:30 -0700 | [diff] [blame] | 303 | assert_emit_eq(reporter, *dict, "<<>>"); |
halcanary | f7a169e | 2015-05-01 07:35:45 -0700 | [diff] [blame] | 304 | |
| 305 | dict->insertInt("n1", 24); |
halcanary | 56586b6 | 2016-07-25 13:59:30 -0700 | [diff] [blame] | 306 | assert_emit_eq(reporter, *dict, "<</n1 24>>"); |
halcanary | f7a169e | 2015-05-01 07:35:45 -0700 | [diff] [blame] | 307 | |
| 308 | dict->insertInt("n2", SkToSizeT(99)); |
halcanary | 56586b6 | 2016-07-25 13:59:30 -0700 | [diff] [blame] | 309 | assert_emit_eq(reporter, *dict, "<</n1 24\n/n2 99>>"); |
halcanary | f7a169e | 2015-05-01 07:35:45 -0700 | [diff] [blame] | 310 | |
| 311 | dict->insertScalar("n3", SK_ScalarHalf); |
halcanary | 56586b6 | 2016-07-25 13:59:30 -0700 | [diff] [blame] | 312 | assert_emit_eq(reporter, *dict, "<</n1 24\n/n2 99\n/n3 .5>>"); |
halcanary | f7a169e | 2015-05-01 07:35:45 -0700 | [diff] [blame] | 313 | |
| 314 | dict->insertName("n4", "AName"); |
halcanary | 56586b6 | 2016-07-25 13:59:30 -0700 | [diff] [blame] | 315 | assert_emit_eq(reporter, *dict, "<</n1 24\n/n2 99\n/n3 .5\n/n4 /AName>>"); |
halcanary | f7a169e | 2015-05-01 07:35:45 -0700 | [diff] [blame] | 316 | |
| 317 | dict->insertName("n5", SkString("AnotherName")); |
halcanary | 56586b6 | 2016-07-25 13:59:30 -0700 | [diff] [blame] | 318 | assert_emit_eq(reporter, *dict, "<</n1 24\n/n2 99\n/n3 .5\n/n4 /AName\n" |
halcanary | f7a169e | 2015-05-01 07:35:45 -0700 | [diff] [blame] | 319 | "/n5 /AnotherName>>"); |
| 320 | |
| 321 | dict->insertString("n6", "A String"); |
halcanary | 56586b6 | 2016-07-25 13:59:30 -0700 | [diff] [blame] | 322 | assert_emit_eq(reporter, *dict, "<</n1 24\n/n2 99\n/n3 .5\n/n4 /AName\n" |
halcanary | f7a169e | 2015-05-01 07:35:45 -0700 | [diff] [blame] | 323 | "/n5 /AnotherName\n/n6 (A String)>>"); |
| 324 | |
| 325 | dict->insertString("n7", SkString("Another String")); |
halcanary | 56586b6 | 2016-07-25 13:59:30 -0700 | [diff] [blame] | 326 | assert_emit_eq(reporter, *dict, "<</n1 24\n/n2 99\n/n3 .5\n/n4 /AName\n" |
halcanary | f7a169e | 2015-05-01 07:35:45 -0700 | [diff] [blame] | 327 | "/n5 /AnotherName\n/n6 (A String)\n/n7 (Another String)>>"); |
| 328 | |
| 329 | dict.reset(new SkPDFDict("DType")); |
halcanary | 56586b6 | 2016-07-25 13:59:30 -0700 | [diff] [blame] | 330 | assert_emit_eq(reporter, *dict, "<</Type /DType>>"); |
halcanary | 8e9f5e3 | 2016-02-24 15:46:46 -0800 | [diff] [blame] | 331 | |
halcanary | 8103a34 | 2016-03-08 15:10:16 -0800 | [diff] [blame] | 332 | sk_sp<SkPDFArray> referencedArray(new SkPDFArray); |
halcanary | 530032a | 2016-08-18 14:22:52 -0700 | [diff] [blame] | 333 | SkPDFObjNumMap catalog; |
Hal Canary | c5b9498 | 2017-10-03 09:38:43 -0400 | [diff] [blame] | 334 | catalog.addObjectRecursively(referencedArray.get()); |
halcanary | 530032a | 2016-08-18 14:22:52 -0700 | [diff] [blame] | 335 | REPORTER_ASSERT(reporter, catalog.getObjectNumber( |
halcanary | f7a169e | 2015-05-01 07:35:45 -0700 | [diff] [blame] | 336 | referencedArray.get()) == 1); |
halcanary | 8103a34 | 2016-03-08 15:10:16 -0800 | [diff] [blame] | 337 | dict->insertObjRef("n1", std::move(referencedArray)); |
halcanary | f7a169e | 2015-05-01 07:35:45 -0700 | [diff] [blame] | 338 | SkString result = emit_to_string(*dict, &catalog); |
halcanary | 56586b6 | 2016-07-25 13:59:30 -0700 | [diff] [blame] | 339 | assert_eq(reporter, result, "<</Type /DType\n/n1 1 0 R>>"); |
halcanary | f7a169e | 2015-05-01 07:35:45 -0700 | [diff] [blame] | 340 | } |
| 341 | |
halcanary | 57f744e | 2016-09-09 11:41:59 -0700 | [diff] [blame] | 342 | DEF_TEST(SkPDF_Primitives, reporter) { |
halcanary | f7a169e | 2015-05-01 07:35:45 -0700 | [diff] [blame] | 343 | TestPDFUnion(reporter); |
| 344 | TestPDFArray(reporter); |
| 345 | TestPDFDict(reporter); |
vandebo@chromium.org | 421d644 | 2011-07-20 17:39:01 +0000 | [diff] [blame] | 346 | TestPDFStream(reporter); |
halcanary | f7a169e | 2015-05-01 07:35:45 -0700 | [diff] [blame] | 347 | TestObjectNumberMap(reporter); |
vandebo@chromium.org | 8459d4e | 2010-09-24 22:25:30 +0000 | [diff] [blame] | 348 | TestObjectRef(reporter); |
sugoi@google.com | 54f0d1b | 2013-02-27 19:17:41 +0000 | [diff] [blame] | 349 | test_issue1083(); |
vandebo@chromium.org | 8459d4e | 2010-09-24 22:25:30 +0000 | [diff] [blame] | 350 | } |
senorblanco | b0e89dc | 2014-10-20 14:03:12 -0700 | [diff] [blame] | 351 | |
| 352 | namespace { |
| 353 | |
| 354 | class DummyImageFilter : public SkImageFilter { |
| 355 | public: |
robertphillips | 43c2ad4 | 2016-04-04 05:05:11 -0700 | [diff] [blame] | 356 | static sk_sp<DummyImageFilter> Make(bool visited = false) { |
| 357 | return sk_sp<DummyImageFilter>(new DummyImageFilter(visited)); |
| 358 | } |
| 359 | |
Cary Clark | 9988541 | 2018-04-05 13:09:58 -0400 | [diff] [blame] | 360 | void toString(SkString* str) const override; |
robertphillips | 43c2ad4 | 2016-04-04 05:05:11 -0700 | [diff] [blame] | 361 | SK_DECLARE_PUBLIC_FLATTENABLE_DESERIALIZATION_PROCS(DummyImageFilter) |
| 362 | bool visited() const { return fVisited; } |
| 363 | |
| 364 | protected: |
robertphillips | 4ba94e2 | 2016-04-04 12:07:47 -0700 | [diff] [blame] | 365 | sk_sp<SkSpecialImage> onFilterImage(SkSpecialImage* source, const Context&, |
| 366 | SkIPoint* offset) const override { |
senorblanco | b0e89dc | 2014-10-20 14:03:12 -0700 | [diff] [blame] | 367 | fVisited = true; |
| 368 | offset->fX = offset->fY = 0; |
robertphillips | 4ba94e2 | 2016-04-04 12:07:47 -0700 | [diff] [blame] | 369 | return sk_ref_sp<SkSpecialImage>(source); |
senorblanco | b0e89dc | 2014-10-20 14:03:12 -0700 | [diff] [blame] | 370 | } |
Matt Sarett | 62745a8 | 2017-04-17 11:57:29 -0400 | [diff] [blame] | 371 | sk_sp<SkImageFilter> onMakeColorSpace(SkColorSpaceXformer*) const override { |
| 372 | return sk_ref_sp(const_cast<DummyImageFilter*>(this)); |
| 373 | } |
senorblanco | b0e89dc | 2014-10-20 14:03:12 -0700 | [diff] [blame] | 374 | |
| 375 | private: |
robertphillips | 43c2ad4 | 2016-04-04 05:05:11 -0700 | [diff] [blame] | 376 | DummyImageFilter(bool visited) : INHERITED(nullptr, 0, nullptr), fVisited(visited) {} |
| 377 | |
senorblanco | b0e89dc | 2014-10-20 14:03:12 -0700 | [diff] [blame] | 378 | mutable bool fVisited; |
robertphillips | 43c2ad4 | 2016-04-04 05:05:11 -0700 | [diff] [blame] | 379 | |
| 380 | typedef SkImageFilter INHERITED; |
senorblanco | b0e89dc | 2014-10-20 14:03:12 -0700 | [diff] [blame] | 381 | }; |
| 382 | |
reed | 60c9b58 | 2016-04-03 09:11:13 -0700 | [diff] [blame] | 383 | sk_sp<SkFlattenable> DummyImageFilter::CreateProc(SkReadBuffer& buffer) { |
senorblanco | b0e89dc | 2014-10-20 14:03:12 -0700 | [diff] [blame] | 384 | SK_IMAGEFILTER_UNFLATTEN_COMMON(common, 0); |
| 385 | bool visited = buffer.readBool(); |
robertphillips | 43c2ad4 | 2016-04-04 05:05:11 -0700 | [diff] [blame] | 386 | return DummyImageFilter::Make(visited); |
senorblanco | b0e89dc | 2014-10-20 14:03:12 -0700 | [diff] [blame] | 387 | } |
| 388 | |
robertphillips | f3f5bad | 2014-12-19 13:49:15 -0800 | [diff] [blame] | 389 | void DummyImageFilter::toString(SkString* str) const { |
| 390 | str->appendf("DummyImageFilter: ("); |
| 391 | str->append(")"); |
| 392 | } |
robertphillips | f3f5bad | 2014-12-19 13:49:15 -0800 | [diff] [blame] | 393 | |
senorblanco | b0e89dc | 2014-10-20 14:03:12 -0700 | [diff] [blame] | 394 | }; |
| 395 | |
| 396 | // Check that PDF rendering of image filters successfully falls back to |
| 397 | // CPU rasterization. |
halcanary | 57f744e | 2016-09-09 11:41:59 -0700 | [diff] [blame] | 398 | DEF_TEST(SkPDF_ImageFilter, reporter) { |
| 399 | REQUIRE_PDF_DOCUMENT(SkPDF_ImageFilter, reporter); |
halcanary | 3d32d50 | 2015-03-01 06:55:20 -0800 | [diff] [blame] | 400 | SkDynamicMemoryWStream stream; |
halcanary | 4b65666 | 2016-04-27 07:45:18 -0700 | [diff] [blame] | 401 | sk_sp<SkDocument> doc(SkDocument::MakePDF(&stream)); |
halcanary | 3d32d50 | 2015-03-01 06:55:20 -0800 | [diff] [blame] | 402 | SkCanvas* canvas = doc->beginPage(100.0f, 100.0f); |
| 403 | |
robertphillips | 43c2ad4 | 2016-04-04 05:05:11 -0700 | [diff] [blame] | 404 | sk_sp<DummyImageFilter> filter(DummyImageFilter::Make()); |
senorblanco | b0e89dc | 2014-10-20 14:03:12 -0700 | [diff] [blame] | 405 | |
| 406 | // Filter just created; should be unvisited. |
| 407 | REPORTER_ASSERT(reporter, !filter->visited()); |
| 408 | SkPaint paint; |
Mike Reed | 5e25717 | 2016-11-01 11:22:05 -0400 | [diff] [blame] | 409 | paint.setImageFilter(filter); |
halcanary | 3d32d50 | 2015-03-01 06:55:20 -0800 | [diff] [blame] | 410 | canvas->drawRect(SkRect::MakeWH(100, 100), paint); |
| 411 | doc->close(); |
senorblanco | b0e89dc | 2014-10-20 14:03:12 -0700 | [diff] [blame] | 412 | |
| 413 | // Filter was used in rendering; should be visited. |
| 414 | REPORTER_ASSERT(reporter, filter->visited()); |
| 415 | } |
halcanary | 66a82f3 | 2015-10-12 13:05:04 -0700 | [diff] [blame] | 416 | |
| 417 | // Check that PDF rendering of image filters successfully falls back to |
| 418 | // CPU rasterization. |
halcanary | 57f744e | 2016-09-09 11:41:59 -0700 | [diff] [blame] | 419 | DEF_TEST(SkPDF_FontCanEmbedTypeface, reporter) { |
halcanary | 66a82f3 | 2015-10-12 13:05:04 -0700 | [diff] [blame] | 420 | SkPDFCanon canon; |
| 421 | |
| 422 | const char resource[] = "fonts/Roboto2-Regular_NoEmbed.ttf"; |
bungeman | 13b9c95 | 2016-05-12 10:09:30 -0700 | [diff] [blame] | 423 | sk_sp<SkTypeface> noEmbedTypeface(MakeResourceAsTypeface(resource)); |
halcanary | 66a82f3 | 2015-10-12 13:05:04 -0700 | [diff] [blame] | 424 | if (noEmbedTypeface) { |
| 425 | REPORTER_ASSERT(reporter, |
halcanary | 8103a34 | 2016-03-08 15:10:16 -0800 | [diff] [blame] | 426 | !SkPDFFont::CanEmbedTypeface(noEmbedTypeface.get(), &canon)); |
halcanary | 66a82f3 | 2015-10-12 13:05:04 -0700 | [diff] [blame] | 427 | } |
halcanary | 8103a34 | 2016-03-08 15:10:16 -0800 | [diff] [blame] | 428 | sk_sp<SkTypeface> portableTypeface( |
Ben Wagner | a93a14a | 2017-08-28 10:34:05 -0400 | [diff] [blame] | 429 | sk_tool_utils::create_portable_typeface(nullptr, SkFontStyle())); |
halcanary | 66a82f3 | 2015-10-12 13:05:04 -0700 | [diff] [blame] | 430 | REPORTER_ASSERT(reporter, |
halcanary | 8103a34 | 2016-03-08 15:10:16 -0800 | [diff] [blame] | 431 | SkPDFFont::CanEmbedTypeface(portableTypeface.get(), &canon)); |
halcanary | 66a82f3 | 2015-10-12 13:05:04 -0700 | [diff] [blame] | 432 | } |
halcanary | 8e9f5e3 | 2016-02-24 15:46:46 -0800 | [diff] [blame] | 433 | |
| 434 | |
| 435 | // test to see that all finite scalars round trip via scanf(). |
| 436 | static void check_pdf_scalar_serialization( |
| 437 | skiatest::Reporter* reporter, float inputFloat) { |
Hal Canary | 3c36ef6 | 2018-01-02 16:25:53 -0500 | [diff] [blame] | 438 | char floatString[kMaximumSkFloatToDecimalLength]; |
| 439 | size_t len = SkFloatToDecimal(inputFloat, floatString); |
halcanary | 8e9f5e3 | 2016-02-24 15:46:46 -0800 | [diff] [blame] | 440 | if (len >= sizeof(floatString)) { |
| 441 | ERRORF(reporter, "string too long: %u", (unsigned)len); |
| 442 | return; |
| 443 | } |
| 444 | if (floatString[len] != '\0' || strlen(floatString) != len) { |
| 445 | ERRORF(reporter, "terminator misplaced."); |
| 446 | return; // The terminator is needed for sscanf(). |
| 447 | } |
| 448 | if (reporter->verbose()) { |
| 449 | SkDebugf("%15.9g = \"%s\"\n", inputFloat, floatString); |
| 450 | } |
| 451 | float roundTripFloat; |
| 452 | if (1 != sscanf(floatString, "%f", &roundTripFloat)) { |
| 453 | ERRORF(reporter, "unscannable result: %s", floatString); |
| 454 | return; |
| 455 | } |
halcanary | 56586b6 | 2016-07-25 13:59:30 -0700 | [diff] [blame] | 456 | if (std::isfinite(inputFloat) && roundTripFloat != inputFloat) { |
halcanary | 8e9f5e3 | 2016-02-24 15:46:46 -0800 | [diff] [blame] | 457 | ERRORF(reporter, "roundTripFloat (%.9g) != inputFloat (%.9g)", |
| 458 | roundTripFloat, inputFloat); |
| 459 | } |
| 460 | } |
| 461 | |
| 462 | // Test SkPDFUtils::AppendScalar for accuracy. |
halcanary | 57f744e | 2016-09-09 11:41:59 -0700 | [diff] [blame] | 463 | DEF_TEST(SkPDF_Primitives_Scalar, reporter) { |
halcanary | 8e9f5e3 | 2016-02-24 15:46:46 -0800 | [diff] [blame] | 464 | SkRandom random(0x5EED); |
| 465 | int iterationCount = 512; |
| 466 | while (iterationCount-- > 0) { |
| 467 | union { uint32_t u; float f; }; |
| 468 | u = random.nextU(); |
| 469 | static_assert(sizeof(float) == sizeof(uint32_t), ""); |
| 470 | check_pdf_scalar_serialization(reporter, f); |
| 471 | } |
| 472 | float alwaysCheck[] = { |
| 473 | 0.0f, -0.0f, 1.0f, -1.0f, SK_ScalarPI, 0.1f, FLT_MIN, FLT_MAX, |
| 474 | -FLT_MIN, -FLT_MAX, FLT_MIN / 16.0f, -FLT_MIN / 16.0f, |
| 475 | SK_FloatNaN, SK_FloatInfinity, SK_FloatNegativeInfinity, |
| 476 | -FLT_MIN / 8388608.0 |
| 477 | }; |
| 478 | for (float inputFloat: alwaysCheck) { |
| 479 | check_pdf_scalar_serialization(reporter, inputFloat); |
| 480 | } |
| 481 | } |
halcanary | eb92cb3 | 2016-07-15 13:41:27 -0700 | [diff] [blame] | 482 | |
| 483 | // Test SkPDFUtils:: for accuracy. |
halcanary | 57f744e | 2016-09-09 11:41:59 -0700 | [diff] [blame] | 484 | DEF_TEST(SkPDF_Primitives_Color, reporter) { |
halcanary | eb92cb3 | 2016-07-15 13:41:27 -0700 | [diff] [blame] | 485 | char buffer[5]; |
| 486 | for (int i = 0; i < 256; ++i) { |
| 487 | size_t len = SkPDFUtils::ColorToDecimal(i, buffer); |
| 488 | REPORTER_ASSERT(reporter, len == strlen(buffer)); |
| 489 | float f; |
| 490 | REPORTER_ASSERT(reporter, 1 == sscanf(buffer, "%f", &f)); |
| 491 | int roundTrip = (int)(0.5 + f * 255); |
| 492 | REPORTER_ASSERT(reporter, roundTrip == i); |
| 493 | } |
| 494 | } |
Hal Canary | 1521c8a | 2018-03-28 09:51:00 -0700 | [diff] [blame] | 495 | |
| 496 | DEF_TEST(SkPDF_Clusterator, reporter) { |
Hal Canary | 1521c8a | 2018-03-28 09:51:00 -0700 | [diff] [blame] | 497 | SkPaint paint; |
| 498 | paint.setTextEncoding(SkPaint::kGlyphID_TextEncoding); |
Hal Canary | e9e19dd | 2018-04-02 11:36:39 -0400 | [diff] [blame] | 499 | { |
| 500 | const uint32_t clusters[11] = { 3, 2, 2, 1, 0, 4, 4, 7, 6, 6, 5 }; |
| 501 | const SkGlyphID glyphs[11] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11}; |
| 502 | const char text[] = "abcdefgh"; |
| 503 | SkClusterator clusterator(glyphs, sizeof(glyphs), paint, clusters, strlen(text), text); |
| 504 | SkClusterator::Cluster expectations[] = { |
| 505 | {&text[3], 1, 0, 1}, |
| 506 | {&text[2], 1, 1, 2}, |
| 507 | {&text[1], 1, 3, 1}, |
| 508 | {&text[0], 1, 4, 1}, |
| 509 | {&text[4], 1, 5, 2}, |
| 510 | {&text[7], 1, 7, 1}, |
| 511 | {&text[6], 1, 8, 2}, |
| 512 | {&text[5], 1, 10, 1}, |
| 513 | {nullptr, 0, 0, 0}, |
| 514 | }; |
| 515 | for (const auto& expectation : expectations) { |
| 516 | REPORTER_ASSERT(reporter, clusterator.next() == expectation); |
| 517 | } |
| 518 | } |
| 519 | { |
| 520 | const uint32_t clusters[5] = { 0, 1, 4, 5, 6 }; |
| 521 | const SkGlyphID glyphs[5] = { 43, 167, 79, 79, 82, }; |
| 522 | const char text[] = "Ha\xCC\x8A" "llo"; |
| 523 | SkClusterator clusterator(glyphs, sizeof(glyphs), paint, clusters, strlen(text), text); |
| 524 | SkClusterator::Cluster expectations[] = { |
| 525 | {&text[0], 1, 0, 1}, |
| 526 | {&text[1], 3, 1, 1}, |
| 527 | {&text[4], 1, 2, 1}, |
| 528 | {&text[5], 1, 3, 1}, |
| 529 | {&text[6], 1, 4, 1}, |
| 530 | {nullptr, 0, 0, 0}, |
| 531 | }; |
| 532 | for (const auto& expectation : expectations) { |
| 533 | REPORTER_ASSERT(reporter, clusterator.next() == expectation); |
| 534 | } |
Hal Canary | 1521c8a | 2018-03-28 09:51:00 -0700 | [diff] [blame] | 535 | } |
| 536 | } |
| 537 | |
Hal Canary | 43fb7a0 | 2016-12-30 13:09:03 -0500 | [diff] [blame] | 538 | #endif |