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 | c640d0d | 2018-06-13 09:59:02 -0400 | [diff] [blame] | 18 | #include "SkDocument.h" |
Hal Canary | 98caedd | 2018-07-23 10:50:49 -0400 | [diff] [blame] | 19 | #include "SkGlyphRun.h" |
edisonn@google.com | d9dfa18 | 2013-04-24 13:01:01 +0000 | [diff] [blame] | 20 | #include "SkImageEncoder.h" |
Cary Clark | 60aaeb2 | 2017-11-03 08:06:09 -0400 | [diff] [blame] | 21 | #include "SkImageFilterPriv.h" |
halcanary | 022c2bd | 2016-09-02 11:29:46 -0700 | [diff] [blame] | 22 | #include "SkMakeUnique.h" |
edisonn@google.com | d9dfa18 | 2013-04-24 13:01:01 +0000 | [diff] [blame] | 23 | #include "SkMatrix.h" |
halcanary | a1f1ee9 | 2015-02-20 06:17:26 -0800 | [diff] [blame] | 24 | #include "SkPDFCanon.h" |
vandebo@chromium.org | 4e1cc6a | 2013-01-25 19:27:23 +0000 | [diff] [blame] | 25 | #include "SkPDFDevice.h" |
halcanary | 66a82f3 | 2015-10-12 13:05:04 -0700 | [diff] [blame] | 26 | #include "SkPDFFont.h" |
vandebo@chromium.org | 8459d4e | 2010-09-24 22:25:30 +0000 | [diff] [blame] | 27 | #include "SkPDFTypes.h" |
halcanary | 8e9f5e3 | 2016-02-24 15:46:46 -0800 | [diff] [blame] | 28 | #include "SkPDFUtils.h" |
senorblanco | b0e89dc | 2014-10-20 14:03:12 -0700 | [diff] [blame] | 29 | #include "SkReadBuffer.h" |
vandebo@chromium.org | 8459d4e | 2010-09-24 22:25:30 +0000 | [diff] [blame] | 30 | #include "SkScalar.h" |
robertphillips | 4ba94e2 | 2016-04-04 12:07:47 -0700 | [diff] [blame] | 31 | #include "SkSpecialImage.h" |
vandebo@chromium.org | 8459d4e | 2010-09-24 22:25:30 +0000 | [diff] [blame] | 32 | #include "SkStream.h" |
Hal Canary | c640d0d | 2018-06-13 09:59:02 -0400 | [diff] [blame] | 33 | #include "SkTo.h" |
vandebo@chromium.org | 421d644 | 2011-07-20 17:39:01 +0000 | [diff] [blame] | 34 | #include "SkTypes.h" |
halcanary | 66a82f3 | 2015-10-12 13:05:04 -0700 | [diff] [blame] | 35 | #include "sk_tool_utils.h" |
vandebo@chromium.org | 8459d4e | 2010-09-24 22:25:30 +0000 | [diff] [blame] | 36 | |
halcanary | 56586b6 | 2016-07-25 13:59:30 -0700 | [diff] [blame] | 37 | #include <cstdlib> |
| 38 | #include <cmath> |
| 39 | |
commit-bot@chromium.org | 608ea65 | 2013-10-03 19:29:21 +0000 | [diff] [blame] | 40 | #define DUMMY_TEXT "DCT compessed stream." |
| 41 | |
halcanary | f7a169e | 2015-05-01 07:35:45 -0700 | [diff] [blame] | 42 | template <typename T> |
halcanary | 530032a | 2016-08-18 14:22:52 -0700 | [diff] [blame] | 43 | static SkString emit_to_string(T& obj, SkPDFObjNumMap* catPtr = nullptr) { |
| 44 | SkPDFObjNumMap catalog; |
halcanary | 6a14434 | 2015-01-23 11:45:10 -0800 | [diff] [blame] | 45 | SkDynamicMemoryWStream buffer; |
halcanary | f7a169e | 2015-05-01 07:35:45 -0700 | [diff] [blame] | 46 | if (!catPtr) { |
| 47 | catPtr = &catalog; |
halcanary | 130444f | 2015-04-25 06:45:07 -0700 | [diff] [blame] | 48 | } |
halcanary | 530032a | 2016-08-18 14:22:52 -0700 | [diff] [blame] | 49 | obj.emitObject(&buffer, *catPtr); |
halcanary | 56586b6 | 2016-07-25 13:59:30 -0700 | [diff] [blame] | 50 | SkString tmp(buffer.bytesWritten()); |
| 51 | buffer.copyTo(tmp.writable_str()); |
halcanary | f7a169e | 2015-05-01 07:35:45 -0700 | [diff] [blame] | 52 | return tmp; |
vandebo@chromium.org | 8459d4e | 2010-09-24 22:25:30 +0000 | [diff] [blame] | 53 | } |
| 54 | |
halcanary | f7a169e | 2015-05-01 07:35:45 -0700 | [diff] [blame] | 55 | static bool eq(const SkString& str, const char* strPtr, size_t len) { |
| 56 | return len == str.size() && 0 == memcmp(str.c_str(), strPtr, len); |
vandebo@chromium.org | 421d644 | 2011-07-20 17:39:01 +0000 | [diff] [blame] | 57 | } |
| 58 | |
halcanary | 56586b6 | 2016-07-25 13:59:30 -0700 | [diff] [blame] | 59 | static void assert_eql(skiatest::Reporter* reporter, |
| 60 | const SkString& skString, |
| 61 | const char* str, |
| 62 | size_t len) { |
| 63 | if (!eq(skString, str, len)) { |
| 64 | REPORT_FAILURE(reporter, "", SkStringPrintf( |
| 65 | "'%*s' != '%s'", len, str, skString.c_str())); |
| 66 | } |
| 67 | } |
halcanary | f7a169e | 2015-05-01 07:35:45 -0700 | [diff] [blame] | 68 | |
halcanary | 56586b6 | 2016-07-25 13:59:30 -0700 | [diff] [blame] | 69 | static void assert_eq(skiatest::Reporter* reporter, |
| 70 | const SkString& skString, |
| 71 | const char* str) { |
| 72 | assert_eql(reporter, skString, str, strlen(str)); |
| 73 | } |
halcanary | f7a169e | 2015-05-01 07:35:45 -0700 | [diff] [blame] | 74 | |
| 75 | |
halcanary | 56586b6 | 2016-07-25 13:59:30 -0700 | [diff] [blame] | 76 | template <typename T> |
| 77 | static void assert_emit_eq(skiatest::Reporter* reporter, |
| 78 | T& object, |
| 79 | const char* string) { |
| 80 | SkString result = emit_to_string(object); |
| 81 | assert_eq(reporter, result, string); |
| 82 | } |
halcanary | f7a169e | 2015-05-01 07:35:45 -0700 | [diff] [blame] | 83 | |
vandebo@chromium.org | 421d644 | 2011-07-20 17:39:01 +0000 | [diff] [blame] | 84 | static void TestPDFStream(skiatest::Reporter* reporter) { |
| 85 | char streamBytes[] = "Test\nFoo\tBar"; |
halcanary | 022c2bd | 2016-09-02 11:29:46 -0700 | [diff] [blame] | 86 | auto streamData = skstd::make_unique<SkMemoryStream>( |
| 87 | streamBytes, strlen(streamBytes), true); |
halcanary | ac0e00d | 2016-07-27 11:12:23 -0700 | [diff] [blame] | 88 | auto stream = sk_make_sp<SkPDFStream>(std::move(streamData)); |
halcanary | 56586b6 | 2016-07-25 13:59:30 -0700 | [diff] [blame] | 89 | assert_emit_eq(reporter, |
halcanary | f7a169e | 2015-05-01 07:35:45 -0700 | [diff] [blame] | 90 | *stream, |
| 91 | "<</Length 12>> stream\nTest\nFoo\tBar\nendstream"); |
halcanary | fa25106 | 2016-07-29 10:13:18 -0700 | [diff] [blame] | 92 | stream->dict()->insertInt("Attribute", 42); |
halcanary | 56586b6 | 2016-07-25 13:59:30 -0700 | [diff] [blame] | 93 | assert_emit_eq(reporter, |
halcanary | f7a169e | 2015-05-01 07:35:45 -0700 | [diff] [blame] | 94 | *stream, |
| 95 | "<</Length 12\n/Attribute 42>> stream\n" |
| 96 | "Test\nFoo\tBar\nendstream"); |
vandebo@chromium.org | 421d644 | 2011-07-20 17:39:01 +0000 | [diff] [blame] | 97 | |
halcanary | 91d1d62 | 2015-02-17 14:43:06 -0800 | [diff] [blame] | 98 | { |
vandebo@chromium.org | 421d644 | 2011-07-20 17:39:01 +0000 | [diff] [blame] | 99 | char streamBytes2[] = "This is a longer string, so that compression " |
| 100 | "can do something with it. With shorter strings, " |
| 101 | "the short circuit logic cuts in and we end up " |
| 102 | "with an uncompressed string."; |
halcanary | fe8f0e0 | 2016-07-27 14:14:04 -0700 | [diff] [blame] | 103 | auto stream = sk_make_sp<SkPDFStream>( |
| 104 | SkData::MakeWithCopy(streamBytes2, strlen(streamBytes2))); |
vandebo@chromium.org | 421d644 | 2011-07-20 17:39:01 +0000 | [diff] [blame] | 105 | |
| 106 | SkDynamicMemoryWStream compressedByteStream; |
halcanary | 48305e8 | 2015-08-18 13:30:25 -0700 | [diff] [blame] | 107 | SkDeflateWStream deflateWStream(&compressedByteStream); |
| 108 | deflateWStream.write(streamBytes2, strlen(streamBytes2)); |
| 109 | deflateWStream.finalize(); |
vandebo@chromium.org | 421d644 | 2011-07-20 17:39:01 +0000 | [diff] [blame] | 110 | |
mtklein | cabc08c | 2015-02-19 08:29:24 -0800 | [diff] [blame] | 111 | SkDynamicMemoryWStream expected; |
halcanary | 37c46ca | 2015-03-31 12:30:20 -0700 | [diff] [blame] | 112 | expected.writeText("<</Filter /FlateDecode\n/Length 116>> stream\n"); |
halcanary | 48305e8 | 2015-08-18 13:30:25 -0700 | [diff] [blame] | 113 | compressedByteStream.writeToStream(&expected); |
| 114 | compressedByteStream.reset(); |
mtklein | cabc08c | 2015-02-19 08:29:24 -0800 | [diff] [blame] | 115 | expected.writeText("\nendstream"); |
reed | 42943c8 | 2016-09-12 12:01:44 -0700 | [diff] [blame] | 116 | sk_sp<SkData> expectedResultData2(expected.detachAsData()); |
halcanary | f7a169e | 2015-05-01 07:35:45 -0700 | [diff] [blame] | 117 | SkString result = emit_to_string(*stream); |
halcanary | 18e0cbc | 2016-08-03 14:17:53 -0700 | [diff] [blame] | 118 | #ifndef SK_PDF_LESS_COMPRESSION |
halcanary | 56586b6 | 2016-07-25 13:59:30 -0700 | [diff] [blame] | 119 | assert_eql(reporter, |
halcanary | f7a169e | 2015-05-01 07:35:45 -0700 | [diff] [blame] | 120 | result, |
| 121 | (const char*)expectedResultData2->data(), |
| 122 | expectedResultData2->size()); |
halcanary | 18e0cbc | 2016-08-03 14:17:53 -0700 | [diff] [blame] | 123 | #endif |
vandebo@chromium.org | 421d644 | 2011-07-20 17:39:01 +0000 | [diff] [blame] | 124 | } |
| 125 | } |
| 126 | |
halcanary | f7a169e | 2015-05-01 07:35:45 -0700 | [diff] [blame] | 127 | static void TestObjectNumberMap(skiatest::Reporter* reporter) { |
| 128 | SkPDFObjNumMap objNumMap; |
halcanary | 8103a34 | 2016-03-08 15:10:16 -0800 | [diff] [blame] | 129 | sk_sp<SkPDFArray> a1(new SkPDFArray); |
| 130 | sk_sp<SkPDFArray> a2(new SkPDFArray); |
| 131 | sk_sp<SkPDFArray> a3(new SkPDFArray); |
vandebo@chromium.org | 8459d4e | 2010-09-24 22:25:30 +0000 | [diff] [blame] | 132 | |
Hal Canary | c5b9498 | 2017-10-03 09:38:43 -0400 | [diff] [blame] | 133 | objNumMap.addObjectRecursively(a1.get()); |
| 134 | objNumMap.addObjectRecursively(a2.get()); |
| 135 | objNumMap.addObjectRecursively(a3.get()); |
vandebo@chromium.org | 8459d4e | 2010-09-24 22:25:30 +0000 | [diff] [blame] | 136 | |
halcanary | f7a169e | 2015-05-01 07:35:45 -0700 | [diff] [blame] | 137 | // The objects should be numbered in the order they are added, |
| 138 | // starting with 1. |
| 139 | REPORTER_ASSERT(reporter, objNumMap.getObjectNumber(a1.get()) == 1); |
| 140 | REPORTER_ASSERT(reporter, objNumMap.getObjectNumber(a2.get()) == 2); |
| 141 | REPORTER_ASSERT(reporter, objNumMap.getObjectNumber(a3.get()) == 3); |
| 142 | // Assert that repeated calls to get the object number return |
| 143 | // consistent result. |
| 144 | REPORTER_ASSERT(reporter, objNumMap.getObjectNumber(a1.get()) == 1); |
vandebo@chromium.org | 8459d4e | 2010-09-24 22:25:30 +0000 | [diff] [blame] | 145 | } |
| 146 | |
| 147 | static void TestObjectRef(skiatest::Reporter* reporter) { |
halcanary | 8103a34 | 2016-03-08 15:10:16 -0800 | [diff] [blame] | 148 | sk_sp<SkPDFArray> a1(new SkPDFArray); |
| 149 | sk_sp<SkPDFArray> a2(new SkPDFArray); |
| 150 | a2->appendObjRef(a1); |
vandebo@chromium.org | 8459d4e | 2010-09-24 22:25:30 +0000 | [diff] [blame] | 151 | |
halcanary | 530032a | 2016-08-18 14:22:52 -0700 | [diff] [blame] | 152 | SkPDFObjNumMap catalog; |
Hal Canary | c5b9498 | 2017-10-03 09:38:43 -0400 | [diff] [blame] | 153 | catalog.addObjectRecursively(a1.get()); |
halcanary | 530032a | 2016-08-18 14:22:52 -0700 | [diff] [blame] | 154 | REPORTER_ASSERT(reporter, catalog.getObjectNumber(a1.get()) == 1); |
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 | SkString result = emit_to_string(*a2, &catalog); |
| 157 | // If appendObjRef misbehaves, then the result would |
| 158 | // be [[]], not [1 0 R]. |
halcanary | 56586b6 | 2016-07-25 13:59:30 -0700 | [diff] [blame] | 159 | assert_eq(reporter, result, "[1 0 R]"); |
vandebo@chromium.org | 8459d4e | 2010-09-24 22:25:30 +0000 | [diff] [blame] | 160 | } |
| 161 | |
vandebo@chromium.org | 4e1cc6a | 2013-01-25 19:27:23 +0000 | [diff] [blame] | 162 | // This test used to assert without the fix submitted for |
| 163 | // http://code.google.com/p/skia/issues/detail?id=1083. |
| 164 | // SKP files might have invalid glyph ids. This test ensures they are ignored, |
| 165 | // and there is no assert on input data in Debug mode. |
sugoi@google.com | 54f0d1b | 2013-02-27 19:17:41 +0000 | [diff] [blame] | 166 | static void test_issue1083() { |
halcanary | 3d32d50 | 2015-03-01 06:55:20 -0800 | [diff] [blame] | 167 | SkDynamicMemoryWStream outStream; |
halcanary | 4b65666 | 2016-04-27 07:45:18 -0700 | [diff] [blame] | 168 | sk_sp<SkDocument> doc(SkDocument::MakePDF(&outStream)); |
halcanary | 3d32d50 | 2015-03-01 06:55:20 -0800 | [diff] [blame] | 169 | SkCanvas* canvas = doc->beginPage(100.0f, 100.0f); |
vandebo@chromium.org | 4e1cc6a | 2013-01-25 19:27:23 +0000 | [diff] [blame] | 170 | SkPaint paint; |
| 171 | paint.setTextEncoding(SkPaint::kGlyphID_TextEncoding); |
| 172 | |
| 173 | uint16_t glyphID = 65000; |
halcanary | 3d32d50 | 2015-03-01 06:55:20 -0800 | [diff] [blame] | 174 | canvas->drawText(&glyphID, 2, 0, 0, paint); |
vandebo@chromium.org | 4e1cc6a | 2013-01-25 19:27:23 +0000 | [diff] [blame] | 175 | |
halcanary | 3d32d50 | 2015-03-01 06:55:20 -0800 | [diff] [blame] | 176 | doc->close(); |
vandebo@chromium.org | 4e1cc6a | 2013-01-25 19:27:23 +0000 | [diff] [blame] | 177 | } |
| 178 | |
halcanary | 56586b6 | 2016-07-25 13:59:30 -0700 | [diff] [blame] | 179 | static void assert_emit_eq_number(skiatest::Reporter* reporter, float number) { |
| 180 | SkPDFUnion pdfUnion = SkPDFUnion::Scalar(number); |
| 181 | SkString result = emit_to_string(pdfUnion); |
| 182 | float value = static_cast<float>(std::atof(result.c_str())); |
| 183 | if (value != number) { |
| 184 | ERRORF(reporter, "%.9g != %s", number, result.c_str()); |
| 185 | } |
| 186 | } |
| 187 | |
| 188 | |
halcanary | f7a169e | 2015-05-01 07:35:45 -0700 | [diff] [blame] | 189 | static void TestPDFUnion(skiatest::Reporter* reporter) { |
| 190 | SkPDFUnion boolTrue = SkPDFUnion::Bool(true); |
halcanary | 56586b6 | 2016-07-25 13:59:30 -0700 | [diff] [blame] | 191 | assert_emit_eq(reporter, boolTrue, "true"); |
vandebo@chromium.org | 8459d4e | 2010-09-24 22:25:30 +0000 | [diff] [blame] | 192 | |
halcanary | f7a169e | 2015-05-01 07:35:45 -0700 | [diff] [blame] | 193 | SkPDFUnion boolFalse = SkPDFUnion::Bool(false); |
halcanary | 56586b6 | 2016-07-25 13:59:30 -0700 | [diff] [blame] | 194 | assert_emit_eq(reporter, boolFalse, "false"); |
vandebo@chromium.org | 8459d4e | 2010-09-24 22:25:30 +0000 | [diff] [blame] | 195 | |
halcanary | f7a169e | 2015-05-01 07:35:45 -0700 | [diff] [blame] | 196 | SkPDFUnion int42 = SkPDFUnion::Int(42); |
halcanary | 56586b6 | 2016-07-25 13:59:30 -0700 | [diff] [blame] | 197 | assert_emit_eq(reporter, int42, "42"); |
halcanary | f7a169e | 2015-05-01 07:35:45 -0700 | [diff] [blame] | 198 | |
halcanary | 56586b6 | 2016-07-25 13:59:30 -0700 | [diff] [blame] | 199 | assert_emit_eq_number(reporter, SK_ScalarHalf); |
| 200 | assert_emit_eq_number(reporter, 110999.75f); // bigScalar |
| 201 | assert_emit_eq_number(reporter, 50000000.1f); // biggerScalar |
| 202 | assert_emit_eq_number(reporter, 1.0f / 65536); // smallScalar |
vandebo@chromium.org | 094316b | 2011-03-04 03:15:13 +0000 | [diff] [blame] | 203 | |
halcanary | f7a169e | 2015-05-01 07:35:45 -0700 | [diff] [blame] | 204 | SkPDFUnion stringSimple = SkPDFUnion::String("test ) string ( foo"); |
halcanary | 56586b6 | 2016-07-25 13:59:30 -0700 | [diff] [blame] | 205 | assert_emit_eq(reporter, stringSimple, "(test \\) string \\( foo)"); |
vandebo@chromium.org | 8459d4e | 2010-09-24 22:25:30 +0000 | [diff] [blame] | 206 | |
halcanary | f7a169e | 2015-05-01 07:35:45 -0700 | [diff] [blame] | 207 | SkString stringComplexInput("\ttest ) string ( foo"); |
| 208 | SkPDFUnion stringComplex = SkPDFUnion::String(stringComplexInput); |
halcanary | 56586b6 | 2016-07-25 13:59:30 -0700 | [diff] [blame] | 209 | assert_emit_eq(reporter, stringComplex, "(\\011test \\) string \\( foo)"); |
halcanary | ee41b75 | 2016-06-23 14:08:11 -0700 | [diff] [blame] | 210 | |
| 211 | SkString binaryStringInput("\1\2\3\4\5\6\7\10\11\12\13\14\15\16\17\20"); |
| 212 | SkPDFUnion binaryString = SkPDFUnion::String(binaryStringInput); |
halcanary | 56586b6 | 2016-07-25 13:59:30 -0700 | [diff] [blame] | 213 | assert_emit_eq(reporter, binaryString, "<0102030405060708090A0B0C0D0E0F10>"); |
vandebo@chromium.org | 8459d4e | 2010-09-24 22:25:30 +0000 | [diff] [blame] | 214 | |
halcanary | f7a169e | 2015-05-01 07:35:45 -0700 | [diff] [blame] | 215 | SkString nameInput("Test name\twith#tab"); |
| 216 | SkPDFUnion name = SkPDFUnion::Name(nameInput); |
halcanary | 56586b6 | 2016-07-25 13:59:30 -0700 | [diff] [blame] | 217 | assert_emit_eq(reporter, name, "/Test#20name#09with#23tab"); |
halcanary | f7a169e | 2015-05-01 07:35:45 -0700 | [diff] [blame] | 218 | |
| 219 | SkString nameInput2("A#/%()<>[]{}B"); |
| 220 | SkPDFUnion name2 = SkPDFUnion::Name(nameInput2); |
halcanary | 56586b6 | 2016-07-25 13:59:30 -0700 | [diff] [blame] | 221 | 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] | 222 | |
| 223 | SkPDFUnion name3 = SkPDFUnion::Name("SimpleNameWithOnlyPrintableASCII"); |
halcanary | 56586b6 | 2016-07-25 13:59:30 -0700 | [diff] [blame] | 224 | assert_emit_eq(reporter, name3, "/SimpleNameWithOnlyPrintableASCII"); |
vandebo@chromium.org | 251a766 | 2012-09-21 17:50:50 +0000 | [diff] [blame] | 225 | |
vandebo@chromium.org | c0376fe | 2012-03-05 18:44:33 +0000 | [diff] [blame] | 226 | // Test that we correctly handle characters with the high-bit set. |
halcanary | f7a169e | 2015-05-01 07:35:45 -0700 | [diff] [blame] | 227 | SkString highBitString("\xDE\xAD" "be\xEF"); |
| 228 | SkPDFUnion highBitName = SkPDFUnion::Name(highBitString); |
halcanary | 56586b6 | 2016-07-25 13:59:30 -0700 | [diff] [blame] | 229 | assert_emit_eq(reporter, highBitName, "/#DE#ADbe#EF"); |
halcanary | f7a169e | 2015-05-01 07:35:45 -0700 | [diff] [blame] | 230 | } |
vandebo@chromium.org | c0376fe | 2012-03-05 18:44:33 +0000 | [diff] [blame] | 231 | |
halcanary | f7a169e | 2015-05-01 07:35:45 -0700 | [diff] [blame] | 232 | static void TestPDFArray(skiatest::Reporter* reporter) { |
halcanary | 8103a34 | 2016-03-08 15:10:16 -0800 | [diff] [blame] | 233 | sk_sp<SkPDFArray> array(new SkPDFArray); |
halcanary | 56586b6 | 2016-07-25 13:59:30 -0700 | [diff] [blame] | 234 | assert_emit_eq(reporter, *array, "[]"); |
vandebo@chromium.org | 8459d4e | 2010-09-24 22:25:30 +0000 | [diff] [blame] | 235 | |
halcanary | f7a169e | 2015-05-01 07:35:45 -0700 | [diff] [blame] | 236 | array->appendInt(42); |
halcanary | 56586b6 | 2016-07-25 13:59:30 -0700 | [diff] [blame] | 237 | assert_emit_eq(reporter, *array, "[42]"); |
halcanary | f7a169e | 2015-05-01 07:35:45 -0700 | [diff] [blame] | 238 | |
| 239 | array->appendScalar(SK_ScalarHalf); |
halcanary | 56586b6 | 2016-07-25 13:59:30 -0700 | [diff] [blame] | 240 | assert_emit_eq(reporter, *array, "[42 .5]"); |
halcanary | f7a169e | 2015-05-01 07:35:45 -0700 | [diff] [blame] | 241 | |
| 242 | array->appendInt(0); |
halcanary | 56586b6 | 2016-07-25 13:59:30 -0700 | [diff] [blame] | 243 | assert_emit_eq(reporter, *array, "[42 .5 0]"); |
halcanary | f7a169e | 2015-05-01 07:35:45 -0700 | [diff] [blame] | 244 | |
| 245 | array->appendBool(true); |
halcanary | 56586b6 | 2016-07-25 13:59:30 -0700 | [diff] [blame] | 246 | assert_emit_eq(reporter, *array, "[42 .5 0 true]"); |
halcanary | f7a169e | 2015-05-01 07:35:45 -0700 | [diff] [blame] | 247 | |
| 248 | array->appendName("ThisName"); |
halcanary | 56586b6 | 2016-07-25 13:59:30 -0700 | [diff] [blame] | 249 | assert_emit_eq(reporter, *array, "[42 .5 0 true /ThisName]"); |
halcanary | f7a169e | 2015-05-01 07:35:45 -0700 | [diff] [blame] | 250 | |
| 251 | array->appendName(SkString("AnotherName")); |
halcanary | 56586b6 | 2016-07-25 13:59:30 -0700 | [diff] [blame] | 252 | assert_emit_eq(reporter, *array, "[42 .5 0 true /ThisName /AnotherName]"); |
halcanary | f7a169e | 2015-05-01 07:35:45 -0700 | [diff] [blame] | 253 | |
| 254 | array->appendString("This String"); |
halcanary | 56586b6 | 2016-07-25 13:59:30 -0700 | [diff] [blame] | 255 | assert_emit_eq(reporter, *array, |
halcanary | 8e9f5e3 | 2016-02-24 15:46:46 -0800 | [diff] [blame] | 256 | "[42 .5 0 true /ThisName /AnotherName (This String)]"); |
halcanary | f7a169e | 2015-05-01 07:35:45 -0700 | [diff] [blame] | 257 | |
| 258 | array->appendString(SkString("Another String")); |
halcanary | 56586b6 | 2016-07-25 13:59:30 -0700 | [diff] [blame] | 259 | assert_emit_eq(reporter, *array, |
halcanary | 8e9f5e3 | 2016-02-24 15:46:46 -0800 | [diff] [blame] | 260 | "[42 .5 0 true /ThisName /AnotherName (This String) " |
halcanary | f7a169e | 2015-05-01 07:35:45 -0700 | [diff] [blame] | 261 | "(Another String)]"); |
| 262 | |
halcanary | 8103a34 | 2016-03-08 15:10:16 -0800 | [diff] [blame] | 263 | sk_sp<SkPDFArray> innerArray(new SkPDFArray); |
halcanary | f7a169e | 2015-05-01 07:35:45 -0700 | [diff] [blame] | 264 | innerArray->appendInt(-1); |
halcanary | 8103a34 | 2016-03-08 15:10:16 -0800 | [diff] [blame] | 265 | array->appendObject(std::move(innerArray)); |
halcanary | 56586b6 | 2016-07-25 13:59:30 -0700 | [diff] [blame] | 266 | assert_emit_eq(reporter, *array, |
halcanary | 8e9f5e3 | 2016-02-24 15:46:46 -0800 | [diff] [blame] | 267 | "[42 .5 0 true /ThisName /AnotherName (This String) " |
halcanary | f7a169e | 2015-05-01 07:35:45 -0700 | [diff] [blame] | 268 | "(Another String) [-1]]"); |
| 269 | |
halcanary | 8103a34 | 2016-03-08 15:10:16 -0800 | [diff] [blame] | 270 | sk_sp<SkPDFArray> referencedArray(new SkPDFArray); |
halcanary | 530032a | 2016-08-18 14:22:52 -0700 | [diff] [blame] | 271 | SkPDFObjNumMap catalog; |
Hal Canary | c5b9498 | 2017-10-03 09:38:43 -0400 | [diff] [blame] | 272 | catalog.addObjectRecursively(referencedArray.get()); |
halcanary | 530032a | 2016-08-18 14:22:52 -0700 | [diff] [blame] | 273 | REPORTER_ASSERT(reporter, catalog.getObjectNumber( |
halcanary | f7a169e | 2015-05-01 07:35:45 -0700 | [diff] [blame] | 274 | referencedArray.get()) == 1); |
halcanary | 8103a34 | 2016-03-08 15:10:16 -0800 | [diff] [blame] | 275 | array->appendObjRef(std::move(referencedArray)); |
halcanary | f7a169e | 2015-05-01 07:35:45 -0700 | [diff] [blame] | 276 | |
| 277 | SkString result = emit_to_string(*array, &catalog); |
halcanary | 56586b6 | 2016-07-25 13:59:30 -0700 | [diff] [blame] | 278 | assert_eq(reporter, result, |
halcanary | 8e9f5e3 | 2016-02-24 15:46:46 -0800 | [diff] [blame] | 279 | "[42 .5 0 true /ThisName /AnotherName (This String) " |
halcanary | f7a169e | 2015-05-01 07:35:45 -0700 | [diff] [blame] | 280 | "(Another String) [-1] 1 0 R]"); |
| 281 | } |
| 282 | |
| 283 | static void TestPDFDict(skiatest::Reporter* reporter) { |
halcanary | 8103a34 | 2016-03-08 15:10:16 -0800 | [diff] [blame] | 284 | sk_sp<SkPDFDict> dict(new SkPDFDict); |
halcanary | 56586b6 | 2016-07-25 13:59:30 -0700 | [diff] [blame] | 285 | assert_emit_eq(reporter, *dict, "<<>>"); |
halcanary | f7a169e | 2015-05-01 07:35:45 -0700 | [diff] [blame] | 286 | |
| 287 | dict->insertInt("n1", SkToSizeT(42)); |
halcanary | 56586b6 | 2016-07-25 13:59:30 -0700 | [diff] [blame] | 288 | assert_emit_eq(reporter, *dict, "<</n1 42>>"); |
halcanary | f7a169e | 2015-05-01 07:35:45 -0700 | [diff] [blame] | 289 | |
| 290 | dict.reset(new SkPDFDict); |
halcanary | 56586b6 | 2016-07-25 13:59:30 -0700 | [diff] [blame] | 291 | assert_emit_eq(reporter, *dict, "<<>>"); |
halcanary | f7a169e | 2015-05-01 07:35:45 -0700 | [diff] [blame] | 292 | |
| 293 | dict->insertInt("n1", 42); |
halcanary | 56586b6 | 2016-07-25 13:59:30 -0700 | [diff] [blame] | 294 | assert_emit_eq(reporter, *dict, "<</n1 42>>"); |
halcanary | f7a169e | 2015-05-01 07:35:45 -0700 | [diff] [blame] | 295 | |
| 296 | dict->insertScalar("n2", SK_ScalarHalf); |
| 297 | |
halcanary | 130444f | 2015-04-25 06:45:07 -0700 | [diff] [blame] | 298 | SkString n3("n3"); |
halcanary | 8103a34 | 2016-03-08 15:10:16 -0800 | [diff] [blame] | 299 | sk_sp<SkPDFArray> innerArray(new SkPDFArray); |
halcanary | f7a169e | 2015-05-01 07:35:45 -0700 | [diff] [blame] | 300 | innerArray->appendInt(-100); |
halcanary | 8103a34 | 2016-03-08 15:10:16 -0800 | [diff] [blame] | 301 | dict->insertObject(n3, std::move(innerArray)); |
halcanary | 56586b6 | 2016-07-25 13:59:30 -0700 | [diff] [blame] | 302 | 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] | 303 | |
halcanary | f7a169e | 2015-05-01 07:35:45 -0700 | [diff] [blame] | 304 | dict.reset(new SkPDFDict); |
halcanary | 56586b6 | 2016-07-25 13:59:30 -0700 | [diff] [blame] | 305 | assert_emit_eq(reporter, *dict, "<<>>"); |
halcanary | f7a169e | 2015-05-01 07:35:45 -0700 | [diff] [blame] | 306 | |
| 307 | dict->insertInt("n1", 24); |
halcanary | 56586b6 | 2016-07-25 13:59:30 -0700 | [diff] [blame] | 308 | assert_emit_eq(reporter, *dict, "<</n1 24>>"); |
halcanary | f7a169e | 2015-05-01 07:35:45 -0700 | [diff] [blame] | 309 | |
| 310 | dict->insertInt("n2", SkToSizeT(99)); |
halcanary | 56586b6 | 2016-07-25 13:59:30 -0700 | [diff] [blame] | 311 | assert_emit_eq(reporter, *dict, "<</n1 24\n/n2 99>>"); |
halcanary | f7a169e | 2015-05-01 07:35:45 -0700 | [diff] [blame] | 312 | |
| 313 | dict->insertScalar("n3", SK_ScalarHalf); |
halcanary | 56586b6 | 2016-07-25 13:59:30 -0700 | [diff] [blame] | 314 | assert_emit_eq(reporter, *dict, "<</n1 24\n/n2 99\n/n3 .5>>"); |
halcanary | f7a169e | 2015-05-01 07:35:45 -0700 | [diff] [blame] | 315 | |
| 316 | dict->insertName("n4", "AName"); |
halcanary | 56586b6 | 2016-07-25 13:59:30 -0700 | [diff] [blame] | 317 | 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] | 318 | |
| 319 | dict->insertName("n5", SkString("AnotherName")); |
halcanary | 56586b6 | 2016-07-25 13:59:30 -0700 | [diff] [blame] | 320 | 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] | 321 | "/n5 /AnotherName>>"); |
| 322 | |
| 323 | dict->insertString("n6", "A String"); |
halcanary | 56586b6 | 2016-07-25 13:59:30 -0700 | [diff] [blame] | 324 | 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] | 325 | "/n5 /AnotherName\n/n6 (A String)>>"); |
| 326 | |
| 327 | dict->insertString("n7", SkString("Another String")); |
halcanary | 56586b6 | 2016-07-25 13:59:30 -0700 | [diff] [blame] | 328 | 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] | 329 | "/n5 /AnotherName\n/n6 (A String)\n/n7 (Another String)>>"); |
| 330 | |
| 331 | dict.reset(new SkPDFDict("DType")); |
halcanary | 56586b6 | 2016-07-25 13:59:30 -0700 | [diff] [blame] | 332 | assert_emit_eq(reporter, *dict, "<</Type /DType>>"); |
halcanary | 8e9f5e3 | 2016-02-24 15:46:46 -0800 | [diff] [blame] | 333 | |
halcanary | 8103a34 | 2016-03-08 15:10:16 -0800 | [diff] [blame] | 334 | sk_sp<SkPDFArray> referencedArray(new SkPDFArray); |
halcanary | 530032a | 2016-08-18 14:22:52 -0700 | [diff] [blame] | 335 | SkPDFObjNumMap catalog; |
Hal Canary | c5b9498 | 2017-10-03 09:38:43 -0400 | [diff] [blame] | 336 | catalog.addObjectRecursively(referencedArray.get()); |
halcanary | 530032a | 2016-08-18 14:22:52 -0700 | [diff] [blame] | 337 | REPORTER_ASSERT(reporter, catalog.getObjectNumber( |
halcanary | f7a169e | 2015-05-01 07:35:45 -0700 | [diff] [blame] | 338 | referencedArray.get()) == 1); |
halcanary | 8103a34 | 2016-03-08 15:10:16 -0800 | [diff] [blame] | 339 | dict->insertObjRef("n1", std::move(referencedArray)); |
halcanary | f7a169e | 2015-05-01 07:35:45 -0700 | [diff] [blame] | 340 | SkString result = emit_to_string(*dict, &catalog); |
halcanary | 56586b6 | 2016-07-25 13:59:30 -0700 | [diff] [blame] | 341 | assert_eq(reporter, result, "<</Type /DType\n/n1 1 0 R>>"); |
halcanary | f7a169e | 2015-05-01 07:35:45 -0700 | [diff] [blame] | 342 | } |
| 343 | |
halcanary | 57f744e | 2016-09-09 11:41:59 -0700 | [diff] [blame] | 344 | DEF_TEST(SkPDF_Primitives, reporter) { |
halcanary | f7a169e | 2015-05-01 07:35:45 -0700 | [diff] [blame] | 345 | TestPDFUnion(reporter); |
| 346 | TestPDFArray(reporter); |
| 347 | TestPDFDict(reporter); |
vandebo@chromium.org | 421d644 | 2011-07-20 17:39:01 +0000 | [diff] [blame] | 348 | TestPDFStream(reporter); |
halcanary | f7a169e | 2015-05-01 07:35:45 -0700 | [diff] [blame] | 349 | TestObjectNumberMap(reporter); |
vandebo@chromium.org | 8459d4e | 2010-09-24 22:25:30 +0000 | [diff] [blame] | 350 | TestObjectRef(reporter); |
sugoi@google.com | 54f0d1b | 2013-02-27 19:17:41 +0000 | [diff] [blame] | 351 | test_issue1083(); |
vandebo@chromium.org | 8459d4e | 2010-09-24 22:25:30 +0000 | [diff] [blame] | 352 | } |
senorblanco | b0e89dc | 2014-10-20 14:03:12 -0700 | [diff] [blame] | 353 | |
| 354 | namespace { |
| 355 | |
| 356 | class DummyImageFilter : public SkImageFilter { |
| 357 | public: |
robertphillips | 43c2ad4 | 2016-04-04 05:05:11 -0700 | [diff] [blame] | 358 | static sk_sp<DummyImageFilter> Make(bool visited = false) { |
| 359 | return sk_sp<DummyImageFilter>(new DummyImageFilter(visited)); |
| 360 | } |
| 361 | |
robertphillips | 43c2ad4 | 2016-04-04 05:05:11 -0700 | [diff] [blame] | 362 | SK_DECLARE_PUBLIC_FLATTENABLE_DESERIALIZATION_PROCS(DummyImageFilter) |
| 363 | bool visited() const { return fVisited; } |
| 364 | |
| 365 | protected: |
robertphillips | 4ba94e2 | 2016-04-04 12:07:47 -0700 | [diff] [blame] | 366 | sk_sp<SkSpecialImage> onFilterImage(SkSpecialImage* source, const Context&, |
| 367 | SkIPoint* offset) const override { |
senorblanco | b0e89dc | 2014-10-20 14:03:12 -0700 | [diff] [blame] | 368 | fVisited = true; |
| 369 | offset->fX = offset->fY = 0; |
robertphillips | 4ba94e2 | 2016-04-04 12:07:47 -0700 | [diff] [blame] | 370 | return sk_ref_sp<SkSpecialImage>(source); |
senorblanco | b0e89dc | 2014-10-20 14:03:12 -0700 | [diff] [blame] | 371 | } |
Matt Sarett | 62745a8 | 2017-04-17 11:57:29 -0400 | [diff] [blame] | 372 | sk_sp<SkImageFilter> onMakeColorSpace(SkColorSpaceXformer*) const override { |
| 373 | return sk_ref_sp(const_cast<DummyImageFilter*>(this)); |
| 374 | } |
senorblanco | b0e89dc | 2014-10-20 14:03:12 -0700 | [diff] [blame] | 375 | |
| 376 | private: |
robertphillips | 43c2ad4 | 2016-04-04 05:05:11 -0700 | [diff] [blame] | 377 | DummyImageFilter(bool visited) : INHERITED(nullptr, 0, nullptr), fVisited(visited) {} |
| 378 | |
senorblanco | b0e89dc | 2014-10-20 14:03:12 -0700 | [diff] [blame] | 379 | mutable bool fVisited; |
robertphillips | 43c2ad4 | 2016-04-04 05:05:11 -0700 | [diff] [blame] | 380 | |
| 381 | typedef SkImageFilter INHERITED; |
senorblanco | b0e89dc | 2014-10-20 14:03:12 -0700 | [diff] [blame] | 382 | }; |
| 383 | |
reed | 60c9b58 | 2016-04-03 09:11:13 -0700 | [diff] [blame] | 384 | sk_sp<SkFlattenable> DummyImageFilter::CreateProc(SkReadBuffer& buffer) { |
senorblanco | b0e89dc | 2014-10-20 14:03:12 -0700 | [diff] [blame] | 385 | SK_IMAGEFILTER_UNFLATTEN_COMMON(common, 0); |
| 386 | bool visited = buffer.readBool(); |
robertphillips | 43c2ad4 | 2016-04-04 05:05:11 -0700 | [diff] [blame] | 387 | return DummyImageFilter::Make(visited); |
senorblanco | b0e89dc | 2014-10-20 14:03:12 -0700 | [diff] [blame] | 388 | } |
| 389 | |
| 390 | }; |
| 391 | |
| 392 | // Check that PDF rendering of image filters successfully falls back to |
| 393 | // CPU rasterization. |
halcanary | 57f744e | 2016-09-09 11:41:59 -0700 | [diff] [blame] | 394 | DEF_TEST(SkPDF_ImageFilter, reporter) { |
| 395 | REQUIRE_PDF_DOCUMENT(SkPDF_ImageFilter, reporter); |
halcanary | 3d32d50 | 2015-03-01 06:55:20 -0800 | [diff] [blame] | 396 | SkDynamicMemoryWStream stream; |
halcanary | 4b65666 | 2016-04-27 07:45:18 -0700 | [diff] [blame] | 397 | sk_sp<SkDocument> doc(SkDocument::MakePDF(&stream)); |
halcanary | 3d32d50 | 2015-03-01 06:55:20 -0800 | [diff] [blame] | 398 | SkCanvas* canvas = doc->beginPage(100.0f, 100.0f); |
| 399 | |
robertphillips | 43c2ad4 | 2016-04-04 05:05:11 -0700 | [diff] [blame] | 400 | sk_sp<DummyImageFilter> filter(DummyImageFilter::Make()); |
senorblanco | b0e89dc | 2014-10-20 14:03:12 -0700 | [diff] [blame] | 401 | |
| 402 | // Filter just created; should be unvisited. |
| 403 | REPORTER_ASSERT(reporter, !filter->visited()); |
| 404 | SkPaint paint; |
Mike Reed | 5e25717 | 2016-11-01 11:22:05 -0400 | [diff] [blame] | 405 | paint.setImageFilter(filter); |
halcanary | 3d32d50 | 2015-03-01 06:55:20 -0800 | [diff] [blame] | 406 | canvas->drawRect(SkRect::MakeWH(100, 100), paint); |
| 407 | doc->close(); |
senorblanco | b0e89dc | 2014-10-20 14:03:12 -0700 | [diff] [blame] | 408 | |
| 409 | // Filter was used in rendering; should be visited. |
| 410 | REPORTER_ASSERT(reporter, filter->visited()); |
| 411 | } |
halcanary | 66a82f3 | 2015-10-12 13:05:04 -0700 | [diff] [blame] | 412 | |
| 413 | // Check that PDF rendering of image filters successfully falls back to |
| 414 | // CPU rasterization. |
halcanary | 57f744e | 2016-09-09 11:41:59 -0700 | [diff] [blame] | 415 | DEF_TEST(SkPDF_FontCanEmbedTypeface, reporter) { |
halcanary | 66a82f3 | 2015-10-12 13:05:04 -0700 | [diff] [blame] | 416 | SkPDFCanon canon; |
| 417 | |
| 418 | const char resource[] = "fonts/Roboto2-Regular_NoEmbed.ttf"; |
bungeman | 13b9c95 | 2016-05-12 10:09:30 -0700 | [diff] [blame] | 419 | sk_sp<SkTypeface> noEmbedTypeface(MakeResourceAsTypeface(resource)); |
halcanary | 66a82f3 | 2015-10-12 13:05:04 -0700 | [diff] [blame] | 420 | if (noEmbedTypeface) { |
| 421 | REPORTER_ASSERT(reporter, |
halcanary | 8103a34 | 2016-03-08 15:10:16 -0800 | [diff] [blame] | 422 | !SkPDFFont::CanEmbedTypeface(noEmbedTypeface.get(), &canon)); |
halcanary | 66a82f3 | 2015-10-12 13:05:04 -0700 | [diff] [blame] | 423 | } |
halcanary | 8103a34 | 2016-03-08 15:10:16 -0800 | [diff] [blame] | 424 | sk_sp<SkTypeface> portableTypeface( |
Ben Wagner | a93a14a | 2017-08-28 10:34:05 -0400 | [diff] [blame] | 425 | sk_tool_utils::create_portable_typeface(nullptr, SkFontStyle())); |
halcanary | 66a82f3 | 2015-10-12 13:05:04 -0700 | [diff] [blame] | 426 | REPORTER_ASSERT(reporter, |
halcanary | 8103a34 | 2016-03-08 15:10:16 -0800 | [diff] [blame] | 427 | SkPDFFont::CanEmbedTypeface(portableTypeface.get(), &canon)); |
halcanary | 66a82f3 | 2015-10-12 13:05:04 -0700 | [diff] [blame] | 428 | } |
halcanary | 8e9f5e3 | 2016-02-24 15:46:46 -0800 | [diff] [blame] | 429 | |
| 430 | |
| 431 | // test to see that all finite scalars round trip via scanf(). |
| 432 | static void check_pdf_scalar_serialization( |
| 433 | skiatest::Reporter* reporter, float inputFloat) { |
Hal Canary | 3c36ef6 | 2018-01-02 16:25:53 -0500 | [diff] [blame] | 434 | char floatString[kMaximumSkFloatToDecimalLength]; |
| 435 | size_t len = SkFloatToDecimal(inputFloat, floatString); |
halcanary | 8e9f5e3 | 2016-02-24 15:46:46 -0800 | [diff] [blame] | 436 | if (len >= sizeof(floatString)) { |
| 437 | ERRORF(reporter, "string too long: %u", (unsigned)len); |
| 438 | return; |
| 439 | } |
| 440 | if (floatString[len] != '\0' || strlen(floatString) != len) { |
| 441 | ERRORF(reporter, "terminator misplaced."); |
| 442 | return; // The terminator is needed for sscanf(). |
| 443 | } |
| 444 | if (reporter->verbose()) { |
| 445 | SkDebugf("%15.9g = \"%s\"\n", inputFloat, floatString); |
| 446 | } |
| 447 | float roundTripFloat; |
| 448 | if (1 != sscanf(floatString, "%f", &roundTripFloat)) { |
| 449 | ERRORF(reporter, "unscannable result: %s", floatString); |
| 450 | return; |
| 451 | } |
halcanary | 56586b6 | 2016-07-25 13:59:30 -0700 | [diff] [blame] | 452 | if (std::isfinite(inputFloat) && roundTripFloat != inputFloat) { |
halcanary | 8e9f5e3 | 2016-02-24 15:46:46 -0800 | [diff] [blame] | 453 | ERRORF(reporter, "roundTripFloat (%.9g) != inputFloat (%.9g)", |
| 454 | roundTripFloat, inputFloat); |
| 455 | } |
| 456 | } |
| 457 | |
| 458 | // Test SkPDFUtils::AppendScalar for accuracy. |
halcanary | 57f744e | 2016-09-09 11:41:59 -0700 | [diff] [blame] | 459 | DEF_TEST(SkPDF_Primitives_Scalar, reporter) { |
halcanary | 8e9f5e3 | 2016-02-24 15:46:46 -0800 | [diff] [blame] | 460 | SkRandom random(0x5EED); |
| 461 | int iterationCount = 512; |
| 462 | while (iterationCount-- > 0) { |
| 463 | union { uint32_t u; float f; }; |
| 464 | u = random.nextU(); |
| 465 | static_assert(sizeof(float) == sizeof(uint32_t), ""); |
| 466 | check_pdf_scalar_serialization(reporter, f); |
| 467 | } |
| 468 | float alwaysCheck[] = { |
| 469 | 0.0f, -0.0f, 1.0f, -1.0f, SK_ScalarPI, 0.1f, FLT_MIN, FLT_MAX, |
| 470 | -FLT_MIN, -FLT_MAX, FLT_MIN / 16.0f, -FLT_MIN / 16.0f, |
| 471 | SK_FloatNaN, SK_FloatInfinity, SK_FloatNegativeInfinity, |
| 472 | -FLT_MIN / 8388608.0 |
| 473 | }; |
| 474 | for (float inputFloat: alwaysCheck) { |
| 475 | check_pdf_scalar_serialization(reporter, inputFloat); |
| 476 | } |
| 477 | } |
halcanary | eb92cb3 | 2016-07-15 13:41:27 -0700 | [diff] [blame] | 478 | |
| 479 | // Test SkPDFUtils:: for accuracy. |
halcanary | 57f744e | 2016-09-09 11:41:59 -0700 | [diff] [blame] | 480 | DEF_TEST(SkPDF_Primitives_Color, reporter) { |
halcanary | eb92cb3 | 2016-07-15 13:41:27 -0700 | [diff] [blame] | 481 | char buffer[5]; |
| 482 | for (int i = 0; i < 256; ++i) { |
| 483 | size_t len = SkPDFUtils::ColorToDecimal(i, buffer); |
| 484 | REPORTER_ASSERT(reporter, len == strlen(buffer)); |
| 485 | float f; |
| 486 | REPORTER_ASSERT(reporter, 1 == sscanf(buffer, "%f", &f)); |
| 487 | int roundTrip = (int)(0.5 + f * 255); |
| 488 | REPORTER_ASSERT(reporter, roundTrip == i); |
| 489 | } |
| 490 | } |
Hal Canary | 1521c8a | 2018-03-28 09:51:00 -0700 | [diff] [blame] | 491 | |
Herb Derby | 81ecdbb | 2018-07-20 11:48:38 -0400 | [diff] [blame] | 492 | static SkGlyphRun make_run(size_t len, const SkGlyphID* glyphs, SkPoint* pos, |
Hal Canary | 98caedd | 2018-07-23 10:50:49 -0400 | [diff] [blame] | 493 | SkPaint paint, const uint32_t* clusters, |
| 494 | size_t utf8TextByteLength, const char* utf8Text) { |
| 495 | return SkGlyphRun(std::move(paint), |
| 496 | SkSpan<const uint16_t>{}, // No dense indices for now. |
Herb Derby | b935cf8 | 2018-07-26 16:54:18 -0400 | [diff] [blame] | 497 | SkSpan<const SkPoint>{pos, len}, |
Hal Canary | 98caedd | 2018-07-23 10:50:49 -0400 | [diff] [blame] | 498 | SkSpan<const SkGlyphID>{glyphs, len}, |
| 499 | SkSpan<const SkGlyphID>{}, |
| 500 | SkSpan<const char>{utf8Text, utf8TextByteLength}, |
| 501 | SkSpan<const uint32_t>{clusters, len}); |
| 502 | } |
| 503 | |
Hal Canary | 1521c8a | 2018-03-28 09:51:00 -0700 | [diff] [blame] | 504 | DEF_TEST(SkPDF_Clusterator, reporter) { |
Hal Canary | 1521c8a | 2018-03-28 09:51:00 -0700 | [diff] [blame] | 505 | SkPaint paint; |
| 506 | paint.setTextEncoding(SkPaint::kGlyphID_TextEncoding); |
Hal Canary | e9e19dd | 2018-04-02 11:36:39 -0400 | [diff] [blame] | 507 | { |
Hal Canary | 98caedd | 2018-07-23 10:50:49 -0400 | [diff] [blame] | 508 | constexpr unsigned len = 11; |
| 509 | const uint32_t clusters[len] = { 3, 2, 2, 1, 0, 4, 4, 7, 6, 6, 5 }; |
| 510 | 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] | 511 | 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] | 512 | {0, 0}, {0, 0}, {0, 0}, {0, 0}, {0, 0}}; |
Hal Canary | e9e19dd | 2018-04-02 11:36:39 -0400 | [diff] [blame] | 513 | const char text[] = "abcdefgh"; |
Hal Canary | 98caedd | 2018-07-23 10:50:49 -0400 | [diff] [blame] | 514 | SkGlyphRun run = make_run(len, glyphs, pos, paint, clusters, strlen(text), text); |
| 515 | SkClusterator clusterator(run); |
Hal Canary | e9e19dd | 2018-04-02 11:36:39 -0400 | [diff] [blame] | 516 | SkClusterator::Cluster expectations[] = { |
| 517 | {&text[3], 1, 0, 1}, |
| 518 | {&text[2], 1, 1, 2}, |
| 519 | {&text[1], 1, 3, 1}, |
| 520 | {&text[0], 1, 4, 1}, |
| 521 | {&text[4], 1, 5, 2}, |
| 522 | {&text[7], 1, 7, 1}, |
| 523 | {&text[6], 1, 8, 2}, |
| 524 | {&text[5], 1, 10, 1}, |
| 525 | {nullptr, 0, 0, 0}, |
| 526 | }; |
| 527 | for (const auto& expectation : expectations) { |
| 528 | REPORTER_ASSERT(reporter, clusterator.next() == expectation); |
| 529 | } |
| 530 | } |
| 531 | { |
Hal Canary | 98caedd | 2018-07-23 10:50:49 -0400 | [diff] [blame] | 532 | constexpr unsigned len = 5; |
| 533 | const uint32_t clusters[len] = { 0, 1, 4, 5, 6 }; |
| 534 | const SkGlyphID glyphs[len] = { 43, 167, 79, 79, 82, }; |
Herb Derby | 81ecdbb | 2018-07-20 11:48:38 -0400 | [diff] [blame] | 535 | 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] | 536 | const char text[] = "Ha\xCC\x8A" "llo"; |
Hal Canary | 98caedd | 2018-07-23 10:50:49 -0400 | [diff] [blame] | 537 | SkGlyphRun run = make_run(len, glyphs, pos, paint, clusters, strlen(text), text); |
| 538 | SkClusterator clusterator(run); |
Hal Canary | e9e19dd | 2018-04-02 11:36:39 -0400 | [diff] [blame] | 539 | SkClusterator::Cluster expectations[] = { |
| 540 | {&text[0], 1, 0, 1}, |
| 541 | {&text[1], 3, 1, 1}, |
| 542 | {&text[4], 1, 2, 1}, |
| 543 | {&text[5], 1, 3, 1}, |
| 544 | {&text[6], 1, 4, 1}, |
| 545 | {nullptr, 0, 0, 0}, |
| 546 | }; |
| 547 | for (const auto& expectation : expectations) { |
| 548 | REPORTER_ASSERT(reporter, clusterator.next() == expectation); |
| 549 | } |
Hal Canary | 1521c8a | 2018-03-28 09:51:00 -0700 | [diff] [blame] | 550 | } |
| 551 | } |
| 552 | |
Hal Canary | 43fb7a0 | 2016-12-30 13:09:03 -0500 | [diff] [blame] | 553 | #endif |