don't discard iframe children.
[kdelibs.git] / khtml / svg / SVGFontFaceElement.cpp
blob2ac6d368e1d65a4b2ec745a795a60ed4486e0285
1 /*
2 Copyright (C) 2007 Eric Seidel <eric@webkit.org>
3 Copyright (C) 2007, 2008 Nikolas Zimmermann <zimmermann@kde.org>
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.
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.
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.
21 #include "config.h"
23 #if ENABLE(SVG_FONTS)
24 #include "SVGFontFaceElement.h"
25 #include "CString.h"
26 #include "CSSFontFaceRule.h"
27 #include "CSSFontFaceSrcValue.h"
28 #include "CSSParser.h"
29 #include "CSSProperty.h"
30 #include "CSSPropertyNames.h"
31 #include "CSSStyleSelector.h"
32 #include "CSSStyleSheet.h"
33 #include "CSSValueKeywords.h"
34 #include "CSSValueList.h"
35 #include "FontCache.h"
36 #include "SimpleFontData.h"
37 #include "SVGDefinitionSrcElement.h"
38 #include "SVGFontElement.h"
39 #include "SVGFontFaceSrcElement.h"
40 #include "SVGGlyphElement.h"
41 #include "SVGNames.h"
43 #include <math.h>
45 namespace WebCore {
47 using namespace SVGNames;
49 SVGFontFaceElement::SVGFontFaceElement(const QualifiedName& tagName, Document* doc)
50 : SVGElement(tagName, doc)
51 , m_fontFaceRule(new CSSFontFaceRule(0))
52 , m_styleDeclaration(new CSSMutableStyleDeclaration)
54 m_styleDeclaration->setParent(document()->mappedElementSheet());
55 m_styleDeclaration->setStrictParsing(true);
56 m_fontFaceRule->setDeclaration(m_styleDeclaration.get());
57 document()->mappedElementSheet()->append(m_fontFaceRule);
60 SVGFontFaceElement::~SVGFontFaceElement()
64 static void mapAttributeToCSSProperty(HashMap<AtomicStringImpl*, int>* propertyNameToIdMap, const QualifiedName& attrName)
66 int propertyId = cssPropertyID(attrName.localName());
67 ASSERT(propertyId > 0);
68 propertyNameToIdMap->set(attrName.localName().impl(), propertyId);
71 static int cssPropertyIdForSVGAttributeName(const QualifiedName& attrName)
73 if (!attrName.namespaceURI().isNull())
74 return 0;
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);
116 return propertyNameToIdMap->get(attrName.localName().impl());
119 void SVGFontFaceElement::parseMappedAttribute(MappedAttribute* attr)
121 int propId = cssPropertyIdForSVGAttributeName(attr->name());
122 if (propId > 0) {
123 m_styleDeclaration->setProperty(propId, attr->value(), false);
124 rebuildFontFace();
125 return;
128 SVGElement::parseMappedAttribute(attr);
131 unsigned SVGFontFaceElement::unitsPerEm() const
133 AtomicString value(getAttribute(units_per_emAttr));
134 if (value.isEmpty())
135 return 1000;
137 return static_cast<unsigned>(ceilf(value.toFloat()));
140 int SVGFontFaceElement::xHeight() const
142 AtomicString value(getAttribute(x_heightAttr));
143 if (value.isEmpty())
144 return 0;
146 return static_cast<int>(ceilf(value.toFloat()));
149 float SVGFontFaceElement::horizontalOriginX() const
151 if (!m_fontElement)
152 return 0.0f;
154 // Spec: The X-coordinate in the font coordinate system of the origin of a glyph to be used when
155 // drawing horizontally oriented text. (Note that the origin applies to all glyphs in the font.)
156 // If the attribute is not specified, the effect is as if a value of "0" were specified.
157 AtomicString value(m_fontElement->getAttribute(horiz_origin_xAttr));
158 if (value.isEmpty())
159 return 0.0f;
161 return value.toFloat();
164 float SVGFontFaceElement::horizontalOriginY() const
166 if (!m_fontElement)
167 return 0.0f;
169 // Spec: The Y-coordinate in the font coordinate system of the origin of a glyph to be used when
170 // drawing horizontally oriented text. (Note that the origin applies to all glyphs in the font.)
171 // If the attribute is not specified, the effect is as if a value of "0" were specified.
172 AtomicString value(m_fontElement->getAttribute(horiz_origin_yAttr));
173 if (value.isEmpty())
174 return 0.0f;
176 return value.toFloat();
179 float SVGFontFaceElement::horizontalAdvanceX() const
181 if (!m_fontElement)
182 return 0.0f;
184 // Spec: The default horizontal advance after rendering a glyph in horizontal orientation. Glyph
185 // widths are required to be non-negative, even if the glyph is typically rendered right-to-left,
186 // as in Hebrew and Arabic scripts.
187 AtomicString value(m_fontElement->getAttribute(horiz_adv_xAttr));
188 if (value.isEmpty())
189 return 0.0f;
191 return value.toFloat();
194 float SVGFontFaceElement::verticalOriginX() const
196 if (!m_fontElement)
197 return 0.0f;
199 // Spec: The default X-coordinate in the font coordinate system of the origin of a glyph to be used when
200 // drawing vertically oriented text. If the attribute is not specified, the effect is as if the attribute
201 // were set to half of the effective value of attribute horiz-adv-x.
202 AtomicString value(m_fontElement->getAttribute(vert_origin_xAttr));
203 if (value.isEmpty())
204 return horizontalAdvanceX() / 2.0f;
206 return value.toFloat();
209 float SVGFontFaceElement::verticalOriginY() const
211 if (!m_fontElement)
212 return 0.0f;
214 // Spec: The default Y-coordinate in the font coordinate system of the origin of a glyph to be used when
215 // drawing vertically oriented text. If the attribute is not specified, the effect is as if the attribute
216 // were set to the position specified by the font's ascent attribute.
217 AtomicString value(m_fontElement->getAttribute(vert_origin_yAttr));
218 if (value.isEmpty())
219 return ascent();
221 return value.toFloat();
224 float SVGFontFaceElement::verticalAdvanceY() const
226 if (!m_fontElement)
227 return 0.0f;
229 // Spec: The default vertical advance after rendering a glyph in vertical orientation. If the attribute is
230 // not specified, the effect is as if a value equivalent of one em were specified (see units-per-em).
231 AtomicString value(m_fontElement->getAttribute(vert_adv_yAttr));
232 if (value.isEmpty())
233 return 1.0f;
235 return value.toFloat();
238 int SVGFontFaceElement::ascent() const
240 if (!m_fontElement)
241 return 0.0f;
243 // Spec: Same syntax and semantics as the 'ascent' descriptor within an @font-face rule. The maximum
244 // unaccented height of the font within the font coordinate system. If the attribute is not specified,
245 // the effect is as if the attribute were set to the difference between the units-per-em value and the
246 // vert-origin-y value for the corresponding font.
247 AtomicString value(m_fontElement->getAttribute(ascentAttr));
248 if (!value.isEmpty())
249 return static_cast<int>(ceilf(value.toFloat()));
251 value = m_fontElement->getAttribute(vert_origin_yAttr);
252 if (!value.isEmpty())
253 return static_cast<int>(unitsPerEm()) - static_cast<int>(ceilf(value.toFloat()));
255 // Match Batiks default value
256 return static_cast<int>(ceilf(unitsPerEm() * 0.8f));
259 int SVGFontFaceElement::descent() const
261 if (!m_fontElement)
262 return 0.0f;
264 // Spec: Same syntax and semantics as the 'descent' descriptor within an @font-face rule. The maximum
265 // unaccented depth of the font within the font coordinate system. If the attribute is not specified,
266 // the effect is as if the attribute were set to the vert-origin-y value for the corresponding font.
267 AtomicString value(m_fontElement->getAttribute(descentAttr));
268 if (!value.isEmpty()) {
269 // Some testcases use a negative descent value, where a positive was meant to be used :(
270 int descent = static_cast<int>(ceilf(value.toFloat()));
271 return descent < 0 ? -descent : descent;
274 value = m_fontElement->getAttribute(vert_origin_yAttr);
275 if (!value.isEmpty())
276 return static_cast<int>(ceilf(value.toFloat()));
278 // Match Batiks default value
279 return static_cast<int>(ceilf(unitsPerEm() * 0.2f));
282 String SVGFontFaceElement::fontFamily() const
284 return m_styleDeclaration->getPropertyValue(CSSPropertyFontFamily);
287 SVGFontElement* SVGFontFaceElement::associatedFontElement() const
289 return m_fontElement.get();
292 void SVGFontFaceElement::rebuildFontFace()
294 // Ignore changes until we live in the tree
295 if (!parentNode())
296 return;
298 // we currently ignore all but the first src element, alternatively we could concat them
299 SVGFontFaceSrcElement* srcElement = 0;
300 SVGDefinitionSrcElement* definitionSrc = 0;
302 for (Node* child = firstChild(); child; child = child->nextSibling()) {
303 if (child->hasTagName(font_face_srcTag) && !srcElement)
304 srcElement = static_cast<SVGFontFaceSrcElement*>(child);
305 else if (child->hasTagName(definition_srcTag) && !definitionSrc)
306 definitionSrc = static_cast<SVGDefinitionSrcElement*>(child);
309 #if 0
310 // @font-face (CSSFontFace) does not yet support definition-src, as soon as it does this code should do the trick!
311 if (definitionSrc)
312 m_styleDeclaration->setProperty(CSSPropertyDefinitionSrc, definitionSrc->getAttribute(XLinkNames::hrefAttr), false);
313 #endif
315 bool describesParentFont = parentNode()->hasTagName(fontTag);
316 RefPtr<CSSValueList> list;
318 if (describesParentFont) {
319 m_fontElement = static_cast<SVGFontElement*>(parentNode());
321 list = new CSSValueList;
322 list->append(new CSSFontFaceSrcValue(fontFamily(), true));
323 } else if (srcElement)
324 list = srcElement->srcValue();
326 if (!list)
327 return;
329 // Parse in-memory CSS rules
330 CSSProperty srcProperty(CSSPropertySrc, list);
331 const CSSProperty* srcPropertyRef = &srcProperty;
332 m_styleDeclaration->addParsedProperties(&srcPropertyRef, 1);
334 if (describesParentFont) {
335 // Traverse parsed CSS values and associate CSSFontFaceSrcValue elements with ourselves.
336 RefPtr<CSSValue> src = m_styleDeclaration->getPropertyCSSValue(CSSPropertySrc);
337 CSSValueList* srcList = static_cast<CSSValueList*>(src.get());
339 unsigned srcLength = srcList ? srcList->length() : 0;
340 for (unsigned i = 0; i < srcLength; i++) {
341 if (CSSFontFaceSrcValue* item = static_cast<CSSFontFaceSrcValue*>(srcList->itemWithoutBoundsCheck(i)))
342 item->setSVGFontFaceElement(this);
346 document()->updateStyleSelector();
349 void SVGFontFaceElement::insertedIntoDocument()
351 rebuildFontFace();
352 SVGElement::insertedIntoDocument();
355 void SVGFontFaceElement::childrenChanged(bool changedByParser, Node* beforeChange, Node* afterChange, int childCountDelta)
357 SVGElement::childrenChanged(changedByParser, beforeChange, afterChange, childCountDelta);
358 rebuildFontFace();
363 #endif // ENABLE(SVG_FONTS)