blob: b1d482ffa2d4ee662055d6cb427bfe0db6aa9c5f [file] [log] [blame]
vandebo@chromium.orgf66025d2010-10-01 23:26:55 +00001/*
epoger@google.comec3ed6a2011-07-28 14:26:00 +00002 * Copyright 2010 The Android Open Source Project
vandebo@chromium.orgf66025d2010-10-01 23:26:55 +00003 *
epoger@google.comec3ed6a2011-07-28 14:26:00 +00004 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
vandebo@chromium.orgf66025d2010-10-01 23:26:55 +00006 */
7
edisonn@google.comd9dfa182013-04-24 13:01:01 +00008#include "SkBitmap.h"
vandebo@chromium.org4e1cc6a2013-01-25 19:27:23 +00009#include "SkCanvas.h"
reed@google.com8a85d0c2011-06-24 19:12:12 +000010#include "SkData.h"
vandebo@chromium.org421d6442011-07-20 17:39:01 +000011#include "SkFlate.h"
edisonn@google.comd9dfa182013-04-24 13:01:01 +000012#include "SkImageEncoder.h"
13#include "SkMatrix.h"
vandebo@chromium.org8459d4e2010-09-24 22:25:30 +000014#include "SkPDFCatalog.h"
vandebo@chromium.org4e1cc6a2013-01-25 19:27:23 +000015#include "SkPDFDevice.h"
vandebo@chromium.org8459d4e2010-09-24 22:25:30 +000016#include "SkPDFStream.h"
17#include "SkPDFTypes.h"
18#include "SkScalar.h"
19#include "SkStream.h"
vandebo@chromium.org421d6442011-07-20 17:39:01 +000020#include "SkTypes.h"
tfarina@chromium.org8f6884a2014-01-24 20:56:26 +000021#include "Test.h"
vandebo@chromium.org8459d4e2010-09-24 22:25:30 +000022
vandebo@chromium.org2ef12d42011-07-06 23:31:24 +000023class SkPDFTestDict : public SkPDFDict {
24public:
edisonn@google.com6addb192013-04-02 15:33:08 +000025 virtual void getResources(const SkTSet<SkPDFObject*>& knownResourceObjects,
26 SkTSet<SkPDFObject*>* newResourceObjects) {
vandebo@chromium.org2ef12d42011-07-06 23:31:24 +000027 for (int i = 0; i < fResources.count(); i++) {
edisonn@google.com6addb192013-04-02 15:33:08 +000028 newResourceObjects->add(fResources[i]);
vandebo@chromium.org2ef12d42011-07-06 23:31:24 +000029 fResources[i]->ref();
30 }
31 }
32
33 void addResource(SkPDFObject* object) {
34 fResources.append(1, &object);
35 }
36
37private:
38 SkTDArray<SkPDFObject*> fResources;
39};
40
commit-bot@chromium.org608ea652013-10-03 19:29:21 +000041#define DUMMY_TEXT "DCT compessed stream."
42
43static 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.comd9dfa182013-04-24 13:01:01 +000046}
47
reed@google.com8a85d0c2011-06-24 19:12:12 +000048static bool stream_equals(const SkDynamicMemoryWStream& stream, size_t offset,
49 const void* buffer, size_t len) {
50 SkAutoDataUnref data(stream.copyToData());
robertphillips@google.com59f46b82012-07-10 17:30:58 +000051 if (offset + len > data->size()) {
reed@google.com8a85d0c2011-06-24 19:12:12 +000052 return false;
53 }
robertphillips@google.com59f46b82012-07-10 17:30:58 +000054 return memcmp(data->bytes() + offset, buffer, len) == 0;
reed@google.com8a85d0c2011-06-24 19:12:12 +000055}
56
edisonn@google.comd9dfa182013-04-24 13:01:01 +000057static 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.org8459d4e2010-09-24 22:25:30 +000071static void CheckObjectOutput(skiatest::Reporter* reporter, SkPDFObject* obj,
vandebo@chromium.org421d6442011-07-20 17:39:01 +000072 const char* expectedData, size_t expectedSize,
73 bool indirect, bool compression) {
74 SkPDFDocument::Flags docFlags = (SkPDFDocument::Flags) 0;
75 if (!compression) {
edisonn@google.com8c020612013-03-12 19:53:16 +000076 docFlags = SkTBitOr(docFlags, SkPDFDocument::kFavorSpeedOverSize_Flags);
vandebo@chromium.org421d6442011-07-20 17:39:01 +000077 }
78 SkPDFCatalog catalog(docFlags);
vandebo@chromium.org2ef12d42011-07-06 23:31:24 +000079 size_t directSize = obj->getOutputSize(&catalog, false);
vandebo@chromium.org421d6442011-07-20 17:39:01 +000080 REPORTER_ASSERT(reporter, directSize == expectedSize);
vandebo@chromium.org8459d4e2010-09-24 22:25:30 +000081
82 SkDynamicMemoryWStream buffer;
vandebo@chromium.org2ef12d42011-07-06 23:31:24 +000083 obj->emit(&buffer, &catalog, false);
vandebo@chromium.org8459d4e2010-09-24 22:25:30 +000084 REPORTER_ASSERT(reporter, directSize == buffer.getOffset());
vandebo@chromium.org421d6442011-07-20 17:39:01 +000085 REPORTER_ASSERT(reporter, stream_equals(buffer, 0, expectedData,
reed@google.com8a85d0c2011-06-24 19:12:12 +000086 directSize));
vandebo@chromium.org8459d4e2010-09-24 22:25:30 +000087
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.org8459d4e2010-09-24 22:25:30 +000095 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.org2ef12d42011-07-06 23:31:24 +0000102 obj->emit(&buffer, &catalog, true);
vandebo@chromium.org8459d4e2010-09-24 22:25:30 +0000103 REPORTER_ASSERT(reporter, indirectSize == buffer.getOffset());
reed@google.com8a85d0c2011-06-24 19:12:12 +0000104 REPORTER_ASSERT(reporter, stream_equals(buffer, 0, header, headerLen));
vandebo@chromium.org421d6442011-07-20 17:39:01 +0000105 REPORTER_ASSERT(reporter, stream_equals(buffer, headerLen, expectedData,
reed@google.com8a85d0c2011-06-24 19:12:12 +0000106 directSize));
107 REPORTER_ASSERT(reporter, stream_equals(buffer, headerLen + directSize,
108 footer, footerLen));
vandebo@chromium.org8459d4e2010-09-24 22:25:30 +0000109 }
110}
111
vandebo@chromium.org421d6442011-07-20 17:39:01 +0000112static void SimpleCheckObjectOutput(skiatest::Reporter* reporter,
113 SkPDFObject* obj,
robertphillips@google.com4debcac2012-05-14 16:33:36 +0000114 const char* expectedResult) {
115 CheckObjectOutput(reporter, obj, expectedResult,
116 strlen(expectedResult), true, false);
vandebo@chromium.org421d6442011-07-20 17:39:01 +0000117}
118
119static void TestPDFStream(skiatest::Reporter* reporter) {
120 char streamBytes[] = "Test\nFoo\tBar";
vandebo@chromium.orgd96d17b2013-01-04 19:31:24 +0000121 SkAutoTUnref<SkMemoryStream> streamData(new SkMemoryStream(
122 streamBytes, strlen(streamBytes), true));
123 SkAutoTUnref<SkPDFStream> stream(new SkPDFStream(streamData.get()));
vandebo@chromium.org421d6442011-07-20 17:39:01 +0000124 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.orgd96d17b2013-01-04 19:31:24 +0000139 SkAutoTUnref<SkPDFStream> stream(new SkPDFStream(streamData2.get()));
vandebo@chromium.org421d6442011-07-20 17:39:01 +0000140
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.com59f46b82012-07-10 17:30:58 +0000152 (const char*) expectedResultData1->data(),
153 expectedResultData1->size(), true, false);
vandebo@chromium.org421d6442011-07-20 17:39:01 +0000154
155 // Then again with compression.
156 SkDynamicMemoryWStream expectedResult2;
157 expectedResult2.writeText("<</Filter /FlateDecode\n/Length 116\n"
158 ">> stream\n");
robertphillips@google.com59f46b82012-07-10 17:30:58 +0000159 expectedResult2.write(compressedData->data(), compressedData->size());
vandebo@chromium.org421d6442011-07-20 17:39:01 +0000160 expectedResult2.writeText("\nendstream");
161 SkAutoDataUnref expectedResultData2(expectedResult2.copyToData());
162 CheckObjectOutput(reporter, stream.get(),
robertphillips@google.com59f46b82012-07-10 17:30:58 +0000163 (const char*) expectedResultData2->data(),
164 expectedResultData2->size(), true, true);
vandebo@chromium.org421d6442011-07-20 17:39:01 +0000165 }
166}
167
vandebo@chromium.org8459d4e2010-09-24 22:25:30 +0000168static void TestCatalog(skiatest::Reporter* reporter) {
vandebo@chromium.org421d6442011-07-20 17:39:01 +0000169 SkPDFCatalog catalog((SkPDFDocument::Flags)0);
vandebo@chromium.orgd96d17b2013-01-04 19:31:24 +0000170 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.org8459d4e2010-09-24 22:25:30 +0000175
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.com8a85d0c2011-06-24 19:12:12 +0000190 REPORTER_ASSERT(reporter, stream_equals(buffer, 0, expectedResult,
191 strlen(expectedResult)));
vandebo@chromium.org8459d4e2010-09-24 22:25:30 +0000192}
193
194static void TestObjectRef(skiatest::Reporter* reporter) {
vandebo@chromium.orgd96d17b2013-01-04 19:31:24 +0000195 SkAutoTUnref<SkPDFInt> int1(new SkPDFInt(1));
196 SkAutoTUnref<SkPDFInt> int2(new SkPDFInt(2));
197 SkAutoTUnref<SkPDFObjRef> int2ref(new SkPDFObjRef(int2.get()));
vandebo@chromium.org8459d4e2010-09-24 22:25:30 +0000198
vandebo@chromium.org421d6442011-07-20 17:39:01 +0000199 SkPDFCatalog catalog((SkPDFDocument::Flags)0);
vandebo@chromium.org8459d4e2010-09-24 22:25:30 +0000200 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.com8a85d0c2011-06-24 19:12:12 +0000209 REPORTER_ASSERT(reporter, stream_equals(buffer, 0, expectedResult,
210 buffer.getOffset()));
vandebo@chromium.org8459d4e2010-09-24 22:25:30 +0000211}
212
vandebo@chromium.org2ef12d42011-07-06 23:31:24 +0000213static void TestSubstitute(skiatest::Reporter* reporter) {
vandebo@chromium.orgd96d17b2013-01-04 19:31:24 +0000214 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.org2ef12d42011-07-06 23:31:24 +0000219
220 stub->insert("Value", int33.get());
221 stubResource->insert("InnerValue", int44.get());
222 stub->addResource(stubResource.get());
223
vandebo@chromium.org421d6442011-07-20 17:39:01 +0000224 SkPDFCatalog catalog((SkPDFDocument::Flags)0);
vandebo@chromium.org2ef12d42011-07-06 23:31:24 +0000225 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.orgd3a094c2011-07-25 22:22:25 +0000232 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.org2ef12d42011-07-06 23:31:24 +0000237 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.comd9dfa182013-04-24 13:01:01 +0000244// Create a bitmap that would be very eficiently compressed in a ZIP.
edisonn@google.comf1a358f2013-04-24 13:21:49 +0000245static void setup_bitmap(SkBitmap* bitmap, int width, int height) {
mike@reedtribe.orgdeee4962014-02-13 14:41:43 +0000246 bitmap->allocN32Pixels(width, height);
edisonn@google.comd9dfa182013-04-24 13:01:01 +0000247 bitmap->eraseColor(SK_ColorWHITE);
248}
249
250static 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.com57c56722013-08-26 11:10:13 +0000253 SkAutoTUnref<SkPDFDevice> dev(new SkPDFDevice(pageSize, pageSize, SkMatrix::I()));
edisonn@google.comd9dfa182013-04-24 13:01:01 +0000254
255 if (useDCTEncoder) {
commit-bot@chromium.org608ea652013-10-03 19:29:21 +0000256 dev->setDCTEncoder(encode_to_dct_data);
edisonn@google.comd9dfa182013-04-24 13:01:01 +0000257 }
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
271static void TestUncompressed(skiatest::Reporter* reporter) {
272 SkBitmap bitmap;
edisonn@google.comf1a358f2013-04-24 13:21:49 +0000273 setup_bitmap(&bitmap, 1, 1);
edisonn@google.comd9dfa182013-04-24 13:01:01 +0000274 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
285static void TestFlateDecode(skiatest::Reporter* reporter) {
286 if (!SkFlate::HaveFlate()) {
287 return;
288 }
skia.committer@gmail.com83f0d302013-04-25 07:01:04 +0000289 SkBitmap bitmap;
edisonn@google.comf1a358f2013-04-24 13:21:49 +0000290 setup_bitmap(&bitmap, 10, 10);
edisonn@google.comd9dfa182013-04-24 13:01:01 +0000291 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
303static void TestDCTDecode(skiatest::Reporter* reporter) {
304 SkBitmap bitmap;
edisonn@google.comf1a358f2013-04-24 13:21:49 +0000305 setup_bitmap(&bitmap, 32, 32);
edisonn@google.comd9dfa182013-04-24 13:01:01 +0000306 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
319static void TestImages(skiatest::Reporter* reporter) {
320 TestUncompressed(reporter);
321 TestFlateDecode(reporter);
322 TestDCTDecode(reporter);
323}
324
vandebo@chromium.org4e1cc6a2013-01-25 19:27:23 +0000325// 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.com54f0d1b2013-02-27 19:17:41 +0000329static void test_issue1083() {
vandebo@chromium.org4e1cc6a2013-01-25 19:27:23 +0000330 SkISize pageSize = SkISize::Make(100, 100);
robertphillips@google.com35300c42013-03-21 17:38:49 +0000331 SkAutoTUnref<SkPDFDevice> dev(new SkPDFDevice(pageSize, pageSize, SkMatrix::I()));
vandebo@chromium.org4e1cc6a2013-01-25 19:27:23 +0000332
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.orge4fafb12013-12-12 21:11:12 +0000347DEF_TEST(PDFPrimitives, reporter) {
vandebo@chromium.orgd96d17b2013-01-04 19:31:24 +0000348 SkAutoTUnref<SkPDFInt> int42(new SkPDFInt(42));
vandebo@chromium.org421d6442011-07-20 17:39:01 +0000349 SimpleCheckObjectOutput(reporter, int42.get(), "42");
vandebo@chromium.org8459d4e2010-09-24 22:25:30 +0000350
vandebo@chromium.orgd96d17b2013-01-04 19:31:24 +0000351 SkAutoTUnref<SkPDFScalar> realHalf(new SkPDFScalar(SK_ScalarHalf));
vandebo@chromium.org421d6442011-07-20 17:39:01 +0000352 SimpleCheckObjectOutput(reporter, realHalf.get(), "0.5");
vandebo@chromium.org8459d4e2010-09-24 22:25:30 +0000353
vandebo@chromium.orgd96d17b2013-01-04 19:31:24 +0000354 SkAutoTUnref<SkPDFScalar> bigScalar(new SkPDFScalar(110999.75f));
vandebo@chromium.org6cc26da2011-05-18 17:08:05 +0000355#if !defined(SK_ALLOW_LARGE_PDF_SCALARS)
vandebo@chromium.org421d6442011-07-20 17:39:01 +0000356 SimpleCheckObjectOutput(reporter, bigScalar.get(), "111000");
vandebo@chromium.org094316b2011-03-04 03:15:13 +0000357#else
vandebo@chromium.org421d6442011-07-20 17:39:01 +0000358 SimpleCheckObjectOutput(reporter, bigScalar.get(), "110999.75");
vandebo@chromium.org094316b2011-03-04 03:15:13 +0000359
vandebo@chromium.orgd96d17b2013-01-04 19:31:24 +0000360 SkAutoTUnref<SkPDFScalar> biggerScalar(new SkPDFScalar(50000000.1));
vandebo@chromium.org421d6442011-07-20 17:39:01 +0000361 SimpleCheckObjectOutput(reporter, biggerScalar.get(), "50000000");
vandebo@chromium.org094316b2011-03-04 03:15:13 +0000362
vandebo@chromium.orgd96d17b2013-01-04 19:31:24 +0000363 SkAutoTUnref<SkPDFScalar> smallestScalar(new SkPDFScalar(1.0/65536));
vandebo@chromium.org421d6442011-07-20 17:39:01 +0000364 SimpleCheckObjectOutput(reporter, smallestScalar.get(), "0.00001526");
vandebo@chromium.org094316b2011-03-04 03:15:13 +0000365#endif
366
vandebo@chromium.orgd96d17b2013-01-04 19:31:24 +0000367 SkAutoTUnref<SkPDFString> stringSimple(
368 new SkPDFString("test ) string ( foo"));
vandebo@chromium.org421d6442011-07-20 17:39:01 +0000369 SimpleCheckObjectOutput(reporter, stringSimple.get(),
370 "(test \\) string \\( foo)");
vandebo@chromium.orgd96d17b2013-01-04 19:31:24 +0000371 SkAutoTUnref<SkPDFString> stringComplex(
372 new SkPDFString("\ttest ) string ( foo"));
vandebo@chromium.org421d6442011-07-20 17:39:01 +0000373 SimpleCheckObjectOutput(reporter, stringComplex.get(),
374 "<0974657374202920737472696E67202820666F6F>");
vandebo@chromium.org8459d4e2010-09-24 22:25:30 +0000375
vandebo@chromium.orgd96d17b2013-01-04 19:31:24 +0000376 SkAutoTUnref<SkPDFName> name(new SkPDFName("Test name\twith#tab"));
vandebo@chromium.org421d6442011-07-20 17:39:01 +0000377 const char expectedResult[] = "/Test#20name#09with#23tab";
378 CheckObjectOutput(reporter, name.get(), expectedResult,
379 strlen(expectedResult), false, false);
vandebo@chromium.org8459d4e2010-09-24 22:25:30 +0000380
vandebo@chromium.orgd96d17b2013-01-04 19:31:24 +0000381 SkAutoTUnref<SkPDFName> escapedName(new SkPDFName("A#/%()<>[]{}B"));
vandebo@chromium.org251a7662012-09-21 17:50:50 +0000382 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.orgc0376fe2012-03-05 18:44:33 +0000386 // Test that we correctly handle characters with the high-bit set.
bungeman@google.comf8aa18c2012-03-19 21:04:52 +0000387 const unsigned char highBitCString[] = {0xDE, 0xAD, 'b', 'e', 0xEF, 0};
vandebo@chromium.orgd96d17b2013-01-04 19:31:24 +0000388 SkAutoTUnref<SkPDFName> highBitName(
389 new SkPDFName((const char*)highBitCString));
vandebo@chromium.orgc0376fe2012-03-05 18:44:33 +0000390 const char highBitExpectedResult[] = "/#DE#ADbe#EF";
391 CheckObjectOutput(reporter, highBitName.get(), highBitExpectedResult,
392 strlen(highBitExpectedResult), false, false);
393
vandebo@chromium.orgd96d17b2013-01-04 19:31:24 +0000394 SkAutoTUnref<SkPDFArray> array(new SkPDFArray);
vandebo@chromium.org421d6442011-07-20 17:39:01 +0000395 SimpleCheckObjectOutput(reporter, array.get(), "[]");
vandebo@chromium.org8459d4e2010-09-24 22:25:30 +0000396 array->append(int42.get());
vandebo@chromium.org421d6442011-07-20 17:39:01 +0000397 SimpleCheckObjectOutput(reporter, array.get(), "[42]");
vandebo@chromium.org8459d4e2010-09-24 22:25:30 +0000398 array->append(realHalf.get());
vandebo@chromium.org421d6442011-07-20 17:39:01 +0000399 SimpleCheckObjectOutput(reporter, array.get(), "[42 0.5]");
vandebo@chromium.orgd96d17b2013-01-04 19:31:24 +0000400 SkAutoTUnref<SkPDFInt> int0(new SkPDFInt(0));
vandebo@chromium.org8459d4e2010-09-24 22:25:30 +0000401 array->append(int0.get());
vandebo@chromium.org421d6442011-07-20 17:39:01 +0000402 SimpleCheckObjectOutput(reporter, array.get(), "[42 0.5 0]");
vandebo@chromium.orgd96d17b2013-01-04 19:31:24 +0000403 SkAutoTUnref<SkPDFInt> int1(new SkPDFInt(1));
vandebo@chromium.org8459d4e2010-09-24 22:25:30 +0000404 array->setAt(0, int1.get());
vandebo@chromium.org421d6442011-07-20 17:39:01 +0000405 SimpleCheckObjectOutput(reporter, array.get(), "[1 0.5 0]");
vandebo@chromium.org8459d4e2010-09-24 22:25:30 +0000406
vandebo@chromium.orgd96d17b2013-01-04 19:31:24 +0000407 SkAutoTUnref<SkPDFDict> dict(new SkPDFDict);
vandebo@chromium.org421d6442011-07-20 17:39:01 +0000408 SimpleCheckObjectOutput(reporter, dict.get(), "<<>>");
vandebo@chromium.orgd96d17b2013-01-04 19:31:24 +0000409 SkAutoTUnref<SkPDFName> n1(new SkPDFName("n1"));
vandebo@chromium.org8459d4e2010-09-24 22:25:30 +0000410 dict->insert(n1.get(), int42.get());
vandebo@chromium.org421d6442011-07-20 17:39:01 +0000411 SimpleCheckObjectOutput(reporter, dict.get(), "<</n1 42\n>>");
vandebo@chromium.orgd96d17b2013-01-04 19:31:24 +0000412 SkAutoTUnref<SkPDFName> n2(new SkPDFName("n2"));
413 SkAutoTUnref<SkPDFName> n3(new SkPDFName("n3"));
vandebo@chromium.org8459d4e2010-09-24 22:25:30 +0000414 dict->insert(n2.get(), realHalf.get());
415 dict->insert(n3.get(), array.get());
vandebo@chromium.org421d6442011-07-20 17:39:01 +0000416 SimpleCheckObjectOutput(reporter, dict.get(),
417 "<</n1 42\n/n2 0.5\n/n3 [1 0.5 0]\n>>");
vandebo@chromium.org8459d4e2010-09-24 22:25:30 +0000418
vandebo@chromium.org421d6442011-07-20 17:39:01 +0000419 TestPDFStream(reporter);
vandebo@chromium.org8459d4e2010-09-24 22:25:30 +0000420
421 TestCatalog(reporter);
422
423 TestObjectRef(reporter);
vandebo@chromium.org2ef12d42011-07-06 23:31:24 +0000424
425 TestSubstitute(reporter);
vandebo@chromium.org4e1cc6a2013-01-25 19:27:23 +0000426
sugoi@google.com54f0d1b2013-02-27 19:17:41 +0000427 test_issue1083();
edisonn@google.comd9dfa182013-04-24 13:01:01 +0000428
skia.committer@gmail.com83f0d302013-04-25 07:01:04 +0000429 TestImages(reporter);
vandebo@chromium.org8459d4e2010-09-24 22:25:30 +0000430}