blob: f23c8e34df72405d4b62aa8d6f7c60c5990a23a6 [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"
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"
Hal Canaryc640d0d2018-06-13 09:59:02 -040032#include "SkTo.h"
vandebo@chromium.org421d6442011-07-20 17:39:01 +000033#include "SkTypes.h"
halcanary66a82f32015-10-12 13:05:04 -070034#include "sk_tool_utils.h"
vandebo@chromium.org8459d4e2010-09-24 22:25:30 +000035
halcanary56586b62016-07-25 13:59:30 -070036#include <cstdlib>
37#include <cmath>
38
commit-bot@chromium.org608ea652013-10-03 19:29:21 +000039#define DUMMY_TEXT "DCT compessed stream."
40
halcanaryf7a169e2015-05-01 07:35:45 -070041template <typename T>
halcanary530032a2016-08-18 14:22:52 -070042static SkString emit_to_string(T& obj, SkPDFObjNumMap* catPtr = nullptr) {
43 SkPDFObjNumMap catalog;
halcanary6a144342015-01-23 11:45:10 -080044 SkDynamicMemoryWStream buffer;
halcanaryf7a169e2015-05-01 07:35:45 -070045 if (!catPtr) {
46 catPtr = &catalog;
halcanary130444f2015-04-25 06:45:07 -070047 }
halcanary530032a2016-08-18 14:22:52 -070048 obj.emitObject(&buffer, *catPtr);
halcanary56586b62016-07-25 13:59:30 -070049 SkString tmp(buffer.bytesWritten());
50 buffer.copyTo(tmp.writable_str());
halcanaryf7a169e2015-05-01 07:35:45 -070051 return tmp;
vandebo@chromium.org8459d4e2010-09-24 22:25:30 +000052}
53
halcanaryf7a169e2015-05-01 07:35:45 -070054static bool eq(const SkString& str, const char* strPtr, size_t len) {
55 return len == str.size() && 0 == memcmp(str.c_str(), strPtr, len);
vandebo@chromium.org421d6442011-07-20 17:39:01 +000056}
57
halcanary56586b62016-07-25 13:59:30 -070058static void assert_eql(skiatest::Reporter* reporter,
59 const SkString& skString,
60 const char* str,
61 size_t len) {
62 if (!eq(skString, str, len)) {
63 REPORT_FAILURE(reporter, "", SkStringPrintf(
64 "'%*s' != '%s'", len, str, skString.c_str()));
65 }
66}
halcanaryf7a169e2015-05-01 07:35:45 -070067
halcanary56586b62016-07-25 13:59:30 -070068static void assert_eq(skiatest::Reporter* reporter,
69 const SkString& skString,
70 const char* str) {
71 assert_eql(reporter, skString, str, strlen(str));
72}
halcanaryf7a169e2015-05-01 07:35:45 -070073
74
halcanary56586b62016-07-25 13:59:30 -070075template <typename T>
76static void assert_emit_eq(skiatest::Reporter* reporter,
77 T& object,
78 const char* string) {
79 SkString result = emit_to_string(object);
80 assert_eq(reporter, result, string);
81}
halcanaryf7a169e2015-05-01 07:35:45 -070082
vandebo@chromium.org421d6442011-07-20 17:39:01 +000083static void TestPDFStream(skiatest::Reporter* reporter) {
84 char streamBytes[] = "Test\nFoo\tBar";
halcanary022c2bd2016-09-02 11:29:46 -070085 auto streamData = skstd::make_unique<SkMemoryStream>(
86 streamBytes, strlen(streamBytes), true);
halcanaryac0e00d2016-07-27 11:12:23 -070087 auto stream = sk_make_sp<SkPDFStream>(std::move(streamData));
halcanary56586b62016-07-25 13:59:30 -070088 assert_emit_eq(reporter,
halcanaryf7a169e2015-05-01 07:35:45 -070089 *stream,
90 "<</Length 12>> stream\nTest\nFoo\tBar\nendstream");
halcanaryfa251062016-07-29 10:13:18 -070091 stream->dict()->insertInt("Attribute", 42);
halcanary56586b62016-07-25 13:59:30 -070092 assert_emit_eq(reporter,
halcanaryf7a169e2015-05-01 07:35:45 -070093 *stream,
94 "<</Length 12\n/Attribute 42>> stream\n"
95 "Test\nFoo\tBar\nendstream");
vandebo@chromium.org421d6442011-07-20 17:39:01 +000096
halcanary91d1d622015-02-17 14:43:06 -080097 {
vandebo@chromium.org421d6442011-07-20 17:39:01 +000098 char streamBytes2[] = "This is a longer string, so that compression "
99 "can do something with it. With shorter strings, "
100 "the short circuit logic cuts in and we end up "
101 "with an uncompressed string.";
halcanaryfe8f0e02016-07-27 14:14:04 -0700102 auto stream = sk_make_sp<SkPDFStream>(
103 SkData::MakeWithCopy(streamBytes2, strlen(streamBytes2)));
vandebo@chromium.org421d6442011-07-20 17:39:01 +0000104
105 SkDynamicMemoryWStream compressedByteStream;
halcanary48305e82015-08-18 13:30:25 -0700106 SkDeflateWStream deflateWStream(&compressedByteStream);
107 deflateWStream.write(streamBytes2, strlen(streamBytes2));
108 deflateWStream.finalize();
vandebo@chromium.org421d6442011-07-20 17:39:01 +0000109
mtkleincabc08c2015-02-19 08:29:24 -0800110 SkDynamicMemoryWStream expected;
halcanary37c46ca2015-03-31 12:30:20 -0700111 expected.writeText("<</Filter /FlateDecode\n/Length 116>> stream\n");
halcanary48305e82015-08-18 13:30:25 -0700112 compressedByteStream.writeToStream(&expected);
113 compressedByteStream.reset();
mtkleincabc08c2015-02-19 08:29:24 -0800114 expected.writeText("\nendstream");
reed42943c82016-09-12 12:01:44 -0700115 sk_sp<SkData> expectedResultData2(expected.detachAsData());
halcanaryf7a169e2015-05-01 07:35:45 -0700116 SkString result = emit_to_string(*stream);
halcanary18e0cbc2016-08-03 14:17:53 -0700117 #ifndef SK_PDF_LESS_COMPRESSION
halcanary56586b62016-07-25 13:59:30 -0700118 assert_eql(reporter,
halcanaryf7a169e2015-05-01 07:35:45 -0700119 result,
120 (const char*)expectedResultData2->data(),
121 expectedResultData2->size());
halcanary18e0cbc2016-08-03 14:17:53 -0700122 #endif
vandebo@chromium.org421d6442011-07-20 17:39:01 +0000123 }
124}
125
halcanaryf7a169e2015-05-01 07:35:45 -0700126static void TestObjectNumberMap(skiatest::Reporter* reporter) {
127 SkPDFObjNumMap objNumMap;
halcanary8103a342016-03-08 15:10:16 -0800128 sk_sp<SkPDFArray> a1(new SkPDFArray);
129 sk_sp<SkPDFArray> a2(new SkPDFArray);
130 sk_sp<SkPDFArray> a3(new SkPDFArray);
vandebo@chromium.org8459d4e2010-09-24 22:25:30 +0000131
Hal Canaryc5b94982017-10-03 09:38:43 -0400132 objNumMap.addObjectRecursively(a1.get());
133 objNumMap.addObjectRecursively(a2.get());
134 objNumMap.addObjectRecursively(a3.get());
vandebo@chromium.org8459d4e2010-09-24 22:25:30 +0000135
halcanaryf7a169e2015-05-01 07:35:45 -0700136 // The objects should be numbered in the order they are added,
137 // starting with 1.
138 REPORTER_ASSERT(reporter, objNumMap.getObjectNumber(a1.get()) == 1);
139 REPORTER_ASSERT(reporter, objNumMap.getObjectNumber(a2.get()) == 2);
140 REPORTER_ASSERT(reporter, objNumMap.getObjectNumber(a3.get()) == 3);
141 // Assert that repeated calls to get the object number return
142 // consistent result.
143 REPORTER_ASSERT(reporter, objNumMap.getObjectNumber(a1.get()) == 1);
vandebo@chromium.org8459d4e2010-09-24 22:25:30 +0000144}
145
146static void TestObjectRef(skiatest::Reporter* reporter) {
halcanary8103a342016-03-08 15:10:16 -0800147 sk_sp<SkPDFArray> a1(new SkPDFArray);
148 sk_sp<SkPDFArray> a2(new SkPDFArray);
149 a2->appendObjRef(a1);
vandebo@chromium.org8459d4e2010-09-24 22:25:30 +0000150
halcanary530032a2016-08-18 14:22:52 -0700151 SkPDFObjNumMap catalog;
Hal Canaryc5b94982017-10-03 09:38:43 -0400152 catalog.addObjectRecursively(a1.get());
halcanary530032a2016-08-18 14:22:52 -0700153 REPORTER_ASSERT(reporter, catalog.getObjectNumber(a1.get()) == 1);
vandebo@chromium.org8459d4e2010-09-24 22:25:30 +0000154
halcanaryf7a169e2015-05-01 07:35:45 -0700155 SkString result = emit_to_string(*a2, &catalog);
156 // If appendObjRef misbehaves, then the result would
157 // be [[]], not [1 0 R].
halcanary56586b62016-07-25 13:59:30 -0700158 assert_eq(reporter, result, "[1 0 R]");
vandebo@chromium.org8459d4e2010-09-24 22:25:30 +0000159}
160
vandebo@chromium.org4e1cc6a2013-01-25 19:27:23 +0000161// This test used to assert without the fix submitted for
162// http://code.google.com/p/skia/issues/detail?id=1083.
163// SKP files might have invalid glyph ids. This test ensures they are ignored,
164// and there is no assert on input data in Debug mode.
sugoi@google.com54f0d1b2013-02-27 19:17:41 +0000165static void test_issue1083() {
halcanary3d32d502015-03-01 06:55:20 -0800166 SkDynamicMemoryWStream outStream;
halcanary4b656662016-04-27 07:45:18 -0700167 sk_sp<SkDocument> doc(SkDocument::MakePDF(&outStream));
halcanary3d32d502015-03-01 06:55:20 -0800168 SkCanvas* canvas = doc->beginPage(100.0f, 100.0f);
vandebo@chromium.org4e1cc6a2013-01-25 19:27:23 +0000169 SkPaint paint;
170 paint.setTextEncoding(SkPaint::kGlyphID_TextEncoding);
171
172 uint16_t glyphID = 65000;
halcanary3d32d502015-03-01 06:55:20 -0800173 canvas->drawText(&glyphID, 2, 0, 0, paint);
vandebo@chromium.org4e1cc6a2013-01-25 19:27:23 +0000174
halcanary3d32d502015-03-01 06:55:20 -0800175 doc->close();
vandebo@chromium.org4e1cc6a2013-01-25 19:27:23 +0000176}
177
halcanary56586b62016-07-25 13:59:30 -0700178static void assert_emit_eq_number(skiatest::Reporter* reporter, float number) {
179 SkPDFUnion pdfUnion = SkPDFUnion::Scalar(number);
180 SkString result = emit_to_string(pdfUnion);
181 float value = static_cast<float>(std::atof(result.c_str()));
182 if (value != number) {
183 ERRORF(reporter, "%.9g != %s", number, result.c_str());
184 }
185}
186
187
halcanaryf7a169e2015-05-01 07:35:45 -0700188static void TestPDFUnion(skiatest::Reporter* reporter) {
189 SkPDFUnion boolTrue = SkPDFUnion::Bool(true);
halcanary56586b62016-07-25 13:59:30 -0700190 assert_emit_eq(reporter, boolTrue, "true");
vandebo@chromium.org8459d4e2010-09-24 22:25:30 +0000191
halcanaryf7a169e2015-05-01 07:35:45 -0700192 SkPDFUnion boolFalse = SkPDFUnion::Bool(false);
halcanary56586b62016-07-25 13:59:30 -0700193 assert_emit_eq(reporter, boolFalse, "false");
vandebo@chromium.org8459d4e2010-09-24 22:25:30 +0000194
halcanaryf7a169e2015-05-01 07:35:45 -0700195 SkPDFUnion int42 = SkPDFUnion::Int(42);
halcanary56586b62016-07-25 13:59:30 -0700196 assert_emit_eq(reporter, int42, "42");
halcanaryf7a169e2015-05-01 07:35:45 -0700197
halcanary56586b62016-07-25 13:59:30 -0700198 assert_emit_eq_number(reporter, SK_ScalarHalf);
199 assert_emit_eq_number(reporter, 110999.75f); // bigScalar
200 assert_emit_eq_number(reporter, 50000000.1f); // biggerScalar
201 assert_emit_eq_number(reporter, 1.0f / 65536); // smallScalar
vandebo@chromium.org094316b2011-03-04 03:15:13 +0000202
halcanaryf7a169e2015-05-01 07:35:45 -0700203 SkPDFUnion stringSimple = SkPDFUnion::String("test ) string ( foo");
halcanary56586b62016-07-25 13:59:30 -0700204 assert_emit_eq(reporter, stringSimple, "(test \\) string \\( foo)");
vandebo@chromium.org8459d4e2010-09-24 22:25:30 +0000205
halcanaryf7a169e2015-05-01 07:35:45 -0700206 SkString stringComplexInput("\ttest ) string ( foo");
207 SkPDFUnion stringComplex = SkPDFUnion::String(stringComplexInput);
halcanary56586b62016-07-25 13:59:30 -0700208 assert_emit_eq(reporter, stringComplex, "(\\011test \\) string \\( foo)");
halcanaryee41b752016-06-23 14:08:11 -0700209
210 SkString binaryStringInput("\1\2\3\4\5\6\7\10\11\12\13\14\15\16\17\20");
211 SkPDFUnion binaryString = SkPDFUnion::String(binaryStringInput);
halcanary56586b62016-07-25 13:59:30 -0700212 assert_emit_eq(reporter, binaryString, "<0102030405060708090A0B0C0D0E0F10>");
vandebo@chromium.org8459d4e2010-09-24 22:25:30 +0000213
halcanaryf7a169e2015-05-01 07:35:45 -0700214 SkString nameInput("Test name\twith#tab");
215 SkPDFUnion name = SkPDFUnion::Name(nameInput);
halcanary56586b62016-07-25 13:59:30 -0700216 assert_emit_eq(reporter, name, "/Test#20name#09with#23tab");
halcanaryf7a169e2015-05-01 07:35:45 -0700217
218 SkString nameInput2("A#/%()<>[]{}B");
219 SkPDFUnion name2 = SkPDFUnion::Name(nameInput2);
halcanary56586b62016-07-25 13:59:30 -0700220 assert_emit_eq(reporter, name2, "/A#23#2F#25#28#29#3C#3E#5B#5D#7B#7DB");
halcanaryf7a169e2015-05-01 07:35:45 -0700221
222 SkPDFUnion name3 = SkPDFUnion::Name("SimpleNameWithOnlyPrintableASCII");
halcanary56586b62016-07-25 13:59:30 -0700223 assert_emit_eq(reporter, name3, "/SimpleNameWithOnlyPrintableASCII");
vandebo@chromium.org251a7662012-09-21 17:50:50 +0000224
vandebo@chromium.orgc0376fe2012-03-05 18:44:33 +0000225 // Test that we correctly handle characters with the high-bit set.
halcanaryf7a169e2015-05-01 07:35:45 -0700226 SkString highBitString("\xDE\xAD" "be\xEF");
227 SkPDFUnion highBitName = SkPDFUnion::Name(highBitString);
halcanary56586b62016-07-25 13:59:30 -0700228 assert_emit_eq(reporter, highBitName, "/#DE#ADbe#EF");
halcanaryf7a169e2015-05-01 07:35:45 -0700229}
vandebo@chromium.orgc0376fe2012-03-05 18:44:33 +0000230
halcanaryf7a169e2015-05-01 07:35:45 -0700231static void TestPDFArray(skiatest::Reporter* reporter) {
halcanary8103a342016-03-08 15:10:16 -0800232 sk_sp<SkPDFArray> array(new SkPDFArray);
halcanary56586b62016-07-25 13:59:30 -0700233 assert_emit_eq(reporter, *array, "[]");
vandebo@chromium.org8459d4e2010-09-24 22:25:30 +0000234
halcanaryf7a169e2015-05-01 07:35:45 -0700235 array->appendInt(42);
halcanary56586b62016-07-25 13:59:30 -0700236 assert_emit_eq(reporter, *array, "[42]");
halcanaryf7a169e2015-05-01 07:35:45 -0700237
238 array->appendScalar(SK_ScalarHalf);
halcanary56586b62016-07-25 13:59:30 -0700239 assert_emit_eq(reporter, *array, "[42 .5]");
halcanaryf7a169e2015-05-01 07:35:45 -0700240
241 array->appendInt(0);
halcanary56586b62016-07-25 13:59:30 -0700242 assert_emit_eq(reporter, *array, "[42 .5 0]");
halcanaryf7a169e2015-05-01 07:35:45 -0700243
244 array->appendBool(true);
halcanary56586b62016-07-25 13:59:30 -0700245 assert_emit_eq(reporter, *array, "[42 .5 0 true]");
halcanaryf7a169e2015-05-01 07:35:45 -0700246
247 array->appendName("ThisName");
halcanary56586b62016-07-25 13:59:30 -0700248 assert_emit_eq(reporter, *array, "[42 .5 0 true /ThisName]");
halcanaryf7a169e2015-05-01 07:35:45 -0700249
250 array->appendName(SkString("AnotherName"));
halcanary56586b62016-07-25 13:59:30 -0700251 assert_emit_eq(reporter, *array, "[42 .5 0 true /ThisName /AnotherName]");
halcanaryf7a169e2015-05-01 07:35:45 -0700252
253 array->appendString("This String");
halcanary56586b62016-07-25 13:59:30 -0700254 assert_emit_eq(reporter, *array,
halcanary8e9f5e32016-02-24 15:46:46 -0800255 "[42 .5 0 true /ThisName /AnotherName (This String)]");
halcanaryf7a169e2015-05-01 07:35:45 -0700256
257 array->appendString(SkString("Another String"));
halcanary56586b62016-07-25 13:59:30 -0700258 assert_emit_eq(reporter, *array,
halcanary8e9f5e32016-02-24 15:46:46 -0800259 "[42 .5 0 true /ThisName /AnotherName (This String) "
halcanaryf7a169e2015-05-01 07:35:45 -0700260 "(Another String)]");
261
halcanary8103a342016-03-08 15:10:16 -0800262 sk_sp<SkPDFArray> innerArray(new SkPDFArray);
halcanaryf7a169e2015-05-01 07:35:45 -0700263 innerArray->appendInt(-1);
halcanary8103a342016-03-08 15:10:16 -0800264 array->appendObject(std::move(innerArray));
halcanary56586b62016-07-25 13:59:30 -0700265 assert_emit_eq(reporter, *array,
halcanary8e9f5e32016-02-24 15:46:46 -0800266 "[42 .5 0 true /ThisName /AnotherName (This String) "
halcanaryf7a169e2015-05-01 07:35:45 -0700267 "(Another String) [-1]]");
268
halcanary8103a342016-03-08 15:10:16 -0800269 sk_sp<SkPDFArray> referencedArray(new SkPDFArray);
halcanary530032a2016-08-18 14:22:52 -0700270 SkPDFObjNumMap catalog;
Hal Canaryc5b94982017-10-03 09:38:43 -0400271 catalog.addObjectRecursively(referencedArray.get());
halcanary530032a2016-08-18 14:22:52 -0700272 REPORTER_ASSERT(reporter, catalog.getObjectNumber(
halcanaryf7a169e2015-05-01 07:35:45 -0700273 referencedArray.get()) == 1);
halcanary8103a342016-03-08 15:10:16 -0800274 array->appendObjRef(std::move(referencedArray));
halcanaryf7a169e2015-05-01 07:35:45 -0700275
276 SkString result = emit_to_string(*array, &catalog);
halcanary56586b62016-07-25 13:59:30 -0700277 assert_eq(reporter, result,
halcanary8e9f5e32016-02-24 15:46:46 -0800278 "[42 .5 0 true /ThisName /AnotherName (This String) "
halcanaryf7a169e2015-05-01 07:35:45 -0700279 "(Another String) [-1] 1 0 R]");
280}
281
282static void TestPDFDict(skiatest::Reporter* reporter) {
halcanary8103a342016-03-08 15:10:16 -0800283 sk_sp<SkPDFDict> dict(new SkPDFDict);
halcanary56586b62016-07-25 13:59:30 -0700284 assert_emit_eq(reporter, *dict, "<<>>");
halcanaryf7a169e2015-05-01 07:35:45 -0700285
286 dict->insertInt("n1", SkToSizeT(42));
halcanary56586b62016-07-25 13:59:30 -0700287 assert_emit_eq(reporter, *dict, "<</n1 42>>");
halcanaryf7a169e2015-05-01 07:35:45 -0700288
289 dict.reset(new SkPDFDict);
halcanary56586b62016-07-25 13:59:30 -0700290 assert_emit_eq(reporter, *dict, "<<>>");
halcanaryf7a169e2015-05-01 07:35:45 -0700291
292 dict->insertInt("n1", 42);
halcanary56586b62016-07-25 13:59:30 -0700293 assert_emit_eq(reporter, *dict, "<</n1 42>>");
halcanaryf7a169e2015-05-01 07:35:45 -0700294
295 dict->insertScalar("n2", SK_ScalarHalf);
296
halcanary130444f2015-04-25 06:45:07 -0700297 SkString n3("n3");
halcanary8103a342016-03-08 15:10:16 -0800298 sk_sp<SkPDFArray> innerArray(new SkPDFArray);
halcanaryf7a169e2015-05-01 07:35:45 -0700299 innerArray->appendInt(-100);
halcanary8103a342016-03-08 15:10:16 -0800300 dict->insertObject(n3, std::move(innerArray));
halcanary56586b62016-07-25 13:59:30 -0700301 assert_emit_eq(reporter, *dict, "<</n1 42\n/n2 .5\n/n3 [-100]>>");
vandebo@chromium.org8459d4e2010-09-24 22:25:30 +0000302
halcanaryf7a169e2015-05-01 07:35:45 -0700303 dict.reset(new SkPDFDict);
halcanary56586b62016-07-25 13:59:30 -0700304 assert_emit_eq(reporter, *dict, "<<>>");
halcanaryf7a169e2015-05-01 07:35:45 -0700305
306 dict->insertInt("n1", 24);
halcanary56586b62016-07-25 13:59:30 -0700307 assert_emit_eq(reporter, *dict, "<</n1 24>>");
halcanaryf7a169e2015-05-01 07:35:45 -0700308
309 dict->insertInt("n2", SkToSizeT(99));
halcanary56586b62016-07-25 13:59:30 -0700310 assert_emit_eq(reporter, *dict, "<</n1 24\n/n2 99>>");
halcanaryf7a169e2015-05-01 07:35:45 -0700311
312 dict->insertScalar("n3", SK_ScalarHalf);
halcanary56586b62016-07-25 13:59:30 -0700313 assert_emit_eq(reporter, *dict, "<</n1 24\n/n2 99\n/n3 .5>>");
halcanaryf7a169e2015-05-01 07:35:45 -0700314
315 dict->insertName("n4", "AName");
halcanary56586b62016-07-25 13:59:30 -0700316 assert_emit_eq(reporter, *dict, "<</n1 24\n/n2 99\n/n3 .5\n/n4 /AName>>");
halcanaryf7a169e2015-05-01 07:35:45 -0700317
318 dict->insertName("n5", SkString("AnotherName"));
halcanary56586b62016-07-25 13:59:30 -0700319 assert_emit_eq(reporter, *dict, "<</n1 24\n/n2 99\n/n3 .5\n/n4 /AName\n"
halcanaryf7a169e2015-05-01 07:35:45 -0700320 "/n5 /AnotherName>>");
321
322 dict->insertString("n6", "A String");
halcanary56586b62016-07-25 13:59:30 -0700323 assert_emit_eq(reporter, *dict, "<</n1 24\n/n2 99\n/n3 .5\n/n4 /AName\n"
halcanaryf7a169e2015-05-01 07:35:45 -0700324 "/n5 /AnotherName\n/n6 (A String)>>");
325
326 dict->insertString("n7", SkString("Another String"));
halcanary56586b62016-07-25 13:59:30 -0700327 assert_emit_eq(reporter, *dict, "<</n1 24\n/n2 99\n/n3 .5\n/n4 /AName\n"
halcanaryf7a169e2015-05-01 07:35:45 -0700328 "/n5 /AnotherName\n/n6 (A String)\n/n7 (Another String)>>");
329
330 dict.reset(new SkPDFDict("DType"));
halcanary56586b62016-07-25 13:59:30 -0700331 assert_emit_eq(reporter, *dict, "<</Type /DType>>");
halcanary8e9f5e32016-02-24 15:46:46 -0800332
halcanary8103a342016-03-08 15:10:16 -0800333 sk_sp<SkPDFArray> referencedArray(new SkPDFArray);
halcanary530032a2016-08-18 14:22:52 -0700334 SkPDFObjNumMap catalog;
Hal Canaryc5b94982017-10-03 09:38:43 -0400335 catalog.addObjectRecursively(referencedArray.get());
halcanary530032a2016-08-18 14:22:52 -0700336 REPORTER_ASSERT(reporter, catalog.getObjectNumber(
halcanaryf7a169e2015-05-01 07:35:45 -0700337 referencedArray.get()) == 1);
halcanary8103a342016-03-08 15:10:16 -0800338 dict->insertObjRef("n1", std::move(referencedArray));
halcanaryf7a169e2015-05-01 07:35:45 -0700339 SkString result = emit_to_string(*dict, &catalog);
halcanary56586b62016-07-25 13:59:30 -0700340 assert_eq(reporter, result, "<</Type /DType\n/n1 1 0 R>>");
halcanaryf7a169e2015-05-01 07:35:45 -0700341}
342
halcanary57f744e2016-09-09 11:41:59 -0700343DEF_TEST(SkPDF_Primitives, reporter) {
halcanaryf7a169e2015-05-01 07:35:45 -0700344 TestPDFUnion(reporter);
345 TestPDFArray(reporter);
346 TestPDFDict(reporter);
vandebo@chromium.org421d6442011-07-20 17:39:01 +0000347 TestPDFStream(reporter);
halcanaryf7a169e2015-05-01 07:35:45 -0700348 TestObjectNumberMap(reporter);
vandebo@chromium.org8459d4e2010-09-24 22:25:30 +0000349 TestObjectRef(reporter);
sugoi@google.com54f0d1b2013-02-27 19:17:41 +0000350 test_issue1083();
vandebo@chromium.org8459d4e2010-09-24 22:25:30 +0000351}
senorblancob0e89dc2014-10-20 14:03:12 -0700352
353namespace {
354
355class DummyImageFilter : public SkImageFilter {
356public:
robertphillips43c2ad42016-04-04 05:05:11 -0700357 static sk_sp<DummyImageFilter> Make(bool visited = false) {
358 return sk_sp<DummyImageFilter>(new DummyImageFilter(visited));
359 }
360
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
389};
390
391// Check that PDF rendering of image filters successfully falls back to
392// CPU rasterization.
halcanary57f744e2016-09-09 11:41:59 -0700393DEF_TEST(SkPDF_ImageFilter, reporter) {
394 REQUIRE_PDF_DOCUMENT(SkPDF_ImageFilter, reporter);
halcanary3d32d502015-03-01 06:55:20 -0800395 SkDynamicMemoryWStream stream;
halcanary4b656662016-04-27 07:45:18 -0700396 sk_sp<SkDocument> doc(SkDocument::MakePDF(&stream));
halcanary3d32d502015-03-01 06:55:20 -0800397 SkCanvas* canvas = doc->beginPage(100.0f, 100.0f);
398
robertphillips43c2ad42016-04-04 05:05:11 -0700399 sk_sp<DummyImageFilter> filter(DummyImageFilter::Make());
senorblancob0e89dc2014-10-20 14:03:12 -0700400
401 // Filter just created; should be unvisited.
402 REPORTER_ASSERT(reporter, !filter->visited());
403 SkPaint paint;
Mike Reed5e257172016-11-01 11:22:05 -0400404 paint.setImageFilter(filter);
halcanary3d32d502015-03-01 06:55:20 -0800405 canvas->drawRect(SkRect::MakeWH(100, 100), paint);
406 doc->close();
senorblancob0e89dc2014-10-20 14:03:12 -0700407
408 // Filter was used in rendering; should be visited.
409 REPORTER_ASSERT(reporter, filter->visited());
410}
halcanary66a82f32015-10-12 13:05:04 -0700411
412// Check that PDF rendering of image filters successfully falls back to
413// CPU rasterization.
halcanary57f744e2016-09-09 11:41:59 -0700414DEF_TEST(SkPDF_FontCanEmbedTypeface, reporter) {
halcanary66a82f32015-10-12 13:05:04 -0700415 SkPDFCanon canon;
416
417 const char resource[] = "fonts/Roboto2-Regular_NoEmbed.ttf";
bungeman13b9c952016-05-12 10:09:30 -0700418 sk_sp<SkTypeface> noEmbedTypeface(MakeResourceAsTypeface(resource));
halcanary66a82f32015-10-12 13:05:04 -0700419 if (noEmbedTypeface) {
420 REPORTER_ASSERT(reporter,
halcanary8103a342016-03-08 15:10:16 -0800421 !SkPDFFont::CanEmbedTypeface(noEmbedTypeface.get(), &canon));
halcanary66a82f32015-10-12 13:05:04 -0700422 }
halcanary8103a342016-03-08 15:10:16 -0800423 sk_sp<SkTypeface> portableTypeface(
Ben Wagnera93a14a2017-08-28 10:34:05 -0400424 sk_tool_utils::create_portable_typeface(nullptr, SkFontStyle()));
halcanary66a82f32015-10-12 13:05:04 -0700425 REPORTER_ASSERT(reporter,
halcanary8103a342016-03-08 15:10:16 -0800426 SkPDFFont::CanEmbedTypeface(portableTypeface.get(), &canon));
halcanary66a82f32015-10-12 13:05:04 -0700427}
halcanary8e9f5e32016-02-24 15:46:46 -0800428
429
430// test to see that all finite scalars round trip via scanf().
431static void check_pdf_scalar_serialization(
432 skiatest::Reporter* reporter, float inputFloat) {
Hal Canary3c36ef62018-01-02 16:25:53 -0500433 char floatString[kMaximumSkFloatToDecimalLength];
434 size_t len = SkFloatToDecimal(inputFloat, floatString);
halcanary8e9f5e32016-02-24 15:46:46 -0800435 if (len >= sizeof(floatString)) {
436 ERRORF(reporter, "string too long: %u", (unsigned)len);
437 return;
438 }
439 if (floatString[len] != '\0' || strlen(floatString) != len) {
440 ERRORF(reporter, "terminator misplaced.");
441 return; // The terminator is needed for sscanf().
442 }
443 if (reporter->verbose()) {
444 SkDebugf("%15.9g = \"%s\"\n", inputFloat, floatString);
445 }
446 float roundTripFloat;
447 if (1 != sscanf(floatString, "%f", &roundTripFloat)) {
448 ERRORF(reporter, "unscannable result: %s", floatString);
449 return;
450 }
halcanary56586b62016-07-25 13:59:30 -0700451 if (std::isfinite(inputFloat) && roundTripFloat != inputFloat) {
halcanary8e9f5e32016-02-24 15:46:46 -0800452 ERRORF(reporter, "roundTripFloat (%.9g) != inputFloat (%.9g)",
453 roundTripFloat, inputFloat);
454 }
455}
456
457// Test SkPDFUtils::AppendScalar for accuracy.
halcanary57f744e2016-09-09 11:41:59 -0700458DEF_TEST(SkPDF_Primitives_Scalar, reporter) {
halcanary8e9f5e32016-02-24 15:46:46 -0800459 SkRandom random(0x5EED);
460 int iterationCount = 512;
461 while (iterationCount-- > 0) {
462 union { uint32_t u; float f; };
463 u = random.nextU();
464 static_assert(sizeof(float) == sizeof(uint32_t), "");
465 check_pdf_scalar_serialization(reporter, f);
466 }
467 float alwaysCheck[] = {
468 0.0f, -0.0f, 1.0f, -1.0f, SK_ScalarPI, 0.1f, FLT_MIN, FLT_MAX,
469 -FLT_MIN, -FLT_MAX, FLT_MIN / 16.0f, -FLT_MIN / 16.0f,
470 SK_FloatNaN, SK_FloatInfinity, SK_FloatNegativeInfinity,
471 -FLT_MIN / 8388608.0
472 };
473 for (float inputFloat: alwaysCheck) {
474 check_pdf_scalar_serialization(reporter, inputFloat);
475 }
476}
halcanaryeb92cb32016-07-15 13:41:27 -0700477
478// Test SkPDFUtils:: for accuracy.
halcanary57f744e2016-09-09 11:41:59 -0700479DEF_TEST(SkPDF_Primitives_Color, reporter) {
halcanaryeb92cb32016-07-15 13:41:27 -0700480 char buffer[5];
481 for (int i = 0; i < 256; ++i) {
482 size_t len = SkPDFUtils::ColorToDecimal(i, buffer);
483 REPORTER_ASSERT(reporter, len == strlen(buffer));
484 float f;
485 REPORTER_ASSERT(reporter, 1 == sscanf(buffer, "%f", &f));
486 int roundTrip = (int)(0.5 + f * 255);
487 REPORTER_ASSERT(reporter, roundTrip == i);
488 }
489}
Hal Canary1521c8a2018-03-28 09:51:00 -0700490
491DEF_TEST(SkPDF_Clusterator, reporter) {
Hal Canary1521c8a2018-03-28 09:51:00 -0700492 SkPaint paint;
493 paint.setTextEncoding(SkPaint::kGlyphID_TextEncoding);
Hal Canarye9e19dd2018-04-02 11:36:39 -0400494 {
495 const uint32_t clusters[11] = { 3, 2, 2, 1, 0, 4, 4, 7, 6, 6, 5 };
496 const SkGlyphID glyphs[11] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11};
497 const char text[] = "abcdefgh";
498 SkClusterator clusterator(glyphs, sizeof(glyphs), paint, clusters, strlen(text), text);
499 SkClusterator::Cluster expectations[] = {
500 {&text[3], 1, 0, 1},
501 {&text[2], 1, 1, 2},
502 {&text[1], 1, 3, 1},
503 {&text[0], 1, 4, 1},
504 {&text[4], 1, 5, 2},
505 {&text[7], 1, 7, 1},
506 {&text[6], 1, 8, 2},
507 {&text[5], 1, 10, 1},
508 {nullptr, 0, 0, 0},
509 };
510 for (const auto& expectation : expectations) {
511 REPORTER_ASSERT(reporter, clusterator.next() == expectation);
512 }
513 }
514 {
515 const uint32_t clusters[5] = { 0, 1, 4, 5, 6 };
516 const SkGlyphID glyphs[5] = { 43, 167, 79, 79, 82, };
517 const char text[] = "Ha\xCC\x8A" "llo";
518 SkClusterator clusterator(glyphs, sizeof(glyphs), paint, clusters, strlen(text), text);
519 SkClusterator::Cluster expectations[] = {
520 {&text[0], 1, 0, 1},
521 {&text[1], 3, 1, 1},
522 {&text[4], 1, 2, 1},
523 {&text[5], 1, 3, 1},
524 {&text[6], 1, 4, 1},
525 {nullptr, 0, 0, 0},
526 };
527 for (const auto& expectation : expectations) {
528 REPORTER_ASSERT(reporter, clusterator.next() == expectation);
529 }
Hal Canary1521c8a2018-03-28 09:51:00 -0700530 }
531}
532
Hal Canary43fb7a02016-12-30 13:09:03 -0500533#endif