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
|
|
|
*/
|
|
|
|
|
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"
|
2011-07-20 17:39:01 +00:00
|
|
|
#include "SkFlate.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"
|
2010-09-24 22:25:30 +00:00
|
|
|
#include "SkPDFCatalog.h"
|
2013-01-25 19:27:23 +00:00
|
|
|
#include "SkPDFDevice.h"
|
2010-09-24 22:25:30 +00:00
|
|
|
#include "SkPDFStream.h"
|
|
|
|
#include "SkPDFTypes.h"
|
2014-10-20 21:03:12 +00:00
|
|
|
#include "SkReadBuffer.h"
|
2010-09-24 22:25:30 +00:00
|
|
|
#include "SkScalar.h"
|
|
|
|
#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"
|
2010-09-24 22:25:30 +00:00
|
|
|
|
2013-10-03 19:29:21 +00:00
|
|
|
#define DUMMY_TEXT "DCT compessed stream."
|
|
|
|
|
2011-06-24 19:12:12 +00:00
|
|
|
static bool stream_equals(const SkDynamicMemoryWStream& stream, size_t offset,
|
|
|
|
const void* buffer, size_t len) {
|
|
|
|
SkAutoDataUnref data(stream.copyToData());
|
2012-07-10 17:30:58 +00:00
|
|
|
if (offset + len > data->size()) {
|
2011-06-24 19:12:12 +00:00
|
|
|
return false;
|
|
|
|
}
|
2012-07-10 17:30:58 +00:00
|
|
|
return memcmp(data->bytes() + offset, buffer, len) == 0;
|
2011-06-24 19:12:12 +00:00
|
|
|
}
|
|
|
|
|
2015-01-23 19:45:10 +00:00
|
|
|
static void emit_object(SkPDFObject* object,
|
|
|
|
SkWStream* stream,
|
|
|
|
SkPDFCatalog* catalog,
|
|
|
|
bool indirect) {
|
|
|
|
SkPDFObject* realObject = catalog->getSubstituteObject(object);
|
|
|
|
if (indirect) {
|
Simplify reference management in SkPDF
Prior to this change, SkPDFObject subclasses were required
to track their resources separately from the document
structure. (An object has a resource if it depends, via an
indirect reference, on another object). This led to a lot
of extra code to duplicate effort. I replace the
getResources() function with the much simpler addResources()
function. I only define a non-trivial addResources() method
on arrays, dictionaries, and indirect object references.
All other specialized classes simply rely on their parent
class's implementation.
SkPDFObject::addResources() works by recursively walking the
directed graph of object (direct and indirect) references
and adding resources to a set. It doesn't matter that there
are closed loops in the graph, since we check the set before
walking down a branch.
- Add SkPDFObject::addResources() virtual function, with
four implementations
- Remove SkPDFObject::getResources() virtual function and
all implementations.
- Remove SkPDFObject::GetResourcesHelper()
- Remove SkPDFObject::AddResourceHelper()
- In SkPDFCatalog::findObjectIndex(), add an object to the
catalog if it doesn't exist yet.
- SkPDFCatalog::setSubstitute() no longer sets up resources
- SkPDFDocument.cpp no longer needs the Streamer object
- SkPDFDocument.cpp calls fDocCatalog->addResources to build
the resource list.
- SkPDFFont::addResource() removed
- All SkPDF-::fResource sets removed (they are redundant).
- removed SkPDFImage::addSMask() function
- SkPDFResourceDict::getReferencedResources() removed.
Motivation: this removes quite a bit of code and makes the
objects slightly slimmer in memory. Most importantly, this
will lead the way towards removing SkPDFObject's inheritance
from SkRefCnt, which will greatly simplify everything.
Testing: I usually test changes to the PDF backend by
comparing checksums of PDF files rendered from GMs and SKPs
before and after the change. This change both re-orders and
re-numbers the indirect PDF objects. I used the qpdf
program to normalize the PDFs and then compared the
normalized outputs from before and after the change; they
matched.
Review URL: https://codereview.chromium.org/870333002
2015-02-10 21:32:09 +00:00
|
|
|
stream->writeDecAsText(catalog->getObjectNumber(object));
|
|
|
|
stream->writeText(" 0 obj\n"); // Generation number is always 0.
|
2015-01-23 19:45:10 +00:00
|
|
|
realObject->emitObject(stream, catalog);
|
|
|
|
stream->writeText("\nendobj\n");
|
|
|
|
} else {
|
|
|
|
realObject->emitObject(stream, catalog);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static size_t get_output_size(SkPDFObject* object,
|
|
|
|
SkPDFCatalog* catalog,
|
|
|
|
bool indirect) {
|
|
|
|
SkDynamicMemoryWStream buffer;
|
|
|
|
emit_object(object, &buffer, catalog, indirect);
|
|
|
|
return buffer.getOffset();
|
|
|
|
}
|
|
|
|
|
2010-09-24 22:25:30 +00:00
|
|
|
static void CheckObjectOutput(skiatest::Reporter* reporter, SkPDFObject* obj,
|
2011-07-20 17:39:01 +00:00
|
|
|
const char* expectedData, size_t expectedSize,
|
2015-02-19 16:29:24 +00:00
|
|
|
bool indirect) {
|
|
|
|
SkPDFCatalog catalog;
|
2015-01-23 19:45:10 +00:00
|
|
|
size_t directSize = get_output_size(obj, &catalog, false);
|
2011-07-20 17:39:01 +00:00
|
|
|
REPORTER_ASSERT(reporter, directSize == expectedSize);
|
2010-09-24 22:25:30 +00:00
|
|
|
|
|
|
|
SkDynamicMemoryWStream buffer;
|
2015-01-23 19:45:10 +00:00
|
|
|
emit_object(obj, &buffer, &catalog, false);
|
2010-09-24 22:25:30 +00:00
|
|
|
REPORTER_ASSERT(reporter, directSize == buffer.getOffset());
|
2011-07-20 17:39:01 +00:00
|
|
|
REPORTER_ASSERT(reporter, stream_equals(buffer, 0, expectedData,
|
2011-06-24 19:12:12 +00:00
|
|
|
directSize));
|
2010-09-24 22:25:30 +00:00
|
|
|
|
|
|
|
if (indirect) {
|
|
|
|
// Indirect output.
|
|
|
|
static char header[] = "1 0 obj\n";
|
|
|
|
static size_t headerLen = strlen(header);
|
|
|
|
static char footer[] = "\nendobj\n";
|
|
|
|
static size_t footerLen = strlen(footer);
|
|
|
|
|
2015-03-25 18:29:18 +00:00
|
|
|
catalog.addObject(obj);
|
2010-09-24 22:25:30 +00:00
|
|
|
|
2015-01-23 19:45:10 +00:00
|
|
|
size_t indirectSize = get_output_size(obj, &catalog, true);
|
2010-09-24 22:25:30 +00:00
|
|
|
REPORTER_ASSERT(reporter,
|
|
|
|
indirectSize == directSize + headerLen + footerLen);
|
|
|
|
|
|
|
|
buffer.reset();
|
2015-01-23 19:45:10 +00:00
|
|
|
emit_object(obj, &buffer, &catalog, true);
|
2010-09-24 22:25:30 +00:00
|
|
|
REPORTER_ASSERT(reporter, indirectSize == buffer.getOffset());
|
2011-06-24 19:12:12 +00:00
|
|
|
REPORTER_ASSERT(reporter, stream_equals(buffer, 0, header, headerLen));
|
2011-07-20 17:39:01 +00:00
|
|
|
REPORTER_ASSERT(reporter, stream_equals(buffer, headerLen, expectedData,
|
2011-06-24 19:12:12 +00:00
|
|
|
directSize));
|
|
|
|
REPORTER_ASSERT(reporter, stream_equals(buffer, headerLen + directSize,
|
|
|
|
footer, footerLen));
|
2010-09-24 22:25:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-07-20 17:39:01 +00:00
|
|
|
static void SimpleCheckObjectOutput(skiatest::Reporter* reporter,
|
|
|
|
SkPDFObject* obj,
|
2012-05-14 16:33:36 +00:00
|
|
|
const char* expectedResult) {
|
|
|
|
CheckObjectOutput(reporter, obj, expectedResult,
|
2015-02-19 16:29:24 +00:00
|
|
|
strlen(expectedResult), true);
|
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));
|
|
|
|
SkAutoTUnref<SkPDFStream> stream(new SkPDFStream(streamData.get()));
|
2011-07-20 17:39:01 +00:00
|
|
|
SimpleCheckObjectOutput(
|
|
|
|
reporter, stream.get(),
|
|
|
|
"<</Length 12\n>> stream\nTest\nFoo\tBar\nendstream");
|
|
|
|
stream->insert("Attribute", new SkPDFInt(42))->unref();
|
|
|
|
SimpleCheckObjectOutput(reporter, stream.get(),
|
|
|
|
"<</Length 12\n/Attribute 42\n>> stream\n"
|
|
|
|
"Test\nFoo\tBar\nendstream");
|
|
|
|
|
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)));
|
2013-01-04 19:31:24 +00:00
|
|
|
SkAutoTUnref<SkPDFStream> stream(new SkPDFStream(streamData2.get()));
|
2011-07-20 17:39:01 +00:00
|
|
|
|
|
|
|
SkDynamicMemoryWStream compressedByteStream;
|
|
|
|
SkFlate::Deflate(streamData2.get(), &compressedByteStream);
|
|
|
|
SkAutoDataUnref compressedData(compressedByteStream.copyToData());
|
|
|
|
|
2015-02-19 16:29:24 +00:00
|
|
|
SkDynamicMemoryWStream expected;
|
|
|
|
expected.writeText("<</Filter /FlateDecode\n/Length 116\n"
|
2011-07-20 17:39:01 +00:00
|
|
|
">> stream\n");
|
2015-02-19 16:29:24 +00:00
|
|
|
expected.write(compressedData->data(), compressedData->size());
|
|
|
|
expected.writeText("\nendstream");
|
|
|
|
SkAutoDataUnref expectedResultData2(expected.copyToData());
|
2011-07-20 17:39:01 +00:00
|
|
|
CheckObjectOutput(reporter, stream.get(),
|
2012-07-10 17:30:58 +00:00
|
|
|
(const char*) expectedResultData2->data(),
|
2015-02-19 16:29:24 +00:00
|
|
|
expectedResultData2->size(), true);
|
2011-07-20 17:39:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-09-24 22:25:30 +00:00
|
|
|
static void TestCatalog(skiatest::Reporter* reporter) {
|
2015-02-19 16:29:24 +00:00
|
|
|
SkPDFCatalog catalog;
|
2013-01-04 19:31:24 +00:00
|
|
|
SkAutoTUnref<SkPDFInt> int1(new SkPDFInt(1));
|
|
|
|
SkAutoTUnref<SkPDFInt> int2(new SkPDFInt(2));
|
|
|
|
SkAutoTUnref<SkPDFInt> int3(new SkPDFInt(3));
|
|
|
|
int1.get()->ref();
|
|
|
|
SkAutoTUnref<SkPDFInt> int1Again(int1.get());
|
2010-09-24 22:25:30 +00:00
|
|
|
|
2015-03-25 18:29:18 +00:00
|
|
|
catalog.addObject(int1.get());
|
|
|
|
catalog.addObject(int2.get());
|
|
|
|
catalog.addObject(int3.get());
|
2010-09-24 22:25:30 +00:00
|
|
|
|
Simplify reference management in SkPDF
Prior to this change, SkPDFObject subclasses were required
to track their resources separately from the document
structure. (An object has a resource if it depends, via an
indirect reference, on another object). This led to a lot
of extra code to duplicate effort. I replace the
getResources() function with the much simpler addResources()
function. I only define a non-trivial addResources() method
on arrays, dictionaries, and indirect object references.
All other specialized classes simply rely on their parent
class's implementation.
SkPDFObject::addResources() works by recursively walking the
directed graph of object (direct and indirect) references
and adding resources to a set. It doesn't matter that there
are closed loops in the graph, since we check the set before
walking down a branch.
- Add SkPDFObject::addResources() virtual function, with
four implementations
- Remove SkPDFObject::getResources() virtual function and
all implementations.
- Remove SkPDFObject::GetResourcesHelper()
- Remove SkPDFObject::AddResourceHelper()
- In SkPDFCatalog::findObjectIndex(), add an object to the
catalog if it doesn't exist yet.
- SkPDFCatalog::setSubstitute() no longer sets up resources
- SkPDFDocument.cpp no longer needs the Streamer object
- SkPDFDocument.cpp calls fDocCatalog->addResources to build
the resource list.
- SkPDFFont::addResource() removed
- All SkPDF-::fResource sets removed (they are redundant).
- removed SkPDFImage::addSMask() function
- SkPDFResourceDict::getReferencedResources() removed.
Motivation: this removes quite a bit of code and makes the
objects slightly slimmer in memory. Most importantly, this
will lead the way towards removing SkPDFObject's inheritance
from SkRefCnt, which will greatly simplify everything.
Testing: I usually test changes to the PDF backend by
comparing checksums of PDF files rendered from GMs and SKPs
before and after the change. This change both re-orders and
re-numbers the indirect PDF objects. I used the qpdf
program to normalize the PDFs and then compared the
normalized outputs from before and after the change; they
matched.
Review URL: https://codereview.chromium.org/870333002
2015-02-10 21:32:09 +00:00
|
|
|
REPORTER_ASSERT(reporter, catalog.getObjectNumber(int1.get()) == 1);
|
|
|
|
REPORTER_ASSERT(reporter, catalog.getObjectNumber(int2.get()) == 2);
|
|
|
|
REPORTER_ASSERT(reporter, catalog.getObjectNumber(int3.get()) == 3);
|
|
|
|
REPORTER_ASSERT(reporter, catalog.getObjectNumber(int1Again.get()) == 1);
|
2010-09-24 22:25:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void TestObjectRef(skiatest::Reporter* reporter) {
|
2013-01-04 19:31:24 +00:00
|
|
|
SkAutoTUnref<SkPDFInt> int1(new SkPDFInt(1));
|
|
|
|
SkAutoTUnref<SkPDFInt> int2(new SkPDFInt(2));
|
|
|
|
SkAutoTUnref<SkPDFObjRef> int2ref(new SkPDFObjRef(int2.get()));
|
2010-09-24 22:25:30 +00:00
|
|
|
|
2015-02-19 16:29:24 +00:00
|
|
|
SkPDFCatalog catalog;
|
2015-03-25 18:29:18 +00:00
|
|
|
catalog.addObject(int1.get());
|
|
|
|
catalog.addObject(int2.get());
|
Simplify reference management in SkPDF
Prior to this change, SkPDFObject subclasses were required
to track their resources separately from the document
structure. (An object has a resource if it depends, via an
indirect reference, on another object). This led to a lot
of extra code to duplicate effort. I replace the
getResources() function with the much simpler addResources()
function. I only define a non-trivial addResources() method
on arrays, dictionaries, and indirect object references.
All other specialized classes simply rely on their parent
class's implementation.
SkPDFObject::addResources() works by recursively walking the
directed graph of object (direct and indirect) references
and adding resources to a set. It doesn't matter that there
are closed loops in the graph, since we check the set before
walking down a branch.
- Add SkPDFObject::addResources() virtual function, with
four implementations
- Remove SkPDFObject::getResources() virtual function and
all implementations.
- Remove SkPDFObject::GetResourcesHelper()
- Remove SkPDFObject::AddResourceHelper()
- In SkPDFCatalog::findObjectIndex(), add an object to the
catalog if it doesn't exist yet.
- SkPDFCatalog::setSubstitute() no longer sets up resources
- SkPDFDocument.cpp no longer needs the Streamer object
- SkPDFDocument.cpp calls fDocCatalog->addResources to build
the resource list.
- SkPDFFont::addResource() removed
- All SkPDF-::fResource sets removed (they are redundant).
- removed SkPDFImage::addSMask() function
- SkPDFResourceDict::getReferencedResources() removed.
Motivation: this removes quite a bit of code and makes the
objects slightly slimmer in memory. Most importantly, this
will lead the way towards removing SkPDFObject's inheritance
from SkRefCnt, which will greatly simplify everything.
Testing: I usually test changes to the PDF backend by
comparing checksums of PDF files rendered from GMs and SKPs
before and after the change. This change both re-orders and
re-numbers the indirect PDF objects. I used the qpdf
program to normalize the PDFs and then compared the
normalized outputs from before and after the change; they
matched.
Review URL: https://codereview.chromium.org/870333002
2015-02-10 21:32:09 +00:00
|
|
|
REPORTER_ASSERT(reporter, catalog.getObjectNumber(int1.get()) == 1);
|
|
|
|
REPORTER_ASSERT(reporter, catalog.getObjectNumber(int2.get()) == 2);
|
2010-09-24 22:25:30 +00:00
|
|
|
|
|
|
|
char expectedResult[] = "2 0 R";
|
|
|
|
SkDynamicMemoryWStream buffer;
|
2015-01-12 18:07:50 +00:00
|
|
|
int2ref->emitObject(&buffer, &catalog);
|
2010-09-24 22:25:30 +00:00
|
|
|
REPORTER_ASSERT(reporter, buffer.getOffset() == strlen(expectedResult));
|
2011-06-24 19:12:12 +00:00
|
|
|
REPORTER_ASSERT(reporter, stream_equals(buffer, 0, expectedResult,
|
|
|
|
buffer.getOffset()));
|
2010-09-24 22:25:30 +00:00
|
|
|
}
|
|
|
|
|
2011-07-06 23:31:24 +00:00
|
|
|
static void TestSubstitute(skiatest::Reporter* reporter) {
|
2015-03-24 22:29:39 +00:00
|
|
|
SkAutoTUnref<SkPDFDict> proxy(new SkPDFDict());
|
|
|
|
SkAutoTUnref<SkPDFDict> stub(new SkPDFDict());
|
2011-07-06 23:31:24 +00:00
|
|
|
|
Simplify reference management in SkPDF
Prior to this change, SkPDFObject subclasses were required
to track their resources separately from the document
structure. (An object has a resource if it depends, via an
indirect reference, on another object). This led to a lot
of extra code to duplicate effort. I replace the
getResources() function with the much simpler addResources()
function. I only define a non-trivial addResources() method
on arrays, dictionaries, and indirect object references.
All other specialized classes simply rely on their parent
class's implementation.
SkPDFObject::addResources() works by recursively walking the
directed graph of object (direct and indirect) references
and adding resources to a set. It doesn't matter that there
are closed loops in the graph, since we check the set before
walking down a branch.
- Add SkPDFObject::addResources() virtual function, with
four implementations
- Remove SkPDFObject::getResources() virtual function and
all implementations.
- Remove SkPDFObject::GetResourcesHelper()
- Remove SkPDFObject::AddResourceHelper()
- In SkPDFCatalog::findObjectIndex(), add an object to the
catalog if it doesn't exist yet.
- SkPDFCatalog::setSubstitute() no longer sets up resources
- SkPDFDocument.cpp no longer needs the Streamer object
- SkPDFDocument.cpp calls fDocCatalog->addResources to build
the resource list.
- SkPDFFont::addResource() removed
- All SkPDF-::fResource sets removed (they are redundant).
- removed SkPDFImage::addSMask() function
- SkPDFResourceDict::getReferencedResources() removed.
Motivation: this removes quite a bit of code and makes the
objects slightly slimmer in memory. Most importantly, this
will lead the way towards removing SkPDFObject's inheritance
from SkRefCnt, which will greatly simplify everything.
Testing: I usually test changes to the PDF backend by
comparing checksums of PDF files rendered from GMs and SKPs
before and after the change. This change both re-orders and
re-numbers the indirect PDF objects. I used the qpdf
program to normalize the PDFs and then compared the
normalized outputs from before and after the change; they
matched.
Review URL: https://codereview.chromium.org/870333002
2015-02-10 21:32:09 +00:00
|
|
|
proxy->insert("Value", new SkPDFInt(33))->unref();
|
|
|
|
stub->insert("Value", new SkPDFInt(44))->unref();
|
2011-07-06 23:31:24 +00:00
|
|
|
|
2015-02-19 16:29:24 +00:00
|
|
|
SkPDFCatalog catalog;
|
2015-03-25 18:29:18 +00:00
|
|
|
catalog.addObject(proxy.get());
|
2011-07-06 23:31:24 +00:00
|
|
|
catalog.setSubstitute(proxy.get(), stub.get());
|
|
|
|
|
Simplify reference management in SkPDF
Prior to this change, SkPDFObject subclasses were required
to track their resources separately from the document
structure. (An object has a resource if it depends, via an
indirect reference, on another object). This led to a lot
of extra code to duplicate effort. I replace the
getResources() function with the much simpler addResources()
function. I only define a non-trivial addResources() method
on arrays, dictionaries, and indirect object references.
All other specialized classes simply rely on their parent
class's implementation.
SkPDFObject::addResources() works by recursively walking the
directed graph of object (direct and indirect) references
and adding resources to a set. It doesn't matter that there
are closed loops in the graph, since we check the set before
walking down a branch.
- Add SkPDFObject::addResources() virtual function, with
four implementations
- Remove SkPDFObject::getResources() virtual function and
all implementations.
- Remove SkPDFObject::GetResourcesHelper()
- Remove SkPDFObject::AddResourceHelper()
- In SkPDFCatalog::findObjectIndex(), add an object to the
catalog if it doesn't exist yet.
- SkPDFCatalog::setSubstitute() no longer sets up resources
- SkPDFDocument.cpp no longer needs the Streamer object
- SkPDFDocument.cpp calls fDocCatalog->addResources to build
the resource list.
- SkPDFFont::addResource() removed
- All SkPDF-::fResource sets removed (they are redundant).
- removed SkPDFImage::addSMask() function
- SkPDFResourceDict::getReferencedResources() removed.
Motivation: this removes quite a bit of code and makes the
objects slightly slimmer in memory. Most importantly, this
will lead the way towards removing SkPDFObject's inheritance
from SkRefCnt, which will greatly simplify everything.
Testing: I usually test changes to the PDF backend by
comparing checksums of PDF files rendered from GMs and SKPs
before and after the change. This change both re-orders and
re-numbers the indirect PDF objects. I used the qpdf
program to normalize the PDFs and then compared the
normalized outputs from before and after the change; they
matched.
Review URL: https://codereview.chromium.org/870333002
2015-02-10 21:32:09 +00:00
|
|
|
REPORTER_ASSERT(reporter, stub.get() == catalog.getSubstituteObject(proxy));
|
|
|
|
REPORTER_ASSERT(reporter, proxy.get() != catalog.getSubstituteObject(stub));
|
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;
|
|
|
|
SkAutoTUnref<SkDocument> doc(SkDocument::CreatePDF(&outStream));
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2013-12-12 21:11:12 +00:00
|
|
|
DEF_TEST(PDFPrimitives, reporter) {
|
2013-01-04 19:31:24 +00:00
|
|
|
SkAutoTUnref<SkPDFInt> int42(new SkPDFInt(42));
|
2011-07-20 17:39:01 +00:00
|
|
|
SimpleCheckObjectOutput(reporter, int42.get(), "42");
|
2010-09-24 22:25:30 +00:00
|
|
|
|
2013-01-04 19:31:24 +00:00
|
|
|
SkAutoTUnref<SkPDFScalar> realHalf(new SkPDFScalar(SK_ScalarHalf));
|
2011-07-20 17:39:01 +00:00
|
|
|
SimpleCheckObjectOutput(reporter, realHalf.get(), "0.5");
|
2010-09-24 22:25:30 +00:00
|
|
|
|
2013-01-04 19:31:24 +00:00
|
|
|
SkAutoTUnref<SkPDFScalar> bigScalar(new SkPDFScalar(110999.75f));
|
2011-05-18 17:08:05 +00:00
|
|
|
#if !defined(SK_ALLOW_LARGE_PDF_SCALARS)
|
2011-07-20 17:39:01 +00:00
|
|
|
SimpleCheckObjectOutput(reporter, bigScalar.get(), "111000");
|
2011-03-04 03:15:13 +00:00
|
|
|
#else
|
2011-07-20 17:39:01 +00:00
|
|
|
SimpleCheckObjectOutput(reporter, bigScalar.get(), "110999.75");
|
2011-03-04 03:15:13 +00:00
|
|
|
|
2013-01-04 19:31:24 +00:00
|
|
|
SkAutoTUnref<SkPDFScalar> biggerScalar(new SkPDFScalar(50000000.1));
|
2011-07-20 17:39:01 +00:00
|
|
|
SimpleCheckObjectOutput(reporter, biggerScalar.get(), "50000000");
|
2011-03-04 03:15:13 +00:00
|
|
|
|
2013-01-04 19:31:24 +00:00
|
|
|
SkAutoTUnref<SkPDFScalar> smallestScalar(new SkPDFScalar(1.0/65536));
|
2011-07-20 17:39:01 +00:00
|
|
|
SimpleCheckObjectOutput(reporter, smallestScalar.get(), "0.00001526");
|
2011-03-04 03:15:13 +00:00
|
|
|
#endif
|
|
|
|
|
2013-01-04 19:31:24 +00:00
|
|
|
SkAutoTUnref<SkPDFString> stringSimple(
|
|
|
|
new SkPDFString("test ) string ( foo"));
|
2011-07-20 17:39:01 +00:00
|
|
|
SimpleCheckObjectOutput(reporter, stringSimple.get(),
|
|
|
|
"(test \\) string \\( foo)");
|
2013-01-04 19:31:24 +00:00
|
|
|
SkAutoTUnref<SkPDFString> stringComplex(
|
|
|
|
new SkPDFString("\ttest ) string ( foo"));
|
2011-07-20 17:39:01 +00:00
|
|
|
SimpleCheckObjectOutput(reporter, stringComplex.get(),
|
|
|
|
"<0974657374202920737472696E67202820666F6F>");
|
2010-09-24 22:25:30 +00:00
|
|
|
|
2013-01-04 19:31:24 +00:00
|
|
|
SkAutoTUnref<SkPDFName> name(new SkPDFName("Test name\twith#tab"));
|
2011-07-20 17:39:01 +00:00
|
|
|
const char expectedResult[] = "/Test#20name#09with#23tab";
|
|
|
|
CheckObjectOutput(reporter, name.get(), expectedResult,
|
2015-02-19 16:29:24 +00:00
|
|
|
strlen(expectedResult), false);
|
2010-09-24 22:25:30 +00:00
|
|
|
|
2013-01-04 19:31:24 +00:00
|
|
|
SkAutoTUnref<SkPDFName> escapedName(new SkPDFName("A#/%()<>[]{}B"));
|
2012-09-21 17:50:50 +00:00
|
|
|
const char escapedNameExpected[] = "/A#23#2F#25#28#29#3C#3E#5B#5D#7B#7DB";
|
|
|
|
CheckObjectOutput(reporter, escapedName.get(), escapedNameExpected,
|
2015-02-19 16:29:24 +00:00
|
|
|
strlen(escapedNameExpected), false);
|
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.
|
2012-03-19 21:04:52 +00:00
|
|
|
const unsigned char highBitCString[] = {0xDE, 0xAD, 'b', 'e', 0xEF, 0};
|
2013-01-04 19:31:24 +00:00
|
|
|
SkAutoTUnref<SkPDFName> highBitName(
|
|
|
|
new SkPDFName((const char*)highBitCString));
|
2012-03-05 18:44:33 +00:00
|
|
|
const char highBitExpectedResult[] = "/#DE#ADbe#EF";
|
|
|
|
CheckObjectOutput(reporter, highBitName.get(), highBitExpectedResult,
|
2015-02-19 16:29:24 +00:00
|
|
|
strlen(highBitExpectedResult), false);
|
2012-03-05 18:44:33 +00:00
|
|
|
|
2013-01-04 19:31:24 +00:00
|
|
|
SkAutoTUnref<SkPDFArray> array(new SkPDFArray);
|
2011-07-20 17:39:01 +00:00
|
|
|
SimpleCheckObjectOutput(reporter, array.get(), "[]");
|
2010-09-24 22:25:30 +00:00
|
|
|
array->append(int42.get());
|
2011-07-20 17:39:01 +00:00
|
|
|
SimpleCheckObjectOutput(reporter, array.get(), "[42]");
|
2010-09-24 22:25:30 +00:00
|
|
|
array->append(realHalf.get());
|
2011-07-20 17:39:01 +00:00
|
|
|
SimpleCheckObjectOutput(reporter, array.get(), "[42 0.5]");
|
2013-01-04 19:31:24 +00:00
|
|
|
SkAutoTUnref<SkPDFInt> int0(new SkPDFInt(0));
|
2010-09-24 22:25:30 +00:00
|
|
|
array->append(int0.get());
|
2011-07-20 17:39:01 +00:00
|
|
|
SimpleCheckObjectOutput(reporter, array.get(), "[42 0.5 0]");
|
2013-01-04 19:31:24 +00:00
|
|
|
SkAutoTUnref<SkPDFInt> int1(new SkPDFInt(1));
|
2010-09-24 22:25:30 +00:00
|
|
|
array->setAt(0, int1.get());
|
2011-07-20 17:39:01 +00:00
|
|
|
SimpleCheckObjectOutput(reporter, array.get(), "[1 0.5 0]");
|
2010-09-24 22:25:30 +00:00
|
|
|
|
2013-01-04 19:31:24 +00:00
|
|
|
SkAutoTUnref<SkPDFDict> dict(new SkPDFDict);
|
2011-07-20 17:39:01 +00:00
|
|
|
SimpleCheckObjectOutput(reporter, dict.get(), "<<>>");
|
2013-01-04 19:31:24 +00:00
|
|
|
SkAutoTUnref<SkPDFName> n1(new SkPDFName("n1"));
|
2010-09-24 22:25:30 +00:00
|
|
|
dict->insert(n1.get(), int42.get());
|
2011-07-20 17:39:01 +00:00
|
|
|
SimpleCheckObjectOutput(reporter, dict.get(), "<</n1 42\n>>");
|
2013-01-04 19:31:24 +00:00
|
|
|
SkAutoTUnref<SkPDFName> n2(new SkPDFName("n2"));
|
|
|
|
SkAutoTUnref<SkPDFName> n3(new SkPDFName("n3"));
|
2010-09-24 22:25:30 +00:00
|
|
|
dict->insert(n2.get(), realHalf.get());
|
|
|
|
dict->insert(n3.get(), array.get());
|
2011-07-20 17:39:01 +00:00
|
|
|
SimpleCheckObjectOutput(reporter, dict.get(),
|
|
|
|
"<</n1 42\n/n2 0.5\n/n3 [1 0.5 0]\n>>");
|
2010-09-24 22:25:30 +00:00
|
|
|
|
2011-07-20 17:39:01 +00:00
|
|
|
TestPDFStream(reporter);
|
2010-09-24 22:25:30 +00:00
|
|
|
|
|
|
|
TestCatalog(reporter);
|
|
|
|
|
|
|
|
TestObjectRef(reporter);
|
2011-07-06 23:31:24 +00:00
|
|
|
|
|
|
|
TestSubstitute(reporter);
|
2013-01-25 19:27:23 +00:00
|
|
|
|
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:
|
|
|
|
DummyImageFilter(bool visited = false) : SkImageFilter(0, NULL), fVisited(visited) {}
|
2015-03-26 01:17:31 +00:00
|
|
|
~DummyImageFilter() override {}
|
2014-10-20 21:03:12 +00:00
|
|
|
virtual bool onFilterImage(Proxy*, const SkBitmap& src, const Context&,
|
2015-03-26 01:17:31 +00:00
|
|
|
SkBitmap* result, SkIPoint* offset) const override {
|
2014-10-20 21:03:12 +00:00
|
|
|
fVisited = true;
|
|
|
|
offset->fX = offset->fY = 0;
|
|
|
|
*result = src;
|
|
|
|
return true;
|
|
|
|
}
|
2014-12-19 21:49:15 +00:00
|
|
|
SK_TO_STRING_OVERRIDE()
|
2014-10-20 21:03:12 +00:00
|
|
|
SK_DECLARE_PUBLIC_FLATTENABLE_DESERIALIZATION_PROCS(DummyImageFilter)
|
|
|
|
bool visited() const { return fVisited; }
|
|
|
|
|
|
|
|
private:
|
|
|
|
mutable bool fVisited;
|
|
|
|
};
|
|
|
|
|
|
|
|
SkFlattenable* DummyImageFilter::CreateProc(SkReadBuffer& buffer) {
|
|
|
|
SK_IMAGEFILTER_UNFLATTEN_COMMON(common, 0);
|
|
|
|
bool visited = buffer.readBool();
|
|
|
|
return SkNEW_ARGS(DummyImageFilter, (visited));
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
SkAutoTUnref<SkDocument> doc(SkDocument::CreatePDF(&stream));
|
|
|
|
SkCanvas* canvas = doc->beginPage(100.0f, 100.0f);
|
|
|
|
|
2014-10-20 21:03:12 +00:00
|
|
|
SkAutoTUnref<DummyImageFilter> filter(new DummyImageFilter());
|
|
|
|
|
|
|
|
// 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());
|
|
|
|
}
|