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 | |
Hal Canary | 43fb7a0 | 2016-12-30 13:09:03 -0500 | [diff] [blame] | 8 | #include "Test.h" |
| 9 | |
| 10 | #ifdef SK_SUPPORT_PDF |
| 11 | |
halcanary | 66a82f3 | 2015-10-12 13:05:04 -0700 | [diff] [blame] | 12 | #include "Resources.h" |
edisonn@google.com | d9dfa18 | 2013-04-24 13:01:01 +0000 | [diff] [blame] | 13 | #include "SkBitmap.h" |
vandebo@chromium.org | 4e1cc6a | 2013-01-25 19:27:23 +0000 | [diff] [blame] | 14 | #include "SkCanvas.h" |
Hal Canary | 1521c8a | 2018-03-28 09:51:00 -0700 | [diff] [blame] | 15 | #include "SkClusterator.h" |
reed@google.com | 8a85d0c | 2011-06-24 19:12:12 +0000 | [diff] [blame] | 16 | #include "SkData.h" |
Hal Canary | fdcfb8b | 2018-06-13 09:42:32 -0400 | [diff] [blame] | 17 | #include "SkDeflate.h" |
Hal Canary | 98caedd | 2018-07-23 10:50:49 -0400 | [diff] [blame] | 18 | #include "SkGlyphRun.h" |
edisonn@google.com | d9dfa18 | 2013-04-24 13:01:01 +0000 | [diff] [blame] | 19 | #include "SkImageEncoder.h" |
Cary Clark | 60aaeb2 | 2017-11-03 08:06:09 -0400 | [diff] [blame] | 20 | #include "SkImageFilterPriv.h" |
halcanary | 022c2bd | 2016-09-02 11:29:46 -0700 | [diff] [blame] | 21 | #include "SkMakeUnique.h" |
edisonn@google.com | d9dfa18 | 2013-04-24 13:01:01 +0000 | [diff] [blame] | 22 | #include "SkMatrix.h" |
halcanary | a1f1ee9 | 2015-02-20 06:17:26 -0800 | [diff] [blame] | 23 | #include "SkPDFCanon.h" |
vandebo@chromium.org | 4e1cc6a | 2013-01-25 19:27:23 +0000 | [diff] [blame] | 24 | #include "SkPDFDevice.h" |
Hal Canary | 23564b9 | 2018-09-07 14:33:14 -0400 | [diff] [blame] | 25 | #include "SkPDFDocument.h" |
Hal Canary | 9a3f554 | 2018-12-10 19:59:07 -0500 | [diff] [blame] | 26 | #include "SkPDFDocumentPriv.h" |
halcanary | 66a82f3 | 2015-10-12 13:05:04 -0700 | [diff] [blame] | 27 | #include "SkPDFFont.h" |
vandebo@chromium.org | 8459d4e | 2010-09-24 22:25:30 +0000 | [diff] [blame] | 28 | #include "SkPDFTypes.h" |
halcanary | 8e9f5e3 | 2016-02-24 15:46:46 -0800 | [diff] [blame] | 29 | #include "SkPDFUtils.h" |
Hal Canary | f34131a | 2018-12-18 15:11:03 -0500 | [diff] [blame] | 30 | #include "SkPDFUnion.h" |
senorblanco | b0e89dc | 2014-10-20 14:03:12 -0700 | [diff] [blame] | 31 | #include "SkReadBuffer.h" |
vandebo@chromium.org | 8459d4e | 2010-09-24 22:25:30 +0000 | [diff] [blame] | 32 | #include "SkScalar.h" |
robertphillips | 4ba94e2 | 2016-04-04 12:07:47 -0700 | [diff] [blame] | 33 | #include "SkSpecialImage.h" |
vandebo@chromium.org | 8459d4e | 2010-09-24 22:25:30 +0000 | [diff] [blame] | 34 | #include "SkStream.h" |
Hal Canary | c640d0d | 2018-06-13 09:59:02 -0400 | [diff] [blame] | 35 | #include "SkTo.h" |
vandebo@chromium.org | 421d644 | 2011-07-20 17:39:01 +0000 | [diff] [blame] | 36 | #include "SkTypes.h" |
halcanary | 66a82f3 | 2015-10-12 13:05:04 -0700 | [diff] [blame] | 37 | #include "sk_tool_utils.h" |
vandebo@chromium.org | 8459d4e | 2010-09-24 22:25:30 +0000 | [diff] [blame] | 38 | |
halcanary | 56586b6 | 2016-07-25 13:59:30 -0700 | [diff] [blame] | 39 | #include <cstdlib> |
| 40 | #include <cmath> |
| 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( |
| 63 | "'%*s' != '%s'", len, str, skString.c_str())); |
| 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 | 23564b9 | 2018-09-07 14:33:14 -0400 | [diff] [blame] | 88 | sk_sp<SkDocument> 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 | SkPaint paint; |
Mike Reed | 97f3cc2 | 2018-12-03 09:45:17 -0500 | [diff] [blame] | 91 | paint.setTextEncoding(kGlyphID_SkTextEncoding); |
vandebo@chromium.org | 4e1cc6a | 2013-01-25 19:27:23 +0000 | [diff] [blame] | 92 | |
| 93 | uint16_t glyphID = 65000; |
halcanary | 3d32d50 | 2015-03-01 06:55:20 -0800 | [diff] [blame] | 94 | canvas->drawText(&glyphID, 2, 0, 0, paint); |
vandebo@chromium.org | 4e1cc6a | 2013-01-25 19:27:23 +0000 | [diff] [blame] | 95 | |
halcanary | 3d32d50 | 2015-03-01 06:55:20 -0800 | [diff] [blame] | 96 | doc->close(); |
vandebo@chromium.org | 4e1cc6a | 2013-01-25 19:27:23 +0000 | [diff] [blame] | 97 | } |
| 98 | |
halcanary | 56586b6 | 2016-07-25 13:59:30 -0700 | [diff] [blame] | 99 | static void assert_emit_eq_number(skiatest::Reporter* reporter, float number) { |
| 100 | SkPDFUnion pdfUnion = SkPDFUnion::Scalar(number); |
| 101 | SkString result = emit_to_string(pdfUnion); |
| 102 | float value = static_cast<float>(std::atof(result.c_str())); |
| 103 | if (value != number) { |
| 104 | ERRORF(reporter, "%.9g != %s", number, result.c_str()); |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | |
halcanary | f7a169e | 2015-05-01 07:35:45 -0700 | [diff] [blame] | 109 | static void TestPDFUnion(skiatest::Reporter* reporter) { |
| 110 | SkPDFUnion boolTrue = SkPDFUnion::Bool(true); |
halcanary | 56586b6 | 2016-07-25 13:59:30 -0700 | [diff] [blame] | 111 | assert_emit_eq(reporter, boolTrue, "true"); |
vandebo@chromium.org | 8459d4e | 2010-09-24 22:25:30 +0000 | [diff] [blame] | 112 | |
halcanary | f7a169e | 2015-05-01 07:35:45 -0700 | [diff] [blame] | 113 | SkPDFUnion boolFalse = SkPDFUnion::Bool(false); |
halcanary | 56586b6 | 2016-07-25 13:59:30 -0700 | [diff] [blame] | 114 | assert_emit_eq(reporter, boolFalse, "false"); |
vandebo@chromium.org | 8459d4e | 2010-09-24 22:25:30 +0000 | [diff] [blame] | 115 | |
halcanary | f7a169e | 2015-05-01 07:35:45 -0700 | [diff] [blame] | 116 | SkPDFUnion int42 = SkPDFUnion::Int(42); |
halcanary | 56586b6 | 2016-07-25 13:59:30 -0700 | [diff] [blame] | 117 | assert_emit_eq(reporter, int42, "42"); |
halcanary | f7a169e | 2015-05-01 07:35:45 -0700 | [diff] [blame] | 118 | |
halcanary | 56586b6 | 2016-07-25 13:59:30 -0700 | [diff] [blame] | 119 | assert_emit_eq_number(reporter, SK_ScalarHalf); |
| 120 | assert_emit_eq_number(reporter, 110999.75f); // bigScalar |
| 121 | assert_emit_eq_number(reporter, 50000000.1f); // biggerScalar |
| 122 | assert_emit_eq_number(reporter, 1.0f / 65536); // smallScalar |
vandebo@chromium.org | 094316b | 2011-03-04 03:15:13 +0000 | [diff] [blame] | 123 | |
halcanary | f7a169e | 2015-05-01 07:35:45 -0700 | [diff] [blame] | 124 | SkPDFUnion stringSimple = SkPDFUnion::String("test ) string ( foo"); |
halcanary | 56586b6 | 2016-07-25 13:59:30 -0700 | [diff] [blame] | 125 | assert_emit_eq(reporter, stringSimple, "(test \\) string \\( foo)"); |
vandebo@chromium.org | 8459d4e | 2010-09-24 22:25:30 +0000 | [diff] [blame] | 126 | |
halcanary | f7a169e | 2015-05-01 07:35:45 -0700 | [diff] [blame] | 127 | SkString stringComplexInput("\ttest ) string ( foo"); |
| 128 | SkPDFUnion stringComplex = SkPDFUnion::String(stringComplexInput); |
halcanary | 56586b6 | 2016-07-25 13:59:30 -0700 | [diff] [blame] | 129 | assert_emit_eq(reporter, stringComplex, "(\\011test \\) string \\( foo)"); |
halcanary | ee41b75 | 2016-06-23 14:08:11 -0700 | [diff] [blame] | 130 | |
| 131 | SkString binaryStringInput("\1\2\3\4\5\6\7\10\11\12\13\14\15\16\17\20"); |
| 132 | SkPDFUnion binaryString = SkPDFUnion::String(binaryStringInput); |
halcanary | 56586b6 | 2016-07-25 13:59:30 -0700 | [diff] [blame] | 133 | assert_emit_eq(reporter, binaryString, "<0102030405060708090A0B0C0D0E0F10>"); |
vandebo@chromium.org | 8459d4e | 2010-09-24 22:25:30 +0000 | [diff] [blame] | 134 | |
halcanary | f7a169e | 2015-05-01 07:35:45 -0700 | [diff] [blame] | 135 | SkString nameInput("Test name\twith#tab"); |
| 136 | SkPDFUnion name = SkPDFUnion::Name(nameInput); |
halcanary | 56586b6 | 2016-07-25 13:59:30 -0700 | [diff] [blame] | 137 | assert_emit_eq(reporter, name, "/Test#20name#09with#23tab"); |
halcanary | f7a169e | 2015-05-01 07:35:45 -0700 | [diff] [blame] | 138 | |
| 139 | SkString nameInput2("A#/%()<>[]{}B"); |
| 140 | SkPDFUnion name2 = SkPDFUnion::Name(nameInput2); |
halcanary | 56586b6 | 2016-07-25 13:59:30 -0700 | [diff] [blame] | 141 | 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] | 142 | |
| 143 | SkPDFUnion name3 = SkPDFUnion::Name("SimpleNameWithOnlyPrintableASCII"); |
halcanary | 56586b6 | 2016-07-25 13:59:30 -0700 | [diff] [blame] | 144 | assert_emit_eq(reporter, name3, "/SimpleNameWithOnlyPrintableASCII"); |
vandebo@chromium.org | 251a766 | 2012-09-21 17:50:50 +0000 | [diff] [blame] | 145 | |
vandebo@chromium.org | c0376fe | 2012-03-05 18:44:33 +0000 | [diff] [blame] | 146 | // Test that we correctly handle characters with the high-bit set. |
halcanary | f7a169e | 2015-05-01 07:35:45 -0700 | [diff] [blame] | 147 | SkString highBitString("\xDE\xAD" "be\xEF"); |
| 148 | SkPDFUnion highBitName = SkPDFUnion::Name(highBitString); |
halcanary | 56586b6 | 2016-07-25 13:59:30 -0700 | [diff] [blame] | 149 | assert_emit_eq(reporter, highBitName, "/#DE#ADbe#EF"); |
halcanary | f7a169e | 2015-05-01 07:35:45 -0700 | [diff] [blame] | 150 | } |
vandebo@chromium.org | c0376fe | 2012-03-05 18:44:33 +0000 | [diff] [blame] | 151 | |
halcanary | f7a169e | 2015-05-01 07:35:45 -0700 | [diff] [blame] | 152 | static void TestPDFArray(skiatest::Reporter* reporter) { |
Hal Canary | 7480158 | 2018-12-18 16:30:41 -0500 | [diff] [blame] | 153 | std::unique_ptr<SkPDFArray> array(new SkPDFArray); |
halcanary | 56586b6 | 2016-07-25 13:59:30 -0700 | [diff] [blame] | 154 | assert_emit_eq(reporter, *array, "[]"); |
vandebo@chromium.org | 8459d4e | 2010-09-24 22:25:30 +0000 | [diff] [blame] | 155 | |
halcanary | f7a169e | 2015-05-01 07:35:45 -0700 | [diff] [blame] | 156 | array->appendInt(42); |
halcanary | 56586b6 | 2016-07-25 13:59:30 -0700 | [diff] [blame] | 157 | assert_emit_eq(reporter, *array, "[42]"); |
halcanary | f7a169e | 2015-05-01 07:35:45 -0700 | [diff] [blame] | 158 | |
| 159 | array->appendScalar(SK_ScalarHalf); |
halcanary | 56586b6 | 2016-07-25 13:59:30 -0700 | [diff] [blame] | 160 | assert_emit_eq(reporter, *array, "[42 .5]"); |
halcanary | f7a169e | 2015-05-01 07:35:45 -0700 | [diff] [blame] | 161 | |
| 162 | array->appendInt(0); |
halcanary | 56586b6 | 2016-07-25 13:59:30 -0700 | [diff] [blame] | 163 | assert_emit_eq(reporter, *array, "[42 .5 0]"); |
halcanary | f7a169e | 2015-05-01 07:35:45 -0700 | [diff] [blame] | 164 | |
| 165 | array->appendBool(true); |
halcanary | 56586b6 | 2016-07-25 13:59:30 -0700 | [diff] [blame] | 166 | assert_emit_eq(reporter, *array, "[42 .5 0 true]"); |
halcanary | f7a169e | 2015-05-01 07:35:45 -0700 | [diff] [blame] | 167 | |
| 168 | array->appendName("ThisName"); |
halcanary | 56586b6 | 2016-07-25 13:59:30 -0700 | [diff] [blame] | 169 | assert_emit_eq(reporter, *array, "[42 .5 0 true /ThisName]"); |
halcanary | f7a169e | 2015-05-01 07:35:45 -0700 | [diff] [blame] | 170 | |
| 171 | array->appendName(SkString("AnotherName")); |
halcanary | 56586b6 | 2016-07-25 13:59:30 -0700 | [diff] [blame] | 172 | assert_emit_eq(reporter, *array, "[42 .5 0 true /ThisName /AnotherName]"); |
halcanary | f7a169e | 2015-05-01 07:35:45 -0700 | [diff] [blame] | 173 | |
| 174 | array->appendString("This String"); |
halcanary | 56586b6 | 2016-07-25 13:59:30 -0700 | [diff] [blame] | 175 | assert_emit_eq(reporter, *array, |
halcanary | 8e9f5e3 | 2016-02-24 15:46:46 -0800 | [diff] [blame] | 176 | "[42 .5 0 true /ThisName /AnotherName (This String)]"); |
halcanary | f7a169e | 2015-05-01 07:35:45 -0700 | [diff] [blame] | 177 | |
| 178 | array->appendString(SkString("Another String")); |
halcanary | 56586b6 | 2016-07-25 13:59:30 -0700 | [diff] [blame] | 179 | assert_emit_eq(reporter, *array, |
halcanary | 8e9f5e3 | 2016-02-24 15:46:46 -0800 | [diff] [blame] | 180 | "[42 .5 0 true /ThisName /AnotherName (This String) " |
halcanary | f7a169e | 2015-05-01 07:35:45 -0700 | [diff] [blame] | 181 | "(Another String)]"); |
| 182 | |
Hal Canary | 7480158 | 2018-12-18 16:30:41 -0500 | [diff] [blame] | 183 | std::unique_ptr<SkPDFArray> innerArray(new SkPDFArray); |
halcanary | f7a169e | 2015-05-01 07:35:45 -0700 | [diff] [blame] | 184 | innerArray->appendInt(-1); |
halcanary | 8103a34 | 2016-03-08 15:10:16 -0800 | [diff] [blame] | 185 | array->appendObject(std::move(innerArray)); |
halcanary | 56586b6 | 2016-07-25 13:59:30 -0700 | [diff] [blame] | 186 | assert_emit_eq(reporter, *array, |
halcanary | 8e9f5e3 | 2016-02-24 15:46:46 -0800 | [diff] [blame] | 187 | "[42 .5 0 true /ThisName /AnotherName (This String) " |
halcanary | f7a169e | 2015-05-01 07:35:45 -0700 | [diff] [blame] | 188 | "(Another String) [-1]]"); |
halcanary | f7a169e | 2015-05-01 07:35:45 -0700 | [diff] [blame] | 189 | } |
| 190 | |
| 191 | static void TestPDFDict(skiatest::Reporter* reporter) { |
Hal Canary | 7480158 | 2018-12-18 16:30:41 -0500 | [diff] [blame] | 192 | std::unique_ptr<SkPDFDict> dict(new SkPDFDict); |
halcanary | 56586b6 | 2016-07-25 13:59:30 -0700 | [diff] [blame] | 193 | assert_emit_eq(reporter, *dict, "<<>>"); |
halcanary | f7a169e | 2015-05-01 07:35:45 -0700 | [diff] [blame] | 194 | |
| 195 | dict->insertInt("n1", SkToSizeT(42)); |
halcanary | 56586b6 | 2016-07-25 13:59:30 -0700 | [diff] [blame] | 196 | assert_emit_eq(reporter, *dict, "<</n1 42>>"); |
halcanary | f7a169e | 2015-05-01 07:35:45 -0700 | [diff] [blame] | 197 | |
| 198 | dict.reset(new SkPDFDict); |
halcanary | 56586b6 | 2016-07-25 13:59:30 -0700 | [diff] [blame] | 199 | assert_emit_eq(reporter, *dict, "<<>>"); |
halcanary | f7a169e | 2015-05-01 07:35:45 -0700 | [diff] [blame] | 200 | |
| 201 | dict->insertInt("n1", 42); |
halcanary | 56586b6 | 2016-07-25 13:59:30 -0700 | [diff] [blame] | 202 | assert_emit_eq(reporter, *dict, "<</n1 42>>"); |
halcanary | f7a169e | 2015-05-01 07:35:45 -0700 | [diff] [blame] | 203 | |
| 204 | dict->insertScalar("n2", SK_ScalarHalf); |
| 205 | |
halcanary | 130444f | 2015-04-25 06:45:07 -0700 | [diff] [blame] | 206 | SkString n3("n3"); |
Hal Canary | 7480158 | 2018-12-18 16:30:41 -0500 | [diff] [blame] | 207 | std::unique_ptr<SkPDFArray> innerArray(new SkPDFArray); |
halcanary | f7a169e | 2015-05-01 07:35:45 -0700 | [diff] [blame] | 208 | innerArray->appendInt(-100); |
halcanary | 8103a34 | 2016-03-08 15:10:16 -0800 | [diff] [blame] | 209 | dict->insertObject(n3, std::move(innerArray)); |
halcanary | 56586b6 | 2016-07-25 13:59:30 -0700 | [diff] [blame] | 210 | 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] | 211 | |
halcanary | f7a169e | 2015-05-01 07:35:45 -0700 | [diff] [blame] | 212 | dict.reset(new SkPDFDict); |
halcanary | 56586b6 | 2016-07-25 13:59:30 -0700 | [diff] [blame] | 213 | assert_emit_eq(reporter, *dict, "<<>>"); |
halcanary | f7a169e | 2015-05-01 07:35:45 -0700 | [diff] [blame] | 214 | |
| 215 | dict->insertInt("n1", 24); |
halcanary | 56586b6 | 2016-07-25 13:59:30 -0700 | [diff] [blame] | 216 | assert_emit_eq(reporter, *dict, "<</n1 24>>"); |
halcanary | f7a169e | 2015-05-01 07:35:45 -0700 | [diff] [blame] | 217 | |
| 218 | dict->insertInt("n2", SkToSizeT(99)); |
halcanary | 56586b6 | 2016-07-25 13:59:30 -0700 | [diff] [blame] | 219 | assert_emit_eq(reporter, *dict, "<</n1 24\n/n2 99>>"); |
halcanary | f7a169e | 2015-05-01 07:35:45 -0700 | [diff] [blame] | 220 | |
| 221 | dict->insertScalar("n3", SK_ScalarHalf); |
halcanary | 56586b6 | 2016-07-25 13:59:30 -0700 | [diff] [blame] | 222 | assert_emit_eq(reporter, *dict, "<</n1 24\n/n2 99\n/n3 .5>>"); |
halcanary | f7a169e | 2015-05-01 07:35:45 -0700 | [diff] [blame] | 223 | |
| 224 | dict->insertName("n4", "AName"); |
halcanary | 56586b6 | 2016-07-25 13:59:30 -0700 | [diff] [blame] | 225 | 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] | 226 | |
| 227 | dict->insertName("n5", SkString("AnotherName")); |
halcanary | 56586b6 | 2016-07-25 13:59:30 -0700 | [diff] [blame] | 228 | 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] | 229 | "/n5 /AnotherName>>"); |
| 230 | |
| 231 | dict->insertString("n6", "A String"); |
halcanary | 56586b6 | 2016-07-25 13:59:30 -0700 | [diff] [blame] | 232 | 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] | 233 | "/n5 /AnotherName\n/n6 (A String)>>"); |
| 234 | |
| 235 | dict->insertString("n7", SkString("Another String")); |
halcanary | 56586b6 | 2016-07-25 13:59:30 -0700 | [diff] [blame] | 236 | 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] | 237 | "/n5 /AnotherName\n/n6 (A String)\n/n7 (Another String)>>"); |
| 238 | |
| 239 | dict.reset(new SkPDFDict("DType")); |
halcanary | 56586b6 | 2016-07-25 13:59:30 -0700 | [diff] [blame] | 240 | assert_emit_eq(reporter, *dict, "<</Type /DType>>"); |
halcanary | f7a169e | 2015-05-01 07:35:45 -0700 | [diff] [blame] | 241 | } |
| 242 | |
halcanary | 57f744e | 2016-09-09 11:41:59 -0700 | [diff] [blame] | 243 | DEF_TEST(SkPDF_Primitives, reporter) { |
halcanary | f7a169e | 2015-05-01 07:35:45 -0700 | [diff] [blame] | 244 | TestPDFUnion(reporter); |
| 245 | TestPDFArray(reporter); |
| 246 | TestPDFDict(reporter); |
sugoi@google.com | 54f0d1b | 2013-02-27 19:17:41 +0000 | [diff] [blame] | 247 | test_issue1083(); |
vandebo@chromium.org | 8459d4e | 2010-09-24 22:25:30 +0000 | [diff] [blame] | 248 | } |
senorblanco | b0e89dc | 2014-10-20 14:03:12 -0700 | [diff] [blame] | 249 | |
| 250 | namespace { |
| 251 | |
| 252 | class DummyImageFilter : public SkImageFilter { |
| 253 | public: |
robertphillips | 43c2ad4 | 2016-04-04 05:05:11 -0700 | [diff] [blame] | 254 | static sk_sp<DummyImageFilter> Make(bool visited = false) { |
| 255 | return sk_sp<DummyImageFilter>(new DummyImageFilter(visited)); |
| 256 | } |
| 257 | |
robertphillips | 43c2ad4 | 2016-04-04 05:05:11 -0700 | [diff] [blame] | 258 | bool visited() const { return fVisited; } |
| 259 | |
| 260 | protected: |
robertphillips | 4ba94e2 | 2016-04-04 12:07:47 -0700 | [diff] [blame] | 261 | sk_sp<SkSpecialImage> onFilterImage(SkSpecialImage* source, const Context&, |
| 262 | SkIPoint* offset) const override { |
senorblanco | b0e89dc | 2014-10-20 14:03:12 -0700 | [diff] [blame] | 263 | fVisited = true; |
| 264 | offset->fX = offset->fY = 0; |
robertphillips | 4ba94e2 | 2016-04-04 12:07:47 -0700 | [diff] [blame] | 265 | return sk_ref_sp<SkSpecialImage>(source); |
senorblanco | b0e89dc | 2014-10-20 14:03:12 -0700 | [diff] [blame] | 266 | } |
Matt Sarett | 62745a8 | 2017-04-17 11:57:29 -0400 | [diff] [blame] | 267 | sk_sp<SkImageFilter> onMakeColorSpace(SkColorSpaceXformer*) const override { |
| 268 | return sk_ref_sp(const_cast<DummyImageFilter*>(this)); |
| 269 | } |
senorblanco | b0e89dc | 2014-10-20 14:03:12 -0700 | [diff] [blame] | 270 | |
| 271 | private: |
Mike Klein | 4fee323 | 2018-10-18 17:27:16 -0400 | [diff] [blame] | 272 | SK_FLATTENABLE_HOOKS(DummyImageFilter) |
robertphillips | 43c2ad4 | 2016-04-04 05:05:11 -0700 | [diff] [blame] | 273 | DummyImageFilter(bool visited) : INHERITED(nullptr, 0, nullptr), fVisited(visited) {} |
| 274 | |
senorblanco | b0e89dc | 2014-10-20 14:03:12 -0700 | [diff] [blame] | 275 | mutable bool fVisited; |
robertphillips | 43c2ad4 | 2016-04-04 05:05:11 -0700 | [diff] [blame] | 276 | |
| 277 | typedef SkImageFilter INHERITED; |
senorblanco | b0e89dc | 2014-10-20 14:03:12 -0700 | [diff] [blame] | 278 | }; |
| 279 | |
reed | 60c9b58 | 2016-04-03 09:11:13 -0700 | [diff] [blame] | 280 | sk_sp<SkFlattenable> DummyImageFilter::CreateProc(SkReadBuffer& buffer) { |
senorblanco | b0e89dc | 2014-10-20 14:03:12 -0700 | [diff] [blame] | 281 | SK_IMAGEFILTER_UNFLATTEN_COMMON(common, 0); |
| 282 | bool visited = buffer.readBool(); |
robertphillips | 43c2ad4 | 2016-04-04 05:05:11 -0700 | [diff] [blame] | 283 | return DummyImageFilter::Make(visited); |
senorblanco | b0e89dc | 2014-10-20 14:03:12 -0700 | [diff] [blame] | 284 | } |
| 285 | |
| 286 | }; |
| 287 | |
| 288 | // Check that PDF rendering of image filters successfully falls back to |
| 289 | // CPU rasterization. |
halcanary | 57f744e | 2016-09-09 11:41:59 -0700 | [diff] [blame] | 290 | DEF_TEST(SkPDF_ImageFilter, reporter) { |
| 291 | REQUIRE_PDF_DOCUMENT(SkPDF_ImageFilter, reporter); |
halcanary | 3d32d50 | 2015-03-01 06:55:20 -0800 | [diff] [blame] | 292 | SkDynamicMemoryWStream stream; |
Hal Canary | 23564b9 | 2018-09-07 14:33:14 -0400 | [diff] [blame] | 293 | sk_sp<SkDocument> doc(SkPDF::MakeDocument(&stream)); |
halcanary | 3d32d50 | 2015-03-01 06:55:20 -0800 | [diff] [blame] | 294 | SkCanvas* canvas = doc->beginPage(100.0f, 100.0f); |
| 295 | |
robertphillips | 43c2ad4 | 2016-04-04 05:05:11 -0700 | [diff] [blame] | 296 | sk_sp<DummyImageFilter> filter(DummyImageFilter::Make()); |
senorblanco | b0e89dc | 2014-10-20 14:03:12 -0700 | [diff] [blame] | 297 | |
| 298 | // Filter just created; should be unvisited. |
| 299 | REPORTER_ASSERT(reporter, !filter->visited()); |
| 300 | SkPaint paint; |
Mike Reed | 5e25717 | 2016-11-01 11:22:05 -0400 | [diff] [blame] | 301 | paint.setImageFilter(filter); |
halcanary | 3d32d50 | 2015-03-01 06:55:20 -0800 | [diff] [blame] | 302 | canvas->drawRect(SkRect::MakeWH(100, 100), paint); |
| 303 | doc->close(); |
senorblanco | b0e89dc | 2014-10-20 14:03:12 -0700 | [diff] [blame] | 304 | |
| 305 | // Filter was used in rendering; should be visited. |
| 306 | REPORTER_ASSERT(reporter, filter->visited()); |
| 307 | } |
halcanary | 66a82f3 | 2015-10-12 13:05:04 -0700 | [diff] [blame] | 308 | |
| 309 | // Check that PDF rendering of image filters successfully falls back to |
| 310 | // CPU rasterization. |
halcanary | 57f744e | 2016-09-09 11:41:59 -0700 | [diff] [blame] | 311 | DEF_TEST(SkPDF_FontCanEmbedTypeface, reporter) { |
halcanary | 66a82f3 | 2015-10-12 13:05:04 -0700 | [diff] [blame] | 312 | SkPDFCanon canon; |
| 313 | |
| 314 | const char resource[] = "fonts/Roboto2-Regular_NoEmbed.ttf"; |
bungeman | 13b9c95 | 2016-05-12 10:09:30 -0700 | [diff] [blame] | 315 | sk_sp<SkTypeface> noEmbedTypeface(MakeResourceAsTypeface(resource)); |
halcanary | 66a82f3 | 2015-10-12 13:05:04 -0700 | [diff] [blame] | 316 | if (noEmbedTypeface) { |
| 317 | REPORTER_ASSERT(reporter, |
halcanary | 8103a34 | 2016-03-08 15:10:16 -0800 | [diff] [blame] | 318 | !SkPDFFont::CanEmbedTypeface(noEmbedTypeface.get(), &canon)); |
halcanary | 66a82f3 | 2015-10-12 13:05:04 -0700 | [diff] [blame] | 319 | } |
halcanary | 8103a34 | 2016-03-08 15:10:16 -0800 | [diff] [blame] | 320 | sk_sp<SkTypeface> portableTypeface( |
Ben Wagner | a93a14a | 2017-08-28 10:34:05 -0400 | [diff] [blame] | 321 | sk_tool_utils::create_portable_typeface(nullptr, SkFontStyle())); |
halcanary | 66a82f3 | 2015-10-12 13:05:04 -0700 | [diff] [blame] | 322 | REPORTER_ASSERT(reporter, |
halcanary | 8103a34 | 2016-03-08 15:10:16 -0800 | [diff] [blame] | 323 | SkPDFFont::CanEmbedTypeface(portableTypeface.get(), &canon)); |
halcanary | 66a82f3 | 2015-10-12 13:05:04 -0700 | [diff] [blame] | 324 | } |
halcanary | 8e9f5e3 | 2016-02-24 15:46:46 -0800 | [diff] [blame] | 325 | |
| 326 | |
| 327 | // test to see that all finite scalars round trip via scanf(). |
| 328 | static void check_pdf_scalar_serialization( |
| 329 | skiatest::Reporter* reporter, float inputFloat) { |
Hal Canary | 3c36ef6 | 2018-01-02 16:25:53 -0500 | [diff] [blame] | 330 | char floatString[kMaximumSkFloatToDecimalLength]; |
| 331 | size_t len = SkFloatToDecimal(inputFloat, floatString); |
halcanary | 8e9f5e3 | 2016-02-24 15:46:46 -0800 | [diff] [blame] | 332 | 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 | } |
halcanary | 56586b6 | 2016-07-25 13:59:30 -0700 | [diff] [blame] | 348 | if (std::isfinite(inputFloat) && roundTripFloat != inputFloat) { |
halcanary | 8e9f5e3 | 2016-02-24 15:46:46 -0800 | [diff] [blame] | 349 | ERRORF(reporter, "roundTripFloat (%.9g) != inputFloat (%.9g)", |
| 350 | roundTripFloat, inputFloat); |
| 351 | } |
| 352 | } |
| 353 | |
| 354 | // Test SkPDFUtils::AppendScalar for accuracy. |
halcanary | 57f744e | 2016-09-09 11:41:59 -0700 | [diff] [blame] | 355 | DEF_TEST(SkPDF_Primitives_Scalar, reporter) { |
halcanary | 8e9f5e3 | 2016-02-24 15:46:46 -0800 | [diff] [blame] | 356 | 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 | } |
halcanary | eb92cb3 | 2016-07-15 13:41:27 -0700 | [diff] [blame] | 374 | |
| 375 | // Test SkPDFUtils:: for accuracy. |
halcanary | 57f744e | 2016-09-09 11:41:59 -0700 | [diff] [blame] | 376 | DEF_TEST(SkPDF_Primitives_Color, reporter) { |
halcanary | eb92cb3 | 2016-07-15 13:41:27 -0700 | [diff] [blame] | 377 | 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 Canary | 1521c8a | 2018-03-28 09:51:00 -0700 | [diff] [blame] | 387 | |
Herb Derby | 81ecdbb | 2018-07-20 11:48:38 -0400 | [diff] [blame] | 388 | static SkGlyphRun make_run(size_t len, const SkGlyphID* glyphs, SkPoint* pos, |
Hal Canary | 98caedd | 2018-07-23 10:50:49 -0400 | [diff] [blame] | 389 | SkPaint paint, const uint32_t* clusters, |
| 390 | size_t utf8TextByteLength, const char* utf8Text) { |
Herb Derby | 95e1760 | 2018-12-06 17:11:43 -0500 | [diff] [blame] | 391 | return SkGlyphRun(SkFont::LEGACY_ExtractFromPaint(paint), |
Herb Derby | b935cf8 | 2018-07-26 16:54:18 -0400 | [diff] [blame] | 392 | SkSpan<const SkPoint>{pos, len}, |
Hal Canary | 98caedd | 2018-07-23 10:50:49 -0400 | [diff] [blame] | 393 | SkSpan<const SkGlyphID>{glyphs, len}, |
Hal Canary | 98caedd | 2018-07-23 10:50:49 -0400 | [diff] [blame] | 394 | SkSpan<const char>{utf8Text, utf8TextByteLength}, |
| 395 | SkSpan<const uint32_t>{clusters, len}); |
| 396 | } |
| 397 | |
Hal Canary | 1521c8a | 2018-03-28 09:51:00 -0700 | [diff] [blame] | 398 | DEF_TEST(SkPDF_Clusterator, reporter) { |
Hal Canary | 1521c8a | 2018-03-28 09:51:00 -0700 | [diff] [blame] | 399 | SkPaint paint; |
Mike Reed | 97f3cc2 | 2018-12-03 09:45:17 -0500 | [diff] [blame] | 400 | paint.setTextEncoding(kGlyphID_SkTextEncoding); |
Hal Canary | e9e19dd | 2018-04-02 11:36:39 -0400 | [diff] [blame] | 401 | { |
Hal Canary | 98caedd | 2018-07-23 10:50:49 -0400 | [diff] [blame] | 402 | constexpr unsigned len = 11; |
| 403 | const uint32_t clusters[len] = { 3, 2, 2, 1, 0, 4, 4, 7, 6, 6, 5 }; |
| 404 | 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] | 405 | 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] | 406 | {0, 0}, {0, 0}, {0, 0}, {0, 0}, {0, 0}}; |
Hal Canary | e9e19dd | 2018-04-02 11:36:39 -0400 | [diff] [blame] | 407 | const char text[] = "abcdefgh"; |
Hal Canary | 98caedd | 2018-07-23 10:50:49 -0400 | [diff] [blame] | 408 | SkGlyphRun run = make_run(len, glyphs, pos, paint, clusters, strlen(text), text); |
| 409 | SkClusterator clusterator(run); |
Hal Canary | e9e19dd | 2018-04-02 11:36:39 -0400 | [diff] [blame] | 410 | SkClusterator::Cluster expectations[] = { |
| 411 | {&text[3], 1, 0, 1}, |
| 412 | {&text[2], 1, 1, 2}, |
| 413 | {&text[1], 1, 3, 1}, |
| 414 | {&text[0], 1, 4, 1}, |
| 415 | {&text[4], 1, 5, 2}, |
| 416 | {&text[7], 1, 7, 1}, |
| 417 | {&text[6], 1, 8, 2}, |
| 418 | {&text[5], 1, 10, 1}, |
| 419 | {nullptr, 0, 0, 0}, |
| 420 | }; |
| 421 | for (const auto& expectation : expectations) { |
| 422 | REPORTER_ASSERT(reporter, clusterator.next() == expectation); |
| 423 | } |
| 424 | } |
| 425 | { |
Hal Canary | 98caedd | 2018-07-23 10:50:49 -0400 | [diff] [blame] | 426 | constexpr unsigned len = 5; |
| 427 | const uint32_t clusters[len] = { 0, 1, 4, 5, 6 }; |
| 428 | const SkGlyphID glyphs[len] = { 43, 167, 79, 79, 82, }; |
Herb Derby | 81ecdbb | 2018-07-20 11:48:38 -0400 | [diff] [blame] | 429 | 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] | 430 | const char text[] = "Ha\xCC\x8A" "llo"; |
Hal Canary | 98caedd | 2018-07-23 10:50:49 -0400 | [diff] [blame] | 431 | SkGlyphRun run = make_run(len, glyphs, pos, paint, clusters, strlen(text), text); |
| 432 | SkClusterator clusterator(run); |
Hal Canary | e9e19dd | 2018-04-02 11:36:39 -0400 | [diff] [blame] | 433 | SkClusterator::Cluster expectations[] = { |
| 434 | {&text[0], 1, 0, 1}, |
| 435 | {&text[1], 3, 1, 1}, |
| 436 | {&text[4], 1, 2, 1}, |
| 437 | {&text[5], 1, 3, 1}, |
| 438 | {&text[6], 1, 4, 1}, |
| 439 | {nullptr, 0, 0, 0}, |
| 440 | }; |
| 441 | for (const auto& expectation : expectations) { |
| 442 | REPORTER_ASSERT(reporter, clusterator.next() == expectation); |
| 443 | } |
Hal Canary | 1521c8a | 2018-03-28 09:51:00 -0700 | [diff] [blame] | 444 | } |
| 445 | } |
| 446 | |
Hal Canary | 43fb7a0 | 2016-12-30 13:09:03 -0500 | [diff] [blame] | 447 | #endif |