don't discard iframe children.
[kdelibs.git] / khtml / svg / SVGGlyphMap.h
blob31e2ebeb98ee064a70ed892f11441a6893c954a9
1 /*
2 Copyright (C) 2008 Apple, Inc
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.
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.
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.
20 #ifndef SVGGlyphMap_h
21 #define SVGGlyphMap_h
23 #if ENABLE(SVG_FONTS)
24 #include "SVGGlyphElement.h"
26 namespace WTF
28 struct QCharHash
30 static unsigned hash(const QChar& c) { return c.unicode(); }
31 static bool equal(const QChar& a, const QChar& b) { return a == b; }
32 static const bool safeToCompareToEmptyOrDeleted = false;
34 template<> struct HashTraits<QChar> : public GenericHashTraits<QChar> {
35 static const bool emptyValueIsZero = true;
36 static const bool needsDestruction = false;
37 static const bool needsRef = false;
38 static QChar deletedValue() { return QChar(-1); }
39 static bool isDeletedValue(const QChar& c) { return false; }
40 static QChar constructDeletedValue(QChar*) { return QChar(-1); }
42 template<> struct DefaultHash<QChar>
44 typedef QCharHash Hash;
48 namespace WebCore {
50 struct GlyphMapNode;
52 typedef HashMap<UChar, RefPtr<GlyphMapNode> > GlyphMapLayer;
55 struct GlyphMapNode : public RefCounted<GlyphMapNode> {
56 private:
57 GlyphMapNode() { }
58 public:
59 static PassRefPtr<GlyphMapNode> create() { return adoptRef(new GlyphMapNode); }
61 Vector<SVGGlyphIdentifier> glyphs;
63 GlyphMapLayer children;
66 class SVGGlyphMap {
68 public:
69 SVGGlyphMap() : m_currentPriority(0) { }
71 void add(const String& string, const SVGGlyphIdentifier& glyph)
73 size_t len = string.length();
74 GlyphMapLayer* currentLayer = &m_rootLayer;
76 RefPtr<GlyphMapNode> node;
77 for (size_t i = 0; i < len; i++) {
78 UChar curChar = string[i];
79 node = currentLayer->get(curChar);
80 if (!node) {
81 node = GlyphMapNode::create();
82 currentLayer->set(curChar, node);
84 currentLayer = &node->children;
87 if (node) {
88 node->glyphs.append(glyph);
89 node->glyphs.last().priority = m_currentPriority++;
90 node->glyphs.last().nameLength = len;
91 node->glyphs.last().isValid = true;
95 static inline bool compareGlyphPriority(const SVGGlyphIdentifier& first, const SVGGlyphIdentifier& second)
97 return first.priority < second.priority;
100 void get(const String& string, Vector<SVGGlyphIdentifier>& glyphs)
102 GlyphMapLayer* currentLayer = &m_rootLayer;
104 for (size_t i = 0; i < string.length(); i++) {
105 UChar curChar = string[i];
106 RefPtr<GlyphMapNode> node = currentLayer->get(curChar);
107 if (!node)
108 break;
109 glyphs.append(node->glyphs);
110 currentLayer = &node->children;
112 std::sort(glyphs.begin(), glyphs.end(), compareGlyphPriority);
115 void clear()
117 m_rootLayer.clear();
118 m_currentPriority = 0;
121 private:
122 GlyphMapLayer m_rootLayer;
123 int m_currentPriority;
128 #endif // ENABLE(SVG_FONTS)
131 #endif //SVGGlyphMap_h