blob: b9f8a05a80300fd8f304976cc1399c3dc7cf53c4 [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"
halcanary3d32d502015-03-01 06:55:20 -080017#include "SkDocument.h"
halcanaryd9e57152015-08-12 11:24:40 -070018#include "SkDeflate.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"
halcanary66a82f32015-10-12 13:05:04 -070025#include "SkPDFFont.h"
vandebo@chromium.org8459d4e2010-09-24 22:25:30 +000026#include "SkPDFTypes.h"
halcanary8e9f5e32016-02-24 15:46:46 -080027#include "SkPDFUtils.h"
senorblancob0e89dc2014-10-20 14:03:12 -070028#include "SkReadBuffer.h"
vandebo@chromium.org8459d4e2010-09-24 22:25:30 +000029#include "SkScalar.h"
robertphillips4ba94e22016-04-04 12:07:47 -070030#include "SkSpecialImage.h"
vandebo@chromium.org8459d4e2010-09-24 22:25:30 +000031#include "SkStream.h"
vandebo@chromium.org421d6442011-07-20 17:39:01 +000032#include "SkTypes.h"
halcanary66a82f32015-10-12 13:05:04 -070033#include "sk_tool_utils.h"
vandebo@chromium.org8459d4e2010-09-24 22:25:30 +000034
halcanary56586b62016-07-25 13:59:30 -070035#include <cstdlib>
36#include <cmath>
37
commit-bot@chromium.org608ea652013-10-03 19:29:21 +000038#define DUMMY_TEXT "DCT compessed stream."
39
halcanaryf7a169e2015-05-01 07:35:45 -070040template <typename T>
halcanary530032a2016-08-18 14:22:52 -070041static SkString emit_to_string(T& obj, SkPDFObjNumMap* catPtr = nullptr) {
42 SkPDFObjNumMap catalog;
halcanary6a144342015-01-23 11:45:10 -080043 SkDynamicMemoryWStream buffer;
halcanaryf7a169e2015-05-01 07:35:45 -070044 if (!catPtr) {
45 catPtr = &catalog;
halcanary130444f2015-04-25 06:45:07 -070046 }
halcanary530032a2016-08-18 14:22:52 -070047 obj.emitObject(&buffer, *catPtr);
halcanary56586b62016-07-25 13:59:30 -070048 SkString tmp(buffer.bytesWritten());
49 buffer.copyTo(tmp.writable_str());
halcanaryf7a169e2015-05-01 07:35:45 -070050 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
halcanary56586b62016-07-25 13:59:30 -070057static void assert_eql(skiatest::Reporter* reporter,
58 const SkString& skString,
59 const char* str,
60 size_t len) {
61 if (!eq(skString, str, len)) {
62 REPORT_FAILURE(reporter, "", SkStringPrintf(
63 "'%*s' != '%s'", len, str, skString.c_str()));
64 }
65}
halcanaryf7a169e2015-05-01 07:35:45 -070066
halcanary56586b62016-07-25 13:59:30 -070067static void assert_eq(skiatest::Reporter* reporter,
68 const SkString& skString,
69 const char* str) {
70 assert_eql(reporter, skString, str, strlen(str));
71}
halcanaryf7a169e2015-05-01 07:35:45 -070072
73
halcanary56586b62016-07-25 13:59:30 -070074template <typename T>
75static void assert_emit_eq(skiatest::Reporter* reporter,
76 T& object,
77 const char* string) {
78 SkString result = emit_to_string(object);
79 assert_eq(reporter, result, string);
80}
halcanaryf7a169e2015-05-01 07:35:45 -070081
vandebo@chromium.org421d6442011-07-20 17:39:01 +000082static void TestPDFStream(skiatest::Reporter* reporter) {
83 char streamBytes[] = "Test\nFoo\tBar";
halcanary022c2bd2016-09-02 11:29:46 -070084 auto streamData = skstd::make_unique<SkMemoryStream>(
85 streamBytes, strlen(streamBytes), true);
halcanaryac0e00d2016-07-27 11:12:23 -070086 auto stream = sk_make_sp<SkPDFStream>(std::move(streamData));
halcanary56586b62016-07-25 13:59:30 -070087 assert_emit_eq(reporter,
halcanaryf7a169e2015-05-01 07:35:45 -070088 *stream,
89 "<</Length 12>> stream\nTest\nFoo\tBar\nendstream");
halcanaryfa251062016-07-29 10:13:18 -070090 stream->dict()->insertInt("Attribute", 42);
halcanary56586b62016-07-25 13:59:30 -070091 assert_emit_eq(reporter,
halcanaryf7a169e2015-05-01 07:35:45 -070092 *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.";
halcanaryfe8f0e02016-07-27 14:14:04 -0700101 auto stream = sk_make_sp<SkPDFStream>(
102 SkData::MakeWithCopy(streamBytes2, strlen(streamBytes2)));
vandebo@chromium.org421d6442011-07-20 17:39:01 +0000103
104 SkDynamicMemoryWStream compressedByteStream;
halcanary48305e82015-08-18 13:30:25 -0700105 SkDeflateWStream deflateWStream(&compressedByteStream);
106 deflateWStream.write(streamBytes2, strlen(streamBytes2));
107 deflateWStream.finalize();
vandebo@chromium.org421d6442011-07-20 17:39:01 +0000108
mtkleincabc08c2015-02-19 08:29:24 -0800109 SkDynamicMemoryWStream expected;
halcanary37c46ca2015-03-31 12:30:20 -0700110 expected.writeText("<</Filter /FlateDecode\n/Length 116>> stream\n");
halcanary48305e82015-08-18 13:30:25 -0700111 compressedByteStream.writeToStream(&expected);
112 compressedByteStream.reset();
mtkleincabc08c2015-02-19 08:29:24 -0800113 expected.writeText("\nendstream");
reed42943c82016-09-12 12:01:44 -0700114 sk_sp<SkData> expectedResultData2(expected.detachAsData());
halcanaryf7a169e2015-05-01 07:35:45 -0700115 SkString result = emit_to_string(*stream);
halcanary18e0cbc2016-08-03 14:17:53 -0700116 #ifndef SK_PDF_LESS_COMPRESSION
halcanary56586b62016-07-25 13:59:30 -0700117 assert_eql(reporter,
halcanaryf7a169e2015-05-01 07:35:45 -0700118 result,
119 (const char*)expectedResultData2->data(),
120 expectedResultData2->size());
halcanary18e0cbc2016-08-03 14:17:53 -0700121 #endif
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
Hal Canaryc5b94982017-10-03 09:38:43 -0400131 objNumMap.addObjectRecursively(a1.get());
132 objNumMap.addObjectRecursively(a2.get());
133 objNumMap.addObjectRecursively(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
halcanary530032a2016-08-18 14:22:52 -0700150 SkPDFObjNumMap catalog;
Hal Canaryc5b94982017-10-03 09:38:43 -0400151 catalog.addObjectRecursively(a1.get());
halcanary530032a2016-08-18 14:22:52 -0700152 REPORTER_ASSERT(reporter, catalog.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].
halcanary56586b62016-07-25 13:59:30 -0700157 assert_eq(reporter, result, "[1 0 R]");
vandebo@chromium.org8459d4e2010-09-24 22:25:30 +0000158}
159
vandebo@chromium.org4e1cc6a2013-01-25 19:27:23 +0000160// This test used to assert without the fix submitted for
161// http://code.google.com/p/skia/issues/detail?id=1083.
162// SKP files might have invalid glyph ids. This test ensures they are ignored,
163// and there is no assert on input data in Debug mode.
sugoi@google.com54f0d1b2013-02-27 19:17:41 +0000164static void test_issue1083() {
halcanary3d32d502015-03-01 06:55:20 -0800165 SkDynamicMemoryWStream outStream;
halcanary4b656662016-04-27 07:45:18 -0700166 sk_sp<SkDocument> doc(SkDocument::MakePDF(&outStream));
halcanary3d32d502015-03-01 06:55:20 -0800167 SkCanvas* canvas = doc->beginPage(100.0f, 100.0f);
vandebo@chromium.org4e1cc6a2013-01-25 19:27:23 +0000168 SkPaint paint;
169 paint.setTextEncoding(SkPaint::kGlyphID_TextEncoding);
170
171 uint16_t glyphID = 65000;
halcanary3d32d502015-03-01 06:55:20 -0800172 canvas->drawText(&glyphID, 2, 0, 0, paint);
vandebo@chromium.org4e1cc6a2013-01-25 19:27:23 +0000173
halcanary3d32d502015-03-01 06:55:20 -0800174 doc->close();
vandebo@chromium.org4e1cc6a2013-01-25 19:27:23 +0000175}
176
halcanary56586b62016-07-25 13:59:30 -0700177static void assert_emit_eq_number(skiatest::Reporter* reporter, float number) {
178 SkPDFUnion pdfUnion = SkPDFUnion::Scalar(number);
179 SkString result = emit_to_string(pdfUnion);
180 float value = static_cast<float>(std::atof(result.c_str()));
181 if (value != number) {
182 ERRORF(reporter, "%.9g != %s", number, result.c_str());
183 }
184}
185
186
halcanaryf7a169e2015-05-01 07:35:45 -0700187static void TestPDFUnion(skiatest::Reporter* reporter) {
188 SkPDFUnion boolTrue = SkPDFUnion::Bool(true);
halcanary56586b62016-07-25 13:59:30 -0700189 assert_emit_eq(reporter, boolTrue, "true");
vandebo@chromium.org8459d4e2010-09-24 22:25:30 +0000190
halcanaryf7a169e2015-05-01 07:35:45 -0700191 SkPDFUnion boolFalse = SkPDFUnion::Bool(false);
halcanary56586b62016-07-25 13:59:30 -0700192 assert_emit_eq(reporter, boolFalse, "false");
vandebo@chromium.org8459d4e2010-09-24 22:25:30 +0000193
halcanaryf7a169e2015-05-01 07:35:45 -0700194 SkPDFUnion int42 = SkPDFUnion::Int(42);
halcanary56586b62016-07-25 13:59:30 -0700195 assert_emit_eq(reporter, int42, "42");
halcanaryf7a169e2015-05-01 07:35:45 -0700196
halcanary56586b62016-07-25 13:59:30 -0700197 assert_emit_eq_number(reporter, SK_ScalarHalf);
198 assert_emit_eq_number(reporter, 110999.75f); // bigScalar
199 assert_emit_eq_number(reporter, 50000000.1f); // biggerScalar
200 assert_emit_eq_number(reporter, 1.0f / 65536); // smallScalar
vandebo@chromium.org094316b2011-03-04 03:15:13 +0000201
halcanaryf7a169e2015-05-01 07:35:45 -0700202 SkPDFUnion stringSimple = SkPDFUnion::String("test ) string ( foo");
halcanary56586b62016-07-25 13:59:30 -0700203 assert_emit_eq(reporter, stringSimple, "(test \\) string \\( foo)");
vandebo@chromium.org8459d4e2010-09-24 22:25:30 +0000204
halcanaryf7a169e2015-05-01 07:35:45 -0700205 SkString stringComplexInput("\ttest ) string ( foo");
206 SkPDFUnion stringComplex = SkPDFUnion::String(stringComplexInput);
halcanary56586b62016-07-25 13:59:30 -0700207 assert_emit_eq(reporter, stringComplex, "(\\011test \\) string \\( foo)");
halcanaryee41b752016-06-23 14:08:11 -0700208
209 SkString binaryStringInput("\1\2\3\4\5\6\7\10\11\12\13\14\15\16\17\20");
210 SkPDFUnion binaryString = SkPDFUnion::String(binaryStringInput);
halcanary56586b62016-07-25 13:59:30 -0700211 assert_emit_eq(reporter, binaryString, "<0102030405060708090A0B0C0D0E0F10>");
vandebo@chromium.org8459d4e2010-09-24 22:25:30 +0000212
halcanaryf7a169e2015-05-01 07:35:45 -0700213 SkString nameInput("Test name\twith#tab");
214 SkPDFUnion name = SkPDFUnion::Name(nameInput);
halcanary56586b62016-07-25 13:59:30 -0700215 assert_emit_eq(reporter, name, "/Test#20name#09with#23tab");
halcanaryf7a169e2015-05-01 07:35:45 -0700216
217 SkString nameInput2("A#/%()<>[]{}B");
218 SkPDFUnion name2 = SkPDFUnion::Name(nameInput2);
halcanary56586b62016-07-25 13:59:30 -0700219 assert_emit_eq(reporter, name2, "/A#23#2F#25#28#29#3C#3E#5B#5D#7B#7DB");
halcanaryf7a169e2015-05-01 07:35:45 -0700220
221 SkPDFUnion name3 = SkPDFUnion::Name("SimpleNameWithOnlyPrintableASCII");
halcanary56586b62016-07-25 13:59:30 -0700222 assert_emit_eq(reporter, name3, "/SimpleNameWithOnlyPrintableASCII");
vandebo@chromium.org251a7662012-09-21 17:50:50 +0000223
vandebo@chromium.orgc0376fe2012-03-05 18:44:33 +0000224 // Test that we correctly handle characters with the high-bit set.
halcanaryf7a169e2015-05-01 07:35:45 -0700225 SkString highBitString("\xDE\xAD" "be\xEF");
226 SkPDFUnion highBitName = SkPDFUnion::Name(highBitString);
halcanary56586b62016-07-25 13:59:30 -0700227 assert_emit_eq(reporter, highBitName, "/#DE#ADbe#EF");
halcanaryf7a169e2015-05-01 07:35:45 -0700228}
vandebo@chromium.orgc0376fe2012-03-05 18:44:33 +0000229
halcanaryf7a169e2015-05-01 07:35:45 -0700230static void TestPDFArray(skiatest::Reporter* reporter) {
halcanary8103a342016-03-08 15:10:16 -0800231 sk_sp<SkPDFArray> array(new SkPDFArray);
halcanary56586b62016-07-25 13:59:30 -0700232 assert_emit_eq(reporter, *array, "[]");
vandebo@chromium.org8459d4e2010-09-24 22:25:30 +0000233
halcanaryf7a169e2015-05-01 07:35:45 -0700234 array->appendInt(42);
halcanary56586b62016-07-25 13:59:30 -0700235 assert_emit_eq(reporter, *array, "[42]");
halcanaryf7a169e2015-05-01 07:35:45 -0700236
237 array->appendScalar(SK_ScalarHalf);
halcanary56586b62016-07-25 13:59:30 -0700238 assert_emit_eq(reporter, *array, "[42 .5]");
halcanaryf7a169e2015-05-01 07:35:45 -0700239
240 array->appendInt(0);
halcanary56586b62016-07-25 13:59:30 -0700241 assert_emit_eq(reporter, *array, "[42 .5 0]");
halcanaryf7a169e2015-05-01 07:35:45 -0700242
243 array->appendBool(true);
halcanary56586b62016-07-25 13:59:30 -0700244 assert_emit_eq(reporter, *array, "[42 .5 0 true]");
halcanaryf7a169e2015-05-01 07:35:45 -0700245
246 array->appendName("ThisName");
halcanary56586b62016-07-25 13:59:30 -0700247 assert_emit_eq(reporter, *array, "[42 .5 0 true /ThisName]");
halcanaryf7a169e2015-05-01 07:35:45 -0700248
249 array->appendName(SkString("AnotherName"));
halcanary56586b62016-07-25 13:59:30 -0700250 assert_emit_eq(reporter, *array, "[42 .5 0 true /ThisName /AnotherName]");
halcanaryf7a169e2015-05-01 07:35:45 -0700251
252 array->appendString("This String");
halcanary56586b62016-07-25 13:59:30 -0700253 assert_emit_eq(reporter, *array,
halcanary8e9f5e32016-02-24 15:46:46 -0800254 "[42 .5 0 true /ThisName /AnotherName (This String)]");
halcanaryf7a169e2015-05-01 07:35:45 -0700255
256 array->appendString(SkString("Another String"));
halcanary56586b62016-07-25 13:59:30 -0700257 assert_emit_eq(reporter, *array,
halcanary8e9f5e32016-02-24 15:46:46 -0800258 "[42 .5 0 true /ThisName /AnotherName (This String) "
halcanaryf7a169e2015-05-01 07:35:45 -0700259 "(Another String)]");
260
halcanary8103a342016-03-08 15:10:16 -0800261 sk_sp<SkPDFArray> innerArray(new SkPDFArray);
halcanaryf7a169e2015-05-01 07:35:45 -0700262 innerArray->appendInt(-1);
halcanary8103a342016-03-08 15:10:16 -0800263 array->appendObject(std::move(innerArray));
halcanary56586b62016-07-25 13:59:30 -0700264 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 "(Another String) [-1]]");
267
halcanary8103a342016-03-08 15:10:16 -0800268 sk_sp<SkPDFArray> referencedArray(new SkPDFArray);
halcanary530032a2016-08-18 14:22:52 -0700269 SkPDFObjNumMap catalog;
Hal Canaryc5b94982017-10-03 09:38:43 -0400270 catalog.addObjectRecursively(referencedArray.get());
halcanary530032a2016-08-18 14:22:52 -0700271 REPORTER_ASSERT(reporter, catalog.getObjectNumber(
halcanaryf7a169e2015-05-01 07:35:45 -0700272 referencedArray.get()) == 1);
halcanary8103a342016-03-08 15:10:16 -0800273 array->appendObjRef(std::move(referencedArray));
halcanaryf7a169e2015-05-01 07:35:45 -0700274
275 SkString result = emit_to_string(*array, &catalog);
halcanary56586b62016-07-25 13:59:30 -0700276 assert_eq(reporter, result,
halcanary8e9f5e32016-02-24 15:46:46 -0800277 "[42 .5 0 true /ThisName /AnotherName (This String) "
halcanaryf7a169e2015-05-01 07:35:45 -0700278 "(Another String) [-1] 1 0 R]");
279}
280
281static void TestPDFDict(skiatest::Reporter* reporter) {
halcanary8103a342016-03-08 15:10:16 -0800282 sk_sp<SkPDFDict> dict(new SkPDFDict);
halcanary56586b62016-07-25 13:59:30 -0700283 assert_emit_eq(reporter, *dict, "<<>>");
halcanaryf7a169e2015-05-01 07:35:45 -0700284
285 dict->insertInt("n1", SkToSizeT(42));
halcanary56586b62016-07-25 13:59:30 -0700286 assert_emit_eq(reporter, *dict, "<</n1 42>>");
halcanaryf7a169e2015-05-01 07:35:45 -0700287
288 dict.reset(new SkPDFDict);
halcanary56586b62016-07-25 13:59:30 -0700289 assert_emit_eq(reporter, *dict, "<<>>");
halcanaryf7a169e2015-05-01 07:35:45 -0700290
291 dict->insertInt("n1", 42);
halcanary56586b62016-07-25 13:59:30 -0700292 assert_emit_eq(reporter, *dict, "<</n1 42>>");
halcanaryf7a169e2015-05-01 07:35:45 -0700293
294 dict->insertScalar("n2", SK_ScalarHalf);
295
halcanary130444f2015-04-25 06:45:07 -0700296 SkString n3("n3");
halcanary8103a342016-03-08 15:10:16 -0800297 sk_sp<SkPDFArray> innerArray(new SkPDFArray);
halcanaryf7a169e2015-05-01 07:35:45 -0700298 innerArray->appendInt(-100);
halcanary8103a342016-03-08 15:10:16 -0800299 dict->insertObject(n3, std::move(innerArray));
halcanary56586b62016-07-25 13:59:30 -0700300 assert_emit_eq(reporter, *dict, "<</n1 42\n/n2 .5\n/n3 [-100]>>");
vandebo@chromium.org8459d4e2010-09-24 22:25:30 +0000301
halcanaryf7a169e2015-05-01 07:35:45 -0700302 dict.reset(new SkPDFDict);
halcanary56586b62016-07-25 13:59:30 -0700303 assert_emit_eq(reporter, *dict, "<<>>");
halcanaryf7a169e2015-05-01 07:35:45 -0700304
305 dict->insertInt("n1", 24);
halcanary56586b62016-07-25 13:59:30 -0700306 assert_emit_eq(reporter, *dict, "<</n1 24>>");
halcanaryf7a169e2015-05-01 07:35:45 -0700307
308 dict->insertInt("n2", SkToSizeT(99));
halcanary56586b62016-07-25 13:59:30 -0700309 assert_emit_eq(reporter, *dict, "<</n1 24\n/n2 99>>");
halcanaryf7a169e2015-05-01 07:35:45 -0700310
311 dict->insertScalar("n3", SK_ScalarHalf);
halcanary56586b62016-07-25 13:59:30 -0700312 assert_emit_eq(reporter, *dict, "<</n1 24\n/n2 99\n/n3 .5>>");
halcanaryf7a169e2015-05-01 07:35:45 -0700313
314 dict->insertName("n4", "AName");
halcanary56586b62016-07-25 13:59:30 -0700315 assert_emit_eq(reporter, *dict, "<</n1 24\n/n2 99\n/n3 .5\n/n4 /AName>>");
halcanaryf7a169e2015-05-01 07:35:45 -0700316
317 dict->insertName("n5", SkString("AnotherName"));
halcanary56586b62016-07-25 13:59:30 -0700318 assert_emit_eq(reporter, *dict, "<</n1 24\n/n2 99\n/n3 .5\n/n4 /AName\n"
halcanaryf7a169e2015-05-01 07:35:45 -0700319 "/n5 /AnotherName>>");
320
321 dict->insertString("n6", "A String");
halcanary56586b62016-07-25 13:59:30 -0700322 assert_emit_eq(reporter, *dict, "<</n1 24\n/n2 99\n/n3 .5\n/n4 /AName\n"
halcanaryf7a169e2015-05-01 07:35:45 -0700323 "/n5 /AnotherName\n/n6 (A String)>>");
324
325 dict->insertString("n7", SkString("Another String"));
halcanary56586b62016-07-25 13:59:30 -0700326 assert_emit_eq(reporter, *dict, "<</n1 24\n/n2 99\n/n3 .5\n/n4 /AName\n"
halcanaryf7a169e2015-05-01 07:35:45 -0700327 "/n5 /AnotherName\n/n6 (A String)\n/n7 (Another String)>>");
328
329 dict.reset(new SkPDFDict("DType"));
halcanary56586b62016-07-25 13:59:30 -0700330 assert_emit_eq(reporter, *dict, "<</Type /DType>>");
halcanary8e9f5e32016-02-24 15:46:46 -0800331
halcanary8103a342016-03-08 15:10:16 -0800332 sk_sp<SkPDFArray> referencedArray(new SkPDFArray);
halcanary530032a2016-08-18 14:22:52 -0700333 SkPDFObjNumMap catalog;
Hal Canaryc5b94982017-10-03 09:38:43 -0400334 catalog.addObjectRecursively(referencedArray.get());
halcanary530032a2016-08-18 14:22:52 -0700335 REPORTER_ASSERT(reporter, catalog.getObjectNumber(
halcanaryf7a169e2015-05-01 07:35:45 -0700336 referencedArray.get()) == 1);
halcanary8103a342016-03-08 15:10:16 -0800337 dict->insertObjRef("n1", std::move(referencedArray));
halcanaryf7a169e2015-05-01 07:35:45 -0700338 SkString result = emit_to_string(*dict, &catalog);
halcanary56586b62016-07-25 13:59:30 -0700339 assert_eq(reporter, result, "<</Type /DType\n/n1 1 0 R>>");
halcanaryf7a169e2015-05-01 07:35:45 -0700340}
341
halcanary57f744e2016-09-09 11:41:59 -0700342DEF_TEST(SkPDF_Primitives, reporter) {
halcanaryf7a169e2015-05-01 07:35:45 -0700343 TestPDFUnion(reporter);
344 TestPDFArray(reporter);
345 TestPDFDict(reporter);
vandebo@chromium.org421d6442011-07-20 17:39:01 +0000346 TestPDFStream(reporter);
halcanaryf7a169e2015-05-01 07:35:45 -0700347 TestObjectNumberMap(reporter);
vandebo@chromium.org8459d4e2010-09-24 22:25:30 +0000348 TestObjectRef(reporter);
sugoi@google.com54f0d1b2013-02-27 19:17:41 +0000349 test_issue1083();
vandebo@chromium.org8459d4e2010-09-24 22:25:30 +0000350}
senorblancob0e89dc2014-10-20 14:03:12 -0700351
352namespace {
353
354class DummyImageFilter : public SkImageFilter {
355public:
robertphillips43c2ad42016-04-04 05:05:11 -0700356 static sk_sp<DummyImageFilter> Make(bool visited = false) {
357 return sk_sp<DummyImageFilter>(new DummyImageFilter(visited));
358 }
359
Cary Clark99885412018-04-05 13:09:58 -0400360 void toString(SkString* str) const override;
robertphillips43c2ad42016-04-04 05:05:11 -0700361 SK_DECLARE_PUBLIC_FLATTENABLE_DESERIALIZATION_PROCS(DummyImageFilter)
362 bool visited() const { return fVisited; }
363
364protected:
robertphillips4ba94e22016-04-04 12:07:47 -0700365 sk_sp<SkSpecialImage> onFilterImage(SkSpecialImage* source, const Context&,
366 SkIPoint* offset) const override {
senorblancob0e89dc2014-10-20 14:03:12 -0700367 fVisited = true;
368 offset->fX = offset->fY = 0;
robertphillips4ba94e22016-04-04 12:07:47 -0700369 return sk_ref_sp<SkSpecialImage>(source);
senorblancob0e89dc2014-10-20 14:03:12 -0700370 }
Matt Sarett62745a82017-04-17 11:57:29 -0400371 sk_sp<SkImageFilter> onMakeColorSpace(SkColorSpaceXformer*) const override {
372 return sk_ref_sp(const_cast<DummyImageFilter*>(this));
373 }
senorblancob0e89dc2014-10-20 14:03:12 -0700374
375private:
robertphillips43c2ad42016-04-04 05:05:11 -0700376 DummyImageFilter(bool visited) : INHERITED(nullptr, 0, nullptr), fVisited(visited) {}
377
senorblancob0e89dc2014-10-20 14:03:12 -0700378 mutable bool fVisited;
robertphillips43c2ad42016-04-04 05:05:11 -0700379
380 typedef SkImageFilter INHERITED;
senorblancob0e89dc2014-10-20 14:03:12 -0700381};
382
reed60c9b582016-04-03 09:11:13 -0700383sk_sp<SkFlattenable> DummyImageFilter::CreateProc(SkReadBuffer& buffer) {
senorblancob0e89dc2014-10-20 14:03:12 -0700384 SK_IMAGEFILTER_UNFLATTEN_COMMON(common, 0);
385 bool visited = buffer.readBool();
robertphillips43c2ad42016-04-04 05:05:11 -0700386 return DummyImageFilter::Make(visited);
senorblancob0e89dc2014-10-20 14:03:12 -0700387}
388
robertphillipsf3f5bad2014-12-19 13:49:15 -0800389void DummyImageFilter::toString(SkString* str) const {
390 str->appendf("DummyImageFilter: (");
391 str->append(")");
392}
robertphillipsf3f5bad2014-12-19 13:49:15 -0800393
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(
Ben Wagnera93a14a2017-08-28 10:34:05 -0400429 sk_tool_utils::create_portable_typeface(nullptr, 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) {
Hal Canary3c36ef62018-01-02 16:25:53 -0500438 char floatString[kMaximumSkFloatToDecimalLength];
439 size_t len = SkFloatToDecimal(inputFloat, floatString);
halcanary8e9f5e32016-02-24 15:46:46 -0800440 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 Canary1521c8a2018-03-28 09:51:00 -0700495
496DEF_TEST(SkPDF_Clusterator, reporter) {
Hal Canary1521c8a2018-03-28 09:51:00 -0700497 SkPaint paint;
498 paint.setTextEncoding(SkPaint::kGlyphID_TextEncoding);
Hal Canarye9e19dd2018-04-02 11:36:39 -0400499 {
500 const uint32_t clusters[11] = { 3, 2, 2, 1, 0, 4, 4, 7, 6, 6, 5 };
501 const SkGlyphID glyphs[11] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11};
502 const char text[] = "abcdefgh";
503 SkClusterator clusterator(glyphs, sizeof(glyphs), paint, clusters, strlen(text), text);
504 SkClusterator::Cluster expectations[] = {
505 {&text[3], 1, 0, 1},
506 {&text[2], 1, 1, 2},
507 {&text[1], 1, 3, 1},
508 {&text[0], 1, 4, 1},
509 {&text[4], 1, 5, 2},
510 {&text[7], 1, 7, 1},
511 {&text[6], 1, 8, 2},
512 {&text[5], 1, 10, 1},
513 {nullptr, 0, 0, 0},
514 };
515 for (const auto& expectation : expectations) {
516 REPORTER_ASSERT(reporter, clusterator.next() == expectation);
517 }
518 }
519 {
520 const uint32_t clusters[5] = { 0, 1, 4, 5, 6 };
521 const SkGlyphID glyphs[5] = { 43, 167, 79, 79, 82, };
522 const char text[] = "Ha\xCC\x8A" "llo";
523 SkClusterator clusterator(glyphs, sizeof(glyphs), paint, clusters, strlen(text), text);
524 SkClusterator::Cluster expectations[] = {
525 {&text[0], 1, 0, 1},
526 {&text[1], 3, 1, 1},
527 {&text[4], 1, 2, 1},
528 {&text[5], 1, 3, 1},
529 {&text[6], 1, 4, 1},
530 {nullptr, 0, 0, 0},
531 };
532 for (const auto& expectation : expectations) {
533 REPORTER_ASSERT(reporter, clusterator.next() == expectation);
534 }
Hal Canary1521c8a2018-03-28 09:51:00 -0700535 }
536}
537
Hal Canary43fb7a02016-12-30 13:09:03 -0500538#endif