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