blob: 0754219480004ad756336030f9c402f056402705 [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"
Hal Canarye650b852018-09-12 09:12:36 -040010#include "SkStream.h"
commit-bot@chromium.org47401352013-07-23 21:49:29 +000011
Hal Canarye650b852018-09-12 09:12:36 -040012// Sanity check that the values of enum ResourceType correspond to the
commit-bot@chromium.org47401352013-07-23 21:49:29 +000013// expected values as defined in the arrays below.
Hal Canarye650b852018-09-12 09:12:36 -040014// If these are failing, you may need to update the kResourceTypePrefixes
15// and kResourceTypeNames arrays below.
16static_assert(0 == (int)SkPDFResourceType::kExtGState, "resource_type_mismatch");
17static_assert(1 == (int)SkPDFResourceType::kPattern, "resource_type_mismatch");
18static_assert(2 == (int)SkPDFResourceType::kXObject, "resource_type_mismatch");
19static_assert(3 == (int)SkPDFResourceType::kFont, "resource_type_mismatch");
commit-bot@chromium.org47401352013-07-23 21:49:29 +000020
Hal Canarye650b852018-09-12 09:12:36 -040021// One extra character for the Prefix.
22constexpr size_t kMaxResourceNameLength = 1 + SkStrAppendS32_MaxSize;
commit-bot@chromium.org47401352013-07-23 21:49:29 +000023
Hal Canarye650b852018-09-12 09:12:36 -040024// returns pointer just past end of what's written into `dst`.
25static char* get_resource_name(char dst[kMaxResourceNameLength], SkPDFResourceType type, int key) {
26 static const char kResourceTypePrefixes[] = {
27 'G', // kExtGState
28 'P', // kPattern
29 'X', // kXObject
30 'F' // kFont
31 };
32 SkASSERT((unsigned)type < SK_ARRAY_COUNT(kResourceTypePrefixes));
33 dst[0] = kResourceTypePrefixes[(unsigned)type];
34 return SkStrAppendS32(dst + 1, key);
commit-bot@chromium.org47401352013-07-23 21:49:29 +000035}
36
Hal Canarye650b852018-09-12 09:12:36 -040037void SkPDFWriteResourceName(SkWStream* dst, SkPDFResourceType type, int key) {
38 // One extra character for the leading '/'.
39 char buffer[1 + kMaxResourceNameLength];
40 buffer[0] = '/';
41 char* end = get_resource_name(buffer + 1, type, key);
42 dst->write(buffer, (size_t)(end - buffer));
commit-bot@chromium.org47401352013-07-23 21:49:29 +000043}
44
Hal Canaryb10f92e2018-11-16 17:01:50 -050045static const char* resource_name(SkPDFResourceType type) {
Hal Canarye650b852018-09-12 09:12:36 -040046 static const char* kResourceTypeNames[] = {
47 "ExtGState",
48 "Pattern",
49 "XObject",
50 "Font"
51 };
52 SkASSERT((unsigned)type < SK_ARRAY_COUNT(kResourceTypeNames));
Hal Canaryb10f92e2018-11-16 17:01:50 -050053 return kResourceTypeNames[(unsigned)type];
54}
55
56static SkString resource(SkPDFResourceType type, int index) {
57 char buffer[kMaxResourceNameLength];
58 char* end = get_resource_name(buffer, type, index);
59 return SkString(buffer, (size_t)(end - buffer));
60}
61
Hal Canaryb10f92e2018-11-16 17:01:50 -050062static void add_subdict(const std::vector<SkPDFIndirectReference>& resourceList,
63 SkPDFResourceType type,
64 SkPDFDict* dst) {
65 if (!resourceList.empty()) {
Hal Canary74801582018-12-18 16:30:41 -050066 auto resources = SkPDFMakeDict();
Hal Canaryb10f92e2018-11-16 17:01:50 -050067 for (SkPDFIndirectReference ref : resourceList) {
68 resources->insertRef(resource(type, ref.fValue), ref);
69 }
70 dst->insertObject(resource_name(type), std::move(resources));
71 }
halcanary2b861552015-04-09 13:27:40 -070072}
commit-bot@chromium.org47401352013-07-23 21:49:29 +000073
Hal Canary74801582018-12-18 16:30:41 -050074static std::unique_ptr<SkPDFArray> make_proc_set() {
75 auto procSets = SkPDFMakeArray();
Hal Canaryfba5b6c2018-12-18 12:55:18 -050076 static const char kProcs[][7] = { "PDF", "Text", "ImageB", "ImageC", "ImageI"};
77 procSets->reserve(SK_ARRAY_COUNT(kProcs));
78 for (const char* proc : kProcs) {
79 procSets->appendName(proc);
80 }
81 return procSets;
82}
83
Hal Canary74801582018-12-18 16:30:41 -050084std::unique_ptr<SkPDFDict> SkPDFMakeResourceDict(
85 const std::vector<SkPDFIndirectReference>& graphicStateResources,
86 const std::vector<SkPDFIndirectReference>& shaderResources,
87 const std::vector<SkPDFIndirectReference>& xObjectResources,
88 const std::vector<SkPDFIndirectReference>& fontResources) {
89 auto dict = SkPDFMakeDict();
Hal Canaryfba5b6c2018-12-18 12:55:18 -050090 dict->insertObject("ProcSets", make_proc_set());
Hal Canary9a3f5542018-12-10 19:59:07 -050091 add_subdict(graphicStateResources, SkPDFResourceType::kExtGState, dict.get());
92 add_subdict(shaderResources, SkPDFResourceType::kPattern, dict.get());
93 add_subdict(xObjectResources, SkPDFResourceType::kXObject, dict.get());
94 add_subdict(fontResources, SkPDFResourceType::kFont, dict.get());
halcanary8103a342016-03-08 15:10:16 -080095 return dict;
commit-bot@chromium.org47401352013-07-23 21:49:29 +000096}