blob: 57b0b60b8726ac05ab12deb1e802785ecf1f6351 [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"
24#include "SkStream.h"
vandebo@chromium.org421d6442011-07-20 17:39:01 +000025#include "SkTypes.h"
tfarina@chromium.org8f6884a2014-01-24 20:56:26 +000026#include "Test.h"
halcanary66a82f32015-10-12 13:05:04 -070027#include "sk_tool_utils.h"
vandebo@chromium.org8459d4e2010-09-24 22:25:30 +000028
commit-bot@chromium.org608ea652013-10-03 19:29:21 +000029#define DUMMY_TEXT "DCT compessed stream."
30
halcanaryf7a169e2015-05-01 07:35:45 -070031namespace {
32struct Catalog {
33 SkPDFSubstituteMap substitutes;
34 SkPDFObjNumMap numbers;
35};
36} // namespace
reed@google.com8a85d0c2011-06-24 19:12:12 +000037
halcanaryf7a169e2015-05-01 07:35:45 -070038template <typename T>
halcanary96fcdcc2015-08-27 07:41:13 -070039static SkString emit_to_string(T& obj, Catalog* catPtr = nullptr) {
halcanaryf7a169e2015-05-01 07:35:45 -070040 Catalog catalog;
halcanary6a144342015-01-23 11:45:10 -080041 SkDynamicMemoryWStream buffer;
halcanaryf7a169e2015-05-01 07:35:45 -070042 if (!catPtr) {
43 catPtr = &catalog;
halcanary130444f2015-04-25 06:45:07 -070044 }
halcanaryf7a169e2015-05-01 07:35:45 -070045 obj.emitObject(&buffer, catPtr->numbers, catPtr->substitutes);
46 SkAutoTDelete<SkStreamAsset> asset(buffer.detachAsStream());
47 SkString tmp(asset->getLength());
48 asset->read(tmp.writable_str(), asset->getLength());
49 return tmp;
vandebo@chromium.org8459d4e2010-09-24 22:25:30 +000050}
51
halcanaryf7a169e2015-05-01 07:35:45 -070052static bool eq(const SkString& str, const char* strPtr, size_t len) {
53 return len == str.size() && 0 == memcmp(str.c_str(), strPtr, len);
vandebo@chromium.org421d6442011-07-20 17:39:01 +000054}
55
halcanaryf7a169e2015-05-01 07:35:45 -070056#define ASSERT_EQL(REPORTER, SKSTRING, STRING, LEN) \
57 do { \
58 const char* strptr = STRING; \
59 const SkString& sks = SKSTRING; \
60 if (!eq(sks, strptr, LEN)) { \
61 REPORT_FAILURE( \
62 REPORTER, \
63 "", \
64 SkStringPrintf("'%s' != '%s'", strptr, sks.c_str())); \
65 } \
66 } while (false)
67
68#define ASSERT_EQ(REPORTER, SKSTRING, STRING) \
69 do { \
70 const char* str = STRING; \
71 ASSERT_EQL(REPORTER, SKSTRING, str, strlen(str)); \
72 } while (false)
73
74#define ASSERT_EMIT_EQ(REPORTER, OBJECT, STRING) \
75 do { \
76 SkString result = emit_to_string(OBJECT); \
77 ASSERT_EQ(REPORTER, result, STRING); \
78 } while (false)
79
80
81
vandebo@chromium.org421d6442011-07-20 17:39:01 +000082static void TestPDFStream(skiatest::Reporter* reporter) {
83 char streamBytes[] = "Test\nFoo\tBar";
scroggoa1193e42015-01-21 12:09:53 -080084 SkAutoTDelete<SkMemoryStream> streamData(new SkMemoryStream(
vandebo@chromium.orgd96d17b2013-01-04 19:31:24 +000085 streamBytes, strlen(streamBytes), true));
halcanary8103a342016-03-08 15:10:16 -080086 sk_sp<SkPDFStream> stream(new SkPDFStream(streamData.get()));
halcanaryf7a169e2015-05-01 07:35:45 -070087 ASSERT_EMIT_EQ(reporter,
88 *stream,
89 "<</Length 12>> stream\nTest\nFoo\tBar\nendstream");
90 stream->insertInt("Attribute", 42);
91 ASSERT_EMIT_EQ(reporter,
92 *stream,
93 "<</Length 12\n/Attribute 42>> stream\n"
94 "Test\nFoo\tBar\nendstream");
vandebo@chromium.org421d6442011-07-20 17:39:01 +000095
halcanary91d1d622015-02-17 14:43:06 -080096 {
vandebo@chromium.org421d6442011-07-20 17:39:01 +000097 char streamBytes2[] = "This is a longer string, so that compression "
98 "can do something with it. With shorter strings, "
99 "the short circuit logic cuts in and we end up "
100 "with an uncompressed string.";
101 SkAutoDataUnref streamData2(SkData::NewWithCopy(streamBytes2,
102 strlen(streamBytes2)));
halcanary8103a342016-03-08 15:10:16 -0800103 sk_sp<SkPDFStream> stream(new SkPDFStream(streamData2.get()));
vandebo@chromium.org421d6442011-07-20 17:39:01 +0000104
105 SkDynamicMemoryWStream compressedByteStream;
halcanary48305e82015-08-18 13:30:25 -0700106 SkDeflateWStream deflateWStream(&compressedByteStream);
107 deflateWStream.write(streamBytes2, strlen(streamBytes2));
108 deflateWStream.finalize();
vandebo@chromium.org421d6442011-07-20 17:39:01 +0000109
mtkleincabc08c2015-02-19 08:29:24 -0800110 SkDynamicMemoryWStream expected;
halcanary37c46ca2015-03-31 12:30:20 -0700111 expected.writeText("<</Filter /FlateDecode\n/Length 116>> stream\n");
halcanary48305e82015-08-18 13:30:25 -0700112 compressedByteStream.writeToStream(&expected);
113 compressedByteStream.reset();
mtkleincabc08c2015-02-19 08:29:24 -0800114 expected.writeText("\nendstream");
115 SkAutoDataUnref expectedResultData2(expected.copyToData());
halcanaryf7a169e2015-05-01 07:35:45 -0700116 SkString result = emit_to_string(*stream);
117 ASSERT_EQL(reporter,
118 result,
119 (const char*)expectedResultData2->data(),
120 expectedResultData2->size());
vandebo@chromium.org421d6442011-07-20 17:39:01 +0000121 }
122}
123
halcanaryf7a169e2015-05-01 07:35:45 -0700124static void TestObjectNumberMap(skiatest::Reporter* reporter) {
125 SkPDFObjNumMap objNumMap;
halcanary8103a342016-03-08 15:10:16 -0800126 sk_sp<SkPDFArray> a1(new SkPDFArray);
127 sk_sp<SkPDFArray> a2(new SkPDFArray);
128 sk_sp<SkPDFArray> a3(new SkPDFArray);
vandebo@chromium.org8459d4e2010-09-24 22:25:30 +0000129
halcanaryf7a169e2015-05-01 07:35:45 -0700130 objNumMap.addObject(a1.get());
131 objNumMap.addObject(a2.get());
132 objNumMap.addObject(a3.get());
vandebo@chromium.org8459d4e2010-09-24 22:25:30 +0000133
halcanaryf7a169e2015-05-01 07:35:45 -0700134 // The objects should be numbered in the order they are added,
135 // starting with 1.
136 REPORTER_ASSERT(reporter, objNumMap.getObjectNumber(a1.get()) == 1);
137 REPORTER_ASSERT(reporter, objNumMap.getObjectNumber(a2.get()) == 2);
138 REPORTER_ASSERT(reporter, objNumMap.getObjectNumber(a3.get()) == 3);
139 // Assert that repeated calls to get the object number return
140 // consistent result.
141 REPORTER_ASSERT(reporter, objNumMap.getObjectNumber(a1.get()) == 1);
vandebo@chromium.org8459d4e2010-09-24 22:25:30 +0000142}
143
144static void TestObjectRef(skiatest::Reporter* reporter) {
halcanary8103a342016-03-08 15:10:16 -0800145 sk_sp<SkPDFArray> a1(new SkPDFArray);
146 sk_sp<SkPDFArray> a2(new SkPDFArray);
147 a2->appendObjRef(a1);
vandebo@chromium.org8459d4e2010-09-24 22:25:30 +0000148
halcanaryf7a169e2015-05-01 07:35:45 -0700149 Catalog catalog;
150 catalog.numbers.addObject(a1.get());
151 REPORTER_ASSERT(reporter, catalog.numbers.getObjectNumber(a1.get()) == 1);
vandebo@chromium.org8459d4e2010-09-24 22:25:30 +0000152
halcanaryf7a169e2015-05-01 07:35:45 -0700153 SkString result = emit_to_string(*a2, &catalog);
154 // If appendObjRef misbehaves, then the result would
155 // be [[]], not [1 0 R].
156 ASSERT_EQ(reporter, result, "[1 0 R]");
vandebo@chromium.org8459d4e2010-09-24 22:25:30 +0000157}
158
vandebo@chromium.org2ef12d42011-07-06 23:31:24 +0000159static void TestSubstitute(skiatest::Reporter* reporter) {
halcanary8103a342016-03-08 15:10:16 -0800160 sk_sp<SkPDFDict> proxy(new SkPDFDict());
161 sk_sp<SkPDFDict> stub(new SkPDFDict());
vandebo@chromium.org2ef12d42011-07-06 23:31:24 +0000162
halcanaryf7a169e2015-05-01 07:35:45 -0700163 proxy->insertInt("Value", 33);
164 stub->insertInt("Value", 44);
vandebo@chromium.org2ef12d42011-07-06 23:31:24 +0000165
halcanary37c46ca2015-03-31 12:30:20 -0700166 SkPDFSubstituteMap substituteMap;
167 substituteMap.setSubstitute(proxy.get(), stub.get());
168 SkPDFObjNumMap catalog;
halcanaryf41061c2015-03-25 11:29:18 -0700169 catalog.addObject(proxy.get());
vandebo@chromium.org2ef12d42011-07-06 23:31:24 +0000170
halcanary8103a342016-03-08 15:10:16 -0800171 REPORTER_ASSERT(reporter, stub.get() == substituteMap.getSubstitute(proxy.get()));
172 REPORTER_ASSERT(reporter, proxy.get() != substituteMap.getSubstitute(stub.get()));
vandebo@chromium.org2ef12d42011-07-06 23:31:24 +0000173}
174
vandebo@chromium.org4e1cc6a2013-01-25 19:27:23 +0000175// This test used to assert without the fix submitted for
176// http://code.google.com/p/skia/issues/detail?id=1083.
177// SKP files might have invalid glyph ids. This test ensures they are ignored,
178// and there is no assert on input data in Debug mode.
sugoi@google.com54f0d1b2013-02-27 19:17:41 +0000179static void test_issue1083() {
halcanary3d32d502015-03-01 06:55:20 -0800180 SkDynamicMemoryWStream outStream;
halcanary8103a342016-03-08 15:10:16 -0800181 sk_sp<SkDocument> doc(SkDocument::CreatePDF(&outStream));
halcanary3d32d502015-03-01 06:55:20 -0800182 SkCanvas* canvas = doc->beginPage(100.0f, 100.0f);
vandebo@chromium.org4e1cc6a2013-01-25 19:27:23 +0000183 SkPaint paint;
184 paint.setTextEncoding(SkPaint::kGlyphID_TextEncoding);
185
186 uint16_t glyphID = 65000;
halcanary3d32d502015-03-01 06:55:20 -0800187 canvas->drawText(&glyphID, 2, 0, 0, paint);
vandebo@chromium.org4e1cc6a2013-01-25 19:27:23 +0000188
halcanary3d32d502015-03-01 06:55:20 -0800189 doc->close();
vandebo@chromium.org4e1cc6a2013-01-25 19:27:23 +0000190}
191
halcanaryf7a169e2015-05-01 07:35:45 -0700192static void TestPDFUnion(skiatest::Reporter* reporter) {
193 SkPDFUnion boolTrue = SkPDFUnion::Bool(true);
194 ASSERT_EMIT_EQ(reporter, boolTrue, "true");
vandebo@chromium.org8459d4e2010-09-24 22:25:30 +0000195
halcanaryf7a169e2015-05-01 07:35:45 -0700196 SkPDFUnion boolFalse = SkPDFUnion::Bool(false);
197 ASSERT_EMIT_EQ(reporter, boolFalse, "false");
vandebo@chromium.org8459d4e2010-09-24 22:25:30 +0000198
halcanaryf7a169e2015-05-01 07:35:45 -0700199 SkPDFUnion int42 = SkPDFUnion::Int(42);
200 ASSERT_EMIT_EQ(reporter, int42, "42");
201
202 SkPDFUnion realHalf = SkPDFUnion::Scalar(SK_ScalarHalf);
halcanary8e9f5e32016-02-24 15:46:46 -0800203 ASSERT_EMIT_EQ(reporter, realHalf, ".5");
halcanaryf7a169e2015-05-01 07:35:45 -0700204
205 SkPDFUnion bigScalar = SkPDFUnion::Scalar(110999.75f);
halcanaryf7a169e2015-05-01 07:35:45 -0700206 ASSERT_EMIT_EQ(reporter, bigScalar, "110999.75");
vandebo@chromium.org094316b2011-03-04 03:15:13 +0000207
halcanary8e9f5e32016-02-24 15:46:46 -0800208 SkPDFUnion biggerScalar = SkPDFUnion::Scalar(50000000.1f);
halcanaryf7a169e2015-05-01 07:35:45 -0700209 ASSERT_EMIT_EQ(reporter, biggerScalar, "50000000");
vandebo@chromium.org094316b2011-03-04 03:15:13 +0000210
halcanary8e9f5e32016-02-24 15:46:46 -0800211 SkPDFUnion smallestScalar = SkPDFUnion::Scalar(1.0f / 65536);
212 ASSERT_EMIT_EQ(reporter, smallestScalar, ".0000152587890");
vandebo@chromium.org094316b2011-03-04 03:15:13 +0000213
halcanaryf7a169e2015-05-01 07:35:45 -0700214 SkPDFUnion stringSimple = SkPDFUnion::String("test ) string ( foo");
215 ASSERT_EMIT_EQ(reporter, stringSimple, "(test \\) string \\( foo)");
vandebo@chromium.org8459d4e2010-09-24 22:25:30 +0000216
halcanaryf7a169e2015-05-01 07:35:45 -0700217 SkString stringComplexInput("\ttest ) string ( foo");
218 SkPDFUnion stringComplex = SkPDFUnion::String(stringComplexInput);
219 ASSERT_EMIT_EQ(reporter,
220 stringComplex,
221 "<0974657374202920737472696E67202820666F6F>");
vandebo@chromium.org8459d4e2010-09-24 22:25:30 +0000222
halcanaryf7a169e2015-05-01 07:35:45 -0700223 SkString nameInput("Test name\twith#tab");
224 SkPDFUnion name = SkPDFUnion::Name(nameInput);
225 ASSERT_EMIT_EQ(reporter, name, "/Test#20name#09with#23tab");
226
227 SkString nameInput2("A#/%()<>[]{}B");
228 SkPDFUnion name2 = SkPDFUnion::Name(nameInput2);
229 ASSERT_EMIT_EQ(reporter, name2, "/A#23#2F#25#28#29#3C#3E#5B#5D#7B#7DB");
230
231 SkPDFUnion name3 = SkPDFUnion::Name("SimpleNameWithOnlyPrintableASCII");
232 ASSERT_EMIT_EQ(reporter, name3, "/SimpleNameWithOnlyPrintableASCII");
vandebo@chromium.org251a7662012-09-21 17:50:50 +0000233
vandebo@chromium.orgc0376fe2012-03-05 18:44:33 +0000234 // Test that we correctly handle characters with the high-bit set.
halcanaryf7a169e2015-05-01 07:35:45 -0700235 SkString highBitString("\xDE\xAD" "be\xEF");
236 SkPDFUnion highBitName = SkPDFUnion::Name(highBitString);
237 ASSERT_EMIT_EQ(reporter, highBitName, "/#DE#ADbe#EF");
238}
vandebo@chromium.orgc0376fe2012-03-05 18:44:33 +0000239
halcanaryf7a169e2015-05-01 07:35:45 -0700240static void TestPDFArray(skiatest::Reporter* reporter) {
halcanary8103a342016-03-08 15:10:16 -0800241 sk_sp<SkPDFArray> array(new SkPDFArray);
halcanaryf7a169e2015-05-01 07:35:45 -0700242 ASSERT_EMIT_EQ(reporter, *array, "[]");
vandebo@chromium.org8459d4e2010-09-24 22:25:30 +0000243
halcanaryf7a169e2015-05-01 07:35:45 -0700244 array->appendInt(42);
245 ASSERT_EMIT_EQ(reporter, *array, "[42]");
246
247 array->appendScalar(SK_ScalarHalf);
halcanary8e9f5e32016-02-24 15:46:46 -0800248 ASSERT_EMIT_EQ(reporter, *array, "[42 .5]");
halcanaryf7a169e2015-05-01 07:35:45 -0700249
250 array->appendInt(0);
halcanary8e9f5e32016-02-24 15:46:46 -0800251 ASSERT_EMIT_EQ(reporter, *array, "[42 .5 0]");
halcanaryf7a169e2015-05-01 07:35:45 -0700252
253 array->appendBool(true);
halcanary8e9f5e32016-02-24 15:46:46 -0800254 ASSERT_EMIT_EQ(reporter, *array, "[42 .5 0 true]");
halcanaryf7a169e2015-05-01 07:35:45 -0700255
256 array->appendName("ThisName");
halcanary8e9f5e32016-02-24 15:46:46 -0800257 ASSERT_EMIT_EQ(reporter, *array, "[42 .5 0 true /ThisName]");
halcanaryf7a169e2015-05-01 07:35:45 -0700258
259 array->appendName(SkString("AnotherName"));
halcanary8e9f5e32016-02-24 15:46:46 -0800260 ASSERT_EMIT_EQ(reporter, *array, "[42 .5 0 true /ThisName /AnotherName]");
halcanaryf7a169e2015-05-01 07:35:45 -0700261
262 array->appendString("This String");
263 ASSERT_EMIT_EQ(reporter, *array,
halcanary8e9f5e32016-02-24 15:46:46 -0800264 "[42 .5 0 true /ThisName /AnotherName (This String)]");
halcanaryf7a169e2015-05-01 07:35:45 -0700265
266 array->appendString(SkString("Another String"));
267 ASSERT_EMIT_EQ(reporter, *array,
halcanary8e9f5e32016-02-24 15:46:46 -0800268 "[42 .5 0 true /ThisName /AnotherName (This String) "
halcanaryf7a169e2015-05-01 07:35:45 -0700269 "(Another String)]");
270
halcanary8103a342016-03-08 15:10:16 -0800271 sk_sp<SkPDFArray> innerArray(new SkPDFArray);
halcanaryf7a169e2015-05-01 07:35:45 -0700272 innerArray->appendInt(-1);
halcanary8103a342016-03-08 15:10:16 -0800273 array->appendObject(std::move(innerArray));
halcanaryf7a169e2015-05-01 07:35:45 -0700274 ASSERT_EMIT_EQ(reporter, *array,
halcanary8e9f5e32016-02-24 15:46:46 -0800275 "[42 .5 0 true /ThisName /AnotherName (This String) "
halcanaryf7a169e2015-05-01 07:35:45 -0700276 "(Another String) [-1]]");
277
halcanary8103a342016-03-08 15:10:16 -0800278 sk_sp<SkPDFArray> referencedArray(new SkPDFArray);
halcanaryf7a169e2015-05-01 07:35:45 -0700279 Catalog catalog;
280 catalog.numbers.addObject(referencedArray.get());
281 REPORTER_ASSERT(reporter, catalog.numbers.getObjectNumber(
282 referencedArray.get()) == 1);
halcanary8103a342016-03-08 15:10:16 -0800283 array->appendObjRef(std::move(referencedArray));
halcanaryf7a169e2015-05-01 07:35:45 -0700284
285 SkString result = emit_to_string(*array, &catalog);
286 ASSERT_EQ(reporter, result,
halcanary8e9f5e32016-02-24 15:46:46 -0800287 "[42 .5 0 true /ThisName /AnotherName (This String) "
halcanaryf7a169e2015-05-01 07:35:45 -0700288 "(Another String) [-1] 1 0 R]");
289}
290
291static void TestPDFDict(skiatest::Reporter* reporter) {
halcanary8103a342016-03-08 15:10:16 -0800292 sk_sp<SkPDFDict> dict(new SkPDFDict);
halcanaryf7a169e2015-05-01 07:35:45 -0700293 ASSERT_EMIT_EQ(reporter, *dict, "<<>>");
294
295 dict->insertInt("n1", SkToSizeT(42));
296 ASSERT_EMIT_EQ(reporter, *dict, "<</n1 42>>");
297
298 dict.reset(new SkPDFDict);
299 ASSERT_EMIT_EQ(reporter, *dict, "<<>>");
300
301 dict->insertInt("n1", 42);
302 ASSERT_EMIT_EQ(reporter, *dict, "<</n1 42>>");
303
304 dict->insertScalar("n2", SK_ScalarHalf);
305
halcanary130444f2015-04-25 06:45:07 -0700306 SkString n3("n3");
halcanary8103a342016-03-08 15:10:16 -0800307 sk_sp<SkPDFArray> innerArray(new SkPDFArray);
halcanaryf7a169e2015-05-01 07:35:45 -0700308 innerArray->appendInt(-100);
halcanary8103a342016-03-08 15:10:16 -0800309 dict->insertObject(n3, std::move(innerArray));
halcanary8e9f5e32016-02-24 15:46:46 -0800310 ASSERT_EMIT_EQ(reporter, *dict, "<</n1 42\n/n2 .5\n/n3 [-100]>>");
vandebo@chromium.org8459d4e2010-09-24 22:25:30 +0000311
halcanaryf7a169e2015-05-01 07:35:45 -0700312 dict.reset(new SkPDFDict);
313 ASSERT_EMIT_EQ(reporter, *dict, "<<>>");
314
315 dict->insertInt("n1", 24);
316 ASSERT_EMIT_EQ(reporter, *dict, "<</n1 24>>");
317
318 dict->insertInt("n2", SkToSizeT(99));
319 ASSERT_EMIT_EQ(reporter, *dict, "<</n1 24\n/n2 99>>");
320
321 dict->insertScalar("n3", SK_ScalarHalf);
halcanary8e9f5e32016-02-24 15:46:46 -0800322 ASSERT_EMIT_EQ(reporter, *dict, "<</n1 24\n/n2 99\n/n3 .5>>");
halcanaryf7a169e2015-05-01 07:35:45 -0700323
324 dict->insertName("n4", "AName");
halcanary8e9f5e32016-02-24 15:46:46 -0800325 ASSERT_EMIT_EQ(reporter, *dict, "<</n1 24\n/n2 99\n/n3 .5\n/n4 /AName>>");
halcanaryf7a169e2015-05-01 07:35:45 -0700326
327 dict->insertName("n5", SkString("AnotherName"));
halcanary8e9f5e32016-02-24 15:46:46 -0800328 ASSERT_EMIT_EQ(reporter, *dict, "<</n1 24\n/n2 99\n/n3 .5\n/n4 /AName\n"
halcanaryf7a169e2015-05-01 07:35:45 -0700329 "/n5 /AnotherName>>");
330
331 dict->insertString("n6", "A String");
halcanary8e9f5e32016-02-24 15:46:46 -0800332 ASSERT_EMIT_EQ(reporter, *dict, "<</n1 24\n/n2 99\n/n3 .5\n/n4 /AName\n"
halcanaryf7a169e2015-05-01 07:35:45 -0700333 "/n5 /AnotherName\n/n6 (A String)>>");
334
335 dict->insertString("n7", SkString("Another String"));
halcanary8e9f5e32016-02-24 15:46:46 -0800336 ASSERT_EMIT_EQ(reporter, *dict, "<</n1 24\n/n2 99\n/n3 .5\n/n4 /AName\n"
halcanaryf7a169e2015-05-01 07:35:45 -0700337 "/n5 /AnotherName\n/n6 (A String)\n/n7 (Another String)>>");
338
339 dict.reset(new SkPDFDict("DType"));
340 ASSERT_EMIT_EQ(reporter, *dict, "<</Type /DType>>");
halcanary8e9f5e32016-02-24 15:46:46 -0800341
halcanary8103a342016-03-08 15:10:16 -0800342 sk_sp<SkPDFArray> referencedArray(new SkPDFArray);
halcanaryf7a169e2015-05-01 07:35:45 -0700343 Catalog catalog;
344 catalog.numbers.addObject(referencedArray.get());
345 REPORTER_ASSERT(reporter, catalog.numbers.getObjectNumber(
346 referencedArray.get()) == 1);
halcanary8103a342016-03-08 15:10:16 -0800347 dict->insertObjRef("n1", std::move(referencedArray));
halcanaryf7a169e2015-05-01 07:35:45 -0700348 SkString result = emit_to_string(*dict, &catalog);
349 ASSERT_EQ(reporter, result, "<</Type /DType\n/n1 1 0 R>>");
350}
351
352DEF_TEST(PDFPrimitives, reporter) {
353 TestPDFUnion(reporter);
354 TestPDFArray(reporter);
355 TestPDFDict(reporter);
vandebo@chromium.org421d6442011-07-20 17:39:01 +0000356 TestPDFStream(reporter);
halcanaryf7a169e2015-05-01 07:35:45 -0700357 TestObjectNumberMap(reporter);
vandebo@chromium.org8459d4e2010-09-24 22:25:30 +0000358 TestObjectRef(reporter);
vandebo@chromium.org2ef12d42011-07-06 23:31:24 +0000359 TestSubstitute(reporter);
sugoi@google.com54f0d1b2013-02-27 19:17:41 +0000360 test_issue1083();
vandebo@chromium.org8459d4e2010-09-24 22:25:30 +0000361}
senorblancob0e89dc2014-10-20 14:03:12 -0700362
363namespace {
364
365class DummyImageFilter : public SkImageFilter {
366public:
halcanary96fcdcc2015-08-27 07:41:13 -0700367 DummyImageFilter(bool visited = false) : SkImageFilter(0, nullptr), fVisited(visited) {}
mtklein36352bf2015-03-25 18:17:31 -0700368 ~DummyImageFilter() override {}
robertphillips48e78462016-02-17 13:57:16 -0800369 bool onFilterImageDeprecated(Proxy*, const SkBitmap& src, const Context&,
370 SkBitmap* result, SkIPoint* offset) const override {
senorblancob0e89dc2014-10-20 14:03:12 -0700371 fVisited = true;
372 offset->fX = offset->fY = 0;
373 *result = src;
374 return true;
375 }
robertphillipsf3f5bad2014-12-19 13:49:15 -0800376 SK_TO_STRING_OVERRIDE()
senorblancob0e89dc2014-10-20 14:03:12 -0700377 SK_DECLARE_PUBLIC_FLATTENABLE_DESERIALIZATION_PROCS(DummyImageFilter)
senorblancob0e89dc2014-10-20 14:03:12 -0700378 bool visited() const { return fVisited; }
379
380private:
381 mutable bool fVisited;
382};
383
384SkFlattenable* DummyImageFilter::CreateProc(SkReadBuffer& buffer) {
385 SK_IMAGEFILTER_UNFLATTEN_COMMON(common, 0);
386 bool visited = buffer.readBool();
halcanary385fe4d2015-08-26 13:07:48 -0700387 return new DummyImageFilter(visited);
senorblancob0e89dc2014-10-20 14:03:12 -0700388}
389
robertphillipsf3f5bad2014-12-19 13:49:15 -0800390#ifndef SK_IGNORE_TO_STRING
391void DummyImageFilter::toString(SkString* str) const {
392 str->appendf("DummyImageFilter: (");
393 str->append(")");
394}
395#endif
396
senorblancob0e89dc2014-10-20 14:03:12 -0700397};
398
399// Check that PDF rendering of image filters successfully falls back to
400// CPU rasterization.
401DEF_TEST(PDFImageFilter, reporter) {
halcanary3d32d502015-03-01 06:55:20 -0800402 SkDynamicMemoryWStream stream;
halcanary8103a342016-03-08 15:10:16 -0800403 sk_sp<SkDocument> doc(SkDocument::CreatePDF(&stream));
halcanary3d32d502015-03-01 06:55:20 -0800404 SkCanvas* canvas = doc->beginPage(100.0f, 100.0f);
405
halcanary8103a342016-03-08 15:10:16 -0800406 sk_sp<DummyImageFilter> filter(new DummyImageFilter());
senorblancob0e89dc2014-10-20 14:03:12 -0700407
408 // Filter just created; should be unvisited.
409 REPORTER_ASSERT(reporter, !filter->visited());
410 SkPaint paint;
411 paint.setImageFilter(filter.get());
halcanary3d32d502015-03-01 06:55:20 -0800412 canvas->drawRect(SkRect::MakeWH(100, 100), paint);
413 doc->close();
senorblancob0e89dc2014-10-20 14:03:12 -0700414
415 // Filter was used in rendering; should be visited.
416 REPORTER_ASSERT(reporter, filter->visited());
417}
halcanary66a82f32015-10-12 13:05:04 -0700418
419// Check that PDF rendering of image filters successfully falls back to
420// CPU rasterization.
421DEF_TEST(PDFFontCanEmbedTypeface, reporter) {
422 SkPDFCanon canon;
423
424 const char resource[] = "fonts/Roboto2-Regular_NoEmbed.ttf";
halcanary8103a342016-03-08 15:10:16 -0800425 sk_sp<SkTypeface> noEmbedTypeface(GetResourceAsTypeface(resource));
halcanary66a82f32015-10-12 13:05:04 -0700426 if (noEmbedTypeface) {
427 REPORTER_ASSERT(reporter,
halcanary8103a342016-03-08 15:10:16 -0800428 !SkPDFFont::CanEmbedTypeface(noEmbedTypeface.get(), &canon));
halcanary66a82f32015-10-12 13:05:04 -0700429 }
halcanary8103a342016-03-08 15:10:16 -0800430 sk_sp<SkTypeface> portableTypeface(
halcanary66a82f32015-10-12 13:05:04 -0700431 sk_tool_utils::create_portable_typeface(NULL, SkTypeface::kNormal));
432 REPORTER_ASSERT(reporter,
halcanary8103a342016-03-08 15:10:16 -0800433 SkPDFFont::CanEmbedTypeface(portableTypeface.get(), &canon));
halcanary66a82f32015-10-12 13:05:04 -0700434}
halcanary8e9f5e32016-02-24 15:46:46 -0800435
436
437// test to see that all finite scalars round trip via scanf().
438static void check_pdf_scalar_serialization(
439 skiatest::Reporter* reporter, float inputFloat) {
440 char floatString[SkPDFUtils::kMaximumFloatDecimalLength];
441 size_t len = SkPDFUtils::FloatToDecimal(inputFloat, floatString);
442 if (len >= sizeof(floatString)) {
443 ERRORF(reporter, "string too long: %u", (unsigned)len);
444 return;
445 }
446 if (floatString[len] != '\0' || strlen(floatString) != len) {
447 ERRORF(reporter, "terminator misplaced.");
448 return; // The terminator is needed for sscanf().
449 }
450 if (reporter->verbose()) {
451 SkDebugf("%15.9g = \"%s\"\n", inputFloat, floatString);
452 }
453 float roundTripFloat;
454 if (1 != sscanf(floatString, "%f", &roundTripFloat)) {
455 ERRORF(reporter, "unscannable result: %s", floatString);
456 return;
457 }
458 if (isfinite(inputFloat) && roundTripFloat != inputFloat) {
459 ERRORF(reporter, "roundTripFloat (%.9g) != inputFloat (%.9g)",
460 roundTripFloat, inputFloat);
461 }
462}
463
464// Test SkPDFUtils::AppendScalar for accuracy.
465DEF_TEST(PDFPrimitives_Scalar, reporter) {
466 SkRandom random(0x5EED);
467 int iterationCount = 512;
468 while (iterationCount-- > 0) {
469 union { uint32_t u; float f; };
470 u = random.nextU();
471 static_assert(sizeof(float) == sizeof(uint32_t), "");
472 check_pdf_scalar_serialization(reporter, f);
473 }
474 float alwaysCheck[] = {
475 0.0f, -0.0f, 1.0f, -1.0f, SK_ScalarPI, 0.1f, FLT_MIN, FLT_MAX,
476 -FLT_MIN, -FLT_MAX, FLT_MIN / 16.0f, -FLT_MIN / 16.0f,
477 SK_FloatNaN, SK_FloatInfinity, SK_FloatNegativeInfinity,
478 -FLT_MIN / 8388608.0
479 };
480 for (float inputFloat: alwaysCheck) {
481 check_pdf_scalar_serialization(reporter, inputFloat);
482 }
483}