blob: 67e81b6c7b5912f461a00fd1997465c46bd83a24 [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"
martina.kollarovab8d6af12016-06-29 05:12:31 -07009#include "SkPDFTypes.h"
commit-bot@chromium.org47401352013-07-23 21:49:29 +000010#include "SkPostConfig.h"
11
commit-bot@chromium.org47401352013-07-23 21:49:29 +000012// Sanity check that the values of enum SkPDFResourceType correspond to the
13// expected values as defined in the arrays below.
14// If these are failing, you may need to update the resource_type_prefixes
15// and resource_type_names arrays below.
bungeman99fe8222015-08-20 07:57:51 -070016static_assert(SkPDFResourceDict::kExtGState_ResourceType == 0, "resource_type_mismatch");
17static_assert(SkPDFResourceDict::kPattern_ResourceType == 1, "resource_type_mismatch");
18static_assert(SkPDFResourceDict::kXObject_ResourceType == 2, "resource_type_mismatch");
19static_assert(SkPDFResourceDict::kFont_ResourceType == 3, "resource_type_mismatch");
commit-bot@chromium.org47401352013-07-23 21:49:29 +000020
21static const char resource_type_prefixes[] = {
22 'G',
23 'P',
24 'X',
25 'F'
26};
27
28static const char* resource_type_names[] = {
29 "ExtGState",
30 "Pattern",
31 "XObject",
32 "Font"
33};
34
halcanaryc2f9ec12016-09-12 08:55:29 -070035char SkPDFResourceDict::GetResourceTypePrefix(
commit-bot@chromium.org47401352013-07-23 21:49:29 +000036 SkPDFResourceDict::SkPDFResourceType type) {
37 SkASSERT(type >= 0);
38 SkASSERT(type < SkPDFResourceDict::kResourceTypeCount);
39
40 return resource_type_prefixes[type];
41}
42
43static const char* get_resource_type_name(
44 SkPDFResourceDict::SkPDFResourceType type) {
45 SkASSERT(type >= 0);
halcanary130444f2015-04-25 06:45:07 -070046 SkASSERT(type < SK_ARRAY_COUNT(resource_type_names));
commit-bot@chromium.org47401352013-07-23 21:49:29 +000047
48 return resource_type_names[type];
49}
50
commit-bot@chromium.org47401352013-07-23 21:49:29 +000051SkString SkPDFResourceDict::getResourceName(
halcanary2b861552015-04-09 13:27:40 -070052 SkPDFResourceDict::SkPDFResourceType type, int key) {
halcanaryc2f9ec12016-09-12 08:55:29 -070053 return SkStringPrintf("%c%d", SkPDFResourceDict::GetResourceTypePrefix(type), key);
commit-bot@chromium.org47401352013-07-23 21:49:29 +000054}
55
halcanary2b861552015-04-09 13:27:40 -070056static void add_subdict(
57 const SkTDArray<SkPDFObject*>& resourceList,
58 SkPDFResourceDict::SkPDFResourceType type,
59 SkPDFDict* dst) {
60 if (0 == resourceList.count()) {
61 return;
commit-bot@chromium.org47401352013-07-23 21:49:29 +000062 }
halcanaryece83922016-03-08 08:32:12 -080063 auto resources = sk_make_sp<SkPDFDict>();
halcanary2b861552015-04-09 13:27:40 -070064 for (int i = 0; i < resourceList.count(); i++) {
halcanary130444f2015-04-25 06:45:07 -070065 resources->insertObjRef(SkPDFResourceDict::getResourceName(type, i),
halcanarye94ea622016-03-09 07:52:09 -080066 sk_ref_sp(resourceList[i]));
halcanary2b861552015-04-09 13:27:40 -070067 }
halcanary8103a342016-03-08 15:10:16 -080068 dst->insertObject(get_resource_type_name(type), std::move(resources));
halcanary2b861552015-04-09 13:27:40 -070069}
commit-bot@chromium.org47401352013-07-23 21:49:29 +000070
halcanary8103a342016-03-08 15:10:16 -080071sk_sp<SkPDFDict> SkPDFResourceDict::Make(
halcanary2b861552015-04-09 13:27:40 -070072 const SkTDArray<SkPDFObject*>* gStateResources,
73 const SkTDArray<SkPDFObject*>* patternResources,
74 const SkTDArray<SkPDFObject*>* xObjectResources,
75 const SkTDArray<SkPDFObject*>* fontResources) {
halcanaryece83922016-03-08 08:32:12 -080076 auto dict = sk_make_sp<SkPDFDict>();
halcanary2b861552015-04-09 13:27:40 -070077 static const char kProcs[][7] = {
78 "PDF", "Text", "ImageB", "ImageC", "ImageI"};
halcanaryece83922016-03-08 08:32:12 -080079 auto procSets = sk_make_sp<SkPDFArray>();
halcanary2b861552015-04-09 13:27:40 -070080
81 procSets->reserve(SK_ARRAY_COUNT(kProcs));
82 for (size_t i = 0; i < SK_ARRAY_COUNT(kProcs); i++) {
83 procSets->appendName(kProcs[i]);
84 }
halcanary8103a342016-03-08 15:10:16 -080085 dict->insertObject("ProcSets", std::move(procSets));
halcanary2b861552015-04-09 13:27:40 -070086
87 if (gStateResources) {
halcanaryfcad44b2016-03-06 14:47:10 -080088 add_subdict(*gStateResources, kExtGState_ResourceType, dict.get());
halcanary2b861552015-04-09 13:27:40 -070089 }
90 if (patternResources) {
halcanaryfcad44b2016-03-06 14:47:10 -080091 add_subdict(*patternResources, kPattern_ResourceType, dict.get());
halcanary2b861552015-04-09 13:27:40 -070092 }
93 if (xObjectResources) {
halcanaryfcad44b2016-03-06 14:47:10 -080094 add_subdict(*xObjectResources, kXObject_ResourceType, dict.get());
halcanary2b861552015-04-09 13:27:40 -070095 }
96 if (fontResources) {
halcanaryfcad44b2016-03-06 14:47:10 -080097 add_subdict(*fontResources, kFont_ResourceType, dict.get());
halcanary2b861552015-04-09 13:27:40 -070098 }
halcanary8103a342016-03-08 15:10:16 -080099 return dict;
commit-bot@chromium.org47401352013-07-23 21:49:29 +0000100}