diff options
author | halcanary <halcanary@google.com> | 2015-02-10 13:32:09 -0800 |
---|---|---|
committer | Commit bot <commit-bot@chromium.org> | 2015-02-10 13:32:09 -0800 |
commit | bf799cd228282431e6311900dd383083f8af7164 (patch) | |
tree | 4c5e1bf301d96a16f58998e89ff048cebc7732cc /tests/PDFPrimitivesTest.cpp | |
parent | c8262ccbf988390729ad77734254a342d3c2cb33 (diff) |
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
Diffstat (limited to 'tests/PDFPrimitivesTest.cpp')
-rw-r--r-- | tests/PDFPrimitivesTest.cpp | 54 |
1 files changed, 12 insertions, 42 deletions
diff --git a/tests/PDFPrimitivesTest.cpp b/tests/PDFPrimitivesTest.cpp index 8b1ebe0051..7cc010f7c1 100644 --- a/tests/PDFPrimitivesTest.cpp +++ b/tests/PDFPrimitivesTest.cpp @@ -75,8 +75,8 @@ static void emit_object(SkPDFObject* object, bool indirect) { SkPDFObject* realObject = catalog->getSubstituteObject(object); if (indirect) { - catalog->emitObjectNumber(stream, realObject); - stream->writeText(" obj\n"); + stream->writeDecAsText(catalog->getObjectNumber(object)); + stream->writeText(" 0 obj\n"); // Generation number is always 0. realObject->emitObject(stream, catalog); stream->writeText("\nendobj\n"); } else { @@ -201,18 +201,10 @@ static void TestCatalog(skiatest::Reporter* reporter) { catalog.addObject(int2.get(), false); catalog.addObject(int3.get(), false); - REPORTER_ASSERT(reporter, catalog.getObjectNumberSize(int1.get()) == 3); - REPORTER_ASSERT(reporter, catalog.getObjectNumberSize(int2.get()) == 3); - REPORTER_ASSERT(reporter, catalog.getObjectNumberSize(int3.get()) == 3); - - SkDynamicMemoryWStream buffer; - catalog.emitObjectNumber(&buffer, int1.get()); - catalog.emitObjectNumber(&buffer, int2.get()); - catalog.emitObjectNumber(&buffer, int3.get()); - catalog.emitObjectNumber(&buffer, int1Again.get()); - char expectedResult[] = "1 02 03 01 0"; - REPORTER_ASSERT(reporter, stream_equals(buffer, 0, expectedResult, - strlen(expectedResult))); + 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); } static void TestObjectRef(skiatest::Reporter* reporter) { @@ -223,8 +215,8 @@ static void TestObjectRef(skiatest::Reporter* reporter) { SkPDFCatalog catalog((SkPDFDocument::Flags)0); catalog.addObject(int1.get(), false); catalog.addObject(int2.get(), false); - REPORTER_ASSERT(reporter, catalog.getObjectNumberSize(int1.get()) == 3); - REPORTER_ASSERT(reporter, catalog.getObjectNumberSize(int2.get()) == 3); + REPORTER_ASSERT(reporter, catalog.getObjectNumber(int1.get()) == 1); + REPORTER_ASSERT(reporter, catalog.getObjectNumber(int2.get()) == 2); char expectedResult[] = "2 0 R"; SkDynamicMemoryWStream buffer; @@ -237,38 +229,16 @@ static void TestObjectRef(skiatest::Reporter* reporter) { static void TestSubstitute(skiatest::Reporter* reporter) { SkAutoTUnref<SkPDFTestDict> proxy(new SkPDFTestDict()); SkAutoTUnref<SkPDFTestDict> stub(new SkPDFTestDict()); - SkAutoTUnref<SkPDFInt> int33(new SkPDFInt(33)); - SkAutoTUnref<SkPDFDict> stubResource(new SkPDFDict()); - SkAutoTUnref<SkPDFInt> int44(new SkPDFInt(44)); - stub->insert("Value", int33.get()); - stubResource->insert("InnerValue", int44.get()); - stub->addResource(stubResource.get()); + proxy->insert("Value", new SkPDFInt(33))->unref(); + stub->insert("Value", new SkPDFInt(44))->unref(); SkPDFCatalog catalog((SkPDFDocument::Flags)0); catalog.addObject(proxy.get(), false); catalog.setSubstitute(proxy.get(), stub.get()); - SkDynamicMemoryWStream buffer; - emit_object(proxy, &buffer, &catalog, false); - SkTSet<SkPDFObject*>* substituteResources = - catalog.getSubstituteList(false); - for (int i = 0; i < substituteResources->count(); ++i) { - emit_object((*substituteResources)[i], &buffer, &catalog, true); - } - - char objectResult[] = "2 0 obj\n<</Value 33\n>>\nendobj\n"; - catalog.setFileOffset(proxy.get(), 0); - - size_t outputSize = get_output_size( - catalog.getSubstituteObject(proxy.get()), &catalog, true); - REPORTER_ASSERT(reporter, outputSize == strlen(objectResult)); - - char expectedResult[] = - "<</Value 33\n>>1 0 obj\n<</InnerValue 44\n>>\nendobj\n"; - REPORTER_ASSERT(reporter, buffer.getOffset() == strlen(expectedResult)); - REPORTER_ASSERT(reporter, stream_equals(buffer, 0, expectedResult, - buffer.getOffset())); + REPORTER_ASSERT(reporter, stub.get() == catalog.getSubstituteObject(proxy)); + REPORTER_ASSERT(reporter, proxy.get() != catalog.getSubstituteObject(stub)); } // Create a bitmap that would be very eficiently compressed in a ZIP. |