Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
SkPDFResourceDict.cpp
Go to the documentation of this file.
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
10#include "src/pdf/SkPDFTypes.h"
11
12// Verify that the values of enum ResourceType correspond to the expected values
13// as defined in the arrays below.
14// 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");
20
21// One extra character for the Prefix.
23
24// returns pointer just past end of what's written into `dst`.
26 static const char kResourceTypePrefixes[] = {
27 'G', // kExtGState
28 'P', // kPattern
29 'X', // kXObject
30 'F' // kFont
31 };
32 SkASSERT((unsigned)type < std::size(kResourceTypePrefixes));
33 dst[0] = kResourceTypePrefixes[(unsigned)type];
34 return SkStrAppendS32(dst + 1, key);
35}
36
38 // One extra character for the leading '/'.
40 buffer[0] = '/';
41 char* end = get_resource_name(buffer + 1, type, key);
42 dst->write(buffer, (size_t)(end - buffer));
43}
44
46 static const char* kResourceTypeNames[] = {
47 "ExtGState",
48 "Pattern",
49 "XObject",
50 "Font"
51 };
52 SkASSERT((unsigned)type < std::size(kResourceTypeNames));
53 return kResourceTypeNames[(unsigned)type];
54}
55
58 char* end = get_resource_name(buffer, type, index);
59 return SkString(buffer, (size_t)(end - buffer));
60}
61
62static void add_subdict(const std::vector<SkPDFIndirectReference>& resourceList,
64 SkPDFDict* dst) {
65 if (!resourceList.empty()) {
66 auto resources = SkPDFMakeDict();
67 for (SkPDFIndirectReference ref : resourceList) {
68 resources->insertRef(resource(type, ref.fValue), ref);
69 }
70 dst->insertObject(resource_name(type), std::move(resources));
71 }
72}
73
74static std::unique_ptr<SkPDFArray> make_proc_set() {
75 auto procSets = SkPDFMakeArray();
76 static const char kProcs[][7] = { "PDF", "Text", "ImageB", "ImageC", "ImageI"};
77 procSets->reserve(std::size(kProcs));
78 for (const char* proc : kProcs) {
79 procSets->appendName(proc);
80 }
81 return procSets;
82}
83
84std::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();
90 dict->insertObject("ProcSet", make_proc_set());
91 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());
95 return dict;
96}
#define SkASSERT(cond)
Definition SkAssert.h:116
static void add_subdict(const std::vector< SkPDFIndirectReference > &resourceList, SkPDFResourceType type, SkPDFDict *dst)
constexpr size_t kMaxResourceNameLength
void SkPDFWriteResourceName(SkWStream *dst, SkPDFResourceType type, int key)
std::unique_ptr< SkPDFDict > SkPDFMakeResourceDict(const std::vector< SkPDFIndirectReference > &graphicStateResources, const std::vector< SkPDFIndirectReference > &shaderResources, const std::vector< SkPDFIndirectReference > &xObjectResources, const std::vector< SkPDFIndirectReference > &fontResources)
static SkString resource(SkPDFResourceType type, int index)
static const char * resource_name(SkPDFResourceType type)
static std::unique_ptr< SkPDFArray > make_proc_set()
static char * get_resource_name(char dst[kMaxResourceNameLength], SkPDFResourceType type, int key)
SkPDFResourceType
static std::unique_ptr< SkPDFDict > SkPDFMakeDict(const char *type=nullptr)
Definition SkPDFTypes.h:195
static std::unique_ptr< SkPDFArray > SkPDFMakeArray(Args... args)
Definition SkPDFTypes.h:135
static constexpr int kSkStrAppendS32_MaxSize
Definition SkString.h:89
char * SkStrAppendS32(char buffer[], int32_t)
Definition SkString.cpp:120
Type::kYUV Type::kRGBA() int(0.7 *637)
glong glong end
static const uint8_t buffer[]