KHtml

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 namespace WTF
27 {
28 struct QCharHash {
29  static unsigned hash(const QChar &c)
30  {
31  return c.unicode();
32  }
33  static bool equal(const QChar &a, const QChar &b)
34  {
35  return a == b;
36  }
37  static const bool safeToCompareToEmptyOrDeleted = false;
38 };
39 template<> struct HashTraits<QChar> : public GenericHashTraits<QChar> {
40  static const bool emptyValueIsZero = true;
41  static const bool needsDestruction = false;
42  static const bool needsRef = false;
43  static QChar deletedValue()
44  {
45  return QChar(-1);
46  }
47  static bool isDeletedValue(const QChar &)
48  {
49  return false;
50  }
51  static QChar constructDeletedValue(QChar *)
52  {
53  return QChar(-1);
54  }
55 };
56 template<> struct DefaultHash<QChar> {
57  typedef QCharHash Hash;
58 };
59 }
60 
61 namespace WebCore
62 {
63 
64 struct GlyphMapNode;
65 
66 typedef HashMap<UChar, RefPtr<GlyphMapNode> > GlyphMapLayer;
67 
68 struct GlyphMapNode : public RefCounted<GlyphMapNode> {
69 private:
70  GlyphMapNode() { }
71 public:
72  static PassRefPtr<GlyphMapNode> create()
73  {
74  return adoptRef(new GlyphMapNode);
75  }
76 
77  Vector<SVGGlyphIdentifier> glyphs;
78 
79  GlyphMapLayer children;
80 };
81 
82 class SVGGlyphMap
83 {
84 
85 public:
86  SVGGlyphMap() : m_currentPriority(0) { }
87 
88  void add(const String &string, const SVGGlyphIdentifier &glyph)
89  {
90  size_t len = string.length();
91  GlyphMapLayer *currentLayer = &m_rootLayer;
92 
93  RefPtr<GlyphMapNode> node;
94  for (size_t i = 0; i < len; i++) {
95  UChar curChar = string[i];
96  node = currentLayer->get(curChar);
97  if (!node) {
98  node = GlyphMapNode::create();
99  currentLayer->set(curChar, node);
100  }
101  currentLayer = &node->children;
102  }
103 
104  if (node) {
105  node->glyphs.append(glyph);
106  node->glyphs.last().priority = m_currentPriority++;
107  node->glyphs.last().nameLength = len;
108  node->glyphs.last().isValid = true;
109  }
110  }
111 
112  static inline bool compareGlyphPriority(const SVGGlyphIdentifier &first, const SVGGlyphIdentifier &second)
113  {
114  return first.priority < second.priority;
115  }
116 
117  void get(const String &string, Vector<SVGGlyphIdentifier> &glyphs)
118  {
119  GlyphMapLayer *currentLayer = &m_rootLayer;
120 
121  for (size_t i = 0; i < string.length(); i++) {
122  UChar curChar = string[i];
123  RefPtr<GlyphMapNode> node = currentLayer->get(curChar);
124  if (!node) {
125  break;
126  }
127  glyphs.append(node->glyphs);
128  currentLayer = &node->children;
129  }
130  std::sort(glyphs.begin(), glyphs.end(), compareGlyphPriority);
131  }
132 
133  void clear()
134  {
135  m_rootLayer.clear();
136  m_currentPriority = 0;
137  }
138 
139 private:
140  GlyphMapLayer m_rootLayer;
141  int m_currentPriority;
142 };
143 
144 }
145 
146 #endif // ENABLE(SVG_FONTS)
147 
148 #endif //SVGGlyphMap_h
QAction * create(StandardAction id, const QObject *recvr, Func slot, QObject *parent)
KIOFILEWIDGETS_EXPORT void add(const QString &fileClass, const QString &directory)
ushort unicode() const const
Definition: css_base.h:371
This file is part of the KDE documentation.
Documentation copyright © 1996-2021 The KDE developers.
Generated on Mon Oct 25 2021 22:48:22 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.