Rubber-stamped by Brady Eidson.
[webbrowser.git] / WebCore / svg / SVGFontFaceElement.cpp
blobaa0b6d8cea94a28534062a25190530e9f81ca5e9
1 /*
2 Copyright (C) 2007 Eric Seidel <eric@webkit.org>
3 Copyright (C) 2007, 2008 Nikolas Zimmermann <zimmermann@kde.org>
4 Copyright (C) 2008 Apple Inc. All rights reserved.
6 This library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Library General Public
8 License as published by the Free Software Foundation; either
9 version 2 of the License, or (at your option) any later version.
11 This library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Library General Public License for more details.
16 You should have received a copy of the GNU Library General Public License
17 along with this library; see the file COPYING.LIB. If not, write to
18 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19 Boston, MA 02110-1301, USA.
22 #include "config.h"
24 #if ENABLE(SVG_FONTS)
25 #include "SVGFontFaceElement.h"
27 #include "CString.h"
28 #include "CSSFontFaceRule.h"
29 #include "CSSFontFaceSrcValue.h"
30 #include "CSSParser.h"
31 #include "CSSProperty.h"
32 #include "CSSPropertyNames.h"
33 #include "CSSStyleSelector.h"
34 #include "CSSStyleSheet.h"
35 #include "CSSValueKeywords.h"
36 #include "CSSValueList.h"
37 #include "Document.h"
38 #include "Font.h"
39 #include "MappedAttribute.h"
40 #include "SVGFontElement.h"
41 #include "SVGFontFaceSrcElement.h"
42 #include "SVGGlyphElement.h"
43 #include "SVGNames.h"
44 #include <math.h>
46 namespace WebCore {
48 using namespace SVGNames;
50 SVGFontFaceElement::SVGFontFaceElement(const QualifiedName& tagName, Document* doc)
51 : SVGElement(tagName, doc)
52 , m_fontFaceRule(CSSFontFaceRule::create())
53 , m_styleDeclaration(CSSMutableStyleDeclaration::create())
55 m_styleDeclaration->setParent(document()->mappedElementSheet());
56 m_styleDeclaration->setStrictParsing(true);
57 m_fontFaceRule->setDeclaration(m_styleDeclaration.get());
60 SVGFontFaceElement::~SVGFontFaceElement()
62 removeFromMappedElementSheet();
65 static void mapAttributeToCSSProperty(HashMap<AtomicStringImpl*, int>* propertyNameToIdMap, const QualifiedName& attrName)
67 int propertyId = cssPropertyID(attrName.localName());
68 ASSERT(propertyId > 0);
69 propertyNameToIdMap->set(attrName.localName().impl(), propertyId);
72 static int cssPropertyIdForSVGAttributeName(const QualifiedName& attrName)
74 if (!attrName.namespaceURI().isNull())
75 return 0;
77 static HashMap<AtomicStringImpl*, int>* propertyNameToIdMap = 0;
78 if (!propertyNameToIdMap) {
79 propertyNameToIdMap = new HashMap<AtomicStringImpl*, int>;
80 // This is a list of all @font-face CSS properties which are exposed as SVG XML attributes
81 // Those commented out are not yet supported by WebCore's style system
82 //mapAttributeToCSSProperty(propertyNameToIdMap, accent_heightAttr);
83 //mapAttributeToCSSProperty(propertyNameToIdMap, alphabeticAttr);
84 //mapAttributeToCSSProperty(propertyNameToIdMap, ascentAttr);
85 //mapAttributeToCSSProperty(propertyNameToIdMap, bboxAttr);
86 //mapAttributeToCSSProperty(propertyNameToIdMap, cap_heightAttr);
87 //mapAttributeToCSSProperty(propertyNameToIdMap, descentAttr);
88 mapAttributeToCSSProperty(propertyNameToIdMap, font_familyAttr);
89 mapAttributeToCSSProperty(propertyNameToIdMap, font_sizeAttr);
90 mapAttributeToCSSProperty(propertyNameToIdMap, font_stretchAttr);
91 mapAttributeToCSSProperty(propertyNameToIdMap, font_styleAttr);
92 mapAttributeToCSSProperty(propertyNameToIdMap, font_variantAttr);
93 mapAttributeToCSSProperty(propertyNameToIdMap, font_weightAttr);
94 //mapAttributeToCSSProperty(propertyNameToIdMap, hangingAttr);
95 //mapAttributeToCSSProperty(propertyNameToIdMap, ideographicAttr);
96 //mapAttributeToCSSProperty(propertyNameToIdMap, mathematicalAttr);
97 //mapAttributeToCSSProperty(propertyNameToIdMap, overline_positionAttr);
98 //mapAttributeToCSSProperty(propertyNameToIdMap, overline_thicknessAttr);
99 //mapAttributeToCSSProperty(propertyNameToIdMap, panose_1Attr);
100 //mapAttributeToCSSProperty(propertyNameToIdMap, slopeAttr);
101 //mapAttributeToCSSProperty(propertyNameToIdMap, stemhAttr);
102 //mapAttributeToCSSProperty(propertyNameToIdMap, stemvAttr);
103 //mapAttributeToCSSProperty(propertyNameToIdMap, strikethrough_positionAttr);
104 //mapAttributeToCSSProperty(propertyNameToIdMap, strikethrough_thicknessAttr);
105 //mapAttributeToCSSProperty(propertyNameToIdMap, underline_positionAttr);
106 //mapAttributeToCSSProperty(propertyNameToIdMap, underline_thicknessAttr);
107 //mapAttributeToCSSProperty(propertyNameToIdMap, unicode_rangeAttr);
108 //mapAttributeToCSSProperty(propertyNameToIdMap, units_per_emAttr);
109 //mapAttributeToCSSProperty(propertyNameToIdMap, v_alphabeticAttr);
110 //mapAttributeToCSSProperty(propertyNameToIdMap, v_hangingAttr);
111 //mapAttributeToCSSProperty(propertyNameToIdMap, v_ideographicAttr);
112 //mapAttributeToCSSProperty(propertyNameToIdMap, v_mathematicalAttr);
113 //mapAttributeToCSSProperty(propertyNameToIdMap, widthsAttr);
114 //mapAttributeToCSSProperty(propertyNameToIdMap, x_heightAttr);
117 return propertyNameToIdMap->get(attrName.localName().impl());
120 void SVGFontFaceElement::parseMappedAttribute(MappedAttribute* attr)
122 int propId = cssPropertyIdForSVGAttributeName(attr->name());
123 if (propId > 0) {
124 m_styleDeclaration->setProperty(propId, attr->value(), false);
125 if (inDocument())
126 rebuildFontFace();
127 return;
130 SVGElement::parseMappedAttribute(attr);
133 unsigned SVGFontFaceElement::unitsPerEm() const
135 const AtomicString& value = getAttribute(units_per_emAttr);
136 if (value.isEmpty())
137 return defaultUnitsPerEm;
139 return static_cast<unsigned>(ceilf(value.toFloat()));
142 int SVGFontFaceElement::xHeight() const
144 return static_cast<int>(ceilf(getAttribute(x_heightAttr).toFloat()));
147 float SVGFontFaceElement::horizontalOriginX() const
149 if (!m_fontElement)
150 return 0.0f;
152 // Spec: The X-coordinate in the font coordinate system of the origin of a glyph to be used when
153 // drawing horizontally oriented text. (Note that the origin applies to all glyphs in the font.)
154 // If the attribute is not specified, the effect is as if a value of "0" were specified.
155 return m_fontElement->getAttribute(horiz_origin_xAttr).toFloat();
158 float SVGFontFaceElement::horizontalOriginY() const
160 if (!m_fontElement)
161 return 0.0f;
163 // Spec: The Y-coordinate in the font coordinate system of the origin of a glyph to be used when
164 // drawing horizontally oriented text. (Note that the origin applies to all glyphs in the font.)
165 // If the attribute is not specified, the effect is as if a value of "0" were specified.
166 return m_fontElement->getAttribute(horiz_origin_yAttr).toFloat();
169 float SVGFontFaceElement::horizontalAdvanceX() const
171 if (!m_fontElement)
172 return 0.0f;
174 // Spec: The default horizontal advance after rendering a glyph in horizontal orientation. Glyph
175 // widths are required to be non-negative, even if the glyph is typically rendered right-to-left,
176 // as in Hebrew and Arabic scripts.
177 return m_fontElement->getAttribute(horiz_adv_xAttr).toFloat();
180 float SVGFontFaceElement::verticalOriginX() const
182 if (!m_fontElement)
183 return 0.0f;
185 // Spec: The default X-coordinate in the font coordinate system of the origin of a glyph to be used when
186 // drawing vertically oriented text. If the attribute is not specified, the effect is as if the attribute
187 // were set to half of the effective value of attribute horiz-adv-x.
188 const AtomicString& value = m_fontElement->getAttribute(vert_origin_xAttr);
189 if (value.isEmpty())
190 return horizontalAdvanceX() / 2.0f;
192 return value.toFloat();
195 float SVGFontFaceElement::verticalOriginY() const
197 if (!m_fontElement)
198 return 0.0f;
200 // Spec: The default Y-coordinate in the font coordinate system of the origin of a glyph to be used when
201 // drawing vertically oriented text. If the attribute is not specified, the effect is as if the attribute
202 // were set to the position specified by the font's ascent attribute.
203 const AtomicString& value = m_fontElement->getAttribute(vert_origin_yAttr);
204 if (value.isEmpty())
205 return ascent();
207 return value.toFloat();
210 float SVGFontFaceElement::verticalAdvanceY() const
212 if (!m_fontElement)
213 return 0.0f;
215 // Spec: The default vertical advance after rendering a glyph in vertical orientation. If the attribute is
216 // not specified, the effect is as if a value equivalent of one em were specified (see units-per-em).
217 const AtomicString& value = m_fontElement->getAttribute(vert_adv_yAttr);
218 if (value.isEmpty())
219 return 1.0f;
221 return value.toFloat();
224 int SVGFontFaceElement::ascent() const
226 // Spec: Same syntax and semantics as the 'ascent' descriptor within an @font-face rule. The maximum
227 // unaccented height of the font within the font coordinate system. If the attribute is not specified,
228 // the effect is as if the attribute were set to the difference between the units-per-em value and the
229 // vert-origin-y value for the corresponding font.
230 const AtomicString& ascentValue = getAttribute(ascentAttr);
231 if (!ascentValue.isEmpty())
232 return static_cast<int>(ceilf(ascentValue.toFloat()));
234 if (m_fontElement) {
235 const AtomicString& vertOriginY = m_fontElement->getAttribute(vert_origin_yAttr);
236 if (!vertOriginY.isEmpty())
237 return static_cast<int>(unitsPerEm()) - static_cast<int>(ceilf(vertOriginY.toFloat()));
240 // Match Batiks default value
241 return static_cast<int>(ceilf(unitsPerEm() * 0.8f));
244 int SVGFontFaceElement::descent() const
246 // Spec: Same syntax and semantics as the 'descent' descriptor within an @font-face rule. The maximum
247 // unaccented depth of the font within the font coordinate system. If the attribute is not specified,
248 // the effect is as if the attribute were set to the vert-origin-y value for the corresponding font.
249 const AtomicString& descentValue = getAttribute(descentAttr);
250 if (!descentValue.isEmpty()) {
251 // 14 different W3C SVG 1.1 testcases use a negative descent value,
252 // where a positive was meant to be used Including:
253 // animate-elem-24-t.svg, fonts-elem-01-t.svg, fonts-elem-02-t.svg (and 11 others)
254 int descent = static_cast<int>(ceilf(descentValue.toFloat()));
255 return descent < 0 ? -descent : descent;
258 if (m_fontElement) {
259 const AtomicString& vertOriginY = m_fontElement->getAttribute(vert_origin_yAttr);
260 if (!vertOriginY.isEmpty())
261 return static_cast<int>(ceilf(vertOriginY.toFloat()));
264 // Match Batiks default value
265 return static_cast<int>(ceilf(unitsPerEm() * 0.2f));
268 String SVGFontFaceElement::fontFamily() const
270 return m_styleDeclaration->getPropertyValue(CSSPropertyFontFamily);
273 void SVGFontFaceElement::rebuildFontFace()
275 ASSERT(inDocument());
277 // we currently ignore all but the first src element, alternatively we could concat them
278 SVGFontFaceSrcElement* srcElement = 0;
280 for (Node* child = firstChild(); child && !srcElement; child = child->nextSibling()) {
281 if (child->hasTagName(font_face_srcTag))
282 srcElement = static_cast<SVGFontFaceSrcElement*>(child);
285 bool describesParentFont = parentNode()->hasTagName(fontTag);
286 RefPtr<CSSValueList> list;
288 if (describesParentFont) {
289 m_fontElement = static_cast<SVGFontElement*>(parentNode());
291 list = CSSValueList::createCommaSeparated();
292 list->append(CSSFontFaceSrcValue::createLocal(fontFamily()));
293 } else {
294 m_fontElement = 0;
295 if (srcElement)
296 list = srcElement->srcValue();
299 if (!list)
300 return;
302 // Parse in-memory CSS rules
303 CSSProperty srcProperty(CSSPropertySrc, list);
304 const CSSProperty* srcPropertyRef = &srcProperty;
305 m_styleDeclaration->addParsedProperties(&srcPropertyRef, 1);
307 if (describesParentFont) {
308 // Traverse parsed CSS values and associate CSSFontFaceSrcValue elements with ourselves.
309 RefPtr<CSSValue> src = m_styleDeclaration->getPropertyCSSValue(CSSPropertySrc);
310 CSSValueList* srcList = static_cast<CSSValueList*>(src.get());
312 unsigned srcLength = srcList ? srcList->length() : 0;
313 for (unsigned i = 0; i < srcLength; i++) {
314 if (CSSFontFaceSrcValue* item = static_cast<CSSFontFaceSrcValue*>(srcList->itemWithoutBoundsCheck(i)))
315 item->setSVGFontFaceElement(this);
319 document()->updateStyleSelector();
322 void SVGFontFaceElement::insertedIntoDocument()
324 SVGElement::insertedIntoDocument();
325 document()->mappedElementSheet()->append(m_fontFaceRule);
326 rebuildFontFace();
329 void SVGFontFaceElement::removedFromDocument()
331 removeFromMappedElementSheet();
332 SVGElement::removedFromDocument();
335 void SVGFontFaceElement::childrenChanged(bool changedByParser, Node* beforeChange, Node* afterChange, int childCountDelta)
337 SVGElement::childrenChanged(changedByParser, beforeChange, afterChange, childCountDelta);
338 if (inDocument())
339 rebuildFontFace();
342 void SVGFontFaceElement::removeFromMappedElementSheet()
344 CSSStyleSheet* mappedElementSheet = document()->mappedElementSheet();
345 if (!mappedElementSheet)
346 return;
348 for (unsigned i = 0; i < mappedElementSheet->length(); ++i) {
349 if (mappedElementSheet->item(i) == m_fontFaceRule) {
350 mappedElementSheet->remove(i);
351 break;
354 document()->updateStyleSelector();
357 } // namespace WebCore
359 #endif // ENABLE(SVG_FONTS)