From 9d85145590894e8114056f0bbe24ba76581336c5 Mon Sep 17 00:00:00 2001 From: halcanary Date: Wed, 25 Mar 2015 07:11:08 -0700 Subject: [PATCH] SkPDF: clean up skpdfdocument, add static functions Add SkPDFDocument::EmitPDF and SkPDFDocument::GetCountOfFontTypes Also, make SkPDFDocument::appendPage return void, not bool. Motivation: These static functions can be used to print subsets of a pdf document (functionality to be added in a later CL). BUG=skia:3585 Review URL: https://codereview.chromium.org/1036853002 --- src/pdf/SkPDFDocument.cpp | 24 ++++++++-------- src/pdf/SkPDFDocument.h | 58 +++++++++++++++++++++++++++------------ 2 files changed, 51 insertions(+), 31 deletions(-) diff --git a/src/pdf/SkPDFDocument.cpp b/src/pdf/SkPDFDocument.cpp index 4009cef9b6..57eab950ed 100644 --- a/src/pdf/SkPDFDocument.cpp +++ b/src/pdf/SkPDFDocument.cpp @@ -39,10 +39,6 @@ static void perform_font_subsetting(SkPDFCatalog* catalog, } } -SkPDFDocument::SkPDFDocument() {} - -SkPDFDocument::~SkPDFDocument() { fPageDevices.unrefAll(); } - static void emit_pdf_header(SkWStream* stream) { stream->writeText("%PDF-1.4\n%"); // The PDF spec recommends including a comment with four bytes, all @@ -69,15 +65,16 @@ static void emit_pdf_footer(SkWStream* stream, stream->writeText("\n%%EOF"); } -bool SkPDFDocument::emitPDF(SkWStream* stream) { - // SkTDArray fPageDevices; - if (fPageDevices.isEmpty()) { +bool SkPDFDocument::EmitPDF(const SkTDArray& pageDevices, + SkWStream* stream) { + if (pageDevices.isEmpty()) { return false; } SkTDArray pages; - for (int i = 0; i < fPageDevices.count(); i++) { + for (int i = 0; i < pageDevices.count(); i++) { + SkASSERT(pageDevices[i]); // Reference from new passed to pages. - pages.push(SkNEW_ARGS(SkPDFPage, (fPageDevices[i]))); + pages.push(SkNEW_ARGS(SkPDFPage, (pageDevices[i]))); } SkPDFCatalog catalog; @@ -194,19 +191,20 @@ bool SkPDFDocument::emitPDF(SkWStream* stream) { } // TODO(halcanary): expose notEmbeddableCount in SkDocument -void SkPDFDocument::getCountOfFontTypes( +void SkPDFDocument::GetCountOfFontTypes( + const SkTDArray& pageDevices, int counts[SkAdvancedTypefaceMetrics::kOther_Font + 1], int* notSubsettableCount, - int* notEmbeddableCount) const { + int* notEmbeddableCount) { sk_bzero(counts, sizeof(int) * (SkAdvancedTypefaceMetrics::kOther_Font + 1)); SkTDArray seenFonts; int notSubsettable = 0; int notEmbeddable = 0; - for (int pageNumber = 0; pageNumber < fPageDevices.count(); pageNumber++) { + for (int pageNumber = 0; pageNumber < pageDevices.count(); pageNumber++) { const SkTDArray& fontResources = - fPageDevices[pageNumber]->getFontResources(); + pageDevices[pageNumber]->getFontResources(); for (int font = 0; font < fontResources.count(); font++) { SkFontID fontID = fontResources[font]->typeface()->uniqueID(); if (seenFonts.find(fontID) == -1) { diff --git a/src/pdf/SkPDFDocument.h b/src/pdf/SkPDFDocument.h index e4c0521cbe..fce9f9df83 100644 --- a/src/pdf/SkPDFDocument.h +++ b/src/pdf/SkPDFDocument.h @@ -29,34 +29,56 @@ template class SkTSet; */ class SkPDFDocument { public: - SkPDFDocument(); - ~SkPDFDocument(); + SkPDFDocument() {} + ~SkPDFDocument() { fPageDevices.unrefAll(); } /** Output the PDF to the passed stream. It is an error to call this (it - * will return false and not modify stream) if no pages have been added - * or there are pages missing (i.e. page 1 and 3 have been added, but not - * page 2). + * will return false and not modify stream) if pageDevices is empty. + * No device pointer can be NULL. + * + * @param pageDevices An array of pages, in order. All pages + * should be created using the same SkPDFCanon. + * TODO(halcanary): ASSERT this condition. + * @param SkWStream The writable output stream to send the PDF to. + */ + static bool EmitPDF(const SkTDArray& pageDevices, SkWStream*); + + /** Output the PDF to the passed stream. It is an error to call this (it + * will return false and not modify stream) if no pages have been added. * * @param stream The writable output stream to send the PDF to. */ - bool emitPDF(SkWStream* stream); - - /** Append the passed pdf device to the document as a new page. Returns - * true if successful. Will fail if the document has already been emitted. - * - * @param pdfDevice The page to add to this document. - */ - bool appendPage(SkPDFDevice* pdfDevice) { - fPageDevices.push(SkRef(pdfDevice)); - return true; + bool emitPDF(SkWStream* stream) const { + return SkPDFDocument::EmitPDF(fPageDevices, stream); } + /** Append the passed pdf device to the document as a new page. + * + * @param pdfDevice The page to add to this document. All pages + * added to this document should be created + * using the same SkPDFCanon. + */ + void appendPage(SkPDFDevice* pdfDevice) { + fPageDevices.push(SkRef(pdfDevice)); + } + + /** Get the count of unique font types used in the given pages. + */ + static void GetCountOfFontTypes( + const SkTDArray& pageDevices, + int counts[SkAdvancedTypefaceMetrics::kOther_Font + 1], + int* notSubsettableCount, + int* notEmbedddableCount); + /** Get the count of unique font types used in the document. */ void getCountOfFontTypes( - int counts[SkAdvancedTypefaceMetrics::kOther_Font + 1], - int* notSubsettableCount, - int* notEmbedddableCount) const; + int counts[SkAdvancedTypefaceMetrics::kOther_Font + 1], + int* notSubsettableCount, + int* notEmbedddableCount) const { + return SkPDFDocument::GetCountOfFontTypes( + fPageDevices, counts, notSubsettableCount, notEmbedddableCount); + } private: SkTDArray fPageDevices;