commit-bot@chromium.org | 4740135 | 2013-07-23 21:49:29 +0000 | [diff] [blame] | 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" |
martina.kollarova | b8d6af1 | 2016-06-29 05:12:31 -0700 | [diff] [blame] | 9 | #include "SkPDFTypes.h" |
Hal Canary | e650b85 | 2018-09-12 09:12:36 -0400 | [diff] [blame] | 10 | #include "SkStream.h" |
commit-bot@chromium.org | 4740135 | 2013-07-23 21:49:29 +0000 | [diff] [blame] | 11 | |
Hal Canary | e650b85 | 2018-09-12 09:12:36 -0400 | [diff] [blame] | 12 | // Sanity check that the values of enum ResourceType correspond to the |
commit-bot@chromium.org | 4740135 | 2013-07-23 21:49:29 +0000 | [diff] [blame] | 13 | // expected values as defined in the arrays below. |
Hal Canary | e650b85 | 2018-09-12 09:12:36 -0400 | [diff] [blame] | 14 | // If these are failing, you may need to update the kResourceTypePrefixes |
| 15 | // and kResourceTypeNames arrays below. |
| 16 | static_assert(0 == (int)SkPDFResourceType::kExtGState, "resource_type_mismatch"); |
| 17 | static_assert(1 == (int)SkPDFResourceType::kPattern, "resource_type_mismatch"); |
| 18 | static_assert(2 == (int)SkPDFResourceType::kXObject, "resource_type_mismatch"); |
| 19 | static_assert(3 == (int)SkPDFResourceType::kFont, "resource_type_mismatch"); |
commit-bot@chromium.org | 4740135 | 2013-07-23 21:49:29 +0000 | [diff] [blame] | 20 | |
Hal Canary | e650b85 | 2018-09-12 09:12:36 -0400 | [diff] [blame] | 21 | // One extra character for the Prefix. |
| 22 | constexpr size_t kMaxResourceNameLength = 1 + SkStrAppendS32_MaxSize; |
commit-bot@chromium.org | 4740135 | 2013-07-23 21:49:29 +0000 | [diff] [blame] | 23 | |
Hal Canary | e650b85 | 2018-09-12 09:12:36 -0400 | [diff] [blame] | 24 | // returns pointer just past end of what's written into `dst`. |
| 25 | static 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.org | 4740135 | 2013-07-23 21:49:29 +0000 | [diff] [blame] | 35 | } |
| 36 | |
Hal Canary | e650b85 | 2018-09-12 09:12:36 -0400 | [diff] [blame] | 37 | void 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.org | 4740135 | 2013-07-23 21:49:29 +0000 | [diff] [blame] | 43 | } |
| 44 | |
Hal Canary | b10f92e | 2018-11-16 17:01:50 -0500 | [diff] [blame] | 45 | static const char* resource_name(SkPDFResourceType type) { |
Hal Canary | e650b85 | 2018-09-12 09:12:36 -0400 | [diff] [blame] | 46 | static const char* kResourceTypeNames[] = { |
| 47 | "ExtGState", |
| 48 | "Pattern", |
| 49 | "XObject", |
| 50 | "Font" |
| 51 | }; |
| 52 | SkASSERT((unsigned)type < SK_ARRAY_COUNT(kResourceTypeNames)); |
Hal Canary | b10f92e | 2018-11-16 17:01:50 -0500 | [diff] [blame] | 53 | return kResourceTypeNames[(unsigned)type]; |
| 54 | } |
| 55 | |
| 56 | static 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 Canary | b10f92e | 2018-11-16 17:01:50 -0500 | [diff] [blame] | 62 | static 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 | } |
halcanary | 2b86155 | 2015-04-09 13:27:40 -0700 | [diff] [blame] | 72 | } |
commit-bot@chromium.org | 4740135 | 2013-07-23 21:49:29 +0000 | [diff] [blame] | 73 | |
Hal Canary | 9a3f554 | 2018-12-10 19:59:07 -0500 | [diff] [blame^] | 74 | sk_sp<SkPDFDict> SkPDFMakeResourceDict(std::vector<SkPDFIndirectReference> graphicStateResources, |
| 75 | std::vector<SkPDFIndirectReference> shaderResources, |
| 76 | std::vector<SkPDFIndirectReference> xObjectResources, |
Hal Canary | b10f92e | 2018-11-16 17:01:50 -0500 | [diff] [blame] | 77 | std::vector<SkPDFIndirectReference> fontResources) { |
halcanary | ece8392 | 2016-03-08 08:32:12 -0800 | [diff] [blame] | 78 | auto dict = sk_make_sp<SkPDFDict>(); |
Hal Canary | 9a3f554 | 2018-12-10 19:59:07 -0500 | [diff] [blame^] | 79 | 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()); |
halcanary | 8103a34 | 2016-03-08 15:10:16 -0800 | [diff] [blame] | 83 | return dict; |
commit-bot@chromium.org | 4740135 | 2013-07-23 21:49:29 +0000 | [diff] [blame] | 84 | } |