blob: 36244ccd669356fbb8ddae6dde0d45f7691f0eda [file] [log] [blame]
halcanary8ee06f22015-08-11 10:30:12 -07001/*
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 */
Mike Kleinc0bd9f92019-04-23 12:05:21 -05007#include "tests/Test.h"
commit-bot@chromium.org8c908272013-10-22 14:49:03 +00008
Mike Reedac9f0c92020-12-23 10:11:33 -05009#include "include/core/SkBitmap.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050010#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"
commit-bot@chromium.org8c908272013-10-22 14:49:03 +000017
Mike Kleinc0bd9f92019-04-23 12:05:21 -050018#include "tools/ToolUtils.h"
Hal Canarydb683012016-11-23 08:55:18 -070019
commit-bot@chromium.org8c908272013-10-22 14:49:03 +000020static void test_empty(skiatest::Reporter* reporter) {
21 SkDynamicMemoryWStream stream;
22
Hal Canary3026d4b2019-01-07 10:00:48 -050023 auto doc = SkPDF::MakeDocument(&stream);
commit-bot@chromium.org8c908272013-10-22 14:49:03 +000024
25 doc->close();
26
27 REPORTER_ASSERT(reporter, stream.bytesWritten() == 0);
28}
29
30static void test_abort(skiatest::Reporter* reporter) {
31 SkDynamicMemoryWStream stream;
Hal Canary3026d4b2019-01-07 10:00:48 -050032 auto doc = SkPDF::MakeDocument(&stream);
commit-bot@chromium.org8c908272013-10-22 14:49:03 +000033
34 SkCanvas* canvas = doc->beginPage(100, 100);
35 canvas->drawColor(SK_ColorRED);
36 doc->endPage();
37
38 doc->abort();
39
halcanary50e82e62016-03-21 13:45:05 -070040 // Test that only the header is written, not the full document.
41 REPORTER_ASSERT(reporter, stream.bytesWritten() < 256);
commit-bot@chromium.org8c908272013-10-22 14:49:03 +000042}
43
44static void test_abortWithFile(skiatest::Reporter* reporter) {
halcanary87f3ba42015-01-20 09:30:20 -080045 SkString tmpDir = skiatest::GetTmpDir();
commit-bot@chromium.org8c908272013-10-22 14:49:03 +000046
47 if (tmpDir.isEmpty()) {
Hal Canary925e31e2017-12-11 14:42:58 -050048 ERRORF(reporter, "missing tmpDir.");
49 return;
commit-bot@chromium.org8c908272013-10-22 14:49:03 +000050 }
51
tfarinaa8e2e152014-07-28 19:26:58 -070052 SkString path = SkOSPath::Join(tmpDir.c_str(), "aborted.pdf");
Hal Canary925e31e2017-12-11 14:42:58 -050053 if (!SkFILEWStream(path.c_str()).isValid()) {
54 ERRORF(reporter, "unable to write to: %s", path.c_str());
55 return;
56 }
commit-bot@chromium.org8c908272013-10-22 14:49:03 +000057
58 // Make sure doc's destructor is called to flush.
59 {
Mike Reeda4daf192017-12-14 13:25:04 -050060 SkFILEWStream stream(path.c_str());
Hal Canary3026d4b2019-01-07 10:00:48 -050061 auto doc = SkPDF::MakeDocument(&stream);
commit-bot@chromium.org8c908272013-10-22 14:49:03 +000062
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");
Hal Canary399bbd92016-11-10 13:03:21 -050071 // 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));
commit-bot@chromium.org8c908272013-10-22 14:49:03 +000074 fclose(file);
75}
76
77static void test_file(skiatest::Reporter* reporter) {
halcanary87f3ba42015-01-20 09:30:20 -080078 SkString tmpDir = skiatest::GetTmpDir();
commit-bot@chromium.org8c908272013-10-22 14:49:03 +000079 if (tmpDir.isEmpty()) {
Hal Canary925e31e2017-12-11 14:42:58 -050080 ERRORF(reporter, "missing tmpDir.");
81 return;
commit-bot@chromium.org8c908272013-10-22 14:49:03 +000082 }
83
tfarinaa8e2e152014-07-28 19:26:58 -070084 SkString path = SkOSPath::Join(tmpDir.c_str(), "file.pdf");
Hal Canary925e31e2017-12-11 14:42:58 -050085 if (!SkFILEWStream(path.c_str()).isValid()) {
86 ERRORF(reporter, "unable to write to: %s", path.c_str());
87 return;
88 }
commit-bot@chromium.org8c908272013-10-22 14:49:03 +000089
Mike Reeda4daf192017-12-14 13:25:04 -050090 {
91 SkFILEWStream stream(path.c_str());
Hal Canary3026d4b2019-01-07 10:00:48 -050092 auto doc = SkPDF::MakeDocument(&stream);
Mike Reeda4daf192017-12-14 13:25:04 -050093 SkCanvas* canvas = doc->beginPage(100, 100);
commit-bot@chromium.org8c908272013-10-22 14:49:03 +000094
Mike Reeda4daf192017-12-14 13:25:04 -050095 canvas->drawColor(SK_ColorRED);
96 doc->endPage();
97 doc->close();
98 }
commit-bot@chromium.org8c908272013-10-22 14:49:03 +000099
100 FILE* file = fopen(path.c_str(), "r");
halcanary96fcdcc2015-08-27 07:41:13 -0700101 REPORTER_ASSERT(reporter, file != nullptr);
commit-bot@chromium.org8c908272013-10-22 14:49:03 +0000102 char header[100];
edisonn@google.com5237b7f2013-10-22 18:33:21 +0000103 REPORTER_ASSERT(reporter, fread(header, 4, 1, file) != 0);
commit-bot@chromium.org8c908272013-10-22 14:49:03 +0000104 REPORTER_ASSERT(reporter, strncmp(header, "%PDF", 4) == 0);
105 fclose(file);
106}
107
108static void test_close(skiatest::Reporter* reporter) {
109 SkDynamicMemoryWStream stream;
Hal Canary3026d4b2019-01-07 10:00:48 -0500110 auto doc = SkPDF::MakeDocument(&stream);
commit-bot@chromium.org8c908272013-10-22 14:49:03 +0000111
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
halcanary57f744e2016-09-09 11:41:59 -0700121DEF_TEST(SkPDF_document_tests, reporter) {
halcanary2ccdb632015-08-11 13:35:12 -0700122 REQUIRE_PDF_DOCUMENT(document_tests, reporter);
commit-bot@chromium.org8c908272013-10-22 14:49:03 +0000123 test_empty(reporter);
124 test_abort(reporter);
125 test_abortWithFile(reporter);
126 test_file(reporter);
127 test_close(reporter);
128}
halcanary712fdf72015-12-10 08:59:43 -0800129
halcanary57f744e2016-09-09 11:41:59 -0700130DEF_TEST(SkPDF_document_skbug_4734, r) {
131 REQUIRE_PDF_DOCUMENT(SkPDF_document_skbug_4734, r);
halcanary53b1c092016-01-06 09:02:25 -0800132 SkDynamicMemoryWStream stream;
Hal Canary3026d4b2019-01-07 10:00:48 -0500133 auto doc = SkPDF::MakeDocument(&stream);
halcanary53b1c092016-01-06 09:02:25 -0800134 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";
Mike Reedc4745d62019-01-07 09:31:58 -0500139 canvas->drawString(text, 0, 0, SkFont(), SkPaint());
halcanary53b1c092016-01-06 09:02:25 -0800140}
halcanary57f744e2016-09-09 11:41:59 -0700141
142static 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.
154DEF_TEST(SkPDF_pdfa_document, r) {
155 REQUIRE_PDF_DOCUMENT(SkPDF_pdfa_document, r);
156
Hal Canary23564b92018-09-07 14:33:14 -0400157 SkPDF::Metadata pdfMetadata;
halcanary57f744e2016-09-09 11:41:59 -0700158 pdfMetadata.fTitle = "test document";
Hal Canary23564b92018-09-07 14:33:14 -0400159 pdfMetadata.fCreation = {0, 1999, 12, 5, 31, 23, 59, 59};
Mike Reeda4daf192017-12-14 13:25:04 -0500160 pdfMetadata.fPDFA = true;
halcanary57f744e2016-09-09 11:41:59 -0700161
162 SkDynamicMemoryWStream buffer;
Hal Canary23564b92018-09-07 14:33:14 -0400163 auto doc = SkPDF::MakeDocument(&buffer, pdfMetadata);
halcanary57f744e2016-09-09 11:41:59 -0700164 doc->beginPage(64, 64)->drawColor(SK_ColorRED);
165 doc->close();
reed42943c82016-09-12 12:01:44 -0700166 sk_sp<SkData> data(buffer.detachAsData());
167
halcanary57f744e2016-09-09 11:41:59 -0700168 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";
Mike Reeda4daf192017-12-14 13:25:04 -0500181 pdfMetadata.fPDFA = true;
Hal Canary23564b92018-09-07 14:33:14 -0400182 doc = SkPDF::MakeDocument(&buffer, pdfMetadata);
halcanary57f744e2016-09-09 11:41:59 -0700183 doc->beginPage(64, 64)->drawColor(SK_ColorRED);
184 doc->close();
reed42943c82016-09-12 12:01:44 -0700185 data = buffer.detachAsData();
halcanary57f744e2016-09-09 11:41:59 -0700186
187 static const char* moreExpectations[] = {
188 "/Producer (phoney library)",
halcanary57f744e2016-09-09 11:41:59 -0700189 "<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}
Hal Canary691fd1b2018-02-28 14:10:42 -0500197
198
199DEF_TEST(SkPDF_unicode_metadata, r) {
200 REQUIRE_PDF_DOCUMENT(SkPDF_unicode_metadata, r);
Hal Canary23564b92018-09-07 14:33:14 -0400201 SkPDF::Metadata pdfMetadata;
Hal Canary691fd1b2018-02-28 14:10:42 -0500202 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 {
Hal Canary23564b92018-09-07 14:33:14 -0400208 auto doc = SkPDF::MakeDocument(&wStream, pdfMetadata);
Hal Canary691fd1b2018-02-28 14:10:42 -0500209 doc->beginPage(612, 792)->drawColor(SK_ColorCYAN);
210 }
211 sk_sp<SkData> data(wStream.detachAsData());
212 static const char* expectations[] = {
Brian Salomon7a492f72020-08-21 12:39:33 -0400213 ("<</Title <FEFFD835DCD0D835DCD1D835DCD2D835DCD3D835DCD40020"
214 "D835DCD5D835DCD6D835DCD7D835DCD8D835DCD9>"),
215 "/Author (ABCDE FGHIJ)",
216 "Subject <FEFF03B103B203B303B403B5002003B603B703B803B903BA>",
Hal Canary691fd1b2018-02-28 14:10:42 -0500217 };
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}
Hal Canaryb5ccf6f2018-10-08 11:36:12 -0400224
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\"" ]
228DEF_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}
Hal Canarydd9003a2018-12-21 13:44:31 -0500242
243// Test to make sure that jobs launched by PDF backend don't cause a segfault
244// after calling abort().
245DEF_TEST(SkPDF_abort_jobs, rep) {
Robert Phillipse5f73282019-06-18 17:15:04 -0400246 REQUIRE_PDF_DOCUMENT(SkPDF_abort_jobs, rep);
Hal Canarydd9003a2018-12-21 13:44:31 -0500247 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;
Hal Canary3026d4b2019-01-07 10:00:48 -0500254 auto doc = SkPDF::MakeDocument(&dst, metadata);
Mike Reed607a3822021-01-24 19:49:21 -0500255 doc->beginPage(612, 792)->drawImage(b.asImage(), 0, 0);
Hal Canarydd9003a2018-12-21 13:44:31 -0500256 doc->abort();
257}
258