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
|
|
|
*/
|
|
|
|
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "tests/Test.h"
|
2016-12-30 18:09:03 +00:00
|
|
|
|
|
|
|
#ifdef SK_SUPPORT_PDF
|
|
|
|
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "include/core/SkBitmap.h"
|
|
|
|
#include "include/core/SkCanvas.h"
|
|
|
|
#include "include/core/SkData.h"
|
|
|
|
#include "include/core/SkImageEncoder.h"
|
|
|
|
#include "include/core/SkMatrix.h"
|
|
|
|
#include "include/core/SkScalar.h"
|
|
|
|
#include "include/core/SkStream.h"
|
|
|
|
#include "include/core/SkTypes.h"
|
2021-01-22 18:01:26 +00:00
|
|
|
#include "include/effects/SkImageFilters.h"
|
2019-07-30 16:23:10 +00:00
|
|
|
#include "include/effects/SkPerlinNoiseShader.h"
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "include/private/SkTo.h"
|
|
|
|
#include "src/core/SkGlyphRun.h"
|
2019-08-02 13:57:04 +00:00
|
|
|
#include "src/core/SkImageFilter_Base.h"
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "src/core/SkReadBuffer.h"
|
|
|
|
#include "src/core/SkSpecialImage.h"
|
|
|
|
#include "src/pdf/SkClusterator.h"
|
|
|
|
#include "src/pdf/SkDeflate.h"
|
|
|
|
#include "src/pdf/SkPDFDevice.h"
|
|
|
|
#include "src/pdf/SkPDFDocumentPriv.h"
|
|
|
|
#include "src/pdf/SkPDFFont.h"
|
|
|
|
#include "src/pdf/SkPDFTypes.h"
|
|
|
|
#include "src/pdf/SkPDFUnion.h"
|
|
|
|
#include "src/pdf/SkPDFUtils.h"
|
|
|
|
#include "tools/Resources.h"
|
|
|
|
#include "tools/ToolUtils.h"
|
2010-09-24 22:25:30 +00:00
|
|
|
|
2016-07-25 20:59:30 +00:00
|
|
|
#include <cstdlib>
|
|
|
|
#include <cmath>
|
2020-08-03 17:21:46 +00:00
|
|
|
#include <memory>
|
2016-07-25 20:59:30 +00:00
|
|
|
|
2015-05-01 14:35:45 +00:00
|
|
|
template <typename T>
|
2018-11-13 21:19:59 +00:00
|
|
|
static SkString emit_to_string(T& obj) {
|
2015-05-01 14:35:45 +00:00
|
|
|
SkDynamicMemoryWStream buffer;
|
2018-11-13 21:19:59 +00:00
|
|
|
obj.emitObject(&buffer);
|
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(
|
2020-05-12 14:41:04 +00:00
|
|
|
"'%*s' != '%s'", (int)len, str, skString.c_str()));
|
2016-07-25 20:59:30 +00:00
|
|
|
}
|
|
|
|
}
|
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
|
|
|
|
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;
|
2019-01-07 15:00:48 +00:00
|
|
|
auto 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
|
|
|
|
|
|
|
uint16_t glyphID = 65000;
|
2019-05-07 19:38:46 +00:00
|
|
|
canvas->drawSimpleText(&glyphID, 2, SkTextEncoding::kGlyphID, 0, 0, SkFont(), SkPaint());
|
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
|
|
|
|
2022-05-23 21:45:13 +00:00
|
|
|
SkPDFUnion stringSimple = SkPDFUnion::TextString("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");
|
2022-05-23 21:45:13 +00:00
|
|
|
SkPDFUnion stringComplex = SkPDFUnion::TextString(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");
|
2022-05-23 21:45:13 +00:00
|
|
|
SkPDFUnion binaryString = SkPDFUnion::ByteString(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");
|
2019-10-11 17:29:42 +00:00
|
|
|
|
|
|
|
// https://bugs.skia.org/9508
|
|
|
|
// https://crbug.com/494913
|
|
|
|
// Trailing '\0' characters must be removed.
|
|
|
|
const char nameInput4[] = "Test name with nil\0";
|
|
|
|
SkPDFUnion name4 = SkPDFUnion::Name(SkString(nameInput4, strlen(nameInput4) + 1));
|
|
|
|
assert_emit_eq(reporter, name4, "/Test#20name#20with#20nil");
|
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) {
|
2018-12-18 21:30:41 +00:00
|
|
|
std::unique_ptr<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
|
|
|
|
2022-05-23 21:45:13 +00:00
|
|
|
array->appendTextString("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
|
|
|
|
2022-05-23 21:45:13 +00:00
|
|
|
array->appendByteString(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)]");
|
|
|
|
|
2018-12-18 21:30:41 +00:00
|
|
|
std::unique_ptr<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]]");
|
|
|
|
}
|
|
|
|
|
|
|
|
static void TestPDFDict(skiatest::Reporter* reporter) {
|
2018-12-18 21:30:41 +00:00
|
|
|
std::unique_ptr<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
|
|
|
|
2020-08-03 17:21:46 +00:00
|
|
|
dict = std::make_unique<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");
|
2018-12-18 21:30:41 +00:00
|
|
|
std::unique_ptr<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
|
|
|
|
2020-08-03 17:21:46 +00:00
|
|
|
dict = std::make_unique<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>>");
|
|
|
|
|
2022-05-23 21:45:13 +00:00
|
|
|
dict->insertTextString("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)>>");
|
|
|
|
|
2022-05-23 21:45:13 +00:00
|
|
|
dict->insertByteString("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)>>");
|
|
|
|
|
2020-08-03 17:21:46 +00:00
|
|
|
dict = std::make_unique<SkPDFDict>("DType");
|
2016-07-25 20:59:30 +00:00
|
|
|
assert_emit_eq(reporter, *dict, "<</Type /DType>>");
|
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);
|
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 {
|
|
|
|
|
2021-06-16 19:28:00 +00:00
|
|
|
class TestImageFilter : public SkImageFilter_Base {
|
2014-10-20 21:03:12 +00:00
|
|
|
public:
|
2021-06-16 19:28:00 +00:00
|
|
|
static sk_sp<TestImageFilter> Make(bool visited = false) {
|
|
|
|
return sk_sp<TestImageFilter>(new TestImageFilter(visited));
|
2016-04-04 12:05:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool visited() const { return fVisited; }
|
|
|
|
|
|
|
|
protected:
|
2019-08-14 18:35:42 +00:00
|
|
|
sk_sp<SkSpecialImage> onFilterImage(const Context& ctx, SkIPoint* offset) const override {
|
2014-10-20 21:03:12 +00:00
|
|
|
fVisited = true;
|
|
|
|
offset->fX = offset->fY = 0;
|
2019-08-14 18:35:42 +00:00
|
|
|
return sk_ref_sp<SkSpecialImage>(ctx.sourceImage());
|
2014-10-20 21:03:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2021-06-16 19:28:00 +00:00
|
|
|
SK_FLATTENABLE_HOOKS(TestImageFilter)
|
|
|
|
TestImageFilter(bool visited) : INHERITED(nullptr, 0, nullptr), fVisited(visited) {}
|
2016-04-04 12:05:11 +00:00
|
|
|
|
2014-10-20 21:03:12 +00:00
|
|
|
mutable bool fVisited;
|
2016-04-04 12:05:11 +00:00
|
|
|
|
2020-09-03 02:42:33 +00:00
|
|
|
using INHERITED = SkImageFilter_Base;
|
2014-10-20 21:03:12 +00:00
|
|
|
};
|
|
|
|
|
2021-06-16 19:28:00 +00:00
|
|
|
sk_sp<SkFlattenable> TestImageFilter::CreateProc(SkReadBuffer& buffer) {
|
2014-10-20 21:03:12 +00:00
|
|
|
SK_IMAGEFILTER_UNFLATTEN_COMMON(common, 0);
|
|
|
|
bool visited = buffer.readBool();
|
2021-06-16 19:28:00 +00:00
|
|
|
return TestImageFilter::Make(visited);
|
2014-10-20 21:03:12 +00:00
|
|
|
}
|
|
|
|
|
2020-08-06 18:11:56 +00:00
|
|
|
} // namespace
|
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;
|
2019-01-07 15:00:48 +00:00
|
|
|
auto doc = SkPDF::MakeDocument(&stream);
|
2015-03-01 14:55:20 +00:00
|
|
|
SkCanvas* canvas = doc->beginPage(100.0f, 100.0f);
|
|
|
|
|
2021-06-16 19:28:00 +00:00
|
|
|
sk_sp<TestImageFilter> filter(TestImageFilter::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) {
|
2018-12-21 21:15:01 +00:00
|
|
|
SkNullWStream nullWStream;
|
|
|
|
SkPDFDocument doc(&nullWStream, SkPDF::Metadata());
|
2015-10-12 20:05:04 +00:00
|
|
|
|
|
|
|
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,
|
2018-12-21 21:15:01 +00:00
|
|
|
!SkPDFFont::CanEmbedTypeface(noEmbedTypeface.get(), &doc));
|
2015-10-12 20:05:04 +00:00
|
|
|
}
|
2019-03-20 16:12:10 +00:00
|
|
|
sk_sp<SkTypeface> portableTypeface(ToolUtils::create_portable_typeface(nullptr, SkFontStyle()));
|
2015-10-12 20:05:04 +00:00
|
|
|
REPORTER_ASSERT(reporter,
|
2018-12-21 21:15:01 +00:00
|
|
|
SkPDFFont::CanEmbedTypeface(portableTypeface.get(), &doc));
|
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,
|
2019-01-07 17:57:10 +00:00
|
|
|
const SkFont& font, const uint32_t* clusters,
|
2018-07-23 14:50:49 +00:00
|
|
|
size_t utf8TextByteLength, const char* utf8Text) {
|
2019-01-07 17:57:10 +00:00
|
|
|
return SkGlyphRun(font,
|
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 char>{utf8Text, utf8TextByteLength},
|
2021-04-08 18:34:55 +00:00
|
|
|
SkSpan<const uint32_t>{clusters, len},
|
|
|
|
SkSpan<const SkVector>{});
|
2018-07-23 14:50:49 +00:00
|
|
|
}
|
|
|
|
|
2018-03-28 16:51:00 +00:00
|
|
|
DEF_TEST(SkPDF_Clusterator, reporter) {
|
2019-01-07 17:57:10 +00:00
|
|
|
SkFont font;
|
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";
|
2019-01-07 17:57:10 +00:00
|
|
|
SkGlyphRun run = make_run(len, glyphs, pos, font, clusters, strlen(text), text);
|
2018-07-23 14:50:49 +00:00
|
|
|
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";
|
2019-01-07 17:57:10 +00:00
|
|
|
SkGlyphRun run = make_run(len, glyphs, pos, font, clusters, strlen(text), text);
|
2018-07-23 14:50:49 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-30 16:23:10 +00:00
|
|
|
DEF_TEST(fuzz875632f0, reporter) {
|
|
|
|
SkNullWStream stream;
|
|
|
|
auto doc = SkPDF::MakeDocument(&stream);
|
|
|
|
REPORTER_ASSERT(reporter, doc);
|
|
|
|
SkCanvas* canvas = doc->beginPage(128, 160);
|
|
|
|
|
|
|
|
SkAutoCanvasRestore autoCanvasRestore(canvas, false);
|
|
|
|
|
|
|
|
SkPaint layerPaint({0, 0, 0, 0});
|
2021-01-22 18:01:26 +00:00
|
|
|
layerPaint.setImageFilter(SkImageFilters::Dilate(536870912, 0, nullptr, nullptr));
|
2019-07-30 16:23:10 +00:00
|
|
|
layerPaint.setBlendMode(SkBlendMode::kClear);
|
|
|
|
|
|
|
|
canvas->saveLayer(nullptr, &layerPaint);
|
|
|
|
canvas->saveLayer(nullptr, nullptr);
|
|
|
|
|
|
|
|
SkPaint paint;
|
|
|
|
paint.setBlendMode(SkBlendMode::kDarken);
|
|
|
|
paint.setShader(SkPerlinNoiseShader::MakeFractalNoise(0, 0, 2, 0, nullptr));
|
|
|
|
paint.setColor4f(SkColor4f{0, 0, 0 ,0});
|
|
|
|
|
|
|
|
canvas->drawPath(SkPath(), paint);
|
|
|
|
}
|
2016-12-30 18:09:03 +00:00
|
|
|
#endif
|