blob: 81b33ba3b77302c927768213d400ba15b62b2893 [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
halcanary66a82f32015-10-12 13:05:04 -07008#include "Resources.h"
edisonn@google.comd9dfa182013-04-24 13:01:01 +00009#include "SkBitmap.h"
vandebo@chromium.org4e1cc6a2013-01-25 19:27:23 +000010#include "SkCanvas.h"
reed@google.com8a85d0c2011-06-24 19:12:12 +000011#include "SkData.h"
halcanary3d32d502015-03-01 06:55:20 -080012#include "SkDocument.h"
halcanaryd9e57152015-08-12 11:24:40 -070013#include "SkDeflate.h"
edisonn@google.comd9dfa182013-04-24 13:01:01 +000014#include "SkImageEncoder.h"
halcanary022c2bd2016-09-02 11:29:46 -070015#include "SkMakeUnique.h"
edisonn@google.comd9dfa182013-04-24 13:01:01 +000016#include "SkMatrix.h"
halcanarya1f1ee92015-02-20 06:17:26 -080017#include "SkPDFCanon.h"
vandebo@chromium.org4e1cc6a2013-01-25 19:27:23 +000018#include "SkPDFDevice.h"
halcanary66a82f32015-10-12 13:05:04 -070019#include "SkPDFFont.h"
vandebo@chromium.org8459d4e2010-09-24 22:25:30 +000020#include "SkPDFTypes.h"
halcanary8e9f5e32016-02-24 15:46:46 -080021#include "SkPDFUtils.h"
senorblancob0e89dc2014-10-20 14:03:12 -070022#include "SkReadBuffer.h"
vandebo@chromium.org8459d4e2010-09-24 22:25:30 +000023#include "SkScalar.h"
robertphillips4ba94e22016-04-04 12:07:47 -070024#include "SkSpecialImage.h"
vandebo@chromium.org8459d4e2010-09-24 22:25:30 +000025#include "SkStream.h"
vandebo@chromium.org421d6442011-07-20 17:39:01 +000026#include "SkTypes.h"
tfarina@chromium.org8f6884a2014-01-24 20:56:26 +000027#include "Test.h"
halcanary66a82f32015-10-12 13:05:04 -070028#include "sk_tool_utils.h"
vandebo@chromium.org8459d4e2010-09-24 22:25:30 +000029
halcanary56586b62016-07-25 13:59:30 -070030#include <cstdlib>
31#include <cmath>
32
commit-bot@chromium.org608ea652013-10-03 19:29:21 +000033#define DUMMY_TEXT "DCT compessed stream."
34
halcanaryf7a169e2015-05-01 07:35:45 -070035template <typename T>
halcanary530032a2016-08-18 14:22:52 -070036static SkString emit_to_string(T& obj, SkPDFObjNumMap* catPtr = nullptr) {
37 SkPDFObjNumMap catalog;
halcanary6a144342015-01-23 11:45:10 -080038 SkDynamicMemoryWStream buffer;
halcanaryf7a169e2015-05-01 07:35:45 -070039 if (!catPtr) {
40 catPtr = &catalog;
halcanary130444f2015-04-25 06:45:07 -070041 }
halcanary530032a2016-08-18 14:22:52 -070042 obj.emitObject(&buffer, *catPtr);
halcanary56586b62016-07-25 13:59:30 -070043 SkString tmp(buffer.bytesWritten());
44 buffer.copyTo(tmp.writable_str());
halcanaryf7a169e2015-05-01 07:35:45 -070045 return tmp;
vandebo@chromium.org8459d4e2010-09-24 22:25:30 +000046}
47
halcanaryf7a169e2015-05-01 07:35:45 -070048static bool eq(const SkString& str, const char* strPtr, size_t len) {
49 return len == str.size() && 0 == memcmp(str.c_str(), strPtr, len);
vandebo@chromium.org421d6442011-07-20 17:39:01 +000050}
51
halcanary56586b62016-07-25 13:59:30 -070052static void assert_eql(skiatest::Reporter* reporter,
53 const SkString& skString,
54 const char* str,
55 size_t len) {
56 if (!eq(skString, str, len)) {
57 REPORT_FAILURE(reporter, "", SkStringPrintf(
58 "'%*s' != '%s'", len, str, skString.c_str()));
59 }
60}
halcanaryf7a169e2015-05-01 07:35:45 -070061
halcanary56586b62016-07-25 13:59:30 -070062static void assert_eq(skiatest::Reporter* reporter,
63 const SkString& skString,
64 const char* str) {
65 assert_eql(reporter, skString, str, strlen(str));
66}
halcanaryf7a169e2015-05-01 07:35:45 -070067
68
halcanary56586b62016-07-25 13:59:30 -070069template <typename T>
70static void assert_emit_eq(skiatest::Reporter* reporter,
71 T& object,
72 const char* string) {
73 SkString result = emit_to_string(object);
74 assert_eq(reporter, result, string);
75}
halcanaryf7a169e2015-05-01 07:35:45 -070076
vandebo@chromium.org421d6442011-07-20 17:39:01 +000077static void TestPDFStream(skiatest::Reporter* reporter) {
78 char streamBytes[] = "Test\nFoo\tBar";
halcanary022c2bd2016-09-02 11:29:46 -070079 auto streamData = skstd::make_unique<SkMemoryStream>(
80 streamBytes, strlen(streamBytes), true);
halcanaryac0e00d2016-07-27 11:12:23 -070081 auto stream = sk_make_sp<SkPDFStream>(std::move(streamData));
halcanary56586b62016-07-25 13:59:30 -070082 assert_emit_eq(reporter,
halcanaryf7a169e2015-05-01 07:35:45 -070083 *stream,
84 "<</Length 12>> stream\nTest\nFoo\tBar\nendstream");
halcanaryfa251062016-07-29 10:13:18 -070085 stream->dict()->insertInt("Attribute", 42);
halcanary56586b62016-07-25 13:59:30 -070086 assert_emit_eq(reporter,
halcanaryf7a169e2015-05-01 07:35:45 -070087 *stream,
88 "<</Length 12\n/Attribute 42>> stream\n"
89 "Test\nFoo\tBar\nendstream");
vandebo@chromium.org421d6442011-07-20 17:39:01 +000090
halcanary91d1d622015-02-17 14:43:06 -080091 {
vandebo@chromium.org421d6442011-07-20 17:39:01 +000092 char streamBytes2[] = "This is a longer string, so that compression "
93 "can do something with it. With shorter strings, "
94 "the short circuit logic cuts in and we end up "
95 "with an uncompressed string.";
halcanaryfe8f0e02016-07-27 14:14:04 -070096 auto stream = sk_make_sp<SkPDFStream>(
97 SkData::MakeWithCopy(streamBytes2, strlen(streamBytes2)));
vandebo@chromium.org421d6442011-07-20 17:39:01 +000098
99 SkDynamicMemoryWStream compressedByteStream;
halcanary48305e82015-08-18 13:30:25 -0700100 SkDeflateWStream deflateWStream(&compressedByteStream);
101 deflateWStream.write(streamBytes2, strlen(streamBytes2));
102 deflateWStream.finalize();
vandebo@chromium.org421d6442011-07-20 17:39:01 +0000103
mtkleincabc08c2015-02-19 08:29:24 -0800104 SkDynamicMemoryWStream expected;
halcanary37c46ca2015-03-31 12:30:20 -0700105 expected.writeText("<</Filter /FlateDecode\n/Length 116>> stream\n");
halcanary48305e82015-08-18 13:30:25 -0700106 compressedByteStream.writeToStream(&expected);
107 compressedByteStream.reset();
mtkleincabc08c2015-02-19 08:29:24 -0800108 expected.writeText("\nendstream");
reed42943c82016-09-12 12:01:44 -0700109 sk_sp<SkData> expectedResultData2(expected.detachAsData());
halcanaryf7a169e2015-05-01 07:35:45 -0700110 SkString result = emit_to_string(*stream);
halcanary18e0cbc2016-08-03 14:17:53 -0700111 #ifndef SK_PDF_LESS_COMPRESSION
halcanary56586b62016-07-25 13:59:30 -0700112 assert_eql(reporter,
halcanaryf7a169e2015-05-01 07:35:45 -0700113 result,
114 (const char*)expectedResultData2->data(),
115 expectedResultData2->size());
halcanary18e0cbc2016-08-03 14:17:53 -0700116 #endif
vandebo@chromium.org421d6442011-07-20 17:39:01 +0000117 }
118}
119
halcanaryf7a169e2015-05-01 07:35:45 -0700120static void TestObjectNumberMap(skiatest::Reporter* reporter) {
121 SkPDFObjNumMap objNumMap;
halcanary8103a342016-03-08 15:10:16 -0800122 sk_sp<SkPDFArray> a1(new SkPDFArray);
123 sk_sp<SkPDFArray> a2(new SkPDFArray);
124 sk_sp<SkPDFArray> a3(new SkPDFArray);
vandebo@chromium.org8459d4e2010-09-24 22:25:30 +0000125
halcanaryf7a169e2015-05-01 07:35:45 -0700126 objNumMap.addObject(a1.get());
127 objNumMap.addObject(a2.get());
128 objNumMap.addObject(a3.get());
vandebo@chromium.org8459d4e2010-09-24 22:25:30 +0000129
halcanaryf7a169e2015-05-01 07:35:45 -0700130 // The objects should be numbered in the order they are added,
131 // starting with 1.
132 REPORTER_ASSERT(reporter, objNumMap.getObjectNumber(a1.get()) == 1);
133 REPORTER_ASSERT(reporter, objNumMap.getObjectNumber(a2.get()) == 2);
134 REPORTER_ASSERT(reporter, objNumMap.getObjectNumber(a3.get()) == 3);
135 // Assert that repeated calls to get the object number return
136 // consistent result.
137 REPORTER_ASSERT(reporter, objNumMap.getObjectNumber(a1.get()) == 1);
vandebo@chromium.org8459d4e2010-09-24 22:25:30 +0000138}
139
140static void TestObjectRef(skiatest::Reporter* reporter) {
halcanary8103a342016-03-08 15:10:16 -0800141 sk_sp<SkPDFArray> a1(new SkPDFArray);
142 sk_sp<SkPDFArray> a2(new SkPDFArray);
143 a2->appendObjRef(a1);
vandebo@chromium.org8459d4e2010-09-24 22:25:30 +0000144
halcanary530032a2016-08-18 14:22:52 -0700145 SkPDFObjNumMap catalog;
146 catalog.addObject(a1.get());
147 REPORTER_ASSERT(reporter, catalog.getObjectNumber(a1.get()) == 1);
vandebo@chromium.org8459d4e2010-09-24 22:25:30 +0000148
halcanaryf7a169e2015-05-01 07:35:45 -0700149 SkString result = emit_to_string(*a2, &catalog);
150 // If appendObjRef misbehaves, then the result would
151 // be [[]], not [1 0 R].
halcanary56586b62016-07-25 13:59:30 -0700152 assert_eq(reporter, result, "[1 0 R]");
vandebo@chromium.org8459d4e2010-09-24 22:25:30 +0000153}
154
vandebo@chromium.org4e1cc6a2013-01-25 19:27:23 +0000155// This test used to assert without the fix submitted for
156// http://code.google.com/p/skia/issues/detail?id=1083.
157// SKP files might have invalid glyph ids. This test ensures they are ignored,
158// and there is no assert on input data in Debug mode.
sugoi@google.com54f0d1b2013-02-27 19:17:41 +0000159static void test_issue1083() {
halcanary3d32d502015-03-01 06:55:20 -0800160 SkDynamicMemoryWStream outStream;
halcanary4b656662016-04-27 07:45:18 -0700161 sk_sp<SkDocument> doc(SkDocument::MakePDF(&outStream));
halcanary3d32d502015-03-01 06:55:20 -0800162 SkCanvas* canvas = doc->beginPage(100.0f, 100.0f);
vandebo@chromium.org4e1cc6a2013-01-25 19:27:23 +0000163 SkPaint paint;
164 paint.setTextEncoding(SkPaint::kGlyphID_TextEncoding);
165
166 uint16_t glyphID = 65000;
halcanary3d32d502015-03-01 06:55:20 -0800167 canvas->drawText(&glyphID, 2, 0, 0, paint);
vandebo@chromium.org4e1cc6a2013-01-25 19:27:23 +0000168
halcanary3d32d502015-03-01 06:55:20 -0800169 doc->close();
vandebo@chromium.org4e1cc6a2013-01-25 19:27:23 +0000170}
171
halcanary56586b62016-07-25 13:59:30 -0700172static void assert_emit_eq_number(skiatest::Reporter* reporter, float number) {
173 SkPDFUnion pdfUnion = SkPDFUnion::Scalar(number);
174 SkString result = emit_to_string(pdfUnion);
175 float value = static_cast<float>(std::atof(result.c_str()));
176 if (value != number) {
177 ERRORF(reporter, "%.9g != %s", number, result.c_str());
178 }
179}
180
181
halcanaryf7a169e2015-05-01 07:35:45 -0700182static void TestPDFUnion(skiatest::Reporter* reporter) {
183 SkPDFUnion boolTrue = SkPDFUnion::Bool(true);
halcanary56586b62016-07-25 13:59:30 -0700184 assert_emit_eq(reporter, boolTrue, "true");
vandebo@chromium.org8459d4e2010-09-24 22:25:30 +0000185
halcanaryf7a169e2015-05-01 07:35:45 -0700186 SkPDFUnion boolFalse = SkPDFUnion::Bool(false);
halcanary56586b62016-07-25 13:59:30 -0700187 assert_emit_eq(reporter, boolFalse, "false");
vandebo@chromium.org8459d4e2010-09-24 22:25:30 +0000188
halcanaryf7a169e2015-05-01 07:35:45 -0700189 SkPDFUnion int42 = SkPDFUnion::Int(42);
halcanary56586b62016-07-25 13:59:30 -0700190 assert_emit_eq(reporter, int42, "42");
halcanaryf7a169e2015-05-01 07:35:45 -0700191
halcanary56586b62016-07-25 13:59:30 -0700192 assert_emit_eq_number(reporter, SK_ScalarHalf);
193 assert_emit_eq_number(reporter, 110999.75f); // bigScalar
194 assert_emit_eq_number(reporter, 50000000.1f); // biggerScalar
195 assert_emit_eq_number(reporter, 1.0f / 65536); // smallScalar
vandebo@chromium.org094316b2011-03-04 03:15:13 +0000196
halcanaryf7a169e2015-05-01 07:35:45 -0700197 SkPDFUnion stringSimple = SkPDFUnion::String("test ) string ( foo");
halcanary56586b62016-07-25 13:59:30 -0700198 assert_emit_eq(reporter, stringSimple, "(test \\) string \\( foo)");
vandebo@chromium.org8459d4e2010-09-24 22:25:30 +0000199
halcanaryf7a169e2015-05-01 07:35:45 -0700200 SkString stringComplexInput("\ttest ) string ( foo");
201 SkPDFUnion stringComplex = SkPDFUnion::String(stringComplexInput);
halcanary56586b62016-07-25 13:59:30 -0700202 assert_emit_eq(reporter, stringComplex, "(\\011test \\) string \\( foo)");
halcanaryee41b752016-06-23 14:08:11 -0700203
204 SkString binaryStringInput("\1\2\3\4\5\6\7\10\11\12\13\14\15\16\17\20");
205 SkPDFUnion binaryString = SkPDFUnion::String(binaryStringInput);
halcanary56586b62016-07-25 13:59:30 -0700206 assert_emit_eq(reporter, binaryString, "<0102030405060708090A0B0C0D0E0F10>");
vandebo@chromium.org8459d4e2010-09-24 22:25:30 +0000207
halcanaryf7a169e2015-05-01 07:35:45 -0700208 SkString nameInput("Test name\twith#tab");
209 SkPDFUnion name = SkPDFUnion::Name(nameInput);
halcanary56586b62016-07-25 13:59:30 -0700210 assert_emit_eq(reporter, name, "/Test#20name#09with#23tab");
halcanaryf7a169e2015-05-01 07:35:45 -0700211
212 SkString nameInput2("A#/%()<>[]{}B");
213 SkPDFUnion name2 = SkPDFUnion::Name(nameInput2);
halcanary56586b62016-07-25 13:59:30 -0700214 assert_emit_eq(reporter, name2, "/A#23#2F#25#28#29#3C#3E#5B#5D#7B#7DB");
halcanaryf7a169e2015-05-01 07:35:45 -0700215
216 SkPDFUnion name3 = SkPDFUnion::Name("SimpleNameWithOnlyPrintableASCII");
halcanary56586b62016-07-25 13:59:30 -0700217 assert_emit_eq(reporter, name3, "/SimpleNameWithOnlyPrintableASCII");
vandebo@chromium.org251a7662012-09-21 17:50:50 +0000218
vandebo@chromium.orgc0376fe2012-03-05 18:44:33 +0000219 // Test that we correctly handle characters with the high-bit set.
halcanaryf7a169e2015-05-01 07:35:45 -0700220 SkString highBitString("\xDE\xAD" "be\xEF");
221 SkPDFUnion highBitName = SkPDFUnion::Name(highBitString);
halcanary56586b62016-07-25 13:59:30 -0700222 assert_emit_eq(reporter, highBitName, "/#DE#ADbe#EF");
halcanaryf7a169e2015-05-01 07:35:45 -0700223}
vandebo@chromium.orgc0376fe2012-03-05 18:44:33 +0000224
halcanaryf7a169e2015-05-01 07:35:45 -0700225static void TestPDFArray(skiatest::Reporter* reporter) {
halcanary8103a342016-03-08 15:10:16 -0800226 sk_sp<SkPDFArray> array(new SkPDFArray);
halcanary56586b62016-07-25 13:59:30 -0700227 assert_emit_eq(reporter, *array, "[]");
vandebo@chromium.org8459d4e2010-09-24 22:25:30 +0000228
halcanaryf7a169e2015-05-01 07:35:45 -0700229 array->appendInt(42);
halcanary56586b62016-07-25 13:59:30 -0700230 assert_emit_eq(reporter, *array, "[42]");
halcanaryf7a169e2015-05-01 07:35:45 -0700231
232 array->appendScalar(SK_ScalarHalf);
halcanary56586b62016-07-25 13:59:30 -0700233 assert_emit_eq(reporter, *array, "[42 .5]");
halcanaryf7a169e2015-05-01 07:35:45 -0700234
235 array->appendInt(0);
halcanary56586b62016-07-25 13:59:30 -0700236 assert_emit_eq(reporter, *array, "[42 .5 0]");
halcanaryf7a169e2015-05-01 07:35:45 -0700237
238 array->appendBool(true);
halcanary56586b62016-07-25 13:59:30 -0700239 assert_emit_eq(reporter, *array, "[42 .5 0 true]");
halcanaryf7a169e2015-05-01 07:35:45 -0700240
241 array->appendName("ThisName");
halcanary56586b62016-07-25 13:59:30 -0700242 assert_emit_eq(reporter, *array, "[42 .5 0 true /ThisName]");
halcanaryf7a169e2015-05-01 07:35:45 -0700243
244 array->appendName(SkString("AnotherName"));
halcanary56586b62016-07-25 13:59:30 -0700245 assert_emit_eq(reporter, *array, "[42 .5 0 true /ThisName /AnotherName]");
halcanaryf7a169e2015-05-01 07:35:45 -0700246
247 array->appendString("This String");
halcanary56586b62016-07-25 13:59:30 -0700248 assert_emit_eq(reporter, *array,
halcanary8e9f5e32016-02-24 15:46:46 -0800249 "[42 .5 0 true /ThisName /AnotherName (This String)]");
halcanaryf7a169e2015-05-01 07:35:45 -0700250
251 array->appendString(SkString("Another String"));
halcanary56586b62016-07-25 13:59:30 -0700252 assert_emit_eq(reporter, *array,
halcanary8e9f5e32016-02-24 15:46:46 -0800253 "[42 .5 0 true /ThisName /AnotherName (This String) "
halcanaryf7a169e2015-05-01 07:35:45 -0700254 "(Another String)]");
255
halcanary8103a342016-03-08 15:10:16 -0800256 sk_sp<SkPDFArray> innerArray(new SkPDFArray);
halcanaryf7a169e2015-05-01 07:35:45 -0700257 innerArray->appendInt(-1);
halcanary8103a342016-03-08 15:10:16 -0800258 array->appendObject(std::move(innerArray));
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) [-1]]");
262
halcanary8103a342016-03-08 15:10:16 -0800263 sk_sp<SkPDFArray> referencedArray(new SkPDFArray);
halcanary530032a2016-08-18 14:22:52 -0700264 SkPDFObjNumMap catalog;
265 catalog.addObject(referencedArray.get());
266 REPORTER_ASSERT(reporter, catalog.getObjectNumber(
halcanaryf7a169e2015-05-01 07:35:45 -0700267 referencedArray.get()) == 1);
halcanary8103a342016-03-08 15:10:16 -0800268 array->appendObjRef(std::move(referencedArray));
halcanaryf7a169e2015-05-01 07:35:45 -0700269
270 SkString result = emit_to_string(*array, &catalog);
halcanary56586b62016-07-25 13:59:30 -0700271 assert_eq(reporter, result,
halcanary8e9f5e32016-02-24 15:46:46 -0800272 "[42 .5 0 true /ThisName /AnotherName (This String) "
halcanaryf7a169e2015-05-01 07:35:45 -0700273 "(Another String) [-1] 1 0 R]");
274}
275
276static void TestPDFDict(skiatest::Reporter* reporter) {
halcanary8103a342016-03-08 15:10:16 -0800277 sk_sp<SkPDFDict> dict(new SkPDFDict);
halcanary56586b62016-07-25 13:59:30 -0700278 assert_emit_eq(reporter, *dict, "<<>>");
halcanaryf7a169e2015-05-01 07:35:45 -0700279
280 dict->insertInt("n1", SkToSizeT(42));
halcanary56586b62016-07-25 13:59:30 -0700281 assert_emit_eq(reporter, *dict, "<</n1 42>>");
halcanaryf7a169e2015-05-01 07:35:45 -0700282
283 dict.reset(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", 42);
halcanary56586b62016-07-25 13:59:30 -0700287 assert_emit_eq(reporter, *dict, "<</n1 42>>");
halcanaryf7a169e2015-05-01 07:35:45 -0700288
289 dict->insertScalar("n2", SK_ScalarHalf);
290
halcanary130444f2015-04-25 06:45:07 -0700291 SkString n3("n3");
halcanary8103a342016-03-08 15:10:16 -0800292 sk_sp<SkPDFArray> innerArray(new SkPDFArray);
halcanaryf7a169e2015-05-01 07:35:45 -0700293 innerArray->appendInt(-100);
halcanary8103a342016-03-08 15:10:16 -0800294 dict->insertObject(n3, std::move(innerArray));
halcanary56586b62016-07-25 13:59:30 -0700295 assert_emit_eq(reporter, *dict, "<</n1 42\n/n2 .5\n/n3 [-100]>>");
vandebo@chromium.org8459d4e2010-09-24 22:25:30 +0000296
halcanaryf7a169e2015-05-01 07:35:45 -0700297 dict.reset(new SkPDFDict);
halcanary56586b62016-07-25 13:59:30 -0700298 assert_emit_eq(reporter, *dict, "<<>>");
halcanaryf7a169e2015-05-01 07:35:45 -0700299
300 dict->insertInt("n1", 24);
halcanary56586b62016-07-25 13:59:30 -0700301 assert_emit_eq(reporter, *dict, "<</n1 24>>");
halcanaryf7a169e2015-05-01 07:35:45 -0700302
303 dict->insertInt("n2", SkToSizeT(99));
halcanary56586b62016-07-25 13:59:30 -0700304 assert_emit_eq(reporter, *dict, "<</n1 24\n/n2 99>>");
halcanaryf7a169e2015-05-01 07:35:45 -0700305
306 dict->insertScalar("n3", SK_ScalarHalf);
halcanary56586b62016-07-25 13:59:30 -0700307 assert_emit_eq(reporter, *dict, "<</n1 24\n/n2 99\n/n3 .5>>");
halcanaryf7a169e2015-05-01 07:35:45 -0700308
309 dict->insertName("n4", "AName");
halcanary56586b62016-07-25 13:59:30 -0700310 assert_emit_eq(reporter, *dict, "<</n1 24\n/n2 99\n/n3 .5\n/n4 /AName>>");
halcanaryf7a169e2015-05-01 07:35:45 -0700311
312 dict->insertName("n5", SkString("AnotherName"));
halcanary56586b62016-07-25 13:59:30 -0700313 assert_emit_eq(reporter, *dict, "<</n1 24\n/n2 99\n/n3 .5\n/n4 /AName\n"
halcanaryf7a169e2015-05-01 07:35:45 -0700314 "/n5 /AnotherName>>");
315
316 dict->insertString("n6", "A String");
halcanary56586b62016-07-25 13:59:30 -0700317 assert_emit_eq(reporter, *dict, "<</n1 24\n/n2 99\n/n3 .5\n/n4 /AName\n"
halcanaryf7a169e2015-05-01 07:35:45 -0700318 "/n5 /AnotherName\n/n6 (A String)>>");
319
320 dict->insertString("n7", SkString("Another String"));
halcanary56586b62016-07-25 13:59:30 -0700321 assert_emit_eq(reporter, *dict, "<</n1 24\n/n2 99\n/n3 .5\n/n4 /AName\n"
halcanaryf7a169e2015-05-01 07:35:45 -0700322 "/n5 /AnotherName\n/n6 (A String)\n/n7 (Another String)>>");
323
324 dict.reset(new SkPDFDict("DType"));
halcanary56586b62016-07-25 13:59:30 -0700325 assert_emit_eq(reporter, *dict, "<</Type /DType>>");
halcanary8e9f5e32016-02-24 15:46:46 -0800326
halcanary8103a342016-03-08 15:10:16 -0800327 sk_sp<SkPDFArray> referencedArray(new SkPDFArray);
halcanary530032a2016-08-18 14:22:52 -0700328 SkPDFObjNumMap catalog;
329 catalog.addObject(referencedArray.get());
330 REPORTER_ASSERT(reporter, catalog.getObjectNumber(
halcanaryf7a169e2015-05-01 07:35:45 -0700331 referencedArray.get()) == 1);
halcanary8103a342016-03-08 15:10:16 -0800332 dict->insertObjRef("n1", std::move(referencedArray));
halcanaryf7a169e2015-05-01 07:35:45 -0700333 SkString result = emit_to_string(*dict, &catalog);
halcanary56586b62016-07-25 13:59:30 -0700334 assert_eq(reporter, result, "<</Type /DType\n/n1 1 0 R>>");
halcanaryf7a169e2015-05-01 07:35:45 -0700335}
336
halcanary57f744e2016-09-09 11:41:59 -0700337DEF_TEST(SkPDF_Primitives, reporter) {
halcanaryf7a169e2015-05-01 07:35:45 -0700338 TestPDFUnion(reporter);
339 TestPDFArray(reporter);
340 TestPDFDict(reporter);
vandebo@chromium.org421d6442011-07-20 17:39:01 +0000341 TestPDFStream(reporter);
halcanaryf7a169e2015-05-01 07:35:45 -0700342 TestObjectNumberMap(reporter);
vandebo@chromium.org8459d4e2010-09-24 22:25:30 +0000343 TestObjectRef(reporter);
sugoi@google.com54f0d1b2013-02-27 19:17:41 +0000344 test_issue1083();
vandebo@chromium.org8459d4e2010-09-24 22:25:30 +0000345}
senorblancob0e89dc2014-10-20 14:03:12 -0700346
347namespace {
348
349class DummyImageFilter : public SkImageFilter {
350public:
robertphillips43c2ad42016-04-04 05:05:11 -0700351 static sk_sp<DummyImageFilter> Make(bool visited = false) {
352 return sk_sp<DummyImageFilter>(new DummyImageFilter(visited));
353 }
354
355 SK_TO_STRING_OVERRIDE()
356 SK_DECLARE_PUBLIC_FLATTENABLE_DESERIALIZATION_PROCS(DummyImageFilter)
357 bool visited() const { return fVisited; }
358
359protected:
robertphillips4ba94e22016-04-04 12:07:47 -0700360 sk_sp<SkSpecialImage> onFilterImage(SkSpecialImage* source, const Context&,
361 SkIPoint* offset) const override {
senorblancob0e89dc2014-10-20 14:03:12 -0700362 fVisited = true;
363 offset->fX = offset->fY = 0;
robertphillips4ba94e22016-04-04 12:07:47 -0700364 return sk_ref_sp<SkSpecialImage>(source);
senorblancob0e89dc2014-10-20 14:03:12 -0700365 }
senorblancob0e89dc2014-10-20 14:03:12 -0700366
367private:
robertphillips43c2ad42016-04-04 05:05:11 -0700368 DummyImageFilter(bool visited) : INHERITED(nullptr, 0, nullptr), fVisited(visited) {}
369
senorblancob0e89dc2014-10-20 14:03:12 -0700370 mutable bool fVisited;
robertphillips43c2ad42016-04-04 05:05:11 -0700371
372 typedef SkImageFilter INHERITED;
senorblancob0e89dc2014-10-20 14:03:12 -0700373};
374
reed60c9b582016-04-03 09:11:13 -0700375sk_sp<SkFlattenable> DummyImageFilter::CreateProc(SkReadBuffer& buffer) {
senorblancob0e89dc2014-10-20 14:03:12 -0700376 SK_IMAGEFILTER_UNFLATTEN_COMMON(common, 0);
377 bool visited = buffer.readBool();
robertphillips43c2ad42016-04-04 05:05:11 -0700378 return DummyImageFilter::Make(visited);
senorblancob0e89dc2014-10-20 14:03:12 -0700379}
380
robertphillipsf3f5bad2014-12-19 13:49:15 -0800381#ifndef SK_IGNORE_TO_STRING
382void DummyImageFilter::toString(SkString* str) const {
383 str->appendf("DummyImageFilter: (");
384 str->append(")");
385}
386#endif
387
senorblancob0e89dc2014-10-20 14:03:12 -0700388};
389
390// Check that PDF rendering of image filters successfully falls back to
391// CPU rasterization.
halcanary57f744e2016-09-09 11:41:59 -0700392DEF_TEST(SkPDF_ImageFilter, reporter) {
393 REQUIRE_PDF_DOCUMENT(SkPDF_ImageFilter, reporter);
halcanary3d32d502015-03-01 06:55:20 -0800394 SkDynamicMemoryWStream stream;
halcanary4b656662016-04-27 07:45:18 -0700395 sk_sp<SkDocument> doc(SkDocument::MakePDF(&stream));
halcanary3d32d502015-03-01 06:55:20 -0800396 SkCanvas* canvas = doc->beginPage(100.0f, 100.0f);
397
robertphillips43c2ad42016-04-04 05:05:11 -0700398 sk_sp<DummyImageFilter> filter(DummyImageFilter::Make());
senorblancob0e89dc2014-10-20 14:03:12 -0700399
400 // Filter just created; should be unvisited.
401 REPORTER_ASSERT(reporter, !filter->visited());
402 SkPaint paint;
Mike Reed5e257172016-11-01 11:22:05 -0400403 paint.setImageFilter(filter);
halcanary3d32d502015-03-01 06:55:20 -0800404 canvas->drawRect(SkRect::MakeWH(100, 100), paint);
405 doc->close();
senorblancob0e89dc2014-10-20 14:03:12 -0700406
407 // Filter was used in rendering; should be visited.
408 REPORTER_ASSERT(reporter, filter->visited());
409}
halcanary66a82f32015-10-12 13:05:04 -0700410
411// Check that PDF rendering of image filters successfully falls back to
412// CPU rasterization.
halcanary57f744e2016-09-09 11:41:59 -0700413DEF_TEST(SkPDF_FontCanEmbedTypeface, reporter) {
halcanary66a82f32015-10-12 13:05:04 -0700414 SkPDFCanon canon;
415
416 const char resource[] = "fonts/Roboto2-Regular_NoEmbed.ttf";
bungeman13b9c952016-05-12 10:09:30 -0700417 sk_sp<SkTypeface> noEmbedTypeface(MakeResourceAsTypeface(resource));
halcanary66a82f32015-10-12 13:05:04 -0700418 if (noEmbedTypeface) {
419 REPORTER_ASSERT(reporter,
halcanary8103a342016-03-08 15:10:16 -0800420 !SkPDFFont::CanEmbedTypeface(noEmbedTypeface.get(), &canon));
halcanary66a82f32015-10-12 13:05:04 -0700421 }
halcanary8103a342016-03-08 15:10:16 -0800422 sk_sp<SkTypeface> portableTypeface(
mbocee6a9912016-05-31 11:42:36 -0700423 sk_tool_utils::create_portable_typeface(NULL, SkFontStyle()));
halcanary66a82f32015-10-12 13:05:04 -0700424 REPORTER_ASSERT(reporter,
halcanary8103a342016-03-08 15:10:16 -0800425 SkPDFFont::CanEmbedTypeface(portableTypeface.get(), &canon));
halcanary66a82f32015-10-12 13:05:04 -0700426}
halcanary8e9f5e32016-02-24 15:46:46 -0800427
428
429// test to see that all finite scalars round trip via scanf().
430static void check_pdf_scalar_serialization(
431 skiatest::Reporter* reporter, float inputFloat) {
432 char floatString[SkPDFUtils::kMaximumFloatDecimalLength];
433 size_t len = SkPDFUtils::FloatToDecimal(inputFloat, floatString);
434 if (len >= sizeof(floatString)) {
435 ERRORF(reporter, "string too long: %u", (unsigned)len);
436 return;
437 }
438 if (floatString[len] != '\0' || strlen(floatString) != len) {
439 ERRORF(reporter, "terminator misplaced.");
440 return; // The terminator is needed for sscanf().
441 }
442 if (reporter->verbose()) {
443 SkDebugf("%15.9g = \"%s\"\n", inputFloat, floatString);
444 }
445 float roundTripFloat;
446 if (1 != sscanf(floatString, "%f", &roundTripFloat)) {
447 ERRORF(reporter, "unscannable result: %s", floatString);
448 return;
449 }
halcanary56586b62016-07-25 13:59:30 -0700450 if (std::isfinite(inputFloat) && roundTripFloat != inputFloat) {
halcanary8e9f5e32016-02-24 15:46:46 -0800451 ERRORF(reporter, "roundTripFloat (%.9g) != inputFloat (%.9g)",
452 roundTripFloat, inputFloat);
453 }
454}
455
456// Test SkPDFUtils::AppendScalar for accuracy.
halcanary57f744e2016-09-09 11:41:59 -0700457DEF_TEST(SkPDF_Primitives_Scalar, reporter) {
halcanary8e9f5e32016-02-24 15:46:46 -0800458 SkRandom random(0x5EED);
459 int iterationCount = 512;
460 while (iterationCount-- > 0) {
461 union { uint32_t u; float f; };
462 u = random.nextU();
463 static_assert(sizeof(float) == sizeof(uint32_t), "");
464 check_pdf_scalar_serialization(reporter, f);
465 }
466 float alwaysCheck[] = {
467 0.0f, -0.0f, 1.0f, -1.0f, SK_ScalarPI, 0.1f, FLT_MIN, FLT_MAX,
468 -FLT_MIN, -FLT_MAX, FLT_MIN / 16.0f, -FLT_MIN / 16.0f,
469 SK_FloatNaN, SK_FloatInfinity, SK_FloatNegativeInfinity,
470 -FLT_MIN / 8388608.0
471 };
472 for (float inputFloat: alwaysCheck) {
473 check_pdf_scalar_serialization(reporter, inputFloat);
474 }
475}
halcanaryeb92cb32016-07-15 13:41:27 -0700476
477// Test SkPDFUtils:: for accuracy.
halcanary57f744e2016-09-09 11:41:59 -0700478DEF_TEST(SkPDF_Primitives_Color, reporter) {
halcanaryeb92cb32016-07-15 13:41:27 -0700479 char buffer[5];
480 for (int i = 0; i < 256; ++i) {
481 size_t len = SkPDFUtils::ColorToDecimal(i, buffer);
482 REPORTER_ASSERT(reporter, len == strlen(buffer));
483 float f;
484 REPORTER_ASSERT(reporter, 1 == sscanf(buffer, "%f", &f));
485 int roundTrip = (int)(0.5 + f * 255);
486 REPORTER_ASSERT(reporter, roundTrip == i);
487 }
488}