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