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.
|
|
|
|
*/
|
2013-10-22 14:49:03 +00:00
|
|
|
#include "Test.h"
|
|
|
|
|
2015-12-10 16:59:43 +00:00
|
|
|
#include "Resources.h"
|
2013-10-22 14:49:03 +00:00
|
|
|
#include "SkCanvas.h"
|
|
|
|
#include "SkDocument.h"
|
|
|
|
#include "SkOSFile.h"
|
|
|
|
#include "SkStream.h"
|
2015-12-10 16:59:43 +00:00
|
|
|
#include "SkPixelSerializer.h"
|
2013-10-22 14:49:03 +00:00
|
|
|
|
|
|
|
static void test_empty(skiatest::Reporter* reporter) {
|
|
|
|
SkDynamicMemoryWStream stream;
|
|
|
|
|
2016-04-27 14:45:18 +00:00
|
|
|
sk_sp<SkDocument> doc(SkDocument::MakePDF(&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;
|
2016-04-27 14:45:18 +00:00
|
|
|
sk_sp<SkDocument> doc(SkDocument::MakePDF(&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()) {
|
|
|
|
return; // TODO(edisonn): unfortunatelly this pattern is used in other
|
|
|
|
// tests, but if GetTmpDir() starts returning and empty dir
|
|
|
|
// allways, then all these tests will be disabled.
|
|
|
|
}
|
|
|
|
|
2014-07-29 02:26:58 +00:00
|
|
|
SkString path = SkOSPath::Join(tmpDir.c_str(), "aborted.pdf");
|
2013-10-22 14:49:03 +00:00
|
|
|
|
|
|
|
// Make sure doc's destructor is called to flush.
|
|
|
|
{
|
2016-04-27 14:45:18 +00:00
|
|
|
sk_sp<SkDocument> doc(SkDocument::MakePDF(path.c_str()));
|
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");
|
|
|
|
// The created file should be empty.
|
|
|
|
char buffer[100];
|
|
|
|
REPORTER_ASSERT(reporter, fread(buffer, 1, 1, file) == 0);
|
|
|
|
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()) {
|
|
|
|
return; // TODO(edisonn): unfortunatelly this pattern is used in other
|
|
|
|
// tests, but if GetTmpDir() starts returning and empty dir
|
|
|
|
// allways, then all these tests will be disabled.
|
|
|
|
}
|
|
|
|
|
2014-07-29 02:26:58 +00:00
|
|
|
SkString path = SkOSPath::Join(tmpDir.c_str(), "file.pdf");
|
2013-10-22 14:49:03 +00:00
|
|
|
|
2016-04-27 14:45:18 +00:00
|
|
|
sk_sp<SkDocument> doc(SkDocument::MakePDF(path.c_str()));
|
2013-10-22 14:49:03 +00:00
|
|
|
|
|
|
|
SkCanvas* canvas = doc->beginPage(100, 100);
|
|
|
|
|
|
|
|
canvas->drawColor(SK_ColorRED);
|
|
|
|
doc->endPage();
|
|
|
|
doc->close();
|
|
|
|
|
|
|
|
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;
|
2016-04-27 14:45:18 +00:00
|
|
|
sk_sp<SkDocument> doc(SkDocument::MakePDF(&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);
|
|
|
|
}
|
|
|
|
|
|
|
|
DEF_TEST(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
|
|
|
|
|
|
|
namespace {
|
|
|
|
class JPEGSerializer final : public SkPixelSerializer {
|
|
|
|
bool onUseEncodedData(const void*, size_t) override { return true; }
|
|
|
|
SkData* onEncode(const SkPixmap& pixmap) override {
|
|
|
|
SkBitmap bm;
|
|
|
|
return bm.installPixels(pixmap.info(),
|
|
|
|
pixmap.writable_addr(),
|
|
|
|
pixmap.rowBytes(),
|
|
|
|
pixmap.ctable(),
|
|
|
|
nullptr, nullptr)
|
|
|
|
? SkImageEncoder::EncodeData(bm, SkImageEncoder::kJPEG_Type, 85)
|
|
|
|
: nullptr;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
size_t count_bytes(const SkBitmap& bm, bool useDCT) {
|
|
|
|
SkDynamicMemoryWStream stream;
|
2016-04-27 14:45:18 +00:00
|
|
|
sk_sp<SkDocument> doc;
|
2015-12-10 16:59:43 +00:00
|
|
|
if (useDCT) {
|
2016-04-27 14:45:18 +00:00
|
|
|
doc = SkDocument::MakePDF(&stream, SK_ScalarDefaultRasterDPI,
|
|
|
|
SkDocument::PDFMetadata(),
|
|
|
|
sk_make_sp<JPEGSerializer>(), false);
|
2015-12-10 16:59:43 +00:00
|
|
|
} else {
|
2016-04-27 14:45:18 +00:00
|
|
|
doc = SkDocument::MakePDF(&stream);
|
2015-12-10 16:59:43 +00:00
|
|
|
}
|
|
|
|
SkCanvas* canvas = doc->beginPage(64, 64);
|
|
|
|
canvas->drawBitmap(bm, 0, 0);
|
|
|
|
doc->endPage();
|
|
|
|
doc->close();
|
|
|
|
return stream.bytesWritten();
|
|
|
|
}
|
|
|
|
|
|
|
|
DEF_TEST(document_dct_encoder, r) {
|
|
|
|
REQUIRE_PDF_DOCUMENT(document_dct_encoder, r);
|
|
|
|
SkBitmap bm;
|
|
|
|
if (GetResourceAsBitmap("mandrill_64.png", &bm)) {
|
|
|
|
// Lossy encoding works better on photographs.
|
|
|
|
REPORTER_ASSERT(r, count_bytes(bm, true) < count_bytes(bm, false));
|
|
|
|
}
|
|
|
|
}
|
2016-01-06 17:02:25 +00:00
|
|
|
|
|
|
|
DEF_TEST(document_skbug_4734, r) {
|
|
|
|
REQUIRE_PDF_DOCUMENT(document_skbug_4734, r);
|
|
|
|
SkDynamicMemoryWStream stream;
|
2016-04-27 14:45:18 +00:00
|
|
|
sk_sp<SkDocument> doc(SkDocument::MakePDF(&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";
|
|
|
|
canvas->drawText(text, strlen(text), 0, 0, SkPaint());
|
|
|
|
}
|