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" |
senorblanco | b0e89dc | 2014-10-20 14:03:12 -0700 | [diff] [blame] | 21 | #include "SkReadBuffer.h" |
vandebo@chromium.org | 8459d4e | 2010-09-24 22:25:30 +0000 | [diff] [blame] | 22 | #include "SkScalar.h" |
| 23 | #include "SkStream.h" |
vandebo@chromium.org | 421d644 | 2011-07-20 17:39:01 +0000 | [diff] [blame] | 24 | #include "SkTypes.h" |
tfarina@chromium.org | 8f6884a | 2014-01-24 20:56:26 +0000 | [diff] [blame] | 25 | #include "Test.h" |
halcanary | 66a82f3 | 2015-10-12 13:05:04 -0700 | [diff] [blame] | 26 | #include "sk_tool_utils.h" |
vandebo@chromium.org | 8459d4e | 2010-09-24 22:25:30 +0000 | [diff] [blame] | 27 | |
commit-bot@chromium.org | 608ea65 | 2013-10-03 19:29:21 +0000 | [diff] [blame] | 28 | #define DUMMY_TEXT "DCT compessed stream." |
| 29 | |
halcanary | f7a169e | 2015-05-01 07:35:45 -0700 | [diff] [blame] | 30 | namespace { |
| 31 | struct Catalog { |
| 32 | SkPDFSubstituteMap substitutes; |
| 33 | SkPDFObjNumMap numbers; |
| 34 | }; |
| 35 | } // namespace |
reed@google.com | 8a85d0c | 2011-06-24 19:12:12 +0000 | [diff] [blame] | 36 | |
halcanary | f7a169e | 2015-05-01 07:35:45 -0700 | [diff] [blame] | 37 | template <typename T> |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 38 | static SkString emit_to_string(T& obj, Catalog* catPtr = nullptr) { |
halcanary | f7a169e | 2015-05-01 07:35:45 -0700 | [diff] [blame] | 39 | Catalog catalog; |
halcanary | 6a14434 | 2015-01-23 11:45:10 -0800 | [diff] [blame] | 40 | SkDynamicMemoryWStream buffer; |
halcanary | f7a169e | 2015-05-01 07:35:45 -0700 | [diff] [blame] | 41 | if (!catPtr) { |
| 42 | catPtr = &catalog; |
halcanary | 130444f | 2015-04-25 06:45:07 -0700 | [diff] [blame] | 43 | } |
halcanary | f7a169e | 2015-05-01 07:35:45 -0700 | [diff] [blame] | 44 | obj.emitObject(&buffer, catPtr->numbers, catPtr->substitutes); |
| 45 | SkAutoTDelete<SkStreamAsset> asset(buffer.detachAsStream()); |
| 46 | SkString tmp(asset->getLength()); |
| 47 | asset->read(tmp.writable_str(), asset->getLength()); |
| 48 | return tmp; |
vandebo@chromium.org | 8459d4e | 2010-09-24 22:25:30 +0000 | [diff] [blame] | 49 | } |
| 50 | |
halcanary | f7a169e | 2015-05-01 07:35:45 -0700 | [diff] [blame] | 51 | static bool eq(const SkString& str, const char* strPtr, size_t len) { |
| 52 | return len == str.size() && 0 == memcmp(str.c_str(), strPtr, len); |
vandebo@chromium.org | 421d644 | 2011-07-20 17:39:01 +0000 | [diff] [blame] | 53 | } |
| 54 | |
halcanary | f7a169e | 2015-05-01 07:35:45 -0700 | [diff] [blame] | 55 | #define ASSERT_EQL(REPORTER, SKSTRING, STRING, LEN) \ |
| 56 | do { \ |
| 57 | const char* strptr = STRING; \ |
| 58 | const SkString& sks = SKSTRING; \ |
| 59 | if (!eq(sks, strptr, LEN)) { \ |
| 60 | REPORT_FAILURE( \ |
| 61 | REPORTER, \ |
| 62 | "", \ |
| 63 | SkStringPrintf("'%s' != '%s'", strptr, sks.c_str())); \ |
| 64 | } \ |
| 65 | } while (false) |
| 66 | |
| 67 | #define ASSERT_EQ(REPORTER, SKSTRING, STRING) \ |
| 68 | do { \ |
| 69 | const char* str = STRING; \ |
| 70 | ASSERT_EQL(REPORTER, SKSTRING, str, strlen(str)); \ |
| 71 | } while (false) |
| 72 | |
| 73 | #define ASSERT_EMIT_EQ(REPORTER, OBJECT, STRING) \ |
| 74 | do { \ |
| 75 | SkString result = emit_to_string(OBJECT); \ |
| 76 | ASSERT_EQ(REPORTER, result, STRING); \ |
| 77 | } while (false) |
| 78 | |
| 79 | |
| 80 | |
vandebo@chromium.org | 421d644 | 2011-07-20 17:39:01 +0000 | [diff] [blame] | 81 | static void TestPDFStream(skiatest::Reporter* reporter) { |
| 82 | char streamBytes[] = "Test\nFoo\tBar"; |
scroggo | a1193e4 | 2015-01-21 12:09:53 -0800 | [diff] [blame] | 83 | SkAutoTDelete<SkMemoryStream> streamData(new SkMemoryStream( |
vandebo@chromium.org | d96d17b | 2013-01-04 19:31:24 +0000 | [diff] [blame] | 84 | streamBytes, strlen(streamBytes), true)); |
| 85 | SkAutoTUnref<SkPDFStream> stream(new SkPDFStream(streamData.get())); |
halcanary | f7a169e | 2015-05-01 07:35:45 -0700 | [diff] [blame] | 86 | ASSERT_EMIT_EQ(reporter, |
| 87 | *stream, |
| 88 | "<</Length 12>> stream\nTest\nFoo\tBar\nendstream"); |
| 89 | stream->insertInt("Attribute", 42); |
| 90 | ASSERT_EMIT_EQ(reporter, |
| 91 | *stream, |
| 92 | "<</Length 12\n/Attribute 42>> stream\n" |
| 93 | "Test\nFoo\tBar\nendstream"); |
vandebo@chromium.org | 421d644 | 2011-07-20 17:39:01 +0000 | [diff] [blame] | 94 | |
halcanary | 91d1d62 | 2015-02-17 14:43:06 -0800 | [diff] [blame] | 95 | { |
vandebo@chromium.org | 421d644 | 2011-07-20 17:39:01 +0000 | [diff] [blame] | 96 | char streamBytes2[] = "This is a longer string, so that compression " |
| 97 | "can do something with it. With shorter strings, " |
| 98 | "the short circuit logic cuts in and we end up " |
| 99 | "with an uncompressed string."; |
| 100 | SkAutoDataUnref streamData2(SkData::NewWithCopy(streamBytes2, |
| 101 | strlen(streamBytes2))); |
vandebo@chromium.org | d96d17b | 2013-01-04 19:31:24 +0000 | [diff] [blame] | 102 | SkAutoTUnref<SkPDFStream> stream(new SkPDFStream(streamData2.get())); |
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"); |
| 114 | SkAutoDataUnref expectedResultData2(expected.copyToData()); |
halcanary | f7a169e | 2015-05-01 07:35:45 -0700 | [diff] [blame] | 115 | SkString result = emit_to_string(*stream); |
| 116 | ASSERT_EQL(reporter, |
| 117 | result, |
| 118 | (const char*)expectedResultData2->data(), |
| 119 | expectedResultData2->size()); |
vandebo@chromium.org | 421d644 | 2011-07-20 17:39:01 +0000 | [diff] [blame] | 120 | } |
| 121 | } |
| 122 | |
halcanary | f7a169e | 2015-05-01 07:35:45 -0700 | [diff] [blame] | 123 | static void TestObjectNumberMap(skiatest::Reporter* reporter) { |
| 124 | SkPDFObjNumMap objNumMap; |
| 125 | SkAutoTUnref<SkPDFArray> a1(new SkPDFArray); |
| 126 | SkAutoTUnref<SkPDFArray> a2(new SkPDFArray); |
| 127 | SkAutoTUnref<SkPDFArray> a3(new SkPDFArray); |
vandebo@chromium.org | 8459d4e | 2010-09-24 22:25:30 +0000 | [diff] [blame] | 128 | |
halcanary | f7a169e | 2015-05-01 07:35:45 -0700 | [diff] [blame] | 129 | objNumMap.addObject(a1.get()); |
| 130 | objNumMap.addObject(a2.get()); |
| 131 | objNumMap.addObject(a3.get()); |
vandebo@chromium.org | 8459d4e | 2010-09-24 22:25:30 +0000 | [diff] [blame] | 132 | |
halcanary | f7a169e | 2015-05-01 07:35:45 -0700 | [diff] [blame] | 133 | // The objects should be numbered in the order they are added, |
| 134 | // starting with 1. |
| 135 | REPORTER_ASSERT(reporter, objNumMap.getObjectNumber(a1.get()) == 1); |
| 136 | REPORTER_ASSERT(reporter, objNumMap.getObjectNumber(a2.get()) == 2); |
| 137 | REPORTER_ASSERT(reporter, objNumMap.getObjectNumber(a3.get()) == 3); |
| 138 | // Assert that repeated calls to get the object number return |
| 139 | // consistent result. |
| 140 | REPORTER_ASSERT(reporter, objNumMap.getObjectNumber(a1.get()) == 1); |
vandebo@chromium.org | 8459d4e | 2010-09-24 22:25:30 +0000 | [diff] [blame] | 141 | } |
| 142 | |
| 143 | static void TestObjectRef(skiatest::Reporter* reporter) { |
halcanary | f7a169e | 2015-05-01 07:35:45 -0700 | [diff] [blame] | 144 | SkAutoTUnref<SkPDFArray> a1(new SkPDFArray); |
| 145 | SkAutoTUnref<SkPDFArray> a2(new SkPDFArray); |
| 146 | a2->appendObjRef(SkRef(a1.get())); |
vandebo@chromium.org | 8459d4e | 2010-09-24 22:25:30 +0000 | [diff] [blame] | 147 | |
halcanary | f7a169e | 2015-05-01 07:35:45 -0700 | [diff] [blame] | 148 | Catalog catalog; |
| 149 | catalog.numbers.addObject(a1.get()); |
| 150 | REPORTER_ASSERT(reporter, catalog.numbers.getObjectNumber(a1.get()) == 1); |
vandebo@chromium.org | 8459d4e | 2010-09-24 22:25:30 +0000 | [diff] [blame] | 151 | |
halcanary | f7a169e | 2015-05-01 07:35:45 -0700 | [diff] [blame] | 152 | SkString result = emit_to_string(*a2, &catalog); |
| 153 | // If appendObjRef misbehaves, then the result would |
| 154 | // be [[]], not [1 0 R]. |
| 155 | ASSERT_EQ(reporter, result, "[1 0 R]"); |
vandebo@chromium.org | 8459d4e | 2010-09-24 22:25:30 +0000 | [diff] [blame] | 156 | } |
| 157 | |
vandebo@chromium.org | 2ef12d4 | 2011-07-06 23:31:24 +0000 | [diff] [blame] | 158 | static void TestSubstitute(skiatest::Reporter* reporter) { |
halcanary | f3d53a0 | 2015-03-24 15:29:39 -0700 | [diff] [blame] | 159 | SkAutoTUnref<SkPDFDict> proxy(new SkPDFDict()); |
| 160 | SkAutoTUnref<SkPDFDict> stub(new SkPDFDict()); |
vandebo@chromium.org | 2ef12d4 | 2011-07-06 23:31:24 +0000 | [diff] [blame] | 161 | |
halcanary | f7a169e | 2015-05-01 07:35:45 -0700 | [diff] [blame] | 162 | proxy->insertInt("Value", 33); |
| 163 | stub->insertInt("Value", 44); |
vandebo@chromium.org | 2ef12d4 | 2011-07-06 23:31:24 +0000 | [diff] [blame] | 164 | |
halcanary | 37c46ca | 2015-03-31 12:30:20 -0700 | [diff] [blame] | 165 | SkPDFSubstituteMap substituteMap; |
| 166 | substituteMap.setSubstitute(proxy.get(), stub.get()); |
| 167 | SkPDFObjNumMap catalog; |
halcanary | f41061c | 2015-03-25 11:29:18 -0700 | [diff] [blame] | 168 | catalog.addObject(proxy.get()); |
vandebo@chromium.org | 2ef12d4 | 2011-07-06 23:31:24 +0000 | [diff] [blame] | 169 | |
halcanary | 37c46ca | 2015-03-31 12:30:20 -0700 | [diff] [blame] | 170 | REPORTER_ASSERT(reporter, stub.get() == substituteMap.getSubstitute(proxy)); |
| 171 | REPORTER_ASSERT(reporter, proxy.get() != substituteMap.getSubstitute(stub)); |
vandebo@chromium.org | 2ef12d4 | 2011-07-06 23:31:24 +0000 | [diff] [blame] | 172 | } |
| 173 | |
vandebo@chromium.org | 4e1cc6a | 2013-01-25 19:27:23 +0000 | [diff] [blame] | 174 | // This test used to assert without the fix submitted for |
| 175 | // http://code.google.com/p/skia/issues/detail?id=1083. |
| 176 | // SKP files might have invalid glyph ids. This test ensures they are ignored, |
| 177 | // and there is no assert on input data in Debug mode. |
sugoi@google.com | 54f0d1b | 2013-02-27 19:17:41 +0000 | [diff] [blame] | 178 | static void test_issue1083() { |
halcanary | 3d32d50 | 2015-03-01 06:55:20 -0800 | [diff] [blame] | 179 | SkDynamicMemoryWStream outStream; |
| 180 | SkAutoTUnref<SkDocument> doc(SkDocument::CreatePDF(&outStream)); |
| 181 | SkCanvas* canvas = doc->beginPage(100.0f, 100.0f); |
vandebo@chromium.org | 4e1cc6a | 2013-01-25 19:27:23 +0000 | [diff] [blame] | 182 | SkPaint paint; |
| 183 | paint.setTextEncoding(SkPaint::kGlyphID_TextEncoding); |
| 184 | |
| 185 | uint16_t glyphID = 65000; |
halcanary | 3d32d50 | 2015-03-01 06:55:20 -0800 | [diff] [blame] | 186 | canvas->drawText(&glyphID, 2, 0, 0, paint); |
vandebo@chromium.org | 4e1cc6a | 2013-01-25 19:27:23 +0000 | [diff] [blame] | 187 | |
halcanary | 3d32d50 | 2015-03-01 06:55:20 -0800 | [diff] [blame] | 188 | doc->close(); |
vandebo@chromium.org | 4e1cc6a | 2013-01-25 19:27:23 +0000 | [diff] [blame] | 189 | } |
| 190 | |
halcanary | f7a169e | 2015-05-01 07:35:45 -0700 | [diff] [blame] | 191 | static void TestPDFUnion(skiatest::Reporter* reporter) { |
| 192 | SkPDFUnion boolTrue = SkPDFUnion::Bool(true); |
| 193 | ASSERT_EMIT_EQ(reporter, boolTrue, "true"); |
vandebo@chromium.org | 8459d4e | 2010-09-24 22:25:30 +0000 | [diff] [blame] | 194 | |
halcanary | f7a169e | 2015-05-01 07:35:45 -0700 | [diff] [blame] | 195 | SkPDFUnion boolFalse = SkPDFUnion::Bool(false); |
| 196 | ASSERT_EMIT_EQ(reporter, boolFalse, "false"); |
vandebo@chromium.org | 8459d4e | 2010-09-24 22:25:30 +0000 | [diff] [blame] | 197 | |
halcanary | f7a169e | 2015-05-01 07:35:45 -0700 | [diff] [blame] | 198 | SkPDFUnion int42 = SkPDFUnion::Int(42); |
| 199 | ASSERT_EMIT_EQ(reporter, int42, "42"); |
| 200 | |
| 201 | SkPDFUnion realHalf = SkPDFUnion::Scalar(SK_ScalarHalf); |
| 202 | ASSERT_EMIT_EQ(reporter, realHalf, "0.5"); |
| 203 | |
| 204 | SkPDFUnion bigScalar = SkPDFUnion::Scalar(110999.75f); |
vandebo@chromium.org | 6cc26da | 2011-05-18 17:08:05 +0000 | [diff] [blame] | 205 | #if !defined(SK_ALLOW_LARGE_PDF_SCALARS) |
halcanary | f7a169e | 2015-05-01 07:35:45 -0700 | [diff] [blame] | 206 | ASSERT_EMIT_EQ(reporter, bigScalar, "111000"); |
vandebo@chromium.org | 094316b | 2011-03-04 03:15:13 +0000 | [diff] [blame] | 207 | #else |
halcanary | f7a169e | 2015-05-01 07:35:45 -0700 | [diff] [blame] | 208 | ASSERT_EMIT_EQ(reporter, bigScalar, "110999.75"); |
vandebo@chromium.org | 094316b | 2011-03-04 03:15:13 +0000 | [diff] [blame] | 209 | |
halcanary | f7a169e | 2015-05-01 07:35:45 -0700 | [diff] [blame] | 210 | SkPDFUnion biggerScalar = SkPDFUnion::Scalar(50000000.1); |
| 211 | ASSERT_EMIT_EQ(reporter, biggerScalar, "50000000"); |
vandebo@chromium.org | 094316b | 2011-03-04 03:15:13 +0000 | [diff] [blame] | 212 | |
halcanary | f7a169e | 2015-05-01 07:35:45 -0700 | [diff] [blame] | 213 | SkPDFUnion smallestScalar = SkPDFUnion::Scalar(1.0 / 65536); |
| 214 | ASSERT_EMIT_EQ(reporter, smallestScalar, "0.00001526"); |
vandebo@chromium.org | 094316b | 2011-03-04 03:15:13 +0000 | [diff] [blame] | 215 | #endif |
| 216 | |
halcanary | f7a169e | 2015-05-01 07:35:45 -0700 | [diff] [blame] | 217 | SkPDFUnion stringSimple = SkPDFUnion::String("test ) string ( foo"); |
| 218 | ASSERT_EMIT_EQ(reporter, stringSimple, "(test \\) string \\( foo)"); |
vandebo@chromium.org | 8459d4e | 2010-09-24 22:25:30 +0000 | [diff] [blame] | 219 | |
halcanary | f7a169e | 2015-05-01 07:35:45 -0700 | [diff] [blame] | 220 | SkString stringComplexInput("\ttest ) string ( foo"); |
| 221 | SkPDFUnion stringComplex = SkPDFUnion::String(stringComplexInput); |
| 222 | ASSERT_EMIT_EQ(reporter, |
| 223 | stringComplex, |
| 224 | "<0974657374202920737472696E67202820666F6F>"); |
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) { |
vandebo@chromium.org | d96d17b | 2013-01-04 19:31:24 +0000 | [diff] [blame] | 244 | SkAutoTUnref<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); |
| 251 | ASSERT_EMIT_EQ(reporter, *array, "[42 0.5]"); |
| 252 | |
| 253 | array->appendInt(0); |
| 254 | ASSERT_EMIT_EQ(reporter, *array, "[42 0.5 0]"); |
| 255 | |
| 256 | array->appendBool(true); |
| 257 | ASSERT_EMIT_EQ(reporter, *array, "[42 0.5 0 true]"); |
| 258 | |
| 259 | array->appendName("ThisName"); |
| 260 | ASSERT_EMIT_EQ(reporter, *array, "[42 0.5 0 true /ThisName]"); |
| 261 | |
| 262 | array->appendName(SkString("AnotherName")); |
| 263 | ASSERT_EMIT_EQ(reporter, *array, "[42 0.5 0 true /ThisName /AnotherName]"); |
| 264 | |
| 265 | array->appendString("This String"); |
| 266 | ASSERT_EMIT_EQ(reporter, *array, |
| 267 | "[42 0.5 0 true /ThisName /AnotherName (This String)]"); |
| 268 | |
| 269 | array->appendString(SkString("Another String")); |
| 270 | ASSERT_EMIT_EQ(reporter, *array, |
| 271 | "[42 0.5 0 true /ThisName /AnotherName (This String) " |
| 272 | "(Another String)]"); |
| 273 | |
| 274 | SkAutoTUnref<SkPDFArray> innerArray(new SkPDFArray); |
| 275 | innerArray->appendInt(-1); |
| 276 | array->appendObject(innerArray.detach()); |
| 277 | ASSERT_EMIT_EQ(reporter, *array, |
| 278 | "[42 0.5 0 true /ThisName /AnotherName (This String) " |
| 279 | "(Another String) [-1]]"); |
| 280 | |
| 281 | SkAutoTUnref<SkPDFArray> referencedArray(new SkPDFArray); |
| 282 | Catalog catalog; |
| 283 | catalog.numbers.addObject(referencedArray.get()); |
| 284 | REPORTER_ASSERT(reporter, catalog.numbers.getObjectNumber( |
| 285 | referencedArray.get()) == 1); |
| 286 | array->appendObjRef(referencedArray.detach()); |
| 287 | |
| 288 | SkString result = emit_to_string(*array, &catalog); |
| 289 | ASSERT_EQ(reporter, result, |
| 290 | "[42 0.5 0 true /ThisName /AnotherName (This String) " |
| 291 | "(Another String) [-1] 1 0 R]"); |
| 292 | } |
| 293 | |
| 294 | static void TestPDFDict(skiatest::Reporter* reporter) { |
vandebo@chromium.org | d96d17b | 2013-01-04 19:31:24 +0000 | [diff] [blame] | 295 | SkAutoTUnref<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 | f7a169e | 2015-05-01 07:35:45 -0700 | [diff] [blame] | 310 | SkAutoTUnref<SkPDFArray> innerArray(new SkPDFArray); |
| 311 | innerArray->appendInt(-100); |
| 312 | dict->insertObject(n3, innerArray.detach()); |
| 313 | ASSERT_EMIT_EQ(reporter, *dict, "<</n1 42\n/n2 0.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); |
| 325 | ASSERT_EMIT_EQ(reporter, *dict, "<</n1 24\n/n2 99\n/n3 0.5>>"); |
| 326 | |
| 327 | dict->insertName("n4", "AName"); |
| 328 | ASSERT_EMIT_EQ(reporter, *dict, "<</n1 24\n/n2 99\n/n3 0.5\n/n4 /AName>>"); |
| 329 | |
| 330 | dict->insertName("n5", SkString("AnotherName")); |
| 331 | ASSERT_EMIT_EQ(reporter, *dict, "<</n1 24\n/n2 99\n/n3 0.5\n/n4 /AName\n" |
| 332 | "/n5 /AnotherName>>"); |
| 333 | |
| 334 | 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 | "/n5 /AnotherName\n/n6 (A String)>>"); |
| 337 | |
| 338 | 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 | "/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>>"); |
| 344 | |
| 345 | SkAutoTUnref<SkPDFArray> referencedArray(new SkPDFArray); |
| 346 | Catalog catalog; |
| 347 | catalog.numbers.addObject(referencedArray.get()); |
| 348 | REPORTER_ASSERT(reporter, catalog.numbers.getObjectNumber( |
| 349 | referencedArray.get()) == 1); |
| 350 | dict->insertObjRef("n1", referencedArray.detach()); |
| 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: |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 370 | DummyImageFilter(bool visited = false) : SkImageFilter(0, nullptr), fVisited(visited) {} |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 371 | ~DummyImageFilter() override {} |
senorblanco | b0e89dc | 2014-10-20 14:03:12 -0700 | [diff] [blame] | 372 | virtual bool onFilterImage(Proxy*, const SkBitmap& src, const Context&, |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 373 | SkBitmap* result, SkIPoint* offset) const override { |
senorblanco | b0e89dc | 2014-10-20 14:03:12 -0700 | [diff] [blame] | 374 | fVisited = true; |
| 375 | offset->fX = offset->fY = 0; |
| 376 | *result = src; |
| 377 | return true; |
| 378 | } |
robertphillips | f3f5bad | 2014-12-19 13:49:15 -0800 | [diff] [blame] | 379 | SK_TO_STRING_OVERRIDE() |
senorblanco | b0e89dc | 2014-10-20 14:03:12 -0700 | [diff] [blame] | 380 | SK_DECLARE_PUBLIC_FLATTENABLE_DESERIALIZATION_PROCS(DummyImageFilter) |
senorblanco | b0e89dc | 2014-10-20 14:03:12 -0700 | [diff] [blame] | 381 | bool visited() const { return fVisited; } |
| 382 | |
| 383 | private: |
| 384 | mutable bool fVisited; |
| 385 | }; |
| 386 | |
| 387 | SkFlattenable* DummyImageFilter::CreateProc(SkReadBuffer& buffer) { |
| 388 | SK_IMAGEFILTER_UNFLATTEN_COMMON(common, 0); |
| 389 | bool visited = buffer.readBool(); |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 390 | return new DummyImageFilter(visited); |
senorblanco | b0e89dc | 2014-10-20 14:03:12 -0700 | [diff] [blame] | 391 | } |
| 392 | |
robertphillips | f3f5bad | 2014-12-19 13:49:15 -0800 | [diff] [blame] | 393 | #ifndef SK_IGNORE_TO_STRING |
| 394 | void DummyImageFilter::toString(SkString* str) const { |
| 395 | str->appendf("DummyImageFilter: ("); |
| 396 | str->append(")"); |
| 397 | } |
| 398 | #endif |
| 399 | |
senorblanco | b0e89dc | 2014-10-20 14:03:12 -0700 | [diff] [blame] | 400 | }; |
| 401 | |
| 402 | // Check that PDF rendering of image filters successfully falls back to |
| 403 | // CPU rasterization. |
| 404 | DEF_TEST(PDFImageFilter, reporter) { |
halcanary | 3d32d50 | 2015-03-01 06:55:20 -0800 | [diff] [blame] | 405 | SkDynamicMemoryWStream stream; |
| 406 | SkAutoTUnref<SkDocument> doc(SkDocument::CreatePDF(&stream)); |
| 407 | SkCanvas* canvas = doc->beginPage(100.0f, 100.0f); |
| 408 | |
senorblanco | b0e89dc | 2014-10-20 14:03:12 -0700 | [diff] [blame] | 409 | SkAutoTUnref<DummyImageFilter> filter(new DummyImageFilter()); |
| 410 | |
| 411 | // Filter just created; should be unvisited. |
| 412 | REPORTER_ASSERT(reporter, !filter->visited()); |
| 413 | SkPaint paint; |
| 414 | paint.setImageFilter(filter.get()); |
halcanary | 3d32d50 | 2015-03-01 06:55:20 -0800 | [diff] [blame] | 415 | canvas->drawRect(SkRect::MakeWH(100, 100), paint); |
| 416 | doc->close(); |
senorblanco | b0e89dc | 2014-10-20 14:03:12 -0700 | [diff] [blame] | 417 | |
| 418 | // Filter was used in rendering; should be visited. |
| 419 | REPORTER_ASSERT(reporter, filter->visited()); |
| 420 | } |
halcanary | 66a82f3 | 2015-10-12 13:05:04 -0700 | [diff] [blame] | 421 | |
| 422 | // Check that PDF rendering of image filters successfully falls back to |
| 423 | // CPU rasterization. |
| 424 | DEF_TEST(PDFFontCanEmbedTypeface, reporter) { |
| 425 | SkPDFCanon canon; |
| 426 | |
| 427 | const char resource[] = "fonts/Roboto2-Regular_NoEmbed.ttf"; |
| 428 | SkAutoTUnref<SkTypeface> noEmbedTypeface(GetResourceAsTypeface(resource)); |
| 429 | if (noEmbedTypeface) { |
| 430 | REPORTER_ASSERT(reporter, |
| 431 | !SkPDFFont::CanEmbedTypeface(noEmbedTypeface, &canon)); |
| 432 | } |
| 433 | SkAutoTUnref<SkTypeface> portableTypeface( |
| 434 | sk_tool_utils::create_portable_typeface(NULL, SkTypeface::kNormal)); |
| 435 | REPORTER_ASSERT(reporter, |
| 436 | SkPDFFont::CanEmbedTypeface(portableTypeface, &canon)); |
| 437 | } |