blob: f6169b3b3367257fe655c1ee681de153d826910f [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
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "tests/Test.h"
Hal Canary43fb7a02016-12-30 13:09:03 -05009
10#ifdef SK_SUPPORT_PDF
11
Mike Kleinc0bd9f92019-04-23 12:05:21 -050012#include "include/core/SkBitmap.h"
13#include "include/core/SkCanvas.h"
14#include "include/core/SkData.h"
15#include "include/core/SkImageEncoder.h"
16#include "include/core/SkMatrix.h"
17#include "include/core/SkScalar.h"
18#include "include/core/SkStream.h"
19#include "include/core/SkTypes.h"
Hal Canaryd7cf0be2019-07-30 12:23:10 -040020#include "include/effects/SkMorphologyImageFilter.h"
21#include "include/effects/SkPerlinNoiseShader.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050022#include "include/private/SkTo.h"
23#include "src/core/SkGlyphRun.h"
Michael Ludwig8ee6cf32019-08-02 09:57:04 -040024#include "src/core/SkImageFilter_Base.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050025#include "src/core/SkReadBuffer.h"
26#include "src/core/SkSpecialImage.h"
27#include "src/pdf/SkClusterator.h"
28#include "src/pdf/SkDeflate.h"
29#include "src/pdf/SkPDFDevice.h"
30#include "src/pdf/SkPDFDocumentPriv.h"
31#include "src/pdf/SkPDFFont.h"
32#include "src/pdf/SkPDFTypes.h"
33#include "src/pdf/SkPDFUnion.h"
34#include "src/pdf/SkPDFUtils.h"
35#include "tools/Resources.h"
36#include "tools/ToolUtils.h"
vandebo@chromium.org8459d4e2010-09-24 22:25:30 +000037
halcanary56586b62016-07-25 13:59:30 -070038#include <cstdlib>
39#include <cmath>
40
commit-bot@chromium.org608ea652013-10-03 19:29:21 +000041#define DUMMY_TEXT "DCT compessed stream."
42
halcanaryf7a169e2015-05-01 07:35:45 -070043template <typename T>
Hal Canaryf6462c42018-11-13 16:19:59 -050044static SkString emit_to_string(T& obj) {
halcanary6a144342015-01-23 11:45:10 -080045 SkDynamicMemoryWStream buffer;
Hal Canaryf6462c42018-11-13 16:19:59 -050046 obj.emitObject(&buffer);
halcanary56586b62016-07-25 13:59:30 -070047 SkString tmp(buffer.bytesWritten());
48 buffer.copyTo(tmp.writable_str());
halcanaryf7a169e2015-05-01 07:35:45 -070049 return tmp;
vandebo@chromium.org8459d4e2010-09-24 22:25:30 +000050}
51
halcanaryf7a169e2015-05-01 07:35:45 -070052static bool eq(const SkString& str, const char* strPtr, size_t len) {
53 return len == str.size() && 0 == memcmp(str.c_str(), strPtr, len);
vandebo@chromium.org421d6442011-07-20 17:39:01 +000054}
55
halcanary56586b62016-07-25 13:59:30 -070056static void assert_eql(skiatest::Reporter* reporter,
57 const SkString& skString,
58 const char* str,
59 size_t len) {
60 if (!eq(skString, str, len)) {
61 REPORT_FAILURE(reporter, "", SkStringPrintf(
62 "'%*s' != '%s'", len, str, skString.c_str()));
63 }
64}
halcanaryf7a169e2015-05-01 07:35:45 -070065
halcanary56586b62016-07-25 13:59:30 -070066static void assert_eq(skiatest::Reporter* reporter,
67 const SkString& skString,
68 const char* str) {
69 assert_eql(reporter, skString, str, strlen(str));
70}
halcanaryf7a169e2015-05-01 07:35:45 -070071
72
halcanary56586b62016-07-25 13:59:30 -070073template <typename T>
74static void assert_emit_eq(skiatest::Reporter* reporter,
75 T& object,
76 const char* string) {
77 SkString result = emit_to_string(object);
78 assert_eq(reporter, result, string);
79}
halcanaryf7a169e2015-05-01 07:35:45 -070080
vandebo@chromium.org4e1cc6a2013-01-25 19:27:23 +000081// This test used to assert without the fix submitted for
82// http://code.google.com/p/skia/issues/detail?id=1083.
83// SKP files might have invalid glyph ids. This test ensures they are ignored,
84// and there is no assert on input data in Debug mode.
sugoi@google.com54f0d1b2013-02-27 19:17:41 +000085static void test_issue1083() {
halcanary3d32d502015-03-01 06:55:20 -080086 SkDynamicMemoryWStream outStream;
Hal Canary3026d4b2019-01-07 10:00:48 -050087 auto doc = SkPDF::MakeDocument(&outStream);
halcanary3d32d502015-03-01 06:55:20 -080088 SkCanvas* canvas = doc->beginPage(100.0f, 100.0f);
vandebo@chromium.org4e1cc6a2013-01-25 19:27:23 +000089
90 uint16_t glyphID = 65000;
Ben Wagner51e15a62019-05-07 15:38:46 -040091 canvas->drawSimpleText(&glyphID, 2, SkTextEncoding::kGlyphID, 0, 0, SkFont(), SkPaint());
vandebo@chromium.org4e1cc6a2013-01-25 19:27:23 +000092
halcanary3d32d502015-03-01 06:55:20 -080093 doc->close();
vandebo@chromium.org4e1cc6a2013-01-25 19:27:23 +000094}
95
halcanary56586b62016-07-25 13:59:30 -070096static void assert_emit_eq_number(skiatest::Reporter* reporter, float number) {
97 SkPDFUnion pdfUnion = SkPDFUnion::Scalar(number);
98 SkString result = emit_to_string(pdfUnion);
99 float value = static_cast<float>(std::atof(result.c_str()));
100 if (value != number) {
101 ERRORF(reporter, "%.9g != %s", number, result.c_str());
102 }
103}
104
105
halcanaryf7a169e2015-05-01 07:35:45 -0700106static void TestPDFUnion(skiatest::Reporter* reporter) {
107 SkPDFUnion boolTrue = SkPDFUnion::Bool(true);
halcanary56586b62016-07-25 13:59:30 -0700108 assert_emit_eq(reporter, boolTrue, "true");
vandebo@chromium.org8459d4e2010-09-24 22:25:30 +0000109
halcanaryf7a169e2015-05-01 07:35:45 -0700110 SkPDFUnion boolFalse = SkPDFUnion::Bool(false);
halcanary56586b62016-07-25 13:59:30 -0700111 assert_emit_eq(reporter, boolFalse, "false");
vandebo@chromium.org8459d4e2010-09-24 22:25:30 +0000112
halcanaryf7a169e2015-05-01 07:35:45 -0700113 SkPDFUnion int42 = SkPDFUnion::Int(42);
halcanary56586b62016-07-25 13:59:30 -0700114 assert_emit_eq(reporter, int42, "42");
halcanaryf7a169e2015-05-01 07:35:45 -0700115
halcanary56586b62016-07-25 13:59:30 -0700116 assert_emit_eq_number(reporter, SK_ScalarHalf);
117 assert_emit_eq_number(reporter, 110999.75f); // bigScalar
118 assert_emit_eq_number(reporter, 50000000.1f); // biggerScalar
119 assert_emit_eq_number(reporter, 1.0f / 65536); // smallScalar
vandebo@chromium.org094316b2011-03-04 03:15:13 +0000120
halcanaryf7a169e2015-05-01 07:35:45 -0700121 SkPDFUnion stringSimple = SkPDFUnion::String("test ) string ( foo");
halcanary56586b62016-07-25 13:59:30 -0700122 assert_emit_eq(reporter, stringSimple, "(test \\) string \\( foo)");
vandebo@chromium.org8459d4e2010-09-24 22:25:30 +0000123
halcanaryf7a169e2015-05-01 07:35:45 -0700124 SkString stringComplexInput("\ttest ) string ( foo");
125 SkPDFUnion stringComplex = SkPDFUnion::String(stringComplexInput);
halcanary56586b62016-07-25 13:59:30 -0700126 assert_emit_eq(reporter, stringComplex, "(\\011test \\) string \\( foo)");
halcanaryee41b752016-06-23 14:08:11 -0700127
128 SkString binaryStringInput("\1\2\3\4\5\6\7\10\11\12\13\14\15\16\17\20");
129 SkPDFUnion binaryString = SkPDFUnion::String(binaryStringInput);
halcanary56586b62016-07-25 13:59:30 -0700130 assert_emit_eq(reporter, binaryString, "<0102030405060708090A0B0C0D0E0F10>");
vandebo@chromium.org8459d4e2010-09-24 22:25:30 +0000131
halcanaryf7a169e2015-05-01 07:35:45 -0700132 SkString nameInput("Test name\twith#tab");
133 SkPDFUnion name = SkPDFUnion::Name(nameInput);
halcanary56586b62016-07-25 13:59:30 -0700134 assert_emit_eq(reporter, name, "/Test#20name#09with#23tab");
halcanaryf7a169e2015-05-01 07:35:45 -0700135
136 SkString nameInput2("A#/%()<>[]{}B");
137 SkPDFUnion name2 = SkPDFUnion::Name(nameInput2);
halcanary56586b62016-07-25 13:59:30 -0700138 assert_emit_eq(reporter, name2, "/A#23#2F#25#28#29#3C#3E#5B#5D#7B#7DB");
halcanaryf7a169e2015-05-01 07:35:45 -0700139
140 SkPDFUnion name3 = SkPDFUnion::Name("SimpleNameWithOnlyPrintableASCII");
halcanary56586b62016-07-25 13:59:30 -0700141 assert_emit_eq(reporter, name3, "/SimpleNameWithOnlyPrintableASCII");
vandebo@chromium.org251a7662012-09-21 17:50:50 +0000142
vandebo@chromium.orgc0376fe2012-03-05 18:44:33 +0000143 // Test that we correctly handle characters with the high-bit set.
halcanaryf7a169e2015-05-01 07:35:45 -0700144 SkString highBitString("\xDE\xAD" "be\xEF");
145 SkPDFUnion highBitName = SkPDFUnion::Name(highBitString);
halcanary56586b62016-07-25 13:59:30 -0700146 assert_emit_eq(reporter, highBitName, "/#DE#ADbe#EF");
Hal Canary3d49d082019-10-11 13:29:42 -0400147
148 // https://bugs.skia.org/9508
149 // https://crbug.com/494913
150 // Trailing '\0' characters must be removed.
151 const char nameInput4[] = "Test name with nil\0";
152 SkPDFUnion name4 = SkPDFUnion::Name(SkString(nameInput4, strlen(nameInput4) + 1));
153 assert_emit_eq(reporter, name4, "/Test#20name#20with#20nil");
halcanaryf7a169e2015-05-01 07:35:45 -0700154}
vandebo@chromium.orgc0376fe2012-03-05 18:44:33 +0000155
halcanaryf7a169e2015-05-01 07:35:45 -0700156static void TestPDFArray(skiatest::Reporter* reporter) {
Hal Canary74801582018-12-18 16:30:41 -0500157 std::unique_ptr<SkPDFArray> array(new SkPDFArray);
halcanary56586b62016-07-25 13:59:30 -0700158 assert_emit_eq(reporter, *array, "[]");
vandebo@chromium.org8459d4e2010-09-24 22:25:30 +0000159
halcanaryf7a169e2015-05-01 07:35:45 -0700160 array->appendInt(42);
halcanary56586b62016-07-25 13:59:30 -0700161 assert_emit_eq(reporter, *array, "[42]");
halcanaryf7a169e2015-05-01 07:35:45 -0700162
163 array->appendScalar(SK_ScalarHalf);
halcanary56586b62016-07-25 13:59:30 -0700164 assert_emit_eq(reporter, *array, "[42 .5]");
halcanaryf7a169e2015-05-01 07:35:45 -0700165
166 array->appendInt(0);
halcanary56586b62016-07-25 13:59:30 -0700167 assert_emit_eq(reporter, *array, "[42 .5 0]");
halcanaryf7a169e2015-05-01 07:35:45 -0700168
169 array->appendBool(true);
halcanary56586b62016-07-25 13:59:30 -0700170 assert_emit_eq(reporter, *array, "[42 .5 0 true]");
halcanaryf7a169e2015-05-01 07:35:45 -0700171
172 array->appendName("ThisName");
halcanary56586b62016-07-25 13:59:30 -0700173 assert_emit_eq(reporter, *array, "[42 .5 0 true /ThisName]");
halcanaryf7a169e2015-05-01 07:35:45 -0700174
175 array->appendName(SkString("AnotherName"));
halcanary56586b62016-07-25 13:59:30 -0700176 assert_emit_eq(reporter, *array, "[42 .5 0 true /ThisName /AnotherName]");
halcanaryf7a169e2015-05-01 07:35:45 -0700177
178 array->appendString("This String");
halcanary56586b62016-07-25 13:59:30 -0700179 assert_emit_eq(reporter, *array,
halcanary8e9f5e32016-02-24 15:46:46 -0800180 "[42 .5 0 true /ThisName /AnotherName (This String)]");
halcanaryf7a169e2015-05-01 07:35:45 -0700181
182 array->appendString(SkString("Another String"));
halcanary56586b62016-07-25 13:59:30 -0700183 assert_emit_eq(reporter, *array,
halcanary8e9f5e32016-02-24 15:46:46 -0800184 "[42 .5 0 true /ThisName /AnotherName (This String) "
halcanaryf7a169e2015-05-01 07:35:45 -0700185 "(Another String)]");
186
Hal Canary74801582018-12-18 16:30:41 -0500187 std::unique_ptr<SkPDFArray> innerArray(new SkPDFArray);
halcanaryf7a169e2015-05-01 07:35:45 -0700188 innerArray->appendInt(-1);
halcanary8103a342016-03-08 15:10:16 -0800189 array->appendObject(std::move(innerArray));
halcanary56586b62016-07-25 13:59:30 -0700190 assert_emit_eq(reporter, *array,
halcanary8e9f5e32016-02-24 15:46:46 -0800191 "[42 .5 0 true /ThisName /AnotherName (This String) "
halcanaryf7a169e2015-05-01 07:35:45 -0700192 "(Another String) [-1]]");
halcanaryf7a169e2015-05-01 07:35:45 -0700193}
194
195static void TestPDFDict(skiatest::Reporter* reporter) {
Hal Canary74801582018-12-18 16:30:41 -0500196 std::unique_ptr<SkPDFDict> dict(new SkPDFDict);
halcanary56586b62016-07-25 13:59:30 -0700197 assert_emit_eq(reporter, *dict, "<<>>");
halcanaryf7a169e2015-05-01 07:35:45 -0700198
199 dict->insertInt("n1", SkToSizeT(42));
halcanary56586b62016-07-25 13:59:30 -0700200 assert_emit_eq(reporter, *dict, "<</n1 42>>");
halcanaryf7a169e2015-05-01 07:35:45 -0700201
202 dict.reset(new SkPDFDict);
halcanary56586b62016-07-25 13:59:30 -0700203 assert_emit_eq(reporter, *dict, "<<>>");
halcanaryf7a169e2015-05-01 07:35:45 -0700204
205 dict->insertInt("n1", 42);
halcanary56586b62016-07-25 13:59:30 -0700206 assert_emit_eq(reporter, *dict, "<</n1 42>>");
halcanaryf7a169e2015-05-01 07:35:45 -0700207
208 dict->insertScalar("n2", SK_ScalarHalf);
209
halcanary130444f2015-04-25 06:45:07 -0700210 SkString n3("n3");
Hal Canary74801582018-12-18 16:30:41 -0500211 std::unique_ptr<SkPDFArray> innerArray(new SkPDFArray);
halcanaryf7a169e2015-05-01 07:35:45 -0700212 innerArray->appendInt(-100);
halcanary8103a342016-03-08 15:10:16 -0800213 dict->insertObject(n3, std::move(innerArray));
halcanary56586b62016-07-25 13:59:30 -0700214 assert_emit_eq(reporter, *dict, "<</n1 42\n/n2 .5\n/n3 [-100]>>");
vandebo@chromium.org8459d4e2010-09-24 22:25:30 +0000215
halcanaryf7a169e2015-05-01 07:35:45 -0700216 dict.reset(new SkPDFDict);
halcanary56586b62016-07-25 13:59:30 -0700217 assert_emit_eq(reporter, *dict, "<<>>");
halcanaryf7a169e2015-05-01 07:35:45 -0700218
219 dict->insertInt("n1", 24);
halcanary56586b62016-07-25 13:59:30 -0700220 assert_emit_eq(reporter, *dict, "<</n1 24>>");
halcanaryf7a169e2015-05-01 07:35:45 -0700221
222 dict->insertInt("n2", SkToSizeT(99));
halcanary56586b62016-07-25 13:59:30 -0700223 assert_emit_eq(reporter, *dict, "<</n1 24\n/n2 99>>");
halcanaryf7a169e2015-05-01 07:35:45 -0700224
225 dict->insertScalar("n3", SK_ScalarHalf);
halcanary56586b62016-07-25 13:59:30 -0700226 assert_emit_eq(reporter, *dict, "<</n1 24\n/n2 99\n/n3 .5>>");
halcanaryf7a169e2015-05-01 07:35:45 -0700227
228 dict->insertName("n4", "AName");
halcanary56586b62016-07-25 13:59:30 -0700229 assert_emit_eq(reporter, *dict, "<</n1 24\n/n2 99\n/n3 .5\n/n4 /AName>>");
halcanaryf7a169e2015-05-01 07:35:45 -0700230
231 dict->insertName("n5", SkString("AnotherName"));
halcanary56586b62016-07-25 13:59:30 -0700232 assert_emit_eq(reporter, *dict, "<</n1 24\n/n2 99\n/n3 .5\n/n4 /AName\n"
halcanaryf7a169e2015-05-01 07:35:45 -0700233 "/n5 /AnotherName>>");
234
235 dict->insertString("n6", "A String");
halcanary56586b62016-07-25 13:59:30 -0700236 assert_emit_eq(reporter, *dict, "<</n1 24\n/n2 99\n/n3 .5\n/n4 /AName\n"
halcanaryf7a169e2015-05-01 07:35:45 -0700237 "/n5 /AnotherName\n/n6 (A String)>>");
238
239 dict->insertString("n7", SkString("Another String"));
halcanary56586b62016-07-25 13:59:30 -0700240 assert_emit_eq(reporter, *dict, "<</n1 24\n/n2 99\n/n3 .5\n/n4 /AName\n"
halcanaryf7a169e2015-05-01 07:35:45 -0700241 "/n5 /AnotherName\n/n6 (A String)\n/n7 (Another String)>>");
242
243 dict.reset(new SkPDFDict("DType"));
halcanary56586b62016-07-25 13:59:30 -0700244 assert_emit_eq(reporter, *dict, "<</Type /DType>>");
halcanaryf7a169e2015-05-01 07:35:45 -0700245}
246
halcanary57f744e2016-09-09 11:41:59 -0700247DEF_TEST(SkPDF_Primitives, reporter) {
halcanaryf7a169e2015-05-01 07:35:45 -0700248 TestPDFUnion(reporter);
249 TestPDFArray(reporter);
250 TestPDFDict(reporter);
sugoi@google.com54f0d1b2013-02-27 19:17:41 +0000251 test_issue1083();
vandebo@chromium.org8459d4e2010-09-24 22:25:30 +0000252}
senorblancob0e89dc2014-10-20 14:03:12 -0700253
254namespace {
255
Michael Ludwig8ee6cf32019-08-02 09:57:04 -0400256class DummyImageFilter : public SkImageFilter_Base {
senorblancob0e89dc2014-10-20 14:03:12 -0700257public:
robertphillips43c2ad42016-04-04 05:05:11 -0700258 static sk_sp<DummyImageFilter> Make(bool visited = false) {
259 return sk_sp<DummyImageFilter>(new DummyImageFilter(visited));
260 }
261
robertphillips43c2ad42016-04-04 05:05:11 -0700262 bool visited() const { return fVisited; }
263
264protected:
Michael Ludwige30a4852019-08-14 14:35:42 -0400265 sk_sp<SkSpecialImage> onFilterImage(const Context& ctx, SkIPoint* offset) const override {
senorblancob0e89dc2014-10-20 14:03:12 -0700266 fVisited = true;
267 offset->fX = offset->fY = 0;
Michael Ludwige30a4852019-08-14 14:35:42 -0400268 return sk_ref_sp<SkSpecialImage>(ctx.sourceImage());
senorblancob0e89dc2014-10-20 14:03:12 -0700269 }
senorblancob0e89dc2014-10-20 14:03:12 -0700270
271private:
Mike Klein4fee3232018-10-18 17:27:16 -0400272 SK_FLATTENABLE_HOOKS(DummyImageFilter)
robertphillips43c2ad42016-04-04 05:05:11 -0700273 DummyImageFilter(bool visited) : INHERITED(nullptr, 0, nullptr), fVisited(visited) {}
274
senorblancob0e89dc2014-10-20 14:03:12 -0700275 mutable bool fVisited;
robertphillips43c2ad42016-04-04 05:05:11 -0700276
Michael Ludwig8ee6cf32019-08-02 09:57:04 -0400277 typedef SkImageFilter_Base INHERITED;
senorblancob0e89dc2014-10-20 14:03:12 -0700278};
279
reed60c9b582016-04-03 09:11:13 -0700280sk_sp<SkFlattenable> DummyImageFilter::CreateProc(SkReadBuffer& buffer) {
senorblancob0e89dc2014-10-20 14:03:12 -0700281 SK_IMAGEFILTER_UNFLATTEN_COMMON(common, 0);
282 bool visited = buffer.readBool();
robertphillips43c2ad42016-04-04 05:05:11 -0700283 return DummyImageFilter::Make(visited);
senorblancob0e89dc2014-10-20 14:03:12 -0700284}
285
286};
287
288// Check that PDF rendering of image filters successfully falls back to
289// CPU rasterization.
halcanary57f744e2016-09-09 11:41:59 -0700290DEF_TEST(SkPDF_ImageFilter, reporter) {
291 REQUIRE_PDF_DOCUMENT(SkPDF_ImageFilter, reporter);
halcanary3d32d502015-03-01 06:55:20 -0800292 SkDynamicMemoryWStream stream;
Hal Canary3026d4b2019-01-07 10:00:48 -0500293 auto doc = SkPDF::MakeDocument(&stream);
halcanary3d32d502015-03-01 06:55:20 -0800294 SkCanvas* canvas = doc->beginPage(100.0f, 100.0f);
295
robertphillips43c2ad42016-04-04 05:05:11 -0700296 sk_sp<DummyImageFilter> filter(DummyImageFilter::Make());
senorblancob0e89dc2014-10-20 14:03:12 -0700297
298 // Filter just created; should be unvisited.
299 REPORTER_ASSERT(reporter, !filter->visited());
300 SkPaint paint;
Mike Reed5e257172016-11-01 11:22:05 -0400301 paint.setImageFilter(filter);
halcanary3d32d502015-03-01 06:55:20 -0800302 canvas->drawRect(SkRect::MakeWH(100, 100), paint);
303 doc->close();
senorblancob0e89dc2014-10-20 14:03:12 -0700304
305 // Filter was used in rendering; should be visited.
306 REPORTER_ASSERT(reporter, filter->visited());
307}
halcanary66a82f32015-10-12 13:05:04 -0700308
309// Check that PDF rendering of image filters successfully falls back to
310// CPU rasterization.
halcanary57f744e2016-09-09 11:41:59 -0700311DEF_TEST(SkPDF_FontCanEmbedTypeface, reporter) {
Hal Canary4ca9fa32018-12-21 16:15:01 -0500312 SkNullWStream nullWStream;
313 SkPDFDocument doc(&nullWStream, SkPDF::Metadata());
halcanary66a82f32015-10-12 13:05:04 -0700314
315 const char resource[] = "fonts/Roboto2-Regular_NoEmbed.ttf";
bungeman13b9c952016-05-12 10:09:30 -0700316 sk_sp<SkTypeface> noEmbedTypeface(MakeResourceAsTypeface(resource));
halcanary66a82f32015-10-12 13:05:04 -0700317 if (noEmbedTypeface) {
318 REPORTER_ASSERT(reporter,
Hal Canary4ca9fa32018-12-21 16:15:01 -0500319 !SkPDFFont::CanEmbedTypeface(noEmbedTypeface.get(), &doc));
halcanary66a82f32015-10-12 13:05:04 -0700320 }
Mike Kleinea3f0142019-03-20 11:12:10 -0500321 sk_sp<SkTypeface> portableTypeface(ToolUtils::create_portable_typeface(nullptr, SkFontStyle()));
halcanary66a82f32015-10-12 13:05:04 -0700322 REPORTER_ASSERT(reporter,
Hal Canary4ca9fa32018-12-21 16:15:01 -0500323 SkPDFFont::CanEmbedTypeface(portableTypeface.get(), &doc));
halcanary66a82f32015-10-12 13:05:04 -0700324}
halcanary8e9f5e32016-02-24 15:46:46 -0800325
326
327// test to see that all finite scalars round trip via scanf().
328static void check_pdf_scalar_serialization(
329 skiatest::Reporter* reporter, float inputFloat) {
Hal Canary3c36ef62018-01-02 16:25:53 -0500330 char floatString[kMaximumSkFloatToDecimalLength];
331 size_t len = SkFloatToDecimal(inputFloat, floatString);
halcanary8e9f5e32016-02-24 15:46:46 -0800332 if (len >= sizeof(floatString)) {
333 ERRORF(reporter, "string too long: %u", (unsigned)len);
334 return;
335 }
336 if (floatString[len] != '\0' || strlen(floatString) != len) {
337 ERRORF(reporter, "terminator misplaced.");
338 return; // The terminator is needed for sscanf().
339 }
340 if (reporter->verbose()) {
341 SkDebugf("%15.9g = \"%s\"\n", inputFloat, floatString);
342 }
343 float roundTripFloat;
344 if (1 != sscanf(floatString, "%f", &roundTripFloat)) {
345 ERRORF(reporter, "unscannable result: %s", floatString);
346 return;
347 }
halcanary56586b62016-07-25 13:59:30 -0700348 if (std::isfinite(inputFloat) && roundTripFloat != inputFloat) {
halcanary8e9f5e32016-02-24 15:46:46 -0800349 ERRORF(reporter, "roundTripFloat (%.9g) != inputFloat (%.9g)",
350 roundTripFloat, inputFloat);
351 }
352}
353
354// Test SkPDFUtils::AppendScalar for accuracy.
halcanary57f744e2016-09-09 11:41:59 -0700355DEF_TEST(SkPDF_Primitives_Scalar, reporter) {
halcanary8e9f5e32016-02-24 15:46:46 -0800356 SkRandom random(0x5EED);
357 int iterationCount = 512;
358 while (iterationCount-- > 0) {
359 union { uint32_t u; float f; };
360 u = random.nextU();
361 static_assert(sizeof(float) == sizeof(uint32_t), "");
362 check_pdf_scalar_serialization(reporter, f);
363 }
364 float alwaysCheck[] = {
365 0.0f, -0.0f, 1.0f, -1.0f, SK_ScalarPI, 0.1f, FLT_MIN, FLT_MAX,
366 -FLT_MIN, -FLT_MAX, FLT_MIN / 16.0f, -FLT_MIN / 16.0f,
367 SK_FloatNaN, SK_FloatInfinity, SK_FloatNegativeInfinity,
368 -FLT_MIN / 8388608.0
369 };
370 for (float inputFloat: alwaysCheck) {
371 check_pdf_scalar_serialization(reporter, inputFloat);
372 }
373}
halcanaryeb92cb32016-07-15 13:41:27 -0700374
375// Test SkPDFUtils:: for accuracy.
halcanary57f744e2016-09-09 11:41:59 -0700376DEF_TEST(SkPDF_Primitives_Color, reporter) {
halcanaryeb92cb32016-07-15 13:41:27 -0700377 char buffer[5];
378 for (int i = 0; i < 256; ++i) {
379 size_t len = SkPDFUtils::ColorToDecimal(i, buffer);
380 REPORTER_ASSERT(reporter, len == strlen(buffer));
381 float f;
382 REPORTER_ASSERT(reporter, 1 == sscanf(buffer, "%f", &f));
383 int roundTrip = (int)(0.5 + f * 255);
384 REPORTER_ASSERT(reporter, roundTrip == i);
385 }
386}
Hal Canary1521c8a2018-03-28 09:51:00 -0700387
Herb Derby81ecdbb2018-07-20 11:48:38 -0400388static SkGlyphRun make_run(size_t len, const SkGlyphID* glyphs, SkPoint* pos,
Mike Reed023403b2019-01-07 12:57:10 -0500389 const SkFont& font, const uint32_t* clusters,
Hal Canary98caedd2018-07-23 10:50:49 -0400390 size_t utf8TextByteLength, const char* utf8Text) {
Mike Reed023403b2019-01-07 12:57:10 -0500391 return SkGlyphRun(font,
Herb Derbyb935cf82018-07-26 16:54:18 -0400392 SkSpan<const SkPoint>{pos, len},
Hal Canary98caedd2018-07-23 10:50:49 -0400393 SkSpan<const SkGlyphID>{glyphs, len},
Hal Canary98caedd2018-07-23 10:50:49 -0400394 SkSpan<const char>{utf8Text, utf8TextByteLength},
395 SkSpan<const uint32_t>{clusters, len});
396}
397
Hal Canary1521c8a2018-03-28 09:51:00 -0700398DEF_TEST(SkPDF_Clusterator, reporter) {
Mike Reed023403b2019-01-07 12:57:10 -0500399 SkFont font;
Hal Canarye9e19dd2018-04-02 11:36:39 -0400400 {
Hal Canary98caedd2018-07-23 10:50:49 -0400401 constexpr unsigned len = 11;
402 const uint32_t clusters[len] = { 3, 2, 2, 1, 0, 4, 4, 7, 6, 6, 5 };
403 const SkGlyphID glyphs[len] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11};
Herb Derby81ecdbb2018-07-20 11:48:38 -0400404 SkPoint pos[len] = {{0, 0}, {0, 0}, {0, 0}, {0, 0}, {0, 0}, {0, 0},
Hal Canary98caedd2018-07-23 10:50:49 -0400405 {0, 0}, {0, 0}, {0, 0}, {0, 0}, {0, 0}};
Hal Canarye9e19dd2018-04-02 11:36:39 -0400406 const char text[] = "abcdefgh";
Mike Reed023403b2019-01-07 12:57:10 -0500407 SkGlyphRun run = make_run(len, glyphs, pos, font, clusters, strlen(text), text);
Hal Canary98caedd2018-07-23 10:50:49 -0400408 SkClusterator clusterator(run);
Hal Canarye9e19dd2018-04-02 11:36:39 -0400409 SkClusterator::Cluster expectations[] = {
410 {&text[3], 1, 0, 1},
411 {&text[2], 1, 1, 2},
412 {&text[1], 1, 3, 1},
413 {&text[0], 1, 4, 1},
414 {&text[4], 1, 5, 2},
415 {&text[7], 1, 7, 1},
416 {&text[6], 1, 8, 2},
417 {&text[5], 1, 10, 1},
418 {nullptr, 0, 0, 0},
419 };
420 for (const auto& expectation : expectations) {
421 REPORTER_ASSERT(reporter, clusterator.next() == expectation);
422 }
423 }
424 {
Hal Canary98caedd2018-07-23 10:50:49 -0400425 constexpr unsigned len = 5;
426 const uint32_t clusters[len] = { 0, 1, 4, 5, 6 };
427 const SkGlyphID glyphs[len] = { 43, 167, 79, 79, 82, };
Herb Derby81ecdbb2018-07-20 11:48:38 -0400428 SkPoint pos[len] = {{0, 0}, {0, 0}, {0, 0}, {0, 0}, {0, 0}};
Hal Canarye9e19dd2018-04-02 11:36:39 -0400429 const char text[] = "Ha\xCC\x8A" "llo";
Mike Reed023403b2019-01-07 12:57:10 -0500430 SkGlyphRun run = make_run(len, glyphs, pos, font, clusters, strlen(text), text);
Hal Canary98caedd2018-07-23 10:50:49 -0400431 SkClusterator clusterator(run);
Hal Canarye9e19dd2018-04-02 11:36:39 -0400432 SkClusterator::Cluster expectations[] = {
433 {&text[0], 1, 0, 1},
434 {&text[1], 3, 1, 1},
435 {&text[4], 1, 2, 1},
436 {&text[5], 1, 3, 1},
437 {&text[6], 1, 4, 1},
438 {nullptr, 0, 0, 0},
439 };
440 for (const auto& expectation : expectations) {
441 REPORTER_ASSERT(reporter, clusterator.next() == expectation);
442 }
Hal Canary1521c8a2018-03-28 09:51:00 -0700443 }
444}
445
Hal Canaryd7cf0be2019-07-30 12:23:10 -0400446DEF_TEST(fuzz875632f0, reporter) {
447 SkNullWStream stream;
448 auto doc = SkPDF::MakeDocument(&stream);
449 REPORTER_ASSERT(reporter, doc);
450 SkCanvas* canvas = doc->beginPage(128, 160);
451
452 SkAutoCanvasRestore autoCanvasRestore(canvas, false);
453
454 SkPaint layerPaint({0, 0, 0, 0});
455 layerPaint.setImageFilter(SkDilateImageFilter::Make(536870912, 0, nullptr, nullptr));
456 layerPaint.setBlendMode(SkBlendMode::kClear);
457
458 canvas->saveLayer(nullptr, &layerPaint);
459 canvas->saveLayer(nullptr, nullptr);
460
461 SkPaint paint;
462 paint.setBlendMode(SkBlendMode::kDarken);
463 paint.setShader(SkPerlinNoiseShader::MakeFractalNoise(0, 0, 2, 0, nullptr));
464 paint.setColor4f(SkColor4f{0, 0, 0 ,0});
465
466 canvas->drawPath(SkPath(), paint);
467}
Hal Canary43fb7a02016-12-30 13:09:03 -0500468#endif