blob: 6445f4437ae6e7bedabc82db9f79937e7921ca64 [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"
Michael Ludwiged552f82021-01-22 13:01:26 -050020#include "include/effects/SkImageFilters.h"
Hal Canaryd7cf0be2019-07-30 12:23:10 -040021#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>
John Stilesfbd050b2020-08-03 13:21:46 -040040#include <memory>
halcanary56586b62016-07-25 13:59:30 -070041
commit-bot@chromium.org608ea652013-10-03 19:29:21 +000042#define DUMMY_TEXT "DCT compessed stream."
43
halcanaryf7a169e2015-05-01 07:35:45 -070044template <typename T>
Hal Canaryf6462c42018-11-13 16:19:59 -050045static SkString emit_to_string(T& obj) {
halcanary6a144342015-01-23 11:45:10 -080046 SkDynamicMemoryWStream buffer;
Hal Canaryf6462c42018-11-13 16:19:59 -050047 obj.emitObject(&buffer);
halcanary56586b62016-07-25 13:59:30 -070048 SkString tmp(buffer.bytesWritten());
49 buffer.copyTo(tmp.writable_str());
halcanaryf7a169e2015-05-01 07:35:45 -070050 return tmp;
vandebo@chromium.org8459d4e2010-09-24 22:25:30 +000051}
52
halcanaryf7a169e2015-05-01 07:35:45 -070053static bool eq(const SkString& str, const char* strPtr, size_t len) {
54 return len == str.size() && 0 == memcmp(str.c_str(), strPtr, len);
vandebo@chromium.org421d6442011-07-20 17:39:01 +000055}
56
halcanary56586b62016-07-25 13:59:30 -070057static void assert_eql(skiatest::Reporter* reporter,
58 const SkString& skString,
59 const char* str,
60 size_t len) {
61 if (!eq(skString, str, len)) {
62 REPORT_FAILURE(reporter, "", SkStringPrintf(
Adlai Holler684838f2020-05-12 10:41:04 -040063 "'%*s' != '%s'", (int)len, str, skString.c_str()));
halcanary56586b62016-07-25 13:59:30 -070064 }
65}
halcanaryf7a169e2015-05-01 07:35:45 -070066
halcanary56586b62016-07-25 13:59:30 -070067static void assert_eq(skiatest::Reporter* reporter,
68 const SkString& skString,
69 const char* str) {
70 assert_eql(reporter, skString, str, strlen(str));
71}
halcanaryf7a169e2015-05-01 07:35:45 -070072
73
halcanary56586b62016-07-25 13:59:30 -070074template <typename T>
75static void assert_emit_eq(skiatest::Reporter* reporter,
76 T& object,
77 const char* string) {
78 SkString result = emit_to_string(object);
79 assert_eq(reporter, result, string);
80}
halcanaryf7a169e2015-05-01 07:35:45 -070081
vandebo@chromium.org4e1cc6a2013-01-25 19:27:23 +000082// This test used to assert without the fix submitted for
83// http://code.google.com/p/skia/issues/detail?id=1083.
84// SKP files might have invalid glyph ids. This test ensures they are ignored,
85// and there is no assert on input data in Debug mode.
sugoi@google.com54f0d1b2013-02-27 19:17:41 +000086static void test_issue1083() {
halcanary3d32d502015-03-01 06:55:20 -080087 SkDynamicMemoryWStream outStream;
Hal Canary3026d4b2019-01-07 10:00:48 -050088 auto doc = SkPDF::MakeDocument(&outStream);
halcanary3d32d502015-03-01 06:55:20 -080089 SkCanvas* canvas = doc->beginPage(100.0f, 100.0f);
vandebo@chromium.org4e1cc6a2013-01-25 19:27:23 +000090
91 uint16_t glyphID = 65000;
Ben Wagner51e15a62019-05-07 15:38:46 -040092 canvas->drawSimpleText(&glyphID, 2, SkTextEncoding::kGlyphID, 0, 0, SkFont(), SkPaint());
vandebo@chromium.org4e1cc6a2013-01-25 19:27:23 +000093
halcanary3d32d502015-03-01 06:55:20 -080094 doc->close();
vandebo@chromium.org4e1cc6a2013-01-25 19:27:23 +000095}
96
halcanary56586b62016-07-25 13:59:30 -070097static void assert_emit_eq_number(skiatest::Reporter* reporter, float number) {
98 SkPDFUnion pdfUnion = SkPDFUnion::Scalar(number);
99 SkString result = emit_to_string(pdfUnion);
100 float value = static_cast<float>(std::atof(result.c_str()));
101 if (value != number) {
102 ERRORF(reporter, "%.9g != %s", number, result.c_str());
103 }
104}
105
106
halcanaryf7a169e2015-05-01 07:35:45 -0700107static void TestPDFUnion(skiatest::Reporter* reporter) {
108 SkPDFUnion boolTrue = SkPDFUnion::Bool(true);
halcanary56586b62016-07-25 13:59:30 -0700109 assert_emit_eq(reporter, boolTrue, "true");
vandebo@chromium.org8459d4e2010-09-24 22:25:30 +0000110
halcanaryf7a169e2015-05-01 07:35:45 -0700111 SkPDFUnion boolFalse = SkPDFUnion::Bool(false);
halcanary56586b62016-07-25 13:59:30 -0700112 assert_emit_eq(reporter, boolFalse, "false");
vandebo@chromium.org8459d4e2010-09-24 22:25:30 +0000113
halcanaryf7a169e2015-05-01 07:35:45 -0700114 SkPDFUnion int42 = SkPDFUnion::Int(42);
halcanary56586b62016-07-25 13:59:30 -0700115 assert_emit_eq(reporter, int42, "42");
halcanaryf7a169e2015-05-01 07:35:45 -0700116
halcanary56586b62016-07-25 13:59:30 -0700117 assert_emit_eq_number(reporter, SK_ScalarHalf);
118 assert_emit_eq_number(reporter, 110999.75f); // bigScalar
119 assert_emit_eq_number(reporter, 50000000.1f); // biggerScalar
120 assert_emit_eq_number(reporter, 1.0f / 65536); // smallScalar
vandebo@chromium.org094316b2011-03-04 03:15:13 +0000121
halcanaryf7a169e2015-05-01 07:35:45 -0700122 SkPDFUnion stringSimple = SkPDFUnion::String("test ) string ( foo");
halcanary56586b62016-07-25 13:59:30 -0700123 assert_emit_eq(reporter, stringSimple, "(test \\) string \\( foo)");
vandebo@chromium.org8459d4e2010-09-24 22:25:30 +0000124
halcanaryf7a169e2015-05-01 07:35:45 -0700125 SkString stringComplexInput("\ttest ) string ( foo");
126 SkPDFUnion stringComplex = SkPDFUnion::String(stringComplexInput);
halcanary56586b62016-07-25 13:59:30 -0700127 assert_emit_eq(reporter, stringComplex, "(\\011test \\) string \\( foo)");
halcanaryee41b752016-06-23 14:08:11 -0700128
129 SkString binaryStringInput("\1\2\3\4\5\6\7\10\11\12\13\14\15\16\17\20");
130 SkPDFUnion binaryString = SkPDFUnion::String(binaryStringInput);
halcanary56586b62016-07-25 13:59:30 -0700131 assert_emit_eq(reporter, binaryString, "<0102030405060708090A0B0C0D0E0F10>");
vandebo@chromium.org8459d4e2010-09-24 22:25:30 +0000132
halcanaryf7a169e2015-05-01 07:35:45 -0700133 SkString nameInput("Test name\twith#tab");
134 SkPDFUnion name = SkPDFUnion::Name(nameInput);
halcanary56586b62016-07-25 13:59:30 -0700135 assert_emit_eq(reporter, name, "/Test#20name#09with#23tab");
halcanaryf7a169e2015-05-01 07:35:45 -0700136
137 SkString nameInput2("A#/%()<>[]{}B");
138 SkPDFUnion name2 = SkPDFUnion::Name(nameInput2);
halcanary56586b62016-07-25 13:59:30 -0700139 assert_emit_eq(reporter, name2, "/A#23#2F#25#28#29#3C#3E#5B#5D#7B#7DB");
halcanaryf7a169e2015-05-01 07:35:45 -0700140
141 SkPDFUnion name3 = SkPDFUnion::Name("SimpleNameWithOnlyPrintableASCII");
halcanary56586b62016-07-25 13:59:30 -0700142 assert_emit_eq(reporter, name3, "/SimpleNameWithOnlyPrintableASCII");
vandebo@chromium.org251a7662012-09-21 17:50:50 +0000143
vandebo@chromium.orgc0376fe2012-03-05 18:44:33 +0000144 // Test that we correctly handle characters with the high-bit set.
halcanaryf7a169e2015-05-01 07:35:45 -0700145 SkString highBitString("\xDE\xAD" "be\xEF");
146 SkPDFUnion highBitName = SkPDFUnion::Name(highBitString);
halcanary56586b62016-07-25 13:59:30 -0700147 assert_emit_eq(reporter, highBitName, "/#DE#ADbe#EF");
Hal Canary3d49d082019-10-11 13:29:42 -0400148
149 // https://bugs.skia.org/9508
150 // https://crbug.com/494913
151 // Trailing '\0' characters must be removed.
152 const char nameInput4[] = "Test name with nil\0";
153 SkPDFUnion name4 = SkPDFUnion::Name(SkString(nameInput4, strlen(nameInput4) + 1));
154 assert_emit_eq(reporter, name4, "/Test#20name#20with#20nil");
halcanaryf7a169e2015-05-01 07:35:45 -0700155}
vandebo@chromium.orgc0376fe2012-03-05 18:44:33 +0000156
halcanaryf7a169e2015-05-01 07:35:45 -0700157static void TestPDFArray(skiatest::Reporter* reporter) {
Hal Canary74801582018-12-18 16:30:41 -0500158 std::unique_ptr<SkPDFArray> array(new SkPDFArray);
halcanary56586b62016-07-25 13:59:30 -0700159 assert_emit_eq(reporter, *array, "[]");
vandebo@chromium.org8459d4e2010-09-24 22:25:30 +0000160
halcanaryf7a169e2015-05-01 07:35:45 -0700161 array->appendInt(42);
halcanary56586b62016-07-25 13:59:30 -0700162 assert_emit_eq(reporter, *array, "[42]");
halcanaryf7a169e2015-05-01 07:35:45 -0700163
164 array->appendScalar(SK_ScalarHalf);
halcanary56586b62016-07-25 13:59:30 -0700165 assert_emit_eq(reporter, *array, "[42 .5]");
halcanaryf7a169e2015-05-01 07:35:45 -0700166
167 array->appendInt(0);
halcanary56586b62016-07-25 13:59:30 -0700168 assert_emit_eq(reporter, *array, "[42 .5 0]");
halcanaryf7a169e2015-05-01 07:35:45 -0700169
170 array->appendBool(true);
halcanary56586b62016-07-25 13:59:30 -0700171 assert_emit_eq(reporter, *array, "[42 .5 0 true]");
halcanaryf7a169e2015-05-01 07:35:45 -0700172
173 array->appendName("ThisName");
halcanary56586b62016-07-25 13:59:30 -0700174 assert_emit_eq(reporter, *array, "[42 .5 0 true /ThisName]");
halcanaryf7a169e2015-05-01 07:35:45 -0700175
176 array->appendName(SkString("AnotherName"));
halcanary56586b62016-07-25 13:59:30 -0700177 assert_emit_eq(reporter, *array, "[42 .5 0 true /ThisName /AnotherName]");
halcanaryf7a169e2015-05-01 07:35:45 -0700178
179 array->appendString("This String");
halcanary56586b62016-07-25 13:59:30 -0700180 assert_emit_eq(reporter, *array,
halcanary8e9f5e32016-02-24 15:46:46 -0800181 "[42 .5 0 true /ThisName /AnotherName (This String)]");
halcanaryf7a169e2015-05-01 07:35:45 -0700182
183 array->appendString(SkString("Another String"));
halcanary56586b62016-07-25 13:59:30 -0700184 assert_emit_eq(reporter, *array,
halcanary8e9f5e32016-02-24 15:46:46 -0800185 "[42 .5 0 true /ThisName /AnotherName (This String) "
halcanaryf7a169e2015-05-01 07:35:45 -0700186 "(Another String)]");
187
Hal Canary74801582018-12-18 16:30:41 -0500188 std::unique_ptr<SkPDFArray> innerArray(new SkPDFArray);
halcanaryf7a169e2015-05-01 07:35:45 -0700189 innerArray->appendInt(-1);
halcanary8103a342016-03-08 15:10:16 -0800190 array->appendObject(std::move(innerArray));
halcanary56586b62016-07-25 13:59:30 -0700191 assert_emit_eq(reporter, *array,
halcanary8e9f5e32016-02-24 15:46:46 -0800192 "[42 .5 0 true /ThisName /AnotherName (This String) "
halcanaryf7a169e2015-05-01 07:35:45 -0700193 "(Another String) [-1]]");
halcanaryf7a169e2015-05-01 07:35:45 -0700194}
195
196static void TestPDFDict(skiatest::Reporter* reporter) {
Hal Canary74801582018-12-18 16:30:41 -0500197 std::unique_ptr<SkPDFDict> dict(new SkPDFDict);
halcanary56586b62016-07-25 13:59:30 -0700198 assert_emit_eq(reporter, *dict, "<<>>");
halcanaryf7a169e2015-05-01 07:35:45 -0700199
200 dict->insertInt("n1", SkToSizeT(42));
halcanary56586b62016-07-25 13:59:30 -0700201 assert_emit_eq(reporter, *dict, "<</n1 42>>");
halcanaryf7a169e2015-05-01 07:35:45 -0700202
John Stilesfbd050b2020-08-03 13:21:46 -0400203 dict = std::make_unique<SkPDFDict>();
halcanary56586b62016-07-25 13:59:30 -0700204 assert_emit_eq(reporter, *dict, "<<>>");
halcanaryf7a169e2015-05-01 07:35:45 -0700205
206 dict->insertInt("n1", 42);
halcanary56586b62016-07-25 13:59:30 -0700207 assert_emit_eq(reporter, *dict, "<</n1 42>>");
halcanaryf7a169e2015-05-01 07:35:45 -0700208
209 dict->insertScalar("n2", SK_ScalarHalf);
210
halcanary130444f2015-04-25 06:45:07 -0700211 SkString n3("n3");
Hal Canary74801582018-12-18 16:30:41 -0500212 std::unique_ptr<SkPDFArray> innerArray(new SkPDFArray);
halcanaryf7a169e2015-05-01 07:35:45 -0700213 innerArray->appendInt(-100);
halcanary8103a342016-03-08 15:10:16 -0800214 dict->insertObject(n3, std::move(innerArray));
halcanary56586b62016-07-25 13:59:30 -0700215 assert_emit_eq(reporter, *dict, "<</n1 42\n/n2 .5\n/n3 [-100]>>");
vandebo@chromium.org8459d4e2010-09-24 22:25:30 +0000216
John Stilesfbd050b2020-08-03 13:21:46 -0400217 dict = std::make_unique<SkPDFDict>();
halcanary56586b62016-07-25 13:59:30 -0700218 assert_emit_eq(reporter, *dict, "<<>>");
halcanaryf7a169e2015-05-01 07:35:45 -0700219
220 dict->insertInt("n1", 24);
halcanary56586b62016-07-25 13:59:30 -0700221 assert_emit_eq(reporter, *dict, "<</n1 24>>");
halcanaryf7a169e2015-05-01 07:35:45 -0700222
223 dict->insertInt("n2", SkToSizeT(99));
halcanary56586b62016-07-25 13:59:30 -0700224 assert_emit_eq(reporter, *dict, "<</n1 24\n/n2 99>>");
halcanaryf7a169e2015-05-01 07:35:45 -0700225
226 dict->insertScalar("n3", SK_ScalarHalf);
halcanary56586b62016-07-25 13:59:30 -0700227 assert_emit_eq(reporter, *dict, "<</n1 24\n/n2 99\n/n3 .5>>");
halcanaryf7a169e2015-05-01 07:35:45 -0700228
229 dict->insertName("n4", "AName");
halcanary56586b62016-07-25 13:59:30 -0700230 assert_emit_eq(reporter, *dict, "<</n1 24\n/n2 99\n/n3 .5\n/n4 /AName>>");
halcanaryf7a169e2015-05-01 07:35:45 -0700231
232 dict->insertName("n5", SkString("AnotherName"));
halcanary56586b62016-07-25 13:59:30 -0700233 assert_emit_eq(reporter, *dict, "<</n1 24\n/n2 99\n/n3 .5\n/n4 /AName\n"
halcanaryf7a169e2015-05-01 07:35:45 -0700234 "/n5 /AnotherName>>");
235
236 dict->insertString("n6", "A String");
halcanary56586b62016-07-25 13:59:30 -0700237 assert_emit_eq(reporter, *dict, "<</n1 24\n/n2 99\n/n3 .5\n/n4 /AName\n"
halcanaryf7a169e2015-05-01 07:35:45 -0700238 "/n5 /AnotherName\n/n6 (A String)>>");
239
240 dict->insertString("n7", SkString("Another String"));
halcanary56586b62016-07-25 13:59:30 -0700241 assert_emit_eq(reporter, *dict, "<</n1 24\n/n2 99\n/n3 .5\n/n4 /AName\n"
halcanaryf7a169e2015-05-01 07:35:45 -0700242 "/n5 /AnotherName\n/n6 (A String)\n/n7 (Another String)>>");
243
John Stilesfbd050b2020-08-03 13:21:46 -0400244 dict = std::make_unique<SkPDFDict>("DType");
halcanary56586b62016-07-25 13:59:30 -0700245 assert_emit_eq(reporter, *dict, "<</Type /DType>>");
halcanaryf7a169e2015-05-01 07:35:45 -0700246}
247
halcanary57f744e2016-09-09 11:41:59 -0700248DEF_TEST(SkPDF_Primitives, reporter) {
halcanaryf7a169e2015-05-01 07:35:45 -0700249 TestPDFUnion(reporter);
250 TestPDFArray(reporter);
251 TestPDFDict(reporter);
sugoi@google.com54f0d1b2013-02-27 19:17:41 +0000252 test_issue1083();
vandebo@chromium.org8459d4e2010-09-24 22:25:30 +0000253}
senorblancob0e89dc2014-10-20 14:03:12 -0700254
255namespace {
256
Michael Ludwig8ee6cf32019-08-02 09:57:04 -0400257class DummyImageFilter : public SkImageFilter_Base {
senorblancob0e89dc2014-10-20 14:03:12 -0700258public:
robertphillips43c2ad42016-04-04 05:05:11 -0700259 static sk_sp<DummyImageFilter> Make(bool visited = false) {
260 return sk_sp<DummyImageFilter>(new DummyImageFilter(visited));
261 }
262
robertphillips43c2ad42016-04-04 05:05:11 -0700263 bool visited() const { return fVisited; }
264
265protected:
Michael Ludwige30a4852019-08-14 14:35:42 -0400266 sk_sp<SkSpecialImage> onFilterImage(const Context& ctx, SkIPoint* offset) const override {
senorblancob0e89dc2014-10-20 14:03:12 -0700267 fVisited = true;
268 offset->fX = offset->fY = 0;
Michael Ludwige30a4852019-08-14 14:35:42 -0400269 return sk_ref_sp<SkSpecialImage>(ctx.sourceImage());
senorblancob0e89dc2014-10-20 14:03:12 -0700270 }
senorblancob0e89dc2014-10-20 14:03:12 -0700271
272private:
Mike Klein4fee3232018-10-18 17:27:16 -0400273 SK_FLATTENABLE_HOOKS(DummyImageFilter)
robertphillips43c2ad42016-04-04 05:05:11 -0700274 DummyImageFilter(bool visited) : INHERITED(nullptr, 0, nullptr), fVisited(visited) {}
275
senorblancob0e89dc2014-10-20 14:03:12 -0700276 mutable bool fVisited;
robertphillips43c2ad42016-04-04 05:05:11 -0700277
John Stiles7571f9e2020-09-02 22:42:33 -0400278 using INHERITED = SkImageFilter_Base;
senorblancob0e89dc2014-10-20 14:03:12 -0700279};
280
reed60c9b582016-04-03 09:11:13 -0700281sk_sp<SkFlattenable> DummyImageFilter::CreateProc(SkReadBuffer& buffer) {
senorblancob0e89dc2014-10-20 14:03:12 -0700282 SK_IMAGEFILTER_UNFLATTEN_COMMON(common, 0);
283 bool visited = buffer.readBool();
robertphillips43c2ad42016-04-04 05:05:11 -0700284 return DummyImageFilter::Make(visited);
senorblancob0e89dc2014-10-20 14:03:12 -0700285}
286
John Stilesa6841be2020-08-06 14:11:56 -0400287} // namespace
senorblancob0e89dc2014-10-20 14:03:12 -0700288
289// Check that PDF rendering of image filters successfully falls back to
290// CPU rasterization.
halcanary57f744e2016-09-09 11:41:59 -0700291DEF_TEST(SkPDF_ImageFilter, reporter) {
292 REQUIRE_PDF_DOCUMENT(SkPDF_ImageFilter, reporter);
halcanary3d32d502015-03-01 06:55:20 -0800293 SkDynamicMemoryWStream stream;
Hal Canary3026d4b2019-01-07 10:00:48 -0500294 auto doc = SkPDF::MakeDocument(&stream);
halcanary3d32d502015-03-01 06:55:20 -0800295 SkCanvas* canvas = doc->beginPage(100.0f, 100.0f);
296
robertphillips43c2ad42016-04-04 05:05:11 -0700297 sk_sp<DummyImageFilter> filter(DummyImageFilter::Make());
senorblancob0e89dc2014-10-20 14:03:12 -0700298
299 // Filter just created; should be unvisited.
300 REPORTER_ASSERT(reporter, !filter->visited());
301 SkPaint paint;
Mike Reed5e257172016-11-01 11:22:05 -0400302 paint.setImageFilter(filter);
halcanary3d32d502015-03-01 06:55:20 -0800303 canvas->drawRect(SkRect::MakeWH(100, 100), paint);
304 doc->close();
senorblancob0e89dc2014-10-20 14:03:12 -0700305
306 // Filter was used in rendering; should be visited.
307 REPORTER_ASSERT(reporter, filter->visited());
308}
halcanary66a82f32015-10-12 13:05:04 -0700309
310// Check that PDF rendering of image filters successfully falls back to
311// CPU rasterization.
halcanary57f744e2016-09-09 11:41:59 -0700312DEF_TEST(SkPDF_FontCanEmbedTypeface, reporter) {
Hal Canary4ca9fa32018-12-21 16:15:01 -0500313 SkNullWStream nullWStream;
314 SkPDFDocument doc(&nullWStream, SkPDF::Metadata());
halcanary66a82f32015-10-12 13:05:04 -0700315
316 const char resource[] = "fonts/Roboto2-Regular_NoEmbed.ttf";
bungeman13b9c952016-05-12 10:09:30 -0700317 sk_sp<SkTypeface> noEmbedTypeface(MakeResourceAsTypeface(resource));
halcanary66a82f32015-10-12 13:05:04 -0700318 if (noEmbedTypeface) {
319 REPORTER_ASSERT(reporter,
Hal Canary4ca9fa32018-12-21 16:15:01 -0500320 !SkPDFFont::CanEmbedTypeface(noEmbedTypeface.get(), &doc));
halcanary66a82f32015-10-12 13:05:04 -0700321 }
Mike Kleinea3f0142019-03-20 11:12:10 -0500322 sk_sp<SkTypeface> portableTypeface(ToolUtils::create_portable_typeface(nullptr, SkFontStyle()));
halcanary66a82f32015-10-12 13:05:04 -0700323 REPORTER_ASSERT(reporter,
Hal Canary4ca9fa32018-12-21 16:15:01 -0500324 SkPDFFont::CanEmbedTypeface(portableTypeface.get(), &doc));
halcanary66a82f32015-10-12 13:05:04 -0700325}
halcanary8e9f5e32016-02-24 15:46:46 -0800326
327
328// test to see that all finite scalars round trip via scanf().
329static void check_pdf_scalar_serialization(
330 skiatest::Reporter* reporter, float inputFloat) {
Hal Canary3c36ef62018-01-02 16:25:53 -0500331 char floatString[kMaximumSkFloatToDecimalLength];
332 size_t len = SkFloatToDecimal(inputFloat, floatString);
halcanary8e9f5e32016-02-24 15:46:46 -0800333 if (len >= sizeof(floatString)) {
334 ERRORF(reporter, "string too long: %u", (unsigned)len);
335 return;
336 }
337 if (floatString[len] != '\0' || strlen(floatString) != len) {
338 ERRORF(reporter, "terminator misplaced.");
339 return; // The terminator is needed for sscanf().
340 }
341 if (reporter->verbose()) {
342 SkDebugf("%15.9g = \"%s\"\n", inputFloat, floatString);
343 }
344 float roundTripFloat;
345 if (1 != sscanf(floatString, "%f", &roundTripFloat)) {
346 ERRORF(reporter, "unscannable result: %s", floatString);
347 return;
348 }
halcanary56586b62016-07-25 13:59:30 -0700349 if (std::isfinite(inputFloat) && roundTripFloat != inputFloat) {
halcanary8e9f5e32016-02-24 15:46:46 -0800350 ERRORF(reporter, "roundTripFloat (%.9g) != inputFloat (%.9g)",
351 roundTripFloat, inputFloat);
352 }
353}
354
355// Test SkPDFUtils::AppendScalar for accuracy.
halcanary57f744e2016-09-09 11:41:59 -0700356DEF_TEST(SkPDF_Primitives_Scalar, reporter) {
halcanary8e9f5e32016-02-24 15:46:46 -0800357 SkRandom random(0x5EED);
358 int iterationCount = 512;
359 while (iterationCount-- > 0) {
360 union { uint32_t u; float f; };
361 u = random.nextU();
362 static_assert(sizeof(float) == sizeof(uint32_t), "");
363 check_pdf_scalar_serialization(reporter, f);
364 }
365 float alwaysCheck[] = {
366 0.0f, -0.0f, 1.0f, -1.0f, SK_ScalarPI, 0.1f, FLT_MIN, FLT_MAX,
367 -FLT_MIN, -FLT_MAX, FLT_MIN / 16.0f, -FLT_MIN / 16.0f,
368 SK_FloatNaN, SK_FloatInfinity, SK_FloatNegativeInfinity,
369 -FLT_MIN / 8388608.0
370 };
371 for (float inputFloat: alwaysCheck) {
372 check_pdf_scalar_serialization(reporter, inputFloat);
373 }
374}
halcanaryeb92cb32016-07-15 13:41:27 -0700375
376// Test SkPDFUtils:: for accuracy.
halcanary57f744e2016-09-09 11:41:59 -0700377DEF_TEST(SkPDF_Primitives_Color, reporter) {
halcanaryeb92cb32016-07-15 13:41:27 -0700378 char buffer[5];
379 for (int i = 0; i < 256; ++i) {
380 size_t len = SkPDFUtils::ColorToDecimal(i, buffer);
381 REPORTER_ASSERT(reporter, len == strlen(buffer));
382 float f;
383 REPORTER_ASSERT(reporter, 1 == sscanf(buffer, "%f", &f));
384 int roundTrip = (int)(0.5 + f * 255);
385 REPORTER_ASSERT(reporter, roundTrip == i);
386 }
387}
Hal Canary1521c8a2018-03-28 09:51:00 -0700388
Herb Derby81ecdbb2018-07-20 11:48:38 -0400389static SkGlyphRun make_run(size_t len, const SkGlyphID* glyphs, SkPoint* pos,
Mike Reed023403b2019-01-07 12:57:10 -0500390 const SkFont& font, const uint32_t* clusters,
Hal Canary98caedd2018-07-23 10:50:49 -0400391 size_t utf8TextByteLength, const char* utf8Text) {
Mike Reed023403b2019-01-07 12:57:10 -0500392 return SkGlyphRun(font,
Herb Derbyb935cf82018-07-26 16:54:18 -0400393 SkSpan<const SkPoint>{pos, len},
Hal Canary98caedd2018-07-23 10:50:49 -0400394 SkSpan<const SkGlyphID>{glyphs, len},
Hal Canary98caedd2018-07-23 10:50:49 -0400395 SkSpan<const char>{utf8Text, utf8TextByteLength},
Herb Derby022636b2021-04-08 14:34:55 -0400396 SkSpan<const uint32_t>{clusters, len},
397 SkSpan<const SkVector>{});
Hal Canary98caedd2018-07-23 10:50:49 -0400398}
399
Hal Canary1521c8a2018-03-28 09:51:00 -0700400DEF_TEST(SkPDF_Clusterator, reporter) {
Mike Reed023403b2019-01-07 12:57:10 -0500401 SkFont font;
Hal Canarye9e19dd2018-04-02 11:36:39 -0400402 {
Hal Canary98caedd2018-07-23 10:50:49 -0400403 constexpr unsigned len = 11;
404 const uint32_t clusters[len] = { 3, 2, 2, 1, 0, 4, 4, 7, 6, 6, 5 };
405 const SkGlyphID glyphs[len] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11};
Herb Derby81ecdbb2018-07-20 11:48:38 -0400406 SkPoint pos[len] = {{0, 0}, {0, 0}, {0, 0}, {0, 0}, {0, 0}, {0, 0},
Hal Canary98caedd2018-07-23 10:50:49 -0400407 {0, 0}, {0, 0}, {0, 0}, {0, 0}, {0, 0}};
Hal Canarye9e19dd2018-04-02 11:36:39 -0400408 const char text[] = "abcdefgh";
Mike Reed023403b2019-01-07 12:57:10 -0500409 SkGlyphRun run = make_run(len, glyphs, pos, font, clusters, strlen(text), text);
Hal Canary98caedd2018-07-23 10:50:49 -0400410 SkClusterator clusterator(run);
Hal Canarye9e19dd2018-04-02 11:36:39 -0400411 SkClusterator::Cluster expectations[] = {
412 {&text[3], 1, 0, 1},
413 {&text[2], 1, 1, 2},
414 {&text[1], 1, 3, 1},
415 {&text[0], 1, 4, 1},
416 {&text[4], 1, 5, 2},
417 {&text[7], 1, 7, 1},
418 {&text[6], 1, 8, 2},
419 {&text[5], 1, 10, 1},
420 {nullptr, 0, 0, 0},
421 };
422 for (const auto& expectation : expectations) {
423 REPORTER_ASSERT(reporter, clusterator.next() == expectation);
424 }
425 }
426 {
Hal Canary98caedd2018-07-23 10:50:49 -0400427 constexpr unsigned len = 5;
428 const uint32_t clusters[len] = { 0, 1, 4, 5, 6 };
429 const SkGlyphID glyphs[len] = { 43, 167, 79, 79, 82, };
Herb Derby81ecdbb2018-07-20 11:48:38 -0400430 SkPoint pos[len] = {{0, 0}, {0, 0}, {0, 0}, {0, 0}, {0, 0}};
Hal Canarye9e19dd2018-04-02 11:36:39 -0400431 const char text[] = "Ha\xCC\x8A" "llo";
Mike Reed023403b2019-01-07 12:57:10 -0500432 SkGlyphRun run = make_run(len, glyphs, pos, font, clusters, strlen(text), text);
Hal Canary98caedd2018-07-23 10:50:49 -0400433 SkClusterator clusterator(run);
Hal Canarye9e19dd2018-04-02 11:36:39 -0400434 SkClusterator::Cluster expectations[] = {
435 {&text[0], 1, 0, 1},
436 {&text[1], 3, 1, 1},
437 {&text[4], 1, 2, 1},
438 {&text[5], 1, 3, 1},
439 {&text[6], 1, 4, 1},
440 {nullptr, 0, 0, 0},
441 };
442 for (const auto& expectation : expectations) {
443 REPORTER_ASSERT(reporter, clusterator.next() == expectation);
444 }
Hal Canary1521c8a2018-03-28 09:51:00 -0700445 }
446}
447
Hal Canaryd7cf0be2019-07-30 12:23:10 -0400448DEF_TEST(fuzz875632f0, reporter) {
449 SkNullWStream stream;
450 auto doc = SkPDF::MakeDocument(&stream);
451 REPORTER_ASSERT(reporter, doc);
452 SkCanvas* canvas = doc->beginPage(128, 160);
453
454 SkAutoCanvasRestore autoCanvasRestore(canvas, false);
455
456 SkPaint layerPaint({0, 0, 0, 0});
Michael Ludwiged552f82021-01-22 13:01:26 -0500457 layerPaint.setImageFilter(SkImageFilters::Dilate(536870912, 0, nullptr, nullptr));
Hal Canaryd7cf0be2019-07-30 12:23:10 -0400458 layerPaint.setBlendMode(SkBlendMode::kClear);
459
460 canvas->saveLayer(nullptr, &layerPaint);
461 canvas->saveLayer(nullptr, nullptr);
462
463 SkPaint paint;
464 paint.setBlendMode(SkBlendMode::kDarken);
465 paint.setShader(SkPerlinNoiseShader::MakeFractalNoise(0, 0, 2, 0, nullptr));
466 paint.setColor4f(SkColor4f{0, 0, 0 ,0});
467
468 canvas->drawPath(SkPath(), paint);
469}
Hal Canary43fb7a02016-12-30 13:09:03 -0500470#endif