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
|
|
|
*/
|
|
|
|
|
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"
|
2011-06-24 19:12:12 +00:00
|
|
|
#include "SkData.h"
|
2015-03-01 14:55:20 +00:00
|
|
|
#include "SkDocument.h"
|
2015-08-12 18:24:40 +00:00
|
|
|
#include "SkDeflate.h"
|
2013-04-24 13:01:01 +00:00
|
|
|
#include "SkImageEncoder.h"
|
|
|
|
#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"
|
2015-10-12 20:05:04 +00:00
|
|
|
#include "SkPDFFont.h"
|
2010-09-24 22:25:30 +00:00
|
|
|
#include "SkPDFStream.h"
|
|
|
|
#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"
|
2011-07-20 17:39:01 +00:00
|
|
|
#include "SkTypes.h"
|
2014-01-24 20:56:26 +00:00
|
|
|
#include "Test.h"
|
2015-10-12 20:05:04 +00:00
|
|
|
#include "sk_tool_utils.h"
|
2010-09-24 22:25:30 +00:00
|
|
|
|
2013-10-03 19:29:21 +00:00
|
|
|
#define DUMMY_TEXT "DCT compessed stream."
|
|
|
|
|
2015-05-01 14:35:45 +00:00
|
|
|
namespace {
|
|
|
|
struct Catalog {
|
|
|
|
SkPDFSubstituteMap substitutes;
|
|
|
|
SkPDFObjNumMap numbers;
|
|
|
|
};
|
|
|
|
} // namespace
|
2011-06-24 19:12:12 +00:00
|
|
|
|
2015-05-01 14:35:45 +00:00
|
|
|
template <typename T>
|
2015-08-27 14:41:13 +00:00
|
|
|
static SkString emit_to_string(T& obj, Catalog* catPtr = nullptr) {
|
2015-05-01 14:35:45 +00:00
|
|
|
Catalog catalog;
|
|
|
|
SkDynamicMemoryWStream buffer;
|
|
|
|
if (!catPtr) {
|
|
|
|
catPtr = &catalog;
|
2015-01-23 19:45:10 +00:00
|
|
|
}
|
2015-05-01 14:35:45 +00:00
|
|
|
obj.emitObject(&buffer, catPtr->numbers, catPtr->substitutes);
|
|
|
|
SkAutoTDelete<SkStreamAsset> asset(buffer.detachAsStream());
|
|
|
|
SkString tmp(asset->getLength());
|
|
|
|
asset->read(tmp.writable_str(), asset->getLength());
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2015-05-01 14:35:45 +00:00
|
|
|
#define ASSERT_EQL(REPORTER, SKSTRING, STRING, LEN) \
|
|
|
|
do { \
|
|
|
|
const char* strptr = STRING; \
|
|
|
|
const SkString& sks = SKSTRING; \
|
|
|
|
if (!eq(sks, strptr, LEN)) { \
|
|
|
|
REPORT_FAILURE( \
|
|
|
|
REPORTER, \
|
|
|
|
"", \
|
|
|
|
SkStringPrintf("'%s' != '%s'", strptr, sks.c_str())); \
|
|
|
|
} \
|
|
|
|
} while (false)
|
2010-09-24 22:25:30 +00:00
|
|
|
|
2015-05-01 14:35:45 +00:00
|
|
|
#define ASSERT_EQ(REPORTER, SKSTRING, STRING) \
|
|
|
|
do { \
|
|
|
|
const char* str = STRING; \
|
|
|
|
ASSERT_EQL(REPORTER, SKSTRING, str, strlen(str)); \
|
|
|
|
} while (false)
|
|
|
|
|
|
|
|
#define ASSERT_EMIT_EQ(REPORTER, OBJECT, STRING) \
|
|
|
|
do { \
|
|
|
|
SkString result = emit_to_string(OBJECT); \
|
|
|
|
ASSERT_EQ(REPORTER, result, STRING); \
|
|
|
|
} while (false)
|
2010-09-24 22:25:30 +00:00
|
|
|
|
|
|
|
|
2011-07-20 17:39:01 +00:00
|
|
|
|
|
|
|
static void TestPDFStream(skiatest::Reporter* reporter) {
|
|
|
|
char streamBytes[] = "Test\nFoo\tBar";
|
2015-01-21 20:09:53 +00:00
|
|
|
SkAutoTDelete<SkMemoryStream> streamData(new SkMemoryStream(
|
2013-01-04 19:31:24 +00:00
|
|
|
streamBytes, strlen(streamBytes), true));
|
2016-03-08 23:10:16 +00:00
|
|
|
sk_sp<SkPDFStream> stream(new SkPDFStream(streamData.get()));
|
2015-05-01 14:35:45 +00:00
|
|
|
ASSERT_EMIT_EQ(reporter,
|
|
|
|
*stream,
|
|
|
|
"<</Length 12>> stream\nTest\nFoo\tBar\nendstream");
|
|
|
|
stream->insertInt("Attribute", 42);
|
|
|
|
ASSERT_EMIT_EQ(reporter,
|
|
|
|
*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.";
|
|
|
|
SkAutoDataUnref streamData2(SkData::NewWithCopy(streamBytes2,
|
|
|
|
strlen(streamBytes2)));
|
2016-03-08 23:10:16 +00:00
|
|
|
sk_sp<SkPDFStream> stream(new SkPDFStream(streamData2.get()));
|
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");
|
|
|
|
SkAutoDataUnref expectedResultData2(expected.copyToData());
|
2015-05-01 14:35:45 +00:00
|
|
|
SkString result = emit_to_string(*stream);
|
|
|
|
ASSERT_EQL(reporter,
|
|
|
|
result,
|
|
|
|
(const char*)expectedResultData2->data(),
|
|
|
|
expectedResultData2->size());
|
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
|
|
|
|
|
|
|
objNumMap.addObject(a1.get());
|
|
|
|
objNumMap.addObject(a2.get());
|
|
|
|
objNumMap.addObject(a3.get());
|
|
|
|
|
|
|
|
// 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
|
|
|
|
|
|
|
Catalog catalog;
|
|
|
|
catalog.numbers.addObject(a1.get());
|
|
|
|
REPORTER_ASSERT(reporter, catalog.numbers.getObjectNumber(a1.get()) == 1);
|
|
|
|
|
|
|
|
SkString result = emit_to_string(*a2, &catalog);
|
|
|
|
// If appendObjRef misbehaves, then the result would
|
|
|
|
// be [[]], not [1 0 R].
|
|
|
|
ASSERT_EQ(reporter, result, "[1 0 R]");
|
2010-09-24 22:25:30 +00:00
|
|
|
}
|
|
|
|
|
2011-07-06 23:31:24 +00:00
|
|
|
static void TestSubstitute(skiatest::Reporter* reporter) {
|
2016-03-08 23:10:16 +00:00
|
|
|
sk_sp<SkPDFDict> proxy(new SkPDFDict());
|
|
|
|
sk_sp<SkPDFDict> stub(new SkPDFDict());
|
2011-07-06 23:31:24 +00:00
|
|
|
|
2015-05-01 14:35:45 +00:00
|
|
|
proxy->insertInt("Value", 33);
|
|
|
|
stub->insertInt("Value", 44);
|
2011-07-06 23:31:24 +00:00
|
|
|
|
2015-03-31 19:30:20 +00:00
|
|
|
SkPDFSubstituteMap substituteMap;
|
|
|
|
substituteMap.setSubstitute(proxy.get(), stub.get());
|
|
|
|
SkPDFObjNumMap catalog;
|
2015-03-25 18:29:18 +00:00
|
|
|
catalog.addObject(proxy.get());
|
2011-07-06 23:31:24 +00:00
|
|
|
|
2016-03-08 23:10:16 +00:00
|
|
|
REPORTER_ASSERT(reporter, stub.get() == substituteMap.getSubstitute(proxy.get()));
|
|
|
|
REPORTER_ASSERT(reporter, proxy.get() != substituteMap.getSubstitute(stub.get()));
|
2011-07-06 23:31:24 +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;
|
2016-04-27 14:45:18 +00:00
|
|
|
sk_sp<SkDocument> doc(SkDocument::MakePDF(&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
|
|
|
}
|
|
|
|
|
2015-05-01 14:35:45 +00:00
|
|
|
static void TestPDFUnion(skiatest::Reporter* reporter) {
|
|
|
|
SkPDFUnion boolTrue = SkPDFUnion::Bool(true);
|
|
|
|
ASSERT_EMIT_EQ(reporter, boolTrue, "true");
|
|
|
|
|
|
|
|
SkPDFUnion boolFalse = SkPDFUnion::Bool(false);
|
|
|
|
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);
|
|
|
|
ASSERT_EMIT_EQ(reporter, int42, "42");
|
2010-09-24 22:25:30 +00:00
|
|
|
|
2015-05-01 14:35:45 +00:00
|
|
|
SkPDFUnion realHalf = SkPDFUnion::Scalar(SK_ScalarHalf);
|
2016-02-24 23:46:46 +00:00
|
|
|
ASSERT_EMIT_EQ(reporter, realHalf, ".5");
|
2015-05-01 14:35:45 +00:00
|
|
|
|
|
|
|
SkPDFUnion bigScalar = SkPDFUnion::Scalar(110999.75f);
|
|
|
|
ASSERT_EMIT_EQ(reporter, bigScalar, "110999.75");
|
2011-03-04 03:15:13 +00:00
|
|
|
|
2016-02-24 23:46:46 +00:00
|
|
|
SkPDFUnion biggerScalar = SkPDFUnion::Scalar(50000000.1f);
|
2015-05-01 14:35:45 +00:00
|
|
|
ASSERT_EMIT_EQ(reporter, biggerScalar, "50000000");
|
2011-03-04 03:15:13 +00:00
|
|
|
|
2016-02-24 23:46:46 +00:00
|
|
|
SkPDFUnion smallestScalar = SkPDFUnion::Scalar(1.0f / 65536);
|
|
|
|
ASSERT_EMIT_EQ(reporter, smallestScalar, ".0000152587890");
|
2011-03-04 03:15:13 +00:00
|
|
|
|
2015-05-01 14:35:45 +00:00
|
|
|
SkPDFUnion stringSimple = SkPDFUnion::String("test ) string ( foo");
|
|
|
|
ASSERT_EMIT_EQ(reporter, stringSimple, "(test \\) string \\( foo)");
|
|
|
|
|
|
|
|
SkString stringComplexInput("\ttest ) string ( foo");
|
|
|
|
SkPDFUnion stringComplex = SkPDFUnion::String(stringComplexInput);
|
2016-06-23 21:08:11 +00:00
|
|
|
ASSERT_EMIT_EQ(reporter, stringComplex, "(\\011test \\) string \\( foo)");
|
|
|
|
|
|
|
|
SkString binaryStringInput("\1\2\3\4\5\6\7\10\11\12\13\14\15\16\17\20");
|
|
|
|
SkPDFUnion binaryString = SkPDFUnion::String(binaryStringInput);
|
|
|
|
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);
|
|
|
|
ASSERT_EMIT_EQ(reporter, name, "/Test#20name#09with#23tab");
|
|
|
|
|
|
|
|
SkString nameInput2("A#/%()<>[]{}B");
|
|
|
|
SkPDFUnion name2 = SkPDFUnion::Name(nameInput2);
|
|
|
|
ASSERT_EMIT_EQ(reporter, name2, "/A#23#2F#25#28#29#3C#3E#5B#5D#7B#7DB");
|
|
|
|
|
|
|
|
SkPDFUnion name3 = SkPDFUnion::Name("SimpleNameWithOnlyPrintableASCII");
|
|
|
|
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);
|
|
|
|
ASSERT_EMIT_EQ(reporter, highBitName, "/#DE#ADbe#EF");
|
|
|
|
}
|
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);
|
2015-05-01 14:35:45 +00:00
|
|
|
ASSERT_EMIT_EQ(reporter, *array, "[]");
|
|
|
|
|
|
|
|
array->appendInt(42);
|
|
|
|
ASSERT_EMIT_EQ(reporter, *array, "[42]");
|
|
|
|
|
|
|
|
array->appendScalar(SK_ScalarHalf);
|
2016-02-24 23:46:46 +00:00
|
|
|
ASSERT_EMIT_EQ(reporter, *array, "[42 .5]");
|
2015-05-01 14:35:45 +00:00
|
|
|
|
|
|
|
array->appendInt(0);
|
2016-02-24 23:46:46 +00:00
|
|
|
ASSERT_EMIT_EQ(reporter, *array, "[42 .5 0]");
|
2015-05-01 14:35:45 +00:00
|
|
|
|
|
|
|
array->appendBool(true);
|
2016-02-24 23:46:46 +00:00
|
|
|
ASSERT_EMIT_EQ(reporter, *array, "[42 .5 0 true]");
|
2015-05-01 14:35:45 +00:00
|
|
|
|
|
|
|
array->appendName("ThisName");
|
2016-02-24 23:46:46 +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-02-24 23:46:46 +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");
|
|
|
|
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"));
|
|
|
|
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));
|
2015-05-01 14:35:45 +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);
|
2015-05-01 14:35:45 +00:00
|
|
|
Catalog catalog;
|
|
|
|
catalog.numbers.addObject(referencedArray.get());
|
|
|
|
REPORTER_ASSERT(reporter, catalog.numbers.getObjectNumber(
|
|
|
|
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);
|
|
|
|
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);
|
2015-05-01 14:35:45 +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));
|
|
|
|
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);
|
|
|
|
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);
|
|
|
|
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-02-24 23:46:46 +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);
|
|
|
|
ASSERT_EMIT_EQ(reporter, *dict, "<<>>");
|
|
|
|
|
|
|
|
dict->insertInt("n1", 24);
|
|
|
|
ASSERT_EMIT_EQ(reporter, *dict, "<</n1 24>>");
|
|
|
|
|
|
|
|
dict->insertInt("n2", SkToSizeT(99));
|
|
|
|
ASSERT_EMIT_EQ(reporter, *dict, "<</n1 24\n/n2 99>>");
|
|
|
|
|
|
|
|
dict->insertScalar("n3", SK_ScalarHalf);
|
2016-02-24 23:46:46 +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-02-24 23:46:46 +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-02-24 23:46:46 +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-02-24 23:46:46 +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-02-24 23:46:46 +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"));
|
|
|
|
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);
|
2015-05-01 14:35:45 +00:00
|
|
|
Catalog catalog;
|
|
|
|
catalog.numbers.addObject(referencedArray.get());
|
|
|
|
REPORTER_ASSERT(reporter, catalog.numbers.getObjectNumber(
|
|
|
|
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);
|
|
|
|
ASSERT_EQ(reporter, result, "<</Type /DType\n/n1 1 0 R>>");
|
|
|
|
}
|
|
|
|
|
|
|
|
DEF_TEST(PDFPrimitives, reporter) {
|
|
|
|
TestPDFUnion(reporter);
|
|
|
|
TestPDFArray(reporter);
|
|
|
|
TestPDFDict(reporter);
|
|
|
|
TestPDFStream(reporter);
|
|
|
|
TestObjectNumberMap(reporter);
|
|
|
|
TestObjectRef(reporter);
|
|
|
|
TestSubstitute(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_TO_STRING_OVERRIDE()
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2014-12-19 21:49:15 +00:00
|
|
|
#ifndef SK_IGNORE_TO_STRING
|
|
|
|
void DummyImageFilter::toString(SkString* str) const {
|
|
|
|
str->appendf("DummyImageFilter: (");
|
|
|
|
str->append(")");
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2014-10-20 21:03:12 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
// Check that PDF rendering of image filters successfully falls back to
|
|
|
|
// CPU rasterization.
|
|
|
|
DEF_TEST(PDFImageFilter, reporter) {
|
2015-03-01 14:55:20 +00:00
|
|
|
SkDynamicMemoryWStream stream;
|
2016-04-27 14:45:18 +00:00
|
|
|
sk_sp<SkDocument> doc(SkDocument::MakePDF(&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;
|
|
|
|
paint.setImageFilter(filter.get());
|
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.
|
|
|
|
DEF_TEST(PDFFontCanEmbedTypeface, reporter) {
|
|
|
|
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(
|
2016-05-31 18:42:36 +00:00
|
|
|
sk_tool_utils::create_portable_typeface(NULL, 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) {
|
|
|
|
char floatString[SkPDFUtils::kMaximumFloatDecimalLength];
|
|
|
|
size_t len = SkPDFUtils::FloatToDecimal(inputFloat, floatString);
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
if (isfinite(inputFloat) && roundTripFloat != inputFloat) {
|
|
|
|
ERRORF(reporter, "roundTripFloat (%.9g) != inputFloat (%.9g)",
|
|
|
|
roundTripFloat, inputFloat);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test SkPDFUtils::AppendScalar for accuracy.
|
|
|
|
DEF_TEST(PDFPrimitives_Scalar, reporter) {
|
|
|
|
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.
|
|
|
|
DEF_TEST(PDFPrimitives_Color, reporter) {
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|