blob: 07ddabc9126d18f2a0df71888b251cc477b2ad80 [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"
halcanary8e9f5e32016-02-24 15:46:46 -080021#include "SkPDFUtils.h"
senorblancob0e89dc2014-10-20 14:03:12 -070022#include "SkReadBuffer.h"
vandebo@chromium.org8459d4e2010-09-24 22:25:30 +000023#include "SkScalar.h"
robertphillips4ba94e22016-04-04 12:07:47 -070024#include "SkSpecialImage.h"
vandebo@chromium.org8459d4e2010-09-24 22:25:30 +000025#include "SkStream.h"
vandebo@chromium.org421d6442011-07-20 17:39:01 +000026#include "SkTypes.h"
tfarina@chromium.org8f6884a2014-01-24 20:56:26 +000027#include "Test.h"
halcanary66a82f32015-10-12 13:05:04 -070028#include "sk_tool_utils.h"
vandebo@chromium.org8459d4e2010-09-24 22:25:30 +000029
commit-bot@chromium.org608ea652013-10-03 19:29:21 +000030#define DUMMY_TEXT "DCT compessed stream."
31
halcanaryf7a169e2015-05-01 07:35:45 -070032namespace {
33struct Catalog {
34 SkPDFSubstituteMap substitutes;
35 SkPDFObjNumMap numbers;
36};
37} // namespace
reed@google.com8a85d0c2011-06-24 19:12:12 +000038
halcanaryf7a169e2015-05-01 07:35:45 -070039template <typename T>
halcanary96fcdcc2015-08-27 07:41:13 -070040static SkString emit_to_string(T& obj, Catalog* catPtr = nullptr) {
halcanaryf7a169e2015-05-01 07:35:45 -070041 Catalog catalog;
halcanary6a144342015-01-23 11:45:10 -080042 SkDynamicMemoryWStream buffer;
halcanaryf7a169e2015-05-01 07:35:45 -070043 if (!catPtr) {
44 catPtr = &catalog;
halcanary130444f2015-04-25 06:45:07 -070045 }
halcanaryf7a169e2015-05-01 07:35:45 -070046 obj.emitObject(&buffer, catPtr->numbers, catPtr->substitutes);
47 SkAutoTDelete<SkStreamAsset> asset(buffer.detachAsStream());
48 SkString tmp(asset->getLength());
49 asset->read(tmp.writable_str(), asset->getLength());
50 return tmp;
vandebo@chromium.org8459d4e2010-09-24 22:25:30 +000051}
52
halcanaryf7a169e2015-05-01 07:35:45 -070053static bool eq(const SkString& str, const char* strPtr, size_t len) {
54 return len == str.size() && 0 == memcmp(str.c_str(), strPtr, len);
vandebo@chromium.org421d6442011-07-20 17:39:01 +000055}
56
halcanaryf7a169e2015-05-01 07:35:45 -070057#define ASSERT_EQL(REPORTER, SKSTRING, STRING, LEN) \
58 do { \
59 const char* strptr = STRING; \
60 const SkString& sks = SKSTRING; \
61 if (!eq(sks, strptr, LEN)) { \
62 REPORT_FAILURE( \
63 REPORTER, \
64 "", \
65 SkStringPrintf("'%s' != '%s'", strptr, sks.c_str())); \
66 } \
67 } while (false)
68
69#define ASSERT_EQ(REPORTER, SKSTRING, STRING) \
70 do { \
71 const char* str = STRING; \
72 ASSERT_EQL(REPORTER, SKSTRING, str, strlen(str)); \
73 } while (false)
74
75#define ASSERT_EMIT_EQ(REPORTER, OBJECT, STRING) \
76 do { \
77 SkString result = emit_to_string(OBJECT); \
78 ASSERT_EQ(REPORTER, result, STRING); \
79 } while (false)
80
81
82
vandebo@chromium.org421d6442011-07-20 17:39:01 +000083static void TestPDFStream(skiatest::Reporter* reporter) {
84 char streamBytes[] = "Test\nFoo\tBar";
scroggoa1193e42015-01-21 12:09:53 -080085 SkAutoTDelete<SkMemoryStream> streamData(new SkMemoryStream(
vandebo@chromium.orgd96d17b2013-01-04 19:31:24 +000086 streamBytes, strlen(streamBytes), true));
halcanary8103a342016-03-08 15:10:16 -080087 sk_sp<SkPDFStream> stream(new SkPDFStream(streamData.get()));
halcanaryf7a169e2015-05-01 07:35:45 -070088 ASSERT_EMIT_EQ(reporter,
89 *stream,
90 "<</Length 12>> stream\nTest\nFoo\tBar\nendstream");
91 stream->insertInt("Attribute", 42);
92 ASSERT_EMIT_EQ(reporter,
93 *stream,
94 "<</Length 12\n/Attribute 42>> stream\n"
95 "Test\nFoo\tBar\nendstream");
vandebo@chromium.org421d6442011-07-20 17:39:01 +000096
halcanary91d1d622015-02-17 14:43:06 -080097 {
vandebo@chromium.org421d6442011-07-20 17:39:01 +000098 char streamBytes2[] = "This is a longer string, so that compression "
99 "can do something with it. With shorter strings, "
100 "the short circuit logic cuts in and we end up "
101 "with an uncompressed string.";
102 SkAutoDataUnref streamData2(SkData::NewWithCopy(streamBytes2,
103 strlen(streamBytes2)));
halcanary8103a342016-03-08 15:10:16 -0800104 sk_sp<SkPDFStream> stream(new SkPDFStream(streamData2.get()));
vandebo@chromium.org421d6442011-07-20 17:39:01 +0000105
106 SkDynamicMemoryWStream compressedByteStream;
halcanary48305e82015-08-18 13:30:25 -0700107 SkDeflateWStream deflateWStream(&compressedByteStream);
108 deflateWStream.write(streamBytes2, strlen(streamBytes2));
109 deflateWStream.finalize();
vandebo@chromium.org421d6442011-07-20 17:39:01 +0000110
mtkleincabc08c2015-02-19 08:29:24 -0800111 SkDynamicMemoryWStream expected;
halcanary37c46ca2015-03-31 12:30:20 -0700112 expected.writeText("<</Filter /FlateDecode\n/Length 116>> stream\n");
halcanary48305e82015-08-18 13:30:25 -0700113 compressedByteStream.writeToStream(&expected);
114 compressedByteStream.reset();
mtkleincabc08c2015-02-19 08:29:24 -0800115 expected.writeText("\nendstream");
116 SkAutoDataUnref expectedResultData2(expected.copyToData());
halcanaryf7a169e2015-05-01 07:35:45 -0700117 SkString result = emit_to_string(*stream);
118 ASSERT_EQL(reporter,
119 result,
120 (const char*)expectedResultData2->data(),
121 expectedResultData2->size());
vandebo@chromium.org421d6442011-07-20 17:39:01 +0000122 }
123}
124
halcanaryf7a169e2015-05-01 07:35:45 -0700125static void TestObjectNumberMap(skiatest::Reporter* reporter) {
126 SkPDFObjNumMap objNumMap;
halcanary8103a342016-03-08 15:10:16 -0800127 sk_sp<SkPDFArray> a1(new SkPDFArray);
128 sk_sp<SkPDFArray> a2(new SkPDFArray);
129 sk_sp<SkPDFArray> a3(new SkPDFArray);
vandebo@chromium.org8459d4e2010-09-24 22:25:30 +0000130
halcanaryf7a169e2015-05-01 07:35:45 -0700131 objNumMap.addObject(a1.get());
132 objNumMap.addObject(a2.get());
133 objNumMap.addObject(a3.get());
vandebo@chromium.org8459d4e2010-09-24 22:25:30 +0000134
halcanaryf7a169e2015-05-01 07:35:45 -0700135 // The objects should be numbered in the order they are added,
136 // starting with 1.
137 REPORTER_ASSERT(reporter, objNumMap.getObjectNumber(a1.get()) == 1);
138 REPORTER_ASSERT(reporter, objNumMap.getObjectNumber(a2.get()) == 2);
139 REPORTER_ASSERT(reporter, objNumMap.getObjectNumber(a3.get()) == 3);
140 // Assert that repeated calls to get the object number return
141 // consistent result.
142 REPORTER_ASSERT(reporter, objNumMap.getObjectNumber(a1.get()) == 1);
vandebo@chromium.org8459d4e2010-09-24 22:25:30 +0000143}
144
145static void TestObjectRef(skiatest::Reporter* reporter) {
halcanary8103a342016-03-08 15:10:16 -0800146 sk_sp<SkPDFArray> a1(new SkPDFArray);
147 sk_sp<SkPDFArray> a2(new SkPDFArray);
148 a2->appendObjRef(a1);
vandebo@chromium.org8459d4e2010-09-24 22:25:30 +0000149
halcanaryf7a169e2015-05-01 07:35:45 -0700150 Catalog catalog;
151 catalog.numbers.addObject(a1.get());
152 REPORTER_ASSERT(reporter, catalog.numbers.getObjectNumber(a1.get()) == 1);
vandebo@chromium.org8459d4e2010-09-24 22:25:30 +0000153
halcanaryf7a169e2015-05-01 07:35:45 -0700154 SkString result = emit_to_string(*a2, &catalog);
155 // If appendObjRef misbehaves, then the result would
156 // be [[]], not [1 0 R].
157 ASSERT_EQ(reporter, result, "[1 0 R]");
vandebo@chromium.org8459d4e2010-09-24 22:25:30 +0000158}
159
vandebo@chromium.org2ef12d42011-07-06 23:31:24 +0000160static void TestSubstitute(skiatest::Reporter* reporter) {
halcanary8103a342016-03-08 15:10:16 -0800161 sk_sp<SkPDFDict> proxy(new SkPDFDict());
162 sk_sp<SkPDFDict> stub(new SkPDFDict());
vandebo@chromium.org2ef12d42011-07-06 23:31:24 +0000163
halcanaryf7a169e2015-05-01 07:35:45 -0700164 proxy->insertInt("Value", 33);
165 stub->insertInt("Value", 44);
vandebo@chromium.org2ef12d42011-07-06 23:31:24 +0000166
halcanary37c46ca2015-03-31 12:30:20 -0700167 SkPDFSubstituteMap substituteMap;
168 substituteMap.setSubstitute(proxy.get(), stub.get());
169 SkPDFObjNumMap catalog;
halcanaryf41061c2015-03-25 11:29:18 -0700170 catalog.addObject(proxy.get());
vandebo@chromium.org2ef12d42011-07-06 23:31:24 +0000171
halcanary8103a342016-03-08 15:10:16 -0800172 REPORTER_ASSERT(reporter, stub.get() == substituteMap.getSubstitute(proxy.get()));
173 REPORTER_ASSERT(reporter, proxy.get() != substituteMap.getSubstitute(stub.get()));
vandebo@chromium.org2ef12d42011-07-06 23:31:24 +0000174}
175
vandebo@chromium.org4e1cc6a2013-01-25 19:27:23 +0000176// This test used to assert without the fix submitted for
177// http://code.google.com/p/skia/issues/detail?id=1083.
178// SKP files might have invalid glyph ids. This test ensures they are ignored,
179// and there is no assert on input data in Debug mode.
sugoi@google.com54f0d1b2013-02-27 19:17:41 +0000180static void test_issue1083() {
halcanary3d32d502015-03-01 06:55:20 -0800181 SkDynamicMemoryWStream outStream;
halcanary4b656662016-04-27 07:45:18 -0700182 sk_sp<SkDocument> doc(SkDocument::MakePDF(&outStream));
halcanary3d32d502015-03-01 06:55:20 -0800183 SkCanvas* canvas = doc->beginPage(100.0f, 100.0f);
vandebo@chromium.org4e1cc6a2013-01-25 19:27:23 +0000184 SkPaint paint;
185 paint.setTextEncoding(SkPaint::kGlyphID_TextEncoding);
186
187 uint16_t glyphID = 65000;
halcanary3d32d502015-03-01 06:55:20 -0800188 canvas->drawText(&glyphID, 2, 0, 0, paint);
vandebo@chromium.org4e1cc6a2013-01-25 19:27:23 +0000189
halcanary3d32d502015-03-01 06:55:20 -0800190 doc->close();
vandebo@chromium.org4e1cc6a2013-01-25 19:27:23 +0000191}
192
halcanaryf7a169e2015-05-01 07:35:45 -0700193static void TestPDFUnion(skiatest::Reporter* reporter) {
194 SkPDFUnion boolTrue = SkPDFUnion::Bool(true);
195 ASSERT_EMIT_EQ(reporter, boolTrue, "true");
vandebo@chromium.org8459d4e2010-09-24 22:25:30 +0000196
halcanaryf7a169e2015-05-01 07:35:45 -0700197 SkPDFUnion boolFalse = SkPDFUnion::Bool(false);
198 ASSERT_EMIT_EQ(reporter, boolFalse, "false");
vandebo@chromium.org8459d4e2010-09-24 22:25:30 +0000199
halcanaryf7a169e2015-05-01 07:35:45 -0700200 SkPDFUnion int42 = SkPDFUnion::Int(42);
201 ASSERT_EMIT_EQ(reporter, int42, "42");
202
203 SkPDFUnion realHalf = SkPDFUnion::Scalar(SK_ScalarHalf);
halcanary8e9f5e32016-02-24 15:46:46 -0800204 ASSERT_EMIT_EQ(reporter, realHalf, ".5");
halcanaryf7a169e2015-05-01 07:35:45 -0700205
206 SkPDFUnion bigScalar = SkPDFUnion::Scalar(110999.75f);
halcanaryf7a169e2015-05-01 07:35:45 -0700207 ASSERT_EMIT_EQ(reporter, bigScalar, "110999.75");
vandebo@chromium.org094316b2011-03-04 03:15:13 +0000208
halcanary8e9f5e32016-02-24 15:46:46 -0800209 SkPDFUnion biggerScalar = SkPDFUnion::Scalar(50000000.1f);
halcanaryf7a169e2015-05-01 07:35:45 -0700210 ASSERT_EMIT_EQ(reporter, biggerScalar, "50000000");
vandebo@chromium.org094316b2011-03-04 03:15:13 +0000211
halcanary8e9f5e32016-02-24 15:46:46 -0800212 SkPDFUnion smallestScalar = SkPDFUnion::Scalar(1.0f / 65536);
213 ASSERT_EMIT_EQ(reporter, smallestScalar, ".0000152587890");
vandebo@chromium.org094316b2011-03-04 03:15:13 +0000214
halcanaryf7a169e2015-05-01 07:35:45 -0700215 SkPDFUnion stringSimple = SkPDFUnion::String("test ) string ( foo");
216 ASSERT_EMIT_EQ(reporter, stringSimple, "(test \\) string \\( foo)");
vandebo@chromium.org8459d4e2010-09-24 22:25:30 +0000217
halcanaryf7a169e2015-05-01 07:35:45 -0700218 SkString stringComplexInput("\ttest ) string ( foo");
219 SkPDFUnion stringComplex = SkPDFUnion::String(stringComplexInput);
220 ASSERT_EMIT_EQ(reporter,
221 stringComplex,
222 "<0974657374202920737472696E67202820666F6F>");
vandebo@chromium.org8459d4e2010-09-24 22:25:30 +0000223
halcanaryf7a169e2015-05-01 07:35:45 -0700224 SkString nameInput("Test name\twith#tab");
225 SkPDFUnion name = SkPDFUnion::Name(nameInput);
226 ASSERT_EMIT_EQ(reporter, name, "/Test#20name#09with#23tab");
227
228 SkString nameInput2("A#/%()<>[]{}B");
229 SkPDFUnion name2 = SkPDFUnion::Name(nameInput2);
230 ASSERT_EMIT_EQ(reporter, name2, "/A#23#2F#25#28#29#3C#3E#5B#5D#7B#7DB");
231
232 SkPDFUnion name3 = SkPDFUnion::Name("SimpleNameWithOnlyPrintableASCII");
233 ASSERT_EMIT_EQ(reporter, name3, "/SimpleNameWithOnlyPrintableASCII");
vandebo@chromium.org251a7662012-09-21 17:50:50 +0000234
vandebo@chromium.orgc0376fe2012-03-05 18:44:33 +0000235 // Test that we correctly handle characters with the high-bit set.
halcanaryf7a169e2015-05-01 07:35:45 -0700236 SkString highBitString("\xDE\xAD" "be\xEF");
237 SkPDFUnion highBitName = SkPDFUnion::Name(highBitString);
238 ASSERT_EMIT_EQ(reporter, highBitName, "/#DE#ADbe#EF");
239}
vandebo@chromium.orgc0376fe2012-03-05 18:44:33 +0000240
halcanaryf7a169e2015-05-01 07:35:45 -0700241static void TestPDFArray(skiatest::Reporter* reporter) {
halcanary8103a342016-03-08 15:10:16 -0800242 sk_sp<SkPDFArray> array(new SkPDFArray);
halcanaryf7a169e2015-05-01 07:35:45 -0700243 ASSERT_EMIT_EQ(reporter, *array, "[]");
vandebo@chromium.org8459d4e2010-09-24 22:25:30 +0000244
halcanaryf7a169e2015-05-01 07:35:45 -0700245 array->appendInt(42);
246 ASSERT_EMIT_EQ(reporter, *array, "[42]");
247
248 array->appendScalar(SK_ScalarHalf);
halcanary8e9f5e32016-02-24 15:46:46 -0800249 ASSERT_EMIT_EQ(reporter, *array, "[42 .5]");
halcanaryf7a169e2015-05-01 07:35:45 -0700250
251 array->appendInt(0);
halcanary8e9f5e32016-02-24 15:46:46 -0800252 ASSERT_EMIT_EQ(reporter, *array, "[42 .5 0]");
halcanaryf7a169e2015-05-01 07:35:45 -0700253
254 array->appendBool(true);
halcanary8e9f5e32016-02-24 15:46:46 -0800255 ASSERT_EMIT_EQ(reporter, *array, "[42 .5 0 true]");
halcanaryf7a169e2015-05-01 07:35:45 -0700256
257 array->appendName("ThisName");
halcanary8e9f5e32016-02-24 15:46:46 -0800258 ASSERT_EMIT_EQ(reporter, *array, "[42 .5 0 true /ThisName]");
halcanaryf7a169e2015-05-01 07:35:45 -0700259
260 array->appendName(SkString("AnotherName"));
halcanary8e9f5e32016-02-24 15:46:46 -0800261 ASSERT_EMIT_EQ(reporter, *array, "[42 .5 0 true /ThisName /AnotherName]");
halcanaryf7a169e2015-05-01 07:35:45 -0700262
263 array->appendString("This String");
264 ASSERT_EMIT_EQ(reporter, *array,
halcanary8e9f5e32016-02-24 15:46:46 -0800265 "[42 .5 0 true /ThisName /AnotherName (This String)]");
halcanaryf7a169e2015-05-01 07:35:45 -0700266
267 array->appendString(SkString("Another String"));
268 ASSERT_EMIT_EQ(reporter, *array,
halcanary8e9f5e32016-02-24 15:46:46 -0800269 "[42 .5 0 true /ThisName /AnotherName (This String) "
halcanaryf7a169e2015-05-01 07:35:45 -0700270 "(Another String)]");
271
halcanary8103a342016-03-08 15:10:16 -0800272 sk_sp<SkPDFArray> innerArray(new SkPDFArray);
halcanaryf7a169e2015-05-01 07:35:45 -0700273 innerArray->appendInt(-1);
halcanary8103a342016-03-08 15:10:16 -0800274 array->appendObject(std::move(innerArray));
halcanaryf7a169e2015-05-01 07:35:45 -0700275 ASSERT_EMIT_EQ(reporter, *array,
halcanary8e9f5e32016-02-24 15:46:46 -0800276 "[42 .5 0 true /ThisName /AnotherName (This String) "
halcanaryf7a169e2015-05-01 07:35:45 -0700277 "(Another String) [-1]]");
278
halcanary8103a342016-03-08 15:10:16 -0800279 sk_sp<SkPDFArray> referencedArray(new SkPDFArray);
halcanaryf7a169e2015-05-01 07:35:45 -0700280 Catalog catalog;
281 catalog.numbers.addObject(referencedArray.get());
282 REPORTER_ASSERT(reporter, catalog.numbers.getObjectNumber(
283 referencedArray.get()) == 1);
halcanary8103a342016-03-08 15:10:16 -0800284 array->appendObjRef(std::move(referencedArray));
halcanaryf7a169e2015-05-01 07:35:45 -0700285
286 SkString result = emit_to_string(*array, &catalog);
287 ASSERT_EQ(reporter, result,
halcanary8e9f5e32016-02-24 15:46:46 -0800288 "[42 .5 0 true /ThisName /AnotherName (This String) "
halcanaryf7a169e2015-05-01 07:35:45 -0700289 "(Another String) [-1] 1 0 R]");
290}
291
292static void TestPDFDict(skiatest::Reporter* reporter) {
halcanary8103a342016-03-08 15:10:16 -0800293 sk_sp<SkPDFDict> dict(new SkPDFDict);
halcanaryf7a169e2015-05-01 07:35:45 -0700294 ASSERT_EMIT_EQ(reporter, *dict, "<<>>");
295
296 dict->insertInt("n1", SkToSizeT(42));
297 ASSERT_EMIT_EQ(reporter, *dict, "<</n1 42>>");
298
299 dict.reset(new SkPDFDict);
300 ASSERT_EMIT_EQ(reporter, *dict, "<<>>");
301
302 dict->insertInt("n1", 42);
303 ASSERT_EMIT_EQ(reporter, *dict, "<</n1 42>>");
304
305 dict->insertScalar("n2", SK_ScalarHalf);
306
halcanary130444f2015-04-25 06:45:07 -0700307 SkString n3("n3");
halcanary8103a342016-03-08 15:10:16 -0800308 sk_sp<SkPDFArray> innerArray(new SkPDFArray);
halcanaryf7a169e2015-05-01 07:35:45 -0700309 innerArray->appendInt(-100);
halcanary8103a342016-03-08 15:10:16 -0800310 dict->insertObject(n3, std::move(innerArray));
halcanary8e9f5e32016-02-24 15:46:46 -0800311 ASSERT_EMIT_EQ(reporter, *dict, "<</n1 42\n/n2 .5\n/n3 [-100]>>");
vandebo@chromium.org8459d4e2010-09-24 22:25:30 +0000312
halcanaryf7a169e2015-05-01 07:35:45 -0700313 dict.reset(new SkPDFDict);
314 ASSERT_EMIT_EQ(reporter, *dict, "<<>>");
315
316 dict->insertInt("n1", 24);
317 ASSERT_EMIT_EQ(reporter, *dict, "<</n1 24>>");
318
319 dict->insertInt("n2", SkToSizeT(99));
320 ASSERT_EMIT_EQ(reporter, *dict, "<</n1 24\n/n2 99>>");
321
322 dict->insertScalar("n3", SK_ScalarHalf);
halcanary8e9f5e32016-02-24 15:46:46 -0800323 ASSERT_EMIT_EQ(reporter, *dict, "<</n1 24\n/n2 99\n/n3 .5>>");
halcanaryf7a169e2015-05-01 07:35:45 -0700324
325 dict->insertName("n4", "AName");
halcanary8e9f5e32016-02-24 15:46:46 -0800326 ASSERT_EMIT_EQ(reporter, *dict, "<</n1 24\n/n2 99\n/n3 .5\n/n4 /AName>>");
halcanaryf7a169e2015-05-01 07:35:45 -0700327
328 dict->insertName("n5", SkString("AnotherName"));
halcanary8e9f5e32016-02-24 15:46:46 -0800329 ASSERT_EMIT_EQ(reporter, *dict, "<</n1 24\n/n2 99\n/n3 .5\n/n4 /AName\n"
halcanaryf7a169e2015-05-01 07:35:45 -0700330 "/n5 /AnotherName>>");
331
332 dict->insertString("n6", "A String");
halcanary8e9f5e32016-02-24 15:46:46 -0800333 ASSERT_EMIT_EQ(reporter, *dict, "<</n1 24\n/n2 99\n/n3 .5\n/n4 /AName\n"
halcanaryf7a169e2015-05-01 07:35:45 -0700334 "/n5 /AnotherName\n/n6 (A String)>>");
335
336 dict->insertString("n7", SkString("Another String"));
halcanary8e9f5e32016-02-24 15:46:46 -0800337 ASSERT_EMIT_EQ(reporter, *dict, "<</n1 24\n/n2 99\n/n3 .5\n/n4 /AName\n"
halcanaryf7a169e2015-05-01 07:35:45 -0700338 "/n5 /AnotherName\n/n6 (A String)\n/n7 (Another String)>>");
339
340 dict.reset(new SkPDFDict("DType"));
341 ASSERT_EMIT_EQ(reporter, *dict, "<</Type /DType>>");
halcanary8e9f5e32016-02-24 15:46:46 -0800342
halcanary8103a342016-03-08 15:10:16 -0800343 sk_sp<SkPDFArray> referencedArray(new SkPDFArray);
halcanaryf7a169e2015-05-01 07:35:45 -0700344 Catalog catalog;
345 catalog.numbers.addObject(referencedArray.get());
346 REPORTER_ASSERT(reporter, catalog.numbers.getObjectNumber(
347 referencedArray.get()) == 1);
halcanary8103a342016-03-08 15:10:16 -0800348 dict->insertObjRef("n1", std::move(referencedArray));
halcanaryf7a169e2015-05-01 07:35:45 -0700349 SkString result = emit_to_string(*dict, &catalog);
350 ASSERT_EQ(reporter, result, "<</Type /DType\n/n1 1 0 R>>");
351}
352
353DEF_TEST(PDFPrimitives, reporter) {
354 TestPDFUnion(reporter);
355 TestPDFArray(reporter);
356 TestPDFDict(reporter);
vandebo@chromium.org421d6442011-07-20 17:39:01 +0000357 TestPDFStream(reporter);
halcanaryf7a169e2015-05-01 07:35:45 -0700358 TestObjectNumberMap(reporter);
vandebo@chromium.org8459d4e2010-09-24 22:25:30 +0000359 TestObjectRef(reporter);
vandebo@chromium.org2ef12d42011-07-06 23:31:24 +0000360 TestSubstitute(reporter);
sugoi@google.com54f0d1b2013-02-27 19:17:41 +0000361 test_issue1083();
vandebo@chromium.org8459d4e2010-09-24 22:25:30 +0000362}
senorblancob0e89dc2014-10-20 14:03:12 -0700363
364namespace {
365
366class DummyImageFilter : public SkImageFilter {
367public:
robertphillips43c2ad42016-04-04 05:05:11 -0700368 static sk_sp<DummyImageFilter> Make(bool visited = false) {
369 return sk_sp<DummyImageFilter>(new DummyImageFilter(visited));
370 }
371
372 SK_TO_STRING_OVERRIDE()
373 SK_DECLARE_PUBLIC_FLATTENABLE_DESERIALIZATION_PROCS(DummyImageFilter)
374 bool visited() const { return fVisited; }
375
376protected:
robertphillips4ba94e22016-04-04 12:07:47 -0700377 sk_sp<SkSpecialImage> onFilterImage(SkSpecialImage* source, const Context&,
378 SkIPoint* offset) const override {
senorblancob0e89dc2014-10-20 14:03:12 -0700379 fVisited = true;
380 offset->fX = offset->fY = 0;
robertphillips4ba94e22016-04-04 12:07:47 -0700381 return sk_ref_sp<SkSpecialImage>(source);
senorblancob0e89dc2014-10-20 14:03:12 -0700382 }
senorblancob0e89dc2014-10-20 14:03:12 -0700383
384private:
robertphillips43c2ad42016-04-04 05:05:11 -0700385 DummyImageFilter(bool visited) : INHERITED(nullptr, 0, nullptr), fVisited(visited) {}
386
senorblancob0e89dc2014-10-20 14:03:12 -0700387 mutable bool fVisited;
robertphillips43c2ad42016-04-04 05:05:11 -0700388
389 typedef SkImageFilter INHERITED;
senorblancob0e89dc2014-10-20 14:03:12 -0700390};
391
reed60c9b582016-04-03 09:11:13 -0700392sk_sp<SkFlattenable> DummyImageFilter::CreateProc(SkReadBuffer& buffer) {
senorblancob0e89dc2014-10-20 14:03:12 -0700393 SK_IMAGEFILTER_UNFLATTEN_COMMON(common, 0);
394 bool visited = buffer.readBool();
robertphillips43c2ad42016-04-04 05:05:11 -0700395 return DummyImageFilter::Make(visited);
senorblancob0e89dc2014-10-20 14:03:12 -0700396}
397
robertphillipsf3f5bad2014-12-19 13:49:15 -0800398#ifndef SK_IGNORE_TO_STRING
399void DummyImageFilter::toString(SkString* str) const {
400 str->appendf("DummyImageFilter: (");
401 str->append(")");
402}
403#endif
404
senorblancob0e89dc2014-10-20 14:03:12 -0700405};
406
407// Check that PDF rendering of image filters successfully falls back to
408// CPU rasterization.
409DEF_TEST(PDFImageFilter, reporter) {
halcanary3d32d502015-03-01 06:55:20 -0800410 SkDynamicMemoryWStream stream;
halcanary4b656662016-04-27 07:45:18 -0700411 sk_sp<SkDocument> doc(SkDocument::MakePDF(&stream));
halcanary3d32d502015-03-01 06:55:20 -0800412 SkCanvas* canvas = doc->beginPage(100.0f, 100.0f);
413
robertphillips43c2ad42016-04-04 05:05:11 -0700414 sk_sp<DummyImageFilter> filter(DummyImageFilter::Make());
senorblancob0e89dc2014-10-20 14:03:12 -0700415
416 // Filter just created; should be unvisited.
417 REPORTER_ASSERT(reporter, !filter->visited());
418 SkPaint paint;
419 paint.setImageFilter(filter.get());
halcanary3d32d502015-03-01 06:55:20 -0800420 canvas->drawRect(SkRect::MakeWH(100, 100), paint);
421 doc->close();
senorblancob0e89dc2014-10-20 14:03:12 -0700422
423 // Filter was used in rendering; should be visited.
424 REPORTER_ASSERT(reporter, filter->visited());
425}
halcanary66a82f32015-10-12 13:05:04 -0700426
427// Check that PDF rendering of image filters successfully falls back to
428// CPU rasterization.
429DEF_TEST(PDFFontCanEmbedTypeface, reporter) {
430 SkPDFCanon canon;
431
432 const char resource[] = "fonts/Roboto2-Regular_NoEmbed.ttf";
bungeman13b9c952016-05-12 10:09:30 -0700433 sk_sp<SkTypeface> noEmbedTypeface(MakeResourceAsTypeface(resource));
halcanary66a82f32015-10-12 13:05:04 -0700434 if (noEmbedTypeface) {
435 REPORTER_ASSERT(reporter,
halcanary8103a342016-03-08 15:10:16 -0800436 !SkPDFFont::CanEmbedTypeface(noEmbedTypeface.get(), &canon));
halcanary66a82f32015-10-12 13:05:04 -0700437 }
halcanary8103a342016-03-08 15:10:16 -0800438 sk_sp<SkTypeface> portableTypeface(
halcanary66a82f32015-10-12 13:05:04 -0700439 sk_tool_utils::create_portable_typeface(NULL, SkTypeface::kNormal));
440 REPORTER_ASSERT(reporter,
halcanary8103a342016-03-08 15:10:16 -0800441 SkPDFFont::CanEmbedTypeface(portableTypeface.get(), &canon));
halcanary66a82f32015-10-12 13:05:04 -0700442}
halcanary8e9f5e32016-02-24 15:46:46 -0800443
444
445// test to see that all finite scalars round trip via scanf().
446static void check_pdf_scalar_serialization(
447 skiatest::Reporter* reporter, float inputFloat) {
448 char floatString[SkPDFUtils::kMaximumFloatDecimalLength];
449 size_t len = SkPDFUtils::FloatToDecimal(inputFloat, floatString);
450 if (len >= sizeof(floatString)) {
451 ERRORF(reporter, "string too long: %u", (unsigned)len);
452 return;
453 }
454 if (floatString[len] != '\0' || strlen(floatString) != len) {
455 ERRORF(reporter, "terminator misplaced.");
456 return; // The terminator is needed for sscanf().
457 }
458 if (reporter->verbose()) {
459 SkDebugf("%15.9g = \"%s\"\n", inputFloat, floatString);
460 }
461 float roundTripFloat;
462 if (1 != sscanf(floatString, "%f", &roundTripFloat)) {
463 ERRORF(reporter, "unscannable result: %s", floatString);
464 return;
465 }
466 if (isfinite(inputFloat) && roundTripFloat != inputFloat) {
467 ERRORF(reporter, "roundTripFloat (%.9g) != inputFloat (%.9g)",
468 roundTripFloat, inputFloat);
469 }
470}
471
472// Test SkPDFUtils::AppendScalar for accuracy.
473DEF_TEST(PDFPrimitives_Scalar, reporter) {
474 SkRandom random(0x5EED);
475 int iterationCount = 512;
476 while (iterationCount-- > 0) {
477 union { uint32_t u; float f; };
478 u = random.nextU();
479 static_assert(sizeof(float) == sizeof(uint32_t), "");
480 check_pdf_scalar_serialization(reporter, f);
481 }
482 float alwaysCheck[] = {
483 0.0f, -0.0f, 1.0f, -1.0f, SK_ScalarPI, 0.1f, FLT_MIN, FLT_MAX,
484 -FLT_MIN, -FLT_MAX, FLT_MIN / 16.0f, -FLT_MIN / 16.0f,
485 SK_FloatNaN, SK_FloatInfinity, SK_FloatNegativeInfinity,
486 -FLT_MIN / 8388608.0
487 };
488 for (float inputFloat: alwaysCheck) {
489 check_pdf_scalar_serialization(reporter, inputFloat);
490 }
491}