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" |
vandebo@chromium.org | 421d644 | 2011-07-20 17:39:01 +0000 | [diff] [blame] | 11 | #include "SkFlate.h" |
edisonn@google.com | d9dfa18 | 2013-04-24 13:01:01 +0000 | [diff] [blame] | 12 | #include "SkImageEncoder.h" |
| 13 | #include "SkMatrix.h" |
vandebo@chromium.org | 8459d4e | 2010-09-24 22:25:30 +0000 | [diff] [blame] | 14 | #include "SkPDFCatalog.h" |
vandebo@chromium.org | 4e1cc6a | 2013-01-25 19:27:23 +0000 | [diff] [blame] | 15 | #include "SkPDFDevice.h" |
vandebo@chromium.org | 8459d4e | 2010-09-24 22:25:30 +0000 | [diff] [blame] | 16 | #include "SkPDFStream.h" |
| 17 | #include "SkPDFTypes.h" |
| 18 | #include "SkScalar.h" |
| 19 | #include "SkStream.h" |
vandebo@chromium.org | 421d644 | 2011-07-20 17:39:01 +0000 | [diff] [blame] | 20 | #include "SkTypes.h" |
tfarina@chromium.org | 8f6884a | 2014-01-24 20:56:26 +0000 | [diff] [blame] | 21 | #include "Test.h" |
vandebo@chromium.org | 8459d4e | 2010-09-24 22:25:30 +0000 | [diff] [blame] | 22 | |
vandebo@chromium.org | 2ef12d4 | 2011-07-06 23:31:24 +0000 | [diff] [blame] | 23 | class SkPDFTestDict : public SkPDFDict { |
| 24 | public: |
edisonn@google.com | 6addb19 | 2013-04-02 15:33:08 +0000 | [diff] [blame] | 25 | virtual void getResources(const SkTSet<SkPDFObject*>& knownResourceObjects, |
| 26 | SkTSet<SkPDFObject*>* newResourceObjects) { |
vandebo@chromium.org | 2ef12d4 | 2011-07-06 23:31:24 +0000 | [diff] [blame] | 27 | for (int i = 0; i < fResources.count(); i++) { |
edisonn@google.com | 6addb19 | 2013-04-02 15:33:08 +0000 | [diff] [blame] | 28 | newResourceObjects->add(fResources[i]); |
vandebo@chromium.org | 2ef12d4 | 2011-07-06 23:31:24 +0000 | [diff] [blame] | 29 | fResources[i]->ref(); |
| 30 | } |
| 31 | } |
| 32 | |
| 33 | void addResource(SkPDFObject* object) { |
| 34 | fResources.append(1, &object); |
| 35 | } |
| 36 | |
| 37 | private: |
| 38 | SkTDArray<SkPDFObject*> fResources; |
| 39 | }; |
| 40 | |
commit-bot@chromium.org | 608ea65 | 2013-10-03 19:29:21 +0000 | [diff] [blame] | 41 | #define DUMMY_TEXT "DCT compessed stream." |
| 42 | |
| 43 | static SkData* encode_to_dct_data(size_t* pixelRefOffset, const SkBitmap& bitmap) { |
| 44 | *pixelRefOffset = 0; |
| 45 | return SkData::NewWithProc(DUMMY_TEXT, sizeof(DUMMY_TEXT) - 1, NULL, NULL); |
edisonn@google.com | d9dfa18 | 2013-04-24 13:01:01 +0000 | [diff] [blame] | 46 | } |
| 47 | |
reed@google.com | 8a85d0c | 2011-06-24 19:12:12 +0000 | [diff] [blame] | 48 | static bool stream_equals(const SkDynamicMemoryWStream& stream, size_t offset, |
| 49 | const void* buffer, size_t len) { |
| 50 | SkAutoDataUnref data(stream.copyToData()); |
robertphillips@google.com | 59f46b8 | 2012-07-10 17:30:58 +0000 | [diff] [blame] | 51 | if (offset + len > data->size()) { |
reed@google.com | 8a85d0c | 2011-06-24 19:12:12 +0000 | [diff] [blame] | 52 | return false; |
| 53 | } |
robertphillips@google.com | 59f46b8 | 2012-07-10 17:30:58 +0000 | [diff] [blame] | 54 | return memcmp(data->bytes() + offset, buffer, len) == 0; |
reed@google.com | 8a85d0c | 2011-06-24 19:12:12 +0000 | [diff] [blame] | 55 | } |
| 56 | |
edisonn@google.com | d9dfa18 | 2013-04-24 13:01:01 +0000 | [diff] [blame] | 57 | static bool stream_contains(const SkDynamicMemoryWStream& stream, |
| 58 | const char* buffer) { |
| 59 | SkAutoDataUnref data(stream.copyToData()); |
| 60 | int len = strlen(buffer); // our buffer does not have EOSs. |
| 61 | |
| 62 | for (int offset = 0 ; offset < (int)data->size() - len; offset++) { |
| 63 | if (memcmp(data->bytes() + offset, buffer, len) == 0) { |
| 64 | return true; |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | return false; |
| 69 | } |
| 70 | |
vandebo@chromium.org | 8459d4e | 2010-09-24 22:25:30 +0000 | [diff] [blame] | 71 | static void CheckObjectOutput(skiatest::Reporter* reporter, SkPDFObject* obj, |
vandebo@chromium.org | 421d644 | 2011-07-20 17:39:01 +0000 | [diff] [blame] | 72 | const char* expectedData, size_t expectedSize, |
| 73 | bool indirect, bool compression) { |
| 74 | SkPDFDocument::Flags docFlags = (SkPDFDocument::Flags) 0; |
| 75 | if (!compression) { |
edisonn@google.com | 8c02061 | 2013-03-12 19:53:16 +0000 | [diff] [blame] | 76 | docFlags = SkTBitOr(docFlags, SkPDFDocument::kFavorSpeedOverSize_Flags); |
vandebo@chromium.org | 421d644 | 2011-07-20 17:39:01 +0000 | [diff] [blame] | 77 | } |
| 78 | SkPDFCatalog catalog(docFlags); |
vandebo@chromium.org | 2ef12d4 | 2011-07-06 23:31:24 +0000 | [diff] [blame] | 79 | size_t directSize = obj->getOutputSize(&catalog, false); |
vandebo@chromium.org | 421d644 | 2011-07-20 17:39:01 +0000 | [diff] [blame] | 80 | REPORTER_ASSERT(reporter, directSize == expectedSize); |
vandebo@chromium.org | 8459d4e | 2010-09-24 22:25:30 +0000 | [diff] [blame] | 81 | |
| 82 | SkDynamicMemoryWStream buffer; |
vandebo@chromium.org | 2ef12d4 | 2011-07-06 23:31:24 +0000 | [diff] [blame] | 83 | obj->emit(&buffer, &catalog, false); |
vandebo@chromium.org | 8459d4e | 2010-09-24 22:25:30 +0000 | [diff] [blame] | 84 | REPORTER_ASSERT(reporter, directSize == buffer.getOffset()); |
vandebo@chromium.org | 421d644 | 2011-07-20 17:39:01 +0000 | [diff] [blame] | 85 | REPORTER_ASSERT(reporter, stream_equals(buffer, 0, expectedData, |
reed@google.com | 8a85d0c | 2011-06-24 19:12:12 +0000 | [diff] [blame] | 86 | directSize)); |
vandebo@chromium.org | 8459d4e | 2010-09-24 22:25:30 +0000 | [diff] [blame] | 87 | |
| 88 | if (indirect) { |
| 89 | // Indirect output. |
| 90 | static char header[] = "1 0 obj\n"; |
| 91 | static size_t headerLen = strlen(header); |
| 92 | static char footer[] = "\nendobj\n"; |
| 93 | static size_t footerLen = strlen(footer); |
| 94 | |
vandebo@chromium.org | 8459d4e | 2010-09-24 22:25:30 +0000 | [diff] [blame] | 95 | catalog.addObject(obj, false); |
| 96 | |
| 97 | size_t indirectSize = obj->getOutputSize(&catalog, true); |
| 98 | REPORTER_ASSERT(reporter, |
| 99 | indirectSize == directSize + headerLen + footerLen); |
| 100 | |
| 101 | buffer.reset(); |
vandebo@chromium.org | 2ef12d4 | 2011-07-06 23:31:24 +0000 | [diff] [blame] | 102 | obj->emit(&buffer, &catalog, true); |
vandebo@chromium.org | 8459d4e | 2010-09-24 22:25:30 +0000 | [diff] [blame] | 103 | REPORTER_ASSERT(reporter, indirectSize == buffer.getOffset()); |
reed@google.com | 8a85d0c | 2011-06-24 19:12:12 +0000 | [diff] [blame] | 104 | REPORTER_ASSERT(reporter, stream_equals(buffer, 0, header, headerLen)); |
vandebo@chromium.org | 421d644 | 2011-07-20 17:39:01 +0000 | [diff] [blame] | 105 | REPORTER_ASSERT(reporter, stream_equals(buffer, headerLen, expectedData, |
reed@google.com | 8a85d0c | 2011-06-24 19:12:12 +0000 | [diff] [blame] | 106 | directSize)); |
| 107 | REPORTER_ASSERT(reporter, stream_equals(buffer, headerLen + directSize, |
| 108 | footer, footerLen)); |
vandebo@chromium.org | 8459d4e | 2010-09-24 22:25:30 +0000 | [diff] [blame] | 109 | } |
| 110 | } |
| 111 | |
vandebo@chromium.org | 421d644 | 2011-07-20 17:39:01 +0000 | [diff] [blame] | 112 | static void SimpleCheckObjectOutput(skiatest::Reporter* reporter, |
| 113 | SkPDFObject* obj, |
robertphillips@google.com | 4debcac | 2012-05-14 16:33:36 +0000 | [diff] [blame] | 114 | const char* expectedResult) { |
| 115 | CheckObjectOutput(reporter, obj, expectedResult, |
| 116 | strlen(expectedResult), true, false); |
vandebo@chromium.org | 421d644 | 2011-07-20 17:39:01 +0000 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | static void TestPDFStream(skiatest::Reporter* reporter) { |
| 120 | char streamBytes[] = "Test\nFoo\tBar"; |
vandebo@chromium.org | d96d17b | 2013-01-04 19:31:24 +0000 | [diff] [blame] | 121 | SkAutoTUnref<SkMemoryStream> streamData(new SkMemoryStream( |
| 122 | streamBytes, strlen(streamBytes), true)); |
| 123 | SkAutoTUnref<SkPDFStream> stream(new SkPDFStream(streamData.get())); |
vandebo@chromium.org | 421d644 | 2011-07-20 17:39:01 +0000 | [diff] [blame] | 124 | SimpleCheckObjectOutput( |
| 125 | reporter, stream.get(), |
| 126 | "<</Length 12\n>> stream\nTest\nFoo\tBar\nendstream"); |
| 127 | stream->insert("Attribute", new SkPDFInt(42))->unref(); |
| 128 | SimpleCheckObjectOutput(reporter, stream.get(), |
| 129 | "<</Length 12\n/Attribute 42\n>> stream\n" |
| 130 | "Test\nFoo\tBar\nendstream"); |
| 131 | |
| 132 | if (SkFlate::HaveFlate()) { |
| 133 | char streamBytes2[] = "This is a longer string, so that compression " |
| 134 | "can do something with it. With shorter strings, " |
| 135 | "the short circuit logic cuts in and we end up " |
| 136 | "with an uncompressed string."; |
| 137 | SkAutoDataUnref streamData2(SkData::NewWithCopy(streamBytes2, |
| 138 | strlen(streamBytes2))); |
vandebo@chromium.org | d96d17b | 2013-01-04 19:31:24 +0000 | [diff] [blame] | 139 | SkAutoTUnref<SkPDFStream> stream(new SkPDFStream(streamData2.get())); |
vandebo@chromium.org | 421d644 | 2011-07-20 17:39:01 +0000 | [diff] [blame] | 140 | |
| 141 | SkDynamicMemoryWStream compressedByteStream; |
| 142 | SkFlate::Deflate(streamData2.get(), &compressedByteStream); |
| 143 | SkAutoDataUnref compressedData(compressedByteStream.copyToData()); |
| 144 | |
| 145 | // Check first without compression. |
| 146 | SkDynamicMemoryWStream expectedResult1; |
| 147 | expectedResult1.writeText("<</Length 167\n>> stream\n"); |
| 148 | expectedResult1.writeText(streamBytes2); |
| 149 | expectedResult1.writeText("\nendstream"); |
| 150 | SkAutoDataUnref expectedResultData1(expectedResult1.copyToData()); |
| 151 | CheckObjectOutput(reporter, stream.get(), |
robertphillips@google.com | 59f46b8 | 2012-07-10 17:30:58 +0000 | [diff] [blame] | 152 | (const char*) expectedResultData1->data(), |
| 153 | expectedResultData1->size(), true, false); |
vandebo@chromium.org | 421d644 | 2011-07-20 17:39:01 +0000 | [diff] [blame] | 154 | |
| 155 | // Then again with compression. |
| 156 | SkDynamicMemoryWStream expectedResult2; |
| 157 | expectedResult2.writeText("<</Filter /FlateDecode\n/Length 116\n" |
| 158 | ">> stream\n"); |
robertphillips@google.com | 59f46b8 | 2012-07-10 17:30:58 +0000 | [diff] [blame] | 159 | expectedResult2.write(compressedData->data(), compressedData->size()); |
vandebo@chromium.org | 421d644 | 2011-07-20 17:39:01 +0000 | [diff] [blame] | 160 | expectedResult2.writeText("\nendstream"); |
| 161 | SkAutoDataUnref expectedResultData2(expectedResult2.copyToData()); |
| 162 | CheckObjectOutput(reporter, stream.get(), |
robertphillips@google.com | 59f46b8 | 2012-07-10 17:30:58 +0000 | [diff] [blame] | 163 | (const char*) expectedResultData2->data(), |
| 164 | expectedResultData2->size(), true, true); |
vandebo@chromium.org | 421d644 | 2011-07-20 17:39:01 +0000 | [diff] [blame] | 165 | } |
| 166 | } |
| 167 | |
vandebo@chromium.org | 8459d4e | 2010-09-24 22:25:30 +0000 | [diff] [blame] | 168 | static void TestCatalog(skiatest::Reporter* reporter) { |
vandebo@chromium.org | 421d644 | 2011-07-20 17:39:01 +0000 | [diff] [blame] | 169 | SkPDFCatalog catalog((SkPDFDocument::Flags)0); |
vandebo@chromium.org | d96d17b | 2013-01-04 19:31:24 +0000 | [diff] [blame] | 170 | SkAutoTUnref<SkPDFInt> int1(new SkPDFInt(1)); |
| 171 | SkAutoTUnref<SkPDFInt> int2(new SkPDFInt(2)); |
| 172 | SkAutoTUnref<SkPDFInt> int3(new SkPDFInt(3)); |
| 173 | int1.get()->ref(); |
| 174 | SkAutoTUnref<SkPDFInt> int1Again(int1.get()); |
vandebo@chromium.org | 8459d4e | 2010-09-24 22:25:30 +0000 | [diff] [blame] | 175 | |
| 176 | catalog.addObject(int1.get(), false); |
| 177 | catalog.addObject(int2.get(), false); |
| 178 | catalog.addObject(int3.get(), false); |
| 179 | |
| 180 | REPORTER_ASSERT(reporter, catalog.getObjectNumberSize(int1.get()) == 3); |
| 181 | REPORTER_ASSERT(reporter, catalog.getObjectNumberSize(int2.get()) == 3); |
| 182 | REPORTER_ASSERT(reporter, catalog.getObjectNumberSize(int3.get()) == 3); |
| 183 | |
| 184 | SkDynamicMemoryWStream buffer; |
| 185 | catalog.emitObjectNumber(&buffer, int1.get()); |
| 186 | catalog.emitObjectNumber(&buffer, int2.get()); |
| 187 | catalog.emitObjectNumber(&buffer, int3.get()); |
| 188 | catalog.emitObjectNumber(&buffer, int1Again.get()); |
| 189 | char expectedResult[] = "1 02 03 01 0"; |
reed@google.com | 8a85d0c | 2011-06-24 19:12:12 +0000 | [diff] [blame] | 190 | REPORTER_ASSERT(reporter, stream_equals(buffer, 0, expectedResult, |
| 191 | strlen(expectedResult))); |
vandebo@chromium.org | 8459d4e | 2010-09-24 22:25:30 +0000 | [diff] [blame] | 192 | } |
| 193 | |
| 194 | static void TestObjectRef(skiatest::Reporter* reporter) { |
vandebo@chromium.org | d96d17b | 2013-01-04 19:31:24 +0000 | [diff] [blame] | 195 | SkAutoTUnref<SkPDFInt> int1(new SkPDFInt(1)); |
| 196 | SkAutoTUnref<SkPDFInt> int2(new SkPDFInt(2)); |
| 197 | SkAutoTUnref<SkPDFObjRef> int2ref(new SkPDFObjRef(int2.get())); |
vandebo@chromium.org | 8459d4e | 2010-09-24 22:25:30 +0000 | [diff] [blame] | 198 | |
vandebo@chromium.org | 421d644 | 2011-07-20 17:39:01 +0000 | [diff] [blame] | 199 | SkPDFCatalog catalog((SkPDFDocument::Flags)0); |
vandebo@chromium.org | 8459d4e | 2010-09-24 22:25:30 +0000 | [diff] [blame] | 200 | catalog.addObject(int1.get(), false); |
| 201 | catalog.addObject(int2.get(), false); |
| 202 | REPORTER_ASSERT(reporter, catalog.getObjectNumberSize(int1.get()) == 3); |
| 203 | REPORTER_ASSERT(reporter, catalog.getObjectNumberSize(int2.get()) == 3); |
| 204 | |
| 205 | char expectedResult[] = "2 0 R"; |
| 206 | SkDynamicMemoryWStream buffer; |
| 207 | int2ref->emitObject(&buffer, &catalog, false); |
| 208 | REPORTER_ASSERT(reporter, buffer.getOffset() == strlen(expectedResult)); |
reed@google.com | 8a85d0c | 2011-06-24 19:12:12 +0000 | [diff] [blame] | 209 | REPORTER_ASSERT(reporter, stream_equals(buffer, 0, expectedResult, |
| 210 | buffer.getOffset())); |
vandebo@chromium.org | 8459d4e | 2010-09-24 22:25:30 +0000 | [diff] [blame] | 211 | } |
| 212 | |
vandebo@chromium.org | 2ef12d4 | 2011-07-06 23:31:24 +0000 | [diff] [blame] | 213 | static void TestSubstitute(skiatest::Reporter* reporter) { |
vandebo@chromium.org | d96d17b | 2013-01-04 19:31:24 +0000 | [diff] [blame] | 214 | SkAutoTUnref<SkPDFTestDict> proxy(new SkPDFTestDict()); |
| 215 | SkAutoTUnref<SkPDFTestDict> stub(new SkPDFTestDict()); |
| 216 | SkAutoTUnref<SkPDFInt> int33(new SkPDFInt(33)); |
| 217 | SkAutoTUnref<SkPDFDict> stubResource(new SkPDFDict()); |
| 218 | SkAutoTUnref<SkPDFInt> int44(new SkPDFInt(44)); |
vandebo@chromium.org | 2ef12d4 | 2011-07-06 23:31:24 +0000 | [diff] [blame] | 219 | |
| 220 | stub->insert("Value", int33.get()); |
| 221 | stubResource->insert("InnerValue", int44.get()); |
| 222 | stub->addResource(stubResource.get()); |
| 223 | |
vandebo@chromium.org | 421d644 | 2011-07-20 17:39:01 +0000 | [diff] [blame] | 224 | SkPDFCatalog catalog((SkPDFDocument::Flags)0); |
vandebo@chromium.org | 2ef12d4 | 2011-07-06 23:31:24 +0000 | [diff] [blame] | 225 | catalog.addObject(proxy.get(), false); |
| 226 | catalog.setSubstitute(proxy.get(), stub.get()); |
| 227 | |
| 228 | SkDynamicMemoryWStream buffer; |
| 229 | proxy->emit(&buffer, &catalog, false); |
| 230 | catalog.emitSubstituteResources(&buffer, false); |
| 231 | |
vandebo@chromium.org | d3a094c | 2011-07-25 22:22:25 +0000 | [diff] [blame] | 232 | char objectResult[] = "2 0 obj\n<</Value 33\n>>\nendobj\n"; |
| 233 | REPORTER_ASSERT( |
| 234 | reporter, |
| 235 | catalog.setFileOffset(proxy.get(), 0) == strlen(objectResult)); |
| 236 | |
vandebo@chromium.org | 2ef12d4 | 2011-07-06 23:31:24 +0000 | [diff] [blame] | 237 | char expectedResult[] = |
| 238 | "<</Value 33\n>>1 0 obj\n<</InnerValue 44\n>>\nendobj\n"; |
| 239 | REPORTER_ASSERT(reporter, buffer.getOffset() == strlen(expectedResult)); |
| 240 | REPORTER_ASSERT(reporter, stream_equals(buffer, 0, expectedResult, |
| 241 | buffer.getOffset())); |
| 242 | } |
| 243 | |
edisonn@google.com | d9dfa18 | 2013-04-24 13:01:01 +0000 | [diff] [blame] | 244 | // Create a bitmap that would be very eficiently compressed in a ZIP. |
edisonn@google.com | f1a358f | 2013-04-24 13:21:49 +0000 | [diff] [blame] | 245 | static void setup_bitmap(SkBitmap* bitmap, int width, int height) { |
mike@reedtribe.org | deee496 | 2014-02-13 14:41:43 +0000 | [diff] [blame] | 246 | bitmap->allocN32Pixels(width, height); |
edisonn@google.com | d9dfa18 | 2013-04-24 13:01:01 +0000 | [diff] [blame] | 247 | bitmap->eraseColor(SK_ColorWHITE); |
| 248 | } |
| 249 | |
| 250 | static void TestImage(skiatest::Reporter* reporter, const SkBitmap& bitmap, |
| 251 | const char* expected, bool useDCTEncoder) { |
| 252 | SkISize pageSize = SkISize::Make(bitmap.width(), bitmap.height()); |
robertphillips@google.com | 57c5672 | 2013-08-26 11:10:13 +0000 | [diff] [blame] | 253 | SkAutoTUnref<SkPDFDevice> dev(new SkPDFDevice(pageSize, pageSize, SkMatrix::I())); |
edisonn@google.com | d9dfa18 | 2013-04-24 13:01:01 +0000 | [diff] [blame] | 254 | |
| 255 | if (useDCTEncoder) { |
commit-bot@chromium.org | 608ea65 | 2013-10-03 19:29:21 +0000 | [diff] [blame] | 256 | dev->setDCTEncoder(encode_to_dct_data); |
edisonn@google.com | d9dfa18 | 2013-04-24 13:01:01 +0000 | [diff] [blame] | 257 | } |
| 258 | |
| 259 | SkCanvas c(dev); |
| 260 | c.drawBitmap(bitmap, 0, 0, NULL); |
| 261 | |
| 262 | SkPDFDocument doc; |
| 263 | doc.appendPage(dev); |
| 264 | |
| 265 | SkDynamicMemoryWStream stream; |
| 266 | doc.emitPDF(&stream); |
| 267 | |
| 268 | REPORTER_ASSERT(reporter, stream_contains(stream, expected)); |
| 269 | } |
| 270 | |
| 271 | static void TestUncompressed(skiatest::Reporter* reporter) { |
| 272 | SkBitmap bitmap; |
edisonn@google.com | f1a358f | 2013-04-24 13:21:49 +0000 | [diff] [blame] | 273 | setup_bitmap(&bitmap, 1, 1); |
edisonn@google.com | d9dfa18 | 2013-04-24 13:01:01 +0000 | [diff] [blame] | 274 | TestImage(reporter, bitmap, |
| 275 | "/Subtype /Image\n" |
| 276 | "/Width 1\n" |
| 277 | "/Height 1\n" |
| 278 | "/ColorSpace /DeviceRGB\n" |
| 279 | "/BitsPerComponent 8\n" |
| 280 | "/Length 3\n" |
| 281 | ">> stream", |
| 282 | true); |
| 283 | } |
| 284 | |
| 285 | static void TestFlateDecode(skiatest::Reporter* reporter) { |
| 286 | if (!SkFlate::HaveFlate()) { |
| 287 | return; |
| 288 | } |
skia.committer@gmail.com | 83f0d30 | 2013-04-25 07:01:04 +0000 | [diff] [blame] | 289 | SkBitmap bitmap; |
edisonn@google.com | f1a358f | 2013-04-24 13:21:49 +0000 | [diff] [blame] | 290 | setup_bitmap(&bitmap, 10, 10); |
edisonn@google.com | d9dfa18 | 2013-04-24 13:01:01 +0000 | [diff] [blame] | 291 | TestImage(reporter, bitmap, |
| 292 | "/Subtype /Image\n" |
| 293 | "/Width 10\n" |
| 294 | "/Height 10\n" |
| 295 | "/ColorSpace /DeviceRGB\n" |
| 296 | "/BitsPerComponent 8\n" |
| 297 | "/Filter /FlateDecode\n" |
| 298 | "/Length 13\n" |
| 299 | ">> stream", |
| 300 | false); |
| 301 | } |
| 302 | |
| 303 | static void TestDCTDecode(skiatest::Reporter* reporter) { |
| 304 | SkBitmap bitmap; |
edisonn@google.com | f1a358f | 2013-04-24 13:21:49 +0000 | [diff] [blame] | 305 | setup_bitmap(&bitmap, 32, 32); |
edisonn@google.com | d9dfa18 | 2013-04-24 13:01:01 +0000 | [diff] [blame] | 306 | TestImage(reporter, bitmap, |
| 307 | "/Subtype /Image\n" |
| 308 | "/Width 32\n" |
| 309 | "/Height 32\n" |
| 310 | "/ColorSpace /DeviceRGB\n" |
| 311 | "/BitsPerComponent 8\n" |
| 312 | "/Filter /DCTDecode\n" |
| 313 | "/ColorTransform 0\n" |
| 314 | "/Length 21\n" |
| 315 | ">> stream", |
| 316 | true); |
| 317 | } |
| 318 | |
| 319 | static void TestImages(skiatest::Reporter* reporter) { |
| 320 | TestUncompressed(reporter); |
| 321 | TestFlateDecode(reporter); |
| 322 | TestDCTDecode(reporter); |
| 323 | } |
| 324 | |
vandebo@chromium.org | 4e1cc6a | 2013-01-25 19:27:23 +0000 | [diff] [blame] | 325 | // This test used to assert without the fix submitted for |
| 326 | // http://code.google.com/p/skia/issues/detail?id=1083. |
| 327 | // SKP files might have invalid glyph ids. This test ensures they are ignored, |
| 328 | // and there is no assert on input data in Debug mode. |
sugoi@google.com | 54f0d1b | 2013-02-27 19:17:41 +0000 | [diff] [blame] | 329 | static void test_issue1083() { |
vandebo@chromium.org | 4e1cc6a | 2013-01-25 19:27:23 +0000 | [diff] [blame] | 330 | SkISize pageSize = SkISize::Make(100, 100); |
robertphillips@google.com | 35300c4 | 2013-03-21 17:38:49 +0000 | [diff] [blame] | 331 | SkAutoTUnref<SkPDFDevice> dev(new SkPDFDevice(pageSize, pageSize, SkMatrix::I())); |
vandebo@chromium.org | 4e1cc6a | 2013-01-25 19:27:23 +0000 | [diff] [blame] | 332 | |
| 333 | SkCanvas c(dev); |
| 334 | SkPaint paint; |
| 335 | paint.setTextEncoding(SkPaint::kGlyphID_TextEncoding); |
| 336 | |
| 337 | uint16_t glyphID = 65000; |
| 338 | c.drawText(&glyphID, 2, 0, 0, paint); |
| 339 | |
| 340 | SkPDFDocument doc; |
| 341 | doc.appendPage(dev); |
| 342 | |
| 343 | SkDynamicMemoryWStream stream; |
| 344 | doc.emitPDF(&stream); |
| 345 | } |
| 346 | |
tfarina@chromium.org | e4fafb1 | 2013-12-12 21:11:12 +0000 | [diff] [blame] | 347 | DEF_TEST(PDFPrimitives, reporter) { |
vandebo@chromium.org | d96d17b | 2013-01-04 19:31:24 +0000 | [diff] [blame] | 348 | SkAutoTUnref<SkPDFInt> int42(new SkPDFInt(42)); |
vandebo@chromium.org | 421d644 | 2011-07-20 17:39:01 +0000 | [diff] [blame] | 349 | SimpleCheckObjectOutput(reporter, int42.get(), "42"); |
vandebo@chromium.org | 8459d4e | 2010-09-24 22:25:30 +0000 | [diff] [blame] | 350 | |
vandebo@chromium.org | d96d17b | 2013-01-04 19:31:24 +0000 | [diff] [blame] | 351 | SkAutoTUnref<SkPDFScalar> realHalf(new SkPDFScalar(SK_ScalarHalf)); |
vandebo@chromium.org | 421d644 | 2011-07-20 17:39:01 +0000 | [diff] [blame] | 352 | SimpleCheckObjectOutput(reporter, realHalf.get(), "0.5"); |
vandebo@chromium.org | 8459d4e | 2010-09-24 22:25:30 +0000 | [diff] [blame] | 353 | |
vandebo@chromium.org | d96d17b | 2013-01-04 19:31:24 +0000 | [diff] [blame] | 354 | SkAutoTUnref<SkPDFScalar> bigScalar(new SkPDFScalar(110999.75f)); |
vandebo@chromium.org | 6cc26da | 2011-05-18 17:08:05 +0000 | [diff] [blame] | 355 | #if !defined(SK_ALLOW_LARGE_PDF_SCALARS) |
vandebo@chromium.org | 421d644 | 2011-07-20 17:39:01 +0000 | [diff] [blame] | 356 | SimpleCheckObjectOutput(reporter, bigScalar.get(), "111000"); |
vandebo@chromium.org | 094316b | 2011-03-04 03:15:13 +0000 | [diff] [blame] | 357 | #else |
vandebo@chromium.org | 421d644 | 2011-07-20 17:39:01 +0000 | [diff] [blame] | 358 | SimpleCheckObjectOutput(reporter, bigScalar.get(), "110999.75"); |
vandebo@chromium.org | 094316b | 2011-03-04 03:15:13 +0000 | [diff] [blame] | 359 | |
vandebo@chromium.org | d96d17b | 2013-01-04 19:31:24 +0000 | [diff] [blame] | 360 | SkAutoTUnref<SkPDFScalar> biggerScalar(new SkPDFScalar(50000000.1)); |
vandebo@chromium.org | 421d644 | 2011-07-20 17:39:01 +0000 | [diff] [blame] | 361 | SimpleCheckObjectOutput(reporter, biggerScalar.get(), "50000000"); |
vandebo@chromium.org | 094316b | 2011-03-04 03:15:13 +0000 | [diff] [blame] | 362 | |
vandebo@chromium.org | d96d17b | 2013-01-04 19:31:24 +0000 | [diff] [blame] | 363 | SkAutoTUnref<SkPDFScalar> smallestScalar(new SkPDFScalar(1.0/65536)); |
vandebo@chromium.org | 421d644 | 2011-07-20 17:39:01 +0000 | [diff] [blame] | 364 | SimpleCheckObjectOutput(reporter, smallestScalar.get(), "0.00001526"); |
vandebo@chromium.org | 094316b | 2011-03-04 03:15:13 +0000 | [diff] [blame] | 365 | #endif |
| 366 | |
vandebo@chromium.org | d96d17b | 2013-01-04 19:31:24 +0000 | [diff] [blame] | 367 | SkAutoTUnref<SkPDFString> stringSimple( |
| 368 | new SkPDFString("test ) string ( foo")); |
vandebo@chromium.org | 421d644 | 2011-07-20 17:39:01 +0000 | [diff] [blame] | 369 | SimpleCheckObjectOutput(reporter, stringSimple.get(), |
| 370 | "(test \\) string \\( foo)"); |
vandebo@chromium.org | d96d17b | 2013-01-04 19:31:24 +0000 | [diff] [blame] | 371 | SkAutoTUnref<SkPDFString> stringComplex( |
| 372 | new SkPDFString("\ttest ) string ( foo")); |
vandebo@chromium.org | 421d644 | 2011-07-20 17:39:01 +0000 | [diff] [blame] | 373 | SimpleCheckObjectOutput(reporter, stringComplex.get(), |
| 374 | "<0974657374202920737472696E67202820666F6F>"); |
vandebo@chromium.org | 8459d4e | 2010-09-24 22:25:30 +0000 | [diff] [blame] | 375 | |
vandebo@chromium.org | d96d17b | 2013-01-04 19:31:24 +0000 | [diff] [blame] | 376 | SkAutoTUnref<SkPDFName> name(new SkPDFName("Test name\twith#tab")); |
vandebo@chromium.org | 421d644 | 2011-07-20 17:39:01 +0000 | [diff] [blame] | 377 | const char expectedResult[] = "/Test#20name#09with#23tab"; |
| 378 | CheckObjectOutput(reporter, name.get(), expectedResult, |
| 379 | strlen(expectedResult), false, false); |
vandebo@chromium.org | 8459d4e | 2010-09-24 22:25:30 +0000 | [diff] [blame] | 380 | |
vandebo@chromium.org | d96d17b | 2013-01-04 19:31:24 +0000 | [diff] [blame] | 381 | SkAutoTUnref<SkPDFName> escapedName(new SkPDFName("A#/%()<>[]{}B")); |
vandebo@chromium.org | 251a766 | 2012-09-21 17:50:50 +0000 | [diff] [blame] | 382 | const char escapedNameExpected[] = "/A#23#2F#25#28#29#3C#3E#5B#5D#7B#7DB"; |
| 383 | CheckObjectOutput(reporter, escapedName.get(), escapedNameExpected, |
| 384 | strlen(escapedNameExpected), false, false); |
| 385 | |
vandebo@chromium.org | c0376fe | 2012-03-05 18:44:33 +0000 | [diff] [blame] | 386 | // Test that we correctly handle characters with the high-bit set. |
bungeman@google.com | f8aa18c | 2012-03-19 21:04:52 +0000 | [diff] [blame] | 387 | const unsigned char highBitCString[] = {0xDE, 0xAD, 'b', 'e', 0xEF, 0}; |
vandebo@chromium.org | d96d17b | 2013-01-04 19:31:24 +0000 | [diff] [blame] | 388 | SkAutoTUnref<SkPDFName> highBitName( |
| 389 | new SkPDFName((const char*)highBitCString)); |
vandebo@chromium.org | c0376fe | 2012-03-05 18:44:33 +0000 | [diff] [blame] | 390 | const char highBitExpectedResult[] = "/#DE#ADbe#EF"; |
| 391 | CheckObjectOutput(reporter, highBitName.get(), highBitExpectedResult, |
| 392 | strlen(highBitExpectedResult), false, false); |
| 393 | |
vandebo@chromium.org | d96d17b | 2013-01-04 19:31:24 +0000 | [diff] [blame] | 394 | SkAutoTUnref<SkPDFArray> array(new SkPDFArray); |
vandebo@chromium.org | 421d644 | 2011-07-20 17:39:01 +0000 | [diff] [blame] | 395 | SimpleCheckObjectOutput(reporter, array.get(), "[]"); |
vandebo@chromium.org | 8459d4e | 2010-09-24 22:25:30 +0000 | [diff] [blame] | 396 | array->append(int42.get()); |
vandebo@chromium.org | 421d644 | 2011-07-20 17:39:01 +0000 | [diff] [blame] | 397 | SimpleCheckObjectOutput(reporter, array.get(), "[42]"); |
vandebo@chromium.org | 8459d4e | 2010-09-24 22:25:30 +0000 | [diff] [blame] | 398 | array->append(realHalf.get()); |
vandebo@chromium.org | 421d644 | 2011-07-20 17:39:01 +0000 | [diff] [blame] | 399 | SimpleCheckObjectOutput(reporter, array.get(), "[42 0.5]"); |
vandebo@chromium.org | d96d17b | 2013-01-04 19:31:24 +0000 | [diff] [blame] | 400 | SkAutoTUnref<SkPDFInt> int0(new SkPDFInt(0)); |
vandebo@chromium.org | 8459d4e | 2010-09-24 22:25:30 +0000 | [diff] [blame] | 401 | array->append(int0.get()); |
vandebo@chromium.org | 421d644 | 2011-07-20 17:39:01 +0000 | [diff] [blame] | 402 | SimpleCheckObjectOutput(reporter, array.get(), "[42 0.5 0]"); |
vandebo@chromium.org | d96d17b | 2013-01-04 19:31:24 +0000 | [diff] [blame] | 403 | SkAutoTUnref<SkPDFInt> int1(new SkPDFInt(1)); |
vandebo@chromium.org | 8459d4e | 2010-09-24 22:25:30 +0000 | [diff] [blame] | 404 | array->setAt(0, int1.get()); |
vandebo@chromium.org | 421d644 | 2011-07-20 17:39:01 +0000 | [diff] [blame] | 405 | SimpleCheckObjectOutput(reporter, array.get(), "[1 0.5 0]"); |
vandebo@chromium.org | 8459d4e | 2010-09-24 22:25:30 +0000 | [diff] [blame] | 406 | |
vandebo@chromium.org | d96d17b | 2013-01-04 19:31:24 +0000 | [diff] [blame] | 407 | SkAutoTUnref<SkPDFDict> dict(new SkPDFDict); |
vandebo@chromium.org | 421d644 | 2011-07-20 17:39:01 +0000 | [diff] [blame] | 408 | SimpleCheckObjectOutput(reporter, dict.get(), "<<>>"); |
vandebo@chromium.org | d96d17b | 2013-01-04 19:31:24 +0000 | [diff] [blame] | 409 | SkAutoTUnref<SkPDFName> n1(new SkPDFName("n1")); |
vandebo@chromium.org | 8459d4e | 2010-09-24 22:25:30 +0000 | [diff] [blame] | 410 | dict->insert(n1.get(), int42.get()); |
vandebo@chromium.org | 421d644 | 2011-07-20 17:39:01 +0000 | [diff] [blame] | 411 | SimpleCheckObjectOutput(reporter, dict.get(), "<</n1 42\n>>"); |
vandebo@chromium.org | d96d17b | 2013-01-04 19:31:24 +0000 | [diff] [blame] | 412 | SkAutoTUnref<SkPDFName> n2(new SkPDFName("n2")); |
| 413 | SkAutoTUnref<SkPDFName> n3(new SkPDFName("n3")); |
vandebo@chromium.org | 8459d4e | 2010-09-24 22:25:30 +0000 | [diff] [blame] | 414 | dict->insert(n2.get(), realHalf.get()); |
| 415 | dict->insert(n3.get(), array.get()); |
vandebo@chromium.org | 421d644 | 2011-07-20 17:39:01 +0000 | [diff] [blame] | 416 | SimpleCheckObjectOutput(reporter, dict.get(), |
| 417 | "<</n1 42\n/n2 0.5\n/n3 [1 0.5 0]\n>>"); |
vandebo@chromium.org | 8459d4e | 2010-09-24 22:25:30 +0000 | [diff] [blame] | 418 | |
vandebo@chromium.org | 421d644 | 2011-07-20 17:39:01 +0000 | [diff] [blame] | 419 | TestPDFStream(reporter); |
vandebo@chromium.org | 8459d4e | 2010-09-24 22:25:30 +0000 | [diff] [blame] | 420 | |
| 421 | TestCatalog(reporter); |
| 422 | |
| 423 | TestObjectRef(reporter); |
vandebo@chromium.org | 2ef12d4 | 2011-07-06 23:31:24 +0000 | [diff] [blame] | 424 | |
| 425 | TestSubstitute(reporter); |
vandebo@chromium.org | 4e1cc6a | 2013-01-25 19:27:23 +0000 | [diff] [blame] | 426 | |
sugoi@google.com | 54f0d1b | 2013-02-27 19:17:41 +0000 | [diff] [blame] | 427 | test_issue1083(); |
edisonn@google.com | d9dfa18 | 2013-04-24 13:01:01 +0000 | [diff] [blame] | 428 | |
skia.committer@gmail.com | 83f0d30 | 2013-04-25 07:01:04 +0000 | [diff] [blame] | 429 | TestImages(reporter); |
vandebo@chromium.org | 8459d4e | 2010-09-24 22:25:30 +0000 | [diff] [blame] | 430 | } |