OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2010 The Android Open Source Project | 2 * Copyright 2010 The Android Open Source Project |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
6 */ | 6 */ |
7 | 7 |
8 #include "Resources.h" | 8 #include "Resources.h" |
9 #include "SkBitmap.h" | 9 #include "SkBitmap.h" |
10 #include "SkCanvas.h" | 10 #include "SkCanvas.h" |
11 #include "SkData.h" | 11 #include "SkData.h" |
12 #include "SkDocument.h" | 12 #include "SkDocument.h" |
13 #include "SkDeflate.h" | 13 #include "SkDeflate.h" |
14 #include "SkImageEncoder.h" | 14 #include "SkImageEncoder.h" |
15 #include "SkMatrix.h" | 15 #include "SkMatrix.h" |
16 #include "SkPDFCanon.h" | 16 #include "SkPDFCanon.h" |
17 #include "SkPDFDevice.h" | 17 #include "SkPDFDevice.h" |
18 #include "SkPDFFont.h" | 18 #include "SkPDFFont.h" |
19 #include "SkPDFStream.h" | 19 #include "SkPDFStream.h" |
20 #include "SkPDFTypes.h" | 20 #include "SkPDFTypes.h" |
21 #include "SkPDFUtils.h" | 21 #include "SkPDFUtils.h" |
22 #include "SkReadBuffer.h" | 22 #include "SkReadBuffer.h" |
23 #include "SkScalar.h" | 23 #include "SkScalar.h" |
24 #include "SkSpecialImage.h" | 24 #include "SkSpecialImage.h" |
25 #include "SkStream.h" | 25 #include "SkStream.h" |
26 #include "SkTypes.h" | 26 #include "SkTypes.h" |
27 #include "Test.h" | 27 #include "Test.h" |
28 #include "sk_tool_utils.h" | 28 #include "sk_tool_utils.h" |
29 | 29 |
| 30 #include <cstdlib> |
| 31 #include <cmath> |
| 32 |
30 #define DUMMY_TEXT "DCT compessed stream." | 33 #define DUMMY_TEXT "DCT compessed stream." |
31 | 34 |
32 namespace { | 35 namespace { |
33 struct Catalog { | 36 struct Catalog { |
34 SkPDFSubstituteMap substitutes; | 37 SkPDFSubstituteMap substitutes; |
35 SkPDFObjNumMap numbers; | 38 SkPDFObjNumMap numbers; |
36 }; | 39 }; |
37 } // namespace | 40 } // namespace |
38 | 41 |
39 template <typename T> | 42 template <typename T> |
40 static SkString emit_to_string(T& obj, Catalog* catPtr = nullptr) { | 43 static SkString emit_to_string(T& obj, Catalog* catPtr = nullptr) { |
41 Catalog catalog; | 44 Catalog catalog; |
42 SkDynamicMemoryWStream buffer; | 45 SkDynamicMemoryWStream buffer; |
43 if (!catPtr) { | 46 if (!catPtr) { |
44 catPtr = &catalog; | 47 catPtr = &catalog; |
45 } | 48 } |
46 obj.emitObject(&buffer, catPtr->numbers, catPtr->substitutes); | 49 obj.emitObject(&buffer, catPtr->numbers, catPtr->substitutes); |
47 SkAutoTDelete<SkStreamAsset> asset(buffer.detachAsStream()); | 50 SkString tmp(buffer.bytesWritten()); |
48 SkString tmp(asset->getLength()); | 51 buffer.copyTo(tmp.writable_str()); |
49 asset->read(tmp.writable_str(), asset->getLength()); | |
50 return tmp; | 52 return tmp; |
51 } | 53 } |
52 | 54 |
53 static bool eq(const SkString& str, const char* strPtr, size_t len) { | 55 static bool eq(const SkString& str, const char* strPtr, size_t len) { |
54 return len == str.size() && 0 == memcmp(str.c_str(), strPtr, len); | 56 return len == str.size() && 0 == memcmp(str.c_str(), strPtr, len); |
55 } | 57 } |
56 | 58 |
57 #define ASSERT_EQL(REPORTER, SKSTRING, STRING, LEN) \ | 59 static void assert_eql(skiatest::Reporter* reporter, |
58 do { \ | 60 const SkString& skString, |
59 const char* strptr = STRING; \ | 61 const char* str, |
60 const SkString& sks = SKSTRING; \ | 62 size_t len) { |
61 if (!eq(sks, strptr, LEN)) { \ | 63 if (!eq(skString, str, len)) { |
62 REPORT_FAILURE( \ | 64 REPORT_FAILURE(reporter, "", SkStringPrintf( |
63 REPORTER, \ | 65 "'%*s' != '%s'", len, str, skString.c_str())); |
64 "", \ | 66 } |
65 SkStringPrintf("'%s' != '%s'", strptr, sks.c_str())); \ | 67 } |
66 } \ | |
67 } while (false) | |
68 | 68 |
69 #define ASSERT_EQ(REPORTER, SKSTRING, STRING) \ | 69 static void assert_eq(skiatest::Reporter* reporter, |
70 do { \ | 70 const SkString& skString, |
71 const char* str = STRING; \ | 71 const char* str) { |
72 ASSERT_EQL(REPORTER, SKSTRING, str, strlen(str)); \ | 72 assert_eql(reporter, skString, str, strlen(str)); |
73 } while (false) | 73 } |
74 | |
75 #define ASSERT_EMIT_EQ(REPORTER, OBJECT, STRING) \ | |
76 do { \ | |
77 SkString result = emit_to_string(OBJECT); \ | |
78 ASSERT_EQ(REPORTER, result, STRING); \ | |
79 } while (false) | |
80 | 74 |
81 | 75 |
| 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 } |
82 | 83 |
83 static void TestPDFStream(skiatest::Reporter* reporter) { | 84 static void TestPDFStream(skiatest::Reporter* reporter) { |
84 char streamBytes[] = "Test\nFoo\tBar"; | 85 char streamBytes[] = "Test\nFoo\tBar"; |
85 SkAutoTDelete<SkMemoryStream> streamData(new SkMemoryStream( | 86 SkAutoTDelete<SkMemoryStream> streamData(new SkMemoryStream( |
86 streamBytes, strlen(streamBytes), true)); | 87 streamBytes, strlen(streamBytes), true)); |
87 sk_sp<SkPDFStream> stream(new SkPDFStream(streamData.get())); | 88 sk_sp<SkPDFStream> stream(new SkPDFStream(streamData.get())); |
88 ASSERT_EMIT_EQ(reporter, | 89 assert_emit_eq(reporter, |
89 *stream, | 90 *stream, |
90 "<</Length 12>> stream\nTest\nFoo\tBar\nendstream"); | 91 "<</Length 12>> stream\nTest\nFoo\tBar\nendstream"); |
91 stream->insertInt("Attribute", 42); | 92 stream->insertInt("Attribute", 42); |
92 ASSERT_EMIT_EQ(reporter, | 93 assert_emit_eq(reporter, |
93 *stream, | 94 *stream, |
94 "<</Length 12\n/Attribute 42>> stream\n" | 95 "<</Length 12\n/Attribute 42>> stream\n" |
95 "Test\nFoo\tBar\nendstream"); | 96 "Test\nFoo\tBar\nendstream"); |
96 | 97 |
97 { | 98 { |
98 char streamBytes2[] = "This is a longer string, so that compression " | 99 char streamBytes2[] = "This is a longer string, so that compression " |
99 "can do something with it. With shorter strings, " | 100 "can do something with it. With shorter strings, " |
100 "the short circuit logic cuts in and we end up " | 101 "the short circuit logic cuts in and we end up " |
101 "with an uncompressed string."; | 102 "with an uncompressed string."; |
102 SkAutoDataUnref streamData2(SkData::NewWithCopy(streamBytes2, | 103 SkAutoDataUnref streamData2(SkData::NewWithCopy(streamBytes2, |
103 strlen(streamBytes2))); | 104 strlen(streamBytes2))); |
104 sk_sp<SkPDFStream> stream(new SkPDFStream(streamData2.get())); | 105 sk_sp<SkPDFStream> stream(new SkPDFStream(streamData2.get())); |
105 | 106 |
106 SkDynamicMemoryWStream compressedByteStream; | 107 SkDynamicMemoryWStream compressedByteStream; |
107 SkDeflateWStream deflateWStream(&compressedByteStream); | 108 SkDeflateWStream deflateWStream(&compressedByteStream); |
108 deflateWStream.write(streamBytes2, strlen(streamBytes2)); | 109 deflateWStream.write(streamBytes2, strlen(streamBytes2)); |
109 deflateWStream.finalize(); | 110 deflateWStream.finalize(); |
110 | 111 |
111 SkDynamicMemoryWStream expected; | 112 SkDynamicMemoryWStream expected; |
112 expected.writeText("<</Filter /FlateDecode\n/Length 116>> stream\n"); | 113 expected.writeText("<</Filter /FlateDecode\n/Length 116>> stream\n"); |
113 compressedByteStream.writeToStream(&expected); | 114 compressedByteStream.writeToStream(&expected); |
114 compressedByteStream.reset(); | 115 compressedByteStream.reset(); |
115 expected.writeText("\nendstream"); | 116 expected.writeText("\nendstream"); |
116 SkAutoDataUnref expectedResultData2(expected.copyToData()); | 117 SkAutoDataUnref expectedResultData2(expected.copyToData()); |
117 SkString result = emit_to_string(*stream); | 118 SkString result = emit_to_string(*stream); |
118 ASSERT_EQL(reporter, | 119 assert_eql(reporter, |
119 result, | 120 result, |
120 (const char*)expectedResultData2->data(), | 121 (const char*)expectedResultData2->data(), |
121 expectedResultData2->size()); | 122 expectedResultData2->size()); |
122 } | 123 } |
123 } | 124 } |
124 | 125 |
125 static void TestObjectNumberMap(skiatest::Reporter* reporter) { | 126 static void TestObjectNumberMap(skiatest::Reporter* reporter) { |
126 SkPDFObjNumMap objNumMap; | 127 SkPDFObjNumMap objNumMap; |
127 sk_sp<SkPDFArray> a1(new SkPDFArray); | 128 sk_sp<SkPDFArray> a1(new SkPDFArray); |
128 sk_sp<SkPDFArray> a2(new SkPDFArray); | 129 sk_sp<SkPDFArray> a2(new SkPDFArray); |
(...skipping 18 matching lines...) Expand all Loading... |
147 sk_sp<SkPDFArray> a2(new SkPDFArray); | 148 sk_sp<SkPDFArray> a2(new SkPDFArray); |
148 a2->appendObjRef(a1); | 149 a2->appendObjRef(a1); |
149 | 150 |
150 Catalog catalog; | 151 Catalog catalog; |
151 catalog.numbers.addObject(a1.get()); | 152 catalog.numbers.addObject(a1.get()); |
152 REPORTER_ASSERT(reporter, catalog.numbers.getObjectNumber(a1.get()) == 1); | 153 REPORTER_ASSERT(reporter, catalog.numbers.getObjectNumber(a1.get()) == 1); |
153 | 154 |
154 SkString result = emit_to_string(*a2, &catalog); | 155 SkString result = emit_to_string(*a2, &catalog); |
155 // If appendObjRef misbehaves, then the result would | 156 // If appendObjRef misbehaves, then the result would |
156 // be [[]], not [1 0 R]. | 157 // be [[]], not [1 0 R]. |
157 ASSERT_EQ(reporter, result, "[1 0 R]"); | 158 assert_eq(reporter, result, "[1 0 R]"); |
158 } | 159 } |
159 | 160 |
160 static void TestSubstitute(skiatest::Reporter* reporter) { | 161 static void TestSubstitute(skiatest::Reporter* reporter) { |
161 sk_sp<SkPDFDict> proxy(new SkPDFDict()); | 162 sk_sp<SkPDFDict> proxy(new SkPDFDict()); |
162 sk_sp<SkPDFDict> stub(new SkPDFDict()); | 163 sk_sp<SkPDFDict> stub(new SkPDFDict()); |
163 | 164 |
164 proxy->insertInt("Value", 33); | 165 proxy->insertInt("Value", 33); |
165 stub->insertInt("Value", 44); | 166 stub->insertInt("Value", 44); |
166 | 167 |
167 SkPDFSubstituteMap substituteMap; | 168 SkPDFSubstituteMap substituteMap; |
(...skipping 15 matching lines...) Expand all Loading... |
183 SkCanvas* canvas = doc->beginPage(100.0f, 100.0f); | 184 SkCanvas* canvas = doc->beginPage(100.0f, 100.0f); |
184 SkPaint paint; | 185 SkPaint paint; |
185 paint.setTextEncoding(SkPaint::kGlyphID_TextEncoding); | 186 paint.setTextEncoding(SkPaint::kGlyphID_TextEncoding); |
186 | 187 |
187 uint16_t glyphID = 65000; | 188 uint16_t glyphID = 65000; |
188 canvas->drawText(&glyphID, 2, 0, 0, paint); | 189 canvas->drawText(&glyphID, 2, 0, 0, paint); |
189 | 190 |
190 doc->close(); | 191 doc->close(); |
191 } | 192 } |
192 | 193 |
| 194 static void assert_emit_eq_number(skiatest::Reporter* reporter, float number) { |
| 195 SkPDFUnion pdfUnion = SkPDFUnion::Scalar(number); |
| 196 SkString result = emit_to_string(pdfUnion); |
| 197 float value = static_cast<float>(std::atof(result.c_str())); |
| 198 if (value != number) { |
| 199 ERRORF(reporter, "%.9g != %s", number, result.c_str()); |
| 200 } |
| 201 } |
| 202 |
| 203 |
193 static void TestPDFUnion(skiatest::Reporter* reporter) { | 204 static void TestPDFUnion(skiatest::Reporter* reporter) { |
194 SkPDFUnion boolTrue = SkPDFUnion::Bool(true); | 205 SkPDFUnion boolTrue = SkPDFUnion::Bool(true); |
195 ASSERT_EMIT_EQ(reporter, boolTrue, "true"); | 206 assert_emit_eq(reporter, boolTrue, "true"); |
196 | 207 |
197 SkPDFUnion boolFalse = SkPDFUnion::Bool(false); | 208 SkPDFUnion boolFalse = SkPDFUnion::Bool(false); |
198 ASSERT_EMIT_EQ(reporter, boolFalse, "false"); | 209 assert_emit_eq(reporter, boolFalse, "false"); |
199 | 210 |
200 SkPDFUnion int42 = SkPDFUnion::Int(42); | 211 SkPDFUnion int42 = SkPDFUnion::Int(42); |
201 ASSERT_EMIT_EQ(reporter, int42, "42"); | 212 assert_emit_eq(reporter, int42, "42"); |
202 | 213 |
203 SkPDFUnion realHalf = SkPDFUnion::Scalar(SK_ScalarHalf); | 214 assert_emit_eq_number(reporter, SK_ScalarHalf); |
204 ASSERT_EMIT_EQ(reporter, realHalf, ".5"); | 215 assert_emit_eq_number(reporter, 110999.75f); // bigScalar |
205 | 216 assert_emit_eq_number(reporter, 50000000.1f); // biggerScalar |
206 SkPDFUnion bigScalar = SkPDFUnion::Scalar(110999.75f); | 217 assert_emit_eq_number(reporter, 1.0f / 65536); // smallScalar |
207 ASSERT_EMIT_EQ(reporter, bigScalar, "110999.75"); | |
208 | |
209 SkPDFUnion biggerScalar = SkPDFUnion::Scalar(50000000.1f); | |
210 ASSERT_EMIT_EQ(reporter, biggerScalar, "50000000"); | |
211 | |
212 SkPDFUnion smallestScalar = SkPDFUnion::Scalar(1.0f / 65536); | |
213 ASSERT_EMIT_EQ(reporter, smallestScalar, ".0000152587890"); | |
214 | 218 |
215 SkPDFUnion stringSimple = SkPDFUnion::String("test ) string ( foo"); | 219 SkPDFUnion stringSimple = SkPDFUnion::String("test ) string ( foo"); |
216 ASSERT_EMIT_EQ(reporter, stringSimple, "(test \\) string \\( foo)"); | 220 assert_emit_eq(reporter, stringSimple, "(test \\) string \\( foo)"); |
217 | 221 |
218 SkString stringComplexInput("\ttest ) string ( foo"); | 222 SkString stringComplexInput("\ttest ) string ( foo"); |
219 SkPDFUnion stringComplex = SkPDFUnion::String(stringComplexInput); | 223 SkPDFUnion stringComplex = SkPDFUnion::String(stringComplexInput); |
220 ASSERT_EMIT_EQ(reporter, stringComplex, "(\\011test \\) string \\( foo)"); | 224 assert_emit_eq(reporter, stringComplex, "(\\011test \\) string \\( foo)"); |
221 | 225 |
222 SkString binaryStringInput("\1\2\3\4\5\6\7\10\11\12\13\14\15\16\17\20"); | 226 SkString binaryStringInput("\1\2\3\4\5\6\7\10\11\12\13\14\15\16\17\20"); |
223 SkPDFUnion binaryString = SkPDFUnion::String(binaryStringInput); | 227 SkPDFUnion binaryString = SkPDFUnion::String(binaryStringInput); |
224 ASSERT_EMIT_EQ(reporter, binaryString, "<0102030405060708090A0B0C0D0E0F10>")
; | 228 assert_emit_eq(reporter, binaryString, "<0102030405060708090A0B0C0D0E0F10>")
; |
225 | 229 |
226 SkString nameInput("Test name\twith#tab"); | 230 SkString nameInput("Test name\twith#tab"); |
227 SkPDFUnion name = SkPDFUnion::Name(nameInput); | 231 SkPDFUnion name = SkPDFUnion::Name(nameInput); |
228 ASSERT_EMIT_EQ(reporter, name, "/Test#20name#09with#23tab"); | 232 assert_emit_eq(reporter, name, "/Test#20name#09with#23tab"); |
229 | 233 |
230 SkString nameInput2("A#/%()<>[]{}B"); | 234 SkString nameInput2("A#/%()<>[]{}B"); |
231 SkPDFUnion name2 = SkPDFUnion::Name(nameInput2); | 235 SkPDFUnion name2 = SkPDFUnion::Name(nameInput2); |
232 ASSERT_EMIT_EQ(reporter, name2, "/A#23#2F#25#28#29#3C#3E#5B#5D#7B#7DB"); | 236 assert_emit_eq(reporter, name2, "/A#23#2F#25#28#29#3C#3E#5B#5D#7B#7DB"); |
233 | 237 |
234 SkPDFUnion name3 = SkPDFUnion::Name("SimpleNameWithOnlyPrintableASCII"); | 238 SkPDFUnion name3 = SkPDFUnion::Name("SimpleNameWithOnlyPrintableASCII"); |
235 ASSERT_EMIT_EQ(reporter, name3, "/SimpleNameWithOnlyPrintableASCII"); | 239 assert_emit_eq(reporter, name3, "/SimpleNameWithOnlyPrintableASCII"); |
236 | 240 |
237 // Test that we correctly handle characters with the high-bit set. | 241 // Test that we correctly handle characters with the high-bit set. |
238 SkString highBitString("\xDE\xAD" "be\xEF"); | 242 SkString highBitString("\xDE\xAD" "be\xEF"); |
239 SkPDFUnion highBitName = SkPDFUnion::Name(highBitString); | 243 SkPDFUnion highBitName = SkPDFUnion::Name(highBitString); |
240 ASSERT_EMIT_EQ(reporter, highBitName, "/#DE#ADbe#EF"); | 244 assert_emit_eq(reporter, highBitName, "/#DE#ADbe#EF"); |
241 } | 245 } |
242 | 246 |
243 static void TestPDFArray(skiatest::Reporter* reporter) { | 247 static void TestPDFArray(skiatest::Reporter* reporter) { |
244 sk_sp<SkPDFArray> array(new SkPDFArray); | 248 sk_sp<SkPDFArray> array(new SkPDFArray); |
245 ASSERT_EMIT_EQ(reporter, *array, "[]"); | 249 assert_emit_eq(reporter, *array, "[]"); |
246 | 250 |
247 array->appendInt(42); | 251 array->appendInt(42); |
248 ASSERT_EMIT_EQ(reporter, *array, "[42]"); | 252 assert_emit_eq(reporter, *array, "[42]"); |
249 | 253 |
250 array->appendScalar(SK_ScalarHalf); | 254 array->appendScalar(SK_ScalarHalf); |
251 ASSERT_EMIT_EQ(reporter, *array, "[42 .5]"); | 255 assert_emit_eq(reporter, *array, "[42 .5]"); |
252 | 256 |
253 array->appendInt(0); | 257 array->appendInt(0); |
254 ASSERT_EMIT_EQ(reporter, *array, "[42 .5 0]"); | 258 assert_emit_eq(reporter, *array, "[42 .5 0]"); |
255 | 259 |
256 array->appendBool(true); | 260 array->appendBool(true); |
257 ASSERT_EMIT_EQ(reporter, *array, "[42 .5 0 true]"); | 261 assert_emit_eq(reporter, *array, "[42 .5 0 true]"); |
258 | 262 |
259 array->appendName("ThisName"); | 263 array->appendName("ThisName"); |
260 ASSERT_EMIT_EQ(reporter, *array, "[42 .5 0 true /ThisName]"); | 264 assert_emit_eq(reporter, *array, "[42 .5 0 true /ThisName]"); |
261 | 265 |
262 array->appendName(SkString("AnotherName")); | 266 array->appendName(SkString("AnotherName")); |
263 ASSERT_EMIT_EQ(reporter, *array, "[42 .5 0 true /ThisName /AnotherName]"); | 267 assert_emit_eq(reporter, *array, "[42 .5 0 true /ThisName /AnotherName]"); |
264 | 268 |
265 array->appendString("This String"); | 269 array->appendString("This String"); |
266 ASSERT_EMIT_EQ(reporter, *array, | 270 assert_emit_eq(reporter, *array, |
267 "[42 .5 0 true /ThisName /AnotherName (This String)]"); | 271 "[42 .5 0 true /ThisName /AnotherName (This String)]"); |
268 | 272 |
269 array->appendString(SkString("Another String")); | 273 array->appendString(SkString("Another String")); |
270 ASSERT_EMIT_EQ(reporter, *array, | 274 assert_emit_eq(reporter, *array, |
271 "[42 .5 0 true /ThisName /AnotherName (This String) " | 275 "[42 .5 0 true /ThisName /AnotherName (This String) " |
272 "(Another String)]"); | 276 "(Another String)]"); |
273 | 277 |
274 sk_sp<SkPDFArray> innerArray(new SkPDFArray); | 278 sk_sp<SkPDFArray> innerArray(new SkPDFArray); |
275 innerArray->appendInt(-1); | 279 innerArray->appendInt(-1); |
276 array->appendObject(std::move(innerArray)); | 280 array->appendObject(std::move(innerArray)); |
277 ASSERT_EMIT_EQ(reporter, *array, | 281 assert_emit_eq(reporter, *array, |
278 "[42 .5 0 true /ThisName /AnotherName (This String) " | 282 "[42 .5 0 true /ThisName /AnotherName (This String) " |
279 "(Another String) [-1]]"); | 283 "(Another String) [-1]]"); |
280 | 284 |
281 sk_sp<SkPDFArray> referencedArray(new SkPDFArray); | 285 sk_sp<SkPDFArray> referencedArray(new SkPDFArray); |
282 Catalog catalog; | 286 Catalog catalog; |
283 catalog.numbers.addObject(referencedArray.get()); | 287 catalog.numbers.addObject(referencedArray.get()); |
284 REPORTER_ASSERT(reporter, catalog.numbers.getObjectNumber( | 288 REPORTER_ASSERT(reporter, catalog.numbers.getObjectNumber( |
285 referencedArray.get()) == 1); | 289 referencedArray.get()) == 1); |
286 array->appendObjRef(std::move(referencedArray)); | 290 array->appendObjRef(std::move(referencedArray)); |
287 | 291 |
288 SkString result = emit_to_string(*array, &catalog); | 292 SkString result = emit_to_string(*array, &catalog); |
289 ASSERT_EQ(reporter, result, | 293 assert_eq(reporter, result, |
290 "[42 .5 0 true /ThisName /AnotherName (This String) " | 294 "[42 .5 0 true /ThisName /AnotherName (This String) " |
291 "(Another String) [-1] 1 0 R]"); | 295 "(Another String) [-1] 1 0 R]"); |
292 } | 296 } |
293 | 297 |
294 static void TestPDFDict(skiatest::Reporter* reporter) { | 298 static void TestPDFDict(skiatest::Reporter* reporter) { |
295 sk_sp<SkPDFDict> dict(new SkPDFDict); | 299 sk_sp<SkPDFDict> dict(new SkPDFDict); |
296 ASSERT_EMIT_EQ(reporter, *dict, "<<>>"); | 300 assert_emit_eq(reporter, *dict, "<<>>"); |
297 | 301 |
298 dict->insertInt("n1", SkToSizeT(42)); | 302 dict->insertInt("n1", SkToSizeT(42)); |
299 ASSERT_EMIT_EQ(reporter, *dict, "<</n1 42>>"); | 303 assert_emit_eq(reporter, *dict, "<</n1 42>>"); |
300 | 304 |
301 dict.reset(new SkPDFDict); | 305 dict.reset(new SkPDFDict); |
302 ASSERT_EMIT_EQ(reporter, *dict, "<<>>"); | 306 assert_emit_eq(reporter, *dict, "<<>>"); |
303 | 307 |
304 dict->insertInt("n1", 42); | 308 dict->insertInt("n1", 42); |
305 ASSERT_EMIT_EQ(reporter, *dict, "<</n1 42>>"); | 309 assert_emit_eq(reporter, *dict, "<</n1 42>>"); |
306 | 310 |
307 dict->insertScalar("n2", SK_ScalarHalf); | 311 dict->insertScalar("n2", SK_ScalarHalf); |
308 | 312 |
309 SkString n3("n3"); | 313 SkString n3("n3"); |
310 sk_sp<SkPDFArray> innerArray(new SkPDFArray); | 314 sk_sp<SkPDFArray> innerArray(new SkPDFArray); |
311 innerArray->appendInt(-100); | 315 innerArray->appendInt(-100); |
312 dict->insertObject(n3, std::move(innerArray)); | 316 dict->insertObject(n3, std::move(innerArray)); |
313 ASSERT_EMIT_EQ(reporter, *dict, "<</n1 42\n/n2 .5\n/n3 [-100]>>"); | 317 assert_emit_eq(reporter, *dict, "<</n1 42\n/n2 .5\n/n3 [-100]>>"); |
314 | 318 |
315 dict.reset(new SkPDFDict); | 319 dict.reset(new SkPDFDict); |
316 ASSERT_EMIT_EQ(reporter, *dict, "<<>>"); | 320 assert_emit_eq(reporter, *dict, "<<>>"); |
317 | 321 |
318 dict->insertInt("n1", 24); | 322 dict->insertInt("n1", 24); |
319 ASSERT_EMIT_EQ(reporter, *dict, "<</n1 24>>"); | 323 assert_emit_eq(reporter, *dict, "<</n1 24>>"); |
320 | 324 |
321 dict->insertInt("n2", SkToSizeT(99)); | 325 dict->insertInt("n2", SkToSizeT(99)); |
322 ASSERT_EMIT_EQ(reporter, *dict, "<</n1 24\n/n2 99>>"); | 326 assert_emit_eq(reporter, *dict, "<</n1 24\n/n2 99>>"); |
323 | 327 |
324 dict->insertScalar("n3", SK_ScalarHalf); | 328 dict->insertScalar("n3", SK_ScalarHalf); |
325 ASSERT_EMIT_EQ(reporter, *dict, "<</n1 24\n/n2 99\n/n3 .5>>"); | 329 assert_emit_eq(reporter, *dict, "<</n1 24\n/n2 99\n/n3 .5>>"); |
326 | 330 |
327 dict->insertName("n4", "AName"); | 331 dict->insertName("n4", "AName"); |
328 ASSERT_EMIT_EQ(reporter, *dict, "<</n1 24\n/n2 99\n/n3 .5\n/n4 /AName>>"); | 332 assert_emit_eq(reporter, *dict, "<</n1 24\n/n2 99\n/n3 .5\n/n4 /AName>>"); |
329 | 333 |
330 dict->insertName("n5", SkString("AnotherName")); | 334 dict->insertName("n5", SkString("AnotherName")); |
331 ASSERT_EMIT_EQ(reporter, *dict, "<</n1 24\n/n2 99\n/n3 .5\n/n4 /AName\n" | 335 assert_emit_eq(reporter, *dict, "<</n1 24\n/n2 99\n/n3 .5\n/n4 /AName\n" |
332 "/n5 /AnotherName>>"); | 336 "/n5 /AnotherName>>"); |
333 | 337 |
334 dict->insertString("n6", "A String"); | 338 dict->insertString("n6", "A String"); |
335 ASSERT_EMIT_EQ(reporter, *dict, "<</n1 24\n/n2 99\n/n3 .5\n/n4 /AName\n" | 339 assert_emit_eq(reporter, *dict, "<</n1 24\n/n2 99\n/n3 .5\n/n4 /AName\n" |
336 "/n5 /AnotherName\n/n6 (A String)>>"); | 340 "/n5 /AnotherName\n/n6 (A String)>>"); |
337 | 341 |
338 dict->insertString("n7", SkString("Another String")); | 342 dict->insertString("n7", SkString("Another String")); |
339 ASSERT_EMIT_EQ(reporter, *dict, "<</n1 24\n/n2 99\n/n3 .5\n/n4 /AName\n" | 343 assert_emit_eq(reporter, *dict, "<</n1 24\n/n2 99\n/n3 .5\n/n4 /AName\n" |
340 "/n5 /AnotherName\n/n6 (A String)\n/n7 (Another String)>>"); | 344 "/n5 /AnotherName\n/n6 (A String)\n/n7 (Another String)>>"); |
341 | 345 |
342 dict.reset(new SkPDFDict("DType")); | 346 dict.reset(new SkPDFDict("DType")); |
343 ASSERT_EMIT_EQ(reporter, *dict, "<</Type /DType>>"); | 347 assert_emit_eq(reporter, *dict, "<</Type /DType>>"); |
344 | 348 |
345 sk_sp<SkPDFArray> referencedArray(new SkPDFArray); | 349 sk_sp<SkPDFArray> referencedArray(new SkPDFArray); |
346 Catalog catalog; | 350 Catalog catalog; |
347 catalog.numbers.addObject(referencedArray.get()); | 351 catalog.numbers.addObject(referencedArray.get()); |
348 REPORTER_ASSERT(reporter, catalog.numbers.getObjectNumber( | 352 REPORTER_ASSERT(reporter, catalog.numbers.getObjectNumber( |
349 referencedArray.get()) == 1); | 353 referencedArray.get()) == 1); |
350 dict->insertObjRef("n1", std::move(referencedArray)); | 354 dict->insertObjRef("n1", std::move(referencedArray)); |
351 SkString result = emit_to_string(*dict, &catalog); | 355 SkString result = emit_to_string(*dict, &catalog); |
352 ASSERT_EQ(reporter, result, "<</Type /DType\n/n1 1 0 R>>"); | 356 assert_eq(reporter, result, "<</Type /DType\n/n1 1 0 R>>"); |
353 } | 357 } |
354 | 358 |
355 DEF_TEST(PDFPrimitives, reporter) { | 359 DEF_TEST(PDFPrimitives, reporter) { |
356 TestPDFUnion(reporter); | 360 TestPDFUnion(reporter); |
357 TestPDFArray(reporter); | 361 TestPDFArray(reporter); |
358 TestPDFDict(reporter); | 362 TestPDFDict(reporter); |
359 TestPDFStream(reporter); | 363 TestPDFStream(reporter); |
360 TestObjectNumberMap(reporter); | 364 TestObjectNumberMap(reporter); |
361 TestObjectRef(reporter); | 365 TestObjectRef(reporter); |
362 TestSubstitute(reporter); | 366 TestSubstitute(reporter); |
(...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
458 return; // The terminator is needed for sscanf(). | 462 return; // The terminator is needed for sscanf(). |
459 } | 463 } |
460 if (reporter->verbose()) { | 464 if (reporter->verbose()) { |
461 SkDebugf("%15.9g = \"%s\"\n", inputFloat, floatString); | 465 SkDebugf("%15.9g = \"%s\"\n", inputFloat, floatString); |
462 } | 466 } |
463 float roundTripFloat; | 467 float roundTripFloat; |
464 if (1 != sscanf(floatString, "%f", &roundTripFloat)) { | 468 if (1 != sscanf(floatString, "%f", &roundTripFloat)) { |
465 ERRORF(reporter, "unscannable result: %s", floatString); | 469 ERRORF(reporter, "unscannable result: %s", floatString); |
466 return; | 470 return; |
467 } | 471 } |
468 if (isfinite(inputFloat) && roundTripFloat != inputFloat) { | 472 if (std::isfinite(inputFloat) && roundTripFloat != inputFloat) { |
469 ERRORF(reporter, "roundTripFloat (%.9g) != inputFloat (%.9g)", | 473 ERRORF(reporter, "roundTripFloat (%.9g) != inputFloat (%.9g)", |
470 roundTripFloat, inputFloat); | 474 roundTripFloat, inputFloat); |
471 } | 475 } |
472 } | 476 } |
473 | 477 |
474 // Test SkPDFUtils::AppendScalar for accuracy. | 478 // Test SkPDFUtils::AppendScalar for accuracy. |
475 DEF_TEST(PDFPrimitives_Scalar, reporter) { | 479 DEF_TEST(PDFPrimitives_Scalar, reporter) { |
476 SkRandom random(0x5EED); | 480 SkRandom random(0x5EED); |
477 int iterationCount = 512; | 481 int iterationCount = 512; |
478 while (iterationCount-- > 0) { | 482 while (iterationCount-- > 0) { |
(...skipping 18 matching lines...) Expand all Loading... |
497 char buffer[5]; | 501 char buffer[5]; |
498 for (int i = 0; i < 256; ++i) { | 502 for (int i = 0; i < 256; ++i) { |
499 size_t len = SkPDFUtils::ColorToDecimal(i, buffer); | 503 size_t len = SkPDFUtils::ColorToDecimal(i, buffer); |
500 REPORTER_ASSERT(reporter, len == strlen(buffer)); | 504 REPORTER_ASSERT(reporter, len == strlen(buffer)); |
501 float f; | 505 float f; |
502 REPORTER_ASSERT(reporter, 1 == sscanf(buffer, "%f", &f)); | 506 REPORTER_ASSERT(reporter, 1 == sscanf(buffer, "%f", &f)); |
503 int roundTrip = (int)(0.5 + f * 255); | 507 int roundTrip = (int)(0.5 + f * 255); |
504 REPORTER_ASSERT(reporter, roundTrip == i); | 508 REPORTER_ASSERT(reporter, roundTrip == i); |
505 } | 509 } |
506 } | 510 } |
OLD | NEW |