Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1)

Side by Side Diff: tests/PDFPrimitivesTest.cpp

Issue 2253283004: SkPDF: in-place font subsetting (Closed) Base URL: https://skia.googlesource.com/skia.git@SkPdfCacheMetrics
Patch Set: 2016-08-18 (Thursday) 16:02:16 EDT Created 4 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « tests/PDFGlyphsToUnicodeTest.cpp ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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"
(...skipping 13 matching lines...) Expand all
24 #include "SkStream.h" 24 #include "SkStream.h"
25 #include "SkTypes.h" 25 #include "SkTypes.h"
26 #include "Test.h" 26 #include "Test.h"
27 #include "sk_tool_utils.h" 27 #include "sk_tool_utils.h"
28 28
29 #include <cstdlib> 29 #include <cstdlib>
30 #include <cmath> 30 #include <cmath>
31 31
32 #define DUMMY_TEXT "DCT compessed stream." 32 #define DUMMY_TEXT "DCT compessed stream."
33 33
34 namespace {
35 struct Catalog {
36 SkPDFSubstituteMap substitutes;
37 SkPDFObjNumMap numbers;
38 };
39 } // namespace
40
41 template <typename T> 34 template <typename T>
42 static SkString emit_to_string(T& obj, Catalog* catPtr = nullptr) { 35 static SkString emit_to_string(T& obj, SkPDFObjNumMap* catPtr = nullptr) {
43 Catalog catalog; 36 SkPDFObjNumMap catalog;
44 SkDynamicMemoryWStream buffer; 37 SkDynamicMemoryWStream buffer;
45 if (!catPtr) { 38 if (!catPtr) {
46 catPtr = &catalog; 39 catPtr = &catalog;
47 } 40 }
48 obj.emitObject(&buffer, catPtr->numbers, catPtr->substitutes); 41 obj.emitObject(&buffer, *catPtr);
49 SkString tmp(buffer.bytesWritten()); 42 SkString tmp(buffer.bytesWritten());
50 buffer.copyTo(tmp.writable_str()); 43 buffer.copyTo(tmp.writable_str());
51 return tmp; 44 return tmp;
52 } 45 }
53 46
54 static bool eq(const SkString& str, const char* strPtr, size_t len) { 47 static bool eq(const SkString& str, const char* strPtr, size_t len) {
55 return len == str.size() && 0 == memcmp(str.c_str(), strPtr, len); 48 return len == str.size() && 0 == memcmp(str.c_str(), strPtr, len);
56 } 49 }
57 50
58 static void assert_eql(skiatest::Reporter* reporter, 51 static void assert_eql(skiatest::Reporter* reporter,
(...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after
141 // Assert that repeated calls to get the object number return 134 // Assert that repeated calls to get the object number return
142 // consistent result. 135 // consistent result.
143 REPORTER_ASSERT(reporter, objNumMap.getObjectNumber(a1.get()) == 1); 136 REPORTER_ASSERT(reporter, objNumMap.getObjectNumber(a1.get()) == 1);
144 } 137 }
145 138
146 static void TestObjectRef(skiatest::Reporter* reporter) { 139 static void TestObjectRef(skiatest::Reporter* reporter) {
147 sk_sp<SkPDFArray> a1(new SkPDFArray); 140 sk_sp<SkPDFArray> a1(new SkPDFArray);
148 sk_sp<SkPDFArray> a2(new SkPDFArray); 141 sk_sp<SkPDFArray> a2(new SkPDFArray);
149 a2->appendObjRef(a1); 142 a2->appendObjRef(a1);
150 143
151 Catalog catalog; 144 SkPDFObjNumMap catalog;
152 catalog.numbers.addObject(a1.get()); 145 catalog.addObject(a1.get());
153 REPORTER_ASSERT(reporter, catalog.numbers.getObjectNumber(a1.get()) == 1); 146 REPORTER_ASSERT(reporter, catalog.getObjectNumber(a1.get()) == 1);
154 147
155 SkString result = emit_to_string(*a2, &catalog); 148 SkString result = emit_to_string(*a2, &catalog);
156 // If appendObjRef misbehaves, then the result would 149 // If appendObjRef misbehaves, then the result would
157 // be [[]], not [1 0 R]. 150 // be [[]], not [1 0 R].
158 assert_eq(reporter, result, "[1 0 R]"); 151 assert_eq(reporter, result, "[1 0 R]");
159 } 152 }
160 153
161 static void TestSubstitute(skiatest::Reporter* reporter) {
162 sk_sp<SkPDFDict> proxy(new SkPDFDict());
163 sk_sp<SkPDFDict> stub(new SkPDFDict());
164
165 proxy->insertInt("Value", 33);
166 stub->insertInt("Value", 44);
167
168 SkPDFSubstituteMap substituteMap;
169 substituteMap.setSubstitute(proxy.get(), stub.get());
170 SkPDFObjNumMap catalog;
171 catalog.addObject(proxy.get());
172
173 REPORTER_ASSERT(reporter, stub.get() == substituteMap.getSubstitute(proxy.ge t()));
174 REPORTER_ASSERT(reporter, proxy.get() != substituteMap.getSubstitute(stub.ge t()));
175 }
176
177 // This test used to assert without the fix submitted for 154 // This test used to assert without the fix submitted for
178 // http://code.google.com/p/skia/issues/detail?id=1083. 155 // http://code.google.com/p/skia/issues/detail?id=1083.
179 // SKP files might have invalid glyph ids. This test ensures they are ignored, 156 // SKP files might have invalid glyph ids. This test ensures they are ignored,
180 // and there is no assert on input data in Debug mode. 157 // and there is no assert on input data in Debug mode.
181 static void test_issue1083() { 158 static void test_issue1083() {
182 SkDynamicMemoryWStream outStream; 159 SkDynamicMemoryWStream outStream;
183 sk_sp<SkDocument> doc(SkDocument::MakePDF(&outStream)); 160 sk_sp<SkDocument> doc(SkDocument::MakePDF(&outStream));
184 SkCanvas* canvas = doc->beginPage(100.0f, 100.0f); 161 SkCanvas* canvas = doc->beginPage(100.0f, 100.0f);
185 SkPaint paint; 162 SkPaint paint;
186 paint.setTextEncoding(SkPaint::kGlyphID_TextEncoding); 163 paint.setTextEncoding(SkPaint::kGlyphID_TextEncoding);
(...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after
276 "(Another String)]"); 253 "(Another String)]");
277 254
278 sk_sp<SkPDFArray> innerArray(new SkPDFArray); 255 sk_sp<SkPDFArray> innerArray(new SkPDFArray);
279 innerArray->appendInt(-1); 256 innerArray->appendInt(-1);
280 array->appendObject(std::move(innerArray)); 257 array->appendObject(std::move(innerArray));
281 assert_emit_eq(reporter, *array, 258 assert_emit_eq(reporter, *array,
282 "[42 .5 0 true /ThisName /AnotherName (This String) " 259 "[42 .5 0 true /ThisName /AnotherName (This String) "
283 "(Another String) [-1]]"); 260 "(Another String) [-1]]");
284 261
285 sk_sp<SkPDFArray> referencedArray(new SkPDFArray); 262 sk_sp<SkPDFArray> referencedArray(new SkPDFArray);
286 Catalog catalog; 263 SkPDFObjNumMap catalog;
287 catalog.numbers.addObject(referencedArray.get()); 264 catalog.addObject(referencedArray.get());
288 REPORTER_ASSERT(reporter, catalog.numbers.getObjectNumber( 265 REPORTER_ASSERT(reporter, catalog.getObjectNumber(
289 referencedArray.get()) == 1); 266 referencedArray.get()) == 1);
290 array->appendObjRef(std::move(referencedArray)); 267 array->appendObjRef(std::move(referencedArray));
291 268
292 SkString result = emit_to_string(*array, &catalog); 269 SkString result = emit_to_string(*array, &catalog);
293 assert_eq(reporter, result, 270 assert_eq(reporter, result,
294 "[42 .5 0 true /ThisName /AnotherName (This String) " 271 "[42 .5 0 true /ThisName /AnotherName (This String) "
295 "(Another String) [-1] 1 0 R]"); 272 "(Another String) [-1] 1 0 R]");
296 } 273 }
297 274
298 static void TestPDFDict(skiatest::Reporter* reporter) { 275 static void TestPDFDict(skiatest::Reporter* reporter) {
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after
340 "/n5 /AnotherName\n/n6 (A String)>>"); 317 "/n5 /AnotherName\n/n6 (A String)>>");
341 318
342 dict->insertString("n7", SkString("Another String")); 319 dict->insertString("n7", SkString("Another String"));
343 assert_emit_eq(reporter, *dict, "<</n1 24\n/n2 99\n/n3 .5\n/n4 /AName\n" 320 assert_emit_eq(reporter, *dict, "<</n1 24\n/n2 99\n/n3 .5\n/n4 /AName\n"
344 "/n5 /AnotherName\n/n6 (A String)\n/n7 (Another String)>>"); 321 "/n5 /AnotherName\n/n6 (A String)\n/n7 (Another String)>>");
345 322
346 dict.reset(new SkPDFDict("DType")); 323 dict.reset(new SkPDFDict("DType"));
347 assert_emit_eq(reporter, *dict, "<</Type /DType>>"); 324 assert_emit_eq(reporter, *dict, "<</Type /DType>>");
348 325
349 sk_sp<SkPDFArray> referencedArray(new SkPDFArray); 326 sk_sp<SkPDFArray> referencedArray(new SkPDFArray);
350 Catalog catalog; 327 SkPDFObjNumMap catalog;
351 catalog.numbers.addObject(referencedArray.get()); 328 catalog.addObject(referencedArray.get());
352 REPORTER_ASSERT(reporter, catalog.numbers.getObjectNumber( 329 REPORTER_ASSERT(reporter, catalog.getObjectNumber(
353 referencedArray.get()) == 1); 330 referencedArray.get()) == 1);
354 dict->insertObjRef("n1", std::move(referencedArray)); 331 dict->insertObjRef("n1", std::move(referencedArray));
355 SkString result = emit_to_string(*dict, &catalog); 332 SkString result = emit_to_string(*dict, &catalog);
356 assert_eq(reporter, result, "<</Type /DType\n/n1 1 0 R>>"); 333 assert_eq(reporter, result, "<</Type /DType\n/n1 1 0 R>>");
357 } 334 }
358 335
359 DEF_TEST(PDFPrimitives, reporter) { 336 DEF_TEST(PDFPrimitives, reporter) {
360 TestPDFUnion(reporter); 337 TestPDFUnion(reporter);
361 TestPDFArray(reporter); 338 TestPDFArray(reporter);
362 TestPDFDict(reporter); 339 TestPDFDict(reporter);
363 TestPDFStream(reporter); 340 TestPDFStream(reporter);
364 TestObjectNumberMap(reporter); 341 TestObjectNumberMap(reporter);
365 TestObjectRef(reporter); 342 TestObjectRef(reporter);
366 TestSubstitute(reporter);
367 test_issue1083(); 343 test_issue1083();
368 } 344 }
369 345
370 namespace { 346 namespace {
371 347
372 class DummyImageFilter : public SkImageFilter { 348 class DummyImageFilter : public SkImageFilter {
373 public: 349 public:
374 static sk_sp<DummyImageFilter> Make(bool visited = false) { 350 static sk_sp<DummyImageFilter> Make(bool visited = false) {
375 return sk_sp<DummyImageFilter>(new DummyImageFilter(visited)); 351 return sk_sp<DummyImageFilter>(new DummyImageFilter(visited));
376 } 352 }
(...skipping 124 matching lines...) Expand 10 before | Expand all | Expand 10 after
501 char buffer[5]; 477 char buffer[5];
502 for (int i = 0; i < 256; ++i) { 478 for (int i = 0; i < 256; ++i) {
503 size_t len = SkPDFUtils::ColorToDecimal(i, buffer); 479 size_t len = SkPDFUtils::ColorToDecimal(i, buffer);
504 REPORTER_ASSERT(reporter, len == strlen(buffer)); 480 REPORTER_ASSERT(reporter, len == strlen(buffer));
505 float f; 481 float f;
506 REPORTER_ASSERT(reporter, 1 == sscanf(buffer, "%f", &f)); 482 REPORTER_ASSERT(reporter, 1 == sscanf(buffer, "%f", &f));
507 int roundTrip = (int)(0.5 + f * 255); 483 int roundTrip = (int)(0.5 + f * 255);
508 REPORTER_ASSERT(reporter, roundTrip == i); 484 REPORTER_ASSERT(reporter, roundTrip == i);
509 } 485 }
510 } 486 }
OLDNEW
« no previous file with comments | « tests/PDFGlyphsToUnicodeTest.cpp ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698