blob: 2f9abca8b6926fdb126296efda406f060de9d415 [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.
Tom Hudson2880df22015-10-29 09:55:42 -040015static_assert(SkPDFResourceDict::kExtGState_ResourceType == 0, "resource_type_mismatch");
16static_assert(SkPDFResourceDict::kPattern_ResourceType == 1, "resource_type_mismatch");
17static_assert(SkPDFResourceDict::kXObject_ResourceType == 2, "resource_type_mismatch");
18static_assert(SkPDFResourceDict::kFont_ResourceType == 3, "resource_type_mismatch");
commit-bot@chromium.org47401352013-07-23 21:49:29 +000019
20static const char resource_type_prefixes[] = {
21 'G',
22 'P',
23 'X',
24 'F'
25};
26
27static const char* resource_type_names[] = {
28 "ExtGState",
29 "Pattern",
30 "XObject",
31 "Font"
32};
33
34static 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
42static const char* get_resource_type_name(
43 SkPDFResourceDict::SkPDFResourceType type) {
44 SkASSERT(type >= 0);
halcanary130444f2015-04-25 06:45:07 -070045 SkASSERT(type < SK_ARRAY_COUNT(resource_type_names));
commit-bot@chromium.org47401352013-07-23 21:49:29 +000046
47 return resource_type_names[type];
48}
49
commit-bot@chromium.org47401352013-07-23 21:49:29 +000050SkString SkPDFResourceDict::getResourceName(
halcanary2b861552015-04-09 13:27:40 -070051 SkPDFResourceDict::SkPDFResourceType type, int key) {
commit-bot@chromium.org47401352013-07-23 21:49:29 +000052 SkString keyString;
53 keyString.printf("%c%d", get_resource_type_prefix(type), key);
54 return keyString;
55}
56
halcanary2b861552015-04-09 13:27:40 -070057static void add_subdict(
58 const SkTDArray<SkPDFObject*>& resourceList,
59 SkPDFResourceDict::SkPDFResourceType type,
60 SkPDFDict* dst) {
61 if (0 == resourceList.count()) {
62 return;
commit-bot@chromium.org47401352013-07-23 21:49:29 +000063 }
Tom Hudson2880df22015-10-29 09:55:42 -040064 SkAutoTUnref<SkPDFDict> resources(new SkPDFDict);
halcanary2b861552015-04-09 13:27:40 -070065 for (int i = 0; i < resourceList.count(); i++) {
halcanary130444f2015-04-25 06:45:07 -070066 resources->insertObjRef(SkPDFResourceDict::getResourceName(type, i),
67 SkRef(resourceList[i]));
halcanary2b861552015-04-09 13:27:40 -070068 }
halcanary130444f2015-04-25 06:45:07 -070069 dst->insertObject(get_resource_type_name(type), resources.detach());
halcanary2b861552015-04-09 13:27:40 -070070}
commit-bot@chromium.org47401352013-07-23 21:49:29 +000071
halcanary2b861552015-04-09 13:27:40 -070072SkPDFDict* SkPDFResourceDict::Create(
73 const SkTDArray<SkPDFObject*>* gStateResources,
74 const SkTDArray<SkPDFObject*>* patternResources,
75 const SkTDArray<SkPDFObject*>* xObjectResources,
76 const SkTDArray<SkPDFObject*>* fontResources) {
Tom Hudson2880df22015-10-29 09:55:42 -040077 SkAutoTUnref<SkPDFDict> dict(new SkPDFDict);
halcanary2b861552015-04-09 13:27:40 -070078 static const char kProcs[][7] = {
79 "PDF", "Text", "ImageB", "ImageC", "ImageI"};
Tom Hudson2880df22015-10-29 09:55:42 -040080 SkAutoTUnref<SkPDFArray> procSets(new SkPDFArray);
halcanary2b861552015-04-09 13:27:40 -070081
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 }
halcanary130444f2015-04-25 06:45:07 -070086 dict->insertObject("ProcSets", procSets.detach());
halcanary2b861552015-04-09 13:27:40 -070087
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();
commit-bot@chromium.org47401352013-07-23 21:49:29 +0000101}