• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 // Sanity check that the values of enum SkPDFResourceType correspond to the
12 // expected values as defined in the arrays below.
13 // If these are failing, you may need to update the resource_type_prefixes
14 // and resource_type_names arrays below.
15 SK_COMPILE_ASSERT(SkPDFResourceDict::kExtGState_ResourceType == 0,
16                   resource_type_mismatch);
17 SK_COMPILE_ASSERT(SkPDFResourceDict::kPattern_ResourceType == 1,
18                   resource_type_mismatch);
19 SK_COMPILE_ASSERT(SkPDFResourceDict::kXObject_ResourceType == 2,
20                   resource_type_mismatch);
21 SK_COMPILE_ASSERT(SkPDFResourceDict::kFont_ResourceType == 3,
22                   resource_type_mismatch);
23 
24 static const char resource_type_prefixes[] = {
25         'G',
26         'P',
27         'X',
28         'F'
29 };
30 
31 static const char* resource_type_names[] = {
32         "ExtGState",
33         "Pattern",
34         "XObject",
35         "Font"
36 };
37 
get_resource_type_prefix(SkPDFResourceDict::SkPDFResourceType type)38 static char get_resource_type_prefix(
39         SkPDFResourceDict::SkPDFResourceType type) {
40     SkASSERT(type >= 0);
41     SkASSERT(type < SkPDFResourceDict::kResourceTypeCount);
42 
43     return resource_type_prefixes[type];
44 }
45 
get_resource_type_name(SkPDFResourceDict::SkPDFResourceType type)46 static const char* get_resource_type_name(
47         SkPDFResourceDict::SkPDFResourceType type) {
48     SkASSERT(type >= 0);
49     SkASSERT(type < SK_ARRAY_COUNT(resource_type_names));
50 
51     return resource_type_names[type];
52 }
53 
getResourceName(SkPDFResourceDict::SkPDFResourceType type,int key)54 SkString SkPDFResourceDict::getResourceName(
55         SkPDFResourceDict::SkPDFResourceType type, int key) {
56     SkString keyString;
57     keyString.printf("%c%d", get_resource_type_prefix(type), key);
58     return keyString;
59 }
60 
add_subdict(const SkTDArray<SkPDFObject * > & resourceList,SkPDFResourceDict::SkPDFResourceType type,SkPDFDict * dst)61 static void add_subdict(
62         const SkTDArray<SkPDFObject*>& resourceList,
63         SkPDFResourceDict::SkPDFResourceType type,
64         SkPDFDict* dst) {
65     if (0 == resourceList.count()) {
66         return;
67     }
68     SkAutoTUnref<SkPDFDict> resources(SkNEW(SkPDFDict));
69     for (int i = 0; i < resourceList.count(); i++) {
70         resources->insertObjRef(SkPDFResourceDict::getResourceName(type, i),
71                                 SkRef(resourceList[i]));
72     }
73     dst->insertObject(get_resource_type_name(type), resources.detach());
74 }
75 
Create(const SkTDArray<SkPDFObject * > * gStateResources,const SkTDArray<SkPDFObject * > * patternResources,const SkTDArray<SkPDFObject * > * xObjectResources,const SkTDArray<SkPDFObject * > * fontResources)76 SkPDFDict* SkPDFResourceDict::Create(
77         const SkTDArray<SkPDFObject*>* gStateResources,
78         const SkTDArray<SkPDFObject*>* patternResources,
79         const SkTDArray<SkPDFObject*>* xObjectResources,
80         const SkTDArray<SkPDFObject*>* fontResources) {
81     SkAutoTUnref<SkPDFDict> dict(SkNEW(SkPDFDict));
82     static const char kProcs[][7] = {
83         "PDF", "Text", "ImageB", "ImageC", "ImageI"};
84     SkAutoTUnref<SkPDFArray> procSets(SkNEW(SkPDFArray));
85 
86     procSets->reserve(SK_ARRAY_COUNT(kProcs));
87     for (size_t i = 0; i < SK_ARRAY_COUNT(kProcs); i++) {
88         procSets->appendName(kProcs[i]);
89     }
90     dict->insertObject("ProcSets", procSets.detach());
91 
92     if (gStateResources) {
93         add_subdict(*gStateResources, kExtGState_ResourceType, dict);
94     }
95     if (patternResources) {
96         add_subdict(*patternResources, kPattern_ResourceType, dict);
97     }
98     if (xObjectResources) {
99         add_subdict(*xObjectResources, kXObject_ResourceType, dict);
100     }
101     if (fontResources) {
102         add_subdict(*fontResources, kFont_ResourceType, dict);
103     }
104     return dict.detach();
105 }
106