• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2013 Google Inc.
3  *
4  * Use of this source code is governed by a BSD-style license that can be
5  * found in the LICENSE file.
6  */
7 #include "tests/Test.h"
8 
9 #include "include/core/SkBitmap.h"
10 #include "include/core/SkCanvas.h"
11 #include "include/core/SkExecutor.h"
12 #include "include/core/SkStream.h"
13 #include "include/docs/SkPDFDocument.h"
14 #include "src/core/SkOSFile.h"
15 #include "src/utils/SkOSPath.h"
16 #include "tools/Resources.h"
17 
18 #include "tools/ToolUtils.h"
19 
test_empty(skiatest::Reporter * reporter)20 static void test_empty(skiatest::Reporter* reporter) {
21     SkDynamicMemoryWStream stream;
22 
23     auto doc = SkPDF::MakeDocument(&stream);
24 
25     doc->close();
26 
27     REPORTER_ASSERT(reporter, stream.bytesWritten() == 0);
28 }
29 
test_abort(skiatest::Reporter * reporter)30 static void test_abort(skiatest::Reporter* reporter) {
31     SkDynamicMemoryWStream stream;
32     auto doc = SkPDF::MakeDocument(&stream);
33 
34     SkCanvas* canvas = doc->beginPage(100, 100);
35     canvas->drawColor(SK_ColorRED);
36     doc->endPage();
37 
38     doc->abort();
39 
40     // Test that only the header is written, not the full document.
41     REPORTER_ASSERT(reporter, stream.bytesWritten() < 256);
42 }
43 
test_abortWithFile(skiatest::Reporter * reporter)44 static void test_abortWithFile(skiatest::Reporter* reporter) {
45     SkString tmpDir = skiatest::GetTmpDir();
46 
47     if (tmpDir.isEmpty()) {
48         ERRORF(reporter, "missing tmpDir.");
49         return;
50     }
51 
52     SkString path = SkOSPath::Join(tmpDir.c_str(), "aborted.pdf");
53     if (!SkFILEWStream(path.c_str()).isValid()) {
54         ERRORF(reporter, "unable to write to: %s", path.c_str());
55         return;
56     }
57 
58     // Make sure doc's destructor is called to flush.
59     {
60         SkFILEWStream stream(path.c_str());
61         auto doc = SkPDF::MakeDocument(&stream);
62 
63         SkCanvas* canvas = doc->beginPage(100, 100);
64         canvas->drawColor(SK_ColorRED);
65         doc->endPage();
66 
67         doc->abort();
68     }
69 
70     FILE* file = fopen(path.c_str(), "r");
71     // Test that only the header is written, not the full document.
72     char buffer[256];
73     REPORTER_ASSERT(reporter, fread(buffer, 1, sizeof(buffer), file) < sizeof(buffer));
74     fclose(file);
75 }
76 
test_file(skiatest::Reporter * reporter)77 static void test_file(skiatest::Reporter* reporter) {
78     SkString tmpDir = skiatest::GetTmpDir();
79     if (tmpDir.isEmpty()) {
80         ERRORF(reporter, "missing tmpDir.");
81         return;
82     }
83 
84     SkString path = SkOSPath::Join(tmpDir.c_str(), "file.pdf");
85     if (!SkFILEWStream(path.c_str()).isValid()) {
86         ERRORF(reporter, "unable to write to: %s", path.c_str());
87         return;
88     }
89 
90     {
91         SkFILEWStream stream(path.c_str());
92         auto doc = SkPDF::MakeDocument(&stream);
93         SkCanvas* canvas = doc->beginPage(100, 100);
94 
95         canvas->drawColor(SK_ColorRED);
96         doc->endPage();
97         doc->close();
98     }
99 
100     FILE* file = fopen(path.c_str(), "r");
101     REPORTER_ASSERT(reporter, file != nullptr);
102     char header[100];
103     REPORTER_ASSERT(reporter, fread(header, 4, 1, file) != 0);
104     REPORTER_ASSERT(reporter, strncmp(header, "%PDF", 4) == 0);
105     fclose(file);
106 }
107 
test_close(skiatest::Reporter * reporter)108 static void test_close(skiatest::Reporter* reporter) {
109     SkDynamicMemoryWStream stream;
110     auto doc = SkPDF::MakeDocument(&stream);
111 
112     SkCanvas* canvas = doc->beginPage(100, 100);
113     canvas->drawColor(SK_ColorRED);
114     doc->endPage();
115 
116     doc->close();
117 
118     REPORTER_ASSERT(reporter, stream.bytesWritten() != 0);
119 }
120 
DEF_TEST(SkPDF_document_tests,reporter)121 DEF_TEST(SkPDF_document_tests, reporter) {
122     REQUIRE_PDF_DOCUMENT(document_tests, reporter);
123     test_empty(reporter);
124     test_abort(reporter);
125     test_abortWithFile(reporter);
126     test_file(reporter);
127     test_close(reporter);
128 }
129 
DEF_TEST(SkPDF_document_skbug_4734,r)130 DEF_TEST(SkPDF_document_skbug_4734, r) {
131     REQUIRE_PDF_DOCUMENT(SkPDF_document_skbug_4734, r);
132     SkDynamicMemoryWStream stream;
133     auto doc = SkPDF::MakeDocument(&stream);
134     SkCanvas* canvas = doc->beginPage(64, 64);
135     canvas->scale(10000.0f, 10000.0f);
136     canvas->translate(20.0f, 10.0f);
137     canvas->rotate(30.0f);
138     const char text[] = "HELLO";
139     canvas->drawString(text, 0, 0, SkFont(), SkPaint());
140 }
141 
contains(const uint8_t * result,size_t size,const char expectation[])142 static bool contains(const uint8_t* result, size_t size, const char expectation[]) {
143     size_t len = strlen(expectation);
144     size_t N = 1 + size - len;
145     for (size_t i = 0; i < N; ++i) {
146         if (0 == memcmp(result + i, expectation, len)) {
147             return true;
148         }
149     }
150     return false;
151 }
152 
153 // verify that the PDFA flag does something.
DEF_TEST(SkPDF_pdfa_document,r)154 DEF_TEST(SkPDF_pdfa_document, r) {
155     REQUIRE_PDF_DOCUMENT(SkPDF_pdfa_document, r);
156 
157     SkPDF::Metadata pdfMetadata;
158     pdfMetadata.fTitle = "test document";
159     pdfMetadata.fCreation = {0, 1999, 12, 5, 31, 23, 59, 59};
160     pdfMetadata.fPDFA = true;
161 
162     SkDynamicMemoryWStream buffer;
163     auto doc = SkPDF::MakeDocument(&buffer, pdfMetadata);
164     doc->beginPage(64, 64)->drawColor(SK_ColorRED);
165     doc->close();
166     sk_sp<SkData> data(buffer.detachAsData());
167 
168     static const char* expectations[] = {
169         "sRGB IEC61966-2.1",
170         "<dc:title><rdf:Alt><rdf:li xml:lang=\"x-default\">test document",
171         "<xmp:CreateDate>1999-12-31T23:59:59+00:00</xmp:CreateDate>",
172         "/Subtype /XML",
173         "/CreationDate (D:19991231235959+00'00')>>",
174     };
175     for (const char* expectation : expectations) {
176         if (!contains(data->bytes(), data->size(), expectation)) {
177             ERRORF(r, "PDFA expectation missing: '%s'.", expectation);
178         }
179     }
180     pdfMetadata.fProducer = "phoney library";
181     pdfMetadata.fPDFA = true;
182     doc = SkPDF::MakeDocument(&buffer, pdfMetadata);
183     doc->beginPage(64, 64)->drawColor(SK_ColorRED);
184     doc->close();
185     data = buffer.detachAsData();
186 
187     static const char* moreExpectations[] = {
188         "/Producer (phoney library)",
189         "<pdf:Producer>phoney library</pdf:Producer>",
190     };
191     for (const char* expectation : moreExpectations) {
192         if (!contains(data->bytes(), data->size(), expectation)) {
193             ERRORF(r, "PDFA expectation missing: '%s'.", expectation);
194         }
195     }
196 }
197 
198 
DEF_TEST(SkPDF_unicode_metadata,r)199 DEF_TEST(SkPDF_unicode_metadata, r) {
200     REQUIRE_PDF_DOCUMENT(SkPDF_unicode_metadata, r);
201     SkPDF::Metadata pdfMetadata;
202     pdfMetadata.fTitle   = "���������� ����������"; // Out of basic multilingual plane
203     pdfMetadata.fAuthor  = "ABCDE FGHIJ"; // ASCII
204     pdfMetadata.fSubject = "αβγδε ζηθικ"; // inside  basic multilingual plane
205     pdfMetadata.fPDFA = true;
206     SkDynamicMemoryWStream wStream;
207     {
208         auto doc = SkPDF::MakeDocument(&wStream, pdfMetadata);
209         doc->beginPage(612, 792)->drawColor(SK_ColorCYAN);
210     }
211     sk_sp<SkData> data(wStream.detachAsData());
212     static const char* expectations[] = {
213         ("<</Title <FEFFD835DCD0D835DCD1D835DCD2D835DCD3D835DCD40020"
214             "D835DCD5D835DCD6D835DCD7D835DCD8D835DCD9>"),
215          "/Author (ABCDE FGHIJ)",
216          "Subject <FEFF03B103B203B303B403B5002003B603B703B803B903BA>",
217     };
218     for (const char* expectation : expectations) {
219         if (!contains(data->bytes(), data->size(), expectation)) {
220             ERRORF(r, "PDF expectation missing: '%s'.", expectation);
221         }
222     }
223 }
224 
225 // Make sure we excercise the multi-page functionality without problems.
226 // Add this to args.gn to output the PDF to a file:
227 //   extra_cflags = [ "-DSK_PDF_TEST_MULTIPAGE=\"/tmp/skpdf_test_multipage.pdf\"" ]
DEF_TEST(SkPDF_multiple_pages,r)228 DEF_TEST(SkPDF_multiple_pages, r) {
229     REQUIRE_PDF_DOCUMENT(SkPDF_multiple_pages, r);
230     int n = 100;
231 #ifdef SK_PDF_TEST_MULTIPAGE
232     SkFILEWStream wStream(SK_PDF_TEST_MULTIPAGE);
233 #else
234     SkDynamicMemoryWStream wStream;
235 #endif
236     auto doc = SkPDF::MakeDocument(&wStream);
237     for (int i = 0; i < n; ++i) {
238         doc->beginPage(612, 792)->drawColor(
239                 SkColorSetARGB(0xFF, 0x00, (uint8_t)(255.0f * i / (n - 1)), 0x00));
240     }
241 }
242 
243 // Test to make sure that jobs launched by PDF backend don't cause a segfault
244 // after calling abort().
DEF_TEST(SkPDF_abort_jobs,rep)245 DEF_TEST(SkPDF_abort_jobs, rep) {
246     REQUIRE_PDF_DOCUMENT(SkPDF_abort_jobs, rep);
247     SkBitmap b;
248     b.allocN32Pixels(612, 792);
249     b.eraseColor(0x4F9643A0);
250     SkPDF::Metadata metadata;
251     std::unique_ptr<SkExecutor> executor = SkExecutor::MakeFIFOThreadPool();
252     metadata.fExecutor = executor.get();
253     SkNullWStream dst;
254     auto doc = SkPDF::MakeDocument(&dst, metadata);
255     doc->beginPage(612, 792)->drawImage(b.asImage(), 0, 0);
256     doc->abort();
257 }
258 
259