blob: a9214ff948595bb00d5cf2733bdc58b328e8db6c [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 Canaryc640d0d2018-06-13 09:59:02 -040018#include "SkDocument.h"
Hal Canary98caedd2018-07-23 10:50:49 -040019#include "SkGlyphRun.h"
edisonn@google.comd9dfa182013-04-24 13:01:01 +000020#include "SkImageEncoder.h"
Cary Clark60aaeb22017-11-03 08:06:09 -040021#include "SkImageFilterPriv.h"
halcanary022c2bd2016-09-02 11:29:46 -070022#include "SkMakeUnique.h"
edisonn@google.comd9dfa182013-04-24 13:01:01 +000023#include "SkMatrix.h"
halcanarya1f1ee92015-02-20 06:17:26 -080024#include "SkPDFCanon.h"
vandebo@chromium.org4e1cc6a2013-01-25 19:27:23 +000025#include "SkPDFDevice.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>
halcanary530032a2016-08-18 14:22:52 -070043static SkString emit_to_string(T& obj, SkPDFObjNumMap* catPtr = nullptr) {
44 SkPDFObjNumMap catalog;
halcanary6a144342015-01-23 11:45:10 -080045 SkDynamicMemoryWStream buffer;
halcanaryf7a169e2015-05-01 07:35:45 -070046 if (!catPtr) {
47 catPtr = &catalog;
halcanary130444f2015-04-25 06:45:07 -070048 }
halcanary530032a2016-08-18 14:22:52 -070049 obj.emitObject(&buffer, *catPtr);
halcanary56586b62016-07-25 13:59:30 -070050 SkString tmp(buffer.bytesWritten());
51 buffer.copyTo(tmp.writable_str());
halcanaryf7a169e2015-05-01 07:35:45 -070052 return tmp;
vandebo@chromium.org8459d4e2010-09-24 22:25:30 +000053}
54
halcanaryf7a169e2015-05-01 07:35:45 -070055static bool eq(const SkString& str, const char* strPtr, size_t len) {
56 return len == str.size() && 0 == memcmp(str.c_str(), strPtr, len);
vandebo@chromium.org421d6442011-07-20 17:39:01 +000057}
58
halcanary56586b62016-07-25 13:59:30 -070059static void assert_eql(skiatest::Reporter* reporter,
60 const SkString& skString,
61 const char* str,
62 size_t len) {
63 if (!eq(skString, str, len)) {
64 REPORT_FAILURE(reporter, "", SkStringPrintf(
65 "'%*s' != '%s'", len, str, skString.c_str()));
66 }
67}
halcanaryf7a169e2015-05-01 07:35:45 -070068
halcanary56586b62016-07-25 13:59:30 -070069static void assert_eq(skiatest::Reporter* reporter,
70 const SkString& skString,
71 const char* str) {
72 assert_eql(reporter, skString, str, strlen(str));
73}
halcanaryf7a169e2015-05-01 07:35:45 -070074
75
halcanary56586b62016-07-25 13:59:30 -070076template <typename T>
77static void assert_emit_eq(skiatest::Reporter* reporter,
78 T& object,
79 const char* string) {
80 SkString result = emit_to_string(object);
81 assert_eq(reporter, result, string);
82}
halcanaryf7a169e2015-05-01 07:35:45 -070083
vandebo@chromium.org421d6442011-07-20 17:39:01 +000084static void TestPDFStream(skiatest::Reporter* reporter) {
85 char streamBytes[] = "Test\nFoo\tBar";
halcanary022c2bd2016-09-02 11:29:46 -070086 auto streamData = skstd::make_unique<SkMemoryStream>(
87 streamBytes, strlen(streamBytes), true);
halcanaryac0e00d2016-07-27 11:12:23 -070088 auto stream = sk_make_sp<SkPDFStream>(std::move(streamData));
halcanary56586b62016-07-25 13:59:30 -070089 assert_emit_eq(reporter,
halcanaryf7a169e2015-05-01 07:35:45 -070090 *stream,
91 "<</Length 12>> stream\nTest\nFoo\tBar\nendstream");
halcanaryfa251062016-07-29 10:13:18 -070092 stream->dict()->insertInt("Attribute", 42);
halcanary56586b62016-07-25 13:59:30 -070093 assert_emit_eq(reporter,
halcanaryf7a169e2015-05-01 07:35:45 -070094 *stream,
95 "<</Length 12\n/Attribute 42>> stream\n"
96 "Test\nFoo\tBar\nendstream");
vandebo@chromium.org421d6442011-07-20 17:39:01 +000097
halcanary91d1d622015-02-17 14:43:06 -080098 {
vandebo@chromium.org421d6442011-07-20 17:39:01 +000099 char streamBytes2[] = "This is a longer string, so that compression "
100 "can do something with it. With shorter strings, "
101 "the short circuit logic cuts in and we end up "
102 "with an uncompressed string.";
halcanaryfe8f0e02016-07-27 14:14:04 -0700103 auto stream = sk_make_sp<SkPDFStream>(
104 SkData::MakeWithCopy(streamBytes2, strlen(streamBytes2)));
vandebo@chromium.org421d6442011-07-20 17:39:01 +0000105
106 SkDynamicMemoryWStream compressedByteStream;
halcanary48305e82015-08-18 13:30:25 -0700107 SkDeflateWStream deflateWStream(&compressedByteStream);
108 deflateWStream.write(streamBytes2, strlen(streamBytes2));
109 deflateWStream.finalize();
vandebo@chromium.org421d6442011-07-20 17:39:01 +0000110
mtkleincabc08c2015-02-19 08:29:24 -0800111 SkDynamicMemoryWStream expected;
halcanary37c46ca2015-03-31 12:30:20 -0700112 expected.writeText("<</Filter /FlateDecode\n/Length 116>> stream\n");
halcanary48305e82015-08-18 13:30:25 -0700113 compressedByteStream.writeToStream(&expected);
114 compressedByteStream.reset();
mtkleincabc08c2015-02-19 08:29:24 -0800115 expected.writeText("\nendstream");
reed42943c82016-09-12 12:01:44 -0700116 sk_sp<SkData> expectedResultData2(expected.detachAsData());
halcanaryf7a169e2015-05-01 07:35:45 -0700117 SkString result = emit_to_string(*stream);
halcanary18e0cbc2016-08-03 14:17:53 -0700118 #ifndef SK_PDF_LESS_COMPRESSION
halcanary56586b62016-07-25 13:59:30 -0700119 assert_eql(reporter,
halcanaryf7a169e2015-05-01 07:35:45 -0700120 result,
121 (const char*)expectedResultData2->data(),
122 expectedResultData2->size());
halcanary18e0cbc2016-08-03 14:17:53 -0700123 #endif
vandebo@chromium.org421d6442011-07-20 17:39:01 +0000124 }
125}
126
halcanaryf7a169e2015-05-01 07:35:45 -0700127static void TestObjectNumberMap(skiatest::Reporter* reporter) {
128 SkPDFObjNumMap objNumMap;
halcanary8103a342016-03-08 15:10:16 -0800129 sk_sp<SkPDFArray> a1(new SkPDFArray);
130 sk_sp<SkPDFArray> a2(new SkPDFArray);
131 sk_sp<SkPDFArray> a3(new SkPDFArray);
vandebo@chromium.org8459d4e2010-09-24 22:25:30 +0000132
Hal Canaryc5b94982017-10-03 09:38:43 -0400133 objNumMap.addObjectRecursively(a1.get());
134 objNumMap.addObjectRecursively(a2.get());
135 objNumMap.addObjectRecursively(a3.get());
vandebo@chromium.org8459d4e2010-09-24 22:25:30 +0000136
halcanaryf7a169e2015-05-01 07:35:45 -0700137 // The objects should be numbered in the order they are added,
138 // starting with 1.
139 REPORTER_ASSERT(reporter, objNumMap.getObjectNumber(a1.get()) == 1);
140 REPORTER_ASSERT(reporter, objNumMap.getObjectNumber(a2.get()) == 2);
141 REPORTER_ASSERT(reporter, objNumMap.getObjectNumber(a3.get()) == 3);
142 // Assert that repeated calls to get the object number return
143 // consistent result.
144 REPORTER_ASSERT(reporter, objNumMap.getObjectNumber(a1.get()) == 1);
vandebo@chromium.org8459d4e2010-09-24 22:25:30 +0000145}
146
147static void TestObjectRef(skiatest::Reporter* reporter) {
halcanary8103a342016-03-08 15:10:16 -0800148 sk_sp<SkPDFArray> a1(new SkPDFArray);
149 sk_sp<SkPDFArray> a2(new SkPDFArray);
150 a2->appendObjRef(a1);
vandebo@chromium.org8459d4e2010-09-24 22:25:30 +0000151
halcanary530032a2016-08-18 14:22:52 -0700152 SkPDFObjNumMap catalog;
Hal Canaryc5b94982017-10-03 09:38:43 -0400153 catalog.addObjectRecursively(a1.get());
halcanary530032a2016-08-18 14:22:52 -0700154 REPORTER_ASSERT(reporter, catalog.getObjectNumber(a1.get()) == 1);
vandebo@chromium.org8459d4e2010-09-24 22:25:30 +0000155
halcanaryf7a169e2015-05-01 07:35:45 -0700156 SkString result = emit_to_string(*a2, &catalog);
157 // If appendObjRef misbehaves, then the result would
158 // be [[]], not [1 0 R].
halcanary56586b62016-07-25 13:59:30 -0700159 assert_eq(reporter, result, "[1 0 R]");
vandebo@chromium.org8459d4e2010-09-24 22:25:30 +0000160}
161
vandebo@chromium.org4e1cc6a2013-01-25 19:27:23 +0000162// This test used to assert without the fix submitted for
163// http://code.google.com/p/skia/issues/detail?id=1083.
164// SKP files might have invalid glyph ids. This test ensures they are ignored,
165// and there is no assert on input data in Debug mode.
sugoi@google.com54f0d1b2013-02-27 19:17:41 +0000166static void test_issue1083() {
halcanary3d32d502015-03-01 06:55:20 -0800167 SkDynamicMemoryWStream outStream;
halcanary4b656662016-04-27 07:45:18 -0700168 sk_sp<SkDocument> doc(SkDocument::MakePDF(&outStream));
halcanary3d32d502015-03-01 06:55:20 -0800169 SkCanvas* canvas = doc->beginPage(100.0f, 100.0f);
vandebo@chromium.org4e1cc6a2013-01-25 19:27:23 +0000170 SkPaint paint;
171 paint.setTextEncoding(SkPaint::kGlyphID_TextEncoding);
172
173 uint16_t glyphID = 65000;
halcanary3d32d502015-03-01 06:55:20 -0800174 canvas->drawText(&glyphID, 2, 0, 0, paint);
vandebo@chromium.org4e1cc6a2013-01-25 19:27:23 +0000175
halcanary3d32d502015-03-01 06:55:20 -0800176 doc->close();
vandebo@chromium.org4e1cc6a2013-01-25 19:27:23 +0000177}
178
halcanary56586b62016-07-25 13:59:30 -0700179static void assert_emit_eq_number(skiatest::Reporter* reporter, float number) {
180 SkPDFUnion pdfUnion = SkPDFUnion::Scalar(number);
181 SkString result = emit_to_string(pdfUnion);
182 float value = static_cast<float>(std::atof(result.c_str()));
183 if (value != number) {
184 ERRORF(reporter, "%.9g != %s", number, result.c_str());
185 }
186}
187
188
halcanaryf7a169e2015-05-01 07:35:45 -0700189static void TestPDFUnion(skiatest::Reporter* reporter) {
190 SkPDFUnion boolTrue = SkPDFUnion::Bool(true);
halcanary56586b62016-07-25 13:59:30 -0700191 assert_emit_eq(reporter, boolTrue, "true");
vandebo@chromium.org8459d4e2010-09-24 22:25:30 +0000192
halcanaryf7a169e2015-05-01 07:35:45 -0700193 SkPDFUnion boolFalse = SkPDFUnion::Bool(false);
halcanary56586b62016-07-25 13:59:30 -0700194 assert_emit_eq(reporter, boolFalse, "false");
vandebo@chromium.org8459d4e2010-09-24 22:25:30 +0000195
halcanaryf7a169e2015-05-01 07:35:45 -0700196 SkPDFUnion int42 = SkPDFUnion::Int(42);
halcanary56586b62016-07-25 13:59:30 -0700197 assert_emit_eq(reporter, int42, "42");
halcanaryf7a169e2015-05-01 07:35:45 -0700198
halcanary56586b62016-07-25 13:59:30 -0700199 assert_emit_eq_number(reporter, SK_ScalarHalf);
200 assert_emit_eq_number(reporter, 110999.75f); // bigScalar
201 assert_emit_eq_number(reporter, 50000000.1f); // biggerScalar
202 assert_emit_eq_number(reporter, 1.0f / 65536); // smallScalar
vandebo@chromium.org094316b2011-03-04 03:15:13 +0000203
halcanaryf7a169e2015-05-01 07:35:45 -0700204 SkPDFUnion stringSimple = SkPDFUnion::String("test ) string ( foo");
halcanary56586b62016-07-25 13:59:30 -0700205 assert_emit_eq(reporter, stringSimple, "(test \\) string \\( foo)");
vandebo@chromium.org8459d4e2010-09-24 22:25:30 +0000206
halcanaryf7a169e2015-05-01 07:35:45 -0700207 SkString stringComplexInput("\ttest ) string ( foo");
208 SkPDFUnion stringComplex = SkPDFUnion::String(stringComplexInput);
halcanary56586b62016-07-25 13:59:30 -0700209 assert_emit_eq(reporter, stringComplex, "(\\011test \\) string \\( foo)");
halcanaryee41b752016-06-23 14:08:11 -0700210
211 SkString binaryStringInput("\1\2\3\4\5\6\7\10\11\12\13\14\15\16\17\20");
212 SkPDFUnion binaryString = SkPDFUnion::String(binaryStringInput);
halcanary56586b62016-07-25 13:59:30 -0700213 assert_emit_eq(reporter, binaryString, "<0102030405060708090A0B0C0D0E0F10>");
vandebo@chromium.org8459d4e2010-09-24 22:25:30 +0000214
halcanaryf7a169e2015-05-01 07:35:45 -0700215 SkString nameInput("Test name\twith#tab");
216 SkPDFUnion name = SkPDFUnion::Name(nameInput);
halcanary56586b62016-07-25 13:59:30 -0700217 assert_emit_eq(reporter, name, "/Test#20name#09with#23tab");
halcanaryf7a169e2015-05-01 07:35:45 -0700218
219 SkString nameInput2("A#/%()<>[]{}B");
220 SkPDFUnion name2 = SkPDFUnion::Name(nameInput2);
halcanary56586b62016-07-25 13:59:30 -0700221 assert_emit_eq(reporter, name2, "/A#23#2F#25#28#29#3C#3E#5B#5D#7B#7DB");
halcanaryf7a169e2015-05-01 07:35:45 -0700222
223 SkPDFUnion name3 = SkPDFUnion::Name("SimpleNameWithOnlyPrintableASCII");
halcanary56586b62016-07-25 13:59:30 -0700224 assert_emit_eq(reporter, name3, "/SimpleNameWithOnlyPrintableASCII");
vandebo@chromium.org251a7662012-09-21 17:50:50 +0000225
vandebo@chromium.orgc0376fe2012-03-05 18:44:33 +0000226 // Test that we correctly handle characters with the high-bit set.
halcanaryf7a169e2015-05-01 07:35:45 -0700227 SkString highBitString("\xDE\xAD" "be\xEF");
228 SkPDFUnion highBitName = SkPDFUnion::Name(highBitString);
halcanary56586b62016-07-25 13:59:30 -0700229 assert_emit_eq(reporter, highBitName, "/#DE#ADbe#EF");
halcanaryf7a169e2015-05-01 07:35:45 -0700230}
vandebo@chromium.orgc0376fe2012-03-05 18:44:33 +0000231
halcanaryf7a169e2015-05-01 07:35:45 -0700232static void TestPDFArray(skiatest::Reporter* reporter) {
halcanary8103a342016-03-08 15:10:16 -0800233 sk_sp<SkPDFArray> array(new SkPDFArray);
halcanary56586b62016-07-25 13:59:30 -0700234 assert_emit_eq(reporter, *array, "[]");
vandebo@chromium.org8459d4e2010-09-24 22:25:30 +0000235
halcanaryf7a169e2015-05-01 07:35:45 -0700236 array->appendInt(42);
halcanary56586b62016-07-25 13:59:30 -0700237 assert_emit_eq(reporter, *array, "[42]");
halcanaryf7a169e2015-05-01 07:35:45 -0700238
239 array->appendScalar(SK_ScalarHalf);
halcanary56586b62016-07-25 13:59:30 -0700240 assert_emit_eq(reporter, *array, "[42 .5]");
halcanaryf7a169e2015-05-01 07:35:45 -0700241
242 array->appendInt(0);
halcanary56586b62016-07-25 13:59:30 -0700243 assert_emit_eq(reporter, *array, "[42 .5 0]");
halcanaryf7a169e2015-05-01 07:35:45 -0700244
245 array->appendBool(true);
halcanary56586b62016-07-25 13:59:30 -0700246 assert_emit_eq(reporter, *array, "[42 .5 0 true]");
halcanaryf7a169e2015-05-01 07:35:45 -0700247
248 array->appendName("ThisName");
halcanary56586b62016-07-25 13:59:30 -0700249 assert_emit_eq(reporter, *array, "[42 .5 0 true /ThisName]");
halcanaryf7a169e2015-05-01 07:35:45 -0700250
251 array->appendName(SkString("AnotherName"));
halcanary56586b62016-07-25 13:59:30 -0700252 assert_emit_eq(reporter, *array, "[42 .5 0 true /ThisName /AnotherName]");
halcanaryf7a169e2015-05-01 07:35:45 -0700253
254 array->appendString("This 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
258 array->appendString(SkString("Another String"));
halcanary56586b62016-07-25 13:59:30 -0700259 assert_emit_eq(reporter, *array,
halcanary8e9f5e32016-02-24 15:46:46 -0800260 "[42 .5 0 true /ThisName /AnotherName (This String) "
halcanaryf7a169e2015-05-01 07:35:45 -0700261 "(Another String)]");
262
halcanary8103a342016-03-08 15:10:16 -0800263 sk_sp<SkPDFArray> innerArray(new SkPDFArray);
halcanaryf7a169e2015-05-01 07:35:45 -0700264 innerArray->appendInt(-1);
halcanary8103a342016-03-08 15:10:16 -0800265 array->appendObject(std::move(innerArray));
halcanary56586b62016-07-25 13:59:30 -0700266 assert_emit_eq(reporter, *array,
halcanary8e9f5e32016-02-24 15:46:46 -0800267 "[42 .5 0 true /ThisName /AnotherName (This String) "
halcanaryf7a169e2015-05-01 07:35:45 -0700268 "(Another String) [-1]]");
269
halcanary8103a342016-03-08 15:10:16 -0800270 sk_sp<SkPDFArray> referencedArray(new SkPDFArray);
halcanary530032a2016-08-18 14:22:52 -0700271 SkPDFObjNumMap catalog;
Hal Canaryc5b94982017-10-03 09:38:43 -0400272 catalog.addObjectRecursively(referencedArray.get());
halcanary530032a2016-08-18 14:22:52 -0700273 REPORTER_ASSERT(reporter, catalog.getObjectNumber(
halcanaryf7a169e2015-05-01 07:35:45 -0700274 referencedArray.get()) == 1);
halcanary8103a342016-03-08 15:10:16 -0800275 array->appendObjRef(std::move(referencedArray));
halcanaryf7a169e2015-05-01 07:35:45 -0700276
277 SkString result = emit_to_string(*array, &catalog);
halcanary56586b62016-07-25 13:59:30 -0700278 assert_eq(reporter, result,
halcanary8e9f5e32016-02-24 15:46:46 -0800279 "[42 .5 0 true /ThisName /AnotherName (This String) "
halcanaryf7a169e2015-05-01 07:35:45 -0700280 "(Another String) [-1] 1 0 R]");
281}
282
283static void TestPDFDict(skiatest::Reporter* reporter) {
halcanary8103a342016-03-08 15:10:16 -0800284 sk_sp<SkPDFDict> dict(new SkPDFDict);
halcanary56586b62016-07-25 13:59:30 -0700285 assert_emit_eq(reporter, *dict, "<<>>");
halcanaryf7a169e2015-05-01 07:35:45 -0700286
287 dict->insertInt("n1", SkToSizeT(42));
halcanary56586b62016-07-25 13:59:30 -0700288 assert_emit_eq(reporter, *dict, "<</n1 42>>");
halcanaryf7a169e2015-05-01 07:35:45 -0700289
290 dict.reset(new SkPDFDict);
halcanary56586b62016-07-25 13:59:30 -0700291 assert_emit_eq(reporter, *dict, "<<>>");
halcanaryf7a169e2015-05-01 07:35:45 -0700292
293 dict->insertInt("n1", 42);
halcanary56586b62016-07-25 13:59:30 -0700294 assert_emit_eq(reporter, *dict, "<</n1 42>>");
halcanaryf7a169e2015-05-01 07:35:45 -0700295
296 dict->insertScalar("n2", SK_ScalarHalf);
297
halcanary130444f2015-04-25 06:45:07 -0700298 SkString n3("n3");
halcanary8103a342016-03-08 15:10:16 -0800299 sk_sp<SkPDFArray> innerArray(new SkPDFArray);
halcanaryf7a169e2015-05-01 07:35:45 -0700300 innerArray->appendInt(-100);
halcanary8103a342016-03-08 15:10:16 -0800301 dict->insertObject(n3, std::move(innerArray));
halcanary56586b62016-07-25 13:59:30 -0700302 assert_emit_eq(reporter, *dict, "<</n1 42\n/n2 .5\n/n3 [-100]>>");
vandebo@chromium.org8459d4e2010-09-24 22:25:30 +0000303
halcanaryf7a169e2015-05-01 07:35:45 -0700304 dict.reset(new SkPDFDict);
halcanary56586b62016-07-25 13:59:30 -0700305 assert_emit_eq(reporter, *dict, "<<>>");
halcanaryf7a169e2015-05-01 07:35:45 -0700306
307 dict->insertInt("n1", 24);
halcanary56586b62016-07-25 13:59:30 -0700308 assert_emit_eq(reporter, *dict, "<</n1 24>>");
halcanaryf7a169e2015-05-01 07:35:45 -0700309
310 dict->insertInt("n2", SkToSizeT(99));
halcanary56586b62016-07-25 13:59:30 -0700311 assert_emit_eq(reporter, *dict, "<</n1 24\n/n2 99>>");
halcanaryf7a169e2015-05-01 07:35:45 -0700312
313 dict->insertScalar("n3", SK_ScalarHalf);
halcanary56586b62016-07-25 13:59:30 -0700314 assert_emit_eq(reporter, *dict, "<</n1 24\n/n2 99\n/n3 .5>>");
halcanaryf7a169e2015-05-01 07:35:45 -0700315
316 dict->insertName("n4", "AName");
halcanary56586b62016-07-25 13:59:30 -0700317 assert_emit_eq(reporter, *dict, "<</n1 24\n/n2 99\n/n3 .5\n/n4 /AName>>");
halcanaryf7a169e2015-05-01 07:35:45 -0700318
319 dict->insertName("n5", SkString("AnotherName"));
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>>");
322
323 dict->insertString("n6", "A 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)>>");
326
327 dict->insertString("n7", SkString("Another String"));
halcanary56586b62016-07-25 13:59:30 -0700328 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\n/n6 (A String)\n/n7 (Another String)>>");
330
331 dict.reset(new SkPDFDict("DType"));
halcanary56586b62016-07-25 13:59:30 -0700332 assert_emit_eq(reporter, *dict, "<</Type /DType>>");
halcanary8e9f5e32016-02-24 15:46:46 -0800333
halcanary8103a342016-03-08 15:10:16 -0800334 sk_sp<SkPDFArray> referencedArray(new SkPDFArray);
halcanary530032a2016-08-18 14:22:52 -0700335 SkPDFObjNumMap catalog;
Hal Canaryc5b94982017-10-03 09:38:43 -0400336 catalog.addObjectRecursively(referencedArray.get());
halcanary530032a2016-08-18 14:22:52 -0700337 REPORTER_ASSERT(reporter, catalog.getObjectNumber(
halcanaryf7a169e2015-05-01 07:35:45 -0700338 referencedArray.get()) == 1);
halcanary8103a342016-03-08 15:10:16 -0800339 dict->insertObjRef("n1", std::move(referencedArray));
halcanaryf7a169e2015-05-01 07:35:45 -0700340 SkString result = emit_to_string(*dict, &catalog);
halcanary56586b62016-07-25 13:59:30 -0700341 assert_eq(reporter, result, "<</Type /DType\n/n1 1 0 R>>");
halcanaryf7a169e2015-05-01 07:35:45 -0700342}
343
halcanary57f744e2016-09-09 11:41:59 -0700344DEF_TEST(SkPDF_Primitives, reporter) {
halcanaryf7a169e2015-05-01 07:35:45 -0700345 TestPDFUnion(reporter);
346 TestPDFArray(reporter);
347 TestPDFDict(reporter);
vandebo@chromium.org421d6442011-07-20 17:39:01 +0000348 TestPDFStream(reporter);
halcanaryf7a169e2015-05-01 07:35:45 -0700349 TestObjectNumberMap(reporter);
vandebo@chromium.org8459d4e2010-09-24 22:25:30 +0000350 TestObjectRef(reporter);
sugoi@google.com54f0d1b2013-02-27 19:17:41 +0000351 test_issue1083();
vandebo@chromium.org8459d4e2010-09-24 22:25:30 +0000352}
senorblancob0e89dc2014-10-20 14:03:12 -0700353
354namespace {
355
356class DummyImageFilter : public SkImageFilter {
357public:
robertphillips43c2ad42016-04-04 05:05:11 -0700358 static sk_sp<DummyImageFilter> Make(bool visited = false) {
359 return sk_sp<DummyImageFilter>(new DummyImageFilter(visited));
360 }
361
robertphillips43c2ad42016-04-04 05:05:11 -0700362 SK_DECLARE_PUBLIC_FLATTENABLE_DESERIALIZATION_PROCS(DummyImageFilter)
363 bool visited() const { return fVisited; }
364
365protected:
robertphillips4ba94e22016-04-04 12:07:47 -0700366 sk_sp<SkSpecialImage> onFilterImage(SkSpecialImage* source, const Context&,
367 SkIPoint* offset) const override {
senorblancob0e89dc2014-10-20 14:03:12 -0700368 fVisited = true;
369 offset->fX = offset->fY = 0;
robertphillips4ba94e22016-04-04 12:07:47 -0700370 return sk_ref_sp<SkSpecialImage>(source);
senorblancob0e89dc2014-10-20 14:03:12 -0700371 }
Matt Sarett62745a82017-04-17 11:57:29 -0400372 sk_sp<SkImageFilter> onMakeColorSpace(SkColorSpaceXformer*) const override {
373 return sk_ref_sp(const_cast<DummyImageFilter*>(this));
374 }
senorblancob0e89dc2014-10-20 14:03:12 -0700375
376private:
robertphillips43c2ad42016-04-04 05:05:11 -0700377 DummyImageFilter(bool visited) : INHERITED(nullptr, 0, nullptr), fVisited(visited) {}
378
senorblancob0e89dc2014-10-20 14:03:12 -0700379 mutable bool fVisited;
robertphillips43c2ad42016-04-04 05:05:11 -0700380
381 typedef SkImageFilter INHERITED;
senorblancob0e89dc2014-10-20 14:03:12 -0700382};
383
reed60c9b582016-04-03 09:11:13 -0700384sk_sp<SkFlattenable> DummyImageFilter::CreateProc(SkReadBuffer& buffer) {
senorblancob0e89dc2014-10-20 14:03:12 -0700385 SK_IMAGEFILTER_UNFLATTEN_COMMON(common, 0);
386 bool visited = buffer.readBool();
robertphillips43c2ad42016-04-04 05:05:11 -0700387 return DummyImageFilter::Make(visited);
senorblancob0e89dc2014-10-20 14:03:12 -0700388}
389
390};
391
392// Check that PDF rendering of image filters successfully falls back to
393// CPU rasterization.
halcanary57f744e2016-09-09 11:41:59 -0700394DEF_TEST(SkPDF_ImageFilter, reporter) {
395 REQUIRE_PDF_DOCUMENT(SkPDF_ImageFilter, reporter);
halcanary3d32d502015-03-01 06:55:20 -0800396 SkDynamicMemoryWStream stream;
halcanary4b656662016-04-27 07:45:18 -0700397 sk_sp<SkDocument> doc(SkDocument::MakePDF(&stream));
halcanary3d32d502015-03-01 06:55:20 -0800398 SkCanvas* canvas = doc->beginPage(100.0f, 100.0f);
399
robertphillips43c2ad42016-04-04 05:05:11 -0700400 sk_sp<DummyImageFilter> filter(DummyImageFilter::Make());
senorblancob0e89dc2014-10-20 14:03:12 -0700401
402 // Filter just created; should be unvisited.
403 REPORTER_ASSERT(reporter, !filter->visited());
404 SkPaint paint;
Mike Reed5e257172016-11-01 11:22:05 -0400405 paint.setImageFilter(filter);
halcanary3d32d502015-03-01 06:55:20 -0800406 canvas->drawRect(SkRect::MakeWH(100, 100), paint);
407 doc->close();
senorblancob0e89dc2014-10-20 14:03:12 -0700408
409 // Filter was used in rendering; should be visited.
410 REPORTER_ASSERT(reporter, filter->visited());
411}
halcanary66a82f32015-10-12 13:05:04 -0700412
413// Check that PDF rendering of image filters successfully falls back to
414// CPU rasterization.
halcanary57f744e2016-09-09 11:41:59 -0700415DEF_TEST(SkPDF_FontCanEmbedTypeface, reporter) {
halcanary66a82f32015-10-12 13:05:04 -0700416 SkPDFCanon canon;
417
418 const char resource[] = "fonts/Roboto2-Regular_NoEmbed.ttf";
bungeman13b9c952016-05-12 10:09:30 -0700419 sk_sp<SkTypeface> noEmbedTypeface(MakeResourceAsTypeface(resource));
halcanary66a82f32015-10-12 13:05:04 -0700420 if (noEmbedTypeface) {
421 REPORTER_ASSERT(reporter,
halcanary8103a342016-03-08 15:10:16 -0800422 !SkPDFFont::CanEmbedTypeface(noEmbedTypeface.get(), &canon));
halcanary66a82f32015-10-12 13:05:04 -0700423 }
halcanary8103a342016-03-08 15:10:16 -0800424 sk_sp<SkTypeface> portableTypeface(
Ben Wagnera93a14a2017-08-28 10:34:05 -0400425 sk_tool_utils::create_portable_typeface(nullptr, SkFontStyle()));
halcanary66a82f32015-10-12 13:05:04 -0700426 REPORTER_ASSERT(reporter,
halcanary8103a342016-03-08 15:10:16 -0800427 SkPDFFont::CanEmbedTypeface(portableTypeface.get(), &canon));
halcanary66a82f32015-10-12 13:05:04 -0700428}
halcanary8e9f5e32016-02-24 15:46:46 -0800429
430
431// test to see that all finite scalars round trip via scanf().
432static void check_pdf_scalar_serialization(
433 skiatest::Reporter* reporter, float inputFloat) {
Hal Canary3c36ef62018-01-02 16:25:53 -0500434 char floatString[kMaximumSkFloatToDecimalLength];
435 size_t len = SkFloatToDecimal(inputFloat, floatString);
halcanary8e9f5e32016-02-24 15:46:46 -0800436 if (len >= sizeof(floatString)) {
437 ERRORF(reporter, "string too long: %u", (unsigned)len);
438 return;
439 }
440 if (floatString[len] != '\0' || strlen(floatString) != len) {
441 ERRORF(reporter, "terminator misplaced.");
442 return; // The terminator is needed for sscanf().
443 }
444 if (reporter->verbose()) {
445 SkDebugf("%15.9g = \"%s\"\n", inputFloat, floatString);
446 }
447 float roundTripFloat;
448 if (1 != sscanf(floatString, "%f", &roundTripFloat)) {
449 ERRORF(reporter, "unscannable result: %s", floatString);
450 return;
451 }
halcanary56586b62016-07-25 13:59:30 -0700452 if (std::isfinite(inputFloat) && roundTripFloat != inputFloat) {
halcanary8e9f5e32016-02-24 15:46:46 -0800453 ERRORF(reporter, "roundTripFloat (%.9g) != inputFloat (%.9g)",
454 roundTripFloat, inputFloat);
455 }
456}
457
458// Test SkPDFUtils::AppendScalar for accuracy.
halcanary57f744e2016-09-09 11:41:59 -0700459DEF_TEST(SkPDF_Primitives_Scalar, reporter) {
halcanary8e9f5e32016-02-24 15:46:46 -0800460 SkRandom random(0x5EED);
461 int iterationCount = 512;
462 while (iterationCount-- > 0) {
463 union { uint32_t u; float f; };
464 u = random.nextU();
465 static_assert(sizeof(float) == sizeof(uint32_t), "");
466 check_pdf_scalar_serialization(reporter, f);
467 }
468 float alwaysCheck[] = {
469 0.0f, -0.0f, 1.0f, -1.0f, SK_ScalarPI, 0.1f, FLT_MIN, FLT_MAX,
470 -FLT_MIN, -FLT_MAX, FLT_MIN / 16.0f, -FLT_MIN / 16.0f,
471 SK_FloatNaN, SK_FloatInfinity, SK_FloatNegativeInfinity,
472 -FLT_MIN / 8388608.0
473 };
474 for (float inputFloat: alwaysCheck) {
475 check_pdf_scalar_serialization(reporter, inputFloat);
476 }
477}
halcanaryeb92cb32016-07-15 13:41:27 -0700478
479// Test SkPDFUtils:: for accuracy.
halcanary57f744e2016-09-09 11:41:59 -0700480DEF_TEST(SkPDF_Primitives_Color, reporter) {
halcanaryeb92cb32016-07-15 13:41:27 -0700481 char buffer[5];
482 for (int i = 0; i < 256; ++i) {
483 size_t len = SkPDFUtils::ColorToDecimal(i, buffer);
484 REPORTER_ASSERT(reporter, len == strlen(buffer));
485 float f;
486 REPORTER_ASSERT(reporter, 1 == sscanf(buffer, "%f", &f));
487 int roundTrip = (int)(0.5 + f * 255);
488 REPORTER_ASSERT(reporter, roundTrip == i);
489 }
490}
Hal Canary1521c8a2018-03-28 09:51:00 -0700491
Herb Derby81ecdbb2018-07-20 11:48:38 -0400492static SkGlyphRun make_run(size_t len, const SkGlyphID* glyphs, SkPoint* pos,
Hal Canary98caedd2018-07-23 10:50:49 -0400493 SkPaint paint, const uint32_t* clusters,
494 size_t utf8TextByteLength, const char* utf8Text) {
495 return SkGlyphRun(std::move(paint),
496 SkSpan<const uint16_t>{}, // No dense indices for now.
Herb Derbyb935cf82018-07-26 16:54:18 -0400497 SkSpan<const SkPoint>{pos, len},
Hal Canary98caedd2018-07-23 10:50:49 -0400498 SkSpan<const SkGlyphID>{glyphs, len},
499 SkSpan<const SkGlyphID>{},
500 SkSpan<const char>{utf8Text, utf8TextByteLength},
501 SkSpan<const uint32_t>{clusters, len});
502}
503
Hal Canary1521c8a2018-03-28 09:51:00 -0700504DEF_TEST(SkPDF_Clusterator, reporter) {
Hal Canary1521c8a2018-03-28 09:51:00 -0700505 SkPaint paint;
506 paint.setTextEncoding(SkPaint::kGlyphID_TextEncoding);
Hal Canarye9e19dd2018-04-02 11:36:39 -0400507 {
Hal Canary98caedd2018-07-23 10:50:49 -0400508 constexpr unsigned len = 11;
509 const uint32_t clusters[len] = { 3, 2, 2, 1, 0, 4, 4, 7, 6, 6, 5 };
510 const SkGlyphID glyphs[len] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11};
Herb Derby81ecdbb2018-07-20 11:48:38 -0400511 SkPoint pos[len] = {{0, 0}, {0, 0}, {0, 0}, {0, 0}, {0, 0}, {0, 0},
Hal Canary98caedd2018-07-23 10:50:49 -0400512 {0, 0}, {0, 0}, {0, 0}, {0, 0}, {0, 0}};
Hal Canarye9e19dd2018-04-02 11:36:39 -0400513 const char text[] = "abcdefgh";
Hal Canary98caedd2018-07-23 10:50:49 -0400514 SkGlyphRun run = make_run(len, glyphs, pos, paint, clusters, strlen(text), text);
515 SkClusterator clusterator(run);
Hal Canarye9e19dd2018-04-02 11:36:39 -0400516 SkClusterator::Cluster expectations[] = {
517 {&text[3], 1, 0, 1},
518 {&text[2], 1, 1, 2},
519 {&text[1], 1, 3, 1},
520 {&text[0], 1, 4, 1},
521 {&text[4], 1, 5, 2},
522 {&text[7], 1, 7, 1},
523 {&text[6], 1, 8, 2},
524 {&text[5], 1, 10, 1},
525 {nullptr, 0, 0, 0},
526 };
527 for (const auto& expectation : expectations) {
528 REPORTER_ASSERT(reporter, clusterator.next() == expectation);
529 }
530 }
531 {
Hal Canary98caedd2018-07-23 10:50:49 -0400532 constexpr unsigned len = 5;
533 const uint32_t clusters[len] = { 0, 1, 4, 5, 6 };
534 const SkGlyphID glyphs[len] = { 43, 167, 79, 79, 82, };
Herb Derby81ecdbb2018-07-20 11:48:38 -0400535 SkPoint pos[len] = {{0, 0}, {0, 0}, {0, 0}, {0, 0}, {0, 0}};
Hal Canarye9e19dd2018-04-02 11:36:39 -0400536 const char text[] = "Ha\xCC\x8A" "llo";
Hal Canary98caedd2018-07-23 10:50:49 -0400537 SkGlyphRun run = make_run(len, glyphs, pos, paint, clusters, strlen(text), text);
538 SkClusterator clusterator(run);
Hal Canarye9e19dd2018-04-02 11:36:39 -0400539 SkClusterator::Cluster expectations[] = {
540 {&text[0], 1, 0, 1},
541 {&text[1], 3, 1, 1},
542 {&text[4], 1, 2, 1},
543 {&text[5], 1, 3, 1},
544 {&text[6], 1, 4, 1},
545 {nullptr, 0, 0, 0},
546 };
547 for (const auto& expectation : expectations) {
548 REPORTER_ASSERT(reporter, clusterator.next() == expectation);
549 }
Hal Canary1521c8a2018-03-28 09:51:00 -0700550 }
551}
552
Hal Canary43fb7a02016-12-30 13:09:03 -0500553#endif