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 SkPDFDict* create_pdf_page(const SkPDFDevice* pageDevice) {
SkAutoTUnref<SkPDFDict> page(SkNEW_ARGS(SkPDFDict, ("Page")));
- SkAutoTUnref<SkPDFResourceDict> deviceResourceDict(
+ SkAutoTUnref<SkPDFDict> 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 @@
fDrawingArea = drawingArea;
}
-SkPDFResourceDict* SkPDFDevice::createResourceDict() const {
- SkAutoTUnref<SkPDFResourceDict> resourceDict(SkNEW(SkPDFResourceDict));
- if (fGraphicStateResources.count()) {
- for (int i = 0; i < fGraphicStateResources.count(); i++) {
- resourceDict->insertResourceAsReference(
- SkPDFResourceDict::kExtGState_ResourceType,
- i, fGraphicStateResources[i]);
- }
+SkPDFDict* SkPDFDevice::createResourceDict() const {
+ SkTDArray<SkPDFObject*> fonts;
+ fonts.setReserve(fFontResources.count());
+ for (SkPDFFont* font : fFontResources) {
+ fonts.push(font);
}
-
- 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<SkPDFDict> patterns(new SkPDFDict());
- for (int i = 0; i < fShaderResources.count(); i++) {
- resourceDict->insertResourceAsReference(
- SkPDFResourceDict::kPattern_ResourceType,
- i, fShaderResources[i]);
- }
- }
- return resourceDict.detach();
+ return SkPDFResourceDict::Create(
+ &fGraphicStateResources,
+ &fShaderResources,
+ &fXObjectResources,
+ &fonts);
}
const SkTDArray<SkPDFFont*>& 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 SkPDFGlyphSetMap;
class SkPDFGraphicState;
class SkPDFObject;
-class SkPDFResourceDict;
class SkPDFShader;
class SkPDFStream;
class SkRRect;
@@ -139,7 +138,7 @@
/** 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 @@
// 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<SkPDFResourceDict> resourceDict(device->createResourceDict());
+ SkAutoTUnref<SkPDFDict> resourceDict(device->createResourceDict());
SkAutoTDelete<SkStreamAsset> content(device->content());
this->setData(content.get());
@@ -46,7 +45,7 @@
* Creates a FormXObject from a content stream and associated resources.
*/
SkPDFFormXObject::SkPDFFormXObject(SkStream* content, SkRect bbox,
- SkPDFResourceDict* resourceDict) {
+ SkPDFDict* resourceDict) {
setData(content);
SkAutoTUnref<SkPDFArray> 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 @@
*/
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 @@
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<SkPDFObjRef> 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<SkPDFDict> newDict(SkNEW(SkPDFDict()));
- SkAutoTUnref<SkPDFName> 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<SkPDFObject*>& resourceList,
+ SkPDFResourceDict::SkPDFResourceType type,
+ SkPDFDict* dst) {
+ if (0 == resourceList.count()) {
+ return;
}
+ SkAutoTUnref<SkPDFDict> resources(SkNEW(SkPDFDict));
+ for (int i = 0; i < resourceList.count(); i++) {
+ SkString keyString = SkPDFResourceDict::getResourceName(type, i);
+ SkAutoTUnref<SkPDFName> keyName(SkNEW_ARGS(SkPDFName, (keyString)));
+ SkAutoTUnref<SkPDFObjRef> ref(
+ SkNEW_ARGS(SkPDFObjRef, (resourceList[i])));
+ resources->insert(keyName, ref);
+ }
+ dst->insert(get_resource_type_name(type), resources);
+}
- SkAutoTUnref<SkPDFName> keyName(
- SkNEW_ARGS(SkPDFName, (getResourceName(type, key))));
- typeDict->insert(keyName, value);
- return value;
+SkPDFDict* SkPDFResourceDict::Create(
+ const SkTDArray<SkPDFObject*>* gStateResources,
+ const SkTDArray<SkPDFObject*>* patternResources,
+ const SkTDArray<SkPDFObject*>* xObjectResources,
+ const SkTDArray<SkPDFObject*>* fontResources) {
+ SkAutoTUnref<SkPDFDict> dict(SkNEW(SkPDFDict));
+ static const char kProcs[][7] = {
+ "PDF", "Text", "ImageB", "ImageC", "ImageI"};
+ SkAutoTUnref<SkPDFArray> 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);
+
+ 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 @@
// 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<SkPDFObject*>* gStateResources,
+ const SkTDArray<SkPDFObject*>* patternResources,
+ const SkTDArray<SkPDFObject*>* xObjectResources,
+ const SkTDArray<SkPDFObject*>* fontResources);
/**
* Returns the name for the resource that will be generated by the resource
@@ -62,23 +51,6 @@
* @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<SkPDFDict*> 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 @@
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<SkPDFObject*> patterns;
+ if (functionShader) {
+ patterns.push(functionShader);
}
- if (gState != NULL) {
- dict->insertResourceAsReference(
- SkPDFResourceDict::kExtGState_ResourceType, 0, gState);
+ SkTDArray<SkPDFObject*> 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 @@
SkAutoTDelete<SkStream> alphaStream(create_pattern_fill_content(-1, bbox));
- SkAutoTUnref<SkPDFResourceDict>
+ SkAutoTUnref<SkPDFDict>
resources(get_gradient_resource_dict(luminosityShader, NULL));
SkAutoTUnref<SkPDFFormXObject> alphaMask(
@@ -682,7 +679,7 @@
SkPDFAlphaFunctionShader* alphaFunctionShader =
SkNEW_ARGS(SkPDFAlphaFunctionShader, (autoState->detach()));
- SkAutoTUnref<SkPDFResourceDict> resourceDict(
+ SkAutoTUnref<SkPDFDict> resourceDict(
get_gradient_resource_dict(colorShader.get(), alphaGs.get()));
SkAutoTDelete<SkStream> colorStream(
@@ -1100,7 +1097,7 @@
SkNEW_ARGS(SkPDFImageShader, (autoState->detach()));
imageShader->setData(content.get());
- SkAutoTUnref<SkPDFResourceDict> resourceDict(
+ SkAutoTUnref<SkPDFDict> 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"