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