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