blob: 02dfb2385018264eeb4f33c6e1cc2a6abababe5b [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 Kleinc0bd9f92019-04-23 12:05:21 -05009#include "include/core/SkCanvas.h"
10#include "include/core/SkExecutor.h"
11#include "include/core/SkStream.h"
12#include "include/docs/SkPDFDocument.h"
13#include "src/core/SkOSFile.h"
14#include "src/utils/SkOSPath.h"
15#include "tools/Resources.h"
commit-bot@chromium.org8c908272013-10-22 14:49:03 +000016
Mike Kleinc0bd9f92019-04-23 12:05:21 -050017#include "tools/ToolUtils.h"
Hal Canarydb683012016-11-23 08:55:18 -070018
commit-bot@chromium.org8c908272013-10-22 14:49:03 +000019static void test_empty(skiatest::Reporter* reporter) {
20 SkDynamicMemoryWStream stream;
21
Hal Canary3026d4b2019-01-07 10:00:48 -050022 auto doc = SkPDF::MakeDocument(&stream);
commit-bot@chromium.org8c908272013-10-22 14:49:03 +000023
24 doc->close();
25
26 REPORTER_ASSERT(reporter, stream.bytesWritten() == 0);
27}
28
29static void test_abort(skiatest::Reporter* reporter) {
30 SkDynamicMemoryWStream stream;
Hal Canary3026d4b2019-01-07 10:00:48 -050031 auto doc = SkPDF::MakeDocument(&stream);
commit-bot@chromium.org8c908272013-10-22 14:49:03 +000032
33 SkCanvas* canvas = doc->beginPage(100, 100);
34 canvas->drawColor(SK_ColorRED);
35 doc->endPage();
36
37 doc->abort();
38
halcanary50e82e62016-03-21 13:45:05 -070039 // Test that only the header is written, not the full document.
40 REPORTER_ASSERT(reporter, stream.bytesWritten() < 256);
commit-bot@chromium.org8c908272013-10-22 14:49:03 +000041}
42
43static void test_abortWithFile(skiatest::Reporter* reporter) {
halcanary87f3ba42015-01-20 09:30:20 -080044 SkString tmpDir = skiatest::GetTmpDir();
commit-bot@chromium.org8c908272013-10-22 14:49:03 +000045
46 if (tmpDir.isEmpty()) {
Hal Canary925e31e2017-12-11 14:42:58 -050047 ERRORF(reporter, "missing tmpDir.");
48 return;
commit-bot@chromium.org8c908272013-10-22 14:49:03 +000049 }
50
tfarinaa8e2e152014-07-28 19:26:58 -070051 SkString path = SkOSPath::Join(tmpDir.c_str(), "aborted.pdf");
Hal Canary925e31e2017-12-11 14:42:58 -050052 if (!SkFILEWStream(path.c_str()).isValid()) {
53 ERRORF(reporter, "unable to write to: %s", path.c_str());
54 return;
55 }
commit-bot@chromium.org8c908272013-10-22 14:49:03 +000056
57 // Make sure doc's destructor is called to flush.
58 {
Mike Reeda4daf192017-12-14 13:25:04 -050059 SkFILEWStream stream(path.c_str());
Hal Canary3026d4b2019-01-07 10:00:48 -050060 auto doc = SkPDF::MakeDocument(&stream);
commit-bot@chromium.org8c908272013-10-22 14:49:03 +000061
62 SkCanvas* canvas = doc->beginPage(100, 100);
63 canvas->drawColor(SK_ColorRED);
64 doc->endPage();
65
66 doc->abort();
67 }
68
69 FILE* file = fopen(path.c_str(), "r");
Hal Canary399bbd92016-11-10 13:03:21 -050070 // Test that only the header is written, not the full document.
71 char buffer[256];
72 REPORTER_ASSERT(reporter, fread(buffer, 1, sizeof(buffer), file) < sizeof(buffer));
commit-bot@chromium.org8c908272013-10-22 14:49:03 +000073 fclose(file);
74}
75
76static void test_file(skiatest::Reporter* reporter) {
halcanary87f3ba42015-01-20 09:30:20 -080077 SkString tmpDir = skiatest::GetTmpDir();
commit-bot@chromium.org8c908272013-10-22 14:49:03 +000078 if (tmpDir.isEmpty()) {
Hal Canary925e31e2017-12-11 14:42:58 -050079 ERRORF(reporter, "missing tmpDir.");
80 return;
commit-bot@chromium.org8c908272013-10-22 14:49:03 +000081 }
82
tfarinaa8e2e152014-07-28 19:26:58 -070083 SkString path = SkOSPath::Join(tmpDir.c_str(), "file.pdf");
Hal Canary925e31e2017-12-11 14:42:58 -050084 if (!SkFILEWStream(path.c_str()).isValid()) {
85 ERRORF(reporter, "unable to write to: %s", path.c_str());
86 return;
87 }
commit-bot@chromium.org8c908272013-10-22 14:49:03 +000088
Mike Reeda4daf192017-12-14 13:25:04 -050089 {
90 SkFILEWStream stream(path.c_str());
Hal Canary3026d4b2019-01-07 10:00:48 -050091 auto doc = SkPDF::MakeDocument(&stream);
Mike Reeda4daf192017-12-14 13:25:04 -050092 SkCanvas* canvas = doc->beginPage(100, 100);
commit-bot@chromium.org8c908272013-10-22 14:49:03 +000093
Mike Reeda4daf192017-12-14 13:25:04 -050094 canvas->drawColor(SK_ColorRED);
95 doc->endPage();
96 doc->close();
97 }
commit-bot@chromium.org8c908272013-10-22 14:49:03 +000098
99 FILE* file = fopen(path.c_str(), "r");
halcanary96fcdcc2015-08-27 07:41:13 -0700100 REPORTER_ASSERT(reporter, file != nullptr);
commit-bot@chromium.org8c908272013-10-22 14:49:03 +0000101 char header[100];
edisonn@google.com5237b7f2013-10-22 18:33:21 +0000102 REPORTER_ASSERT(reporter, fread(header, 4, 1, file) != 0);
commit-bot@chromium.org8c908272013-10-22 14:49:03 +0000103 REPORTER_ASSERT(reporter, strncmp(header, "%PDF", 4) == 0);
104 fclose(file);
105}
106
107static void test_close(skiatest::Reporter* reporter) {
108 SkDynamicMemoryWStream stream;
Hal Canary3026d4b2019-01-07 10:00:48 -0500109 auto doc = SkPDF::MakeDocument(&stream);
commit-bot@chromium.org8c908272013-10-22 14:49:03 +0000110
111 SkCanvas* canvas = doc->beginPage(100, 100);
112 canvas->drawColor(SK_ColorRED);
113 doc->endPage();
114
115 doc->close();
116
117 REPORTER_ASSERT(reporter, stream.bytesWritten() != 0);
118}
119
halcanary57f744e2016-09-09 11:41:59 -0700120DEF_TEST(SkPDF_document_tests, reporter) {
halcanary2ccdb632015-08-11 13:35:12 -0700121 REQUIRE_PDF_DOCUMENT(document_tests, reporter);
commit-bot@chromium.org8c908272013-10-22 14:49:03 +0000122 test_empty(reporter);
123 test_abort(reporter);
124 test_abortWithFile(reporter);
125 test_file(reporter);
126 test_close(reporter);
127}
halcanary712fdf72015-12-10 08:59:43 -0800128
halcanary57f744e2016-09-09 11:41:59 -0700129DEF_TEST(SkPDF_document_skbug_4734, r) {
130 REQUIRE_PDF_DOCUMENT(SkPDF_document_skbug_4734, r);
halcanary53b1c092016-01-06 09:02:25 -0800131 SkDynamicMemoryWStream stream;
Hal Canary3026d4b2019-01-07 10:00:48 -0500132 auto doc = SkPDF::MakeDocument(&stream);
halcanary53b1c092016-01-06 09:02:25 -0800133 SkCanvas* canvas = doc->beginPage(64, 64);
134 canvas->scale(10000.0f, 10000.0f);
135 canvas->translate(20.0f, 10.0f);
136 canvas->rotate(30.0f);
137 const char text[] = "HELLO";
Mike Reedc4745d62019-01-07 09:31:58 -0500138 canvas->drawString(text, 0, 0, SkFont(), SkPaint());
halcanary53b1c092016-01-06 09:02:25 -0800139}
halcanary57f744e2016-09-09 11:41:59 -0700140
141static bool contains(const uint8_t* result, size_t size, const char expectation[]) {
142 size_t len = strlen(expectation);
143 size_t N = 1 + size - len;
144 for (size_t i = 0; i < N; ++i) {
145 if (0 == memcmp(result + i, expectation, len)) {
146 return true;
147 }
148 }
149 return false;
150}
151
152// verify that the PDFA flag does something.
153DEF_TEST(SkPDF_pdfa_document, r) {
154 REQUIRE_PDF_DOCUMENT(SkPDF_pdfa_document, r);
155
Hal Canary23564b92018-09-07 14:33:14 -0400156 SkPDF::Metadata pdfMetadata;
halcanary57f744e2016-09-09 11:41:59 -0700157 pdfMetadata.fTitle = "test document";
Hal Canary23564b92018-09-07 14:33:14 -0400158 pdfMetadata.fCreation = {0, 1999, 12, 5, 31, 23, 59, 59};
Mike Reeda4daf192017-12-14 13:25:04 -0500159 pdfMetadata.fPDFA = true;
halcanary57f744e2016-09-09 11:41:59 -0700160
161 SkDynamicMemoryWStream buffer;
Hal Canary23564b92018-09-07 14:33:14 -0400162 auto doc = SkPDF::MakeDocument(&buffer, pdfMetadata);
halcanary57f744e2016-09-09 11:41:59 -0700163 doc->beginPage(64, 64)->drawColor(SK_ColorRED);
164 doc->close();
reed42943c82016-09-12 12:01:44 -0700165 sk_sp<SkData> data(buffer.detachAsData());
166
halcanary57f744e2016-09-09 11:41:59 -0700167 static const char* expectations[] = {
168 "sRGB IEC61966-2.1",
169 "<dc:title><rdf:Alt><rdf:li xml:lang=\"x-default\">test document",
170 "<xmp:CreateDate>1999-12-31T23:59:59+00:00</xmp:CreateDate>",
171 "/Subtype /XML",
172 "/CreationDate (D:19991231235959+00'00')>>",
173 };
174 for (const char* expectation : expectations) {
175 if (!contains(data->bytes(), data->size(), expectation)) {
176 ERRORF(r, "PDFA expectation missing: '%s'.", expectation);
177 }
178 }
179 pdfMetadata.fProducer = "phoney library";
Mike Reeda4daf192017-12-14 13:25:04 -0500180 pdfMetadata.fPDFA = true;
Hal Canary23564b92018-09-07 14:33:14 -0400181 doc = SkPDF::MakeDocument(&buffer, pdfMetadata);
halcanary57f744e2016-09-09 11:41:59 -0700182 doc->beginPage(64, 64)->drawColor(SK_ColorRED);
183 doc->close();
reed42943c82016-09-12 12:01:44 -0700184 data = buffer.detachAsData();
halcanary57f744e2016-09-09 11:41:59 -0700185
186 static const char* moreExpectations[] = {
187 "/Producer (phoney library)",
halcanary57f744e2016-09-09 11:41:59 -0700188 "<pdf:Producer>phoney library</pdf:Producer>",
189 };
190 for (const char* expectation : moreExpectations) {
191 if (!contains(data->bytes(), data->size(), expectation)) {
192 ERRORF(r, "PDFA expectation missing: '%s'.", expectation);
193 }
194 }
195}
Hal Canary691fd1b2018-02-28 14:10:42 -0500196
197
198DEF_TEST(SkPDF_unicode_metadata, r) {
199 REQUIRE_PDF_DOCUMENT(SkPDF_unicode_metadata, r);
Hal Canary23564b92018-09-07 14:33:14 -0400200 SkPDF::Metadata pdfMetadata;
Hal Canary691fd1b2018-02-28 14:10:42 -0500201 pdfMetadata.fTitle = "𝓐𝓑𝓒𝓓𝓔 π“•π“–π“—π“˜π“™"; // Out of basic multilingual plane
202 pdfMetadata.fAuthor = "ABCDE FGHIJ"; // ASCII
203 pdfMetadata.fSubject = "αβγδΡ ΢ηθικ"; // inside basic multilingual plane
204 pdfMetadata.fPDFA = true;
205 SkDynamicMemoryWStream wStream;
206 {
Hal Canary23564b92018-09-07 14:33:14 -0400207 auto doc = SkPDF::MakeDocument(&wStream, pdfMetadata);
Hal Canary691fd1b2018-02-28 14:10:42 -0500208 doc->beginPage(612, 792)->drawColor(SK_ColorCYAN);
209 }
210 sk_sp<SkData> data(wStream.detachAsData());
211 static const char* expectations[] = {
212 "<</Title <FEFFD835DCD0D835DCD1D835DCD2D835DCD3D835DCD40020"
213 "D835DCD5D835DCD6D835DCD7D835DCD8D835DCD9>",
214 "/Author (ABCDE FGHIJ)",
215 "Subject <FEFF03B103B203B303B403B5002003B603B703B803B903BA>",
216 };
217 for (const char* expectation : expectations) {
218 if (!contains(data->bytes(), data->size(), expectation)) {
219 ERRORF(r, "PDF expectation missing: '%s'.", expectation);
220 }
221 }
222}
Hal Canaryb5ccf6f2018-10-08 11:36:12 -0400223
224// Make sure we excercise the multi-page functionality without problems.
225// Add this to args.gn to output the PDF to a file:
226// extra_cflags = [ "-DSK_PDF_TEST_MULTIPAGE=\"/tmp/skpdf_test_multipage.pdf\"" ]
227DEF_TEST(SkPDF_multiple_pages, r) {
228 REQUIRE_PDF_DOCUMENT(SkPDF_multiple_pages, r);
229 int n = 100;
230#ifdef SK_PDF_TEST_MULTIPAGE
231 SkFILEWStream wStream(SK_PDF_TEST_MULTIPAGE);
232#else
233 SkDynamicMemoryWStream wStream;
234#endif
235 auto doc = SkPDF::MakeDocument(&wStream);
236 for (int i = 0; i < n; ++i) {
237 doc->beginPage(612, 792)->drawColor(
238 SkColorSetARGB(0xFF, 0x00, (uint8_t)(255.0f * i / (n - 1)), 0x00));
239 }
240}
Hal Canarydd9003a2018-12-21 13:44:31 -0500241
242// Test to make sure that jobs launched by PDF backend don't cause a segfault
243// after calling abort().
244DEF_TEST(SkPDF_abort_jobs, rep) {
Robert Phillipse5f73282019-06-18 17:15:04 -0400245 REQUIRE_PDF_DOCUMENT(SkPDF_abort_jobs, rep);
Hal Canarydd9003a2018-12-21 13:44:31 -0500246 SkBitmap b;
247 b.allocN32Pixels(612, 792);
248 b.eraseColor(0x4F9643A0);
249 SkPDF::Metadata metadata;
250 std::unique_ptr<SkExecutor> executor = SkExecutor::MakeFIFOThreadPool();
251 metadata.fExecutor = executor.get();
252 SkNullWStream dst;
Hal Canary3026d4b2019-01-07 10:00:48 -0500253 auto doc = SkPDF::MakeDocument(&dst, metadata);
Hal Canarydd9003a2018-12-21 13:44:31 -0500254 doc->beginPage(612, 792)->drawBitmap(b, 0, 0);
255 doc->abort();
256}
257