| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright 2013 Google Inc. | 2 * Copyright 2013 Google Inc. |
| 3 * | 3 * |
| 4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
| 5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
| 6 */ | 6 */ |
| 7 | 7 |
| 8 #include "SkPDFResourceDict.h" | 8 #include "SkPDFResourceDict.h" |
| 9 #include "SkPostConfig.h" | 9 #include "SkPostConfig.h" |
| 10 | 10 |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 44 } | 44 } |
| 45 | 45 |
| 46 static const char* get_resource_type_name( | 46 static const char* get_resource_type_name( |
| 47 SkPDFResourceDict::SkPDFResourceType type) { | 47 SkPDFResourceDict::SkPDFResourceType type) { |
| 48 SkASSERT(type >= 0); | 48 SkASSERT(type >= 0); |
| 49 SkASSERT(type < SkPDFResourceDict::kResourceTypeCount); | 49 SkASSERT(type < SkPDFResourceDict::kResourceTypeCount); |
| 50 | 50 |
| 51 return resource_type_names[type]; | 51 return resource_type_names[type]; |
| 52 } | 52 } |
| 53 | 53 |
| 54 SkPDFResourceDict::SkPDFResourceDict() : SkPDFDict() { | |
| 55 const char procs[][7] = {"PDF", "Text", "ImageB", "ImageC", "ImageI"}; | |
| 56 SkPDFArray* procSets = SkNEW(SkPDFArray()); | |
| 57 | |
| 58 procSets->reserve(SK_ARRAY_COUNT(procs)); | |
| 59 for (size_t i = 0; i < SK_ARRAY_COUNT(procs); i++) { | |
| 60 procSets->appendName(procs[i]); | |
| 61 } | |
| 62 insert("ProcSets", procSets)->unref(); | |
| 63 | |
| 64 // Actual sub-dicts will be lazily added later | |
| 65 fTypes.setCount(kResourceTypeCount); | |
| 66 for (int i=0; i < kResourceTypeCount; i++) { | |
| 67 fTypes[i] = NULL; | |
| 68 } | |
| 69 } | |
| 70 | |
| 71 SkPDFObject* SkPDFResourceDict::insertResourceAsReference( | |
| 72 SkPDFResourceType type, int key, SkPDFObject* value) { | |
| 73 SkAutoTUnref<SkPDFObjRef> ref(SkNEW_ARGS(SkPDFObjRef, (value))); | |
| 74 insertResource(type, key, ref); | |
| 75 | |
| 76 return value; | |
| 77 } | |
| 78 | |
| 79 SkString SkPDFResourceDict::getResourceName( | 54 SkString SkPDFResourceDict::getResourceName( |
| 80 SkPDFResourceType type, int key) { | 55 SkPDFResourceDict::SkPDFResourceType type, int key) { |
| 81 SkString keyString; | 56 SkString keyString; |
| 82 keyString.printf("%c%d", get_resource_type_prefix(type), key); | 57 keyString.printf("%c%d", get_resource_type_prefix(type), key); |
| 83 return keyString; | 58 return keyString; |
| 84 } | 59 } |
| 85 | 60 |
| 86 SkPDFObject* SkPDFResourceDict::insertResource( | 61 static void add_subdict( |
| 87 SkPDFResourceType type, int key, SkPDFObject* value) { | 62 const SkTDArray<SkPDFObject*>& resourceList, |
| 88 SkPDFDict* typeDict = fTypes[type]; | 63 SkPDFResourceDict::SkPDFResourceType type, |
| 89 if (NULL == typeDict) { | 64 SkPDFDict* dst) { |
| 90 SkAutoTUnref<SkPDFDict> newDict(SkNEW(SkPDFDict())); | 65 if (0 == resourceList.count()) { |
| 91 SkAutoTUnref<SkPDFName> typeName( | 66 return; |
| 92 SkNEW_ARGS(SkPDFName, (get_resource_type_name(type)))); | |
| 93 insert(typeName, newDict); // ref counting handled here | |
| 94 fTypes[type] = newDict; | |
| 95 typeDict = newDict.get(); | |
| 96 } | 67 } |
| 68 SkAutoTUnref<SkPDFDict> resources(SkNEW(SkPDFDict)); |
| 69 for (int i = 0; i < resourceList.count(); i++) { |
| 70 SkString keyString = SkPDFResourceDict::getResourceName(type, i); |
| 71 SkAutoTUnref<SkPDFName> keyName(SkNEW_ARGS(SkPDFName, (keyString))); |
| 72 SkAutoTUnref<SkPDFObjRef> ref( |
| 73 SkNEW_ARGS(SkPDFObjRef, (resourceList[i]))); |
| 74 resources->insert(keyName, ref); |
| 75 } |
| 76 dst->insert(get_resource_type_name(type), resources); |
| 77 } |
| 97 | 78 |
| 98 SkAutoTUnref<SkPDFName> keyName( | 79 SkPDFDict* SkPDFResourceDict::Create( |
| 99 SkNEW_ARGS(SkPDFName, (getResourceName(type, key)))); | 80 const SkTDArray<SkPDFObject*>* gStateResources, |
| 100 typeDict->insert(keyName, value); | 81 const SkTDArray<SkPDFObject*>* patternResources, |
| 101 return value; | 82 const SkTDArray<SkPDFObject*>* xObjectResources, |
| 83 const SkTDArray<SkPDFObject*>* fontResources) { |
| 84 SkAutoTUnref<SkPDFDict> dict(SkNEW(SkPDFDict)); |
| 85 static const char kProcs[][7] = { |
| 86 "PDF", "Text", "ImageB", "ImageC", "ImageI"}; |
| 87 SkAutoTUnref<SkPDFArray> procSets(SkNEW(SkPDFArray)); |
| 88 |
| 89 procSets->reserve(SK_ARRAY_COUNT(kProcs)); |
| 90 for (size_t i = 0; i < SK_ARRAY_COUNT(kProcs); i++) { |
| 91 procSets->appendName(kProcs[i]); |
| 92 } |
| 93 dict->insert("ProcSets", procSets); |
| 94 |
| 95 if (gStateResources) { |
| 96 add_subdict(*gStateResources, kExtGState_ResourceType, dict); |
| 97 } |
| 98 if (patternResources) { |
| 99 add_subdict(*patternResources, kPattern_ResourceType, dict); |
| 100 } |
| 101 if (xObjectResources) { |
| 102 add_subdict(*xObjectResources, kXObject_ResourceType, dict); |
| 103 } |
| 104 if (fontResources) { |
| 105 add_subdict(*fontResources, kFont_ResourceType, dict); |
| 106 } |
| 107 return dict.detach(); |
| 102 } | 108 } |
| OLD | NEW |