OLD | NEW |
---|---|
(Empty) | |
1 /* | |
2 * Copyright 2013 Google Inc. | |
3 * | |
4 * Use of this source code is governed by a BSD-style license that can be | |
5 * found in the LICENSE file. | |
6 */ | |
7 | |
8 #include "SkPDFResourceDict.h" | |
9 #include "SkPostConfig.h" | |
10 | |
11 SK_DEFINE_INST_COUNT(SkPDFResourceDict) | |
12 | |
13 // Sanity check that the values of enum SkPDFResourceType correspond to the | |
14 // expected values as defined in the arrays below. | |
15 // If these are failing, you may need to update the resource_type_prefixes | |
16 // and resource_type_names arrays below. | |
17 SK_COMPILE_ASSERT(SkPDFResourceDict::kExtGState_ResourceType == 0, resource_type _mismatch); | |
18 SK_COMPILE_ASSERT(SkPDFResourceDict::kPattern_ResourceType == 1, resource_type_m ismatch); | |
19 SK_COMPILE_ASSERT(SkPDFResourceDict::kXObject_ResourceType == 2, resource_type_m ismatch); | |
20 SK_COMPILE_ASSERT(SkPDFResourceDict::kFont_ResourceType == 3, resource_type_mism atch); | |
21 | |
22 const char resource_type_prefixes[][2] = { | |
vandebo (ex-Chrome)
2013/07/11 17:50:23
static
ducky
2013/07/11 23:09:50
Done.
| |
23 "G", | |
24 "P", | |
25 "X", | |
26 "F" | |
27 }; | |
28 | |
29 const char resource_type_names[][11] = { | |
vandebo (ex-Chrome)
2013/07/11 17:50:23
static
ducky
2013/07/11 23:09:50
Done.
| |
30 "ExtGState", | |
31 "Pattern", | |
32 "XObject", | |
33 "Font" | |
34 }; | |
35 | |
36 static const char* getResourceTypePrefix(SkPDFResourceDict::SkPDFResourceType ty pe) { | |
vandebo (ex-Chrome)
2013/07/11 17:50:23
get_resource_type_prefix
ducky
2013/07/11 23:09:50
Done.
| |
37 SkASSERT(type >= SkPDFResourceDict::kExtGState_ResourceType); | |
38 SkASSERT(type <= SkPDFResourceDict::kFont_ResourceType); | |
39 | |
40 return resource_type_prefixes[type]; | |
41 } | |
42 | |
43 SkPDFResourceDict::SkPDFResourceDict() : SkPDFDict() { | |
44 const char procs[][7] = {"PDF", "Text", "ImageB", "ImageC", "ImageI"}; | |
45 SkPDFArray* procSets = SkNEW(SkPDFArray()); | |
46 | |
47 procSets->reserve(SK_ARRAY_COUNT(procs)); | |
48 for (size_t i = 0; i < SK_ARRAY_COUNT(procs); i++) { | |
49 procSets->appendName(procs[i]); | |
50 } | |
51 insert("ProcSets", procSets)->unref(); | |
52 | |
53 fTypes.setCount(SK_ARRAY_COUNT(resource_type_names)); | |
54 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. =(
| |
55 SkAutoTUnref<SkPDFDict> newDict(SkNEW(SkPDFDict())); | |
56 SkPDFName* typeName = SkNEW_ARGS(SkPDFName, (resource_type_names[i])); | |
57 insert(typeName, newDict); // ref counting handled in SkPDFDict::insert | |
58 fTypes[i] = newDict.get(); | |
59 } | |
60 } | |
61 | |
62 SkPDFObject* SkPDFResourceDict::insertResource( | |
63 SkPDFResourceType type, int key, SkPDFObject* value) { | |
64 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.
| |
65 | |
66 SkString keyString(getResourceTypePrefix(type)); | |
67 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.
| |
68 SkPDFName* keyName = SkNEW_ARGS(SkPDFName, (keyString)); | |
69 typeDict->insert(keyName, value); | |
70 return value; | |
71 } | |
72 | |
73 SkPDFObject* SkPDFResourceDict::insertResourceAsRef( | |
74 SkPDFResourceType type, int key, SkPDFObject* value) { | |
75 SkAutoTUnref<SkPDFObjRef> ref(SkNEW_ARGS(SkPDFObjRef, (value))); | |
76 insertResource(type, key, ref); | |
77 fResources.add(value); | |
78 | |
79 return value; | |
80 } | |
81 | |
82 void SkPDFResourceDict::getResources( | |
83 const SkTSet<SkPDFObject*>& knownResourceObjects, | |
84 SkTSet<SkPDFObject*>* newResourceObjects, | |
85 bool recursive) const { | |
86 // TODO: reserve not correct if we need to recursively explore. | |
87 newResourceObjects->setReserve(newResourceObjects->count() + | |
88 fResources.count()); | |
89 | |
90 for (int i = 0; i < fResources.count(); i++) { | |
91 if (!knownResourceObjects.contains(fResources[i]) && | |
92 !newResourceObjects->contains(fResources[i])) { | |
93 newResourceObjects->add(fResources[i]); | |
94 fResources[i]->ref(); | |
95 if (recursive) { | |
96 fResources[i]->getResources(knownResourceObjects, | |
97 newResourceObjects); | |
98 } | |
99 } | |
100 } | |
101 } | |
OLD | NEW |