blob: 545d8016795571ce59c18d7559cc84cc4904bbca [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()) {
66 auto resources = sk_make_sp<SkPDFDict>();
67 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 Canary9a3f5542018-12-10 19:59:07 -050074sk_sp<SkPDFDict> SkPDFMakeResourceDict(std::vector<SkPDFIndirectReference> graphicStateResources,
75 std::vector<SkPDFIndirectReference> shaderResources,
76 std::vector<SkPDFIndirectReference> xObjectResources,
Hal Canaryb10f92e2018-11-16 17:01:50 -050077 std::vector<SkPDFIndirectReference> fontResources) {
halcanaryece83922016-03-08 08:32:12 -080078 auto dict = sk_make_sp<SkPDFDict>();
Hal Canary9a3f5542018-12-10 19:59:07 -050079 add_subdict(graphicStateResources, SkPDFResourceType::kExtGState, dict.get());
80 add_subdict(shaderResources, SkPDFResourceType::kPattern, dict.get());
81 add_subdict(xObjectResources, SkPDFResourceType::kXObject, dict.get());
82 add_subdict(fontResources, SkPDFResourceType::kFont, dict.get());
halcanary8103a342016-03-08 15:10:16 -080083 return dict;
commit-bot@chromium.org47401352013-07-23 21:49:29 +000084}