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 | |
edisonn@google.com | d9dfa18 | 2013-04-24 13:01:01 +0000 | [diff] [blame] | 8 | #include "SkBitmap.h" |
vandebo@chromium.org | 4e1cc6a | 2013-01-25 19:27:23 +0000 | [diff] [blame] | 9 | #include "SkCanvas.h" |
reed@google.com | 8a85d0c | 2011-06-24 19:12:12 +0000 | [diff] [blame] | 10 | #include "SkData.h" |
halcanary | 3d32d50 | 2015-03-01 06:55:20 -0800 | [diff] [blame] | 11 | #include "SkDocument.h" |
vandebo@chromium.org | 421d644 | 2011-07-20 17:39:01 +0000 | [diff] [blame] | 12 | #include "SkFlate.h" |
edisonn@google.com | d9dfa18 | 2013-04-24 13:01:01 +0000 | [diff] [blame] | 13 | #include "SkImageEncoder.h" |
| 14 | #include "SkMatrix.h" |
halcanary | a1f1ee9 | 2015-02-20 06:17:26 -0800 | [diff] [blame] | 15 | #include "SkPDFCanon.h" |
vandebo@chromium.org | 4e1cc6a | 2013-01-25 19:27:23 +0000 | [diff] [blame] | 16 | #include "SkPDFDevice.h" |
vandebo@chromium.org | 8459d4e | 2010-09-24 22:25:30 +0000 | [diff] [blame] | 17 | #include "SkPDFStream.h" |
| 18 | #include "SkPDFTypes.h" |
senorblanco | b0e89dc | 2014-10-20 14:03:12 -0700 | [diff] [blame] | 19 | #include "SkReadBuffer.h" |
vandebo@chromium.org | 8459d4e | 2010-09-24 22:25:30 +0000 | [diff] [blame] | 20 | #include "SkScalar.h" |
| 21 | #include "SkStream.h" |
vandebo@chromium.org | 421d644 | 2011-07-20 17:39:01 +0000 | [diff] [blame] | 22 | #include "SkTypes.h" |
tfarina@chromium.org | 8f6884a | 2014-01-24 20:56:26 +0000 | [diff] [blame] | 23 | #include "Test.h" |
vandebo@chromium.org | 8459d4e | 2010-09-24 22:25:30 +0000 | [diff] [blame] | 24 | |
commit-bot@chromium.org | 608ea65 | 2013-10-03 19:29:21 +0000 | [diff] [blame] | 25 | #define DUMMY_TEXT "DCT compessed stream." |
| 26 | |
halcanary | f7a169e | 2015-05-01 07:35:45 -0700 | [diff] [blame] | 27 | namespace { |
| 28 | struct Catalog { |
| 29 | SkPDFSubstituteMap substitutes; |
| 30 | SkPDFObjNumMap numbers; |
| 31 | }; |
| 32 | } // namespace |
reed@google.com | 8a85d0c | 2011-06-24 19:12:12 +0000 | [diff] [blame] | 33 | |
halcanary | f7a169e | 2015-05-01 07:35:45 -0700 | [diff] [blame] | 34 | template <typename T> |
| 35 | static SkString emit_to_string(T& obj, Catalog* catPtr = NULL) { |
| 36 | Catalog catalog; |
halcanary | 6a14434 | 2015-01-23 11:45:10 -0800 | [diff] [blame] | 37 | SkDynamicMemoryWStream buffer; |
halcanary | f7a169e | 2015-05-01 07:35:45 -0700 | [diff] [blame] | 38 | if (!catPtr) { |
| 39 | catPtr = &catalog; |
halcanary | 130444f | 2015-04-25 06:45:07 -0700 | [diff] [blame] | 40 | } |
halcanary | f7a169e | 2015-05-01 07:35:45 -0700 | [diff] [blame] | 41 | obj.emitObject(&buffer, catPtr->numbers, catPtr->substitutes); |
| 42 | SkAutoTDelete<SkStreamAsset> asset(buffer.detachAsStream()); |
| 43 | SkString tmp(asset->getLength()); |
| 44 | asset->read(tmp.writable_str(), asset->getLength()); |
| 45 | return tmp; |
vandebo@chromium.org | 8459d4e | 2010-09-24 22:25:30 +0000 | [diff] [blame] | 46 | } |
| 47 | |
halcanary | f7a169e | 2015-05-01 07:35:45 -0700 | [diff] [blame] | 48 | static bool eq(const SkString& str, const char* strPtr, size_t len) { |
| 49 | return len == str.size() && 0 == memcmp(str.c_str(), strPtr, len); |
vandebo@chromium.org | 421d644 | 2011-07-20 17:39:01 +0000 | [diff] [blame] | 50 | } |
| 51 | |
halcanary | f7a169e | 2015-05-01 07:35:45 -0700 | [diff] [blame] | 52 | #define ASSERT_EQL(REPORTER, SKSTRING, STRING, LEN) \ |
| 53 | do { \ |
| 54 | const char* strptr = STRING; \ |
| 55 | const SkString& sks = SKSTRING; \ |
| 56 | if (!eq(sks, strptr, LEN)) { \ |
| 57 | REPORT_FAILURE( \ |
| 58 | REPORTER, \ |
| 59 | "", \ |
| 60 | SkStringPrintf("'%s' != '%s'", strptr, sks.c_str())); \ |
| 61 | } \ |
| 62 | } while (false) |
| 63 | |
| 64 | #define ASSERT_EQ(REPORTER, SKSTRING, STRING) \ |
| 65 | do { \ |
| 66 | const char* str = STRING; \ |
| 67 | ASSERT_EQL(REPORTER, SKSTRING, str, strlen(str)); \ |
| 68 | } while (false) |
| 69 | |
| 70 | #define ASSERT_EMIT_EQ(REPORTER, OBJECT, STRING) \ |
| 71 | do { \ |
| 72 | SkString result = emit_to_string(OBJECT); \ |
| 73 | ASSERT_EQ(REPORTER, result, STRING); \ |
| 74 | } while (false) |
| 75 | |
| 76 | |
| 77 | |
vandebo@chromium.org | 421d644 | 2011-07-20 17:39:01 +0000 | [diff] [blame] | 78 | static void TestPDFStream(skiatest::Reporter* reporter) { |
| 79 | char streamBytes[] = "Test\nFoo\tBar"; |
scroggo | a1193e4 | 2015-01-21 12:09:53 -0800 | [diff] [blame] | 80 | SkAutoTDelete<SkMemoryStream> streamData(new SkMemoryStream( |
vandebo@chromium.org | d96d17b | 2013-01-04 19:31:24 +0000 | [diff] [blame] | 81 | streamBytes, strlen(streamBytes), true)); |
| 82 | SkAutoTUnref<SkPDFStream> stream(new SkPDFStream(streamData.get())); |
halcanary | f7a169e | 2015-05-01 07:35:45 -0700 | [diff] [blame] | 83 | ASSERT_EMIT_EQ(reporter, |
| 84 | *stream, |
| 85 | "<</Length 12>> stream\nTest\nFoo\tBar\nendstream"); |
| 86 | stream->insertInt("Attribute", 42); |
| 87 | ASSERT_EMIT_EQ(reporter, |
| 88 | *stream, |
| 89 | "<</Length 12\n/Attribute 42>> stream\n" |
| 90 | "Test\nFoo\tBar\nendstream"); |
vandebo@chromium.org | 421d644 | 2011-07-20 17:39:01 +0000 | [diff] [blame] | 91 | |
halcanary | 91d1d62 | 2015-02-17 14:43:06 -0800 | [diff] [blame] | 92 | { |
vandebo@chromium.org | 421d644 | 2011-07-20 17:39:01 +0000 | [diff] [blame] | 93 | char streamBytes2[] = "This is a longer string, so that compression " |
| 94 | "can do something with it. With shorter strings, " |
| 95 | "the short circuit logic cuts in and we end up " |
| 96 | "with an uncompressed string."; |
| 97 | SkAutoDataUnref streamData2(SkData::NewWithCopy(streamBytes2, |
| 98 | strlen(streamBytes2))); |
vandebo@chromium.org | d96d17b | 2013-01-04 19:31:24 +0000 | [diff] [blame] | 99 | SkAutoTUnref<SkPDFStream> stream(new SkPDFStream(streamData2.get())); |
vandebo@chromium.org | 421d644 | 2011-07-20 17:39:01 +0000 | [diff] [blame] | 100 | |
| 101 | SkDynamicMemoryWStream compressedByteStream; |
| 102 | SkFlate::Deflate(streamData2.get(), &compressedByteStream); |
| 103 | SkAutoDataUnref compressedData(compressedByteStream.copyToData()); |
| 104 | |
mtklein | cabc08c | 2015-02-19 08:29:24 -0800 | [diff] [blame] | 105 | SkDynamicMemoryWStream expected; |
halcanary | 37c46ca | 2015-03-31 12:30:20 -0700 | [diff] [blame] | 106 | expected.writeText("<</Filter /FlateDecode\n/Length 116>> stream\n"); |
mtklein | cabc08c | 2015-02-19 08:29:24 -0800 | [diff] [blame] | 107 | expected.write(compressedData->data(), compressedData->size()); |
| 108 | expected.writeText("\nendstream"); |
| 109 | SkAutoDataUnref expectedResultData2(expected.copyToData()); |
halcanary | f7a169e | 2015-05-01 07:35:45 -0700 | [diff] [blame] | 110 | SkString result = emit_to_string(*stream); |
| 111 | ASSERT_EQL(reporter, |
| 112 | result, |
| 113 | (const char*)expectedResultData2->data(), |
| 114 | expectedResultData2->size()); |
vandebo@chromium.org | 421d644 | 2011-07-20 17:39:01 +0000 | [diff] [blame] | 115 | } |
| 116 | } |
| 117 | |
halcanary | f7a169e | 2015-05-01 07:35:45 -0700 | [diff] [blame] | 118 | static void TestObjectNumberMap(skiatest::Reporter* reporter) { |
| 119 | SkPDFObjNumMap objNumMap; |
| 120 | SkAutoTUnref<SkPDFArray> a1(new SkPDFArray); |
| 121 | SkAutoTUnref<SkPDFArray> a2(new SkPDFArray); |
| 122 | SkAutoTUnref<SkPDFArray> a3(new SkPDFArray); |
vandebo@chromium.org | 8459d4e | 2010-09-24 22:25:30 +0000 | [diff] [blame] | 123 | |
halcanary | f7a169e | 2015-05-01 07:35:45 -0700 | [diff] [blame] | 124 | objNumMap.addObject(a1.get()); |
| 125 | objNumMap.addObject(a2.get()); |
| 126 | objNumMap.addObject(a3.get()); |
vandebo@chromium.org | 8459d4e | 2010-09-24 22:25:30 +0000 | [diff] [blame] | 127 | |
halcanary | f7a169e | 2015-05-01 07:35:45 -0700 | [diff] [blame] | 128 | // The objects should be numbered in the order they are added, |
| 129 | // starting with 1. |
| 130 | REPORTER_ASSERT(reporter, objNumMap.getObjectNumber(a1.get()) == 1); |
| 131 | REPORTER_ASSERT(reporter, objNumMap.getObjectNumber(a2.get()) == 2); |
| 132 | REPORTER_ASSERT(reporter, objNumMap.getObjectNumber(a3.get()) == 3); |
| 133 | // Assert that repeated calls to get the object number return |
| 134 | // consistent result. |
| 135 | REPORTER_ASSERT(reporter, objNumMap.getObjectNumber(a1.get()) == 1); |
vandebo@chromium.org | 8459d4e | 2010-09-24 22:25:30 +0000 | [diff] [blame] | 136 | } |
| 137 | |
| 138 | static void TestObjectRef(skiatest::Reporter* reporter) { |
halcanary | f7a169e | 2015-05-01 07:35:45 -0700 | [diff] [blame] | 139 | SkAutoTUnref<SkPDFArray> a1(new SkPDFArray); |
| 140 | SkAutoTUnref<SkPDFArray> a2(new SkPDFArray); |
| 141 | a2->appendObjRef(SkRef(a1.get())); |
vandebo@chromium.org | 8459d4e | 2010-09-24 22:25:30 +0000 | [diff] [blame] | 142 | |
halcanary | f7a169e | 2015-05-01 07:35:45 -0700 | [diff] [blame] | 143 | Catalog catalog; |
| 144 | catalog.numbers.addObject(a1.get()); |
| 145 | REPORTER_ASSERT(reporter, catalog.numbers.getObjectNumber(a1.get()) == 1); |
vandebo@chromium.org | 8459d4e | 2010-09-24 22:25:30 +0000 | [diff] [blame] | 146 | |
halcanary | f7a169e | 2015-05-01 07:35:45 -0700 | [diff] [blame] | 147 | SkString result = emit_to_string(*a2, &catalog); |
| 148 | // If appendObjRef misbehaves, then the result would |
| 149 | // be [[]], not [1 0 R]. |
| 150 | ASSERT_EQ(reporter, result, "[1 0 R]"); |
vandebo@chromium.org | 8459d4e | 2010-09-24 22:25:30 +0000 | [diff] [blame] | 151 | } |
| 152 | |
vandebo@chromium.org | 2ef12d4 | 2011-07-06 23:31:24 +0000 | [diff] [blame] | 153 | static void TestSubstitute(skiatest::Reporter* reporter) { |
halcanary | f3d53a0 | 2015-03-24 15:29:39 -0700 | [diff] [blame] | 154 | SkAutoTUnref<SkPDFDict> proxy(new SkPDFDict()); |
| 155 | SkAutoTUnref<SkPDFDict> stub(new SkPDFDict()); |
vandebo@chromium.org | 2ef12d4 | 2011-07-06 23:31:24 +0000 | [diff] [blame] | 156 | |
halcanary | f7a169e | 2015-05-01 07:35:45 -0700 | [diff] [blame] | 157 | proxy->insertInt("Value", 33); |
| 158 | stub->insertInt("Value", 44); |
vandebo@chromium.org | 2ef12d4 | 2011-07-06 23:31:24 +0000 | [diff] [blame] | 159 | |
halcanary | 37c46ca | 2015-03-31 12:30:20 -0700 | [diff] [blame] | 160 | SkPDFSubstituteMap substituteMap; |
| 161 | substituteMap.setSubstitute(proxy.get(), stub.get()); |
| 162 | SkPDFObjNumMap catalog; |
halcanary | f41061c | 2015-03-25 11:29:18 -0700 | [diff] [blame] | 163 | catalog.addObject(proxy.get()); |
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 | REPORTER_ASSERT(reporter, stub.get() == substituteMap.getSubstitute(proxy)); |
| 166 | REPORTER_ASSERT(reporter, proxy.get() != substituteMap.getSubstitute(stub)); |
vandebo@chromium.org | 2ef12d4 | 2011-07-06 23:31:24 +0000 | [diff] [blame] | 167 | } |
| 168 | |
vandebo@chromium.org | 4e1cc6a | 2013-01-25 19:27:23 +0000 | [diff] [blame] | 169 | // This test used to assert without the fix submitted for |
| 170 | // http://code.google.com/p/skia/issues/detail?id=1083. |
| 171 | // SKP files might have invalid glyph ids. This test ensures they are ignored, |
| 172 | // and there is no assert on input data in Debug mode. |
sugoi@google.com | 54f0d1b | 2013-02-27 19:17:41 +0000 | [diff] [blame] | 173 | static void test_issue1083() { |
halcanary | 3d32d50 | 2015-03-01 06:55:20 -0800 | [diff] [blame] | 174 | SkDynamicMemoryWStream outStream; |
| 175 | SkAutoTUnref<SkDocument> doc(SkDocument::CreatePDF(&outStream)); |
| 176 | SkCanvas* canvas = doc->beginPage(100.0f, 100.0f); |
vandebo@chromium.org | 4e1cc6a | 2013-01-25 19:27:23 +0000 | [diff] [blame] | 177 | SkPaint paint; |
| 178 | paint.setTextEncoding(SkPaint::kGlyphID_TextEncoding); |
| 179 | |
| 180 | uint16_t glyphID = 65000; |
halcanary | 3d32d50 | 2015-03-01 06:55:20 -0800 | [diff] [blame] | 181 | canvas->drawText(&glyphID, 2, 0, 0, paint); |
vandebo@chromium.org | 4e1cc6a | 2013-01-25 19:27:23 +0000 | [diff] [blame] | 182 | |
halcanary | 3d32d50 | 2015-03-01 06:55:20 -0800 | [diff] [blame] | 183 | doc->close(); |
vandebo@chromium.org | 4e1cc6a | 2013-01-25 19:27:23 +0000 | [diff] [blame] | 184 | } |
| 185 | |
halcanary | f7a169e | 2015-05-01 07:35:45 -0700 | [diff] [blame] | 186 | static void TestPDFUnion(skiatest::Reporter* reporter) { |
| 187 | SkPDFUnion boolTrue = SkPDFUnion::Bool(true); |
| 188 | ASSERT_EMIT_EQ(reporter, boolTrue, "true"); |
vandebo@chromium.org | 8459d4e | 2010-09-24 22:25:30 +0000 | [diff] [blame] | 189 | |
halcanary | f7a169e | 2015-05-01 07:35:45 -0700 | [diff] [blame] | 190 | SkPDFUnion boolFalse = SkPDFUnion::Bool(false); |
| 191 | ASSERT_EMIT_EQ(reporter, boolFalse, "false"); |
vandebo@chromium.org | 8459d4e | 2010-09-24 22:25:30 +0000 | [diff] [blame] | 192 | |
halcanary | f7a169e | 2015-05-01 07:35:45 -0700 | [diff] [blame] | 193 | SkPDFUnion int42 = SkPDFUnion::Int(42); |
| 194 | ASSERT_EMIT_EQ(reporter, int42, "42"); |
| 195 | |
| 196 | SkPDFUnion realHalf = SkPDFUnion::Scalar(SK_ScalarHalf); |
| 197 | ASSERT_EMIT_EQ(reporter, realHalf, "0.5"); |
| 198 | |
| 199 | SkPDFUnion bigScalar = SkPDFUnion::Scalar(110999.75f); |
vandebo@chromium.org | 6cc26da | 2011-05-18 17:08:05 +0000 | [diff] [blame] | 200 | #if !defined(SK_ALLOW_LARGE_PDF_SCALARS) |
halcanary | f7a169e | 2015-05-01 07:35:45 -0700 | [diff] [blame] | 201 | ASSERT_EMIT_EQ(reporter, bigScalar, "111000"); |
vandebo@chromium.org | 094316b | 2011-03-04 03:15:13 +0000 | [diff] [blame] | 202 | #else |
halcanary | f7a169e | 2015-05-01 07:35:45 -0700 | [diff] [blame] | 203 | ASSERT_EMIT_EQ(reporter, bigScalar, "110999.75"); |
vandebo@chromium.org | 094316b | 2011-03-04 03:15:13 +0000 | [diff] [blame] | 204 | |
halcanary | f7a169e | 2015-05-01 07:35:45 -0700 | [diff] [blame] | 205 | SkPDFUnion biggerScalar = SkPDFUnion::Scalar(50000000.1); |
| 206 | ASSERT_EMIT_EQ(reporter, biggerScalar, "50000000"); |
vandebo@chromium.org | 094316b | 2011-03-04 03:15:13 +0000 | [diff] [blame] | 207 | |
halcanary | f7a169e | 2015-05-01 07:35:45 -0700 | [diff] [blame] | 208 | SkPDFUnion smallestScalar = SkPDFUnion::Scalar(1.0 / 65536); |
| 209 | ASSERT_EMIT_EQ(reporter, smallestScalar, "0.00001526"); |
vandebo@chromium.org | 094316b | 2011-03-04 03:15:13 +0000 | [diff] [blame] | 210 | #endif |
| 211 | |
halcanary | f7a169e | 2015-05-01 07:35:45 -0700 | [diff] [blame] | 212 | SkPDFUnion stringSimple = SkPDFUnion::String("test ) string ( foo"); |
| 213 | ASSERT_EMIT_EQ(reporter, stringSimple, "(test \\) string \\( foo)"); |
vandebo@chromium.org | 8459d4e | 2010-09-24 22:25:30 +0000 | [diff] [blame] | 214 | |
halcanary | f7a169e | 2015-05-01 07:35:45 -0700 | [diff] [blame] | 215 | SkString stringComplexInput("\ttest ) string ( foo"); |
| 216 | SkPDFUnion stringComplex = SkPDFUnion::String(stringComplexInput); |
| 217 | ASSERT_EMIT_EQ(reporter, |
| 218 | stringComplex, |
| 219 | "<0974657374202920737472696E67202820666F6F>"); |
vandebo@chromium.org | 8459d4e | 2010-09-24 22:25:30 +0000 | [diff] [blame] | 220 | |
halcanary | f7a169e | 2015-05-01 07:35:45 -0700 | [diff] [blame] | 221 | SkString nameInput("Test name\twith#tab"); |
| 222 | SkPDFUnion name = SkPDFUnion::Name(nameInput); |
| 223 | ASSERT_EMIT_EQ(reporter, name, "/Test#20name#09with#23tab"); |
| 224 | |
| 225 | SkString nameInput2("A#/%()<>[]{}B"); |
| 226 | SkPDFUnion name2 = SkPDFUnion::Name(nameInput2); |
| 227 | ASSERT_EMIT_EQ(reporter, name2, "/A#23#2F#25#28#29#3C#3E#5B#5D#7B#7DB"); |
| 228 | |
| 229 | SkPDFUnion name3 = SkPDFUnion::Name("SimpleNameWithOnlyPrintableASCII"); |
| 230 | ASSERT_EMIT_EQ(reporter, name3, "/SimpleNameWithOnlyPrintableASCII"); |
vandebo@chromium.org | 251a766 | 2012-09-21 17:50:50 +0000 | [diff] [blame] | 231 | |
vandebo@chromium.org | c0376fe | 2012-03-05 18:44:33 +0000 | [diff] [blame] | 232 | // Test that we correctly handle characters with the high-bit set. |
halcanary | f7a169e | 2015-05-01 07:35:45 -0700 | [diff] [blame] | 233 | SkString highBitString("\xDE\xAD" "be\xEF"); |
| 234 | SkPDFUnion highBitName = SkPDFUnion::Name(highBitString); |
| 235 | ASSERT_EMIT_EQ(reporter, highBitName, "/#DE#ADbe#EF"); |
| 236 | } |
vandebo@chromium.org | c0376fe | 2012-03-05 18:44:33 +0000 | [diff] [blame] | 237 | |
halcanary | f7a169e | 2015-05-01 07:35:45 -0700 | [diff] [blame] | 238 | static void TestPDFArray(skiatest::Reporter* reporter) { |
vandebo@chromium.org | d96d17b | 2013-01-04 19:31:24 +0000 | [diff] [blame] | 239 | SkAutoTUnref<SkPDFArray> array(new SkPDFArray); |
halcanary | f7a169e | 2015-05-01 07:35:45 -0700 | [diff] [blame] | 240 | ASSERT_EMIT_EQ(reporter, *array, "[]"); |
vandebo@chromium.org | 8459d4e | 2010-09-24 22:25:30 +0000 | [diff] [blame] | 241 | |
halcanary | f7a169e | 2015-05-01 07:35:45 -0700 | [diff] [blame] | 242 | array->appendInt(42); |
| 243 | ASSERT_EMIT_EQ(reporter, *array, "[42]"); |
| 244 | |
| 245 | array->appendScalar(SK_ScalarHalf); |
| 246 | ASSERT_EMIT_EQ(reporter, *array, "[42 0.5]"); |
| 247 | |
| 248 | array->appendInt(0); |
| 249 | ASSERT_EMIT_EQ(reporter, *array, "[42 0.5 0]"); |
| 250 | |
| 251 | array->appendBool(true); |
| 252 | ASSERT_EMIT_EQ(reporter, *array, "[42 0.5 0 true]"); |
| 253 | |
| 254 | array->appendName("ThisName"); |
| 255 | ASSERT_EMIT_EQ(reporter, *array, "[42 0.5 0 true /ThisName]"); |
| 256 | |
| 257 | array->appendName(SkString("AnotherName")); |
| 258 | ASSERT_EMIT_EQ(reporter, *array, "[42 0.5 0 true /ThisName /AnotherName]"); |
| 259 | |
| 260 | array->appendString("This String"); |
| 261 | ASSERT_EMIT_EQ(reporter, *array, |
| 262 | "[42 0.5 0 true /ThisName /AnotherName (This String)]"); |
| 263 | |
| 264 | array->appendString(SkString("Another String")); |
| 265 | ASSERT_EMIT_EQ(reporter, *array, |
| 266 | "[42 0.5 0 true /ThisName /AnotherName (This String) " |
| 267 | "(Another String)]"); |
| 268 | |
| 269 | SkAutoTUnref<SkPDFArray> innerArray(new SkPDFArray); |
| 270 | innerArray->appendInt(-1); |
| 271 | array->appendObject(innerArray.detach()); |
| 272 | ASSERT_EMIT_EQ(reporter, *array, |
| 273 | "[42 0.5 0 true /ThisName /AnotherName (This String) " |
| 274 | "(Another String) [-1]]"); |
| 275 | |
| 276 | SkAutoTUnref<SkPDFArray> referencedArray(new SkPDFArray); |
| 277 | Catalog catalog; |
| 278 | catalog.numbers.addObject(referencedArray.get()); |
| 279 | REPORTER_ASSERT(reporter, catalog.numbers.getObjectNumber( |
| 280 | referencedArray.get()) == 1); |
| 281 | array->appendObjRef(referencedArray.detach()); |
| 282 | |
| 283 | SkString result = emit_to_string(*array, &catalog); |
| 284 | ASSERT_EQ(reporter, result, |
| 285 | "[42 0.5 0 true /ThisName /AnotherName (This String) " |
| 286 | "(Another String) [-1] 1 0 R]"); |
| 287 | } |
| 288 | |
| 289 | static void TestPDFDict(skiatest::Reporter* reporter) { |
vandebo@chromium.org | d96d17b | 2013-01-04 19:31:24 +0000 | [diff] [blame] | 290 | SkAutoTUnref<SkPDFDict> dict(new SkPDFDict); |
halcanary | f7a169e | 2015-05-01 07:35:45 -0700 | [diff] [blame] | 291 | ASSERT_EMIT_EQ(reporter, *dict, "<<>>"); |
| 292 | |
| 293 | dict->insertInt("n1", SkToSizeT(42)); |
| 294 | ASSERT_EMIT_EQ(reporter, *dict, "<</n1 42>>"); |
| 295 | |
| 296 | dict.reset(new SkPDFDict); |
| 297 | ASSERT_EMIT_EQ(reporter, *dict, "<<>>"); |
| 298 | |
| 299 | dict->insertInt("n1", 42); |
| 300 | ASSERT_EMIT_EQ(reporter, *dict, "<</n1 42>>"); |
| 301 | |
| 302 | dict->insertScalar("n2", SK_ScalarHalf); |
| 303 | |
halcanary | 130444f | 2015-04-25 06:45:07 -0700 | [diff] [blame] | 304 | SkString n3("n3"); |
halcanary | f7a169e | 2015-05-01 07:35:45 -0700 | [diff] [blame] | 305 | SkAutoTUnref<SkPDFArray> innerArray(new SkPDFArray); |
| 306 | innerArray->appendInt(-100); |
| 307 | dict->insertObject(n3, innerArray.detach()); |
| 308 | 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] | 309 | |
halcanary | f7a169e | 2015-05-01 07:35:45 -0700 | [diff] [blame] | 310 | dict.reset(new SkPDFDict); |
| 311 | ASSERT_EMIT_EQ(reporter, *dict, "<<>>"); |
| 312 | |
| 313 | dict->insertInt("n1", 24); |
| 314 | ASSERT_EMIT_EQ(reporter, *dict, "<</n1 24>>"); |
| 315 | |
| 316 | dict->insertInt("n2", SkToSizeT(99)); |
| 317 | ASSERT_EMIT_EQ(reporter, *dict, "<</n1 24\n/n2 99>>"); |
| 318 | |
| 319 | dict->insertScalar("n3", SK_ScalarHalf); |
| 320 | ASSERT_EMIT_EQ(reporter, *dict, "<</n1 24\n/n2 99\n/n3 0.5>>"); |
| 321 | |
| 322 | dict->insertName("n4", "AName"); |
| 323 | ASSERT_EMIT_EQ(reporter, *dict, "<</n1 24\n/n2 99\n/n3 0.5\n/n4 /AName>>"); |
| 324 | |
| 325 | dict->insertName("n5", SkString("AnotherName")); |
| 326 | ASSERT_EMIT_EQ(reporter, *dict, "<</n1 24\n/n2 99\n/n3 0.5\n/n4 /AName\n" |
| 327 | "/n5 /AnotherName>>"); |
| 328 | |
| 329 | dict->insertString("n6", "A String"); |
| 330 | ASSERT_EMIT_EQ(reporter, *dict, "<</n1 24\n/n2 99\n/n3 0.5\n/n4 /AName\n" |
| 331 | "/n5 /AnotherName\n/n6 (A String)>>"); |
| 332 | |
| 333 | dict->insertString("n7", SkString("Another String")); |
| 334 | ASSERT_EMIT_EQ(reporter, *dict, "<</n1 24\n/n2 99\n/n3 0.5\n/n4 /AName\n" |
| 335 | "/n5 /AnotherName\n/n6 (A String)\n/n7 (Another String)>>"); |
| 336 | |
| 337 | dict.reset(new SkPDFDict("DType")); |
| 338 | ASSERT_EMIT_EQ(reporter, *dict, "<</Type /DType>>"); |
| 339 | |
| 340 | SkAutoTUnref<SkPDFArray> referencedArray(new SkPDFArray); |
| 341 | Catalog catalog; |
| 342 | catalog.numbers.addObject(referencedArray.get()); |
| 343 | REPORTER_ASSERT(reporter, catalog.numbers.getObjectNumber( |
| 344 | referencedArray.get()) == 1); |
| 345 | dict->insertObjRef("n1", referencedArray.detach()); |
| 346 | SkString result = emit_to_string(*dict, &catalog); |
| 347 | ASSERT_EQ(reporter, result, "<</Type /DType\n/n1 1 0 R>>"); |
| 348 | } |
| 349 | |
| 350 | DEF_TEST(PDFPrimitives, reporter) { |
| 351 | TestPDFUnion(reporter); |
| 352 | TestPDFArray(reporter); |
| 353 | TestPDFDict(reporter); |
vandebo@chromium.org | 421d644 | 2011-07-20 17:39:01 +0000 | [diff] [blame] | 354 | TestPDFStream(reporter); |
halcanary | f7a169e | 2015-05-01 07:35:45 -0700 | [diff] [blame] | 355 | TestObjectNumberMap(reporter); |
vandebo@chromium.org | 8459d4e | 2010-09-24 22:25:30 +0000 | [diff] [blame] | 356 | TestObjectRef(reporter); |
vandebo@chromium.org | 2ef12d4 | 2011-07-06 23:31:24 +0000 | [diff] [blame] | 357 | TestSubstitute(reporter); |
sugoi@google.com | 54f0d1b | 2013-02-27 19:17:41 +0000 | [diff] [blame] | 358 | test_issue1083(); |
vandebo@chromium.org | 8459d4e | 2010-09-24 22:25:30 +0000 | [diff] [blame] | 359 | } |
senorblanco | b0e89dc | 2014-10-20 14:03:12 -0700 | [diff] [blame] | 360 | |
| 361 | namespace { |
| 362 | |
| 363 | class DummyImageFilter : public SkImageFilter { |
| 364 | public: |
| 365 | DummyImageFilter(bool visited = false) : SkImageFilter(0, NULL), fVisited(visited) {} |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 366 | ~DummyImageFilter() override {} |
senorblanco | b0e89dc | 2014-10-20 14:03:12 -0700 | [diff] [blame] | 367 | virtual bool onFilterImage(Proxy*, const SkBitmap& src, const Context&, |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 368 | SkBitmap* result, SkIPoint* offset) const override { |
senorblanco | b0e89dc | 2014-10-20 14:03:12 -0700 | [diff] [blame] | 369 | fVisited = true; |
| 370 | offset->fX = offset->fY = 0; |
| 371 | *result = src; |
| 372 | return true; |
| 373 | } |
robertphillips | f3f5bad | 2014-12-19 13:49:15 -0800 | [diff] [blame] | 374 | SK_TO_STRING_OVERRIDE() |
senorblanco | b0e89dc | 2014-10-20 14:03:12 -0700 | [diff] [blame] | 375 | SK_DECLARE_PUBLIC_FLATTENABLE_DESERIALIZATION_PROCS(DummyImageFilter) |
senorblanco | b0e89dc | 2014-10-20 14:03:12 -0700 | [diff] [blame] | 376 | bool visited() const { return fVisited; } |
| 377 | |
| 378 | private: |
| 379 | mutable bool fVisited; |
| 380 | }; |
| 381 | |
| 382 | SkFlattenable* DummyImageFilter::CreateProc(SkReadBuffer& buffer) { |
| 383 | SK_IMAGEFILTER_UNFLATTEN_COMMON(common, 0); |
| 384 | bool visited = buffer.readBool(); |
| 385 | return SkNEW_ARGS(DummyImageFilter, (visited)); |
| 386 | } |
| 387 | |
robertphillips | f3f5bad | 2014-12-19 13:49:15 -0800 | [diff] [blame] | 388 | #ifndef SK_IGNORE_TO_STRING |
| 389 | void DummyImageFilter::toString(SkString* str) const { |
| 390 | str->appendf("DummyImageFilter: ("); |
| 391 | str->append(")"); |
| 392 | } |
| 393 | #endif |
| 394 | |
senorblanco | b0e89dc | 2014-10-20 14:03:12 -0700 | [diff] [blame] | 395 | }; |
| 396 | |
| 397 | // Check that PDF rendering of image filters successfully falls back to |
| 398 | // CPU rasterization. |
| 399 | DEF_TEST(PDFImageFilter, reporter) { |
halcanary | 3d32d50 | 2015-03-01 06:55:20 -0800 | [diff] [blame] | 400 | SkDynamicMemoryWStream stream; |
| 401 | SkAutoTUnref<SkDocument> doc(SkDocument::CreatePDF(&stream)); |
| 402 | SkCanvas* canvas = doc->beginPage(100.0f, 100.0f); |
| 403 | |
senorblanco | b0e89dc | 2014-10-20 14:03:12 -0700 | [diff] [blame] | 404 | SkAutoTUnref<DummyImageFilter> filter(new DummyImageFilter()); |
| 405 | |
| 406 | // Filter just created; should be unvisited. |
| 407 | REPORTER_ASSERT(reporter, !filter->visited()); |
| 408 | SkPaint paint; |
| 409 | paint.setImageFilter(filter.get()); |
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 | } |