blob: cd70d36b5223b4da625c25ca021bbe17542d7585 [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"
Hal Canary1521c8a2018-03-28 09:51:00 -070015#include "SkClusterator.h"
reed@google.com8a85d0c2011-06-24 19:12:12 +000016#include "SkData.h"
Hal Canaryfdcfb8b2018-06-13 09:42:32 -040017#include "SkDeflate.h"
Hal Canary98caedd2018-07-23 10:50:49 -040018#include "SkGlyphRun.h"
edisonn@google.comd9dfa182013-04-24 13:01:01 +000019#include "SkImageEncoder.h"
Cary Clark60aaeb22017-11-03 08:06:09 -040020#include "SkImageFilterPriv.h"
halcanary022c2bd2016-09-02 11:29:46 -070021#include "SkMakeUnique.h"
edisonn@google.comd9dfa182013-04-24 13:01:01 +000022#include "SkMatrix.h"
halcanarya1f1ee92015-02-20 06:17:26 -080023#include "SkPDFCanon.h"
vandebo@chromium.org4e1cc6a2013-01-25 19:27:23 +000024#include "SkPDFDevice.h"
Hal Canary23564b92018-09-07 14:33:14 -040025#include "SkPDFDocument.h"
halcanary66a82f32015-10-12 13:05:04 -070026#include "SkPDFFont.h"
vandebo@chromium.org8459d4e2010-09-24 22:25:30 +000027#include "SkPDFTypes.h"
halcanary8e9f5e32016-02-24 15:46:46 -080028#include "SkPDFUtils.h"
senorblancob0e89dc2014-10-20 14:03:12 -070029#include "SkReadBuffer.h"
vandebo@chromium.org8459d4e2010-09-24 22:25:30 +000030#include "SkScalar.h"
robertphillips4ba94e22016-04-04 12:07:47 -070031#include "SkSpecialImage.h"
vandebo@chromium.org8459d4e2010-09-24 22:25:30 +000032#include "SkStream.h"
Hal Canaryc640d0d2018-06-13 09:59:02 -040033#include "SkTo.h"
vandebo@chromium.org421d6442011-07-20 17:39:01 +000034#include "SkTypes.h"
halcanary66a82f32015-10-12 13:05:04 -070035#include "sk_tool_utils.h"
vandebo@chromium.org8459d4e2010-09-24 22:25:30 +000036
halcanary56586b62016-07-25 13:59:30 -070037#include <cstdlib>
38#include <cmath>
39
commit-bot@chromium.org608ea652013-10-03 19:29:21 +000040#define DUMMY_TEXT "DCT compessed stream."
41
halcanaryf7a169e2015-05-01 07:35:45 -070042template <typename T>
Hal Canaryf6462c42018-11-13 16:19:59 -050043static SkString emit_to_string(T& obj) {
halcanary6a144342015-01-23 11:45:10 -080044 SkDynamicMemoryWStream buffer;
Hal Canaryf6462c42018-11-13 16:19:59 -050045 obj.emitObject(&buffer);
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
Hal Canaryc5b94982017-10-03 09:38:43 -0400129 objNumMap.addObjectRecursively(a1.get());
130 objNumMap.addObjectRecursively(a2.get());
131 objNumMap.addObjectRecursively(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;
Hal Canaryc5b94982017-10-03 09:38:43 -0400149 catalog.addObjectRecursively(a1.get());
halcanary530032a2016-08-18 14:22:52 -0700150 REPORTER_ASSERT(reporter, catalog.getObjectNumber(a1.get()) == 1);
vandebo@chromium.org8459d4e2010-09-24 22:25:30 +0000151
Hal Canaryf6462c42018-11-13 16:19:59 -0500152 SkString result = emit_to_string(*a2);
halcanaryf7a169e2015-05-01 07:35:45 -0700153 // 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;
Hal Canary23564b92018-09-07 14:33:14 -0400164 sk_sp<SkDocument> doc(SkPDF::MakeDocument(&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;
Mike Reed97f3cc22018-12-03 09:45:17 -0500167 paint.setTextEncoding(kGlyphID_SkTextEncoding);
vandebo@chromium.org4e1cc6a2013-01-25 19:27:23 +0000168
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;
Hal Canaryc5b94982017-10-03 09:38:43 -0400268 catalog.addObjectRecursively(referencedArray.get());
halcanary530032a2016-08-18 14:22:52 -0700269 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
Hal Canaryf6462c42018-11-13 16:19:59 -0500273 SkString result = emit_to_string(*array);
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;
Hal Canaryc5b94982017-10-03 09:38:43 -0400332 catalog.addObjectRecursively(referencedArray.get());
halcanary530032a2016-08-18 14:22:52 -0700333 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));
Hal Canaryf6462c42018-11-13 16:19:59 -0500336 SkString result = emit_to_string(*dict);
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
robertphillips43c2ad42016-04-04 05:05:11 -0700358 bool visited() const { return fVisited; }
359
360protected:
robertphillips4ba94e22016-04-04 12:07:47 -0700361 sk_sp<SkSpecialImage> onFilterImage(SkSpecialImage* source, const Context&,
362 SkIPoint* offset) const override {
senorblancob0e89dc2014-10-20 14:03:12 -0700363 fVisited = true;
364 offset->fX = offset->fY = 0;
robertphillips4ba94e22016-04-04 12:07:47 -0700365 return sk_ref_sp<SkSpecialImage>(source);
senorblancob0e89dc2014-10-20 14:03:12 -0700366 }
Matt Sarett62745a82017-04-17 11:57:29 -0400367 sk_sp<SkImageFilter> onMakeColorSpace(SkColorSpaceXformer*) const override {
368 return sk_ref_sp(const_cast<DummyImageFilter*>(this));
369 }
senorblancob0e89dc2014-10-20 14:03:12 -0700370
371private:
Mike Klein4fee3232018-10-18 17:27:16 -0400372 SK_FLATTENABLE_HOOKS(DummyImageFilter)
robertphillips43c2ad42016-04-04 05:05:11 -0700373 DummyImageFilter(bool visited) : INHERITED(nullptr, 0, nullptr), fVisited(visited) {}
374
senorblancob0e89dc2014-10-20 14:03:12 -0700375 mutable bool fVisited;
robertphillips43c2ad42016-04-04 05:05:11 -0700376
377 typedef SkImageFilter INHERITED;
senorblancob0e89dc2014-10-20 14:03:12 -0700378};
379
reed60c9b582016-04-03 09:11:13 -0700380sk_sp<SkFlattenable> DummyImageFilter::CreateProc(SkReadBuffer& buffer) {
senorblancob0e89dc2014-10-20 14:03:12 -0700381 SK_IMAGEFILTER_UNFLATTEN_COMMON(common, 0);
382 bool visited = buffer.readBool();
robertphillips43c2ad42016-04-04 05:05:11 -0700383 return DummyImageFilter::Make(visited);
senorblancob0e89dc2014-10-20 14:03:12 -0700384}
385
386};
387
388// Check that PDF rendering of image filters successfully falls back to
389// CPU rasterization.
halcanary57f744e2016-09-09 11:41:59 -0700390DEF_TEST(SkPDF_ImageFilter, reporter) {
391 REQUIRE_PDF_DOCUMENT(SkPDF_ImageFilter, reporter);
halcanary3d32d502015-03-01 06:55:20 -0800392 SkDynamicMemoryWStream stream;
Hal Canary23564b92018-09-07 14:33:14 -0400393 sk_sp<SkDocument> doc(SkPDF::MakeDocument(&stream));
halcanary3d32d502015-03-01 06:55:20 -0800394 SkCanvas* canvas = doc->beginPage(100.0f, 100.0f);
395
robertphillips43c2ad42016-04-04 05:05:11 -0700396 sk_sp<DummyImageFilter> filter(DummyImageFilter::Make());
senorblancob0e89dc2014-10-20 14:03:12 -0700397
398 // Filter just created; should be unvisited.
399 REPORTER_ASSERT(reporter, !filter->visited());
400 SkPaint paint;
Mike Reed5e257172016-11-01 11:22:05 -0400401 paint.setImageFilter(filter);
halcanary3d32d502015-03-01 06:55:20 -0800402 canvas->drawRect(SkRect::MakeWH(100, 100), paint);
403 doc->close();
senorblancob0e89dc2014-10-20 14:03:12 -0700404
405 // Filter was used in rendering; should be visited.
406 REPORTER_ASSERT(reporter, filter->visited());
407}
halcanary66a82f32015-10-12 13:05:04 -0700408
409// Check that PDF rendering of image filters successfully falls back to
410// CPU rasterization.
halcanary57f744e2016-09-09 11:41:59 -0700411DEF_TEST(SkPDF_FontCanEmbedTypeface, reporter) {
halcanary66a82f32015-10-12 13:05:04 -0700412 SkPDFCanon canon;
413
414 const char resource[] = "fonts/Roboto2-Regular_NoEmbed.ttf";
bungeman13b9c952016-05-12 10:09:30 -0700415 sk_sp<SkTypeface> noEmbedTypeface(MakeResourceAsTypeface(resource));
halcanary66a82f32015-10-12 13:05:04 -0700416 if (noEmbedTypeface) {
417 REPORTER_ASSERT(reporter,
halcanary8103a342016-03-08 15:10:16 -0800418 !SkPDFFont::CanEmbedTypeface(noEmbedTypeface.get(), &canon));
halcanary66a82f32015-10-12 13:05:04 -0700419 }
halcanary8103a342016-03-08 15:10:16 -0800420 sk_sp<SkTypeface> portableTypeface(
Ben Wagnera93a14a2017-08-28 10:34:05 -0400421 sk_tool_utils::create_portable_typeface(nullptr, SkFontStyle()));
halcanary66a82f32015-10-12 13:05:04 -0700422 REPORTER_ASSERT(reporter,
halcanary8103a342016-03-08 15:10:16 -0800423 SkPDFFont::CanEmbedTypeface(portableTypeface.get(), &canon));
halcanary66a82f32015-10-12 13:05:04 -0700424}
halcanary8e9f5e32016-02-24 15:46:46 -0800425
426
427// test to see that all finite scalars round trip via scanf().
428static void check_pdf_scalar_serialization(
429 skiatest::Reporter* reporter, float inputFloat) {
Hal Canary3c36ef62018-01-02 16:25:53 -0500430 char floatString[kMaximumSkFloatToDecimalLength];
431 size_t len = SkFloatToDecimal(inputFloat, floatString);
halcanary8e9f5e32016-02-24 15:46:46 -0800432 if (len >= sizeof(floatString)) {
433 ERRORF(reporter, "string too long: %u", (unsigned)len);
434 return;
435 }
436 if (floatString[len] != '\0' || strlen(floatString) != len) {
437 ERRORF(reporter, "terminator misplaced.");
438 return; // The terminator is needed for sscanf().
439 }
440 if (reporter->verbose()) {
441 SkDebugf("%15.9g = \"%s\"\n", inputFloat, floatString);
442 }
443 float roundTripFloat;
444 if (1 != sscanf(floatString, "%f", &roundTripFloat)) {
445 ERRORF(reporter, "unscannable result: %s", floatString);
446 return;
447 }
halcanary56586b62016-07-25 13:59:30 -0700448 if (std::isfinite(inputFloat) && roundTripFloat != inputFloat) {
halcanary8e9f5e32016-02-24 15:46:46 -0800449 ERRORF(reporter, "roundTripFloat (%.9g) != inputFloat (%.9g)",
450 roundTripFloat, inputFloat);
451 }
452}
453
454// Test SkPDFUtils::AppendScalar for accuracy.
halcanary57f744e2016-09-09 11:41:59 -0700455DEF_TEST(SkPDF_Primitives_Scalar, reporter) {
halcanary8e9f5e32016-02-24 15:46:46 -0800456 SkRandom random(0x5EED);
457 int iterationCount = 512;
458 while (iterationCount-- > 0) {
459 union { uint32_t u; float f; };
460 u = random.nextU();
461 static_assert(sizeof(float) == sizeof(uint32_t), "");
462 check_pdf_scalar_serialization(reporter, f);
463 }
464 float alwaysCheck[] = {
465 0.0f, -0.0f, 1.0f, -1.0f, SK_ScalarPI, 0.1f, FLT_MIN, FLT_MAX,
466 -FLT_MIN, -FLT_MAX, FLT_MIN / 16.0f, -FLT_MIN / 16.0f,
467 SK_FloatNaN, SK_FloatInfinity, SK_FloatNegativeInfinity,
468 -FLT_MIN / 8388608.0
469 };
470 for (float inputFloat: alwaysCheck) {
471 check_pdf_scalar_serialization(reporter, inputFloat);
472 }
473}
halcanaryeb92cb32016-07-15 13:41:27 -0700474
475// Test SkPDFUtils:: for accuracy.
halcanary57f744e2016-09-09 11:41:59 -0700476DEF_TEST(SkPDF_Primitives_Color, reporter) {
halcanaryeb92cb32016-07-15 13:41:27 -0700477 char buffer[5];
478 for (int i = 0; i < 256; ++i) {
479 size_t len = SkPDFUtils::ColorToDecimal(i, buffer);
480 REPORTER_ASSERT(reporter, len == strlen(buffer));
481 float f;
482 REPORTER_ASSERT(reporter, 1 == sscanf(buffer, "%f", &f));
483 int roundTrip = (int)(0.5 + f * 255);
484 REPORTER_ASSERT(reporter, roundTrip == i);
485 }
486}
Hal Canary1521c8a2018-03-28 09:51:00 -0700487
Herb Derby81ecdbb2018-07-20 11:48:38 -0400488static SkGlyphRun make_run(size_t len, const SkGlyphID* glyphs, SkPoint* pos,
Hal Canary98caedd2018-07-23 10:50:49 -0400489 SkPaint paint, const uint32_t* clusters,
490 size_t utf8TextByteLength, const char* utf8Text) {
Mike Reed7b7ab592018-12-05 21:32:05 -0500491 return SkGlyphRun(paint, SkFont::LEGACY_ExtractFromPaint(paint),
Herb Derbyb935cf82018-07-26 16:54:18 -0400492 SkSpan<const SkPoint>{pos, len},
Hal Canary98caedd2018-07-23 10:50:49 -0400493 SkSpan<const SkGlyphID>{glyphs, len},
Hal Canary98caedd2018-07-23 10:50:49 -0400494 SkSpan<const char>{utf8Text, utf8TextByteLength},
495 SkSpan<const uint32_t>{clusters, len});
496}
497
Hal Canary1521c8a2018-03-28 09:51:00 -0700498DEF_TEST(SkPDF_Clusterator, reporter) {
Hal Canary1521c8a2018-03-28 09:51:00 -0700499 SkPaint paint;
Mike Reed97f3cc22018-12-03 09:45:17 -0500500 paint.setTextEncoding(kGlyphID_SkTextEncoding);
Hal Canarye9e19dd2018-04-02 11:36:39 -0400501 {
Hal Canary98caedd2018-07-23 10:50:49 -0400502 constexpr unsigned len = 11;
503 const uint32_t clusters[len] = { 3, 2, 2, 1, 0, 4, 4, 7, 6, 6, 5 };
504 const SkGlyphID glyphs[len] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11};
Herb Derby81ecdbb2018-07-20 11:48:38 -0400505 SkPoint pos[len] = {{0, 0}, {0, 0}, {0, 0}, {0, 0}, {0, 0}, {0, 0},
Hal Canary98caedd2018-07-23 10:50:49 -0400506 {0, 0}, {0, 0}, {0, 0}, {0, 0}, {0, 0}};
Hal Canarye9e19dd2018-04-02 11:36:39 -0400507 const char text[] = "abcdefgh";
Hal Canary98caedd2018-07-23 10:50:49 -0400508 SkGlyphRun run = make_run(len, glyphs, pos, paint, clusters, strlen(text), text);
509 SkClusterator clusterator(run);
Hal Canarye9e19dd2018-04-02 11:36:39 -0400510 SkClusterator::Cluster expectations[] = {
511 {&text[3], 1, 0, 1},
512 {&text[2], 1, 1, 2},
513 {&text[1], 1, 3, 1},
514 {&text[0], 1, 4, 1},
515 {&text[4], 1, 5, 2},
516 {&text[7], 1, 7, 1},
517 {&text[6], 1, 8, 2},
518 {&text[5], 1, 10, 1},
519 {nullptr, 0, 0, 0},
520 };
521 for (const auto& expectation : expectations) {
522 REPORTER_ASSERT(reporter, clusterator.next() == expectation);
523 }
524 }
525 {
Hal Canary98caedd2018-07-23 10:50:49 -0400526 constexpr unsigned len = 5;
527 const uint32_t clusters[len] = { 0, 1, 4, 5, 6 };
528 const SkGlyphID glyphs[len] = { 43, 167, 79, 79, 82, };
Herb Derby81ecdbb2018-07-20 11:48:38 -0400529 SkPoint pos[len] = {{0, 0}, {0, 0}, {0, 0}, {0, 0}, {0, 0}};
Hal Canarye9e19dd2018-04-02 11:36:39 -0400530 const char text[] = "Ha\xCC\x8A" "llo";
Hal Canary98caedd2018-07-23 10:50:49 -0400531 SkGlyphRun run = make_run(len, glyphs, pos, paint, clusters, strlen(text), text);
532 SkClusterator clusterator(run);
Hal Canarye9e19dd2018-04-02 11:36:39 -0400533 SkClusterator::Cluster expectations[] = {
534 {&text[0], 1, 0, 1},
535 {&text[1], 3, 1, 1},
536 {&text[4], 1, 2, 1},
537 {&text[5], 1, 3, 1},
538 {&text[6], 1, 4, 1},
539 {nullptr, 0, 0, 0},
540 };
541 for (const auto& expectation : expectations) {
542 REPORTER_ASSERT(reporter, clusterator.next() == expectation);
543 }
Hal Canary1521c8a2018-03-28 09:51:00 -0700544 }
545}
546
Hal Canary43fb7a02016-12-30 13:09:03 -0500547#endif