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.
25 #include "SVGFontFaceElement.h"
27 #include "Attribute.h"
28 #include "CSSFontFaceSrcValue.h"
29 #include "CSSParser.h"
30 #include "CSSProperty.h"
31 #include "CSSPropertyNames.h"
32 #include "CSSStyleSelector.h"
33 #include "CSSStyleSheet.h"
34 #include "CSSValueKeywords.h"
35 #include "CSSValueList.h"
38 #include "SVGDocumentExtensions.h"
39 #include "SVGFontElement.h"
40 #include "SVGFontFaceSrcElement.h"
41 #include "SVGGlyphElement.h"
43 #include "StyleRule.h"
48 using namespace SVGNames;
50 inline SVGFontFaceElement::SVGFontFaceElement(const QualifiedName& tagName, Document* document)
51 : SVGElement(tagName, document)
52 , m_fontFaceRule(StyleRuleFontFace::create())
54 ASSERT(hasTagName(font_faceTag));
55 RefPtr<StylePropertySet> styleDeclaration = StylePropertySet::create(CSSStrictMode);
56 m_fontFaceRule->setProperties(styleDeclaration.release());
59 PassRefPtr<SVGFontFaceElement> SVGFontFaceElement::create(const QualifiedName& tagName, Document* document)
61 return adoptRef(new SVGFontFaceElement(tagName, document));
64 static int cssPropertyIdForSVGAttributeName(const QualifiedName& attrName)
66 if (!attrName.namespaceURI().isNull())
69 static HashMap<AtomicStringImpl*, int>* propertyNameToIdMap = 0;
70 if (!propertyNameToIdMap) {
71 propertyNameToIdMap = new HashMap<AtomicStringImpl*, int>;
72 // This is a list of all @font-face CSS properties which are exposed as SVG XML attributes
73 // Those commented out are not yet supported by WebCore's style system
74 // mapAttributeToCSSProperty(propertyNameToIdMap, accent_heightAttr);
75 // mapAttributeToCSSProperty(propertyNameToIdMap, alphabeticAttr);
76 // mapAttributeToCSSProperty(propertyNameToIdMap, ascentAttr);
77 // mapAttributeToCSSProperty(propertyNameToIdMap, bboxAttr);
78 // mapAttributeToCSSProperty(propertyNameToIdMap, cap_heightAttr);
79 // mapAttributeToCSSProperty(propertyNameToIdMap, descentAttr);
80 mapAttributeToCSSProperty(propertyNameToIdMap, font_familyAttr);
81 mapAttributeToCSSProperty(propertyNameToIdMap, font_sizeAttr);
82 mapAttributeToCSSProperty(propertyNameToIdMap, font_stretchAttr);
83 mapAttributeToCSSProperty(propertyNameToIdMap, font_styleAttr);
84 mapAttributeToCSSProperty(propertyNameToIdMap, font_variantAttr);
85 mapAttributeToCSSProperty(propertyNameToIdMap, font_weightAttr);
86 // mapAttributeToCSSProperty(propertyNameToIdMap, hangingAttr);
87 // mapAttributeToCSSProperty(propertyNameToIdMap, ideographicAttr);
88 // mapAttributeToCSSProperty(propertyNameToIdMap, mathematicalAttr);
89 // mapAttributeToCSSProperty(propertyNameToIdMap, overline_positionAttr);
90 // mapAttributeToCSSProperty(propertyNameToIdMap, overline_thicknessAttr);
91 // mapAttributeToCSSProperty(propertyNameToIdMap, panose_1Attr);
92 // mapAttributeToCSSProperty(propertyNameToIdMap, slopeAttr);
93 // mapAttributeToCSSProperty(propertyNameToIdMap, stemhAttr);
94 // mapAttributeToCSSProperty(propertyNameToIdMap, stemvAttr);
95 // mapAttributeToCSSProperty(propertyNameToIdMap, strikethrough_positionAttr);
96 // mapAttributeToCSSProperty(propertyNameToIdMap, strikethrough_thicknessAttr);
97 // mapAttributeToCSSProperty(propertyNameToIdMap, underline_positionAttr);
98 // mapAttributeToCSSProperty(propertyNameToIdMap, underline_thicknessAttr);
99 // mapAttributeToCSSProperty(propertyNameToIdMap, unicode_rangeAttr);
100 // mapAttributeToCSSProperty(propertyNameToIdMap, units_per_emAttr);
101 // mapAttributeToCSSProperty(propertyNameToIdMap, v_alphabeticAttr);
102 // mapAttributeToCSSProperty(propertyNameToIdMap, v_hangingAttr);
103 // mapAttributeToCSSProperty(propertyNameToIdMap, v_ideographicAttr);
104 // mapAttributeToCSSProperty(propertyNameToIdMap, v_mathematicalAttr);
105 // mapAttributeToCSSProperty(propertyNameToIdMap, widthsAttr);
106 // mapAttributeToCSSProperty(propertyNameToIdMap, x_heightAttr);
109 return propertyNameToIdMap->get(attrName.localName().impl());
112 void SVGFontFaceElement::parseAttribute(Attribute* attr)
114 int propId = cssPropertyIdForSVGAttributeName(attr->name());
116 m_fontFaceRule->properties()->setProperty(propId, attr->value(), false);
121 SVGElement::parseAttribute(attr);
124 unsigned SVGFontFaceElement::unitsPerEm() const
126 const AtomicString& value = fastGetAttribute(units_per_emAttr);
128 return gDefaultUnitsPerEm;
130 return static_cast<unsigned>(ceilf(value.toFloat()));
133 int SVGFontFaceElement::xHeight() const
135 return static_cast<int>(ceilf(fastGetAttribute(x_heightAttr).toFloat()));
138 float SVGFontFaceElement::horizontalOriginX() const
143 // Spec: The X-coordinate in the font coordinate system of the origin of a glyph to be used when
144 // drawing horizontally oriented text. (Note that the origin applies to all glyphs in the font.)
145 // If the attribute is not specified, the effect is as if a value of "0" were specified.
146 return m_fontElement->fastGetAttribute(horiz_origin_xAttr).toFloat();
149 float SVGFontFaceElement::horizontalOriginY() const
154 // Spec: The Y-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 return m_fontElement->fastGetAttribute(horiz_origin_yAttr).toFloat();
160 float SVGFontFaceElement::horizontalAdvanceX() const
165 // Spec: The default horizontal advance after rendering a glyph in horizontal orientation. Glyph
166 // widths are required to be non-negative, even if the glyph is typically rendered right-to-left,
167 // as in Hebrew and Arabic scripts.
168 return m_fontElement->fastGetAttribute(horiz_adv_xAttr).toFloat();
171 float SVGFontFaceElement::verticalOriginX() const
176 // Spec: The default X-coordinate in the font coordinate system of the origin of a glyph to be used when
177 // drawing vertically oriented text. If the attribute is not specified, the effect is as if the attribute
178 // were set to half of the effective value of attribute horiz-adv-x.
179 const AtomicString& value = m_fontElement->fastGetAttribute(vert_origin_xAttr);
181 return horizontalAdvanceX() / 2.0f;
183 return value.toFloat();
186 float SVGFontFaceElement::verticalOriginY() const
191 // Spec: The default Y-coordinate in the font coordinate system of the origin of a glyph to be used when
192 // drawing vertically oriented text. If the attribute is not specified, the effect is as if the attribute
193 // were set to the position specified by the font's ascent attribute.
194 const AtomicString& value = m_fontElement->fastGetAttribute(vert_origin_yAttr);
198 return value.toFloat();
201 float SVGFontFaceElement::verticalAdvanceY() const
206 // Spec: The default vertical advance after rendering a glyph in vertical orientation. If the attribute is
207 // not specified, the effect is as if a value equivalent of one em were specified (see units-per-em).
208 const AtomicString& value = m_fontElement->fastGetAttribute(vert_adv_yAttr);
212 return value.toFloat();
215 int SVGFontFaceElement::ascent() const
217 // Spec: Same syntax and semantics as the 'ascent' descriptor within an @font-face rule. The maximum
218 // unaccented height of the font within the font coordinate system. If the attribute is not specified,
219 // the effect is as if the attribute were set to the difference between the units-per-em value and the
220 // vert-origin-y value for the corresponding font.
221 const AtomicString& ascentValue = fastGetAttribute(ascentAttr);
222 if (!ascentValue.isEmpty())
223 return static_cast<int>(ceilf(ascentValue.toFloat()));
226 const AtomicString& vertOriginY = m_fontElement->fastGetAttribute(vert_origin_yAttr);
227 if (!vertOriginY.isEmpty())
228 return static_cast<int>(unitsPerEm()) - static_cast<int>(ceilf(vertOriginY.toFloat()));
231 // Match Batiks default value
232 return static_cast<int>(ceilf(unitsPerEm() * 0.8f));
235 int SVGFontFaceElement::descent() const
237 // Spec: Same syntax and semantics as the 'descent' descriptor within an @font-face rule. The maximum
238 // unaccented depth of the font within the font coordinate system. If the attribute is not specified,
239 // the effect is as if the attribute were set to the vert-origin-y value for the corresponding font.
240 const AtomicString& descentValue = fastGetAttribute(descentAttr);
241 if (!descentValue.isEmpty()) {
242 // 14 different W3C SVG 1.1 testcases use a negative descent value,
243 // where a positive was meant to be used Including:
244 // animate-elem-24-t.svg, fonts-elem-01-t.svg, fonts-elem-02-t.svg (and 11 others)
245 int descent = static_cast<int>(ceilf(descentValue.toFloat()));
246 return descent < 0 ? -descent : descent;
250 const AtomicString& vertOriginY = m_fontElement->fastGetAttribute(vert_origin_yAttr);
251 if (!vertOriginY.isEmpty())
252 return static_cast<int>(ceilf(vertOriginY.toFloat()));
255 // Match Batiks default value
256 return static_cast<int>(ceilf(unitsPerEm() * 0.2f));
259 String SVGFontFaceElement::fontFamily() const
261 return m_fontFaceRule->properties()->getPropertyValue(CSSPropertyFontFamily);
264 SVGFontElement* SVGFontFaceElement::associatedFontElement() const
266 return m_fontElement.get();
269 void SVGFontFaceElement::rebuildFontFace()
274 // we currently ignore all but the first src element, alternatively we could concat them
275 SVGFontFaceSrcElement* srcElement = 0;
277 for (Node* child = firstChild(); child && !srcElement; child = child->nextSibling()) {
278 if (child->hasTagName(font_face_srcTag))
279 srcElement = static_cast<SVGFontFaceSrcElement*>(child);
282 bool describesParentFont = parentNode()->hasTagName(SVGNames::fontTag);
283 RefPtr<CSSValueList> list;
285 if (describesParentFont) {
286 m_fontElement = static_cast<SVGFontElement*>(parentNode());
288 list = CSSValueList::createCommaSeparated();
289 list->append(CSSFontFaceSrcValue::createLocal(fontFamily()));
293 list = srcElement->srcValue();
296 if (!list || !list->length())
299 // Parse in-memory CSS rules
300 CSSProperty srcProperty(CSSPropertySrc, list);
301 m_fontFaceRule->properties()->addParsedProperties(&srcProperty, 1);
303 if (describesParentFont) {
304 // Traverse parsed CSS values and associate CSSFontFaceSrcValue elements with ourselves.
305 RefPtr<CSSValue> src = m_fontFaceRule->properties()->getPropertyCSSValue(CSSPropertySrc);
306 CSSValueList* srcList = static_cast<CSSValueList*>(src.get());
308 unsigned srcLength = srcList ? srcList->length() : 0;
309 for (unsigned i = 0; i < srcLength; i++) {
310 if (CSSFontFaceSrcValue* item = static_cast<CSSFontFaceSrcValue*>(srcList->itemWithoutBoundsCheck(i)))
311 item->setSVGFontFaceElement(this);
315 document()->styleSelectorChanged(DeferRecalcStyle);
318 void SVGFontFaceElement::insertedIntoDocument()
320 SVGElement::insertedIntoDocument();
322 document()->accessSVGExtensions()->registerSVGFontFaceElement(this);
327 void SVGFontFaceElement::removedFromDocument()
329 SVGElement::removedFromDocument();
331 document()->accessSVGExtensions()->unregisterSVGFontFaceElement(this);
332 m_fontFaceRule->properties()->parseDeclaration(emptyString(), 0);
334 document()->styleSelectorChanged(DeferRecalcStyle);
337 void SVGFontFaceElement::childrenChanged(bool changedByParser, Node* beforeChange, Node* afterChange, int childCountDelta)
339 SVGElement::childrenChanged(changedByParser, beforeChange, afterChange, childCountDelta);
343 } // namespace WebCore
345 #endif // ENABLE(SVG_FONTS)