1 /* 2 * Copyright (C) 2008 Apple Inc. All rights reserved. 3 * 4 * This library is free software; you can redistribute it and/or 5 * modify it under the terms of the GNU Library General Public 6 * License as published by the Free Software Foundation; either 7 * version 2 of the License, or (at your option) any later version. 8 * 9 * This library is distributed in the hope that it will be useful, 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 12 * Library General Public License for more details. 13 * 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 16 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 17 * Boston, MA 02110-1301, USA. 18 */ 19 20 #ifndef SVGGlyphMap_h 21 #define SVGGlyphMap_h 22 23 #if ENABLE(SVG_FONTS) 24 #include "platform/fonts/Latin1TextIterator.h" 25 #include "platform/fonts/SVGGlyph.h" 26 #include "platform/text/SurrogatePairAwareTextIterator.h" 27 #include "wtf/HashMap.h" 28 #include "wtf/Vector.h" 29 30 namespace WebCore { 31 32 struct GlyphMapNode; 33 class SVGFontData; 34 35 typedef HashMap<UChar32, RefPtr<GlyphMapNode> > GlyphMapLayer; 36 37 struct GlyphMapNode : public RefCounted<GlyphMapNode> { 38 private: GlyphMapNodeGlyphMapNode39 GlyphMapNode() { } 40 public: createGlyphMapNode41 static PassRefPtr<GlyphMapNode> create() { return adoptRef(new GlyphMapNode); } 42 43 Vector<SVGGlyph> glyphs; 44 45 GlyphMapLayer children; 46 }; 47 48 class SVGGlyphMap { 49 public: SVGGlyphMap()50 SVGGlyphMap() : m_currentPriority(0) { } 51 addGlyph(const String & glyphName,const String & unicodeString,SVGGlyph glyph)52 void addGlyph(const String& glyphName, const String& unicodeString, SVGGlyph glyph) 53 { 54 ASSERT(!glyphName.isEmpty() || !unicodeString.isEmpty()); 55 56 bool hasGlyphName = !glyphName.isEmpty(); 57 if (unicodeString.isEmpty()) { 58 // Register named glyph in the named glyph map and in the glyph table. 59 ASSERT(hasGlyphName); 60 appendToGlyphTable(glyph); 61 m_namedGlyphs.add(glyphName, glyph.tableEntry); 62 return; 63 } 64 65 unsigned length = unicodeString.length(); 66 67 RefPtr<GlyphMapNode> node; 68 if (unicodeString.is8Bit()) { 69 Latin1TextIterator textIterator(unicodeString.characters8(), 0, length, length); 70 node = findOrCreateNode(textIterator); 71 } else { 72 SurrogatePairAwareTextIterator textIterator(unicodeString.characters16(), 0, length, length); 73 node = findOrCreateNode(textIterator); 74 } 75 if (!node) 76 return; 77 78 // Register glyph associated with an unicode string into the glyph map. 79 node->glyphs.append(glyph); 80 SVGGlyph& lastGlyph = node->glyphs.last(); 81 lastGlyph.priority = m_currentPriority++; 82 lastGlyph.unicodeStringLength = length; 83 84 // If the glyph is named, also add it to the named glyph name, and to the glyph table in both cases. 85 appendToGlyphTable(lastGlyph); 86 if (hasGlyphName) 87 m_namedGlyphs.add(glyphName, lastGlyph.tableEntry); 88 } 89 appendToGlyphTable(SVGGlyph & glyph)90 void appendToGlyphTable(SVGGlyph& glyph) 91 { 92 size_t tableEntry = m_glyphTable.size(); 93 ASSERT(tableEntry < std::numeric_limits<unsigned short>::max()); 94 95 // The first table entry starts with 1. 0 denotes an unknown glyph. 96 glyph.tableEntry = tableEntry + 1; 97 m_glyphTable.append(glyph); 98 } 99 compareGlyphPriority(const SVGGlyph & first,const SVGGlyph & second)100 static inline bool compareGlyphPriority(const SVGGlyph& first, const SVGGlyph& second) 101 { 102 return first.priority < second.priority; 103 } 104 collectGlyphsForString(const String & string,Vector<SVGGlyph> & glyphs)105 void collectGlyphsForString(const String& string, Vector<SVGGlyph>& glyphs) 106 { 107 unsigned length = string.length(); 108 109 if (!length) 110 return; 111 112 if (string.is8Bit()) { 113 Latin1TextIterator textIterator(string.characters8(), 0, length, length); 114 collectGlyphsForIterator(textIterator, glyphs); 115 } else { 116 SurrogatePairAwareTextIterator textIterator(string.characters16(), 0, length, length); 117 collectGlyphsForIterator(textIterator, glyphs); 118 } 119 120 std::sort(glyphs.begin(), glyphs.end(), compareGlyphPriority); 121 } 122 clear()123 void clear() 124 { 125 m_rootLayer.clear(); 126 m_glyphTable.clear(); 127 m_currentPriority = 0; 128 } 129 svgGlyphForGlyph(Glyph glyph)130 const SVGGlyph& svgGlyphForGlyph(Glyph glyph) const 131 { 132 if (!glyph || glyph > m_glyphTable.size()) { 133 DEFINE_STATIC_LOCAL(SVGGlyph, defaultGlyph, ()); 134 return defaultGlyph; 135 } 136 return m_glyphTable[glyph - 1]; 137 } 138 glyphIdentifierForGlyphName(const String & glyphName)139 const SVGGlyph& glyphIdentifierForGlyphName(const String& glyphName) const 140 { 141 return svgGlyphForGlyph(m_namedGlyphs.get(glyphName)); 142 } 143 144 private: 145 template<typename Iterator> findOrCreateNode(Iterator & textIterator)146 PassRefPtr<GlyphMapNode> findOrCreateNode(Iterator& textIterator) 147 { 148 GlyphMapLayer* currentLayer = &m_rootLayer; 149 150 RefPtr<GlyphMapNode> node; 151 UChar32 character = 0; 152 unsigned clusterLength = 0; 153 while (textIterator.consume(character, clusterLength)) { 154 node = currentLayer->get(character); 155 if (!node) { 156 node = GlyphMapNode::create(); 157 currentLayer->set(character, node); 158 } 159 currentLayer = &node->children; 160 textIterator.advance(clusterLength); 161 } 162 163 return node.release(); 164 } 165 166 template<typename Iterator> collectGlyphsForIterator(Iterator & textIterator,Vector<SVGGlyph> & glyphs)167 void collectGlyphsForIterator(Iterator& textIterator, Vector<SVGGlyph>& glyphs) 168 { 169 GlyphMapLayer* currentLayer = &m_rootLayer; 170 171 UChar32 character = 0; 172 unsigned clusterLength = 0; 173 while (textIterator.consume(character, clusterLength)) { 174 RefPtr<GlyphMapNode> node = currentLayer->get(character); 175 if (!node) 176 break; 177 glyphs.append(node->glyphs); 178 currentLayer = &node->children; 179 textIterator.advance(clusterLength); 180 } 181 } 182 183 GlyphMapLayer m_rootLayer; 184 Vector<SVGGlyph> m_glyphTable; 185 HashMap<String, Glyph> m_namedGlyphs; 186 int m_currentPriority; 187 }; 188 189 } 190 191 #endif // ENABLE(SVG_FONTS) 192 #endif // SVGGlyphMap_h 193