Index: src/pdf/SkPDFResourceDict.cpp |
diff --git a/src/pdf/SkPDFResourceDict.cpp b/src/pdf/SkPDFResourceDict.cpp |
new file mode 100644 |
index 0000000000000000000000000000000000000000..c3a89b14a952676f460b97802dd122fde827dfa1 |
--- /dev/null |
+++ b/src/pdf/SkPDFResourceDict.cpp |
@@ -0,0 +1,101 @@ |
+/* |
+ * Copyright 2013 Google Inc. |
+ * |
+ * Use of this source code is governed by a BSD-style license that can be |
+ * found in the LICENSE file. |
+ */ |
+ |
+#include "SkPDFResourceDict.h" |
+#include "SkPostConfig.h" |
+ |
+SK_DEFINE_INST_COUNT(SkPDFResourceDict) |
+ |
+// Sanity check that the values of enum SkPDFResourceType correspond to the |
+// expected values as defined in the arrays below. |
+// If these are failing, you may need to update the resource_type_prefixes |
+// and resource_type_names arrays below. |
+SK_COMPILE_ASSERT(SkPDFResourceDict::kExtGState_ResourceType == 0, resource_type_mismatch); |
+SK_COMPILE_ASSERT(SkPDFResourceDict::kPattern_ResourceType == 1, resource_type_mismatch); |
+SK_COMPILE_ASSERT(SkPDFResourceDict::kXObject_ResourceType == 2, resource_type_mismatch); |
+SK_COMPILE_ASSERT(SkPDFResourceDict::kFont_ResourceType == 3, resource_type_mismatch); |
+ |
+const char resource_type_prefixes[][2] = { |
vandebo (ex-Chrome)
2013/07/11 17:50:23
static
ducky
2013/07/11 23:09:50
Done.
|
+ "G", |
+ "P", |
+ "X", |
+ "F" |
+}; |
+ |
+const char resource_type_names[][11] = { |
vandebo (ex-Chrome)
2013/07/11 17:50:23
static
ducky
2013/07/11 23:09:50
Done.
|
+ "ExtGState", |
+ "Pattern", |
+ "XObject", |
+ "Font" |
+}; |
+ |
+static const char* getResourceTypePrefix(SkPDFResourceDict::SkPDFResourceType type) { |
vandebo (ex-Chrome)
2013/07/11 17:50:23
get_resource_type_prefix
ducky
2013/07/11 23:09:50
Done.
|
+ SkASSERT(type >= SkPDFResourceDict::kExtGState_ResourceType); |
+ SkASSERT(type <= SkPDFResourceDict::kFont_ResourceType); |
+ |
+ return resource_type_prefixes[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(); |
+ |
+ fTypes.setCount(SK_ARRAY_COUNT(resource_type_names)); |
+ for (size_t i=0; i < SK_ARRAY_COUNT(resource_type_names); i++) { |
vandebo (ex-Chrome)
2013/07/11 17:50:23
fTypes should be populated lazily - we don't want
ducky
2013/07/11 23:09:50
Yep. Just realized that after I left yesterday. =(
|
+ SkAutoTUnref<SkPDFDict> newDict(SkNEW(SkPDFDict())); |
+ SkPDFName* typeName = SkNEW_ARGS(SkPDFName, (resource_type_names[i])); |
+ insert(typeName, newDict); // ref counting handled in SkPDFDict::insert |
+ fTypes[i] = newDict.get(); |
+ } |
+} |
+ |
+SkPDFObject* SkPDFResourceDict::insertResource( |
+ SkPDFResourceType type, int key, SkPDFObject* value) { |
+ SkPDFDict* typeDict = fTypes[type]; |
vandebo (ex-Chrome)
2013/07/11 17:50:23
Lazy population can go here:
if (!typeDict) ...
ducky
2013/07/11 23:09:50
Done.
|
+ |
+ SkString keyString(getResourceTypePrefix(type)); |
+ keyString.appendS32(key); |
vandebo (ex-Chrome)
2013/07/11 17:50:23
You could use keyString.printf("%c%d", getResource
ducky
2013/07/11 23:09:50
Done.
|
+ SkPDFName* keyName = SkNEW_ARGS(SkPDFName, (keyString)); |
+ typeDict->insert(keyName, value); |
+ return value; |
+} |
+ |
+SkPDFObject* SkPDFResourceDict::insertResourceAsRef( |
+ SkPDFResourceType type, int key, SkPDFObject* value) { |
+ SkAutoTUnref<SkPDFObjRef> ref(SkNEW_ARGS(SkPDFObjRef, (value))); |
+ insertResource(type, key, ref); |
+ fResources.add(value); |
+ |
+ return value; |
+} |
+ |
+void SkPDFResourceDict::getResources( |
+ const SkTSet<SkPDFObject*>& knownResourceObjects, |
+ SkTSet<SkPDFObject*>* newResourceObjects, |
+ bool recursive) const { |
+ // TODO: reserve not correct if we need to recursively explore. |
+ newResourceObjects->setReserve(newResourceObjects->count() + |
+ fResources.count()); |
+ |
+ for (int i = 0; i < fResources.count(); i++) { |
+ if (!knownResourceObjects.contains(fResources[i]) && |
+ !newResourceObjects->contains(fResources[i])) { |
+ newResourceObjects->add(fResources[i]); |
+ fResources[i]->ref(); |
+ if (recursive) { |
+ fResources[i]->getResources(knownResourceObjects, |
+ newResourceObjects); |
+ } |
+ } |
+ } |
+} |