Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(82)

Side by Side Diff: src/pdf/SkPDFResourceDict.cpp

Issue 18977002: Add SkPDFResourceDict class, refactor existing code (Closed) Base URL: https://skia.googlecode.com/svn/trunk
Patch Set: Fixes 80 cols Created 7 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(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,
18 resource_type_mismatch);
19 SK_COMPILE_ASSERT(SkPDFResourceDict::kPattern_ResourceType == 1,
20 resource_type_mismatch);
21 SK_COMPILE_ASSERT(SkPDFResourceDict::kXObject_ResourceType == 2,
22 resource_type_mismatch);
23 SK_COMPILE_ASSERT(SkPDFResourceDict::kFont_ResourceType == 3,
24 resource_type_mismatch);
25
26 static char resource_type_prefixes[] = {
vandebo (ex-Chrome) 2013/07/12 19:14:59 sorry, this should be const as well.
ducky 2013/07/12 21:18:17 Done.
27 'G',
vandebo (ex-Chrome) 2013/07/12 19:14:59 These constants are currently being used directly
ducky 2013/07/12 21:18:17 Done. Also refactored all usage cases.
28 'P',
29 'X',
30 'F'
31 };
32
33 static const char* resource_type_names[] = {
34 "ExtGState",
35 "Pattern",
36 "XObject",
37 "Font"
38 };
39
40 static char get_resource_type_prefix(
41 SkPDFResourceDict::SkPDFResourceType type) {
42 SkASSERT(type >= SkPDFResourceDict::kExtGState_ResourceType);
43 SkASSERT(type <= SkPDFResourceDict::kFont_ResourceType);
vandebo (ex-Chrome) 2013/07/12 19:14:59 Your asserts here should use 0 and Count
ducky 2013/07/12 21:18:17 Done.
44
45 return resource_type_prefixes[type];
46 }
47
48 static const char* get_resource_type_name(
49 SkPDFResourceDict::SkPDFResourceType type) {
50 SkASSERT(type >= SkPDFResourceDict::kExtGState_ResourceType);
51 SkASSERT(type <= SkPDFResourceDict::kFont_ResourceType);
52
53 return resource_type_names[type];
54 }
55
56 SkPDFResourceDict::SkPDFResourceDict() : SkPDFDict() {
57 const char procs[][7] = {"PDF", "Text", "ImageB", "ImageC", "ImageI"};
58 SkPDFArray* procSets = SkNEW(SkPDFArray());
59
60 procSets->reserve(SK_ARRAY_COUNT(procs));
61 for (size_t i = 0; i < SK_ARRAY_COUNT(procs); i++) {
62 procSets->appendName(procs[i]);
63 }
64 insert("ProcSets", procSets)->unref();
65
66 // Actual sub-dicts will be lazily added later
67 fTypes.setCount(kResourceTypeCount);
68 for (size_t i=0; i < kResourceTypeCount; i++) {
69 fTypes[i] = NULL;
70 }
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 }
102
103 SkPDFObject* SkPDFResourceDict::insertResource(
104 SkPDFResourceType type, int key, SkPDFObject* value) {
105 SkPDFDict* typeDict = fTypes[type];
106 if (NULL == typeDict) {
107 typeDict = SkNEW(SkPDFDict());
vandebo (ex-Chrome) 2013/07/12 19:14:59 It'll be the same number of lines, but less fragil
ducky 2013/07/12 21:18:17 Done.
108 SkPDFName* typeName = SkNEW_ARGS(
109 SkPDFName, (get_resource_type_name(type)));
110 insert(typeName, typeDict); // ref counting handled here
111 fTypes[type] = typeDict;
112 typeDict->unref();
113 }
114
115 SkString keyString;
116 keyString.printf("%c%d", get_resource_type_prefix(type), key);
117 SkPDFName* keyName = SkNEW_ARGS(SkPDFName, (keyString));
118 typeDict->insert(keyName, value);
119 return value;
120 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698