OLD | NEW |
1 /* | 1 /* |
2 * Copyright (C) 2008 Apple Inc. All rights reserved. | 2 * Copyright (C) 2008 Apple Inc. All rights reserved. |
3 * | 3 * |
4 * This library is free software; you can redistribute it and/or | 4 * This library is free software; you can redistribute it and/or |
5 * modify it under the terms of the GNU Library General Public | 5 * modify it under the terms of the GNU Library General Public |
6 * License as published by the Free Software Foundation; either | 6 * License as published by the Free Software Foundation; either |
7 * version 2 of the License, or (at your option) any later version. | 7 * version 2 of the License, or (at your option) any later version. |
8 * | 8 * |
9 * This library is distributed in the hope that it will be useful, | 9 * This library is distributed in the hope that it will be useful, |
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of | 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
12 * Library General Public License for more details. | 12 * Library General Public License for more details. |
13 * | 13 * |
14 * You should have received a copy of the GNU Library General Public License | 14 * You should have received a copy of the GNU Library General Public License |
15 * along with this library; see the file COPYING.LIB. If not, write to | 15 * along with this library; see the file COPYING.LIB. If not, write to |
16 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, | 16 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, |
17 * Boston, MA 02110-1301, USA. | 17 * Boston, MA 02110-1301, USA. |
18 */ | 18 */ |
19 | 19 |
20 #ifndef SVGGlyphMap_h | 20 #ifndef SVGGlyphMap_h |
21 #define SVGGlyphMap_h | 21 #define SVGGlyphMap_h |
22 | 22 |
23 #if ENABLE(SVG_FONTS) | 23 #if ENABLE(SVG_FONTS) |
| 24 #include "core/svg/SVGParserUtilities.h" |
24 #include "platform/fonts/Latin1TextIterator.h" | 25 #include "platform/fonts/Latin1TextIterator.h" |
25 #include "platform/fonts/SVGGlyph.h" | 26 #include "platform/fonts/SVGGlyph.h" |
26 #include "platform/text/SurrogatePairAwareTextIterator.h" | 27 #include "platform/text/SurrogatePairAwareTextIterator.h" |
27 #include "wtf/HashMap.h" | 28 #include "wtf/HashMap.h" |
28 #include "wtf/Vector.h" | 29 #include "wtf/Vector.h" |
29 | 30 |
30 namespace WebCore { | 31 namespace WebCore { |
31 | 32 |
32 struct GlyphMapNode; | 33 struct GlyphMapNode; |
33 class SVGFontData; | 34 class SVGFontData; |
34 | 35 |
35 typedef HashMap<UChar32, RefPtr<GlyphMapNode> > GlyphMapLayer; | 36 typedef HashMap<UChar32, RefPtr<GlyphMapNode> > GlyphMapLayer; |
36 | 37 |
37 struct GlyphMapNode : public RefCounted<GlyphMapNode> { | 38 struct GlyphMapNode : public RefCounted<GlyphMapNode> { |
38 private: | 39 private: |
39 GlyphMapNode() { } | 40 GlyphMapNode() { } |
40 public: | 41 public: |
41 static PassRefPtr<GlyphMapNode> create() { return adoptRef(new GlyphMapNode)
; } | 42 static PassRefPtr<GlyphMapNode> create() { return adoptRef(new GlyphMapNode)
; } |
42 | 43 |
43 Vector<SVGGlyph> glyphs; | 44 Vector<SVGGlyph> glyphs; |
44 | 45 |
45 GlyphMapLayer children; | 46 GlyphMapLayer children; |
46 }; | 47 }; |
47 | 48 |
48 class SVGGlyphMap { | 49 class SVGGlyphMap { |
49 public: | 50 public: |
50 SVGGlyphMap() : m_currentPriority(0) { } | 51 SVGGlyphMap() : m_currentPriority(0) { } |
51 | 52 |
52 void addGlyph(const String& glyphName, const String& unicodeString, SVGGlyph
glyph) | 53 void addGlyph(const String& glyphIdentifier, const String& unicodeString, SV
GGlyph glyph) |
53 { | 54 { |
54 ASSERT(!glyphName.isEmpty() || !unicodeString.isEmpty()); | 55 ASSERT(!glyphIdentifier.isEmpty() || !unicodeString.isEmpty()); |
55 | 56 |
56 bool hasGlyphName = !glyphName.isEmpty(); | 57 bool hasGlyphIdentifier = !glyphIdentifier.isEmpty(); |
57 if (unicodeString.isEmpty()) { | 58 if (unicodeString.isEmpty()) { |
58 // Register named glyph in the named glyph map and in the glyph tabl
e. | 59 // Register glyphs with 'id's in the id glyph map and in the glyph t
able. |
59 ASSERT(hasGlyphName); | 60 ASSERT(hasGlyphIdentifier); |
60 appendToGlyphTable(glyph); | 61 appendToGlyphTable(glyph); |
61 m_namedGlyphs.add(glyphName, glyph.tableEntry); | 62 m_idGlyphs.add(glyphIdentifier, glyph.tableEntry); |
62 return; | 63 return; |
63 } | 64 } |
64 | 65 |
65 unsigned length = unicodeString.length(); | 66 unsigned length = unicodeString.length(); |
66 | 67 |
67 RefPtr<GlyphMapNode> node; | 68 RefPtr<GlyphMapNode> node; |
68 if (unicodeString.is8Bit()) { | 69 if (unicodeString.is8Bit()) { |
69 Latin1TextIterator textIterator(unicodeString.characters8(), 0, leng
th, length); | 70 Latin1TextIterator textIterator(unicodeString.characters8(), 0, leng
th, length); |
70 node = findOrCreateNode(textIterator); | 71 node = findOrCreateNode(textIterator); |
71 } else { | 72 } else { |
72 SurrogatePairAwareTextIterator textIterator(unicodeString.characters
16(), 0, length, length); | 73 SurrogatePairAwareTextIterator textIterator(unicodeString.characters
16(), 0, length, length); |
73 node = findOrCreateNode(textIterator); | 74 node = findOrCreateNode(textIterator); |
74 } | 75 } |
75 if (!node) | 76 if (!node) |
76 return; | 77 return; |
77 | 78 |
78 // Register glyph associated with an unicode string into the glyph map. | 79 // Register glyph associated with an unicode string into the glyph map. |
79 node->glyphs.append(glyph); | 80 node->glyphs.append(glyph); |
80 SVGGlyph& lastGlyph = node->glyphs.last(); | 81 SVGGlyph& lastGlyph = node->glyphs.last(); |
81 lastGlyph.priority = m_currentPriority++; | 82 lastGlyph.priority = m_currentPriority++; |
82 lastGlyph.unicodeStringLength = length; | 83 lastGlyph.unicodeStringLength = length; |
83 | 84 |
84 // If the glyph is named, also add it to the named glyph name, and to th
e glyph table in both cases. | 85 // If the glyph is named, also add it to the named glyph name, and to th
e glyph table in both cases. |
85 appendToGlyphTable(lastGlyph); | 86 appendToGlyphTable(lastGlyph); |
86 if (hasGlyphName) | 87 if (!lastGlyph.glyphName.isEmpty()) |
87 m_namedGlyphs.add(glyphName, lastGlyph.tableEntry); | 88 m_namedGlyphs.add(lastGlyph.glyphName, lastGlyph.tableEntry); |
| 89 if (hasGlyphIdentifier) |
| 90 m_idGlyphs.add(glyphIdentifier, lastGlyph.tableEntry); |
88 } | 91 } |
89 | 92 |
90 void appendToGlyphTable(SVGGlyph& glyph) | 93 void appendToGlyphTable(SVGGlyph& glyph) |
91 { | 94 { |
92 size_t tableEntry = m_glyphTable.size(); | 95 size_t tableEntry = m_glyphTable.size(); |
93 ASSERT(tableEntry < std::numeric_limits<unsigned short>::max()); | 96 ASSERT(tableEntry < std::numeric_limits<unsigned short>::max()); |
94 | 97 |
95 // The first table entry starts with 1. 0 denotes an unknown glyph. | 98 // The first table entry starts with 1. 0 denotes an unknown glyph. |
96 glyph.tableEntry = tableEntry + 1; | 99 glyph.tableEntry = tableEntry + 1; |
97 m_glyphTable.append(glyph); | 100 m_glyphTable.append(glyph); |
(...skipping 15 matching lines...) Expand all Loading... |
113 Latin1TextIterator textIterator(string.characters8(), 0, length, len
gth); | 116 Latin1TextIterator textIterator(string.characters8(), 0, length, len
gth); |
114 collectGlyphsForIterator(textIterator, glyphs); | 117 collectGlyphsForIterator(textIterator, glyphs); |
115 } else { | 118 } else { |
116 SurrogatePairAwareTextIterator textIterator(string.characters16(), 0
, length, length); | 119 SurrogatePairAwareTextIterator textIterator(string.characters16(), 0
, length, length); |
117 collectGlyphsForIterator(textIterator, glyphs); | 120 collectGlyphsForIterator(textIterator, glyphs); |
118 } | 121 } |
119 | 122 |
120 std::sort(glyphs.begin(), glyphs.end(), compareGlyphPriority); | 123 std::sort(glyphs.begin(), glyphs.end(), compareGlyphPriority); |
121 } | 124 } |
122 | 125 |
| 126 void collectGlyphsForStringExact(const String& string, Vector<SVGGlyph>& gly
phs) const |
| 127 { |
| 128 unsigned length = string.length(); |
| 129 |
| 130 if (!length) |
| 131 return; |
| 132 |
| 133 RefPtr<GlyphMapNode> node; |
| 134 if (string.is8Bit()) { |
| 135 Latin1TextIterator textIterator(string.characters8(), 0, length, len
gth); |
| 136 node = findNode(textIterator); |
| 137 } else { |
| 138 SurrogatePairAwareTextIterator textIterator(string.characters16(), 0
, length, length); |
| 139 node = findNode(textIterator); |
| 140 } |
| 141 |
| 142 if (node) |
| 143 glyphs.appendVector(node->glyphs); |
| 144 } |
| 145 |
| 146 void collectGlyphsForUnicodeRange(const UnicodeRange& unicodeRange, Vector<S
VGGlyph>& glyphs) const |
| 147 { |
| 148 for (unsigned character = unicodeRange.first; character <= unicodeRange.
second; ++character) { |
| 149 if (RefPtr<GlyphMapNode> node = m_rootLayer.get(character)) |
| 150 glyphs.appendVector(node->glyphs); |
| 151 } |
| 152 } |
| 153 |
123 void clear() | 154 void clear() |
124 { | 155 { |
125 m_rootLayer.clear(); | 156 m_rootLayer.clear(); |
126 m_glyphTable.clear(); | 157 m_glyphTable.clear(); |
| 158 m_idGlyphs.clear(); |
| 159 m_namedGlyphs.clear(); |
127 m_currentPriority = 0; | 160 m_currentPriority = 0; |
128 } | 161 } |
129 | 162 |
| 163 void dropNamedGlyphMap() |
| 164 { |
| 165 m_namedGlyphs.clear(); |
| 166 } |
| 167 |
130 const SVGGlyph& svgGlyphForGlyph(Glyph glyph) const | 168 const SVGGlyph& svgGlyphForGlyph(Glyph glyph) const |
131 { | 169 { |
132 if (!glyph || glyph > m_glyphTable.size()) { | 170 if (!glyph || glyph > m_glyphTable.size()) { |
133 DEFINE_STATIC_LOCAL(SVGGlyph, defaultGlyph, ()); | 171 DEFINE_STATIC_LOCAL(SVGGlyph, defaultGlyph, ()); |
134 return defaultGlyph; | 172 return defaultGlyph; |
135 } | 173 } |
136 return m_glyphTable[glyph - 1]; | 174 return m_glyphTable[glyph - 1]; |
137 } | 175 } |
138 | 176 |
| 177 const SVGGlyph& glyphIdentifierForAltGlyphReference(const String& glyphIdent
ifier) const |
| 178 { |
| 179 return svgGlyphForGlyph(m_idGlyphs.get(glyphIdentifier)); |
| 180 } |
| 181 |
139 const SVGGlyph& glyphIdentifierForGlyphName(const String& glyphName) const | 182 const SVGGlyph& glyphIdentifierForGlyphName(const String& glyphName) const |
140 { | 183 { |
141 return svgGlyphForGlyph(m_namedGlyphs.get(glyphName)); | 184 return svgGlyphForGlyph(m_namedGlyphs.get(glyphName)); |
142 } | 185 } |
143 | 186 |
144 private: | 187 private: |
145 template<typename Iterator> | 188 template<typename Iterator> |
146 PassRefPtr<GlyphMapNode> findOrCreateNode(Iterator& textIterator) | 189 PassRefPtr<GlyphMapNode> findOrCreateNode(Iterator& textIterator) |
147 { | 190 { |
148 GlyphMapLayer* currentLayer = &m_rootLayer; | 191 GlyphMapLayer* currentLayer = &m_rootLayer; |
149 | 192 |
150 RefPtr<GlyphMapNode> node; | 193 RefPtr<GlyphMapNode> node; |
151 UChar32 character = 0; | 194 UChar32 character = 0; |
152 unsigned clusterLength = 0; | 195 unsigned clusterLength = 0; |
153 while (textIterator.consume(character, clusterLength)) { | 196 while (textIterator.consume(character, clusterLength)) { |
154 node = currentLayer->get(character); | 197 node = currentLayer->get(character); |
155 if (!node) { | 198 if (!node) { |
156 node = GlyphMapNode::create(); | 199 node = GlyphMapNode::create(); |
157 currentLayer->set(character, node); | 200 currentLayer->set(character, node); |
158 } | 201 } |
159 currentLayer = &node->children; | 202 currentLayer = &node->children; |
160 textIterator.advance(clusterLength); | 203 textIterator.advance(clusterLength); |
161 } | 204 } |
162 | 205 |
163 return node.release(); | 206 return node.release(); |
164 } | 207 } |
165 | 208 |
166 template<typename Iterator> | 209 template<typename Iterator> |
| 210 PassRefPtr<GlyphMapNode> findNode(Iterator& textIterator) const |
| 211 { |
| 212 const GlyphMapLayer* currentLayer = &m_rootLayer; |
| 213 |
| 214 RefPtr<GlyphMapNode> node; |
| 215 UChar32 character = 0; |
| 216 unsigned clusterLength = 0; |
| 217 while (textIterator.consume(character, clusterLength)) { |
| 218 node = currentLayer->get(character); |
| 219 if (!node) |
| 220 break; |
| 221 currentLayer = &node->children; |
| 222 textIterator.advance(clusterLength); |
| 223 } |
| 224 |
| 225 return node.release(); |
| 226 } |
| 227 |
| 228 template<typename Iterator> |
167 void collectGlyphsForIterator(Iterator& textIterator, Vector<SVGGlyph>& glyp
hs) | 229 void collectGlyphsForIterator(Iterator& textIterator, Vector<SVGGlyph>& glyp
hs) |
168 { | 230 { |
169 GlyphMapLayer* currentLayer = &m_rootLayer; | 231 GlyphMapLayer* currentLayer = &m_rootLayer; |
170 | 232 |
171 UChar32 character = 0; | 233 UChar32 character = 0; |
172 unsigned clusterLength = 0; | 234 unsigned clusterLength = 0; |
173 while (textIterator.consume(character, clusterLength)) { | 235 while (textIterator.consume(character, clusterLength)) { |
174 RefPtr<GlyphMapNode> node = currentLayer->get(character); | 236 RefPtr<GlyphMapNode> node = currentLayer->get(character); |
175 if (!node) | 237 if (!node) |
176 break; | 238 break; |
177 glyphs.append(node->glyphs); | 239 glyphs.append(node->glyphs); |
178 currentLayer = &node->children; | 240 currentLayer = &node->children; |
179 textIterator.advance(clusterLength); | 241 textIterator.advance(clusterLength); |
180 } | 242 } |
181 } | 243 } |
182 | 244 |
183 GlyphMapLayer m_rootLayer; | 245 GlyphMapLayer m_rootLayer; |
184 Vector<SVGGlyph> m_glyphTable; | 246 Vector<SVGGlyph> m_glyphTable; |
185 HashMap<String, Glyph> m_namedGlyphs; | 247 HashMap<String, Glyph> m_namedGlyphs; |
| 248 HashMap<String, Glyph> m_idGlyphs; |
186 int m_currentPriority; | 249 int m_currentPriority; |
187 }; | 250 }; |
188 | 251 |
189 } | 252 } |
190 | 253 |
191 #endif // ENABLE(SVG_FONTS) | 254 #endif // ENABLE(SVG_FONTS) |
192 #endif // SVGGlyphMap_h | 255 #endif // SVGGlyphMap_h |
OLD | NEW |