2015-08-11 17:30:12 +00:00
|
|
|
|
/*
|
|
|
|
|
* Copyright 2013 Google Inc.
|
|
|
|
|
*
|
|
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
|
* found in the LICENSE file.
|
|
|
|
|
*/
|
2019-04-23 17:05:21 +00:00
|
|
|
|
#include "tests/Test.h"
|
2013-10-22 14:49:03 +00:00
|
|
|
|
|
2019-04-23 17:05:21 +00:00
|
|
|
|
#include "include/core/SkCanvas.h"
|
|
|
|
|
#include "include/core/SkExecutor.h"
|
|
|
|
|
#include "include/core/SkStream.h"
|
|
|
|
|
#include "include/docs/SkPDFDocument.h"
|
|
|
|
|
#include "src/core/SkOSFile.h"
|
|
|
|
|
#include "src/utils/SkOSPath.h"
|
|
|
|
|
#include "tools/Resources.h"
|
2013-10-22 14:49:03 +00:00
|
|
|
|
|
2019-04-23 17:05:21 +00:00
|
|
|
|
#include "tools/ToolUtils.h"
|
2016-11-23 15:55:18 +00:00
|
|
|
|
|
2013-10-22 14:49:03 +00:00
|
|
|
|
static void test_empty(skiatest::Reporter* reporter) {
|
|
|
|
|
SkDynamicMemoryWStream stream;
|
|
|
|
|
|
2019-01-07 15:00:48 +00:00
|
|
|
|
auto doc = SkPDF::MakeDocument(&stream);
|
2013-10-22 14:49:03 +00:00
|
|
|
|
|
|
|
|
|
doc->close();
|
|
|
|
|
|
|
|
|
|
REPORTER_ASSERT(reporter, stream.bytesWritten() == 0);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void test_abort(skiatest::Reporter* reporter) {
|
|
|
|
|
SkDynamicMemoryWStream stream;
|
2019-01-07 15:00:48 +00:00
|
|
|
|
auto doc = SkPDF::MakeDocument(&stream);
|
2013-10-22 14:49:03 +00:00
|
|
|
|
|
|
|
|
|
SkCanvas* canvas = doc->beginPage(100, 100);
|
|
|
|
|
canvas->drawColor(SK_ColorRED);
|
|
|
|
|
doc->endPage();
|
|
|
|
|
|
|
|
|
|
doc->abort();
|
|
|
|
|
|
2016-03-21 20:45:05 +00:00
|
|
|
|
// Test that only the header is written, not the full document.
|
|
|
|
|
REPORTER_ASSERT(reporter, stream.bytesWritten() < 256);
|
2013-10-22 14:49:03 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void test_abortWithFile(skiatest::Reporter* reporter) {
|
2015-01-20 17:30:20 +00:00
|
|
|
|
SkString tmpDir = skiatest::GetTmpDir();
|
2013-10-22 14:49:03 +00:00
|
|
|
|
|
|
|
|
|
if (tmpDir.isEmpty()) {
|
2017-12-11 19:42:58 +00:00
|
|
|
|
ERRORF(reporter, "missing tmpDir.");
|
|
|
|
|
return;
|
2013-10-22 14:49:03 +00:00
|
|
|
|
}
|
|
|
|
|
|
2014-07-29 02:26:58 +00:00
|
|
|
|
SkString path = SkOSPath::Join(tmpDir.c_str(), "aborted.pdf");
|
2017-12-11 19:42:58 +00:00
|
|
|
|
if (!SkFILEWStream(path.c_str()).isValid()) {
|
|
|
|
|
ERRORF(reporter, "unable to write to: %s", path.c_str());
|
|
|
|
|
return;
|
|
|
|
|
}
|
2013-10-22 14:49:03 +00:00
|
|
|
|
|
|
|
|
|
// Make sure doc's destructor is called to flush.
|
|
|
|
|
{
|
2017-12-14 18:25:04 +00:00
|
|
|
|
SkFILEWStream stream(path.c_str());
|
2019-01-07 15:00:48 +00:00
|
|
|
|
auto doc = SkPDF::MakeDocument(&stream);
|
2013-10-22 14:49:03 +00:00
|
|
|
|
|
|
|
|
|
SkCanvas* canvas = doc->beginPage(100, 100);
|
|
|
|
|
canvas->drawColor(SK_ColorRED);
|
|
|
|
|
doc->endPage();
|
|
|
|
|
|
|
|
|
|
doc->abort();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
FILE* file = fopen(path.c_str(), "r");
|
2016-11-10 18:03:21 +00:00
|
|
|
|
// Test that only the header is written, not the full document.
|
|
|
|
|
char buffer[256];
|
|
|
|
|
REPORTER_ASSERT(reporter, fread(buffer, 1, sizeof(buffer), file) < sizeof(buffer));
|
2013-10-22 14:49:03 +00:00
|
|
|
|
fclose(file);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void test_file(skiatest::Reporter* reporter) {
|
2015-01-20 17:30:20 +00:00
|
|
|
|
SkString tmpDir = skiatest::GetTmpDir();
|
2013-10-22 14:49:03 +00:00
|
|
|
|
if (tmpDir.isEmpty()) {
|
2017-12-11 19:42:58 +00:00
|
|
|
|
ERRORF(reporter, "missing tmpDir.");
|
|
|
|
|
return;
|
2013-10-22 14:49:03 +00:00
|
|
|
|
}
|
|
|
|
|
|
2014-07-29 02:26:58 +00:00
|
|
|
|
SkString path = SkOSPath::Join(tmpDir.c_str(), "file.pdf");
|
2017-12-11 19:42:58 +00:00
|
|
|
|
if (!SkFILEWStream(path.c_str()).isValid()) {
|
|
|
|
|
ERRORF(reporter, "unable to write to: %s", path.c_str());
|
|
|
|
|
return;
|
|
|
|
|
}
|
2013-10-22 14:49:03 +00:00
|
|
|
|
|
2017-12-14 18:25:04 +00:00
|
|
|
|
{
|
|
|
|
|
SkFILEWStream stream(path.c_str());
|
2019-01-07 15:00:48 +00:00
|
|
|
|
auto doc = SkPDF::MakeDocument(&stream);
|
2017-12-14 18:25:04 +00:00
|
|
|
|
SkCanvas* canvas = doc->beginPage(100, 100);
|
2013-10-22 14:49:03 +00:00
|
|
|
|
|
2017-12-14 18:25:04 +00:00
|
|
|
|
canvas->drawColor(SK_ColorRED);
|
|
|
|
|
doc->endPage();
|
|
|
|
|
doc->close();
|
|
|
|
|
}
|
2013-10-22 14:49:03 +00:00
|
|
|
|
|
|
|
|
|
FILE* file = fopen(path.c_str(), "r");
|
2015-08-27 14:41:13 +00:00
|
|
|
|
REPORTER_ASSERT(reporter, file != nullptr);
|
2013-10-22 14:49:03 +00:00
|
|
|
|
char header[100];
|
2013-10-22 18:33:21 +00:00
|
|
|
|
REPORTER_ASSERT(reporter, fread(header, 4, 1, file) != 0);
|
2013-10-22 14:49:03 +00:00
|
|
|
|
REPORTER_ASSERT(reporter, strncmp(header, "%PDF", 4) == 0);
|
|
|
|
|
fclose(file);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void test_close(skiatest::Reporter* reporter) {
|
|
|
|
|
SkDynamicMemoryWStream stream;
|
2019-01-07 15:00:48 +00:00
|
|
|
|
auto doc = SkPDF::MakeDocument(&stream);
|
2013-10-22 14:49:03 +00:00
|
|
|
|
|
|
|
|
|
SkCanvas* canvas = doc->beginPage(100, 100);
|
|
|
|
|
canvas->drawColor(SK_ColorRED);
|
|
|
|
|
doc->endPage();
|
|
|
|
|
|
|
|
|
|
doc->close();
|
|
|
|
|
|
|
|
|
|
REPORTER_ASSERT(reporter, stream.bytesWritten() != 0);
|
|
|
|
|
}
|
|
|
|
|
|
2016-09-09 18:41:59 +00:00
|
|
|
|
DEF_TEST(SkPDF_document_tests, reporter) {
|
2015-08-11 20:35:12 +00:00
|
|
|
|
REQUIRE_PDF_DOCUMENT(document_tests, reporter);
|
2013-10-22 14:49:03 +00:00
|
|
|
|
test_empty(reporter);
|
|
|
|
|
test_abort(reporter);
|
|
|
|
|
test_abortWithFile(reporter);
|
|
|
|
|
test_file(reporter);
|
|
|
|
|
test_close(reporter);
|
|
|
|
|
}
|
2015-12-10 16:59:43 +00:00
|
|
|
|
|
2016-09-09 18:41:59 +00:00
|
|
|
|
DEF_TEST(SkPDF_document_skbug_4734, r) {
|
|
|
|
|
REQUIRE_PDF_DOCUMENT(SkPDF_document_skbug_4734, r);
|
2016-01-06 17:02:25 +00:00
|
|
|
|
SkDynamicMemoryWStream stream;
|
2019-01-07 15:00:48 +00:00
|
|
|
|
auto doc = SkPDF::MakeDocument(&stream);
|
2016-01-06 17:02:25 +00:00
|
|
|
|
SkCanvas* canvas = doc->beginPage(64, 64);
|
|
|
|
|
canvas->scale(10000.0f, 10000.0f);
|
|
|
|
|
canvas->translate(20.0f, 10.0f);
|
|
|
|
|
canvas->rotate(30.0f);
|
|
|
|
|
const char text[] = "HELLO";
|
2019-01-07 14:31:58 +00:00
|
|
|
|
canvas->drawString(text, 0, 0, SkFont(), SkPaint());
|
2016-01-06 17:02:25 +00:00
|
|
|
|
}
|
2016-09-09 18:41:59 +00:00
|
|
|
|
|
|
|
|
|
static bool contains(const uint8_t* result, size_t size, const char expectation[]) {
|
|
|
|
|
size_t len = strlen(expectation);
|
|
|
|
|
size_t N = 1 + size - len;
|
|
|
|
|
for (size_t i = 0; i < N; ++i) {
|
|
|
|
|
if (0 == memcmp(result + i, expectation, len)) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// verify that the PDFA flag does something.
|
|
|
|
|
DEF_TEST(SkPDF_pdfa_document, r) {
|
|
|
|
|
REQUIRE_PDF_DOCUMENT(SkPDF_pdfa_document, r);
|
|
|
|
|
|
2018-09-07 18:33:14 +00:00
|
|
|
|
SkPDF::Metadata pdfMetadata;
|
2016-09-09 18:41:59 +00:00
|
|
|
|
pdfMetadata.fTitle = "test document";
|
2018-09-07 18:33:14 +00:00
|
|
|
|
pdfMetadata.fCreation = {0, 1999, 12, 5, 31, 23, 59, 59};
|
2017-12-14 18:25:04 +00:00
|
|
|
|
pdfMetadata.fPDFA = true;
|
2016-09-09 18:41:59 +00:00
|
|
|
|
|
|
|
|
|
SkDynamicMemoryWStream buffer;
|
2018-09-07 18:33:14 +00:00
|
|
|
|
auto doc = SkPDF::MakeDocument(&buffer, pdfMetadata);
|
2016-09-09 18:41:59 +00:00
|
|
|
|
doc->beginPage(64, 64)->drawColor(SK_ColorRED);
|
|
|
|
|
doc->close();
|
2016-09-12 19:01:44 +00:00
|
|
|
|
sk_sp<SkData> data(buffer.detachAsData());
|
|
|
|
|
|
2016-09-09 18:41:59 +00:00
|
|
|
|
static const char* expectations[] = {
|
|
|
|
|
"sRGB IEC61966-2.1",
|
|
|
|
|
"<dc:title><rdf:Alt><rdf:li xml:lang=\"x-default\">test document",
|
|
|
|
|
"<xmp:CreateDate>1999-12-31T23:59:59+00:00</xmp:CreateDate>",
|
|
|
|
|
"/Subtype /XML",
|
|
|
|
|
"/CreationDate (D:19991231235959+00'00')>>",
|
|
|
|
|
};
|
|
|
|
|
for (const char* expectation : expectations) {
|
|
|
|
|
if (!contains(data->bytes(), data->size(), expectation)) {
|
|
|
|
|
ERRORF(r, "PDFA expectation missing: '%s'.", expectation);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
pdfMetadata.fProducer = "phoney library";
|
2017-12-14 18:25:04 +00:00
|
|
|
|
pdfMetadata.fPDFA = true;
|
2018-09-07 18:33:14 +00:00
|
|
|
|
doc = SkPDF::MakeDocument(&buffer, pdfMetadata);
|
2016-09-09 18:41:59 +00:00
|
|
|
|
doc->beginPage(64, 64)->drawColor(SK_ColorRED);
|
|
|
|
|
doc->close();
|
2016-09-12 19:01:44 +00:00
|
|
|
|
data = buffer.detachAsData();
|
2016-09-09 18:41:59 +00:00
|
|
|
|
|
|
|
|
|
static const char* moreExpectations[] = {
|
|
|
|
|
"/Producer (phoney library)",
|
|
|
|
|
"/ProductionLibrary (Skia/PDF m",
|
|
|
|
|
"<!-- <skia:ProductionLibrary>Skia/PDF m",
|
|
|
|
|
"<pdf:Producer>phoney library</pdf:Producer>",
|
|
|
|
|
};
|
|
|
|
|
for (const char* expectation : moreExpectations) {
|
|
|
|
|
if (!contains(data->bytes(), data->size(), expectation)) {
|
|
|
|
|
ERRORF(r, "PDFA expectation missing: '%s'.", expectation);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2018-02-28 19:10:42 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
DEF_TEST(SkPDF_unicode_metadata, r) {
|
|
|
|
|
REQUIRE_PDF_DOCUMENT(SkPDF_unicode_metadata, r);
|
2018-09-07 18:33:14 +00:00
|
|
|
|
SkPDF::Metadata pdfMetadata;
|
2018-02-28 19:10:42 +00:00
|
|
|
|
pdfMetadata.fTitle = "𝓐𝓑𝓒𝓓𝓔 𝓕𝓖𝓗𝓘𝓙"; // Out of basic multilingual plane
|
|
|
|
|
pdfMetadata.fAuthor = "ABCDE FGHIJ"; // ASCII
|
|
|
|
|
pdfMetadata.fSubject = "αβγδε ζηθικ"; // inside basic multilingual plane
|
|
|
|
|
pdfMetadata.fPDFA = true;
|
|
|
|
|
SkDynamicMemoryWStream wStream;
|
|
|
|
|
{
|
2018-09-07 18:33:14 +00:00
|
|
|
|
auto doc = SkPDF::MakeDocument(&wStream, pdfMetadata);
|
2018-02-28 19:10:42 +00:00
|
|
|
|
doc->beginPage(612, 792)->drawColor(SK_ColorCYAN);
|
|
|
|
|
}
|
|
|
|
|
sk_sp<SkData> data(wStream.detachAsData());
|
|
|
|
|
static const char* expectations[] = {
|
|
|
|
|
"<</Title <FEFFD835DCD0D835DCD1D835DCD2D835DCD3D835DCD40020"
|
|
|
|
|
"D835DCD5D835DCD6D835DCD7D835DCD8D835DCD9>",
|
|
|
|
|
"/Author (ABCDE FGHIJ)",
|
|
|
|
|
"Subject <FEFF03B103B203B303B403B5002003B603B703B803B903BA>",
|
|
|
|
|
};
|
|
|
|
|
for (const char* expectation : expectations) {
|
|
|
|
|
if (!contains(data->bytes(), data->size(), expectation)) {
|
|
|
|
|
ERRORF(r, "PDF expectation missing: '%s'.", expectation);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2018-10-08 15:36:12 +00:00
|
|
|
|
|
|
|
|
|
// Make sure we excercise the multi-page functionality without problems.
|
|
|
|
|
// Add this to args.gn to output the PDF to a file:
|
|
|
|
|
// extra_cflags = [ "-DSK_PDF_TEST_MULTIPAGE=\"/tmp/skpdf_test_multipage.pdf\"" ]
|
|
|
|
|
DEF_TEST(SkPDF_multiple_pages, r) {
|
|
|
|
|
REQUIRE_PDF_DOCUMENT(SkPDF_multiple_pages, r);
|
|
|
|
|
int n = 100;
|
|
|
|
|
#ifdef SK_PDF_TEST_MULTIPAGE
|
|
|
|
|
SkFILEWStream wStream(SK_PDF_TEST_MULTIPAGE);
|
|
|
|
|
#else
|
|
|
|
|
SkDynamicMemoryWStream wStream;
|
|
|
|
|
#endif
|
|
|
|
|
auto doc = SkPDF::MakeDocument(&wStream);
|
|
|
|
|
for (int i = 0; i < n; ++i) {
|
|
|
|
|
doc->beginPage(612, 792)->drawColor(
|
|
|
|
|
SkColorSetARGB(0xFF, 0x00, (uint8_t)(255.0f * i / (n - 1)), 0x00));
|
|
|
|
|
}
|
|
|
|
|
}
|
2018-12-21 18:44:31 +00:00
|
|
|
|
|
|
|
|
|
// Test to make sure that jobs launched by PDF backend don't cause a segfault
|
|
|
|
|
// after calling abort().
|
|
|
|
|
DEF_TEST(SkPDF_abort_jobs, rep) {
|
2019-06-18 21:15:04 +00:00
|
|
|
|
REQUIRE_PDF_DOCUMENT(SkPDF_abort_jobs, rep);
|
2018-12-21 18:44:31 +00:00
|
|
|
|
SkBitmap b;
|
|
|
|
|
b.allocN32Pixels(612, 792);
|
|
|
|
|
b.eraseColor(0x4F9643A0);
|
|
|
|
|
SkPDF::Metadata metadata;
|
|
|
|
|
std::unique_ptr<SkExecutor> executor = SkExecutor::MakeFIFOThreadPool();
|
|
|
|
|
metadata.fExecutor = executor.get();
|
|
|
|
|
SkNullWStream dst;
|
2019-01-07 15:00:48 +00:00
|
|
|
|
auto doc = SkPDF::MakeDocument(&dst, metadata);
|
2018-12-21 18:44:31 +00:00
|
|
|
|
doc->beginPage(612, 792)->drawBitmap(b, 0, 0);
|
|
|
|
|
doc->abort();
|
|
|
|
|
}
|
|
|
|
|
|