OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2015 Google Inc. | 2 * Copyright 2015 Google Inc. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
6 */ | 6 */ |
7 | 7 |
8 #include "SkImage.h" | 8 #include "SkImage.h" |
9 #include "SkPDFBitmap.h" | 9 #include "SkPDFBitmap.h" |
10 #include "SkPDFCanon.h" | 10 #include "SkPDFCanon.h" |
11 #include "SkPDFFont.h" | 11 #include "SkPDFFont.h" |
12 | 12 |
13 //////////////////////////////////////////////////////////////////////////////// | 13 //////////////////////////////////////////////////////////////////////////////// |
14 | 14 |
15 void SkPDFCanon::reset() { | 15 namespace { |
16 for (int i = 0; i < fFontRecords.count(); ++i) { | 16 template <typename K, typename V> struct UnrefValue { |
17 fFontRecords[i].fFont->unref(); | 17 void operator()(K, V** v) { SkSafeUnref(*v); } |
18 } | 18 }; |
19 fFontRecords.reset(); | 19 } |
20 | 20 |
21 fFunctionShaderRecords.reset(); | 21 SkPDFCanon::~SkPDFCanon() { |
22 fAlphaShaderRecords.reset(); | |
23 fImageShaderRecords.reset(); | |
24 | |
25 // TODO(halcanary): make SkTHashSet work nicely with sk_sp<>, | 22 // TODO(halcanary): make SkTHashSet work nicely with sk_sp<>, |
26 // or use std::unordered_set<> | 23 // or use std::unordered_set<> |
27 fGraphicStateRecords.foreach ([](WrapGS w) { w.fPtr->unref(); }); | 24 fGraphicStateRecords.foreach ([](WrapGS w) { w.fPtr->unref(); }); |
28 fGraphicStateRecords.reset(); | 25 fPDFBitmapMap.foreach(UnrefValue<SkBitmapKey, SkPDFObject>()); |
| 26 fTypefaceMetrics.foreach(UnrefValue<uint32_t, SkAdvancedTypefaceMetrics>()); |
| 27 fFontDescriptors.foreach(UnrefValue<uint32_t, SkPDFDict>()); |
| 28 fFontMap.foreach(UnrefValue<uint64_t, SkPDFFont>()); |
| 29 } |
29 | 30 |
30 fPDFBitmapMap.foreach([](SkBitmapKey, SkPDFObject** p) { (*p)->unref(); }); | 31 void SkPDFCanon::reset() { |
31 fPDFBitmapMap.reset(); | 32 this->~SkPDFCanon(); |
| 33 new (this)SkPDFCanon; |
32 } | 34 } |
33 | 35 |
34 //////////////////////////////////////////////////////////////////////////////// | 36 //////////////////////////////////////////////////////////////////////////////// |
35 | |
36 SkPDFFont* SkPDFCanon::findFont(uint32_t fontID, | |
37 uint16_t glyphID, | |
38 SkPDFFont** relatedFontPtr) const { | |
39 SkASSERT(relatedFontPtr); | |
40 | |
41 SkPDFFont* relatedFont = nullptr; | |
42 for (int i = 0; i < fFontRecords.count(); ++i) { | |
43 SkPDFFont::Match match = SkPDFFont::IsMatch( | |
44 fFontRecords[i].fFont, fFontRecords[i].fFontID, | |
45 fFontRecords[i].fGlyphID, fontID, glyphID); | |
46 if (SkPDFFont::kExact_Match == match) { | |
47 return fFontRecords[i].fFont; | |
48 } else if (!relatedFont && SkPDFFont::kRelated_Match == match) { | |
49 relatedFont = fFontRecords[i].fFont; | |
50 } | |
51 } | |
52 *relatedFontPtr = relatedFont; // May still be nullptr. | |
53 return nullptr; | |
54 } | |
55 | |
56 void SkPDFCanon::addFont(SkPDFFont* font, uint32_t fontID, uint16_t fGlyphID) { | |
57 SkPDFCanon::FontRec* rec = fFontRecords.push(); | |
58 rec->fFont = SkRef(font); | |
59 rec->fFontID = fontID; | |
60 rec->fGlyphID = fGlyphID; | |
61 } | |
62 | |
63 //////////////////////////////////////////////////////////////////////////////// | |
64 | 37 |
65 template <typename T> | 38 template <typename T> |
66 sk_sp<SkPDFObject> find_shader(const SkTArray<T>& records, | 39 sk_sp<SkPDFObject> find_shader(const SkTArray<T>& records, |
67 const SkPDFShader::State& state) { | 40 const SkPDFShader::State& state) { |
68 for (const T& record : records) { | 41 for (const T& record : records) { |
69 if (record.fShaderState == state) { | 42 if (record.fShaderState == state) { |
70 return record.fShaderObject; | 43 return record.fShaderObject; |
71 } | 44 } |
72 } | 45 } |
73 return nullptr; | 46 return nullptr; |
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
143 fNoSmaskGraphicState = SkPDFGraphicState::MakeNoSmaskGraphicState(); | 116 fNoSmaskGraphicState = SkPDFGraphicState::MakeNoSmaskGraphicState(); |
144 return fNoSmaskGraphicState; | 117 return fNoSmaskGraphicState; |
145 } | 118 } |
146 sk_sp<SkPDFArray> SkPDFCanon::makeRangeObject() { | 119 sk_sp<SkPDFArray> SkPDFCanon::makeRangeObject() { |
147 if (fRangeObject) { | 120 if (fRangeObject) { |
148 return fRangeObject; | 121 return fRangeObject; |
149 } | 122 } |
150 fRangeObject = SkPDFShader::MakeRangeObject(); | 123 fRangeObject = SkPDFShader::MakeRangeObject(); |
151 return fRangeObject; | 124 return fRangeObject; |
152 } | 125 } |
OLD | NEW |