2010-10-01 23:26:55 +00:00
|
|
|
/*
|
2011-07-28 14:26:00 +00:00
|
|
|
* Copyright 2010 The Android Open Source Project
|
2010-10-01 23:26:55 +00:00
|
|
|
*
|
2011-07-28 14:26:00 +00:00
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
2010-10-01 23:26:55 +00:00
|
|
|
*/
|
|
|
|
|
2016-12-30 18:09:03 +00:00
|
|
|
#include "Test.h"
|
|
|
|
|
|
|
|
#ifdef SK_SUPPORT_PDF
|
|
|
|
|
2015-10-12 20:05:04 +00:00
|
|
|
#include "Resources.h"
|
2013-04-24 13:01:01 +00:00
|
|
|
#include "SkBitmap.h"
|
2013-01-25 19:27:23 +00:00
|
|
|
#include "SkCanvas.h"
|
2018-03-28 16:51:00 +00:00
|
|
|
#include "SkClusterator.h"
|
2011-06-24 19:12:12 +00:00
|
|
|
#include "SkData.h"
|
2018-06-13 13:42:32 +00:00
|
|
|
#include "SkDeflate.h"
|
2018-07-23 14:50:49 +00:00
|
|
|
#include "SkGlyphRun.h"
|
2013-04-24 13:01:01 +00:00
|
|
|
#include "SkImageEncoder.h"
|
2017-11-03 12:06:09 +00:00
|
|
|
#include "SkImageFilterPriv.h"
|
2016-09-02 18:29:46 +00:00
|
|
|
#include "SkMakeUnique.h"
|
2013-04-24 13:01:01 +00:00
|
|
|
#include "SkMatrix.h"
|
2015-02-20 14:17:26 +00:00
|
|
|
#include "SkPDFCanon.h"
|
2013-01-25 19:27:23 +00:00
|
|
|
#include "SkPDFDevice.h"
|
2018-09-07 18:33:14 +00:00
|
|
|
#include "SkPDFDocument.h"
|
2015-10-12 20:05:04 +00:00
|
|
|
#include "SkPDFFont.h"
|
2010-09-24 22:25:30 +00:00
|
|
|
#include "SkPDFTypes.h"
|
2016-02-24 23:46:46 +00:00
|
|
|
#include "SkPDFUtils.h"
|
2014-10-20 21:03:12 +00:00
|
|
|
#include "SkReadBuffer.h"
|
2010-09-24 22:25:30 +00:00
|
|
|
#include "SkScalar.h"
|
2016-04-04 19:07:47 +00:00
|
|
|
#include "SkSpecialImage.h"
|
2010-09-24 22:25:30 +00:00
|
|
|
#include "SkStream.h"
|
2018-06-13 13:59:02 +00:00
|
|
|
#include "SkTo.h"
|
2011-07-20 17:39:01 +00:00
|
|
|
#include "SkTypes.h"
|
2015-10-12 20:05:04 +00:00
|
|
|
#include "sk_tool_utils.h"
|
2010-09-24 22:25:30 +00:00
|
|
|
|
2016-07-25 20:59:30 +00:00
|
|
|
#include <cstdlib>
|
|
|
|
#include <cmath>
|
|
|
|
|
2013-10-03 19:29:21 +00:00
|
|
|
#define DUMMY_TEXT "DCT compessed stream."
|
|
|
|
|
2015-05-01 14:35:45 +00:00
|
|
|
template <typename T>
|
2016-08-18 21:22:52 +00:00
|
|
|
static SkString emit_to_string(T& obj, SkPDFObjNumMap* catPtr = nullptr) {
|
|
|
|
SkPDFObjNumMap catalog;
|
2015-05-01 14:35:45 +00:00
|
|
|
SkDynamicMemoryWStream buffer;
|
|
|
|
if (!catPtr) {
|
|
|
|
catPtr = &catalog;
|
2015-01-23 19:45:10 +00:00
|
|
|
}
|
2016-08-18 21:22:52 +00:00
|
|
|
obj.emitObject(&buffer, *catPtr);
|
2016-07-25 20:59:30 +00:00
|
|
|
SkString tmp(buffer.bytesWritten());
|
|
|
|
buffer.copyTo(tmp.writable_str());
|
2015-05-01 14:35:45 +00:00
|
|
|
return tmp;
|
2015-01-23 19:45:10 +00:00
|
|
|
}
|
|
|
|
|
2015-05-01 14:35:45 +00:00
|
|
|
static bool eq(const SkString& str, const char* strPtr, size_t len) {
|
|
|
|
return len == str.size() && 0 == memcmp(str.c_str(), strPtr, len);
|
2015-01-23 19:45:10 +00:00
|
|
|
}
|
|
|
|
|
2016-07-25 20:59:30 +00:00
|
|
|
static void assert_eql(skiatest::Reporter* reporter,
|
|
|
|
const SkString& skString,
|
|
|
|
const char* str,
|
|
|
|
size_t len) {
|
|
|
|
if (!eq(skString, str, len)) {
|
|
|
|
REPORT_FAILURE(reporter, "", SkStringPrintf(
|
|
|
|
"'%*s' != '%s'", len, str, skString.c_str()));
|
|
|
|
}
|
|
|
|
}
|
2015-05-01 14:35:45 +00:00
|
|
|
|
2016-07-25 20:59:30 +00:00
|
|
|
static void assert_eq(skiatest::Reporter* reporter,
|
|
|
|
const SkString& skString,
|
|
|
|
const char* str) {
|
|
|
|
assert_eql(reporter, skString, str, strlen(str));
|
|
|
|
}
|
2010-09-24 22:25:30 +00:00
|
|
|
|
|
|
|
|
2016-07-25 20:59:30 +00:00
|
|
|
template <typename T>
|
|
|
|
static void assert_emit_eq(skiatest::Reporter* reporter,
|
|
|
|
T& object,
|
|
|
|
const char* string) {
|
|
|
|
SkString result = emit_to_string(object);
|
|
|
|
assert_eq(reporter, result, string);
|
|
|
|
}
|
2011-07-20 17:39:01 +00:00
|
|
|
|
|
|
|
static void TestPDFStream(skiatest::Reporter* reporter) {
|
|
|
|
char streamBytes[] = "Test\nFoo\tBar";
|
2016-09-02 18:29:46 +00:00
|
|
|
auto streamData = skstd::make_unique<SkMemoryStream>(
|
|
|
|
streamBytes, strlen(streamBytes), true);
|
2016-07-27 18:12:23 +00:00
|
|
|
auto stream = sk_make_sp<SkPDFStream>(std::move(streamData));
|
2016-07-25 20:59:30 +00:00
|
|
|
assert_emit_eq(reporter,
|
2015-05-01 14:35:45 +00:00
|
|
|
*stream,
|
|
|
|
"<</Length 12>> stream\nTest\nFoo\tBar\nendstream");
|
2016-07-29 17:13:18 +00:00
|
|
|
stream->dict()->insertInt("Attribute", 42);
|
2016-07-25 20:59:30 +00:00
|
|
|
assert_emit_eq(reporter,
|
2015-05-01 14:35:45 +00:00
|
|
|
*stream,
|
|
|
|
"<</Length 12\n/Attribute 42>> stream\n"
|
|
|
|
"Test\nFoo\tBar\nendstream");
|
2011-07-20 17:39:01 +00:00
|
|
|
|
2015-02-17 22:43:06 +00:00
|
|
|
{
|
2011-07-20 17:39:01 +00:00
|
|
|
char streamBytes2[] = "This is a longer string, so that compression "
|
|
|
|
"can do something with it. With shorter strings, "
|
|
|
|
"the short circuit logic cuts in and we end up "
|
|
|
|
"with an uncompressed string.";
|
2016-07-27 21:14:04 +00:00
|
|
|
auto stream = sk_make_sp<SkPDFStream>(
|
|
|
|
SkData::MakeWithCopy(streamBytes2, strlen(streamBytes2)));
|
2011-07-20 17:39:01 +00:00
|
|
|
|
|
|
|
SkDynamicMemoryWStream compressedByteStream;
|
2015-08-18 20:30:25 +00:00
|
|
|
SkDeflateWStream deflateWStream(&compressedByteStream);
|
|
|
|
deflateWStream.write(streamBytes2, strlen(streamBytes2));
|
|
|
|
deflateWStream.finalize();
|
2011-07-20 17:39:01 +00:00
|
|
|
|
2015-02-19 16:29:24 +00:00
|
|
|
SkDynamicMemoryWStream expected;
|
2015-03-31 19:30:20 +00:00
|
|
|
expected.writeText("<</Filter /FlateDecode\n/Length 116>> stream\n");
|
2015-08-18 20:30:25 +00:00
|
|
|
compressedByteStream.writeToStream(&expected);
|
|
|
|
compressedByteStream.reset();
|
2015-02-19 16:29:24 +00:00
|
|
|
expected.writeText("\nendstream");
|
2016-09-12 19:01:44 +00:00
|
|
|
sk_sp<SkData> expectedResultData2(expected.detachAsData());
|
2015-05-01 14:35:45 +00:00
|
|
|
SkString result = emit_to_string(*stream);
|
2016-08-03 21:17:53 +00:00
|
|
|
#ifndef SK_PDF_LESS_COMPRESSION
|
2016-07-25 20:59:30 +00:00
|
|
|
assert_eql(reporter,
|
2015-05-01 14:35:45 +00:00
|
|
|
result,
|
|
|
|
(const char*)expectedResultData2->data(),
|
|
|
|
expectedResultData2->size());
|
2016-08-03 21:17:53 +00:00
|
|
|
#endif
|
2011-07-20 17:39:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-01 14:35:45 +00:00
|
|
|
static void TestObjectNumberMap(skiatest::Reporter* reporter) {
|
|
|
|
SkPDFObjNumMap objNumMap;
|
2016-03-08 23:10:16 +00:00
|
|
|
sk_sp<SkPDFArray> a1(new SkPDFArray);
|
|
|
|
sk_sp<SkPDFArray> a2(new SkPDFArray);
|
|
|
|
sk_sp<SkPDFArray> a3(new SkPDFArray);
|
2015-05-01 14:35:45 +00:00
|
|
|
|
2017-10-03 13:38:43 +00:00
|
|
|
objNumMap.addObjectRecursively(a1.get());
|
|
|
|
objNumMap.addObjectRecursively(a2.get());
|
|
|
|
objNumMap.addObjectRecursively(a3.get());
|
2015-05-01 14:35:45 +00:00
|
|
|
|
|
|
|
// The objects should be numbered in the order they are added,
|
|
|
|
// starting with 1.
|
|
|
|
REPORTER_ASSERT(reporter, objNumMap.getObjectNumber(a1.get()) == 1);
|
|
|
|
REPORTER_ASSERT(reporter, objNumMap.getObjectNumber(a2.get()) == 2);
|
|
|
|
REPORTER_ASSERT(reporter, objNumMap.getObjectNumber(a3.get()) == 3);
|
|
|
|
// Assert that repeated calls to get the object number return
|
|
|
|
// consistent result.
|
|
|
|
REPORTER_ASSERT(reporter, objNumMap.getObjectNumber(a1.get()) == 1);
|
2010-09-24 22:25:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void TestObjectRef(skiatest::Reporter* reporter) {
|
2016-03-08 23:10:16 +00:00
|
|
|
sk_sp<SkPDFArray> a1(new SkPDFArray);
|
|
|
|
sk_sp<SkPDFArray> a2(new SkPDFArray);
|
|
|
|
a2->appendObjRef(a1);
|
2015-05-01 14:35:45 +00:00
|
|
|
|
2016-08-18 21:22:52 +00:00
|
|
|
SkPDFObjNumMap catalog;
|
2017-10-03 13:38:43 +00:00
|
|
|
catalog.addObjectRecursively(a1.get());
|
2016-08-18 21:22:52 +00:00
|
|
|
REPORTER_ASSERT(reporter, catalog.getObjectNumber(a1.get()) == 1);
|
2015-05-01 14:35:45 +00:00
|
|
|
|
|
|
|
SkString result = emit_to_string(*a2, &catalog);
|
|
|
|
// If appendObjRef misbehaves, then the result would
|
|
|
|
// be [[]], not [1 0 R].
|
2016-07-25 20:59:30 +00:00
|
|
|
assert_eq(reporter, result, "[1 0 R]");
|
2010-09-24 22:25:30 +00:00
|
|
|
}
|
|
|
|
|
2013-01-25 19:27:23 +00:00
|
|
|
// This test used to assert without the fix submitted for
|
|
|
|
// http://code.google.com/p/skia/issues/detail?id=1083.
|
|
|
|
// SKP files might have invalid glyph ids. This test ensures they are ignored,
|
|
|
|
// and there is no assert on input data in Debug mode.
|
2013-02-27 19:17:41 +00:00
|
|
|
static void test_issue1083() {
|
2015-03-01 14:55:20 +00:00
|
|
|
SkDynamicMemoryWStream outStream;
|
2018-09-07 18:33:14 +00:00
|
|
|
sk_sp<SkDocument> doc(SkPDF::MakeDocument(&outStream));
|
2015-03-01 14:55:20 +00:00
|
|
|
SkCanvas* canvas = doc->beginPage(100.0f, 100.0f);
|
2013-01-25 19:27:23 +00:00
|
|
|
SkPaint paint;
|
|
|
|
paint.setTextEncoding(SkPaint::kGlyphID_TextEncoding);
|
|
|
|
|
|
|
|
uint16_t glyphID = 65000;
|
2015-03-01 14:55:20 +00:00
|
|
|
canvas->drawText(&glyphID, 2, 0, 0, paint);
|
2013-01-25 19:27:23 +00:00
|
|
|
|
2015-03-01 14:55:20 +00:00
|
|
|
doc->close();
|
2013-01-25 19:27:23 +00:00
|
|
|
}
|
|
|
|
|
2016-07-25 20:59:30 +00:00
|
|
|
static void assert_emit_eq_number(skiatest::Reporter* reporter, float number) {
|
|
|
|
SkPDFUnion pdfUnion = SkPDFUnion::Scalar(number);
|
|
|
|
SkString result = emit_to_string(pdfUnion);
|
|
|
|
float value = static_cast<float>(std::atof(result.c_str()));
|
|
|
|
if (value != number) {
|
|
|
|
ERRORF(reporter, "%.9g != %s", number, result.c_str());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-05-01 14:35:45 +00:00
|
|
|
static void TestPDFUnion(skiatest::Reporter* reporter) {
|
|
|
|
SkPDFUnion boolTrue = SkPDFUnion::Bool(true);
|
2016-07-25 20:59:30 +00:00
|
|
|
assert_emit_eq(reporter, boolTrue, "true");
|
2015-05-01 14:35:45 +00:00
|
|
|
|
|
|
|
SkPDFUnion boolFalse = SkPDFUnion::Bool(false);
|
2016-07-25 20:59:30 +00:00
|
|
|
assert_emit_eq(reporter, boolFalse, "false");
|
2010-09-24 22:25:30 +00:00
|
|
|
|
2015-05-01 14:35:45 +00:00
|
|
|
SkPDFUnion int42 = SkPDFUnion::Int(42);
|
2016-07-25 20:59:30 +00:00
|
|
|
assert_emit_eq(reporter, int42, "42");
|
2011-03-04 03:15:13 +00:00
|
|
|
|
2016-07-25 20:59:30 +00:00
|
|
|
assert_emit_eq_number(reporter, SK_ScalarHalf);
|
|
|
|
assert_emit_eq_number(reporter, 110999.75f); // bigScalar
|
|
|
|
assert_emit_eq_number(reporter, 50000000.1f); // biggerScalar
|
|
|
|
assert_emit_eq_number(reporter, 1.0f / 65536); // smallScalar
|
2011-03-04 03:15:13 +00:00
|
|
|
|
2015-05-01 14:35:45 +00:00
|
|
|
SkPDFUnion stringSimple = SkPDFUnion::String("test ) string ( foo");
|
2016-07-25 20:59:30 +00:00
|
|
|
assert_emit_eq(reporter, stringSimple, "(test \\) string \\( foo)");
|
2015-05-01 14:35:45 +00:00
|
|
|
|
|
|
|
SkString stringComplexInput("\ttest ) string ( foo");
|
|
|
|
SkPDFUnion stringComplex = SkPDFUnion::String(stringComplexInput);
|
2016-07-25 20:59:30 +00:00
|
|
|
assert_emit_eq(reporter, stringComplex, "(\\011test \\) string \\( foo)");
|
2016-06-23 21:08:11 +00:00
|
|
|
|
|
|
|
SkString binaryStringInput("\1\2\3\4\5\6\7\10\11\12\13\14\15\16\17\20");
|
|
|
|
SkPDFUnion binaryString = SkPDFUnion::String(binaryStringInput);
|
2016-07-25 20:59:30 +00:00
|
|
|
assert_emit_eq(reporter, binaryString, "<0102030405060708090A0B0C0D0E0F10>");
|
2015-05-01 14:35:45 +00:00
|
|
|
|
|
|
|
SkString nameInput("Test name\twith#tab");
|
|
|
|
SkPDFUnion name = SkPDFUnion::Name(nameInput);
|
2016-07-25 20:59:30 +00:00
|
|
|
assert_emit_eq(reporter, name, "/Test#20name#09with#23tab");
|
2015-05-01 14:35:45 +00:00
|
|
|
|
|
|
|
SkString nameInput2("A#/%()<>[]{}B");
|
|
|
|
SkPDFUnion name2 = SkPDFUnion::Name(nameInput2);
|
2016-07-25 20:59:30 +00:00
|
|
|
assert_emit_eq(reporter, name2, "/A#23#2F#25#28#29#3C#3E#5B#5D#7B#7DB");
|
2015-05-01 14:35:45 +00:00
|
|
|
|
|
|
|
SkPDFUnion name3 = SkPDFUnion::Name("SimpleNameWithOnlyPrintableASCII");
|
2016-07-25 20:59:30 +00:00
|
|
|
assert_emit_eq(reporter, name3, "/SimpleNameWithOnlyPrintableASCII");
|
2012-09-21 17:50:50 +00:00
|
|
|
|
2012-03-05 18:44:33 +00:00
|
|
|
// Test that we correctly handle characters with the high-bit set.
|
2015-05-01 14:35:45 +00:00
|
|
|
SkString highBitString("\xDE\xAD" "be\xEF");
|
|
|
|
SkPDFUnion highBitName = SkPDFUnion::Name(highBitString);
|
2016-07-25 20:59:30 +00:00
|
|
|
assert_emit_eq(reporter, highBitName, "/#DE#ADbe#EF");
|
2015-05-01 14:35:45 +00:00
|
|
|
}
|
2012-03-05 18:44:33 +00:00
|
|
|
|
2015-05-01 14:35:45 +00:00
|
|
|
static void TestPDFArray(skiatest::Reporter* reporter) {
|
2016-03-08 23:10:16 +00:00
|
|
|
sk_sp<SkPDFArray> array(new SkPDFArray);
|
2016-07-25 20:59:30 +00:00
|
|
|
assert_emit_eq(reporter, *array, "[]");
|
2015-05-01 14:35:45 +00:00
|
|
|
|
|
|
|
array->appendInt(42);
|
2016-07-25 20:59:30 +00:00
|
|
|
assert_emit_eq(reporter, *array, "[42]");
|
2015-05-01 14:35:45 +00:00
|
|
|
|
|
|
|
array->appendScalar(SK_ScalarHalf);
|
2016-07-25 20:59:30 +00:00
|
|
|
assert_emit_eq(reporter, *array, "[42 .5]");
|
2015-05-01 14:35:45 +00:00
|
|
|
|
|
|
|
array->appendInt(0);
|
2016-07-25 20:59:30 +00:00
|
|
|
assert_emit_eq(reporter, *array, "[42 .5 0]");
|
2015-05-01 14:35:45 +00:00
|
|
|
|
|
|
|
array->appendBool(true);
|
2016-07-25 20:59:30 +00:00
|
|
|
assert_emit_eq(reporter, *array, "[42 .5 0 true]");
|
2015-05-01 14:35:45 +00:00
|
|
|
|
|
|
|
array->appendName("ThisName");
|
2016-07-25 20:59:30 +00:00
|
|
|
assert_emit_eq(reporter, *array, "[42 .5 0 true /ThisName]");
|
2015-05-01 14:35:45 +00:00
|
|
|
|
|
|
|
array->appendName(SkString("AnotherName"));
|
2016-07-25 20:59:30 +00:00
|
|
|
assert_emit_eq(reporter, *array, "[42 .5 0 true /ThisName /AnotherName]");
|
2015-05-01 14:35:45 +00:00
|
|
|
|
|
|
|
array->appendString("This String");
|
2016-07-25 20:59:30 +00:00
|
|
|
assert_emit_eq(reporter, *array,
|
2016-02-24 23:46:46 +00:00
|
|
|
"[42 .5 0 true /ThisName /AnotherName (This String)]");
|
2015-05-01 14:35:45 +00:00
|
|
|
|
|
|
|
array->appendString(SkString("Another String"));
|
2016-07-25 20:59:30 +00:00
|
|
|
assert_emit_eq(reporter, *array,
|
2016-02-24 23:46:46 +00:00
|
|
|
"[42 .5 0 true /ThisName /AnotherName (This String) "
|
2015-05-01 14:35:45 +00:00
|
|
|
"(Another String)]");
|
|
|
|
|
2016-03-08 23:10:16 +00:00
|
|
|
sk_sp<SkPDFArray> innerArray(new SkPDFArray);
|
2015-05-01 14:35:45 +00:00
|
|
|
innerArray->appendInt(-1);
|
2016-03-08 23:10:16 +00:00
|
|
|
array->appendObject(std::move(innerArray));
|
2016-07-25 20:59:30 +00:00
|
|
|
assert_emit_eq(reporter, *array,
|
2016-02-24 23:46:46 +00:00
|
|
|
"[42 .5 0 true /ThisName /AnotherName (This String) "
|
2015-05-01 14:35:45 +00:00
|
|
|
"(Another String) [-1]]");
|
|
|
|
|
2016-03-08 23:10:16 +00:00
|
|
|
sk_sp<SkPDFArray> referencedArray(new SkPDFArray);
|
2016-08-18 21:22:52 +00:00
|
|
|
SkPDFObjNumMap catalog;
|
2017-10-03 13:38:43 +00:00
|
|
|
catalog.addObjectRecursively(referencedArray.get());
|
2016-08-18 21:22:52 +00:00
|
|
|
REPORTER_ASSERT(reporter, catalog.getObjectNumber(
|
2015-05-01 14:35:45 +00:00
|
|
|
referencedArray.get()) == 1);
|
2016-03-08 23:10:16 +00:00
|
|
|
array->appendObjRef(std::move(referencedArray));
|
2010-09-24 22:25:30 +00:00
|
|
|
|
2015-05-01 14:35:45 +00:00
|
|
|
SkString result = emit_to_string(*array, &catalog);
|
2016-07-25 20:59:30 +00:00
|
|
|
assert_eq(reporter, result,
|
2016-02-24 23:46:46 +00:00
|
|
|
"[42 .5 0 true /ThisName /AnotherName (This String) "
|
2015-05-01 14:35:45 +00:00
|
|
|
"(Another String) [-1] 1 0 R]");
|
|
|
|
}
|
|
|
|
|
|
|
|
static void TestPDFDict(skiatest::Reporter* reporter) {
|
2016-03-08 23:10:16 +00:00
|
|
|
sk_sp<SkPDFDict> dict(new SkPDFDict);
|
2016-07-25 20:59:30 +00:00
|
|
|
assert_emit_eq(reporter, *dict, "<<>>");
|
2010-09-24 22:25:30 +00:00
|
|
|
|
2015-05-01 14:35:45 +00:00
|
|
|
dict->insertInt("n1", SkToSizeT(42));
|
2016-07-25 20:59:30 +00:00
|
|
|
assert_emit_eq(reporter, *dict, "<</n1 42>>");
|
2010-09-24 22:25:30 +00:00
|
|
|
|
2015-05-01 14:35:45 +00:00
|
|
|
dict.reset(new SkPDFDict);
|
2016-07-25 20:59:30 +00:00
|
|
|
assert_emit_eq(reporter, *dict, "<<>>");
|
2010-09-24 22:25:30 +00:00
|
|
|
|
2015-05-01 14:35:45 +00:00
|
|
|
dict->insertInt("n1", 42);
|
2016-07-25 20:59:30 +00:00
|
|
|
assert_emit_eq(reporter, *dict, "<</n1 42>>");
|
2011-07-06 23:31:24 +00:00
|
|
|
|
2015-05-01 14:35:45 +00:00
|
|
|
dict->insertScalar("n2", SK_ScalarHalf);
|
2013-01-25 19:27:23 +00:00
|
|
|
|
2015-05-01 14:35:45 +00:00
|
|
|
SkString n3("n3");
|
2016-03-08 23:10:16 +00:00
|
|
|
sk_sp<SkPDFArray> innerArray(new SkPDFArray);
|
2015-05-01 14:35:45 +00:00
|
|
|
innerArray->appendInt(-100);
|
2016-03-08 23:10:16 +00:00
|
|
|
dict->insertObject(n3, std::move(innerArray));
|
2016-07-25 20:59:30 +00:00
|
|
|
assert_emit_eq(reporter, *dict, "<</n1 42\n/n2 .5\n/n3 [-100]>>");
|
2015-05-01 14:35:45 +00:00
|
|
|
|
|
|
|
dict.reset(new SkPDFDict);
|
2016-07-25 20:59:30 +00:00
|
|
|
assert_emit_eq(reporter, *dict, "<<>>");
|
2015-05-01 14:35:45 +00:00
|
|
|
|
|
|
|
dict->insertInt("n1", 24);
|
2016-07-25 20:59:30 +00:00
|
|
|
assert_emit_eq(reporter, *dict, "<</n1 24>>");
|
2015-05-01 14:35:45 +00:00
|
|
|
|
|
|
|
dict->insertInt("n2", SkToSizeT(99));
|
2016-07-25 20:59:30 +00:00
|
|
|
assert_emit_eq(reporter, *dict, "<</n1 24\n/n2 99>>");
|
2015-05-01 14:35:45 +00:00
|
|
|
|
|
|
|
dict->insertScalar("n3", SK_ScalarHalf);
|
2016-07-25 20:59:30 +00:00
|
|
|
assert_emit_eq(reporter, *dict, "<</n1 24\n/n2 99\n/n3 .5>>");
|
2015-05-01 14:35:45 +00:00
|
|
|
|
|
|
|
dict->insertName("n4", "AName");
|
2016-07-25 20:59:30 +00:00
|
|
|
assert_emit_eq(reporter, *dict, "<</n1 24\n/n2 99\n/n3 .5\n/n4 /AName>>");
|
2015-05-01 14:35:45 +00:00
|
|
|
|
|
|
|
dict->insertName("n5", SkString("AnotherName"));
|
2016-07-25 20:59:30 +00:00
|
|
|
assert_emit_eq(reporter, *dict, "<</n1 24\n/n2 99\n/n3 .5\n/n4 /AName\n"
|
2015-05-01 14:35:45 +00:00
|
|
|
"/n5 /AnotherName>>");
|
|
|
|
|
|
|
|
dict->insertString("n6", "A String");
|
2016-07-25 20:59:30 +00:00
|
|
|
assert_emit_eq(reporter, *dict, "<</n1 24\n/n2 99\n/n3 .5\n/n4 /AName\n"
|
2015-05-01 14:35:45 +00:00
|
|
|
"/n5 /AnotherName\n/n6 (A String)>>");
|
|
|
|
|
|
|
|
dict->insertString("n7", SkString("Another String"));
|
2016-07-25 20:59:30 +00:00
|
|
|
assert_emit_eq(reporter, *dict, "<</n1 24\n/n2 99\n/n3 .5\n/n4 /AName\n"
|
2015-05-01 14:35:45 +00:00
|
|
|
"/n5 /AnotherName\n/n6 (A String)\n/n7 (Another String)>>");
|
|
|
|
|
|
|
|
dict.reset(new SkPDFDict("DType"));
|
2016-07-25 20:59:30 +00:00
|
|
|
assert_emit_eq(reporter, *dict, "<</Type /DType>>");
|
2016-02-24 23:46:46 +00:00
|
|
|
|
2016-03-08 23:10:16 +00:00
|
|
|
sk_sp<SkPDFArray> referencedArray(new SkPDFArray);
|
2016-08-18 21:22:52 +00:00
|
|
|
SkPDFObjNumMap catalog;
|
2017-10-03 13:38:43 +00:00
|
|
|
catalog.addObjectRecursively(referencedArray.get());
|
2016-08-18 21:22:52 +00:00
|
|
|
REPORTER_ASSERT(reporter, catalog.getObjectNumber(
|
2015-05-01 14:35:45 +00:00
|
|
|
referencedArray.get()) == 1);
|
2016-03-08 23:10:16 +00:00
|
|
|
dict->insertObjRef("n1", std::move(referencedArray));
|
2015-05-01 14:35:45 +00:00
|
|
|
SkString result = emit_to_string(*dict, &catalog);
|
2016-07-25 20:59:30 +00:00
|
|
|
assert_eq(reporter, result, "<</Type /DType\n/n1 1 0 R>>");
|
2015-05-01 14:35:45 +00:00
|
|
|
}
|
|
|
|
|
2016-09-09 18:41:59 +00:00
|
|
|
DEF_TEST(SkPDF_Primitives, reporter) {
|
2015-05-01 14:35:45 +00:00
|
|
|
TestPDFUnion(reporter);
|
|
|
|
TestPDFArray(reporter);
|
|
|
|
TestPDFDict(reporter);
|
|
|
|
TestPDFStream(reporter);
|
|
|
|
TestObjectNumberMap(reporter);
|
|
|
|
TestObjectRef(reporter);
|
2013-02-27 19:17:41 +00:00
|
|
|
test_issue1083();
|
2010-09-24 22:25:30 +00:00
|
|
|
}
|
2014-10-20 21:03:12 +00:00
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
class DummyImageFilter : public SkImageFilter {
|
|
|
|
public:
|
2016-04-04 12:05:11 +00:00
|
|
|
static sk_sp<DummyImageFilter> Make(bool visited = false) {
|
|
|
|
return sk_sp<DummyImageFilter>(new DummyImageFilter(visited));
|
|
|
|
}
|
|
|
|
|
|
|
|
SK_DECLARE_PUBLIC_FLATTENABLE_DESERIALIZATION_PROCS(DummyImageFilter)
|
|
|
|
bool visited() const { return fVisited; }
|
|
|
|
|
|
|
|
protected:
|
2016-04-04 19:07:47 +00:00
|
|
|
sk_sp<SkSpecialImage> onFilterImage(SkSpecialImage* source, const Context&,
|
|
|
|
SkIPoint* offset) const override {
|
2014-10-20 21:03:12 +00:00
|
|
|
fVisited = true;
|
|
|
|
offset->fX = offset->fY = 0;
|
2016-04-04 19:07:47 +00:00
|
|
|
return sk_ref_sp<SkSpecialImage>(source);
|
2014-10-20 21:03:12 +00:00
|
|
|
}
|
2017-04-17 15:57:29 +00:00
|
|
|
sk_sp<SkImageFilter> onMakeColorSpace(SkColorSpaceXformer*) const override {
|
|
|
|
return sk_ref_sp(const_cast<DummyImageFilter*>(this));
|
|
|
|
}
|
2014-10-20 21:03:12 +00:00
|
|
|
|
|
|
|
private:
|
2016-04-04 12:05:11 +00:00
|
|
|
DummyImageFilter(bool visited) : INHERITED(nullptr, 0, nullptr), fVisited(visited) {}
|
|
|
|
|
2014-10-20 21:03:12 +00:00
|
|
|
mutable bool fVisited;
|
2016-04-04 12:05:11 +00:00
|
|
|
|
|
|
|
typedef SkImageFilter INHERITED;
|
2014-10-20 21:03:12 +00:00
|
|
|
};
|
|
|
|
|
2016-04-03 16:11:13 +00:00
|
|
|
sk_sp<SkFlattenable> DummyImageFilter::CreateProc(SkReadBuffer& buffer) {
|
2014-10-20 21:03:12 +00:00
|
|
|
SK_IMAGEFILTER_UNFLATTEN_COMMON(common, 0);
|
|
|
|
bool visited = buffer.readBool();
|
2016-04-04 12:05:11 +00:00
|
|
|
return DummyImageFilter::Make(visited);
|
2014-10-20 21:03:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
// Check that PDF rendering of image filters successfully falls back to
|
|
|
|
// CPU rasterization.
|
2016-09-09 18:41:59 +00:00
|
|
|
DEF_TEST(SkPDF_ImageFilter, reporter) {
|
|
|
|
REQUIRE_PDF_DOCUMENT(SkPDF_ImageFilter, reporter);
|
2015-03-01 14:55:20 +00:00
|
|
|
SkDynamicMemoryWStream stream;
|
2018-09-07 18:33:14 +00:00
|
|
|
sk_sp<SkDocument> doc(SkPDF::MakeDocument(&stream));
|
2015-03-01 14:55:20 +00:00
|
|
|
SkCanvas* canvas = doc->beginPage(100.0f, 100.0f);
|
|
|
|
|
2016-04-04 12:05:11 +00:00
|
|
|
sk_sp<DummyImageFilter> filter(DummyImageFilter::Make());
|
2014-10-20 21:03:12 +00:00
|
|
|
|
|
|
|
// Filter just created; should be unvisited.
|
|
|
|
REPORTER_ASSERT(reporter, !filter->visited());
|
|
|
|
SkPaint paint;
|
2016-11-01 15:22:05 +00:00
|
|
|
paint.setImageFilter(filter);
|
2015-03-01 14:55:20 +00:00
|
|
|
canvas->drawRect(SkRect::MakeWH(100, 100), paint);
|
|
|
|
doc->close();
|
2014-10-20 21:03:12 +00:00
|
|
|
|
|
|
|
// Filter was used in rendering; should be visited.
|
|
|
|
REPORTER_ASSERT(reporter, filter->visited());
|
|
|
|
}
|
2015-10-12 20:05:04 +00:00
|
|
|
|
|
|
|
// Check that PDF rendering of image filters successfully falls back to
|
|
|
|
// CPU rasterization.
|
2016-09-09 18:41:59 +00:00
|
|
|
DEF_TEST(SkPDF_FontCanEmbedTypeface, reporter) {
|
2015-10-12 20:05:04 +00:00
|
|
|
SkPDFCanon canon;
|
|
|
|
|
|
|
|
const char resource[] = "fonts/Roboto2-Regular_NoEmbed.ttf";
|
2016-05-12 17:09:30 +00:00
|
|
|
sk_sp<SkTypeface> noEmbedTypeface(MakeResourceAsTypeface(resource));
|
2015-10-12 20:05:04 +00:00
|
|
|
if (noEmbedTypeface) {
|
|
|
|
REPORTER_ASSERT(reporter,
|
2016-03-08 23:10:16 +00:00
|
|
|
!SkPDFFont::CanEmbedTypeface(noEmbedTypeface.get(), &canon));
|
2015-10-12 20:05:04 +00:00
|
|
|
}
|
2016-03-08 23:10:16 +00:00
|
|
|
sk_sp<SkTypeface> portableTypeface(
|
2017-08-28 14:34:05 +00:00
|
|
|
sk_tool_utils::create_portable_typeface(nullptr, SkFontStyle()));
|
2015-10-12 20:05:04 +00:00
|
|
|
REPORTER_ASSERT(reporter,
|
2016-03-08 23:10:16 +00:00
|
|
|
SkPDFFont::CanEmbedTypeface(portableTypeface.get(), &canon));
|
2015-10-12 20:05:04 +00:00
|
|
|
}
|
2016-02-24 23:46:46 +00:00
|
|
|
|
|
|
|
|
|
|
|
// test to see that all finite scalars round trip via scanf().
|
|
|
|
static void check_pdf_scalar_serialization(
|
|
|
|
skiatest::Reporter* reporter, float inputFloat) {
|
2018-01-02 21:25:53 +00:00
|
|
|
char floatString[kMaximumSkFloatToDecimalLength];
|
|
|
|
size_t len = SkFloatToDecimal(inputFloat, floatString);
|
2016-02-24 23:46:46 +00:00
|
|
|
if (len >= sizeof(floatString)) {
|
|
|
|
ERRORF(reporter, "string too long: %u", (unsigned)len);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (floatString[len] != '\0' || strlen(floatString) != len) {
|
|
|
|
ERRORF(reporter, "terminator misplaced.");
|
|
|
|
return; // The terminator is needed for sscanf().
|
|
|
|
}
|
|
|
|
if (reporter->verbose()) {
|
|
|
|
SkDebugf("%15.9g = \"%s\"\n", inputFloat, floatString);
|
|
|
|
}
|
|
|
|
float roundTripFloat;
|
|
|
|
if (1 != sscanf(floatString, "%f", &roundTripFloat)) {
|
|
|
|
ERRORF(reporter, "unscannable result: %s", floatString);
|
|
|
|
return;
|
|
|
|
}
|
2016-07-25 20:59:30 +00:00
|
|
|
if (std::isfinite(inputFloat) && roundTripFloat != inputFloat) {
|
2016-02-24 23:46:46 +00:00
|
|
|
ERRORF(reporter, "roundTripFloat (%.9g) != inputFloat (%.9g)",
|
|
|
|
roundTripFloat, inputFloat);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test SkPDFUtils::AppendScalar for accuracy.
|
2016-09-09 18:41:59 +00:00
|
|
|
DEF_TEST(SkPDF_Primitives_Scalar, reporter) {
|
2016-02-24 23:46:46 +00:00
|
|
|
SkRandom random(0x5EED);
|
|
|
|
int iterationCount = 512;
|
|
|
|
while (iterationCount-- > 0) {
|
|
|
|
union { uint32_t u; float f; };
|
|
|
|
u = random.nextU();
|
|
|
|
static_assert(sizeof(float) == sizeof(uint32_t), "");
|
|
|
|
check_pdf_scalar_serialization(reporter, f);
|
|
|
|
}
|
|
|
|
float alwaysCheck[] = {
|
|
|
|
0.0f, -0.0f, 1.0f, -1.0f, SK_ScalarPI, 0.1f, FLT_MIN, FLT_MAX,
|
|
|
|
-FLT_MIN, -FLT_MAX, FLT_MIN / 16.0f, -FLT_MIN / 16.0f,
|
|
|
|
SK_FloatNaN, SK_FloatInfinity, SK_FloatNegativeInfinity,
|
|
|
|
-FLT_MIN / 8388608.0
|
|
|
|
};
|
|
|
|
for (float inputFloat: alwaysCheck) {
|
|
|
|
check_pdf_scalar_serialization(reporter, inputFloat);
|
|
|
|
}
|
|
|
|
}
|
2016-07-15 20:41:27 +00:00
|
|
|
|
|
|
|
// Test SkPDFUtils:: for accuracy.
|
2016-09-09 18:41:59 +00:00
|
|
|
DEF_TEST(SkPDF_Primitives_Color, reporter) {
|
2016-07-15 20:41:27 +00:00
|
|
|
char buffer[5];
|
|
|
|
for (int i = 0; i < 256; ++i) {
|
|
|
|
size_t len = SkPDFUtils::ColorToDecimal(i, buffer);
|
|
|
|
REPORTER_ASSERT(reporter, len == strlen(buffer));
|
|
|
|
float f;
|
|
|
|
REPORTER_ASSERT(reporter, 1 == sscanf(buffer, "%f", &f));
|
|
|
|
int roundTrip = (int)(0.5 + f * 255);
|
|
|
|
REPORTER_ASSERT(reporter, roundTrip == i);
|
|
|
|
}
|
|
|
|
}
|
2018-03-28 16:51:00 +00:00
|
|
|
|
2018-07-20 15:48:38 +00:00
|
|
|
static SkGlyphRun make_run(size_t len, const SkGlyphID* glyphs, SkPoint* pos,
|
2018-07-23 14:50:49 +00:00
|
|
|
SkPaint paint, const uint32_t* clusters,
|
|
|
|
size_t utf8TextByteLength, const char* utf8Text) {
|
|
|
|
return SkGlyphRun(std::move(paint),
|
|
|
|
SkSpan<const uint16_t>{}, // No dense indices for now.
|
2018-07-26 20:54:18 +00:00
|
|
|
SkSpan<const SkPoint>{pos, len},
|
2018-07-23 14:50:49 +00:00
|
|
|
SkSpan<const SkGlyphID>{glyphs, len},
|
|
|
|
SkSpan<const SkGlyphID>{},
|
|
|
|
SkSpan<const char>{utf8Text, utf8TextByteLength},
|
|
|
|
SkSpan<const uint32_t>{clusters, len});
|
|
|
|
}
|
|
|
|
|
2018-03-28 16:51:00 +00:00
|
|
|
DEF_TEST(SkPDF_Clusterator, reporter) {
|
|
|
|
SkPaint paint;
|
|
|
|
paint.setTextEncoding(SkPaint::kGlyphID_TextEncoding);
|
2018-04-02 15:36:39 +00:00
|
|
|
{
|
2018-07-23 14:50:49 +00:00
|
|
|
constexpr unsigned len = 11;
|
|
|
|
const uint32_t clusters[len] = { 3, 2, 2, 1, 0, 4, 4, 7, 6, 6, 5 };
|
|
|
|
const SkGlyphID glyphs[len] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11};
|
2018-07-20 15:48:38 +00:00
|
|
|
SkPoint pos[len] = {{0, 0}, {0, 0}, {0, 0}, {0, 0}, {0, 0}, {0, 0},
|
2018-07-23 14:50:49 +00:00
|
|
|
{0, 0}, {0, 0}, {0, 0}, {0, 0}, {0, 0}};
|
2018-04-02 15:36:39 +00:00
|
|
|
const char text[] = "abcdefgh";
|
2018-07-23 14:50:49 +00:00
|
|
|
SkGlyphRun run = make_run(len, glyphs, pos, paint, clusters, strlen(text), text);
|
|
|
|
SkClusterator clusterator(run);
|
2018-04-02 15:36:39 +00:00
|
|
|
SkClusterator::Cluster expectations[] = {
|
|
|
|
{&text[3], 1, 0, 1},
|
|
|
|
{&text[2], 1, 1, 2},
|
|
|
|
{&text[1], 1, 3, 1},
|
|
|
|
{&text[0], 1, 4, 1},
|
|
|
|
{&text[4], 1, 5, 2},
|
|
|
|
{&text[7], 1, 7, 1},
|
|
|
|
{&text[6], 1, 8, 2},
|
|
|
|
{&text[5], 1, 10, 1},
|
|
|
|
{nullptr, 0, 0, 0},
|
|
|
|
};
|
|
|
|
for (const auto& expectation : expectations) {
|
|
|
|
REPORTER_ASSERT(reporter, clusterator.next() == expectation);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
{
|
2018-07-23 14:50:49 +00:00
|
|
|
constexpr unsigned len = 5;
|
|
|
|
const uint32_t clusters[len] = { 0, 1, 4, 5, 6 };
|
|
|
|
const SkGlyphID glyphs[len] = { 43, 167, 79, 79, 82, };
|
2018-07-20 15:48:38 +00:00
|
|
|
SkPoint pos[len] = {{0, 0}, {0, 0}, {0, 0}, {0, 0}, {0, 0}};
|
2018-04-02 15:36:39 +00:00
|
|
|
const char text[] = "Ha\xCC\x8A" "llo";
|
2018-07-23 14:50:49 +00:00
|
|
|
SkGlyphRun run = make_run(len, glyphs, pos, paint, clusters, strlen(text), text);
|
|
|
|
SkClusterator clusterator(run);
|
2018-04-02 15:36:39 +00:00
|
|
|
SkClusterator::Cluster expectations[] = {
|
|
|
|
{&text[0], 1, 0, 1},
|
|
|
|
{&text[1], 3, 1, 1},
|
|
|
|
{&text[4], 1, 2, 1},
|
|
|
|
{&text[5], 1, 3, 1},
|
|
|
|
{&text[6], 1, 4, 1},
|
|
|
|
{nullptr, 0, 0, 0},
|
|
|
|
};
|
|
|
|
for (const auto& expectation : expectations) {
|
|
|
|
REPORTER_ASSERT(reporter, clusterator.next() == expectation);
|
|
|
|
}
|
2018-03-28 16:51:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-30 18:09:03 +00:00
|
|
|
#endif
|