vandebo@chromium.org | f66025d | 2010-10-01 23:26:55 +0000 | [diff] [blame] | 1 | /* |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 2 | * Copyright 2010 The Android Open Source Project |
vandebo@chromium.org | f66025d | 2010-10-01 23:26:55 +0000 | [diff] [blame] | 3 | * |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
vandebo@chromium.org | f66025d | 2010-10-01 23:26:55 +0000 | [diff] [blame] | 6 | */ |
| 7 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 8 | #include "tests/Test.h" |
Hal Canary | 43fb7a0 | 2016-12-30 13:09:03 -0500 | [diff] [blame] | 9 | |
| 10 | #ifdef SK_SUPPORT_PDF |
| 11 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 12 | #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 Ludwig | ed552f8 | 2021-01-22 13:01:26 -0500 | [diff] [blame] | 20 | #include "include/effects/SkImageFilters.h" |
Hal Canary | d7cf0be | 2019-07-30 12:23:10 -0400 | [diff] [blame] | 21 | #include "include/effects/SkPerlinNoiseShader.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 22 | #include "include/private/SkTo.h" |
| 23 | #include "src/core/SkGlyphRun.h" |
Michael Ludwig | 8ee6cf3 | 2019-08-02 09:57:04 -0400 | [diff] [blame] | 24 | #include "src/core/SkImageFilter_Base.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 25 | #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.org | 8459d4e | 2010-09-24 22:25:30 +0000 | [diff] [blame] | 37 | |
halcanary | 56586b6 | 2016-07-25 13:59:30 -0700 | [diff] [blame] | 38 | #include <cstdlib> |
| 39 | #include <cmath> |
John Stiles | fbd050b | 2020-08-03 13:21:46 -0400 | [diff] [blame] | 40 | #include <memory> |
halcanary | 56586b6 | 2016-07-25 13:59:30 -0700 | [diff] [blame] | 41 | |
commit-bot@chromium.org | 608ea65 | 2013-10-03 19:29:21 +0000 | [diff] [blame] | 42 | #define DUMMY_TEXT "DCT compessed stream." |
| 43 | |
halcanary | f7a169e | 2015-05-01 07:35:45 -0700 | [diff] [blame] | 44 | template <typename T> |
Hal Canary | f6462c4 | 2018-11-13 16:19:59 -0500 | [diff] [blame] | 45 | static SkString emit_to_string(T& obj) { |
halcanary | 6a14434 | 2015-01-23 11:45:10 -0800 | [diff] [blame] | 46 | SkDynamicMemoryWStream buffer; |
Hal Canary | f6462c4 | 2018-11-13 16:19:59 -0500 | [diff] [blame] | 47 | obj.emitObject(&buffer); |
halcanary | 56586b6 | 2016-07-25 13:59:30 -0700 | [diff] [blame] | 48 | SkString tmp(buffer.bytesWritten()); |
| 49 | buffer.copyTo(tmp.writable_str()); |
halcanary | f7a169e | 2015-05-01 07:35:45 -0700 | [diff] [blame] | 50 | return tmp; |
vandebo@chromium.org | 8459d4e | 2010-09-24 22:25:30 +0000 | [diff] [blame] | 51 | } |
| 52 | |
halcanary | f7a169e | 2015-05-01 07:35:45 -0700 | [diff] [blame] | 53 | static 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.org | 421d644 | 2011-07-20 17:39:01 +0000 | [diff] [blame] | 55 | } |
| 56 | |
halcanary | 56586b6 | 2016-07-25 13:59:30 -0700 | [diff] [blame] | 57 | static 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 Holler | 684838f | 2020-05-12 10:41:04 -0400 | [diff] [blame] | 63 | "'%*s' != '%s'", (int)len, str, skString.c_str())); |
halcanary | 56586b6 | 2016-07-25 13:59:30 -0700 | [diff] [blame] | 64 | } |
| 65 | } |
halcanary | f7a169e | 2015-05-01 07:35:45 -0700 | [diff] [blame] | 66 | |
halcanary | 56586b6 | 2016-07-25 13:59:30 -0700 | [diff] [blame] | 67 | static void assert_eq(skiatest::Reporter* reporter, |
| 68 | const SkString& skString, |
| 69 | const char* str) { |
| 70 | assert_eql(reporter, skString, str, strlen(str)); |
| 71 | } |
halcanary | f7a169e | 2015-05-01 07:35:45 -0700 | [diff] [blame] | 72 | |
| 73 | |
halcanary | 56586b6 | 2016-07-25 13:59:30 -0700 | [diff] [blame] | 74 | template <typename T> |
| 75 | static 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 | } |
halcanary | f7a169e | 2015-05-01 07:35:45 -0700 | [diff] [blame] | 81 | |
vandebo@chromium.org | 4e1cc6a | 2013-01-25 19:27:23 +0000 | [diff] [blame] | 82 | // 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.com | 54f0d1b | 2013-02-27 19:17:41 +0000 | [diff] [blame] | 86 | static void test_issue1083() { |
halcanary | 3d32d50 | 2015-03-01 06:55:20 -0800 | [diff] [blame] | 87 | SkDynamicMemoryWStream outStream; |
Hal Canary | 3026d4b | 2019-01-07 10:00:48 -0500 | [diff] [blame] | 88 | auto doc = SkPDF::MakeDocument(&outStream); |
halcanary | 3d32d50 | 2015-03-01 06:55:20 -0800 | [diff] [blame] | 89 | SkCanvas* canvas = doc->beginPage(100.0f, 100.0f); |
vandebo@chromium.org | 4e1cc6a | 2013-01-25 19:27:23 +0000 | [diff] [blame] | 90 | |
| 91 | uint16_t glyphID = 65000; |
Ben Wagner | 51e15a6 | 2019-05-07 15:38:46 -0400 | [diff] [blame] | 92 | canvas->drawSimpleText(&glyphID, 2, SkTextEncoding::kGlyphID, 0, 0, SkFont(), SkPaint()); |
vandebo@chromium.org | 4e1cc6a | 2013-01-25 19:27:23 +0000 | [diff] [blame] | 93 | |
halcanary | 3d32d50 | 2015-03-01 06:55:20 -0800 | [diff] [blame] | 94 | doc->close(); |
vandebo@chromium.org | 4e1cc6a | 2013-01-25 19:27:23 +0000 | [diff] [blame] | 95 | } |
| 96 | |
halcanary | 56586b6 | 2016-07-25 13:59:30 -0700 | [diff] [blame] | 97 | static 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 | |
halcanary | f7a169e | 2015-05-01 07:35:45 -0700 | [diff] [blame] | 107 | static void TestPDFUnion(skiatest::Reporter* reporter) { |
| 108 | SkPDFUnion boolTrue = SkPDFUnion::Bool(true); |
halcanary | 56586b6 | 2016-07-25 13:59:30 -0700 | [diff] [blame] | 109 | assert_emit_eq(reporter, boolTrue, "true"); |
vandebo@chromium.org | 8459d4e | 2010-09-24 22:25:30 +0000 | [diff] [blame] | 110 | |
halcanary | f7a169e | 2015-05-01 07:35:45 -0700 | [diff] [blame] | 111 | SkPDFUnion boolFalse = SkPDFUnion::Bool(false); |
halcanary | 56586b6 | 2016-07-25 13:59:30 -0700 | [diff] [blame] | 112 | assert_emit_eq(reporter, boolFalse, "false"); |
vandebo@chromium.org | 8459d4e | 2010-09-24 22:25:30 +0000 | [diff] [blame] | 113 | |
halcanary | f7a169e | 2015-05-01 07:35:45 -0700 | [diff] [blame] | 114 | SkPDFUnion int42 = SkPDFUnion::Int(42); |
halcanary | 56586b6 | 2016-07-25 13:59:30 -0700 | [diff] [blame] | 115 | assert_emit_eq(reporter, int42, "42"); |
halcanary | f7a169e | 2015-05-01 07:35:45 -0700 | [diff] [blame] | 116 | |
halcanary | 56586b6 | 2016-07-25 13:59:30 -0700 | [diff] [blame] | 117 | 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.org | 094316b | 2011-03-04 03:15:13 +0000 | [diff] [blame] | 121 | |
halcanary | f7a169e | 2015-05-01 07:35:45 -0700 | [diff] [blame] | 122 | SkPDFUnion stringSimple = SkPDFUnion::String("test ) string ( foo"); |
halcanary | 56586b6 | 2016-07-25 13:59:30 -0700 | [diff] [blame] | 123 | assert_emit_eq(reporter, stringSimple, "(test \\) string \\( foo)"); |
vandebo@chromium.org | 8459d4e | 2010-09-24 22:25:30 +0000 | [diff] [blame] | 124 | |
halcanary | f7a169e | 2015-05-01 07:35:45 -0700 | [diff] [blame] | 125 | SkString stringComplexInput("\ttest ) string ( foo"); |
| 126 | SkPDFUnion stringComplex = SkPDFUnion::String(stringComplexInput); |
halcanary | 56586b6 | 2016-07-25 13:59:30 -0700 | [diff] [blame] | 127 | assert_emit_eq(reporter, stringComplex, "(\\011test \\) string \\( foo)"); |
halcanary | ee41b75 | 2016-06-23 14:08:11 -0700 | [diff] [blame] | 128 | |
| 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); |
halcanary | 56586b6 | 2016-07-25 13:59:30 -0700 | [diff] [blame] | 131 | assert_emit_eq(reporter, binaryString, "<0102030405060708090A0B0C0D0E0F10>"); |
vandebo@chromium.org | 8459d4e | 2010-09-24 22:25:30 +0000 | [diff] [blame] | 132 | |
halcanary | f7a169e | 2015-05-01 07:35:45 -0700 | [diff] [blame] | 133 | SkString nameInput("Test name\twith#tab"); |
| 134 | SkPDFUnion name = SkPDFUnion::Name(nameInput); |
halcanary | 56586b6 | 2016-07-25 13:59:30 -0700 | [diff] [blame] | 135 | assert_emit_eq(reporter, name, "/Test#20name#09with#23tab"); |
halcanary | f7a169e | 2015-05-01 07:35:45 -0700 | [diff] [blame] | 136 | |
| 137 | SkString nameInput2("A#/%()<>[]{}B"); |
| 138 | SkPDFUnion name2 = SkPDFUnion::Name(nameInput2); |
halcanary | 56586b6 | 2016-07-25 13:59:30 -0700 | [diff] [blame] | 139 | assert_emit_eq(reporter, name2, "/A#23#2F#25#28#29#3C#3E#5B#5D#7B#7DB"); |
halcanary | f7a169e | 2015-05-01 07:35:45 -0700 | [diff] [blame] | 140 | |
| 141 | SkPDFUnion name3 = SkPDFUnion::Name("SimpleNameWithOnlyPrintableASCII"); |
halcanary | 56586b6 | 2016-07-25 13:59:30 -0700 | [diff] [blame] | 142 | assert_emit_eq(reporter, name3, "/SimpleNameWithOnlyPrintableASCII"); |
vandebo@chromium.org | 251a766 | 2012-09-21 17:50:50 +0000 | [diff] [blame] | 143 | |
vandebo@chromium.org | c0376fe | 2012-03-05 18:44:33 +0000 | [diff] [blame] | 144 | // Test that we correctly handle characters with the high-bit set. |
halcanary | f7a169e | 2015-05-01 07:35:45 -0700 | [diff] [blame] | 145 | SkString highBitString("\xDE\xAD" "be\xEF"); |
| 146 | SkPDFUnion highBitName = SkPDFUnion::Name(highBitString); |
halcanary | 56586b6 | 2016-07-25 13:59:30 -0700 | [diff] [blame] | 147 | assert_emit_eq(reporter, highBitName, "/#DE#ADbe#EF"); |
Hal Canary | 3d49d08 | 2019-10-11 13:29:42 -0400 | [diff] [blame] | 148 | |
| 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"); |
halcanary | f7a169e | 2015-05-01 07:35:45 -0700 | [diff] [blame] | 155 | } |
vandebo@chromium.org | c0376fe | 2012-03-05 18:44:33 +0000 | [diff] [blame] | 156 | |
halcanary | f7a169e | 2015-05-01 07:35:45 -0700 | [diff] [blame] | 157 | static void TestPDFArray(skiatest::Reporter* reporter) { |
Hal Canary | 7480158 | 2018-12-18 16:30:41 -0500 | [diff] [blame] | 158 | std::unique_ptr<SkPDFArray> array(new SkPDFArray); |
halcanary | 56586b6 | 2016-07-25 13:59:30 -0700 | [diff] [blame] | 159 | assert_emit_eq(reporter, *array, "[]"); |
vandebo@chromium.org | 8459d4e | 2010-09-24 22:25:30 +0000 | [diff] [blame] | 160 | |
halcanary | f7a169e | 2015-05-01 07:35:45 -0700 | [diff] [blame] | 161 | array->appendInt(42); |
halcanary | 56586b6 | 2016-07-25 13:59:30 -0700 | [diff] [blame] | 162 | assert_emit_eq(reporter, *array, "[42]"); |
halcanary | f7a169e | 2015-05-01 07:35:45 -0700 | [diff] [blame] | 163 | |
| 164 | array->appendScalar(SK_ScalarHalf); |
halcanary | 56586b6 | 2016-07-25 13:59:30 -0700 | [diff] [blame] | 165 | assert_emit_eq(reporter, *array, "[42 .5]"); |
halcanary | f7a169e | 2015-05-01 07:35:45 -0700 | [diff] [blame] | 166 | |
| 167 | array->appendInt(0); |
halcanary | 56586b6 | 2016-07-25 13:59:30 -0700 | [diff] [blame] | 168 | assert_emit_eq(reporter, *array, "[42 .5 0]"); |
halcanary | f7a169e | 2015-05-01 07:35:45 -0700 | [diff] [blame] | 169 | |
| 170 | array->appendBool(true); |
halcanary | 56586b6 | 2016-07-25 13:59:30 -0700 | [diff] [blame] | 171 | assert_emit_eq(reporter, *array, "[42 .5 0 true]"); |
halcanary | f7a169e | 2015-05-01 07:35:45 -0700 | [diff] [blame] | 172 | |
| 173 | array->appendName("ThisName"); |
halcanary | 56586b6 | 2016-07-25 13:59:30 -0700 | [diff] [blame] | 174 | assert_emit_eq(reporter, *array, "[42 .5 0 true /ThisName]"); |
halcanary | f7a169e | 2015-05-01 07:35:45 -0700 | [diff] [blame] | 175 | |
| 176 | array->appendName(SkString("AnotherName")); |
halcanary | 56586b6 | 2016-07-25 13:59:30 -0700 | [diff] [blame] | 177 | assert_emit_eq(reporter, *array, "[42 .5 0 true /ThisName /AnotherName]"); |
halcanary | f7a169e | 2015-05-01 07:35:45 -0700 | [diff] [blame] | 178 | |
| 179 | array->appendString("This String"); |
halcanary | 56586b6 | 2016-07-25 13:59:30 -0700 | [diff] [blame] | 180 | assert_emit_eq(reporter, *array, |
halcanary | 8e9f5e3 | 2016-02-24 15:46:46 -0800 | [diff] [blame] | 181 | "[42 .5 0 true /ThisName /AnotherName (This String)]"); |
halcanary | f7a169e | 2015-05-01 07:35:45 -0700 | [diff] [blame] | 182 | |
| 183 | array->appendString(SkString("Another String")); |
halcanary | 56586b6 | 2016-07-25 13:59:30 -0700 | [diff] [blame] | 184 | assert_emit_eq(reporter, *array, |
halcanary | 8e9f5e3 | 2016-02-24 15:46:46 -0800 | [diff] [blame] | 185 | "[42 .5 0 true /ThisName /AnotherName (This String) " |
halcanary | f7a169e | 2015-05-01 07:35:45 -0700 | [diff] [blame] | 186 | "(Another String)]"); |
| 187 | |
Hal Canary | 7480158 | 2018-12-18 16:30:41 -0500 | [diff] [blame] | 188 | std::unique_ptr<SkPDFArray> innerArray(new SkPDFArray); |
halcanary | f7a169e | 2015-05-01 07:35:45 -0700 | [diff] [blame] | 189 | innerArray->appendInt(-1); |
halcanary | 8103a34 | 2016-03-08 15:10:16 -0800 | [diff] [blame] | 190 | array->appendObject(std::move(innerArray)); |
halcanary | 56586b6 | 2016-07-25 13:59:30 -0700 | [diff] [blame] | 191 | assert_emit_eq(reporter, *array, |
halcanary | 8e9f5e3 | 2016-02-24 15:46:46 -0800 | [diff] [blame] | 192 | "[42 .5 0 true /ThisName /AnotherName (This String) " |
halcanary | f7a169e | 2015-05-01 07:35:45 -0700 | [diff] [blame] | 193 | "(Another String) [-1]]"); |
halcanary | f7a169e | 2015-05-01 07:35:45 -0700 | [diff] [blame] | 194 | } |
| 195 | |
| 196 | static void TestPDFDict(skiatest::Reporter* reporter) { |
Hal Canary | 7480158 | 2018-12-18 16:30:41 -0500 | [diff] [blame] | 197 | std::unique_ptr<SkPDFDict> dict(new SkPDFDict); |
halcanary | 56586b6 | 2016-07-25 13:59:30 -0700 | [diff] [blame] | 198 | assert_emit_eq(reporter, *dict, "<<>>"); |
halcanary | f7a169e | 2015-05-01 07:35:45 -0700 | [diff] [blame] | 199 | |
| 200 | dict->insertInt("n1", SkToSizeT(42)); |
halcanary | 56586b6 | 2016-07-25 13:59:30 -0700 | [diff] [blame] | 201 | assert_emit_eq(reporter, *dict, "<</n1 42>>"); |
halcanary | f7a169e | 2015-05-01 07:35:45 -0700 | [diff] [blame] | 202 | |
John Stiles | fbd050b | 2020-08-03 13:21:46 -0400 | [diff] [blame] | 203 | dict = std::make_unique<SkPDFDict>(); |
halcanary | 56586b6 | 2016-07-25 13:59:30 -0700 | [diff] [blame] | 204 | assert_emit_eq(reporter, *dict, "<<>>"); |
halcanary | f7a169e | 2015-05-01 07:35:45 -0700 | [diff] [blame] | 205 | |
| 206 | dict->insertInt("n1", 42); |
halcanary | 56586b6 | 2016-07-25 13:59:30 -0700 | [diff] [blame] | 207 | assert_emit_eq(reporter, *dict, "<</n1 42>>"); |
halcanary | f7a169e | 2015-05-01 07:35:45 -0700 | [diff] [blame] | 208 | |
| 209 | dict->insertScalar("n2", SK_ScalarHalf); |
| 210 | |
halcanary | 130444f | 2015-04-25 06:45:07 -0700 | [diff] [blame] | 211 | SkString n3("n3"); |
Hal Canary | 7480158 | 2018-12-18 16:30:41 -0500 | [diff] [blame] | 212 | std::unique_ptr<SkPDFArray> innerArray(new SkPDFArray); |
halcanary | f7a169e | 2015-05-01 07:35:45 -0700 | [diff] [blame] | 213 | innerArray->appendInt(-100); |
halcanary | 8103a34 | 2016-03-08 15:10:16 -0800 | [diff] [blame] | 214 | dict->insertObject(n3, std::move(innerArray)); |
halcanary | 56586b6 | 2016-07-25 13:59:30 -0700 | [diff] [blame] | 215 | assert_emit_eq(reporter, *dict, "<</n1 42\n/n2 .5\n/n3 [-100]>>"); |
vandebo@chromium.org | 8459d4e | 2010-09-24 22:25:30 +0000 | [diff] [blame] | 216 | |
John Stiles | fbd050b | 2020-08-03 13:21:46 -0400 | [diff] [blame] | 217 | dict = std::make_unique<SkPDFDict>(); |
halcanary | 56586b6 | 2016-07-25 13:59:30 -0700 | [diff] [blame] | 218 | assert_emit_eq(reporter, *dict, "<<>>"); |
halcanary | f7a169e | 2015-05-01 07:35:45 -0700 | [diff] [blame] | 219 | |
| 220 | dict->insertInt("n1", 24); |
halcanary | 56586b6 | 2016-07-25 13:59:30 -0700 | [diff] [blame] | 221 | assert_emit_eq(reporter, *dict, "<</n1 24>>"); |
halcanary | f7a169e | 2015-05-01 07:35:45 -0700 | [diff] [blame] | 222 | |
| 223 | dict->insertInt("n2", SkToSizeT(99)); |
halcanary | 56586b6 | 2016-07-25 13:59:30 -0700 | [diff] [blame] | 224 | assert_emit_eq(reporter, *dict, "<</n1 24\n/n2 99>>"); |
halcanary | f7a169e | 2015-05-01 07:35:45 -0700 | [diff] [blame] | 225 | |
| 226 | dict->insertScalar("n3", SK_ScalarHalf); |
halcanary | 56586b6 | 2016-07-25 13:59:30 -0700 | [diff] [blame] | 227 | assert_emit_eq(reporter, *dict, "<</n1 24\n/n2 99\n/n3 .5>>"); |
halcanary | f7a169e | 2015-05-01 07:35:45 -0700 | [diff] [blame] | 228 | |
| 229 | dict->insertName("n4", "AName"); |
halcanary | 56586b6 | 2016-07-25 13:59:30 -0700 | [diff] [blame] | 230 | assert_emit_eq(reporter, *dict, "<</n1 24\n/n2 99\n/n3 .5\n/n4 /AName>>"); |
halcanary | f7a169e | 2015-05-01 07:35:45 -0700 | [diff] [blame] | 231 | |
| 232 | dict->insertName("n5", SkString("AnotherName")); |
halcanary | 56586b6 | 2016-07-25 13:59:30 -0700 | [diff] [blame] | 233 | assert_emit_eq(reporter, *dict, "<</n1 24\n/n2 99\n/n3 .5\n/n4 /AName\n" |
halcanary | f7a169e | 2015-05-01 07:35:45 -0700 | [diff] [blame] | 234 | "/n5 /AnotherName>>"); |
| 235 | |
| 236 | dict->insertString("n6", "A String"); |
halcanary | 56586b6 | 2016-07-25 13:59:30 -0700 | [diff] [blame] | 237 | assert_emit_eq(reporter, *dict, "<</n1 24\n/n2 99\n/n3 .5\n/n4 /AName\n" |
halcanary | f7a169e | 2015-05-01 07:35:45 -0700 | [diff] [blame] | 238 | "/n5 /AnotherName\n/n6 (A String)>>"); |
| 239 | |
| 240 | dict->insertString("n7", SkString("Another String")); |
halcanary | 56586b6 | 2016-07-25 13:59:30 -0700 | [diff] [blame] | 241 | assert_emit_eq(reporter, *dict, "<</n1 24\n/n2 99\n/n3 .5\n/n4 /AName\n" |
halcanary | f7a169e | 2015-05-01 07:35:45 -0700 | [diff] [blame] | 242 | "/n5 /AnotherName\n/n6 (A String)\n/n7 (Another String)>>"); |
| 243 | |
John Stiles | fbd050b | 2020-08-03 13:21:46 -0400 | [diff] [blame] | 244 | dict = std::make_unique<SkPDFDict>("DType"); |
halcanary | 56586b6 | 2016-07-25 13:59:30 -0700 | [diff] [blame] | 245 | assert_emit_eq(reporter, *dict, "<</Type /DType>>"); |
halcanary | f7a169e | 2015-05-01 07:35:45 -0700 | [diff] [blame] | 246 | } |
| 247 | |
halcanary | 57f744e | 2016-09-09 11:41:59 -0700 | [diff] [blame] | 248 | DEF_TEST(SkPDF_Primitives, reporter) { |
halcanary | f7a169e | 2015-05-01 07:35:45 -0700 | [diff] [blame] | 249 | TestPDFUnion(reporter); |
| 250 | TestPDFArray(reporter); |
| 251 | TestPDFDict(reporter); |
sugoi@google.com | 54f0d1b | 2013-02-27 19:17:41 +0000 | [diff] [blame] | 252 | test_issue1083(); |
vandebo@chromium.org | 8459d4e | 2010-09-24 22:25:30 +0000 | [diff] [blame] | 253 | } |
senorblanco | b0e89dc | 2014-10-20 14:03:12 -0700 | [diff] [blame] | 254 | |
| 255 | namespace { |
| 256 | |
Michael Ludwig | 8ee6cf3 | 2019-08-02 09:57:04 -0400 | [diff] [blame] | 257 | class DummyImageFilter : public SkImageFilter_Base { |
senorblanco | b0e89dc | 2014-10-20 14:03:12 -0700 | [diff] [blame] | 258 | public: |
robertphillips | 43c2ad4 | 2016-04-04 05:05:11 -0700 | [diff] [blame] | 259 | static sk_sp<DummyImageFilter> Make(bool visited = false) { |
| 260 | return sk_sp<DummyImageFilter>(new DummyImageFilter(visited)); |
| 261 | } |
| 262 | |
robertphillips | 43c2ad4 | 2016-04-04 05:05:11 -0700 | [diff] [blame] | 263 | bool visited() const { return fVisited; } |
| 264 | |
| 265 | protected: |
Michael Ludwig | e30a485 | 2019-08-14 14:35:42 -0400 | [diff] [blame] | 266 | sk_sp<SkSpecialImage> onFilterImage(const Context& ctx, SkIPoint* offset) const override { |
senorblanco | b0e89dc | 2014-10-20 14:03:12 -0700 | [diff] [blame] | 267 | fVisited = true; |
| 268 | offset->fX = offset->fY = 0; |
Michael Ludwig | e30a485 | 2019-08-14 14:35:42 -0400 | [diff] [blame] | 269 | return sk_ref_sp<SkSpecialImage>(ctx.sourceImage()); |
senorblanco | b0e89dc | 2014-10-20 14:03:12 -0700 | [diff] [blame] | 270 | } |
senorblanco | b0e89dc | 2014-10-20 14:03:12 -0700 | [diff] [blame] | 271 | |
| 272 | private: |
Mike Klein | 4fee323 | 2018-10-18 17:27:16 -0400 | [diff] [blame] | 273 | SK_FLATTENABLE_HOOKS(DummyImageFilter) |
robertphillips | 43c2ad4 | 2016-04-04 05:05:11 -0700 | [diff] [blame] | 274 | DummyImageFilter(bool visited) : INHERITED(nullptr, 0, nullptr), fVisited(visited) {} |
| 275 | |
senorblanco | b0e89dc | 2014-10-20 14:03:12 -0700 | [diff] [blame] | 276 | mutable bool fVisited; |
robertphillips | 43c2ad4 | 2016-04-04 05:05:11 -0700 | [diff] [blame] | 277 | |
John Stiles | 7571f9e | 2020-09-02 22:42:33 -0400 | [diff] [blame] | 278 | using INHERITED = SkImageFilter_Base; |
senorblanco | b0e89dc | 2014-10-20 14:03:12 -0700 | [diff] [blame] | 279 | }; |
| 280 | |
reed | 60c9b58 | 2016-04-03 09:11:13 -0700 | [diff] [blame] | 281 | sk_sp<SkFlattenable> DummyImageFilter::CreateProc(SkReadBuffer& buffer) { |
senorblanco | b0e89dc | 2014-10-20 14:03:12 -0700 | [diff] [blame] | 282 | SK_IMAGEFILTER_UNFLATTEN_COMMON(common, 0); |
| 283 | bool visited = buffer.readBool(); |
robertphillips | 43c2ad4 | 2016-04-04 05:05:11 -0700 | [diff] [blame] | 284 | return DummyImageFilter::Make(visited); |
senorblanco | b0e89dc | 2014-10-20 14:03:12 -0700 | [diff] [blame] | 285 | } |
| 286 | |
John Stiles | a6841be | 2020-08-06 14:11:56 -0400 | [diff] [blame] | 287 | } // namespace |
senorblanco | b0e89dc | 2014-10-20 14:03:12 -0700 | [diff] [blame] | 288 | |
| 289 | // Check that PDF rendering of image filters successfully falls back to |
| 290 | // CPU rasterization. |
halcanary | 57f744e | 2016-09-09 11:41:59 -0700 | [diff] [blame] | 291 | DEF_TEST(SkPDF_ImageFilter, reporter) { |
| 292 | REQUIRE_PDF_DOCUMENT(SkPDF_ImageFilter, reporter); |
halcanary | 3d32d50 | 2015-03-01 06:55:20 -0800 | [diff] [blame] | 293 | SkDynamicMemoryWStream stream; |
Hal Canary | 3026d4b | 2019-01-07 10:00:48 -0500 | [diff] [blame] | 294 | auto doc = SkPDF::MakeDocument(&stream); |
halcanary | 3d32d50 | 2015-03-01 06:55:20 -0800 | [diff] [blame] | 295 | SkCanvas* canvas = doc->beginPage(100.0f, 100.0f); |
| 296 | |
robertphillips | 43c2ad4 | 2016-04-04 05:05:11 -0700 | [diff] [blame] | 297 | sk_sp<DummyImageFilter> filter(DummyImageFilter::Make()); |
senorblanco | b0e89dc | 2014-10-20 14:03:12 -0700 | [diff] [blame] | 298 | |
| 299 | // Filter just created; should be unvisited. |
| 300 | REPORTER_ASSERT(reporter, !filter->visited()); |
| 301 | SkPaint paint; |
Mike Reed | 5e25717 | 2016-11-01 11:22:05 -0400 | [diff] [blame] | 302 | paint.setImageFilter(filter); |
halcanary | 3d32d50 | 2015-03-01 06:55:20 -0800 | [diff] [blame] | 303 | canvas->drawRect(SkRect::MakeWH(100, 100), paint); |
| 304 | doc->close(); |
senorblanco | b0e89dc | 2014-10-20 14:03:12 -0700 | [diff] [blame] | 305 | |
| 306 | // Filter was used in rendering; should be visited. |
| 307 | REPORTER_ASSERT(reporter, filter->visited()); |
| 308 | } |
halcanary | 66a82f3 | 2015-10-12 13:05:04 -0700 | [diff] [blame] | 309 | |
| 310 | // Check that PDF rendering of image filters successfully falls back to |
| 311 | // CPU rasterization. |
halcanary | 57f744e | 2016-09-09 11:41:59 -0700 | [diff] [blame] | 312 | DEF_TEST(SkPDF_FontCanEmbedTypeface, reporter) { |
Hal Canary | 4ca9fa3 | 2018-12-21 16:15:01 -0500 | [diff] [blame] | 313 | SkNullWStream nullWStream; |
| 314 | SkPDFDocument doc(&nullWStream, SkPDF::Metadata()); |
halcanary | 66a82f3 | 2015-10-12 13:05:04 -0700 | [diff] [blame] | 315 | |
| 316 | const char resource[] = "fonts/Roboto2-Regular_NoEmbed.ttf"; |
bungeman | 13b9c95 | 2016-05-12 10:09:30 -0700 | [diff] [blame] | 317 | sk_sp<SkTypeface> noEmbedTypeface(MakeResourceAsTypeface(resource)); |
halcanary | 66a82f3 | 2015-10-12 13:05:04 -0700 | [diff] [blame] | 318 | if (noEmbedTypeface) { |
| 319 | REPORTER_ASSERT(reporter, |
Hal Canary | 4ca9fa3 | 2018-12-21 16:15:01 -0500 | [diff] [blame] | 320 | !SkPDFFont::CanEmbedTypeface(noEmbedTypeface.get(), &doc)); |
halcanary | 66a82f3 | 2015-10-12 13:05:04 -0700 | [diff] [blame] | 321 | } |
Mike Klein | ea3f014 | 2019-03-20 11:12:10 -0500 | [diff] [blame] | 322 | sk_sp<SkTypeface> portableTypeface(ToolUtils::create_portable_typeface(nullptr, SkFontStyle())); |
halcanary | 66a82f3 | 2015-10-12 13:05:04 -0700 | [diff] [blame] | 323 | REPORTER_ASSERT(reporter, |
Hal Canary | 4ca9fa3 | 2018-12-21 16:15:01 -0500 | [diff] [blame] | 324 | SkPDFFont::CanEmbedTypeface(portableTypeface.get(), &doc)); |
halcanary | 66a82f3 | 2015-10-12 13:05:04 -0700 | [diff] [blame] | 325 | } |
halcanary | 8e9f5e3 | 2016-02-24 15:46:46 -0800 | [diff] [blame] | 326 | |
| 327 | |
| 328 | // test to see that all finite scalars round trip via scanf(). |
| 329 | static void check_pdf_scalar_serialization( |
| 330 | skiatest::Reporter* reporter, float inputFloat) { |
Hal Canary | 3c36ef6 | 2018-01-02 16:25:53 -0500 | [diff] [blame] | 331 | char floatString[kMaximumSkFloatToDecimalLength]; |
| 332 | size_t len = SkFloatToDecimal(inputFloat, floatString); |
halcanary | 8e9f5e3 | 2016-02-24 15:46:46 -0800 | [diff] [blame] | 333 | 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 | } |
halcanary | 56586b6 | 2016-07-25 13:59:30 -0700 | [diff] [blame] | 349 | if (std::isfinite(inputFloat) && roundTripFloat != inputFloat) { |
halcanary | 8e9f5e3 | 2016-02-24 15:46:46 -0800 | [diff] [blame] | 350 | ERRORF(reporter, "roundTripFloat (%.9g) != inputFloat (%.9g)", |
| 351 | roundTripFloat, inputFloat); |
| 352 | } |
| 353 | } |
| 354 | |
| 355 | // Test SkPDFUtils::AppendScalar for accuracy. |
halcanary | 57f744e | 2016-09-09 11:41:59 -0700 | [diff] [blame] | 356 | DEF_TEST(SkPDF_Primitives_Scalar, reporter) { |
halcanary | 8e9f5e3 | 2016-02-24 15:46:46 -0800 | [diff] [blame] | 357 | 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 | } |
halcanary | eb92cb3 | 2016-07-15 13:41:27 -0700 | [diff] [blame] | 375 | |
| 376 | // Test SkPDFUtils:: for accuracy. |
halcanary | 57f744e | 2016-09-09 11:41:59 -0700 | [diff] [blame] | 377 | DEF_TEST(SkPDF_Primitives_Color, reporter) { |
halcanary | eb92cb3 | 2016-07-15 13:41:27 -0700 | [diff] [blame] | 378 | 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 Canary | 1521c8a | 2018-03-28 09:51:00 -0700 | [diff] [blame] | 388 | |
Herb Derby | 81ecdbb | 2018-07-20 11:48:38 -0400 | [diff] [blame] | 389 | static SkGlyphRun make_run(size_t len, const SkGlyphID* glyphs, SkPoint* pos, |
Mike Reed | 023403b | 2019-01-07 12:57:10 -0500 | [diff] [blame] | 390 | const SkFont& font, const uint32_t* clusters, |
Hal Canary | 98caedd | 2018-07-23 10:50:49 -0400 | [diff] [blame] | 391 | size_t utf8TextByteLength, const char* utf8Text) { |
Mike Reed | 023403b | 2019-01-07 12:57:10 -0500 | [diff] [blame] | 392 | return SkGlyphRun(font, |
Herb Derby | b935cf8 | 2018-07-26 16:54:18 -0400 | [diff] [blame] | 393 | SkSpan<const SkPoint>{pos, len}, |
Hal Canary | 98caedd | 2018-07-23 10:50:49 -0400 | [diff] [blame] | 394 | SkSpan<const SkGlyphID>{glyphs, len}, |
Hal Canary | 98caedd | 2018-07-23 10:50:49 -0400 | [diff] [blame] | 395 | SkSpan<const char>{utf8Text, utf8TextByteLength}, |
Herb Derby | 022636b | 2021-04-08 14:34:55 -0400 | [diff] [blame] | 396 | SkSpan<const uint32_t>{clusters, len}, |
| 397 | SkSpan<const SkVector>{}); |
Hal Canary | 98caedd | 2018-07-23 10:50:49 -0400 | [diff] [blame] | 398 | } |
| 399 | |
Hal Canary | 1521c8a | 2018-03-28 09:51:00 -0700 | [diff] [blame] | 400 | DEF_TEST(SkPDF_Clusterator, reporter) { |
Mike Reed | 023403b | 2019-01-07 12:57:10 -0500 | [diff] [blame] | 401 | SkFont font; |
Hal Canary | e9e19dd | 2018-04-02 11:36:39 -0400 | [diff] [blame] | 402 | { |
Hal Canary | 98caedd | 2018-07-23 10:50:49 -0400 | [diff] [blame] | 403 | 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 Derby | 81ecdbb | 2018-07-20 11:48:38 -0400 | [diff] [blame] | 406 | SkPoint pos[len] = {{0, 0}, {0, 0}, {0, 0}, {0, 0}, {0, 0}, {0, 0}, |
Hal Canary | 98caedd | 2018-07-23 10:50:49 -0400 | [diff] [blame] | 407 | {0, 0}, {0, 0}, {0, 0}, {0, 0}, {0, 0}}; |
Hal Canary | e9e19dd | 2018-04-02 11:36:39 -0400 | [diff] [blame] | 408 | const char text[] = "abcdefgh"; |
Mike Reed | 023403b | 2019-01-07 12:57:10 -0500 | [diff] [blame] | 409 | SkGlyphRun run = make_run(len, glyphs, pos, font, clusters, strlen(text), text); |
Hal Canary | 98caedd | 2018-07-23 10:50:49 -0400 | [diff] [blame] | 410 | SkClusterator clusterator(run); |
Hal Canary | e9e19dd | 2018-04-02 11:36:39 -0400 | [diff] [blame] | 411 | 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 Canary | 98caedd | 2018-07-23 10:50:49 -0400 | [diff] [blame] | 427 | 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 Derby | 81ecdbb | 2018-07-20 11:48:38 -0400 | [diff] [blame] | 430 | SkPoint pos[len] = {{0, 0}, {0, 0}, {0, 0}, {0, 0}, {0, 0}}; |
Hal Canary | e9e19dd | 2018-04-02 11:36:39 -0400 | [diff] [blame] | 431 | const char text[] = "Ha\xCC\x8A" "llo"; |
Mike Reed | 023403b | 2019-01-07 12:57:10 -0500 | [diff] [blame] | 432 | SkGlyphRun run = make_run(len, glyphs, pos, font, clusters, strlen(text), text); |
Hal Canary | 98caedd | 2018-07-23 10:50:49 -0400 | [diff] [blame] | 433 | SkClusterator clusterator(run); |
Hal Canary | e9e19dd | 2018-04-02 11:36:39 -0400 | [diff] [blame] | 434 | 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 Canary | 1521c8a | 2018-03-28 09:51:00 -0700 | [diff] [blame] | 445 | } |
| 446 | } |
| 447 | |
Hal Canary | d7cf0be | 2019-07-30 12:23:10 -0400 | [diff] [blame] | 448 | DEF_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 Ludwig | ed552f8 | 2021-01-22 13:01:26 -0500 | [diff] [blame] | 457 | layerPaint.setImageFilter(SkImageFilters::Dilate(536870912, 0, nullptr, nullptr)); |
Hal Canary | d7cf0be | 2019-07-30 12:23:10 -0400 | [diff] [blame] | 458 | 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 Canary | 43fb7a0 | 2016-12-30 13:09:03 -0500 | [diff] [blame] | 470 | #endif |