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