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" |
commit-bot@chromium.org | 4740135 | 2013-07-23 21:49:29 +0000 | [diff] [blame] | 10 | #include "SkPostConfig.h" |
| 11 | |
commit-bot@chromium.org | 4740135 | 2013-07-23 21:49:29 +0000 | [diff] [blame] | 12 | // Sanity check that the values of enum SkPDFResourceType correspond to the |
| 13 | // expected values as defined in the arrays below. |
| 14 | // If these are failing, you may need to update the resource_type_prefixes |
| 15 | // and resource_type_names arrays below. |
bungeman | 99fe822 | 2015-08-20 07:57:51 -0700 | [diff] [blame] | 16 | static_assert(SkPDFResourceDict::kExtGState_ResourceType == 0, "resource_type_mismatch"); |
| 17 | static_assert(SkPDFResourceDict::kPattern_ResourceType == 1, "resource_type_mismatch"); |
| 18 | static_assert(SkPDFResourceDict::kXObject_ResourceType == 2, "resource_type_mismatch"); |
| 19 | static_assert(SkPDFResourceDict::kFont_ResourceType == 3, "resource_type_mismatch"); |
commit-bot@chromium.org | 4740135 | 2013-07-23 21:49:29 +0000 | [diff] [blame] | 20 | |
| 21 | static const char resource_type_prefixes[] = { |
| 22 | 'G', |
| 23 | 'P', |
| 24 | 'X', |
| 25 | 'F' |
| 26 | }; |
| 27 | |
| 28 | static const char* resource_type_names[] = { |
| 29 | "ExtGState", |
| 30 | "Pattern", |
| 31 | "XObject", |
| 32 | "Font" |
| 33 | }; |
| 34 | |
| 35 | static char get_resource_type_prefix( |
| 36 | SkPDFResourceDict::SkPDFResourceType type) { |
| 37 | SkASSERT(type >= 0); |
| 38 | SkASSERT(type < SkPDFResourceDict::kResourceTypeCount); |
| 39 | |
| 40 | return resource_type_prefixes[type]; |
| 41 | } |
| 42 | |
| 43 | static const char* get_resource_type_name( |
| 44 | SkPDFResourceDict::SkPDFResourceType type) { |
| 45 | SkASSERT(type >= 0); |
halcanary | 130444f | 2015-04-25 06:45:07 -0700 | [diff] [blame] | 46 | SkASSERT(type < SK_ARRAY_COUNT(resource_type_names)); |
commit-bot@chromium.org | 4740135 | 2013-07-23 21:49:29 +0000 | [diff] [blame] | 47 | |
| 48 | return resource_type_names[type]; |
| 49 | } |
| 50 | |
commit-bot@chromium.org | 4740135 | 2013-07-23 21:49:29 +0000 | [diff] [blame] | 51 | SkString SkPDFResourceDict::getResourceName( |
halcanary | 2b86155 | 2015-04-09 13:27:40 -0700 | [diff] [blame] | 52 | SkPDFResourceDict::SkPDFResourceType type, int key) { |
commit-bot@chromium.org | 4740135 | 2013-07-23 21:49:29 +0000 | [diff] [blame] | 53 | SkString keyString; |
| 54 | keyString.printf("%c%d", get_resource_type_prefix(type), key); |
| 55 | return keyString; |
| 56 | } |
| 57 | |
halcanary | 2b86155 | 2015-04-09 13:27:40 -0700 | [diff] [blame] | 58 | static void add_subdict( |
| 59 | const SkTDArray<SkPDFObject*>& resourceList, |
| 60 | SkPDFResourceDict::SkPDFResourceType type, |
| 61 | SkPDFDict* dst) { |
| 62 | if (0 == resourceList.count()) { |
| 63 | return; |
commit-bot@chromium.org | 4740135 | 2013-07-23 21:49:29 +0000 | [diff] [blame] | 64 | } |
halcanary | ece8392 | 2016-03-08 08:32:12 -0800 | [diff] [blame] | 65 | auto resources = sk_make_sp<SkPDFDict>(); |
halcanary | 2b86155 | 2015-04-09 13:27:40 -0700 | [diff] [blame] | 66 | for (int i = 0; i < resourceList.count(); i++) { |
halcanary | 130444f | 2015-04-25 06:45:07 -0700 | [diff] [blame] | 67 | resources->insertObjRef(SkPDFResourceDict::getResourceName(type, i), |
halcanary | e94ea62 | 2016-03-09 07:52:09 -0800 | [diff] [blame] | 68 | sk_ref_sp(resourceList[i])); |
halcanary | 2b86155 | 2015-04-09 13:27:40 -0700 | [diff] [blame] | 69 | } |
halcanary | 8103a34 | 2016-03-08 15:10:16 -0800 | [diff] [blame] | 70 | dst->insertObject(get_resource_type_name(type), std::move(resources)); |
halcanary | 2b86155 | 2015-04-09 13:27:40 -0700 | [diff] [blame] | 71 | } |
commit-bot@chromium.org | 4740135 | 2013-07-23 21:49:29 +0000 | [diff] [blame] | 72 | |
halcanary | 8103a34 | 2016-03-08 15:10:16 -0800 | [diff] [blame] | 73 | sk_sp<SkPDFDict> SkPDFResourceDict::Make( |
halcanary | 2b86155 | 2015-04-09 13:27:40 -0700 | [diff] [blame] | 74 | const SkTDArray<SkPDFObject*>* gStateResources, |
| 75 | const SkTDArray<SkPDFObject*>* patternResources, |
| 76 | const SkTDArray<SkPDFObject*>* xObjectResources, |
| 77 | const SkTDArray<SkPDFObject*>* fontResources) { |
halcanary | ece8392 | 2016-03-08 08:32:12 -0800 | [diff] [blame] | 78 | auto dict = sk_make_sp<SkPDFDict>(); |
halcanary | 2b86155 | 2015-04-09 13:27:40 -0700 | [diff] [blame] | 79 | static const char kProcs[][7] = { |
| 80 | "PDF", "Text", "ImageB", "ImageC", "ImageI"}; |
halcanary | ece8392 | 2016-03-08 08:32:12 -0800 | [diff] [blame] | 81 | auto procSets = sk_make_sp<SkPDFArray>(); |
halcanary | 2b86155 | 2015-04-09 13:27:40 -0700 | [diff] [blame] | 82 | |
| 83 | procSets->reserve(SK_ARRAY_COUNT(kProcs)); |
| 84 | for (size_t i = 0; i < SK_ARRAY_COUNT(kProcs); i++) { |
| 85 | procSets->appendName(kProcs[i]); |
| 86 | } |
halcanary | 8103a34 | 2016-03-08 15:10:16 -0800 | [diff] [blame] | 87 | dict->insertObject("ProcSets", std::move(procSets)); |
halcanary | 2b86155 | 2015-04-09 13:27:40 -0700 | [diff] [blame] | 88 | |
| 89 | if (gStateResources) { |
halcanary | fcad44b | 2016-03-06 14:47:10 -0800 | [diff] [blame] | 90 | add_subdict(*gStateResources, kExtGState_ResourceType, dict.get()); |
halcanary | 2b86155 | 2015-04-09 13:27:40 -0700 | [diff] [blame] | 91 | } |
| 92 | if (patternResources) { |
halcanary | fcad44b | 2016-03-06 14:47:10 -0800 | [diff] [blame] | 93 | add_subdict(*patternResources, kPattern_ResourceType, dict.get()); |
halcanary | 2b86155 | 2015-04-09 13:27:40 -0700 | [diff] [blame] | 94 | } |
| 95 | if (xObjectResources) { |
halcanary | fcad44b | 2016-03-06 14:47:10 -0800 | [diff] [blame] | 96 | add_subdict(*xObjectResources, kXObject_ResourceType, dict.get()); |
halcanary | 2b86155 | 2015-04-09 13:27:40 -0700 | [diff] [blame] | 97 | } |
| 98 | if (fontResources) { |
halcanary | fcad44b | 2016-03-06 14:47:10 -0800 | [diff] [blame] | 99 | add_subdict(*fontResources, kFont_ResourceType, dict.get()); |
halcanary | 2b86155 | 2015-04-09 13:27:40 -0700 | [diff] [blame] | 100 | } |
halcanary | 8103a34 | 2016-03-08 15:10:16 -0800 | [diff] [blame] | 101 | return dict; |
commit-bot@chromium.org | 4740135 | 2013-07-23 21:49:29 +0000 | [diff] [blame] | 102 | } |