blob: 5fce53ea0a7f29659d1c8b89a04b8547cf951bab [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
halcanary66a82f32015-10-12 13:05:04 -07008#include "Resources.h"
edisonn@google.comd9dfa182013-04-24 13:01:01 +00009#include "SkBitmap.h"
vandebo@chromium.org4e1cc6a2013-01-25 19:27:23 +000010#include "SkCanvas.h"
reed@google.com8a85d0c2011-06-24 19:12:12 +000011#include "SkData.h"
halcanary3d32d502015-03-01 06:55:20 -080012#include "SkDocument.h"
halcanaryd9e57152015-08-12 11:24:40 -070013#include "SkDeflate.h"
edisonn@google.comd9dfa182013-04-24 13:01:01 +000014#include "SkImageEncoder.h"
15#include "SkMatrix.h"
halcanarya1f1ee92015-02-20 06:17:26 -080016#include "SkPDFCanon.h"
vandebo@chromium.org4e1cc6a2013-01-25 19:27:23 +000017#include "SkPDFDevice.h"
halcanary66a82f32015-10-12 13:05:04 -070018#include "SkPDFFont.h"
vandebo@chromium.org8459d4e2010-09-24 22:25:30 +000019#include "SkPDFStream.h"
20#include "SkPDFTypes.h"
senorblancob0e89dc2014-10-20 14:03:12 -070021#include "SkReadBuffer.h"
vandebo@chromium.org8459d4e2010-09-24 22:25:30 +000022#include "SkScalar.h"
23#include "SkStream.h"
vandebo@chromium.org421d6442011-07-20 17:39:01 +000024#include "SkTypes.h"
tfarina@chromium.org8f6884a2014-01-24 20:56:26 +000025#include "Test.h"
halcanary66a82f32015-10-12 13:05:04 -070026#include "sk_tool_utils.h"
vandebo@chromium.org8459d4e2010-09-24 22:25:30 +000027
commit-bot@chromium.org608ea652013-10-03 19:29:21 +000028#define DUMMY_TEXT "DCT compessed stream."
29
halcanaryf7a169e2015-05-01 07:35:45 -070030namespace {
31struct Catalog {
32 SkPDFSubstituteMap substitutes;
33 SkPDFObjNumMap numbers;
34};
35} // namespace
reed@google.com8a85d0c2011-06-24 19:12:12 +000036
halcanaryf7a169e2015-05-01 07:35:45 -070037template <typename T>
halcanary96fcdcc2015-08-27 07:41:13 -070038static SkString emit_to_string(T& obj, Catalog* catPtr = nullptr) {
halcanaryf7a169e2015-05-01 07:35:45 -070039 Catalog catalog;
halcanary6a144342015-01-23 11:45:10 -080040 SkDynamicMemoryWStream buffer;
halcanaryf7a169e2015-05-01 07:35:45 -070041 if (!catPtr) {
42 catPtr = &catalog;
halcanary130444f2015-04-25 06:45:07 -070043 }
halcanaryf7a169e2015-05-01 07:35:45 -070044 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.org8459d4e2010-09-24 22:25:30 +000049}
50
halcanaryf7a169e2015-05-01 07:35:45 -070051static 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.org421d6442011-07-20 17:39:01 +000053}
54
halcanaryf7a169e2015-05-01 07:35:45 -070055#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.org421d6442011-07-20 17:39:01 +000081static void TestPDFStream(skiatest::Reporter* reporter) {
82 char streamBytes[] = "Test\nFoo\tBar";
scroggoa1193e42015-01-21 12:09:53 -080083 SkAutoTDelete<SkMemoryStream> streamData(new SkMemoryStream(
vandebo@chromium.orgd96d17b2013-01-04 19:31:24 +000084 streamBytes, strlen(streamBytes), true));
85 SkAutoTUnref<SkPDFStream> stream(new SkPDFStream(streamData.get()));
halcanaryf7a169e2015-05-01 07:35:45 -070086 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.org421d6442011-07-20 17:39:01 +000094
halcanary91d1d622015-02-17 14:43:06 -080095 {
vandebo@chromium.org421d6442011-07-20 17:39:01 +000096 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.orgd96d17b2013-01-04 19:31:24 +0000102 SkAutoTUnref<SkPDFStream> stream(new SkPDFStream(streamData2.get()));
vandebo@chromium.org421d6442011-07-20 17:39:01 +0000103
104 SkDynamicMemoryWStream compressedByteStream;
halcanary48305e82015-08-18 13:30:25 -0700105 SkDeflateWStream deflateWStream(&compressedByteStream);
106 deflateWStream.write(streamBytes2, strlen(streamBytes2));
107 deflateWStream.finalize();
vandebo@chromium.org421d6442011-07-20 17:39:01 +0000108
mtkleincabc08c2015-02-19 08:29:24 -0800109 SkDynamicMemoryWStream expected;
halcanary37c46ca2015-03-31 12:30:20 -0700110 expected.writeText("<</Filter /FlateDecode\n/Length 116>> stream\n");
halcanary48305e82015-08-18 13:30:25 -0700111 compressedByteStream.writeToStream(&expected);
112 compressedByteStream.reset();
mtkleincabc08c2015-02-19 08:29:24 -0800113 expected.writeText("\nendstream");
114 SkAutoDataUnref expectedResultData2(expected.copyToData());
halcanaryf7a169e2015-05-01 07:35:45 -0700115 SkString result = emit_to_string(*stream);
116 ASSERT_EQL(reporter,
117 result,
118 (const char*)expectedResultData2->data(),
119 expectedResultData2->size());
vandebo@chromium.org421d6442011-07-20 17:39:01 +0000120 }
121}
122
halcanaryf7a169e2015-05-01 07:35:45 -0700123static 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.org8459d4e2010-09-24 22:25:30 +0000128
halcanaryf7a169e2015-05-01 07:35:45 -0700129 objNumMap.addObject(a1.get());
130 objNumMap.addObject(a2.get());
131 objNumMap.addObject(a3.get());
vandebo@chromium.org8459d4e2010-09-24 22:25:30 +0000132
halcanaryf7a169e2015-05-01 07:35:45 -0700133 // 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.org8459d4e2010-09-24 22:25:30 +0000141}
142
143static void TestObjectRef(skiatest::Reporter* reporter) {
halcanaryf7a169e2015-05-01 07:35:45 -0700144 SkAutoTUnref<SkPDFArray> a1(new SkPDFArray);
145 SkAutoTUnref<SkPDFArray> a2(new SkPDFArray);
146 a2->appendObjRef(SkRef(a1.get()));
vandebo@chromium.org8459d4e2010-09-24 22:25:30 +0000147
halcanaryf7a169e2015-05-01 07:35:45 -0700148 Catalog catalog;
149 catalog.numbers.addObject(a1.get());
150 REPORTER_ASSERT(reporter, catalog.numbers.getObjectNumber(a1.get()) == 1);
vandebo@chromium.org8459d4e2010-09-24 22:25:30 +0000151
halcanaryf7a169e2015-05-01 07:35:45 -0700152 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.org8459d4e2010-09-24 22:25:30 +0000156}
157
vandebo@chromium.org2ef12d42011-07-06 23:31:24 +0000158static void TestSubstitute(skiatest::Reporter* reporter) {
halcanaryf3d53a02015-03-24 15:29:39 -0700159 SkAutoTUnref<SkPDFDict> proxy(new SkPDFDict());
160 SkAutoTUnref<SkPDFDict> stub(new SkPDFDict());
vandebo@chromium.org2ef12d42011-07-06 23:31:24 +0000161
halcanaryf7a169e2015-05-01 07:35:45 -0700162 proxy->insertInt("Value", 33);
163 stub->insertInt("Value", 44);
vandebo@chromium.org2ef12d42011-07-06 23:31:24 +0000164
halcanary37c46ca2015-03-31 12:30:20 -0700165 SkPDFSubstituteMap substituteMap;
166 substituteMap.setSubstitute(proxy.get(), stub.get());
167 SkPDFObjNumMap catalog;
halcanaryf41061c2015-03-25 11:29:18 -0700168 catalog.addObject(proxy.get());
vandebo@chromium.org2ef12d42011-07-06 23:31:24 +0000169
halcanary37c46ca2015-03-31 12:30:20 -0700170 REPORTER_ASSERT(reporter, stub.get() == substituteMap.getSubstitute(proxy));
171 REPORTER_ASSERT(reporter, proxy.get() != substituteMap.getSubstitute(stub));
vandebo@chromium.org2ef12d42011-07-06 23:31:24 +0000172}
173
vandebo@chromium.org4e1cc6a2013-01-25 19:27:23 +0000174// 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.com54f0d1b2013-02-27 19:17:41 +0000178static void test_issue1083() {
halcanary3d32d502015-03-01 06:55:20 -0800179 SkDynamicMemoryWStream outStream;
180 SkAutoTUnref<SkDocument> doc(SkDocument::CreatePDF(&outStream));
181 SkCanvas* canvas = doc->beginPage(100.0f, 100.0f);
vandebo@chromium.org4e1cc6a2013-01-25 19:27:23 +0000182 SkPaint paint;
183 paint.setTextEncoding(SkPaint::kGlyphID_TextEncoding);
184
185 uint16_t glyphID = 65000;
halcanary3d32d502015-03-01 06:55:20 -0800186 canvas->drawText(&glyphID, 2, 0, 0, paint);
vandebo@chromium.org4e1cc6a2013-01-25 19:27:23 +0000187
halcanary3d32d502015-03-01 06:55:20 -0800188 doc->close();
vandebo@chromium.org4e1cc6a2013-01-25 19:27:23 +0000189}
190
halcanaryf7a169e2015-05-01 07:35:45 -0700191static void TestPDFUnion(skiatest::Reporter* reporter) {
192 SkPDFUnion boolTrue = SkPDFUnion::Bool(true);
193 ASSERT_EMIT_EQ(reporter, boolTrue, "true");
vandebo@chromium.org8459d4e2010-09-24 22:25:30 +0000194
halcanaryf7a169e2015-05-01 07:35:45 -0700195 SkPDFUnion boolFalse = SkPDFUnion::Bool(false);
196 ASSERT_EMIT_EQ(reporter, boolFalse, "false");
vandebo@chromium.org8459d4e2010-09-24 22:25:30 +0000197
halcanaryf7a169e2015-05-01 07:35:45 -0700198 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.org6cc26da2011-05-18 17:08:05 +0000205#if !defined(SK_ALLOW_LARGE_PDF_SCALARS)
halcanaryf7a169e2015-05-01 07:35:45 -0700206 ASSERT_EMIT_EQ(reporter, bigScalar, "111000");
vandebo@chromium.org094316b2011-03-04 03:15:13 +0000207#else
halcanaryf7a169e2015-05-01 07:35:45 -0700208 ASSERT_EMIT_EQ(reporter, bigScalar, "110999.75");
vandebo@chromium.org094316b2011-03-04 03:15:13 +0000209
halcanaryf7a169e2015-05-01 07:35:45 -0700210 SkPDFUnion biggerScalar = SkPDFUnion::Scalar(50000000.1);
211 ASSERT_EMIT_EQ(reporter, biggerScalar, "50000000");
vandebo@chromium.org094316b2011-03-04 03:15:13 +0000212
halcanaryf7a169e2015-05-01 07:35:45 -0700213 SkPDFUnion smallestScalar = SkPDFUnion::Scalar(1.0 / 65536);
214 ASSERT_EMIT_EQ(reporter, smallestScalar, "0.00001526");
vandebo@chromium.org094316b2011-03-04 03:15:13 +0000215#endif
216
halcanaryf7a169e2015-05-01 07:35:45 -0700217 SkPDFUnion stringSimple = SkPDFUnion::String("test ) string ( foo");
218 ASSERT_EMIT_EQ(reporter, stringSimple, "(test \\) string \\( foo)");
vandebo@chromium.org8459d4e2010-09-24 22:25:30 +0000219
halcanaryf7a169e2015-05-01 07:35:45 -0700220 SkString stringComplexInput("\ttest ) string ( foo");
221 SkPDFUnion stringComplex = SkPDFUnion::String(stringComplexInput);
222 ASSERT_EMIT_EQ(reporter,
223 stringComplex,
224 "<0974657374202920737472696E67202820666F6F>");
vandebo@chromium.org8459d4e2010-09-24 22:25:30 +0000225
halcanaryf7a169e2015-05-01 07:35:45 -0700226 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.org251a7662012-09-21 17:50:50 +0000236
vandebo@chromium.orgc0376fe2012-03-05 18:44:33 +0000237 // Test that we correctly handle characters with the high-bit set.
halcanaryf7a169e2015-05-01 07:35:45 -0700238 SkString highBitString("\xDE\xAD" "be\xEF");
239 SkPDFUnion highBitName = SkPDFUnion::Name(highBitString);
240 ASSERT_EMIT_EQ(reporter, highBitName, "/#DE#ADbe#EF");
241}
vandebo@chromium.orgc0376fe2012-03-05 18:44:33 +0000242
halcanaryf7a169e2015-05-01 07:35:45 -0700243static void TestPDFArray(skiatest::Reporter* reporter) {
vandebo@chromium.orgd96d17b2013-01-04 19:31:24 +0000244 SkAutoTUnref<SkPDFArray> array(new SkPDFArray);
halcanaryf7a169e2015-05-01 07:35:45 -0700245 ASSERT_EMIT_EQ(reporter, *array, "[]");
vandebo@chromium.org8459d4e2010-09-24 22:25:30 +0000246
halcanaryf7a169e2015-05-01 07:35:45 -0700247 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
294static void TestPDFDict(skiatest::Reporter* reporter) {
vandebo@chromium.orgd96d17b2013-01-04 19:31:24 +0000295 SkAutoTUnref<SkPDFDict> dict(new SkPDFDict);
halcanaryf7a169e2015-05-01 07:35:45 -0700296 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
halcanary130444f2015-04-25 06:45:07 -0700309 SkString n3("n3");
halcanaryf7a169e2015-05-01 07:35:45 -0700310 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.org8459d4e2010-09-24 22:25:30 +0000314
halcanaryf7a169e2015-05-01 07:35:45 -0700315 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
355DEF_TEST(PDFPrimitives, reporter) {
356 TestPDFUnion(reporter);
357 TestPDFArray(reporter);
358 TestPDFDict(reporter);
vandebo@chromium.org421d6442011-07-20 17:39:01 +0000359 TestPDFStream(reporter);
halcanaryf7a169e2015-05-01 07:35:45 -0700360 TestObjectNumberMap(reporter);
vandebo@chromium.org8459d4e2010-09-24 22:25:30 +0000361 TestObjectRef(reporter);
vandebo@chromium.org2ef12d42011-07-06 23:31:24 +0000362 TestSubstitute(reporter);
sugoi@google.com54f0d1b2013-02-27 19:17:41 +0000363 test_issue1083();
vandebo@chromium.org8459d4e2010-09-24 22:25:30 +0000364}
senorblancob0e89dc2014-10-20 14:03:12 -0700365
366namespace {
367
368class DummyImageFilter : public SkImageFilter {
369public:
halcanary96fcdcc2015-08-27 07:41:13 -0700370 DummyImageFilter(bool visited = false) : SkImageFilter(0, nullptr), fVisited(visited) {}
mtklein36352bf2015-03-25 18:17:31 -0700371 ~DummyImageFilter() override {}
senorblancob0e89dc2014-10-20 14:03:12 -0700372 virtual bool onFilterImage(Proxy*, const SkBitmap& src, const Context&,
mtklein36352bf2015-03-25 18:17:31 -0700373 SkBitmap* result, SkIPoint* offset) const override {
senorblancob0e89dc2014-10-20 14:03:12 -0700374 fVisited = true;
375 offset->fX = offset->fY = 0;
376 *result = src;
377 return true;
378 }
robertphillipsf3f5bad2014-12-19 13:49:15 -0800379 SK_TO_STRING_OVERRIDE()
senorblancob0e89dc2014-10-20 14:03:12 -0700380 SK_DECLARE_PUBLIC_FLATTENABLE_DESERIALIZATION_PROCS(DummyImageFilter)
senorblancob0e89dc2014-10-20 14:03:12 -0700381 bool visited() const { return fVisited; }
382
383private:
384 mutable bool fVisited;
385};
386
387SkFlattenable* DummyImageFilter::CreateProc(SkReadBuffer& buffer) {
388 SK_IMAGEFILTER_UNFLATTEN_COMMON(common, 0);
389 bool visited = buffer.readBool();
halcanary385fe4d2015-08-26 13:07:48 -0700390 return new DummyImageFilter(visited);
senorblancob0e89dc2014-10-20 14:03:12 -0700391}
392
robertphillipsf3f5bad2014-12-19 13:49:15 -0800393#ifndef SK_IGNORE_TO_STRING
394void DummyImageFilter::toString(SkString* str) const {
395 str->appendf("DummyImageFilter: (");
396 str->append(")");
397}
398#endif
399
senorblancob0e89dc2014-10-20 14:03:12 -0700400};
401
402// Check that PDF rendering of image filters successfully falls back to
403// CPU rasterization.
404DEF_TEST(PDFImageFilter, reporter) {
halcanary3d32d502015-03-01 06:55:20 -0800405 SkDynamicMemoryWStream stream;
406 SkAutoTUnref<SkDocument> doc(SkDocument::CreatePDF(&stream));
407 SkCanvas* canvas = doc->beginPage(100.0f, 100.0f);
408
senorblancob0e89dc2014-10-20 14:03:12 -0700409 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());
halcanary3d32d502015-03-01 06:55:20 -0800415 canvas->drawRect(SkRect::MakeWH(100, 100), paint);
416 doc->close();
senorblancob0e89dc2014-10-20 14:03:12 -0700417
418 // Filter was used in rendering; should be visited.
419 REPORTER_ASSERT(reporter, filter->visited());
420}
halcanary66a82f32015-10-12 13:05:04 -0700421
422// Check that PDF rendering of image filters successfully falls back to
423// CPU rasterization.
424DEF_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}