From: halcanary Date: Thu, 9 Apr 2015 20:27:40 +0000 (-0700) Subject: SkPDF: ResourceDict replaced by factory function X-Git-Tag: accepted/tizen/5.0/unified/20181102.025319~2820 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=2b86155b42c2493ff0c558ce105a464769962274;p=platform%2Fupstream%2FlibSkiaSharp.git SkPDF: ResourceDict replaced by factory function Motivation: Having a class here was unnecessary, since the only thing that set this class apart was how it is created, not how it behaves. BUG=skia:3585 Review URL: https://codereview.chromium.org/1068343003 --- diff --git a/src/doc/SkDocument_PDF.cpp b/src/doc/SkDocument_PDF.cpp index 0b62e2c..e812cd5 100644 --- a/src/doc/SkDocument_PDF.cpp +++ b/src/doc/SkDocument_PDF.cpp @@ -9,7 +9,6 @@ #include "SkPDFCanon.h" #include "SkPDFDevice.h" #include "SkPDFFont.h" -#include "SkPDFResourceDict.h" #include "SkPDFStream.h" #include "SkPDFTypes.h" #include "SkStream.h" @@ -64,7 +63,7 @@ static void perform_font_subsetting( static SkPDFDict* create_pdf_page(const SkPDFDevice* pageDevice) { SkAutoTUnref page(SkNEW_ARGS(SkPDFDict, ("Page"))); - SkAutoTUnref deviceResourceDict( + SkAutoTUnref deviceResourceDict( pageDevice->createResourceDict()); page->insert("Resources", deviceResourceDict.get()); diff --git a/src/pdf/SkPDFDevice.cpp b/src/pdf/SkPDFDevice.cpp index f624cb4..5167474 100644 --- a/src/pdf/SkPDFDevice.cpp +++ b/src/pdf/SkPDFDevice.cpp @@ -1281,41 +1281,17 @@ void SkPDFDevice::setDrawingArea(DrawingArea drawingArea) { fDrawingArea = drawingArea; } -SkPDFResourceDict* SkPDFDevice::createResourceDict() const { - SkAutoTUnref resourceDict(SkNEW(SkPDFResourceDict)); - if (fGraphicStateResources.count()) { - for (int i = 0; i < fGraphicStateResources.count(); i++) { - resourceDict->insertResourceAsReference( - SkPDFResourceDict::kExtGState_ResourceType, - i, fGraphicStateResources[i]); - } - } - - if (fXObjectResources.count()) { - for (int i = 0; i < fXObjectResources.count(); i++) { - resourceDict->insertResourceAsReference( - SkPDFResourceDict::kXObject_ResourceType, - i, fXObjectResources[i]); - } - } - - if (fFontResources.count()) { - for (int i = 0; i < fFontResources.count(); i++) { - resourceDict->insertResourceAsReference( - SkPDFResourceDict::kFont_ResourceType, - i, fFontResources[i]); - } - } - - if (fShaderResources.count()) { - SkAutoTUnref patterns(new SkPDFDict()); - for (int i = 0; i < fShaderResources.count(); i++) { - resourceDict->insertResourceAsReference( - SkPDFResourceDict::kPattern_ResourceType, - i, fShaderResources[i]); - } - } - return resourceDict.detach(); +SkPDFDict* SkPDFDevice::createResourceDict() const { + SkTDArray fonts; + fonts.setReserve(fFontResources.count()); + for (SkPDFFont* font : fFontResources) { + fonts.push(font); + } + return SkPDFResourceDict::Create( + &fGraphicStateResources, + &fShaderResources, + &fXObjectResources, + &fonts); } const SkTDArray& SkPDFDevice::getFontResources() const { diff --git a/src/pdf/SkPDFDevice.h b/src/pdf/SkPDFDevice.h index fd870fe..a701405 100644 --- a/src/pdf/SkPDFDevice.h +++ b/src/pdf/SkPDFDevice.h @@ -31,7 +31,6 @@ class SkPDFFormXObject; class SkPDFGlyphSetMap; class SkPDFGraphicState; class SkPDFObject; -class SkPDFResourceDict; class SkPDFShader; class SkPDFStream; class SkRRect; @@ -139,7 +138,7 @@ public: /** Create the resource dictionary for this device. */ - SkPDFResourceDict* createResourceDict() const; + SkPDFDict* createResourceDict() const; /** Get the fonts used on this device. */ diff --git a/src/pdf/SkPDFFormXObject.cpp b/src/pdf/SkPDFFormXObject.cpp index 6797eac..3e765c0 100644 --- a/src/pdf/SkPDFFormXObject.cpp +++ b/src/pdf/SkPDFFormXObject.cpp @@ -11,7 +11,6 @@ #include "SkMatrix.h" #include "SkPDFDevice.h" -#include "SkPDFResourceDict.h" #include "SkPDFUtils.h" #include "SkStream.h" #include "SkTypes.h" @@ -20,7 +19,7 @@ SkPDFFormXObject::SkPDFFormXObject(SkPDFDevice* device) { // We don't want to keep around device because we'd have two copies // of content, so reference or copy everything we need (content and // resources). - SkAutoTUnref resourceDict(device->createResourceDict()); + SkAutoTUnref resourceDict(device->createResourceDict()); SkAutoTDelete content(device->content()); this->setData(content.get()); @@ -46,7 +45,7 @@ SkPDFFormXObject::SkPDFFormXObject(SkPDFDevice* device) { * Creates a FormXObject from a content stream and associated resources. */ SkPDFFormXObject::SkPDFFormXObject(SkStream* content, SkRect bbox, - SkPDFResourceDict* resourceDict) { + SkPDFDict* resourceDict) { setData(content); SkAutoTUnref bboxArray(SkPDFUtils::RectToArray(bbox)); diff --git a/src/pdf/SkPDFFormXObject.h b/src/pdf/SkPDFFormXObject.h index 337f64b..4f903f6 100644 --- a/src/pdf/SkPDFFormXObject.h +++ b/src/pdf/SkPDFFormXObject.h @@ -14,7 +14,6 @@ #include "SkPDFTypes.h" #include "SkRect.h" #include "SkRefCnt.h" -#include "SkPDFResourceDict.h" #include "SkString.h" class SkMatrix; @@ -44,7 +43,7 @@ public: */ explicit SkPDFFormXObject(SkStream* content, SkRect bbox, - SkPDFResourceDict* resourceDict); + SkPDFDict* resourceDict); virtual ~SkPDFFormXObject(); private: diff --git a/src/pdf/SkPDFResourceDict.cpp b/src/pdf/SkPDFResourceDict.cpp index 69618de..de5c910 100644 --- a/src/pdf/SkPDFResourceDict.cpp +++ b/src/pdf/SkPDFResourceDict.cpp @@ -51,52 +51,58 @@ static const char* get_resource_type_name( return resource_type_names[type]; } -SkPDFResourceDict::SkPDFResourceDict() : SkPDFDict() { - const char procs[][7] = {"PDF", "Text", "ImageB", "ImageC", "ImageI"}; - SkPDFArray* procSets = SkNEW(SkPDFArray()); - - procSets->reserve(SK_ARRAY_COUNT(procs)); - for (size_t i = 0; i < SK_ARRAY_COUNT(procs); i++) { - procSets->appendName(procs[i]); - } - insert("ProcSets", procSets)->unref(); - - // Actual sub-dicts will be lazily added later - fTypes.setCount(kResourceTypeCount); - for (int i=0; i < kResourceTypeCount; i++) { - fTypes[i] = NULL; - } -} - -SkPDFObject* SkPDFResourceDict::insertResourceAsReference( - SkPDFResourceType type, int key, SkPDFObject* value) { - SkAutoTUnref ref(SkNEW_ARGS(SkPDFObjRef, (value))); - insertResource(type, key, ref); - - return value; -} - SkString SkPDFResourceDict::getResourceName( - SkPDFResourceType type, int key) { + SkPDFResourceDict::SkPDFResourceType type, int key) { SkString keyString; keyString.printf("%c%d", get_resource_type_prefix(type), key); return keyString; } -SkPDFObject* SkPDFResourceDict::insertResource( - SkPDFResourceType type, int key, SkPDFObject* value) { - SkPDFDict* typeDict = fTypes[type]; - if (NULL == typeDict) { - SkAutoTUnref newDict(SkNEW(SkPDFDict())); - SkAutoTUnref typeName( - SkNEW_ARGS(SkPDFName, (get_resource_type_name(type)))); - insert(typeName, newDict); // ref counting handled here - fTypes[type] = newDict; - typeDict = newDict.get(); +static void add_subdict( + const SkTDArray& resourceList, + SkPDFResourceDict::SkPDFResourceType type, + SkPDFDict* dst) { + if (0 == resourceList.count()) { + return; } + SkAutoTUnref resources(SkNEW(SkPDFDict)); + for (int i = 0; i < resourceList.count(); i++) { + SkString keyString = SkPDFResourceDict::getResourceName(type, i); + SkAutoTUnref keyName(SkNEW_ARGS(SkPDFName, (keyString))); + SkAutoTUnref ref( + SkNEW_ARGS(SkPDFObjRef, (resourceList[i]))); + resources->insert(keyName, ref); + } + dst->insert(get_resource_type_name(type), resources); +} + +SkPDFDict* SkPDFResourceDict::Create( + const SkTDArray* gStateResources, + const SkTDArray* patternResources, + const SkTDArray* xObjectResources, + const SkTDArray* fontResources) { + SkAutoTUnref dict(SkNEW(SkPDFDict)); + static const char kProcs[][7] = { + "PDF", "Text", "ImageB", "ImageC", "ImageI"}; + SkAutoTUnref procSets(SkNEW(SkPDFArray)); + + procSets->reserve(SK_ARRAY_COUNT(kProcs)); + for (size_t i = 0; i < SK_ARRAY_COUNT(kProcs); i++) { + procSets->appendName(kProcs[i]); + } + dict->insert("ProcSets", procSets); - SkAutoTUnref keyName( - SkNEW_ARGS(SkPDFName, (getResourceName(type, key)))); - typeDict->insert(keyName, value); - return value; + if (gStateResources) { + add_subdict(*gStateResources, kExtGState_ResourceType, dict); + } + if (patternResources) { + add_subdict(*patternResources, kPattern_ResourceType, dict); + } + if (xObjectResources) { + add_subdict(*xObjectResources, kXObject_ResourceType, dict); + } + if (fontResources) { + add_subdict(*fontResources, kFont_ResourceType, dict); + } + return dict.detach(); } diff --git a/src/pdf/SkPDFResourceDict.h b/src/pdf/SkPDFResourceDict.h index 2217371..2913779 100644 --- a/src/pdf/SkPDFResourceDict.h +++ b/src/pdf/SkPDFResourceDict.h @@ -18,11 +18,9 @@ allows generation of a list of referenced SkPDFObjects inserted with insertResourceAsRef. */ -class SkPDFResourceDict : public SkPDFDict { +class SkPDFResourceDict { public: - SK_DECLARE_INST_COUNT(SkPDFResourceDict) - - enum SkPDFResourceType{ + enum SkPDFResourceType { kExtGState_ResourceType, kPattern_ResourceType, kXObject_ResourceType, @@ -30,28 +28,19 @@ public: // These additional types are defined by the spec, but not // currently used by Skia: ColorSpace, Shading, Properties kResourceTypeCount - }; + }; /** Create a PDF resource dictionary. * The full set of ProcSet entries is automatically created for backwards * compatibility, as recommended by the PDF spec. + * + * Any arguments can be NULL. */ - SkPDFResourceDict(); - - /** Add the value SkPDFObject as a reference to the resource dictionary - * with the give type and key. - * The relevant sub-dicts will be automatically generated, and the - * resource will be named by concatenating a type-specific prefix and - * the input key. - * This object will be part of the resource list when requested later. - * @param type The type of resource being entered, like - * kPattern_ResourceType or kExtGState_ResourceType. - * @param key The resource key, should be unique within its type. - * @param value The resource itself. - * @return The value argument is returned. - */ - SkPDFObject* insertResourceAsReference(SkPDFResourceType type, int key, - SkPDFObject* value); + static SkPDFDict* Create( + const SkTDArray* gStateResources, + const SkTDArray* patternResources, + const SkTDArray* xObjectResources, + const SkTDArray* fontResources); /** * Returns the name for the resource that will be generated by the resource @@ -62,23 +51,6 @@ public: * @param key The resource key, should be unique within its type. */ static SkString getResourceName(SkPDFResourceType type, int key); - -private: - /** Add the value to the dictionary with the given key. Refs value. - * The relevant sub-dicts will be automatically generated, and the - * resource will be named by concatenating a type-specific prefix and - * the input key. - * The object will NOT be part of the resource list when requested later. - * @param type The type of resource being entered. - * @param key The resource key, should be unique within its type. - * @param value The resource itself. - * @return The value argument is returned. - */ - SkPDFObject* insertResource(SkPDFResourceType type, int key, - SkPDFObject* value); - - SkTDArray fTypes; - typedef SkPDFDict INHERITED; }; #endif diff --git a/src/pdf/SkPDFShader.cpp b/src/pdf/SkPDFShader.cpp index 596bbc7..be9798f 100644 --- a/src/pdf/SkPDFShader.cpp +++ b/src/pdf/SkPDFShader.cpp @@ -575,21 +575,18 @@ SkPDFObject* SkPDFShader::GetPDFShader(SkPDFCanon* canon, return get_pdf_shader_by_state(canon, dpi, &state); } -static SkPDFResourceDict* get_gradient_resource_dict( +static SkPDFDict* get_gradient_resource_dict( SkPDFObject* functionShader, SkPDFObject* gState) { - SkPDFResourceDict* dict = new SkPDFResourceDict(); - - if (functionShader != NULL) { - dict->insertResourceAsReference( - SkPDFResourceDict::kPattern_ResourceType, 0, functionShader); + SkTDArray patterns; + if (functionShader) { + patterns.push(functionShader); } - if (gState != NULL) { - dict->insertResourceAsReference( - SkPDFResourceDict::kExtGState_ResourceType, 0, gState); + SkTDArray graphicStates; + if (gState) { + graphicStates.push(gState); } - - return dict; + return SkPDFResourceDict::Create(&graphicStates, &patterns, NULL, NULL); } static void populate_tiling_pattern_dict(SkPDFDict* pattern, @@ -647,7 +644,7 @@ static SkPDFObject* create_smask_graphic_state( SkAutoTDelete alphaStream(create_pattern_fill_content(-1, bbox)); - SkAutoTUnref + SkAutoTUnref resources(get_gradient_resource_dict(luminosityShader, NULL)); SkAutoTUnref alphaMask( @@ -682,7 +679,7 @@ SkPDFAlphaFunctionShader* SkPDFAlphaFunctionShader::Create( SkPDFAlphaFunctionShader* alphaFunctionShader = SkNEW_ARGS(SkPDFAlphaFunctionShader, (autoState->detach())); - SkAutoTUnref resourceDict( + SkAutoTUnref resourceDict( get_gradient_resource_dict(colorShader.get(), alphaGs.get())); SkAutoTDelete colorStream( @@ -1100,7 +1097,7 @@ SkPDFImageShader* SkPDFImageShader::Create( SkNEW_ARGS(SkPDFImageShader, (autoState->detach())); imageShader->setData(content.get()); - SkAutoTUnref resourceDict( + SkAutoTUnref resourceDict( patternDevice->createResourceDict()); populate_tiling_pattern_dict(imageShader, patternBBox, resourceDict.get(), finalMatrix); diff --git a/src/pdf/SkPDFShader.h b/src/pdf/SkPDFShader.h index 51add58..8ebe8a2 100644 --- a/src/pdf/SkPDFShader.h +++ b/src/pdf/SkPDFShader.h @@ -10,7 +10,6 @@ #ifndef SkPDFShader_DEFINED #define SkPDFShader_DEFINED -#include "SkPDFResourceDict.h" #include "SkPDFStream.h" #include "SkPDFTypes.h"