blob: 766bbefec29e419cd5d85790c808ef473c05a229 [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
Hal Canary43fb7a02016-12-30 13:09:03 -05008#include "Test.h"
9
10#ifdef SK_SUPPORT_PDF
11
halcanary66a82f32015-10-12 13:05:04 -070012#include "Resources.h"
edisonn@google.comd9dfa182013-04-24 13:01:01 +000013#include "SkBitmap.h"
vandebo@chromium.org4e1cc6a2013-01-25 19:27:23 +000014#include "SkCanvas.h"
reed@google.com8a85d0c2011-06-24 19:12:12 +000015#include "SkData.h"
halcanary3d32d502015-03-01 06:55:20 -080016#include "SkDocument.h"
halcanaryd9e57152015-08-12 11:24:40 -070017#include "SkDeflate.h"
edisonn@google.comd9dfa182013-04-24 13:01:01 +000018#include "SkImageEncoder.h"
halcanary022c2bd2016-09-02 11:29:46 -070019#include "SkMakeUnique.h"
edisonn@google.comd9dfa182013-04-24 13:01:01 +000020#include "SkMatrix.h"
halcanarya1f1ee92015-02-20 06:17:26 -080021#include "SkPDFCanon.h"
vandebo@chromium.org4e1cc6a2013-01-25 19:27:23 +000022#include "SkPDFDevice.h"
halcanary66a82f32015-10-12 13:05:04 -070023#include "SkPDFFont.h"
vandebo@chromium.org8459d4e2010-09-24 22:25:30 +000024#include "SkPDFTypes.h"
halcanary8e9f5e32016-02-24 15:46:46 -080025#include "SkPDFUtils.h"
senorblancob0e89dc2014-10-20 14:03:12 -070026#include "SkReadBuffer.h"
vandebo@chromium.org8459d4e2010-09-24 22:25:30 +000027#include "SkScalar.h"
robertphillips4ba94e22016-04-04 12:07:47 -070028#include "SkSpecialImage.h"
vandebo@chromium.org8459d4e2010-09-24 22:25:30 +000029#include "SkStream.h"
vandebo@chromium.org421d6442011-07-20 17:39:01 +000030#include "SkTypes.h"
halcanary66a82f32015-10-12 13:05:04 -070031#include "sk_tool_utils.h"
vandebo@chromium.org8459d4e2010-09-24 22:25:30 +000032
halcanary56586b62016-07-25 13:59:30 -070033#include <cstdlib>
34#include <cmath>
35
commit-bot@chromium.org608ea652013-10-03 19:29:21 +000036#define DUMMY_TEXT "DCT compessed stream."
37
halcanaryf7a169e2015-05-01 07:35:45 -070038template <typename T>
halcanary530032a2016-08-18 14:22:52 -070039static SkString emit_to_string(T& obj, SkPDFObjNumMap* catPtr = nullptr) {
40 SkPDFObjNumMap 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 }
halcanary530032a2016-08-18 14:22:52 -070045 obj.emitObject(&buffer, *catPtr);
halcanary56586b62016-07-25 13:59:30 -070046 SkString tmp(buffer.bytesWritten());
47 buffer.copyTo(tmp.writable_str());
halcanaryf7a169e2015-05-01 07:35:45 -070048 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
halcanary56586b62016-07-25 13:59:30 -070055static void assert_eql(skiatest::Reporter* reporter,
56 const SkString& skString,
57 const char* str,
58 size_t len) {
59 if (!eq(skString, str, len)) {
60 REPORT_FAILURE(reporter, "", SkStringPrintf(
61 "'%*s' != '%s'", len, str, skString.c_str()));
62 }
63}
halcanaryf7a169e2015-05-01 07:35:45 -070064
halcanary56586b62016-07-25 13:59:30 -070065static void assert_eq(skiatest::Reporter* reporter,
66 const SkString& skString,
67 const char* str) {
68 assert_eql(reporter, skString, str, strlen(str));
69}
halcanaryf7a169e2015-05-01 07:35:45 -070070
71
halcanary56586b62016-07-25 13:59:30 -070072template <typename T>
73static void assert_emit_eq(skiatest::Reporter* reporter,
74 T& object,
75 const char* string) {
76 SkString result = emit_to_string(object);
77 assert_eq(reporter, result, string);
78}
halcanaryf7a169e2015-05-01 07:35:45 -070079
vandebo@chromium.org421d6442011-07-20 17:39:01 +000080static void TestPDFStream(skiatest::Reporter* reporter) {
81 char streamBytes[] = "Test\nFoo\tBar";
halcanary022c2bd2016-09-02 11:29:46 -070082 auto streamData = skstd::make_unique<SkMemoryStream>(
83 streamBytes, strlen(streamBytes), true);
halcanaryac0e00d2016-07-27 11:12:23 -070084 auto stream = sk_make_sp<SkPDFStream>(std::move(streamData));
halcanary56586b62016-07-25 13:59:30 -070085 assert_emit_eq(reporter,
halcanaryf7a169e2015-05-01 07:35:45 -070086 *stream,
87 "<</Length 12>> stream\nTest\nFoo\tBar\nendstream");
halcanaryfa251062016-07-29 10:13:18 -070088 stream->dict()->insertInt("Attribute", 42);
halcanary56586b62016-07-25 13:59:30 -070089 assert_emit_eq(reporter,
halcanaryf7a169e2015-05-01 07:35:45 -070090 *stream,
91 "<</Length 12\n/Attribute 42>> stream\n"
92 "Test\nFoo\tBar\nendstream");
vandebo@chromium.org421d6442011-07-20 17:39:01 +000093
halcanary91d1d622015-02-17 14:43:06 -080094 {
vandebo@chromium.org421d6442011-07-20 17:39:01 +000095 char streamBytes2[] = "This is a longer string, so that compression "
96 "can do something with it. With shorter strings, "
97 "the short circuit logic cuts in and we end up "
98 "with an uncompressed string.";
halcanaryfe8f0e02016-07-27 14:14:04 -070099 auto stream = sk_make_sp<SkPDFStream>(
100 SkData::MakeWithCopy(streamBytes2, strlen(streamBytes2)));
vandebo@chromium.org421d6442011-07-20 17:39:01 +0000101
102 SkDynamicMemoryWStream compressedByteStream;
halcanary48305e82015-08-18 13:30:25 -0700103 SkDeflateWStream deflateWStream(&compressedByteStream);
104 deflateWStream.write(streamBytes2, strlen(streamBytes2));
105 deflateWStream.finalize();
vandebo@chromium.org421d6442011-07-20 17:39:01 +0000106
mtkleincabc08c2015-02-19 08:29:24 -0800107 SkDynamicMemoryWStream expected;
halcanary37c46ca2015-03-31 12:30:20 -0700108 expected.writeText("<</Filter /FlateDecode\n/Length 116>> stream\n");
halcanary48305e82015-08-18 13:30:25 -0700109 compressedByteStream.writeToStream(&expected);
110 compressedByteStream.reset();
mtkleincabc08c2015-02-19 08:29:24 -0800111 expected.writeText("\nendstream");
reed42943c82016-09-12 12:01:44 -0700112 sk_sp<SkData> expectedResultData2(expected.detachAsData());
halcanaryf7a169e2015-05-01 07:35:45 -0700113 SkString result = emit_to_string(*stream);
halcanary18e0cbc2016-08-03 14:17:53 -0700114 #ifndef SK_PDF_LESS_COMPRESSION
halcanary56586b62016-07-25 13:59:30 -0700115 assert_eql(reporter,
halcanaryf7a169e2015-05-01 07:35:45 -0700116 result,
117 (const char*)expectedResultData2->data(),
118 expectedResultData2->size());
halcanary18e0cbc2016-08-03 14:17:53 -0700119 #endif
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;
halcanary8103a342016-03-08 15:10:16 -0800125 sk_sp<SkPDFArray> a1(new SkPDFArray);
126 sk_sp<SkPDFArray> a2(new SkPDFArray);
127 sk_sp<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) {
halcanary8103a342016-03-08 15:10:16 -0800144 sk_sp<SkPDFArray> a1(new SkPDFArray);
145 sk_sp<SkPDFArray> a2(new SkPDFArray);
146 a2->appendObjRef(a1);
vandebo@chromium.org8459d4e2010-09-24 22:25:30 +0000147
halcanary530032a2016-08-18 14:22:52 -0700148 SkPDFObjNumMap catalog;
149 catalog.addObject(a1.get());
150 REPORTER_ASSERT(reporter, catalog.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].
halcanary56586b62016-07-25 13:59:30 -0700155 assert_eq(reporter, result, "[1 0 R]");
vandebo@chromium.org8459d4e2010-09-24 22:25:30 +0000156}
157
vandebo@chromium.org4e1cc6a2013-01-25 19:27:23 +0000158// This test used to assert without the fix submitted for
159// http://code.google.com/p/skia/issues/detail?id=1083.
160// SKP files might have invalid glyph ids. This test ensures they are ignored,
161// and there is no assert on input data in Debug mode.
sugoi@google.com54f0d1b2013-02-27 19:17:41 +0000162static void test_issue1083() {
halcanary3d32d502015-03-01 06:55:20 -0800163 SkDynamicMemoryWStream outStream;
halcanary4b656662016-04-27 07:45:18 -0700164 sk_sp<SkDocument> doc(SkDocument::MakePDF(&outStream));
halcanary3d32d502015-03-01 06:55:20 -0800165 SkCanvas* canvas = doc->beginPage(100.0f, 100.0f);
vandebo@chromium.org4e1cc6a2013-01-25 19:27:23 +0000166 SkPaint paint;
167 paint.setTextEncoding(SkPaint::kGlyphID_TextEncoding);
168
169 uint16_t glyphID = 65000;
halcanary3d32d502015-03-01 06:55:20 -0800170 canvas->drawText(&glyphID, 2, 0, 0, paint);
vandebo@chromium.org4e1cc6a2013-01-25 19:27:23 +0000171
halcanary3d32d502015-03-01 06:55:20 -0800172 doc->close();
vandebo@chromium.org4e1cc6a2013-01-25 19:27:23 +0000173}
174
halcanary56586b62016-07-25 13:59:30 -0700175static void assert_emit_eq_number(skiatest::Reporter* reporter, float number) {
176 SkPDFUnion pdfUnion = SkPDFUnion::Scalar(number);
177 SkString result = emit_to_string(pdfUnion);
178 float value = static_cast<float>(std::atof(result.c_str()));
179 if (value != number) {
180 ERRORF(reporter, "%.9g != %s", number, result.c_str());
181 }
182}
183
184
halcanaryf7a169e2015-05-01 07:35:45 -0700185static void TestPDFUnion(skiatest::Reporter* reporter) {
186 SkPDFUnion boolTrue = SkPDFUnion::Bool(true);
halcanary56586b62016-07-25 13:59:30 -0700187 assert_emit_eq(reporter, boolTrue, "true");
vandebo@chromium.org8459d4e2010-09-24 22:25:30 +0000188
halcanaryf7a169e2015-05-01 07:35:45 -0700189 SkPDFUnion boolFalse = SkPDFUnion::Bool(false);
halcanary56586b62016-07-25 13:59:30 -0700190 assert_emit_eq(reporter, boolFalse, "false");
vandebo@chromium.org8459d4e2010-09-24 22:25:30 +0000191
halcanaryf7a169e2015-05-01 07:35:45 -0700192 SkPDFUnion int42 = SkPDFUnion::Int(42);
halcanary56586b62016-07-25 13:59:30 -0700193 assert_emit_eq(reporter, int42, "42");
halcanaryf7a169e2015-05-01 07:35:45 -0700194
halcanary56586b62016-07-25 13:59:30 -0700195 assert_emit_eq_number(reporter, SK_ScalarHalf);
196 assert_emit_eq_number(reporter, 110999.75f); // bigScalar
197 assert_emit_eq_number(reporter, 50000000.1f); // biggerScalar
198 assert_emit_eq_number(reporter, 1.0f / 65536); // smallScalar
vandebo@chromium.org094316b2011-03-04 03:15:13 +0000199
halcanaryf7a169e2015-05-01 07:35:45 -0700200 SkPDFUnion stringSimple = SkPDFUnion::String("test ) string ( foo");
halcanary56586b62016-07-25 13:59:30 -0700201 assert_emit_eq(reporter, stringSimple, "(test \\) string \\( foo)");
vandebo@chromium.org8459d4e2010-09-24 22:25:30 +0000202
halcanaryf7a169e2015-05-01 07:35:45 -0700203 SkString stringComplexInput("\ttest ) string ( foo");
204 SkPDFUnion stringComplex = SkPDFUnion::String(stringComplexInput);
halcanary56586b62016-07-25 13:59:30 -0700205 assert_emit_eq(reporter, stringComplex, "(\\011test \\) string \\( foo)");
halcanaryee41b752016-06-23 14:08:11 -0700206
207 SkString binaryStringInput("\1\2\3\4\5\6\7\10\11\12\13\14\15\16\17\20");
208 SkPDFUnion binaryString = SkPDFUnion::String(binaryStringInput);
halcanary56586b62016-07-25 13:59:30 -0700209 assert_emit_eq(reporter, binaryString, "<0102030405060708090A0B0C0D0E0F10>");
vandebo@chromium.org8459d4e2010-09-24 22:25:30 +0000210
halcanaryf7a169e2015-05-01 07:35:45 -0700211 SkString nameInput("Test name\twith#tab");
212 SkPDFUnion name = SkPDFUnion::Name(nameInput);
halcanary56586b62016-07-25 13:59:30 -0700213 assert_emit_eq(reporter, name, "/Test#20name#09with#23tab");
halcanaryf7a169e2015-05-01 07:35:45 -0700214
215 SkString nameInput2("A#/%()<>[]{}B");
216 SkPDFUnion name2 = SkPDFUnion::Name(nameInput2);
halcanary56586b62016-07-25 13:59:30 -0700217 assert_emit_eq(reporter, name2, "/A#23#2F#25#28#29#3C#3E#5B#5D#7B#7DB");
halcanaryf7a169e2015-05-01 07:35:45 -0700218
219 SkPDFUnion name3 = SkPDFUnion::Name("SimpleNameWithOnlyPrintableASCII");
halcanary56586b62016-07-25 13:59:30 -0700220 assert_emit_eq(reporter, name3, "/SimpleNameWithOnlyPrintableASCII");
vandebo@chromium.org251a7662012-09-21 17:50:50 +0000221
vandebo@chromium.orgc0376fe2012-03-05 18:44:33 +0000222 // Test that we correctly handle characters with the high-bit set.
halcanaryf7a169e2015-05-01 07:35:45 -0700223 SkString highBitString("\xDE\xAD" "be\xEF");
224 SkPDFUnion highBitName = SkPDFUnion::Name(highBitString);
halcanary56586b62016-07-25 13:59:30 -0700225 assert_emit_eq(reporter, highBitName, "/#DE#ADbe#EF");
halcanaryf7a169e2015-05-01 07:35:45 -0700226}
vandebo@chromium.orgc0376fe2012-03-05 18:44:33 +0000227
halcanaryf7a169e2015-05-01 07:35:45 -0700228static void TestPDFArray(skiatest::Reporter* reporter) {
halcanary8103a342016-03-08 15:10:16 -0800229 sk_sp<SkPDFArray> array(new SkPDFArray);
halcanary56586b62016-07-25 13:59:30 -0700230 assert_emit_eq(reporter, *array, "[]");
vandebo@chromium.org8459d4e2010-09-24 22:25:30 +0000231
halcanaryf7a169e2015-05-01 07:35:45 -0700232 array->appendInt(42);
halcanary56586b62016-07-25 13:59:30 -0700233 assert_emit_eq(reporter, *array, "[42]");
halcanaryf7a169e2015-05-01 07:35:45 -0700234
235 array->appendScalar(SK_ScalarHalf);
halcanary56586b62016-07-25 13:59:30 -0700236 assert_emit_eq(reporter, *array, "[42 .5]");
halcanaryf7a169e2015-05-01 07:35:45 -0700237
238 array->appendInt(0);
halcanary56586b62016-07-25 13:59:30 -0700239 assert_emit_eq(reporter, *array, "[42 .5 0]");
halcanaryf7a169e2015-05-01 07:35:45 -0700240
241 array->appendBool(true);
halcanary56586b62016-07-25 13:59:30 -0700242 assert_emit_eq(reporter, *array, "[42 .5 0 true]");
halcanaryf7a169e2015-05-01 07:35:45 -0700243
244 array->appendName("ThisName");
halcanary56586b62016-07-25 13:59:30 -0700245 assert_emit_eq(reporter, *array, "[42 .5 0 true /ThisName]");
halcanaryf7a169e2015-05-01 07:35:45 -0700246
247 array->appendName(SkString("AnotherName"));
halcanary56586b62016-07-25 13:59:30 -0700248 assert_emit_eq(reporter, *array, "[42 .5 0 true /ThisName /AnotherName]");
halcanaryf7a169e2015-05-01 07:35:45 -0700249
250 array->appendString("This String");
halcanary56586b62016-07-25 13:59:30 -0700251 assert_emit_eq(reporter, *array,
halcanary8e9f5e32016-02-24 15:46:46 -0800252 "[42 .5 0 true /ThisName /AnotherName (This String)]");
halcanaryf7a169e2015-05-01 07:35:45 -0700253
254 array->appendString(SkString("Another String"));
halcanary56586b62016-07-25 13:59:30 -0700255 assert_emit_eq(reporter, *array,
halcanary8e9f5e32016-02-24 15:46:46 -0800256 "[42 .5 0 true /ThisName /AnotherName (This String) "
halcanaryf7a169e2015-05-01 07:35:45 -0700257 "(Another String)]");
258
halcanary8103a342016-03-08 15:10:16 -0800259 sk_sp<SkPDFArray> innerArray(new SkPDFArray);
halcanaryf7a169e2015-05-01 07:35:45 -0700260 innerArray->appendInt(-1);
halcanary8103a342016-03-08 15:10:16 -0800261 array->appendObject(std::move(innerArray));
halcanary56586b62016-07-25 13:59:30 -0700262 assert_emit_eq(reporter, *array,
halcanary8e9f5e32016-02-24 15:46:46 -0800263 "[42 .5 0 true /ThisName /AnotherName (This String) "
halcanaryf7a169e2015-05-01 07:35:45 -0700264 "(Another String) [-1]]");
265
halcanary8103a342016-03-08 15:10:16 -0800266 sk_sp<SkPDFArray> referencedArray(new SkPDFArray);
halcanary530032a2016-08-18 14:22:52 -0700267 SkPDFObjNumMap catalog;
268 catalog.addObject(referencedArray.get());
269 REPORTER_ASSERT(reporter, catalog.getObjectNumber(
halcanaryf7a169e2015-05-01 07:35:45 -0700270 referencedArray.get()) == 1);
halcanary8103a342016-03-08 15:10:16 -0800271 array->appendObjRef(std::move(referencedArray));
halcanaryf7a169e2015-05-01 07:35:45 -0700272
273 SkString result = emit_to_string(*array, &catalog);
halcanary56586b62016-07-25 13:59:30 -0700274 assert_eq(reporter, result,
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] 1 0 R]");
277}
278
279static void TestPDFDict(skiatest::Reporter* reporter) {
halcanary8103a342016-03-08 15:10:16 -0800280 sk_sp<SkPDFDict> dict(new SkPDFDict);
halcanary56586b62016-07-25 13:59:30 -0700281 assert_emit_eq(reporter, *dict, "<<>>");
halcanaryf7a169e2015-05-01 07:35:45 -0700282
283 dict->insertInt("n1", SkToSizeT(42));
halcanary56586b62016-07-25 13:59:30 -0700284 assert_emit_eq(reporter, *dict, "<</n1 42>>");
halcanaryf7a169e2015-05-01 07:35:45 -0700285
286 dict.reset(new SkPDFDict);
halcanary56586b62016-07-25 13:59:30 -0700287 assert_emit_eq(reporter, *dict, "<<>>");
halcanaryf7a169e2015-05-01 07:35:45 -0700288
289 dict->insertInt("n1", 42);
halcanary56586b62016-07-25 13:59:30 -0700290 assert_emit_eq(reporter, *dict, "<</n1 42>>");
halcanaryf7a169e2015-05-01 07:35:45 -0700291
292 dict->insertScalar("n2", SK_ScalarHalf);
293
halcanary130444f2015-04-25 06:45:07 -0700294 SkString n3("n3");
halcanary8103a342016-03-08 15:10:16 -0800295 sk_sp<SkPDFArray> innerArray(new SkPDFArray);
halcanaryf7a169e2015-05-01 07:35:45 -0700296 innerArray->appendInt(-100);
halcanary8103a342016-03-08 15:10:16 -0800297 dict->insertObject(n3, std::move(innerArray));
halcanary56586b62016-07-25 13:59:30 -0700298 assert_emit_eq(reporter, *dict, "<</n1 42\n/n2 .5\n/n3 [-100]>>");
vandebo@chromium.org8459d4e2010-09-24 22:25:30 +0000299
halcanaryf7a169e2015-05-01 07:35:45 -0700300 dict.reset(new SkPDFDict);
halcanary56586b62016-07-25 13:59:30 -0700301 assert_emit_eq(reporter, *dict, "<<>>");
halcanaryf7a169e2015-05-01 07:35:45 -0700302
303 dict->insertInt("n1", 24);
halcanary56586b62016-07-25 13:59:30 -0700304 assert_emit_eq(reporter, *dict, "<</n1 24>>");
halcanaryf7a169e2015-05-01 07:35:45 -0700305
306 dict->insertInt("n2", SkToSizeT(99));
halcanary56586b62016-07-25 13:59:30 -0700307 assert_emit_eq(reporter, *dict, "<</n1 24\n/n2 99>>");
halcanaryf7a169e2015-05-01 07:35:45 -0700308
309 dict->insertScalar("n3", SK_ScalarHalf);
halcanary56586b62016-07-25 13:59:30 -0700310 assert_emit_eq(reporter, *dict, "<</n1 24\n/n2 99\n/n3 .5>>");
halcanaryf7a169e2015-05-01 07:35:45 -0700311
312 dict->insertName("n4", "AName");
halcanary56586b62016-07-25 13:59:30 -0700313 assert_emit_eq(reporter, *dict, "<</n1 24\n/n2 99\n/n3 .5\n/n4 /AName>>");
halcanaryf7a169e2015-05-01 07:35:45 -0700314
315 dict->insertName("n5", SkString("AnotherName"));
halcanary56586b62016-07-25 13:59:30 -0700316 assert_emit_eq(reporter, *dict, "<</n1 24\n/n2 99\n/n3 .5\n/n4 /AName\n"
halcanaryf7a169e2015-05-01 07:35:45 -0700317 "/n5 /AnotherName>>");
318
319 dict->insertString("n6", "A String");
halcanary56586b62016-07-25 13:59:30 -0700320 assert_emit_eq(reporter, *dict, "<</n1 24\n/n2 99\n/n3 .5\n/n4 /AName\n"
halcanaryf7a169e2015-05-01 07:35:45 -0700321 "/n5 /AnotherName\n/n6 (A String)>>");
322
323 dict->insertString("n7", SkString("Another String"));
halcanary56586b62016-07-25 13:59:30 -0700324 assert_emit_eq(reporter, *dict, "<</n1 24\n/n2 99\n/n3 .5\n/n4 /AName\n"
halcanaryf7a169e2015-05-01 07:35:45 -0700325 "/n5 /AnotherName\n/n6 (A String)\n/n7 (Another String)>>");
326
327 dict.reset(new SkPDFDict("DType"));
halcanary56586b62016-07-25 13:59:30 -0700328 assert_emit_eq(reporter, *dict, "<</Type /DType>>");
halcanary8e9f5e32016-02-24 15:46:46 -0800329
halcanary8103a342016-03-08 15:10:16 -0800330 sk_sp<SkPDFArray> referencedArray(new SkPDFArray);
halcanary530032a2016-08-18 14:22:52 -0700331 SkPDFObjNumMap catalog;
332 catalog.addObject(referencedArray.get());
333 REPORTER_ASSERT(reporter, catalog.getObjectNumber(
halcanaryf7a169e2015-05-01 07:35:45 -0700334 referencedArray.get()) == 1);
halcanary8103a342016-03-08 15:10:16 -0800335 dict->insertObjRef("n1", std::move(referencedArray));
halcanaryf7a169e2015-05-01 07:35:45 -0700336 SkString result = emit_to_string(*dict, &catalog);
halcanary56586b62016-07-25 13:59:30 -0700337 assert_eq(reporter, result, "<</Type /DType\n/n1 1 0 R>>");
halcanaryf7a169e2015-05-01 07:35:45 -0700338}
339
halcanary57f744e2016-09-09 11:41:59 -0700340DEF_TEST(SkPDF_Primitives, reporter) {
halcanaryf7a169e2015-05-01 07:35:45 -0700341 TestPDFUnion(reporter);
342 TestPDFArray(reporter);
343 TestPDFDict(reporter);
vandebo@chromium.org421d6442011-07-20 17:39:01 +0000344 TestPDFStream(reporter);
halcanaryf7a169e2015-05-01 07:35:45 -0700345 TestObjectNumberMap(reporter);
vandebo@chromium.org8459d4e2010-09-24 22:25:30 +0000346 TestObjectRef(reporter);
sugoi@google.com54f0d1b2013-02-27 19:17:41 +0000347 test_issue1083();
vandebo@chromium.org8459d4e2010-09-24 22:25:30 +0000348}
senorblancob0e89dc2014-10-20 14:03:12 -0700349
350namespace {
351
352class DummyImageFilter : public SkImageFilter {
353public:
robertphillips43c2ad42016-04-04 05:05:11 -0700354 static sk_sp<DummyImageFilter> Make(bool visited = false) {
355 return sk_sp<DummyImageFilter>(new DummyImageFilter(visited));
356 }
357
358 SK_TO_STRING_OVERRIDE()
359 SK_DECLARE_PUBLIC_FLATTENABLE_DESERIALIZATION_PROCS(DummyImageFilter)
360 bool visited() const { return fVisited; }
361
362protected:
robertphillips4ba94e22016-04-04 12:07:47 -0700363 sk_sp<SkSpecialImage> onFilterImage(SkSpecialImage* source, const Context&,
364 SkIPoint* offset) const override {
senorblancob0e89dc2014-10-20 14:03:12 -0700365 fVisited = true;
366 offset->fX = offset->fY = 0;
robertphillips4ba94e22016-04-04 12:07:47 -0700367 return sk_ref_sp<SkSpecialImage>(source);
senorblancob0e89dc2014-10-20 14:03:12 -0700368 }
Matt Sarett62745a82017-04-17 11:57:29 -0400369 sk_sp<SkImageFilter> onMakeColorSpace(SkColorSpaceXformer*) const override {
370 return sk_ref_sp(const_cast<DummyImageFilter*>(this));
371 }
senorblancob0e89dc2014-10-20 14:03:12 -0700372
373private:
robertphillips43c2ad42016-04-04 05:05:11 -0700374 DummyImageFilter(bool visited) : INHERITED(nullptr, 0, nullptr), fVisited(visited) {}
375
senorblancob0e89dc2014-10-20 14:03:12 -0700376 mutable bool fVisited;
robertphillips43c2ad42016-04-04 05:05:11 -0700377
378 typedef SkImageFilter INHERITED;
senorblancob0e89dc2014-10-20 14:03:12 -0700379};
380
reed60c9b582016-04-03 09:11:13 -0700381sk_sp<SkFlattenable> DummyImageFilter::CreateProc(SkReadBuffer& buffer) {
senorblancob0e89dc2014-10-20 14:03:12 -0700382 SK_IMAGEFILTER_UNFLATTEN_COMMON(common, 0);
383 bool visited = buffer.readBool();
robertphillips43c2ad42016-04-04 05:05:11 -0700384 return DummyImageFilter::Make(visited);
senorblancob0e89dc2014-10-20 14:03:12 -0700385}
386
robertphillipsf3f5bad2014-12-19 13:49:15 -0800387#ifndef SK_IGNORE_TO_STRING
388void DummyImageFilter::toString(SkString* str) const {
389 str->appendf("DummyImageFilter: (");
390 str->append(")");
391}
392#endif
393
senorblancob0e89dc2014-10-20 14:03:12 -0700394};
395
396// Check that PDF rendering of image filters successfully falls back to
397// CPU rasterization.
halcanary57f744e2016-09-09 11:41:59 -0700398DEF_TEST(SkPDF_ImageFilter, reporter) {
399 REQUIRE_PDF_DOCUMENT(SkPDF_ImageFilter, reporter);
halcanary3d32d502015-03-01 06:55:20 -0800400 SkDynamicMemoryWStream stream;
halcanary4b656662016-04-27 07:45:18 -0700401 sk_sp<SkDocument> doc(SkDocument::MakePDF(&stream));
halcanary3d32d502015-03-01 06:55:20 -0800402 SkCanvas* canvas = doc->beginPage(100.0f, 100.0f);
403
robertphillips43c2ad42016-04-04 05:05:11 -0700404 sk_sp<DummyImageFilter> filter(DummyImageFilter::Make());
senorblancob0e89dc2014-10-20 14:03:12 -0700405
406 // Filter just created; should be unvisited.
407 REPORTER_ASSERT(reporter, !filter->visited());
408 SkPaint paint;
Mike Reed5e257172016-11-01 11:22:05 -0400409 paint.setImageFilter(filter);
halcanary3d32d502015-03-01 06:55:20 -0800410 canvas->drawRect(SkRect::MakeWH(100, 100), paint);
411 doc->close();
senorblancob0e89dc2014-10-20 14:03:12 -0700412
413 // Filter was used in rendering; should be visited.
414 REPORTER_ASSERT(reporter, filter->visited());
415}
halcanary66a82f32015-10-12 13:05:04 -0700416
417// Check that PDF rendering of image filters successfully falls back to
418// CPU rasterization.
halcanary57f744e2016-09-09 11:41:59 -0700419DEF_TEST(SkPDF_FontCanEmbedTypeface, reporter) {
halcanary66a82f32015-10-12 13:05:04 -0700420 SkPDFCanon canon;
421
422 const char resource[] = "fonts/Roboto2-Regular_NoEmbed.ttf";
bungeman13b9c952016-05-12 10:09:30 -0700423 sk_sp<SkTypeface> noEmbedTypeface(MakeResourceAsTypeface(resource));
halcanary66a82f32015-10-12 13:05:04 -0700424 if (noEmbedTypeface) {
425 REPORTER_ASSERT(reporter,
halcanary8103a342016-03-08 15:10:16 -0800426 !SkPDFFont::CanEmbedTypeface(noEmbedTypeface.get(), &canon));
halcanary66a82f32015-10-12 13:05:04 -0700427 }
halcanary8103a342016-03-08 15:10:16 -0800428 sk_sp<SkTypeface> portableTypeface(
mbocee6a9912016-05-31 11:42:36 -0700429 sk_tool_utils::create_portable_typeface(NULL, SkFontStyle()));
halcanary66a82f32015-10-12 13:05:04 -0700430 REPORTER_ASSERT(reporter,
halcanary8103a342016-03-08 15:10:16 -0800431 SkPDFFont::CanEmbedTypeface(portableTypeface.get(), &canon));
halcanary66a82f32015-10-12 13:05:04 -0700432}
halcanary8e9f5e32016-02-24 15:46:46 -0800433
434
435// test to see that all finite scalars round trip via scanf().
436static void check_pdf_scalar_serialization(
437 skiatest::Reporter* reporter, float inputFloat) {
438 char floatString[SkPDFUtils::kMaximumFloatDecimalLength];
439 size_t len = SkPDFUtils::FloatToDecimal(inputFloat, floatString);
440 if (len >= sizeof(floatString)) {
441 ERRORF(reporter, "string too long: %u", (unsigned)len);
442 return;
443 }
444 if (floatString[len] != '\0' || strlen(floatString) != len) {
445 ERRORF(reporter, "terminator misplaced.");
446 return; // The terminator is needed for sscanf().
447 }
448 if (reporter->verbose()) {
449 SkDebugf("%15.9g = \"%s\"\n", inputFloat, floatString);
450 }
451 float roundTripFloat;
452 if (1 != sscanf(floatString, "%f", &roundTripFloat)) {
453 ERRORF(reporter, "unscannable result: %s", floatString);
454 return;
455 }
halcanary56586b62016-07-25 13:59:30 -0700456 if (std::isfinite(inputFloat) && roundTripFloat != inputFloat) {
halcanary8e9f5e32016-02-24 15:46:46 -0800457 ERRORF(reporter, "roundTripFloat (%.9g) != inputFloat (%.9g)",
458 roundTripFloat, inputFloat);
459 }
460}
461
462// Test SkPDFUtils::AppendScalar for accuracy.
halcanary57f744e2016-09-09 11:41:59 -0700463DEF_TEST(SkPDF_Primitives_Scalar, reporter) {
halcanary8e9f5e32016-02-24 15:46:46 -0800464 SkRandom random(0x5EED);
465 int iterationCount = 512;
466 while (iterationCount-- > 0) {
467 union { uint32_t u; float f; };
468 u = random.nextU();
469 static_assert(sizeof(float) == sizeof(uint32_t), "");
470 check_pdf_scalar_serialization(reporter, f);
471 }
472 float alwaysCheck[] = {
473 0.0f, -0.0f, 1.0f, -1.0f, SK_ScalarPI, 0.1f, FLT_MIN, FLT_MAX,
474 -FLT_MIN, -FLT_MAX, FLT_MIN / 16.0f, -FLT_MIN / 16.0f,
475 SK_FloatNaN, SK_FloatInfinity, SK_FloatNegativeInfinity,
476 -FLT_MIN / 8388608.0
477 };
478 for (float inputFloat: alwaysCheck) {
479 check_pdf_scalar_serialization(reporter, inputFloat);
480 }
481}
halcanaryeb92cb32016-07-15 13:41:27 -0700482
483// Test SkPDFUtils:: for accuracy.
halcanary57f744e2016-09-09 11:41:59 -0700484DEF_TEST(SkPDF_Primitives_Color, reporter) {
halcanaryeb92cb32016-07-15 13:41:27 -0700485 char buffer[5];
486 for (int i = 0; i < 256; ++i) {
487 size_t len = SkPDFUtils::ColorToDecimal(i, buffer);
488 REPORTER_ASSERT(reporter, len == strlen(buffer));
489 float f;
490 REPORTER_ASSERT(reporter, 1 == sscanf(buffer, "%f", &f));
491 int roundTrip = (int)(0.5 + f * 255);
492 REPORTER_ASSERT(reporter, roundTrip == i);
493 }
494}
Hal Canary43fb7a02016-12-30 13:09:03 -0500495#endif