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 | |
halcanary | 66a82f3 | 2015-10-12 13:05:04 -0700 | [diff] [blame] | 8 | #include "Resources.h" |
edisonn@google.com | d9dfa18 | 2013-04-24 13:01:01 +0000 | [diff] [blame] | 9 | #include "SkBitmap.h" |
vandebo@chromium.org | 4e1cc6a | 2013-01-25 19:27:23 +0000 | [diff] [blame] | 10 | #include "SkCanvas.h" |
reed@google.com | 8a85d0c | 2011-06-24 19:12:12 +0000 | [diff] [blame] | 11 | #include "SkData.h" |
halcanary | 3d32d50 | 2015-03-01 06:55:20 -0800 | [diff] [blame] | 12 | #include "SkDocument.h" |
halcanary | d9e5715 | 2015-08-12 11:24:40 -0700 | [diff] [blame] | 13 | #include "SkDeflate.h" |
edisonn@google.com | d9dfa18 | 2013-04-24 13:01:01 +0000 | [diff] [blame] | 14 | #include "SkImageEncoder.h" |
| 15 | #include "SkMatrix.h" |
halcanary | a1f1ee9 | 2015-02-20 06:17:26 -0800 | [diff] [blame] | 16 | #include "SkPDFCanon.h" |
vandebo@chromium.org | 4e1cc6a | 2013-01-25 19:27:23 +0000 | [diff] [blame] | 17 | #include "SkPDFDevice.h" |
halcanary | 66a82f3 | 2015-10-12 13:05:04 -0700 | [diff] [blame] | 18 | #include "SkPDFFont.h" |
vandebo@chromium.org | 8459d4e | 2010-09-24 22:25:30 +0000 | [diff] [blame] | 19 | #include "SkPDFStream.h" |
| 20 | #include "SkPDFTypes.h" |
halcanary | 8e9f5e3 | 2016-02-24 15:46:46 -0800 | [diff] [blame] | 21 | #include "SkPDFUtils.h" |
senorblanco | b0e89dc | 2014-10-20 14:03:12 -0700 | [diff] [blame] | 22 | #include "SkReadBuffer.h" |
vandebo@chromium.org | 8459d4e | 2010-09-24 22:25:30 +0000 | [diff] [blame] | 23 | #include "SkScalar.h" |
robertphillips | 4ba94e2 | 2016-04-04 12:07:47 -0700 | [diff] [blame] | 24 | #include "SkSpecialImage.h" |
vandebo@chromium.org | 8459d4e | 2010-09-24 22:25:30 +0000 | [diff] [blame] | 25 | #include "SkStream.h" |
vandebo@chromium.org | 421d644 | 2011-07-20 17:39:01 +0000 | [diff] [blame] | 26 | #include "SkTypes.h" |
tfarina@chromium.org | 8f6884a | 2014-01-24 20:56:26 +0000 | [diff] [blame] | 27 | #include "Test.h" |
halcanary | 66a82f3 | 2015-10-12 13:05:04 -0700 | [diff] [blame] | 28 | #include "sk_tool_utils.h" |
vandebo@chromium.org | 8459d4e | 2010-09-24 22:25:30 +0000 | [diff] [blame] | 29 | |
commit-bot@chromium.org | 608ea65 | 2013-10-03 19:29:21 +0000 | [diff] [blame] | 30 | #define DUMMY_TEXT "DCT compessed stream." |
| 31 | |
halcanary | f7a169e | 2015-05-01 07:35:45 -0700 | [diff] [blame] | 32 | namespace { |
| 33 | struct Catalog { |
| 34 | SkPDFSubstituteMap substitutes; |
| 35 | SkPDFObjNumMap numbers; |
| 36 | }; |
| 37 | } // namespace |
reed@google.com | 8a85d0c | 2011-06-24 19:12:12 +0000 | [diff] [blame] | 38 | |
halcanary | f7a169e | 2015-05-01 07:35:45 -0700 | [diff] [blame] | 39 | template <typename T> |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 40 | static SkString emit_to_string(T& obj, Catalog* catPtr = nullptr) { |
halcanary | f7a169e | 2015-05-01 07:35:45 -0700 | [diff] [blame] | 41 | Catalog catalog; |
halcanary | 6a14434 | 2015-01-23 11:45:10 -0800 | [diff] [blame] | 42 | SkDynamicMemoryWStream buffer; |
halcanary | f7a169e | 2015-05-01 07:35:45 -0700 | [diff] [blame] | 43 | if (!catPtr) { |
| 44 | catPtr = &catalog; |
halcanary | 130444f | 2015-04-25 06:45:07 -0700 | [diff] [blame] | 45 | } |
halcanary | f7a169e | 2015-05-01 07:35:45 -0700 | [diff] [blame] | 46 | obj.emitObject(&buffer, catPtr->numbers, catPtr->substitutes); |
| 47 | SkAutoTDelete<SkStreamAsset> asset(buffer.detachAsStream()); |
| 48 | SkString tmp(asset->getLength()); |
| 49 | asset->read(tmp.writable_str(), asset->getLength()); |
| 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 | f7a169e | 2015-05-01 07:35:45 -0700 | [diff] [blame] | 57 | #define ASSERT_EQL(REPORTER, SKSTRING, STRING, LEN) \ |
| 58 | do { \ |
| 59 | const char* strptr = STRING; \ |
| 60 | const SkString& sks = SKSTRING; \ |
| 61 | if (!eq(sks, strptr, LEN)) { \ |
| 62 | REPORT_FAILURE( \ |
| 63 | REPORTER, \ |
| 64 | "", \ |
| 65 | SkStringPrintf("'%s' != '%s'", strptr, sks.c_str())); \ |
| 66 | } \ |
| 67 | } while (false) |
| 68 | |
| 69 | #define ASSERT_EQ(REPORTER, SKSTRING, STRING) \ |
| 70 | do { \ |
| 71 | const char* str = STRING; \ |
| 72 | ASSERT_EQL(REPORTER, SKSTRING, str, strlen(str)); \ |
| 73 | } while (false) |
| 74 | |
| 75 | #define ASSERT_EMIT_EQ(REPORTER, OBJECT, STRING) \ |
| 76 | do { \ |
| 77 | SkString result = emit_to_string(OBJECT); \ |
| 78 | ASSERT_EQ(REPORTER, result, STRING); \ |
| 79 | } while (false) |
| 80 | |
| 81 | |
| 82 | |
vandebo@chromium.org | 421d644 | 2011-07-20 17:39:01 +0000 | [diff] [blame] | 83 | static void TestPDFStream(skiatest::Reporter* reporter) { |
| 84 | char streamBytes[] = "Test\nFoo\tBar"; |
scroggo | a1193e4 | 2015-01-21 12:09:53 -0800 | [diff] [blame] | 85 | SkAutoTDelete<SkMemoryStream> streamData(new SkMemoryStream( |
vandebo@chromium.org | d96d17b | 2013-01-04 19:31:24 +0000 | [diff] [blame] | 86 | streamBytes, strlen(streamBytes), true)); |
halcanary | 8103a34 | 2016-03-08 15:10:16 -0800 | [diff] [blame] | 87 | sk_sp<SkPDFStream> stream(new SkPDFStream(streamData.get())); |
halcanary | f7a169e | 2015-05-01 07:35:45 -0700 | [diff] [blame] | 88 | ASSERT_EMIT_EQ(reporter, |
| 89 | *stream, |
| 90 | "<</Length 12>> stream\nTest\nFoo\tBar\nendstream"); |
| 91 | stream->insertInt("Attribute", 42); |
| 92 | ASSERT_EMIT_EQ(reporter, |
| 93 | *stream, |
| 94 | "<</Length 12\n/Attribute 42>> stream\n" |
| 95 | "Test\nFoo\tBar\nendstream"); |
vandebo@chromium.org | 421d644 | 2011-07-20 17:39:01 +0000 | [diff] [blame] | 96 | |
halcanary | 91d1d62 | 2015-02-17 14:43:06 -0800 | [diff] [blame] | 97 | { |
vandebo@chromium.org | 421d644 | 2011-07-20 17:39:01 +0000 | [diff] [blame] | 98 | char streamBytes2[] = "This is a longer string, so that compression " |
| 99 | "can do something with it. With shorter strings, " |
| 100 | "the short circuit logic cuts in and we end up " |
| 101 | "with an uncompressed string."; |
| 102 | SkAutoDataUnref streamData2(SkData::NewWithCopy(streamBytes2, |
| 103 | strlen(streamBytes2))); |
halcanary | 8103a34 | 2016-03-08 15:10:16 -0800 | [diff] [blame] | 104 | sk_sp<SkPDFStream> stream(new SkPDFStream(streamData2.get())); |
vandebo@chromium.org | 421d644 | 2011-07-20 17:39:01 +0000 | [diff] [blame] | 105 | |
| 106 | SkDynamicMemoryWStream compressedByteStream; |
halcanary | 48305e8 | 2015-08-18 13:30:25 -0700 | [diff] [blame] | 107 | SkDeflateWStream deflateWStream(&compressedByteStream); |
| 108 | deflateWStream.write(streamBytes2, strlen(streamBytes2)); |
| 109 | deflateWStream.finalize(); |
vandebo@chromium.org | 421d644 | 2011-07-20 17:39:01 +0000 | [diff] [blame] | 110 | |
mtklein | cabc08c | 2015-02-19 08:29:24 -0800 | [diff] [blame] | 111 | SkDynamicMemoryWStream expected; |
halcanary | 37c46ca | 2015-03-31 12:30:20 -0700 | [diff] [blame] | 112 | expected.writeText("<</Filter /FlateDecode\n/Length 116>> stream\n"); |
halcanary | 48305e8 | 2015-08-18 13:30:25 -0700 | [diff] [blame] | 113 | compressedByteStream.writeToStream(&expected); |
| 114 | compressedByteStream.reset(); |
mtklein | cabc08c | 2015-02-19 08:29:24 -0800 | [diff] [blame] | 115 | expected.writeText("\nendstream"); |
| 116 | SkAutoDataUnref expectedResultData2(expected.copyToData()); |
halcanary | f7a169e | 2015-05-01 07:35:45 -0700 | [diff] [blame] | 117 | SkString result = emit_to_string(*stream); |
| 118 | ASSERT_EQL(reporter, |
| 119 | result, |
| 120 | (const char*)expectedResultData2->data(), |
| 121 | expectedResultData2->size()); |
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 | |
halcanary | f7a169e | 2015-05-01 07:35:45 -0700 | [diff] [blame] | 131 | objNumMap.addObject(a1.get()); |
| 132 | objNumMap.addObject(a2.get()); |
| 133 | objNumMap.addObject(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 | f7a169e | 2015-05-01 07:35:45 -0700 | [diff] [blame] | 150 | Catalog catalog; |
| 151 | catalog.numbers.addObject(a1.get()); |
| 152 | REPORTER_ASSERT(reporter, catalog.numbers.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]. |
| 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 | 2ef12d4 | 2011-07-06 23:31:24 +0000 | [diff] [blame] | 160 | static void TestSubstitute(skiatest::Reporter* reporter) { |
halcanary | 8103a34 | 2016-03-08 15:10:16 -0800 | [diff] [blame] | 161 | sk_sp<SkPDFDict> proxy(new SkPDFDict()); |
| 162 | sk_sp<SkPDFDict> stub(new SkPDFDict()); |
vandebo@chromium.org | 2ef12d4 | 2011-07-06 23:31:24 +0000 | [diff] [blame] | 163 | |
halcanary | f7a169e | 2015-05-01 07:35:45 -0700 | [diff] [blame] | 164 | proxy->insertInt("Value", 33); |
| 165 | stub->insertInt("Value", 44); |
vandebo@chromium.org | 2ef12d4 | 2011-07-06 23:31:24 +0000 | [diff] [blame] | 166 | |
halcanary | 37c46ca | 2015-03-31 12:30:20 -0700 | [diff] [blame] | 167 | SkPDFSubstituteMap substituteMap; |
| 168 | substituteMap.setSubstitute(proxy.get(), stub.get()); |
| 169 | SkPDFObjNumMap catalog; |
halcanary | f41061c | 2015-03-25 11:29:18 -0700 | [diff] [blame] | 170 | catalog.addObject(proxy.get()); |
vandebo@chromium.org | 2ef12d4 | 2011-07-06 23:31:24 +0000 | [diff] [blame] | 171 | |
halcanary | 8103a34 | 2016-03-08 15:10:16 -0800 | [diff] [blame] | 172 | REPORTER_ASSERT(reporter, stub.get() == substituteMap.getSubstitute(proxy.get())); |
| 173 | REPORTER_ASSERT(reporter, proxy.get() != substituteMap.getSubstitute(stub.get())); |
vandebo@chromium.org | 2ef12d4 | 2011-07-06 23:31:24 +0000 | [diff] [blame] | 174 | } |
| 175 | |
vandebo@chromium.org | 4e1cc6a | 2013-01-25 19:27:23 +0000 | [diff] [blame] | 176 | // This test used to assert without the fix submitted for |
| 177 | // http://code.google.com/p/skia/issues/detail?id=1083. |
| 178 | // SKP files might have invalid glyph ids. This test ensures they are ignored, |
| 179 | // and there is no assert on input data in Debug mode. |
sugoi@google.com | 54f0d1b | 2013-02-27 19:17:41 +0000 | [diff] [blame] | 180 | static void test_issue1083() { |
halcanary | 3d32d50 | 2015-03-01 06:55:20 -0800 | [diff] [blame] | 181 | SkDynamicMemoryWStream outStream; |
halcanary | 4b65666 | 2016-04-27 07:45:18 -0700 | [diff] [blame] | 182 | sk_sp<SkDocument> doc(SkDocument::MakePDF(&outStream)); |
halcanary | 3d32d50 | 2015-03-01 06:55:20 -0800 | [diff] [blame] | 183 | SkCanvas* canvas = doc->beginPage(100.0f, 100.0f); |
vandebo@chromium.org | 4e1cc6a | 2013-01-25 19:27:23 +0000 | [diff] [blame] | 184 | SkPaint paint; |
| 185 | paint.setTextEncoding(SkPaint::kGlyphID_TextEncoding); |
| 186 | |
| 187 | uint16_t glyphID = 65000; |
halcanary | 3d32d50 | 2015-03-01 06:55:20 -0800 | [diff] [blame] | 188 | canvas->drawText(&glyphID, 2, 0, 0, paint); |
vandebo@chromium.org | 4e1cc6a | 2013-01-25 19:27:23 +0000 | [diff] [blame] | 189 | |
halcanary | 3d32d50 | 2015-03-01 06:55:20 -0800 | [diff] [blame] | 190 | doc->close(); |
vandebo@chromium.org | 4e1cc6a | 2013-01-25 19:27:23 +0000 | [diff] [blame] | 191 | } |
| 192 | |
halcanary | f7a169e | 2015-05-01 07:35:45 -0700 | [diff] [blame] | 193 | static void TestPDFUnion(skiatest::Reporter* reporter) { |
| 194 | SkPDFUnion boolTrue = SkPDFUnion::Bool(true); |
| 195 | ASSERT_EMIT_EQ(reporter, boolTrue, "true"); |
vandebo@chromium.org | 8459d4e | 2010-09-24 22:25:30 +0000 | [diff] [blame] | 196 | |
halcanary | f7a169e | 2015-05-01 07:35:45 -0700 | [diff] [blame] | 197 | SkPDFUnion boolFalse = SkPDFUnion::Bool(false); |
| 198 | ASSERT_EMIT_EQ(reporter, boolFalse, "false"); |
vandebo@chromium.org | 8459d4e | 2010-09-24 22:25:30 +0000 | [diff] [blame] | 199 | |
halcanary | f7a169e | 2015-05-01 07:35:45 -0700 | [diff] [blame] | 200 | SkPDFUnion int42 = SkPDFUnion::Int(42); |
| 201 | ASSERT_EMIT_EQ(reporter, int42, "42"); |
| 202 | |
| 203 | SkPDFUnion realHalf = SkPDFUnion::Scalar(SK_ScalarHalf); |
halcanary | 8e9f5e3 | 2016-02-24 15:46:46 -0800 | [diff] [blame] | 204 | ASSERT_EMIT_EQ(reporter, realHalf, ".5"); |
halcanary | f7a169e | 2015-05-01 07:35:45 -0700 | [diff] [blame] | 205 | |
| 206 | SkPDFUnion bigScalar = SkPDFUnion::Scalar(110999.75f); |
halcanary | f7a169e | 2015-05-01 07:35:45 -0700 | [diff] [blame] | 207 | ASSERT_EMIT_EQ(reporter, bigScalar, "110999.75"); |
vandebo@chromium.org | 094316b | 2011-03-04 03:15:13 +0000 | [diff] [blame] | 208 | |
halcanary | 8e9f5e3 | 2016-02-24 15:46:46 -0800 | [diff] [blame] | 209 | SkPDFUnion biggerScalar = SkPDFUnion::Scalar(50000000.1f); |
halcanary | f7a169e | 2015-05-01 07:35:45 -0700 | [diff] [blame] | 210 | ASSERT_EMIT_EQ(reporter, biggerScalar, "50000000"); |
vandebo@chromium.org | 094316b | 2011-03-04 03:15:13 +0000 | [diff] [blame] | 211 | |
halcanary | 8e9f5e3 | 2016-02-24 15:46:46 -0800 | [diff] [blame] | 212 | SkPDFUnion smallestScalar = SkPDFUnion::Scalar(1.0f / 65536); |
| 213 | ASSERT_EMIT_EQ(reporter, smallestScalar, ".0000152587890"); |
vandebo@chromium.org | 094316b | 2011-03-04 03:15:13 +0000 | [diff] [blame] | 214 | |
halcanary | f7a169e | 2015-05-01 07:35:45 -0700 | [diff] [blame] | 215 | SkPDFUnion stringSimple = SkPDFUnion::String("test ) string ( foo"); |
| 216 | ASSERT_EMIT_EQ(reporter, stringSimple, "(test \\) string \\( foo)"); |
vandebo@chromium.org | 8459d4e | 2010-09-24 22:25:30 +0000 | [diff] [blame] | 217 | |
halcanary | f7a169e | 2015-05-01 07:35:45 -0700 | [diff] [blame] | 218 | SkString stringComplexInput("\ttest ) string ( foo"); |
| 219 | SkPDFUnion stringComplex = SkPDFUnion::String(stringComplexInput); |
halcanary | ee41b75 | 2016-06-23 14:08:11 -0700 | [diff] [blame] | 220 | ASSERT_EMIT_EQ(reporter, stringComplex, "(\\011test \\) string \\( foo)"); |
| 221 | |
| 222 | SkString binaryStringInput("\1\2\3\4\5\6\7\10\11\12\13\14\15\16\17\20"); |
| 223 | SkPDFUnion binaryString = SkPDFUnion::String(binaryStringInput); |
| 224 | ASSERT_EMIT_EQ(reporter, binaryString, "<0102030405060708090A0B0C0D0E0F10>"); |
vandebo@chromium.org | 8459d4e | 2010-09-24 22:25:30 +0000 | [diff] [blame] | 225 | |
halcanary | f7a169e | 2015-05-01 07:35:45 -0700 | [diff] [blame] | 226 | SkString nameInput("Test name\twith#tab"); |
| 227 | SkPDFUnion name = SkPDFUnion::Name(nameInput); |
| 228 | ASSERT_EMIT_EQ(reporter, name, "/Test#20name#09with#23tab"); |
| 229 | |
| 230 | SkString nameInput2("A#/%()<>[]{}B"); |
| 231 | SkPDFUnion name2 = SkPDFUnion::Name(nameInput2); |
| 232 | ASSERT_EMIT_EQ(reporter, name2, "/A#23#2F#25#28#29#3C#3E#5B#5D#7B#7DB"); |
| 233 | |
| 234 | SkPDFUnion name3 = SkPDFUnion::Name("SimpleNameWithOnlyPrintableASCII"); |
| 235 | ASSERT_EMIT_EQ(reporter, name3, "/SimpleNameWithOnlyPrintableASCII"); |
vandebo@chromium.org | 251a766 | 2012-09-21 17:50:50 +0000 | [diff] [blame] | 236 | |
vandebo@chromium.org | c0376fe | 2012-03-05 18:44:33 +0000 | [diff] [blame] | 237 | // Test that we correctly handle characters with the high-bit set. |
halcanary | f7a169e | 2015-05-01 07:35:45 -0700 | [diff] [blame] | 238 | SkString highBitString("\xDE\xAD" "be\xEF"); |
| 239 | SkPDFUnion highBitName = SkPDFUnion::Name(highBitString); |
| 240 | ASSERT_EMIT_EQ(reporter, highBitName, "/#DE#ADbe#EF"); |
| 241 | } |
vandebo@chromium.org | c0376fe | 2012-03-05 18:44:33 +0000 | [diff] [blame] | 242 | |
halcanary | f7a169e | 2015-05-01 07:35:45 -0700 | [diff] [blame] | 243 | static void TestPDFArray(skiatest::Reporter* reporter) { |
halcanary | 8103a34 | 2016-03-08 15:10:16 -0800 | [diff] [blame] | 244 | sk_sp<SkPDFArray> array(new SkPDFArray); |
halcanary | f7a169e | 2015-05-01 07:35:45 -0700 | [diff] [blame] | 245 | ASSERT_EMIT_EQ(reporter, *array, "[]"); |
vandebo@chromium.org | 8459d4e | 2010-09-24 22:25:30 +0000 | [diff] [blame] | 246 | |
halcanary | f7a169e | 2015-05-01 07:35:45 -0700 | [diff] [blame] | 247 | array->appendInt(42); |
| 248 | ASSERT_EMIT_EQ(reporter, *array, "[42]"); |
| 249 | |
| 250 | array->appendScalar(SK_ScalarHalf); |
halcanary | 8e9f5e3 | 2016-02-24 15:46:46 -0800 | [diff] [blame] | 251 | ASSERT_EMIT_EQ(reporter, *array, "[42 .5]"); |
halcanary | f7a169e | 2015-05-01 07:35:45 -0700 | [diff] [blame] | 252 | |
| 253 | array->appendInt(0); |
halcanary | 8e9f5e3 | 2016-02-24 15:46:46 -0800 | [diff] [blame] | 254 | ASSERT_EMIT_EQ(reporter, *array, "[42 .5 0]"); |
halcanary | f7a169e | 2015-05-01 07:35:45 -0700 | [diff] [blame] | 255 | |
| 256 | array->appendBool(true); |
halcanary | 8e9f5e3 | 2016-02-24 15:46:46 -0800 | [diff] [blame] | 257 | ASSERT_EMIT_EQ(reporter, *array, "[42 .5 0 true]"); |
halcanary | f7a169e | 2015-05-01 07:35:45 -0700 | [diff] [blame] | 258 | |
| 259 | array->appendName("ThisName"); |
halcanary | 8e9f5e3 | 2016-02-24 15:46:46 -0800 | [diff] [blame] | 260 | ASSERT_EMIT_EQ(reporter, *array, "[42 .5 0 true /ThisName]"); |
halcanary | f7a169e | 2015-05-01 07:35:45 -0700 | [diff] [blame] | 261 | |
| 262 | array->appendName(SkString("AnotherName")); |
halcanary | 8e9f5e3 | 2016-02-24 15:46:46 -0800 | [diff] [blame] | 263 | ASSERT_EMIT_EQ(reporter, *array, "[42 .5 0 true /ThisName /AnotherName]"); |
halcanary | f7a169e | 2015-05-01 07:35:45 -0700 | [diff] [blame] | 264 | |
| 265 | array->appendString("This String"); |
| 266 | ASSERT_EMIT_EQ(reporter, *array, |
halcanary | 8e9f5e3 | 2016-02-24 15:46:46 -0800 | [diff] [blame] | 267 | "[42 .5 0 true /ThisName /AnotherName (This String)]"); |
halcanary | f7a169e | 2015-05-01 07:35:45 -0700 | [diff] [blame] | 268 | |
| 269 | array->appendString(SkString("Another String")); |
| 270 | ASSERT_EMIT_EQ(reporter, *array, |
halcanary | 8e9f5e3 | 2016-02-24 15:46:46 -0800 | [diff] [blame] | 271 | "[42 .5 0 true /ThisName /AnotherName (This String) " |
halcanary | f7a169e | 2015-05-01 07:35:45 -0700 | [diff] [blame] | 272 | "(Another String)]"); |
| 273 | |
halcanary | 8103a34 | 2016-03-08 15:10:16 -0800 | [diff] [blame] | 274 | sk_sp<SkPDFArray> innerArray(new SkPDFArray); |
halcanary | f7a169e | 2015-05-01 07:35:45 -0700 | [diff] [blame] | 275 | innerArray->appendInt(-1); |
halcanary | 8103a34 | 2016-03-08 15:10:16 -0800 | [diff] [blame] | 276 | array->appendObject(std::move(innerArray)); |
halcanary | f7a169e | 2015-05-01 07:35:45 -0700 | [diff] [blame] | 277 | ASSERT_EMIT_EQ(reporter, *array, |
halcanary | 8e9f5e3 | 2016-02-24 15:46:46 -0800 | [diff] [blame] | 278 | "[42 .5 0 true /ThisName /AnotherName (This String) " |
halcanary | f7a169e | 2015-05-01 07:35:45 -0700 | [diff] [blame] | 279 | "(Another String) [-1]]"); |
| 280 | |
halcanary | 8103a34 | 2016-03-08 15:10:16 -0800 | [diff] [blame] | 281 | sk_sp<SkPDFArray> referencedArray(new SkPDFArray); |
halcanary | f7a169e | 2015-05-01 07:35:45 -0700 | [diff] [blame] | 282 | Catalog catalog; |
| 283 | catalog.numbers.addObject(referencedArray.get()); |
| 284 | REPORTER_ASSERT(reporter, catalog.numbers.getObjectNumber( |
| 285 | referencedArray.get()) == 1); |
halcanary | 8103a34 | 2016-03-08 15:10:16 -0800 | [diff] [blame] | 286 | array->appendObjRef(std::move(referencedArray)); |
halcanary | f7a169e | 2015-05-01 07:35:45 -0700 | [diff] [blame] | 287 | |
| 288 | SkString result = emit_to_string(*array, &catalog); |
| 289 | ASSERT_EQ(reporter, result, |
halcanary | 8e9f5e3 | 2016-02-24 15:46:46 -0800 | [diff] [blame] | 290 | "[42 .5 0 true /ThisName /AnotherName (This String) " |
halcanary | f7a169e | 2015-05-01 07:35:45 -0700 | [diff] [blame] | 291 | "(Another String) [-1] 1 0 R]"); |
| 292 | } |
| 293 | |
| 294 | static void TestPDFDict(skiatest::Reporter* reporter) { |
halcanary | 8103a34 | 2016-03-08 15:10:16 -0800 | [diff] [blame] | 295 | sk_sp<SkPDFDict> dict(new SkPDFDict); |
halcanary | f7a169e | 2015-05-01 07:35:45 -0700 | [diff] [blame] | 296 | ASSERT_EMIT_EQ(reporter, *dict, "<<>>"); |
| 297 | |
| 298 | dict->insertInt("n1", SkToSizeT(42)); |
| 299 | ASSERT_EMIT_EQ(reporter, *dict, "<</n1 42>>"); |
| 300 | |
| 301 | dict.reset(new SkPDFDict); |
| 302 | ASSERT_EMIT_EQ(reporter, *dict, "<<>>"); |
| 303 | |
| 304 | dict->insertInt("n1", 42); |
| 305 | ASSERT_EMIT_EQ(reporter, *dict, "<</n1 42>>"); |
| 306 | |
| 307 | dict->insertScalar("n2", SK_ScalarHalf); |
| 308 | |
halcanary | 130444f | 2015-04-25 06:45:07 -0700 | [diff] [blame] | 309 | SkString n3("n3"); |
halcanary | 8103a34 | 2016-03-08 15:10:16 -0800 | [diff] [blame] | 310 | sk_sp<SkPDFArray> innerArray(new SkPDFArray); |
halcanary | f7a169e | 2015-05-01 07:35:45 -0700 | [diff] [blame] | 311 | innerArray->appendInt(-100); |
halcanary | 8103a34 | 2016-03-08 15:10:16 -0800 | [diff] [blame] | 312 | dict->insertObject(n3, std::move(innerArray)); |
halcanary | 8e9f5e3 | 2016-02-24 15:46:46 -0800 | [diff] [blame] | 313 | 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] | 314 | |
halcanary | f7a169e | 2015-05-01 07:35:45 -0700 | [diff] [blame] | 315 | dict.reset(new SkPDFDict); |
| 316 | ASSERT_EMIT_EQ(reporter, *dict, "<<>>"); |
| 317 | |
| 318 | dict->insertInt("n1", 24); |
| 319 | ASSERT_EMIT_EQ(reporter, *dict, "<</n1 24>>"); |
| 320 | |
| 321 | dict->insertInt("n2", SkToSizeT(99)); |
| 322 | ASSERT_EMIT_EQ(reporter, *dict, "<</n1 24\n/n2 99>>"); |
| 323 | |
| 324 | dict->insertScalar("n3", SK_ScalarHalf); |
halcanary | 8e9f5e3 | 2016-02-24 15:46:46 -0800 | [diff] [blame] | 325 | ASSERT_EMIT_EQ(reporter, *dict, "<</n1 24\n/n2 99\n/n3 .5>>"); |
halcanary | f7a169e | 2015-05-01 07:35:45 -0700 | [diff] [blame] | 326 | |
| 327 | dict->insertName("n4", "AName"); |
halcanary | 8e9f5e3 | 2016-02-24 15:46:46 -0800 | [diff] [blame] | 328 | 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] | 329 | |
| 330 | dict->insertName("n5", SkString("AnotherName")); |
halcanary | 8e9f5e3 | 2016-02-24 15:46:46 -0800 | [diff] [blame] | 331 | 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] | 332 | "/n5 /AnotherName>>"); |
| 333 | |
| 334 | dict->insertString("n6", "A String"); |
halcanary | 8e9f5e3 | 2016-02-24 15:46:46 -0800 | [diff] [blame] | 335 | 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] | 336 | "/n5 /AnotherName\n/n6 (A String)>>"); |
| 337 | |
| 338 | dict->insertString("n7", SkString("Another String")); |
halcanary | 8e9f5e3 | 2016-02-24 15:46:46 -0800 | [diff] [blame] | 339 | 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] | 340 | "/n5 /AnotherName\n/n6 (A String)\n/n7 (Another String)>>"); |
| 341 | |
| 342 | dict.reset(new SkPDFDict("DType")); |
| 343 | ASSERT_EMIT_EQ(reporter, *dict, "<</Type /DType>>"); |
halcanary | 8e9f5e3 | 2016-02-24 15:46:46 -0800 | [diff] [blame] | 344 | |
halcanary | 8103a34 | 2016-03-08 15:10:16 -0800 | [diff] [blame] | 345 | sk_sp<SkPDFArray> referencedArray(new SkPDFArray); |
halcanary | f7a169e | 2015-05-01 07:35:45 -0700 | [diff] [blame] | 346 | Catalog catalog; |
| 347 | catalog.numbers.addObject(referencedArray.get()); |
| 348 | REPORTER_ASSERT(reporter, catalog.numbers.getObjectNumber( |
| 349 | referencedArray.get()) == 1); |
halcanary | 8103a34 | 2016-03-08 15:10:16 -0800 | [diff] [blame] | 350 | dict->insertObjRef("n1", std::move(referencedArray)); |
halcanary | f7a169e | 2015-05-01 07:35:45 -0700 | [diff] [blame] | 351 | SkString result = emit_to_string(*dict, &catalog); |
| 352 | ASSERT_EQ(reporter, result, "<</Type /DType\n/n1 1 0 R>>"); |
| 353 | } |
| 354 | |
| 355 | DEF_TEST(PDFPrimitives, reporter) { |
| 356 | TestPDFUnion(reporter); |
| 357 | TestPDFArray(reporter); |
| 358 | TestPDFDict(reporter); |
vandebo@chromium.org | 421d644 | 2011-07-20 17:39:01 +0000 | [diff] [blame] | 359 | TestPDFStream(reporter); |
halcanary | f7a169e | 2015-05-01 07:35:45 -0700 | [diff] [blame] | 360 | TestObjectNumberMap(reporter); |
vandebo@chromium.org | 8459d4e | 2010-09-24 22:25:30 +0000 | [diff] [blame] | 361 | TestObjectRef(reporter); |
vandebo@chromium.org | 2ef12d4 | 2011-07-06 23:31:24 +0000 | [diff] [blame] | 362 | TestSubstitute(reporter); |
sugoi@google.com | 54f0d1b | 2013-02-27 19:17:41 +0000 | [diff] [blame] | 363 | test_issue1083(); |
vandebo@chromium.org | 8459d4e | 2010-09-24 22:25:30 +0000 | [diff] [blame] | 364 | } |
senorblanco | b0e89dc | 2014-10-20 14:03:12 -0700 | [diff] [blame] | 365 | |
| 366 | namespace { |
| 367 | |
| 368 | class DummyImageFilter : public SkImageFilter { |
| 369 | public: |
robertphillips | 43c2ad4 | 2016-04-04 05:05:11 -0700 | [diff] [blame] | 370 | static sk_sp<DummyImageFilter> Make(bool visited = false) { |
| 371 | return sk_sp<DummyImageFilter>(new DummyImageFilter(visited)); |
| 372 | } |
| 373 | |
| 374 | SK_TO_STRING_OVERRIDE() |
| 375 | SK_DECLARE_PUBLIC_FLATTENABLE_DESERIALIZATION_PROCS(DummyImageFilter) |
| 376 | bool visited() const { return fVisited; } |
| 377 | |
| 378 | protected: |
robertphillips | 4ba94e2 | 2016-04-04 12:07:47 -0700 | [diff] [blame] | 379 | sk_sp<SkSpecialImage> onFilterImage(SkSpecialImage* source, const Context&, |
| 380 | SkIPoint* offset) const override { |
senorblanco | b0e89dc | 2014-10-20 14:03:12 -0700 | [diff] [blame] | 381 | fVisited = true; |
| 382 | offset->fX = offset->fY = 0; |
robertphillips | 4ba94e2 | 2016-04-04 12:07:47 -0700 | [diff] [blame] | 383 | return sk_ref_sp<SkSpecialImage>(source); |
senorblanco | b0e89dc | 2014-10-20 14:03:12 -0700 | [diff] [blame] | 384 | } |
senorblanco | b0e89dc | 2014-10-20 14:03:12 -0700 | [diff] [blame] | 385 | |
| 386 | private: |
robertphillips | 43c2ad4 | 2016-04-04 05:05:11 -0700 | [diff] [blame] | 387 | DummyImageFilter(bool visited) : INHERITED(nullptr, 0, nullptr), fVisited(visited) {} |
| 388 | |
senorblanco | b0e89dc | 2014-10-20 14:03:12 -0700 | [diff] [blame] | 389 | mutable bool fVisited; |
robertphillips | 43c2ad4 | 2016-04-04 05:05:11 -0700 | [diff] [blame] | 390 | |
| 391 | typedef SkImageFilter INHERITED; |
senorblanco | b0e89dc | 2014-10-20 14:03:12 -0700 | [diff] [blame] | 392 | }; |
| 393 | |
reed | 60c9b58 | 2016-04-03 09:11:13 -0700 | [diff] [blame] | 394 | sk_sp<SkFlattenable> DummyImageFilter::CreateProc(SkReadBuffer& buffer) { |
senorblanco | b0e89dc | 2014-10-20 14:03:12 -0700 | [diff] [blame] | 395 | SK_IMAGEFILTER_UNFLATTEN_COMMON(common, 0); |
| 396 | bool visited = buffer.readBool(); |
robertphillips | 43c2ad4 | 2016-04-04 05:05:11 -0700 | [diff] [blame] | 397 | return DummyImageFilter::Make(visited); |
senorblanco | b0e89dc | 2014-10-20 14:03:12 -0700 | [diff] [blame] | 398 | } |
| 399 | |
robertphillips | f3f5bad | 2014-12-19 13:49:15 -0800 | [diff] [blame] | 400 | #ifndef SK_IGNORE_TO_STRING |
| 401 | void DummyImageFilter::toString(SkString* str) const { |
| 402 | str->appendf("DummyImageFilter: ("); |
| 403 | str->append(")"); |
| 404 | } |
| 405 | #endif |
| 406 | |
senorblanco | b0e89dc | 2014-10-20 14:03:12 -0700 | [diff] [blame] | 407 | }; |
| 408 | |
| 409 | // Check that PDF rendering of image filters successfully falls back to |
| 410 | // CPU rasterization. |
| 411 | DEF_TEST(PDFImageFilter, reporter) { |
halcanary | 3d32d50 | 2015-03-01 06:55:20 -0800 | [diff] [blame] | 412 | SkDynamicMemoryWStream stream; |
halcanary | 4b65666 | 2016-04-27 07:45:18 -0700 | [diff] [blame] | 413 | sk_sp<SkDocument> doc(SkDocument::MakePDF(&stream)); |
halcanary | 3d32d50 | 2015-03-01 06:55:20 -0800 | [diff] [blame] | 414 | SkCanvas* canvas = doc->beginPage(100.0f, 100.0f); |
| 415 | |
robertphillips | 43c2ad4 | 2016-04-04 05:05:11 -0700 | [diff] [blame] | 416 | sk_sp<DummyImageFilter> filter(DummyImageFilter::Make()); |
senorblanco | b0e89dc | 2014-10-20 14:03:12 -0700 | [diff] [blame] | 417 | |
| 418 | // Filter just created; should be unvisited. |
| 419 | REPORTER_ASSERT(reporter, !filter->visited()); |
| 420 | SkPaint paint; |
| 421 | paint.setImageFilter(filter.get()); |
halcanary | 3d32d50 | 2015-03-01 06:55:20 -0800 | [diff] [blame] | 422 | canvas->drawRect(SkRect::MakeWH(100, 100), paint); |
| 423 | doc->close(); |
senorblanco | b0e89dc | 2014-10-20 14:03:12 -0700 | [diff] [blame] | 424 | |
| 425 | // Filter was used in rendering; should be visited. |
| 426 | REPORTER_ASSERT(reporter, filter->visited()); |
| 427 | } |
halcanary | 66a82f3 | 2015-10-12 13:05:04 -0700 | [diff] [blame] | 428 | |
| 429 | // Check that PDF rendering of image filters successfully falls back to |
| 430 | // CPU rasterization. |
| 431 | DEF_TEST(PDFFontCanEmbedTypeface, reporter) { |
| 432 | SkPDFCanon canon; |
| 433 | |
| 434 | const char resource[] = "fonts/Roboto2-Regular_NoEmbed.ttf"; |
bungeman | 13b9c95 | 2016-05-12 10:09:30 -0700 | [diff] [blame] | 435 | sk_sp<SkTypeface> noEmbedTypeface(MakeResourceAsTypeface(resource)); |
halcanary | 66a82f3 | 2015-10-12 13:05:04 -0700 | [diff] [blame] | 436 | if (noEmbedTypeface) { |
| 437 | REPORTER_ASSERT(reporter, |
halcanary | 8103a34 | 2016-03-08 15:10:16 -0800 | [diff] [blame] | 438 | !SkPDFFont::CanEmbedTypeface(noEmbedTypeface.get(), &canon)); |
halcanary | 66a82f3 | 2015-10-12 13:05:04 -0700 | [diff] [blame] | 439 | } |
halcanary | 8103a34 | 2016-03-08 15:10:16 -0800 | [diff] [blame] | 440 | sk_sp<SkTypeface> portableTypeface( |
mboc | ee6a991 | 2016-05-31 11:42:36 -0700 | [diff] [blame] | 441 | sk_tool_utils::create_portable_typeface(NULL, SkFontStyle())); |
halcanary | 66a82f3 | 2015-10-12 13:05:04 -0700 | [diff] [blame] | 442 | REPORTER_ASSERT(reporter, |
halcanary | 8103a34 | 2016-03-08 15:10:16 -0800 | [diff] [blame] | 443 | SkPDFFont::CanEmbedTypeface(portableTypeface.get(), &canon)); |
halcanary | 66a82f3 | 2015-10-12 13:05:04 -0700 | [diff] [blame] | 444 | } |
halcanary | 8e9f5e3 | 2016-02-24 15:46:46 -0800 | [diff] [blame] | 445 | |
| 446 | |
| 447 | // test to see that all finite scalars round trip via scanf(). |
| 448 | static void check_pdf_scalar_serialization( |
| 449 | skiatest::Reporter* reporter, float inputFloat) { |
| 450 | char floatString[SkPDFUtils::kMaximumFloatDecimalLength]; |
| 451 | size_t len = SkPDFUtils::FloatToDecimal(inputFloat, floatString); |
| 452 | if (len >= sizeof(floatString)) { |
| 453 | ERRORF(reporter, "string too long: %u", (unsigned)len); |
| 454 | return; |
| 455 | } |
| 456 | if (floatString[len] != '\0' || strlen(floatString) != len) { |
| 457 | ERRORF(reporter, "terminator misplaced."); |
| 458 | return; // The terminator is needed for sscanf(). |
| 459 | } |
| 460 | if (reporter->verbose()) { |
| 461 | SkDebugf("%15.9g = \"%s\"\n", inputFloat, floatString); |
| 462 | } |
| 463 | float roundTripFloat; |
| 464 | if (1 != sscanf(floatString, "%f", &roundTripFloat)) { |
| 465 | ERRORF(reporter, "unscannable result: %s", floatString); |
| 466 | return; |
| 467 | } |
| 468 | if (isfinite(inputFloat) && roundTripFloat != inputFloat) { |
| 469 | ERRORF(reporter, "roundTripFloat (%.9g) != inputFloat (%.9g)", |
| 470 | roundTripFloat, inputFloat); |
| 471 | } |
| 472 | } |
| 473 | |
| 474 | // Test SkPDFUtils::AppendScalar for accuracy. |
| 475 | DEF_TEST(PDFPrimitives_Scalar, reporter) { |
| 476 | SkRandom random(0x5EED); |
| 477 | int iterationCount = 512; |
| 478 | while (iterationCount-- > 0) { |
| 479 | union { uint32_t u; float f; }; |
| 480 | u = random.nextU(); |
| 481 | static_assert(sizeof(float) == sizeof(uint32_t), ""); |
| 482 | check_pdf_scalar_serialization(reporter, f); |
| 483 | } |
| 484 | float alwaysCheck[] = { |
| 485 | 0.0f, -0.0f, 1.0f, -1.0f, SK_ScalarPI, 0.1f, FLT_MIN, FLT_MAX, |
| 486 | -FLT_MIN, -FLT_MAX, FLT_MIN / 16.0f, -FLT_MIN / 16.0f, |
| 487 | SK_FloatNaN, SK_FloatInfinity, SK_FloatNegativeInfinity, |
| 488 | -FLT_MIN / 8388608.0 |
| 489 | }; |
| 490 | for (float inputFloat: alwaysCheck) { |
| 491 | check_pdf_scalar_serialization(reporter, inputFloat); |
| 492 | } |
| 493 | } |