Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / core / svg / SVGFontFaceElement.cpp
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.
5  *
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.
10  *
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.
15  *
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.
20  */
21
22 #include "config.h"
23
24 #if ENABLE(SVG_FONTS)
25 #include "core/svg/SVGFontFaceElement.h"
26
27 #include <math.h>
28 #include "CSSPropertyNames.h"
29 #include "CSSValueKeywords.h"
30 #include "core/css/CSSFontFaceSrcValue.h"
31 #include "core/css/CSSStyleSheet.h"
32 #include "core/css/CSSValueList.h"
33 #include "core/css/StylePropertySet.h"
34 #include "core/css/StyleRule.h"
35 #include "core/dom/Attribute.h"
36 #include "core/dom/Document.h"
37 #include "core/svg/SVGDocumentExtensions.h"
38 #include "core/svg/SVGFontElement.h"
39 #include "core/svg/SVGFontFaceSrcElement.h"
40 #include "core/svg/SVGGlyphElement.h"
41 #include "platform/fonts/Font.h"
42
43 namespace WebCore {
44
45 using namespace SVGNames;
46
47 inline SVGFontFaceElement::SVGFontFaceElement(Document& document)
48     : SVGElement(font_faceTag, document)
49     , m_fontFaceRule(StyleRuleFontFace::create())
50     , m_fontElement(0)
51 {
52     ScriptWrappable::init(this);
53     RefPtr<MutableStylePropertySet> styleDeclaration = MutableStylePropertySet::create(HTMLStandardMode);
54     m_fontFaceRule->setProperties(styleDeclaration.release());
55 }
56
57 PassRefPtr<SVGFontFaceElement> SVGFontFaceElement::create(Document& document)
58 {
59     return adoptRef(new SVGFontFaceElement(document));
60 }
61
62 static CSSPropertyID cssPropertyIdForFontFaceAttributeName(const QualifiedName& attrName)
63 {
64     if (!attrName.namespaceURI().isNull())
65         return CSSPropertyInvalid;
66
67     static HashMap<StringImpl*, CSSPropertyID>* propertyNameToIdMap = 0;
68     if (!propertyNameToIdMap) {
69         propertyNameToIdMap = new HashMap<StringImpl*, CSSPropertyID>;
70         // This is a list of all @font-face CSS properties which are exposed as SVG XML attributes
71         // Those commented out are not yet supported by WebCore's style system
72         // mapAttributeToCSSProperty(propertyNameToIdMap, accent_heightAttr);
73         // mapAttributeToCSSProperty(propertyNameToIdMap, alphabeticAttr);
74         // mapAttributeToCSSProperty(propertyNameToIdMap, ascentAttr);
75         // mapAttributeToCSSProperty(propertyNameToIdMap, bboxAttr);
76         // mapAttributeToCSSProperty(propertyNameToIdMap, cap_heightAttr);
77         // mapAttributeToCSSProperty(propertyNameToIdMap, descentAttr);
78         mapAttributeToCSSProperty(propertyNameToIdMap, font_familyAttr);
79         mapAttributeToCSSProperty(propertyNameToIdMap, font_sizeAttr);
80         mapAttributeToCSSProperty(propertyNameToIdMap, font_stretchAttr);
81         mapAttributeToCSSProperty(propertyNameToIdMap, font_styleAttr);
82         mapAttributeToCSSProperty(propertyNameToIdMap, font_variantAttr);
83         mapAttributeToCSSProperty(propertyNameToIdMap, font_weightAttr);
84         // mapAttributeToCSSProperty(propertyNameToIdMap, hangingAttr);
85         // mapAttributeToCSSProperty(propertyNameToIdMap, ideographicAttr);
86         // mapAttributeToCSSProperty(propertyNameToIdMap, mathematicalAttr);
87         // mapAttributeToCSSProperty(propertyNameToIdMap, overline_positionAttr);
88         // mapAttributeToCSSProperty(propertyNameToIdMap, overline_thicknessAttr);
89         // mapAttributeToCSSProperty(propertyNameToIdMap, panose_1Attr);
90         // mapAttributeToCSSProperty(propertyNameToIdMap, slopeAttr);
91         // mapAttributeToCSSProperty(propertyNameToIdMap, stemhAttr);
92         // mapAttributeToCSSProperty(propertyNameToIdMap, stemvAttr);
93         // mapAttributeToCSSProperty(propertyNameToIdMap, strikethrough_positionAttr);
94         // mapAttributeToCSSProperty(propertyNameToIdMap, strikethrough_thicknessAttr);
95         // mapAttributeToCSSProperty(propertyNameToIdMap, underline_positionAttr);
96         // mapAttributeToCSSProperty(propertyNameToIdMap, underline_thicknessAttr);
97         // mapAttributeToCSSProperty(propertyNameToIdMap, unicode_rangeAttr);
98         // mapAttributeToCSSProperty(propertyNameToIdMap, units_per_emAttr);
99         // mapAttributeToCSSProperty(propertyNameToIdMap, v_alphabeticAttr);
100         // mapAttributeToCSSProperty(propertyNameToIdMap, v_hangingAttr);
101         // mapAttributeToCSSProperty(propertyNameToIdMap, v_ideographicAttr);
102         // mapAttributeToCSSProperty(propertyNameToIdMap, v_mathematicalAttr);
103         // mapAttributeToCSSProperty(propertyNameToIdMap, widthsAttr);
104         // mapAttributeToCSSProperty(propertyNameToIdMap, x_heightAttr);
105     }
106
107     return propertyNameToIdMap->get(attrName.localName().impl());
108 }
109
110 void SVGFontFaceElement::parseAttribute(const QualifiedName& name, const AtomicString& value)
111 {
112     CSSPropertyID propId = cssPropertyIdForFontFaceAttributeName(name);
113     if (propId > 0) {
114         m_fontFaceRule->mutableProperties()->setProperty(propId, value, false);
115         rebuildFontFace();
116         return;
117     }
118
119     SVGElement::parseAttribute(name, value);
120 }
121
122 unsigned SVGFontFaceElement::unitsPerEm() const
123 {
124     const AtomicString& value = fastGetAttribute(units_per_emAttr);
125     if (value.isEmpty())
126         return gDefaultUnitsPerEm;
127
128     return static_cast<unsigned>(ceilf(value.toFloat()));
129 }
130
131 int SVGFontFaceElement::xHeight() const
132 {
133     return static_cast<int>(ceilf(fastGetAttribute(x_heightAttr).toFloat()));
134 }
135
136 float SVGFontFaceElement::horizontalOriginX() const
137 {
138     if (!m_fontElement)
139         return 0.0f;
140
141     // Spec: The X-coordinate in the font coordinate system of the origin of a glyph to be used when
142     // drawing horizontally oriented text. (Note that the origin applies to all glyphs in the font.)
143     // If the attribute is not specified, the effect is as if a value of "0" were specified.
144     return m_fontElement->fastGetAttribute(horiz_origin_xAttr).toFloat();
145 }
146
147 float SVGFontFaceElement::horizontalOriginY() const
148 {
149     if (!m_fontElement)
150         return 0.0f;
151
152     // Spec: The Y-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->fastGetAttribute(horiz_origin_yAttr).toFloat();
156 }
157
158 float SVGFontFaceElement::horizontalAdvanceX() const
159 {
160     if (!m_fontElement)
161         return 0.0f;
162
163     // Spec: The default horizontal advance after rendering a glyph in horizontal orientation. Glyph
164     // widths are required to be non-negative, even if the glyph is typically rendered right-to-left,
165     // as in Hebrew and Arabic scripts.
166     return m_fontElement->fastGetAttribute(horiz_adv_xAttr).toFloat();
167 }
168
169 float SVGFontFaceElement::verticalOriginX() const
170 {
171     if (!m_fontElement)
172         return 0.0f;
173
174     // Spec: The default X-coordinate in the font coordinate system of the origin of a glyph to be used when
175     // drawing vertically oriented text. If the attribute is not specified, the effect is as if the attribute
176     // were set to half of the effective value of attribute horiz-adv-x.
177     const AtomicString& value = m_fontElement->fastGetAttribute(vert_origin_xAttr);
178     if (value.isEmpty())
179         return horizontalAdvanceX() / 2.0f;
180
181     return value.toFloat();
182 }
183
184 float SVGFontFaceElement::verticalOriginY() const
185 {
186     if (!m_fontElement)
187         return 0.0f;
188
189     // Spec: The default Y-coordinate in the font coordinate system of the origin of a glyph to be used when
190     // drawing vertically oriented text. If the attribute is not specified, the effect is as if the attribute
191     // were set to the position specified by the font's ascent attribute.
192     const AtomicString& value = m_fontElement->fastGetAttribute(vert_origin_yAttr);
193     if (value.isEmpty())
194         return ascent();
195
196     return value.toFloat();
197 }
198
199 float SVGFontFaceElement::verticalAdvanceY() const
200 {
201     if (!m_fontElement)
202         return 0.0f;
203
204     // Spec: The default vertical advance after rendering a glyph in vertical orientation. If the attribute is
205     // not specified, the effect is as if a value equivalent of one em were specified (see units-per-em).
206     const AtomicString& value = m_fontElement->fastGetAttribute(vert_adv_yAttr);
207        if (value.isEmpty())
208         return 1.0f;
209
210     return value.toFloat();
211 }
212
213 int SVGFontFaceElement::ascent() const
214 {
215     // Spec: Same syntax and semantics as the 'ascent' descriptor within an @font-face rule. The maximum
216     // unaccented height of the font within the font coordinate system. If the attribute is not specified,
217     // the effect is as if the attribute were set to the difference between the units-per-em value and the
218     // vert-origin-y value for the corresponding font.
219     const AtomicString& ascentValue = fastGetAttribute(ascentAttr);
220     if (!ascentValue.isEmpty())
221         return static_cast<int>(ceilf(ascentValue.toFloat()));
222
223     if (m_fontElement) {
224         const AtomicString& vertOriginY = m_fontElement->fastGetAttribute(vert_origin_yAttr);
225         if (!vertOriginY.isEmpty())
226             return static_cast<int>(unitsPerEm()) - static_cast<int>(ceilf(vertOriginY.toFloat()));
227     }
228
229     // Match Batiks default value
230     return static_cast<int>(ceilf(unitsPerEm() * 0.8f));
231 }
232
233 int SVGFontFaceElement::descent() const
234 {
235     // Spec: Same syntax and semantics as the 'descent' descriptor within an @font-face rule. The maximum
236     // unaccented depth of the font within the font coordinate system. If the attribute is not specified,
237     // the effect is as if the attribute were set to the vert-origin-y value for the corresponding font.
238     const AtomicString& descentValue = fastGetAttribute(descentAttr);
239     if (!descentValue.isEmpty()) {
240         // 14 different W3C SVG 1.1 testcases use a negative descent value,
241         // where a positive was meant to be used  Including:
242         // animate-elem-24-t.svg, fonts-elem-01-t.svg, fonts-elem-02-t.svg (and 11 others)
243         int descent = static_cast<int>(ceilf(descentValue.toFloat()));
244         return descent < 0 ? -descent : descent;
245     }
246
247     if (m_fontElement) {
248         const AtomicString& vertOriginY = m_fontElement->fastGetAttribute(vert_origin_yAttr);
249         if (!vertOriginY.isEmpty())
250             return static_cast<int>(ceilf(vertOriginY.toFloat()));
251     }
252
253     // Match Batiks default value
254     return static_cast<int>(ceilf(unitsPerEm() * 0.2f));
255 }
256
257 String SVGFontFaceElement::fontFamily() const
258 {
259     return m_fontFaceRule->properties()->getPropertyValue(CSSPropertyFontFamily);
260 }
261
262 SVGFontElement* SVGFontFaceElement::associatedFontElement() const
263 {
264     ASSERT(parentNode() == m_fontElement);
265     ASSERT(!parentNode() || parentNode()->hasTagName(SVGNames::fontTag));
266     return m_fontElement;
267 }
268
269 void SVGFontFaceElement::rebuildFontFace()
270 {
271     if (!inDocument()) {
272         ASSERT(!m_fontElement);
273         return;
274     }
275
276     bool describesParentFont = parentNode()->hasTagName(SVGNames::fontTag);
277     RefPtrWillBeRawPtr<CSSValueList> list;
278
279     if (describesParentFont) {
280         m_fontElement = toSVGFontElement(parentNode());
281
282         list = CSSValueList::createCommaSeparated();
283         list->append(CSSFontFaceSrcValue::createLocal(fontFamily()));
284     } else {
285         m_fontElement = 0;
286         // we currently ignore all but the last src element, alternatively we could concat them
287         for (Node* child = lastChild(); child && !list; child = child->previousSibling()) {
288             if (child->hasTagName(font_face_srcTag)) {
289                 list = toSVGFontFaceSrcElement(child)->srcValue();
290                 break;
291             }
292         }
293     }
294
295     if (!list || !list->length())
296         return;
297
298     // Parse in-memory CSS rules
299     m_fontFaceRule->mutableProperties()->addParsedProperty(CSSProperty(CSSPropertySrc, list));
300
301     if (describesParentFont) {
302         // Traverse parsed CSS values and associate CSSFontFaceSrcValue elements with ourselves.
303         RefPtr<CSSValue> src = m_fontFaceRule->properties()->getPropertyCSSValue(CSSPropertySrc);
304         CSSValueList* srcList = toCSSValueList(src.get());
305
306         unsigned srcLength = srcList ? srcList->length() : 0;
307         for (unsigned i = 0; i < srcLength; i++) {
308             if (CSSFontFaceSrcValue* item = toCSSFontFaceSrcValue(srcList->itemWithoutBoundsCheck(i)))
309                 item->setSVGFontFaceElement(this);
310         }
311     }
312
313     document().styleResolverChanged(RecalcStyleDeferred);
314 }
315
316 Node::InsertionNotificationRequest SVGFontFaceElement::insertedInto(ContainerNode* rootParent)
317 {
318     SVGElement::insertedInto(rootParent);
319     if (!rootParent->inDocument()) {
320         ASSERT(!m_fontElement);
321         return InsertionDone;
322     }
323     document().accessSVGExtensions()->registerSVGFontFaceElement(this);
324
325     rebuildFontFace();
326     return InsertionDone;
327 }
328
329 void SVGFontFaceElement::removedFrom(ContainerNode* rootParent)
330 {
331     SVGElement::removedFrom(rootParent);
332
333     if (rootParent->inDocument()) {
334         m_fontElement = 0;
335         document().accessSVGExtensions()->unregisterSVGFontFaceElement(this);
336         m_fontFaceRule->mutableProperties()->clear();
337
338         document().styleResolverChanged(RecalcStyleDeferred);
339     } else
340         ASSERT(!m_fontElement);
341 }
342
343 void SVGFontFaceElement::childrenChanged(bool changedByParser, Node* beforeChange, Node* afterChange, int childCountDelta)
344 {
345     SVGElement::childrenChanged(changedByParser, beforeChange, afterChange, childCountDelta);
346     rebuildFontFace();
347 }
348
349 } // namespace WebCore
350
351 #endif // ENABLE(SVG_FONTS)