KHtml

SVGFontElement.cpp
1 /*
2  Copyright (C) 2007 Eric Seidel <[email protected]>
3  Copyright (C) 2007 Nikolas Zimmermann <[email protected]>
4 
5  This library is free software; you can redistribute it and/or
6  modify it under the terms of the GNU Library General Public
7  License as published by the Free Software Foundation; either
8  version 2 of the License, or (at your option) any later version.
9 
10  This library is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  Library General Public License for more details.
14 
15  You should have received a copy of the GNU Library General Public License
16  along with this library; see the file COPYING.LIB. If not, write to
17  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18  Boston, MA 02110-1301, USA.
19 */
20 
21 #include "wtf/Platform.h"
22 
23 #if ENABLE(SVG_FONTS)
24 #include "SVGFontElement.h"
25 
26 //#include "Font.h"
27 //FIXME khtml #include "GlyphPageTreeNode.h"
28 #include "SVGMissingGlyphElement.h"
29 #include "SVGNames.h"
30 #include "SVGParserUtilities.h"
31 #include <wtf/ASCIICType.h>
32 
33 using namespace WTF;
34 
35 namespace WebCore
36 {
37 
38 using namespace SVGNames;
39 
40 SVGFontElement::SVGFontElement(const QualifiedName &tagName, Document *doc)
41  : SVGStyledElement(tagName, doc)
42  , m_isGlyphCacheValid(false)
43 {
44 }
45 
46 SVGFontElement::~SVGFontElement()
47 {
48 }
49 
50 void SVGFontElement::invalidateGlyphCache()
51 {
52  if (m_isGlyphCacheValid) {
53  m_glyphMap.clear();
54  m_kerningPairs.clear();
55  }
56  m_isGlyphCacheValid = false;
57 }
58 
59 SVGMissingGlyphElement *SVGFontElement::firstMissingGlyphElement() const
60 {
61  for (Node *child = firstChild(); child; child = child->nextSibling()) {
62  if (child->hasTagName(missing_glyphTag)) {
63  return static_cast<SVGMissingGlyphElement *>(child);
64  }
65  }
66 
67  return nullptr;
68 }
69 
70 void SVGFontElement::ensureGlyphCache() const
71 {
72  if (m_isGlyphCacheValid) {
73  return;
74  }
75 
76  for (Node *child = firstChild(); child; child = child->nextSibling()) {
77  if (child->hasTagName(glyphTag)) {
78  SVGGlyphElement *glyph = static_cast<SVGGlyphElement *>(child);
79  String unicode = glyph->getAttribute(unicodeAttr);
80  if (unicode.length()) {
81  m_glyphMap.add(unicode, glyph->buildGlyphIdentifier());
82  }
83  } else if (child->hasTagName(hkernTag)) {
84  SVGHKernElement *hkern = static_cast<SVGHKernElement *>(child);
85  SVGHorizontalKerningPair kerningPair = hkern->buildHorizontalKerningPair();
86  m_kerningPairs.append(kerningPair);
87  }
88  }
89 
90  m_isGlyphCacheValid = true;
91 }
92 
93 // Returns the number of characters consumed or 0 if no range was found.
94 static unsigned parseUnicodeRange(const UChar *characters, unsigned length, pair<unsigned, unsigned> &range)
95 {
96  Q_UNUSED(characters);
97  Q_UNUSED(length);
98  Q_UNUSED(range);
99  // FIXME khtml
100  return 0;
101  /*if (length < 2)
102  return 0;
103  if (characters[0] != 'U')
104  return 0;
105  if (characters[1] != '+')
106  return 0;
107 
108  // Parse the starting hex number (or its prefix).
109  unsigned start = 0;
110  unsigned startLength = 0;
111  for (unsigned i = 2; i < length; ++i) {
112  if (!isASCIIHexDigit(characters[i]))
113  break;
114  if (++startLength > 6)
115  return 0;
116  start = (start << 4) | toASCIIHexValue(characters[i]);
117  }
118 
119  // Handle the case of ranges separated by "-" sign.
120  if (2 + startLength < length && characters[2 + startLength] == '-') {
121  if (!startLength)
122  return 0;
123 
124  // Parse the ending hex number (or its prefix).
125  unsigned end = 0;
126  unsigned endLength = 0;
127  for (unsigned i = 2 + startLength + 1; i < length; ++i) {
128  if (!isASCIIHexDigit(characters[i]))
129  break;
130  if (++endLength > 6)
131  return 0;
132  end = (end << 4) | toASCIIHexValue(characters[i]);
133  }
134 
135  if (!endLength)
136  return 0;
137 
138  range.first = start;
139  range.second = end;
140  return 2 + startLength + 1 + endLength;
141  }
142 
143  // Handle the case of a number with some optional trailing question marks.
144  unsigned end = start;
145  for (unsigned i = 2 + startLength; i < length; ++i) {
146  if (characters[i] != '?')
147  break;
148  if (++startLength > 6)
149  return 0;
150  start <<= 4;
151  end = (end << 4) | 0xF;
152  }
153 
154  if (!startLength)
155  return 0;
156 
157  range.first = start;
158  range.second = end;
159  return 2 + startLength;*/
160 }
161 
162 static bool parseUnicodeRangeList(const UChar *characters, unsigned length, Vector<pair<unsigned, unsigned> > &ranges)
163 {
164  ranges.clear();
165  if (!length) {
166  return true;
167  }
168 
169  const UChar *remainingCharacters = characters;
170  unsigned remainingLength = length;
171 
172  while (1) {
173  pair<unsigned, unsigned> range;
174  unsigned charactersConsumed = parseUnicodeRange(remainingCharacters, remainingLength, range);
175  if (charactersConsumed) {
176  ranges.append(range);
177  remainingCharacters += charactersConsumed;
178  remainingLength -= charactersConsumed;
179  } else {
180  if (!remainingLength) {
181  return false;
182  }
183  UChar character = remainingCharacters[0];
184  if (character == ',') {
185  return false;
186  }
187  ranges.append(make_pair(character.unicode(), character.unicode()));
188  ++remainingCharacters;
189  --remainingLength;
190  }
191  if (!remainingLength) {
192  return true;
193  }
194  if (remainingCharacters[0] != ',') {
195  return false;
196  }
197  ++remainingCharacters;
198  --remainingLength;
199  }
200 }
201 
202 static bool stringMatchesUnicodeRange(const String &unicodeString, const String &unicodeRangeSpec)
203 {
204  Vector<pair<unsigned, unsigned> > ranges;
205  if (!parseUnicodeRangeList(unicodeRangeSpec.characters(), unicodeRangeSpec.length(), ranges)) {
206  return false;
207  }
208 
209  if (unicodeString.length() != ranges.size()) {
210  return false;
211  }
212 
213  for (size_t i = 0; i < unicodeString.length(); ++i) {
214  UChar c = unicodeString[i];
215  if (c < ranges[i].first || c > ranges[i].second) {
216  return false;
217  }
218  }
219 
220  return true;
221 }
222 
223 static bool matches(const String &u1, const String &g1, const String &u2, const String &g2, const SVGHorizontalKerningPair &kerningPair)
224 {
225  if (kerningPair.unicode1.length() && !stringMatchesUnicodeRange(u1, kerningPair.unicode1)) {
226  return false;
227  }
228  if (kerningPair.glyphName1.length() && kerningPair.glyphName1 != g1) {
229  return false;
230  }
231 
232  if (kerningPair.unicode2.length() && !stringMatchesUnicodeRange(u2, kerningPair.unicode2)) {
233  return false;
234  }
235  if (kerningPair.glyphName2.length() && kerningPair.glyphName2 != g2) {
236  return false;
237  }
238 
239  return true;
240 }
241 
242 bool SVGFontElement::getHorizontalKerningPairForStringsAndGlyphs(const String &u1, const String &g1, const String &u2, const String &g2, SVGHorizontalKerningPair &kerningPair) const
243 {
244  for (size_t i = 0; i < m_kerningPairs.size(); ++i) {
245  if (matches(u1, g1, u2, g2, m_kerningPairs[i])) {
246  kerningPair = m_kerningPairs[i];
247  return true;
248  }
249  }
250 
251  return false;
252 }
253 
254 void SVGFontElement::getGlyphIdentifiersForString(const String &string, Vector<SVGGlyphIdentifier> &glyphs) const
255 {
256  ensureGlyphCache();
257  m_glyphMap.get(string, glyphs);
258 }
259 
260 }
261 
262 #endif // ENABLE(SVG_FONTS)
MESSAGECORE_EXPORT KMime::Content * firstChild(const KMime::Content *node)
Definition: css_base.h:371
This file is part of the KDE documentation.
Documentation copyright © 1996-2021 The KDE developers.
Generated on Sat Oct 16 2021 22:48:02 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.