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.
24 #include "SVGGlyphElement.h"
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
;
52 typedef HashMap
<UChar
, RefPtr
<GlyphMapNode
> > GlyphMapLayer
;
55 struct GlyphMapNode
: public RefCounted
<GlyphMapNode
> {
59 static PassRefPtr
<GlyphMapNode
> create() { return adoptRef(new GlyphMapNode
); }
61 Vector
<SVGGlyphIdentifier
> glyphs
;
63 GlyphMapLayer children
;
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
);
81 node
= GlyphMapNode::create();
82 currentLayer
->set(curChar
, node
);
84 currentLayer
= &node
->children
;
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
);
109 glyphs
.append(node
->glyphs
);
110 currentLayer
= &node
->children
;
112 std::sort(glyphs
.begin(), glyphs
.end(), compareGlyphPriority
);
118 m_currentPriority
= 0;
122 GlyphMapLayer m_rootLayer
;
123 int m_currentPriority
;
128 #endif // ENABLE(SVG_FONTS)
131 #endif //SVGGlyphMap_h