xref: /aosp_15_r20/external/skia/src/pdf/SkPDFResourceDict.cpp (revision c8dee2aa9b3f27cf6c858bd81872bdeb2c07ed17)
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 "src/pdf/SkPDFResourceDict.h"
9 
10 #include "include/core/SkStream.h"
11 #include "include/core/SkString.h"
12 #include "include/private/base/SkAssert.h"
13 #include "src/pdf/SkPDFTypes.h"
14 
15 #include <cstddef>
16 #include <utility>
17 
18 // Verify that the values of enum ResourceType correspond to the expected values
19 // as defined in the arrays below.
20 // If these are failing, you may need to update the kResourceTypePrefixes
21 // and kResourceTypeNames arrays below.
22 static_assert(0 == (int)SkPDFResourceType::kExtGState, "resource_type_mismatch");
23 static_assert(1 == (int)SkPDFResourceType::kPattern,   "resource_type_mismatch");
24 static_assert(2 == (int)SkPDFResourceType::kXObject,   "resource_type_mismatch");
25 static_assert(3 == (int)SkPDFResourceType::kFont,      "resource_type_mismatch");
26 
27 // One extra character for the Prefix.
28 constexpr size_t kMaxResourceNameLength = 1 + kSkStrAppendS32_MaxSize;
29 
30 // returns pointer just past end of what's written into `dst`.
get_resource_name(char dst[kMaxResourceNameLength],SkPDFResourceType type,int key)31 static char* get_resource_name(char dst[kMaxResourceNameLength], SkPDFResourceType type, int key) {
32     static const char kResourceTypePrefixes[] = {
33         'G',  // kExtGState
34         'P',  // kPattern
35         'X',  // kXObject
36         'F'   // kFont
37     };
38     SkASSERT((unsigned)type < std::size(kResourceTypePrefixes));
39     dst[0] = kResourceTypePrefixes[(unsigned)type];
40     return SkStrAppendS32(dst + 1, key);
41 }
42 
SkPDFWriteResourceName(SkWStream * dst,SkPDFResourceType type,int key)43 void SkPDFWriteResourceName(SkWStream* dst, SkPDFResourceType type, int key) {
44     // One extra character for the leading '/'.
45     char buffer[1 + kMaxResourceNameLength];
46     buffer[0] = '/';
47     char* end = get_resource_name(buffer + 1, type, key);
48     dst->write(buffer, (size_t)(end - buffer));
49 }
50 
resource_name(SkPDFResourceType type)51 static const char* resource_name(SkPDFResourceType type) {
52     static const char* kResourceTypeNames[] = {
53         "ExtGState",
54         "Pattern",
55         "XObject",
56         "Font"
57     };
58     SkASSERT((unsigned)type < std::size(kResourceTypeNames));
59     return kResourceTypeNames[(unsigned)type];
60 }
61 
resource(SkPDFResourceType type,int index)62 static SkString resource(SkPDFResourceType type, int index) {
63     char buffer[kMaxResourceNameLength];
64     char* end = get_resource_name(buffer, type, index);
65     return SkString(buffer, (size_t)(end - buffer));
66 }
67 
add_subdict(const std::vector<SkPDFIndirectReference> & resourceList,SkPDFResourceType type,SkPDFDict * dst)68 static void add_subdict(const std::vector<SkPDFIndirectReference>& resourceList,
69                         SkPDFResourceType type,
70                         SkPDFDict* dst) {
71     if (!resourceList.empty()) {
72         auto resources = SkPDFMakeDict();
73         for (SkPDFIndirectReference ref : resourceList) {
74             resources->insertRef(resource(type, ref.fValue), ref);
75         }
76         dst->insertObject(resource_name(type), std::move(resources));
77     }
78 }
79 
make_proc_set()80 static std::unique_ptr<SkPDFArray> make_proc_set() {
81     auto procSets = SkPDFMakeArray();
82     static const char kProcs[][7] = { "PDF", "Text", "ImageB", "ImageC", "ImageI"};
83     procSets->reserve(std::size(kProcs));
84     for (const char* proc : kProcs) {
85         procSets->appendName(proc);
86     }
87     return procSets;
88 }
89 
SkPDFMakeResourceDict(const std::vector<SkPDFIndirectReference> & graphicStateResources,const std::vector<SkPDFIndirectReference> & shaderResources,const std::vector<SkPDFIndirectReference> & xObjectResources,const std::vector<SkPDFIndirectReference> & fontResources)90 std::unique_ptr<SkPDFDict> SkPDFMakeResourceDict(
91         const std::vector<SkPDFIndirectReference>& graphicStateResources,
92         const std::vector<SkPDFIndirectReference>& shaderResources,
93         const std::vector<SkPDFIndirectReference>& xObjectResources,
94         const std::vector<SkPDFIndirectReference>& fontResources) {
95     auto dict = SkPDFMakeDict();
96     dict->insertObject("ProcSet", make_proc_set());
97     add_subdict(graphicStateResources, SkPDFResourceType::kExtGState, dict.get());
98     add_subdict(shaderResources,       SkPDFResourceType::kPattern,   dict.get());
99     add_subdict(xObjectResources,      SkPDFResourceType::kXObject,   dict.get());
100     add_subdict(fontResources,         SkPDFResourceType::kFont,      dict.get());
101     return dict;
102 }
103