KHtml

SVGFontFaceElement.cpp
1 /*
2  Copyright (C) 2007 Eric Seidel <[email protected]>
3  Copyright (C) 2007, 2008 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 #if ENABLE(SVG_FONTS)
22 #include "SVGFontFaceElement.h"
23 #include "CString.h"
24 #include "CSSFontFaceRule.h"
25 #include "CSSFontFaceSrcValue.h"
26 #include "CSSParser.h"
27 #include "CSSProperty.h"
28 #include "CSSPropertyNames.h"
29 #include "CSSStyleSelector.h"
30 #include "CSSStyleSheet.h"
31 #include "CSSValueKeywords.h"
32 #include "CSSValueList.h"
33 #include "FontCache.h"
34 #include "SimpleFontData.h"
35 #include "SVGDefinitionSrcElement.h"
36 #include "SVGFontElement.h"
37 #include "SVGFontFaceSrcElement.h"
38 #include "SVGGlyphElement.h"
39 #include "SVGNames.h"
40 
41 #include <math.h>
42 
43 namespace WebCore
44 {
45 
46 using namespace SVGNames;
47 
48 SVGFontFaceElement::SVGFontFaceElement(const QualifiedName &tagName, Document *doc)
49  : SVGElement(tagName, doc)
50  , m_fontFaceRule(new CSSFontFaceRule(0))
51  , m_styleDeclaration(new CSSMutableStyleDeclaration)
52 {
53  m_styleDeclaration->setParent(document()->mappedElementSheet());
54  m_styleDeclaration->setStrictParsing(true);
55  m_fontFaceRule->setDeclaration(m_styleDeclaration.get());
56  document()->mappedElementSheet()->append(m_fontFaceRule);
57 }
58 
59 SVGFontFaceElement::~SVGFontFaceElement()
60 {
61 }
62 
63 static void mapAttributeToCSSProperty(HashMap<AtomicStringImpl *, int> *propertyNameToIdMap, const QualifiedName &attrName)
64 {
65  int propertyId = cssPropertyID(attrName.localName());
66  ASSERT(propertyId > 0);
67  propertyNameToIdMap->set(attrName.localName().impl(), propertyId);
68 }
69 
70 static int cssPropertyIdForSVGAttributeName(const QualifiedName &attrName)
71 {
72  if (!attrName.namespaceURI().isNull()) {
73  return 0;
74  }
75 
76  static HashMap<AtomicStringImpl *, int> *propertyNameToIdMap = 0;
77  if (!propertyNameToIdMap) {
78  propertyNameToIdMap = new HashMap<AtomicStringImpl *, int>;
79  // This is a list of all @font-face CSS properties which are exposed as SVG XML attributes
80  // Those commented out are not yet supported by WebCore's style system
81  //mapAttributeToCSSProperty(propertyNameToIdMap, accent_heightAttr);
82  //mapAttributeToCSSProperty(propertyNameToIdMap, alphabeticAttr);
83  //mapAttributeToCSSProperty(propertyNameToIdMap, ascentAttr);
84  //mapAttributeToCSSProperty(propertyNameToIdMap, bboxAttr);
85  //mapAttributeToCSSProperty(propertyNameToIdMap, cap_heightAttr);
86  //mapAttributeToCSSProperty(propertyNameToIdMap, descentAttr);
87  mapAttributeToCSSProperty(propertyNameToIdMap, font_familyAttr);
88  mapAttributeToCSSProperty(propertyNameToIdMap, font_sizeAttr);
89  mapAttributeToCSSProperty(propertyNameToIdMap, font_stretchAttr);
90  mapAttributeToCSSProperty(propertyNameToIdMap, font_styleAttr);
91  mapAttributeToCSSProperty(propertyNameToIdMap, font_variantAttr);
92  mapAttributeToCSSProperty(propertyNameToIdMap, font_weightAttr);
93  //mapAttributeToCSSProperty(propertyNameToIdMap, hangingAttr);
94  //mapAttributeToCSSProperty(propertyNameToIdMap, ideographicAttr);
95  //mapAttributeToCSSProperty(propertyNameToIdMap, mathematicalAttr);
96  //mapAttributeToCSSProperty(propertyNameToIdMap, overline_positionAttr);
97  //mapAttributeToCSSProperty(propertyNameToIdMap, overline_thicknessAttr);
98  //mapAttributeToCSSProperty(propertyNameToIdMap, panose_1Attr);
99  //mapAttributeToCSSProperty(propertyNameToIdMap, slopeAttr);
100  //mapAttributeToCSSProperty(propertyNameToIdMap, stemhAttr);
101  //mapAttributeToCSSProperty(propertyNameToIdMap, stemvAttr);
102  //mapAttributeToCSSProperty(propertyNameToIdMap, strikethrough_positionAttr);
103  //mapAttributeToCSSProperty(propertyNameToIdMap, strikethrough_thicknessAttr);
104  //mapAttributeToCSSProperty(propertyNameToIdMap, underline_positionAttr);
105  //mapAttributeToCSSProperty(propertyNameToIdMap, underline_thicknessAttr);
106  //mapAttributeToCSSProperty(propertyNameToIdMap, unicode_rangeAttr);
107  //mapAttributeToCSSProperty(propertyNameToIdMap, units_per_emAttr);
108  //mapAttributeToCSSProperty(propertyNameToIdMap, v_alphabeticAttr);
109  //mapAttributeToCSSProperty(propertyNameToIdMap, v_hangingAttr);
110  //mapAttributeToCSSProperty(propertyNameToIdMap, v_ideographicAttr);
111  //mapAttributeToCSSProperty(propertyNameToIdMap, v_mathematicalAttr);
112  //mapAttributeToCSSProperty(propertyNameToIdMap, widthsAttr);
113  //mapAttributeToCSSProperty(propertyNameToIdMap, x_heightAttr);
114  }
115 
116  return propertyNameToIdMap->get(attrName.localName().impl());
117 }
118 
119 void SVGFontFaceElement::parseMappedAttribute(MappedAttribute *attr)
120 {
121  int propId = cssPropertyIdForSVGAttributeName(attr->name());
122  if (propId > 0) {
123  m_styleDeclaration->setProperty(propId, attr->value(), false);
124  rebuildFontFace();
125  return;
126  }
127 
128  SVGElement::parseMappedAttribute(attr);
129 }
130 
131 unsigned SVGFontFaceElement::unitsPerEm() const
132 {
133  AtomicString value(getAttribute(units_per_emAttr));
134  if (value.isEmpty()) {
135  return 1000;
136  }
137 
138  return static_cast<unsigned>(ceilf(value.toFloat()));
139 }
140 
141 int SVGFontFaceElement::xHeight() const
142 {
143  AtomicString value(getAttribute(x_heightAttr));
144  if (value.isEmpty()) {
145  return 0;
146  }
147 
148  return static_cast<int>(ceilf(value.toFloat()));
149 }
150 
151 float SVGFontFaceElement::horizontalOriginX() const
152 {
153  if (!m_fontElement) {
154  return 0.0f;
155  }
156 
157  // Spec: The X-coordinate in the font coordinate system of the origin of a glyph to be used when
158  // drawing horizontally oriented text. (Note that the origin applies to all glyphs in the font.)
159  // If the attribute is not specified, the effect is as if a value of "0" were specified.
160  AtomicString value(m_fontElement->getAttribute(horiz_origin_xAttr));
161  if (value.isEmpty()) {
162  return 0.0f;
163  }
164 
165  return value.toFloat();
166 }
167 
168 float SVGFontFaceElement::horizontalOriginY() const
169 {
170  if (!m_fontElement) {
171  return 0.0f;
172  }
173 
174  // Spec: The Y-coordinate in the font coordinate system of the origin of a glyph to be used when
175  // drawing horizontally oriented text. (Note that the origin applies to all glyphs in the font.)
176  // If the attribute is not specified, the effect is as if a value of "0" were specified.
177  AtomicString value(m_fontElement->getAttribute(horiz_origin_yAttr));
178  if (value.isEmpty()) {
179  return 0.0f;
180  }
181 
182  return value.toFloat();
183 }
184 
185 float SVGFontFaceElement::horizontalAdvanceX() const
186 {
187  if (!m_fontElement) {
188  return 0.0f;
189  }
190 
191  // Spec: The default horizontal advance after rendering a glyph in horizontal orientation. Glyph
192  // widths are required to be non-negative, even if the glyph is typically rendered right-to-left,
193  // as in Hebrew and Arabic scripts.
194  AtomicString value(m_fontElement->getAttribute(horiz_adv_xAttr));
195  if (value.isEmpty()) {
196  return 0.0f;
197  }
198 
199  return value.toFloat();
200 }
201 
202 float SVGFontFaceElement::verticalOriginX() const
203 {
204  if (!m_fontElement) {
205  return 0.0f;
206  }
207 
208  // Spec: The default X-coordinate in the font coordinate system of the origin of a glyph to be used when
209  // drawing vertically oriented text. If the attribute is not specified, the effect is as if the attribute
210  // were set to half of the effective value of attribute horiz-adv-x.
211  AtomicString value(m_fontElement->getAttribute(vert_origin_xAttr));
212  if (value.isEmpty()) {
213  return horizontalAdvanceX() / 2.0f;
214  }
215 
216  return value.toFloat();
217 }
218 
219 float SVGFontFaceElement::verticalOriginY() const
220 {
221  if (!m_fontElement) {
222  return 0.0f;
223  }
224 
225  // Spec: The default Y-coordinate in the font coordinate system of the origin of a glyph to be used when
226  // drawing vertically oriented text. If the attribute is not specified, the effect is as if the attribute
227  // were set to the position specified by the font's ascent attribute.
228  AtomicString value(m_fontElement->getAttribute(vert_origin_yAttr));
229  if (value.isEmpty()) {
230  return ascent();
231  }
232 
233  return value.toFloat();
234 }
235 
236 float SVGFontFaceElement::verticalAdvanceY() const
237 {
238  if (!m_fontElement) {
239  return 0.0f;
240  }
241 
242  // Spec: The default vertical advance after rendering a glyph in vertical orientation. If the attribute is
243  // not specified, the effect is as if a value equivalent of one em were specified (see units-per-em).
244  AtomicString value(m_fontElement->getAttribute(vert_adv_yAttr));
245  if (value.isEmpty()) {
246  return 1.0f;
247  }
248 
249  return value.toFloat();
250 }
251 
252 int SVGFontFaceElement::ascent() const
253 {
254  if (!m_fontElement) {
255  return 0.0f;
256  }
257 
258  // Spec: Same syntax and semantics as the 'ascent' descriptor within an @font-face rule. The maximum
259  // unaccented height of the font within the font coordinate system. If the attribute is not specified,
260  // the effect is as if the attribute were set to the difference between the units-per-em value and the
261  // vert-origin-y value for the corresponding font.
262  AtomicString value(m_fontElement->getAttribute(ascentAttr));
263  if (!value.isEmpty()) {
264  return static_cast<int>(ceilf(value.toFloat()));
265  }
266 
267  value = m_fontElement->getAttribute(vert_origin_yAttr);
268  if (!value.isEmpty()) {
269  return static_cast<int>(unitsPerEm()) - static_cast<int>(ceilf(value.toFloat()));
270  }
271 
272  // Match Batiks default value
273  return static_cast<int>(ceilf(unitsPerEm() * 0.8f));
274 }
275 
276 int SVGFontFaceElement::descent() const
277 {
278  if (!m_fontElement) {
279  return 0.0f;
280  }
281 
282  // Spec: Same syntax and semantics as the 'descent' descriptor within an @font-face rule. The maximum
283  // unaccented depth of the font within the font coordinate system. If the attribute is not specified,
284  // the effect is as if the attribute were set to the vert-origin-y value for the corresponding font.
285  AtomicString value(m_fontElement->getAttribute(descentAttr));
286  if (!value.isEmpty()) {
287  // Some testcases use a negative descent value, where a positive was meant to be used :(
288  int descent = static_cast<int>(ceilf(value.toFloat()));
289  return descent < 0 ? -descent : descent;
290  }
291 
292  value = m_fontElement->getAttribute(vert_origin_yAttr);
293  if (!value.isEmpty()) {
294  return static_cast<int>(ceilf(value.toFloat()));
295  }
296 
297  // Match Batiks default value
298  return static_cast<int>(ceilf(unitsPerEm() * 0.2f));
299 }
300 
301 String SVGFontFaceElement::fontFamily() const
302 {
303  return m_styleDeclaration->getPropertyValue(CSSPropertyFontFamily);
304 }
305 
306 SVGFontElement *SVGFontFaceElement::associatedFontElement() const
307 {
308  return m_fontElement.get();
309 }
310 
311 void SVGFontFaceElement::rebuildFontFace()
312 {
313  // Ignore changes until we live in the tree
314  if (!parentNode()) {
315  return;
316  }
317 
318  // we currently ignore all but the first src element, alternatively we could concat them
319  SVGFontFaceSrcElement *srcElement = 0;
320  SVGDefinitionSrcElement *definitionSrc = 0;
321 
322  for (Node *child = firstChild(); child; child = child->nextSibling()) {
323  if (child->hasTagName(font_face_srcTag) && !srcElement) {
324  srcElement = static_cast<SVGFontFaceSrcElement *>(child);
325  } else if (child->hasTagName(definition_srcTag) && !definitionSrc) {
326  definitionSrc = static_cast<SVGDefinitionSrcElement *>(child);
327  }
328  }
329 
330 #if 0
331  // @font-face (CSSFontFace) does not yet support definition-src, as soon as it does this code should do the trick!
332  if (definitionSrc) {
333  m_styleDeclaration->setProperty(CSSPropertyDefinitionSrc, definitionSrc->getAttribute(XLinkNames::hrefAttr), false);
334  }
335 #endif
336 
337  bool describesParentFont = parentNode()->hasTagName(fontTag);
338  RefPtr<CSSValueList> list;
339 
340  if (describesParentFont) {
341  m_fontElement = static_cast<SVGFontElement *>(parentNode());
342 
343  list = new CSSValueList; // ### CSSValueListImpl(CSSValueListImpl::Comma);
344  list->append(new CSSFontFaceSrcValue(fontFamily(), true));
345  } else if (srcElement) {
346  list = srcElement->srcValue();
347  }
348 
349  if (!list) {
350  return;
351  }
352 
353  // Parse in-memory CSS rules
354  CSSProperty srcProperty(CSSPropertySrc, list);
355  const CSSProperty *srcPropertyRef = &srcProperty;
356  m_styleDeclaration->addParsedProperties(&srcPropertyRef, 1);
357 
358  if (describesParentFont) {
359  // Traverse parsed CSS values and associate CSSFontFaceSrcValue elements with ourselves.
360  RefPtr<CSSValue> src = m_styleDeclaration->getPropertyCSSValue(CSSPropertySrc);
361  CSSValueList *srcList = static_cast<CSSValueList *>(src.get());
362 
363  unsigned srcLength = srcList ? srcList->length() : 0;
364  for (unsigned i = 0; i < srcLength; i++) {
365  if (CSSFontFaceSrcValue *item = static_cast<CSSFontFaceSrcValue *>(srcList->itemWithoutBoundsCheck(i))) {
366  item->setSVGFontFaceElement(this);
367  }
368  }
369  }
370 
371  document()->updateStyleSelector();
372 }
373 
374 void SVGFontFaceElement::insertedIntoDocument()
375 {
376  rebuildFontFace();
377  SVGElement::insertedIntoDocument();
378 }
379 
380 void SVGFontFaceElement::childrenChanged(bool changedByParser, Node *beforeChange, Node *afterChange, int childCountDelta)
381 {
382  SVGElement::childrenChanged(changedByParser, beforeChange, afterChange, childCountDelta);
383  rebuildFontFace();
384 }
385 
386 }
387 
388 #endif // ENABLE(SVG_FONTS)
void append(const T &value)
MESSAGECORE_EXPORT KMime::Content * firstChild(const KMime::Content *node)
KIOFILEWIDGETS_EXPORT QStringList list(const QString &fileClass)
This file is part of the KDE documentation.
Documentation copyright © 1996-2021 The KDE developers.
Generated on Tue Oct 26 2021 22:48:10 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.