2f49a678e9cb8d0e88f59d78309165dea3af1cd7
[WebKit.git] / WebCore / svg / SVGGlyphMap.h
1 /*
2    Copyright (C) 2008 Apple, Inc
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 "SVGGlyphElement.h"
25
26
27 namespace WebCore {
28
29     struct GlyphMapNode;
30
31     typedef HashMap<UChar, RefPtr<GlyphMapNode> > GlyphMapLayer;
32
33     struct GlyphMapNode : public RefCounted<GlyphMapNode> {
34     private:
35         GlyphMapNode() { }
36     public:
37         PassRefPtr<GlyphMapNode> create() { return adoptRef(new GlyphMapNode); }
38
39         Vector<SVGGlyphIdentifier> glyphs;
40
41         GlyphMapLayer children;
42     };
43
44     class SVGGlyphMap {
45
46     public:
47         SVGGlyphMap() : m_currentPriority(0) { }
48
49         void add(const String& string, const SVGGlyphIdentifier& glyph) 
50         {
51             size_t len = string.length();
52             GlyphMapLayer* currentLayer = &m_rootLayer;
53
54             RefPtr<GlyphMapNode> node;
55             for (size_t i = 0; i < len; i++) {
56                 UChar curChar = string[i];
57                 node = currentLayer->get(curChar);
58                 if (!node) {
59                     node = GlyphMapNode::create();
60                     currentLayer->set(curChar, node);
61                 }
62                 currentLayer = &node->children;
63             }
64
65             if (node) {
66                 node->glyphs.append(glyph);
67                 node->glyphs.last().priority = m_currentPriority++;
68                 node->glyphs.last().nameLength = len;
69                 node->glyphs.last().isValid = true;
70             }
71         }
72
73         static inline bool compareGlyphPriority(const SVGGlyphIdentifier& first, const SVGGlyphIdentifier& second)
74         {
75             return first.priority < second.priority;
76         }
77
78         void get(const String& string, Vector<SVGGlyphIdentifier>& glyphs)
79         {
80             GlyphMapLayer* currentLayer = &m_rootLayer;
81
82             for (size_t i = 0; i < string.length(); i++) {
83                 UChar curChar = string[i];
84                 RefPtr<GlyphMapNode> node = currentLayer->get(curChar);
85                 if (!node)
86                     break;
87                 glyphs.append(node->glyphs);
88                 currentLayer = &node->children;
89             }
90             std::sort(glyphs.begin(), glyphs.end(), compareGlyphPriority);
91         }
92
93         void clear() 
94         { 
95             m_rootLayer.clear(); 
96             m_currentPriority = 0;
97         }
98
99     private:
100         GlyphMapLayer m_rootLayer;
101         int m_currentPriority;
102     };
103
104 }
105
106 #endif // ENABLE(SVG_FONTS)
107
108
109 #endif //SVGGlyphMap_h