blob: 1f5bceac9c6e804b7aa15a4518931e68040e4bd4 [file] [log] [blame]
commit-bot@chromium.org47401352013-07-23 21:49:29 +00001/*
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
commit-bot@chromium.org47401352013-07-23 21:49:29 +000011// 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.
15SK_COMPILE_ASSERT(SkPDFResourceDict::kExtGState_ResourceType == 0,
16 resource_type_mismatch);
17SK_COMPILE_ASSERT(SkPDFResourceDict::kPattern_ResourceType == 1,
18 resource_type_mismatch);
19SK_COMPILE_ASSERT(SkPDFResourceDict::kXObject_ResourceType == 2,
20 resource_type_mismatch);
21SK_COMPILE_ASSERT(SkPDFResourceDict::kFont_ResourceType == 3,
22 resource_type_mismatch);
23
24static const char resource_type_prefixes[] = {
25 'G',
26 'P',
27 'X',
28 'F'
29};
30
31static const char* resource_type_names[] = {
32 "ExtGState",
33 "Pattern",
34 "XObject",
35 "Font"
36};
37
38static 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
46static const char* get_resource_type_name(
47 SkPDFResourceDict::SkPDFResourceType type) {
48 SkASSERT(type >= 0);
halcanary130444f2015-04-25 06:45:07 -070049 SkASSERT(type < SK_ARRAY_COUNT(resource_type_names));
commit-bot@chromium.org47401352013-07-23 21:49:29 +000050
51 return resource_type_names[type];
52}
53
commit-bot@chromium.org47401352013-07-23 21:49:29 +000054SkString SkPDFResourceDict::getResourceName(
halcanary2b861552015-04-09 13:27:40 -070055 SkPDFResourceDict::SkPDFResourceType type, int key) {
commit-bot@chromium.org47401352013-07-23 21:49:29 +000056 SkString keyString;
57 keyString.printf("%c%d", get_resource_type_prefix(type), key);
58 return keyString;
59}
60
halcanary2b861552015-04-09 13:27:40 -070061static void add_subdict(
62 const SkTDArray<SkPDFObject*>& resourceList,
63 SkPDFResourceDict::SkPDFResourceType type,
64 SkPDFDict* dst) {
65 if (0 == resourceList.count()) {
66 return;
commit-bot@chromium.org47401352013-07-23 21:49:29 +000067 }
halcanary2b861552015-04-09 13:27:40 -070068 SkAutoTUnref<SkPDFDict> resources(SkNEW(SkPDFDict));
69 for (int i = 0; i < resourceList.count(); i++) {
halcanary130444f2015-04-25 06:45:07 -070070 resources->insertObjRef(SkPDFResourceDict::getResourceName(type, i),
71 SkRef(resourceList[i]));
halcanary2b861552015-04-09 13:27:40 -070072 }
halcanary130444f2015-04-25 06:45:07 -070073 dst->insertObject(get_resource_type_name(type), resources.detach());
halcanary2b861552015-04-09 13:27:40 -070074}
commit-bot@chromium.org47401352013-07-23 21:49:29 +000075
halcanary2b861552015-04-09 13:27:40 -070076SkPDFDict* 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 }
halcanary130444f2015-04-25 06:45:07 -070090 dict->insertObject("ProcSets", procSets.detach());
halcanary2b861552015-04-09 13:27:40 -070091
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();
commit-bot@chromium.org47401352013-07-23 21:49:29 +0000105}