1/*
2 * Copyright (C) 2007 Eric Seidel <eric@webkit.org>
3 * Copyright (C) 2007, 2008 Nikolas Zimmermann <zimmermann@kde.org>
4 * Copyright (C) 2008 Rob Buis <buis@kde.org>
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/SVGGlyphElement.h"
26
27#include "core/svg/SVGFontData.h"
28#include "core/svg/SVGFontElement.h"
29#include "core/svg/SVGPathUtilities.h"
30
31namespace blink {
32
33inline SVGGlyphElement::SVGGlyphElement(Document& document)
34    : SVGElement(SVGNames::glyphTag, document)
35{
36}
37
38DEFINE_NODE_FACTORY(SVGGlyphElement)
39
40void SVGGlyphElement::invalidateGlyphCache()
41{
42    ContainerNode* fontNode = parentNode();
43    if (isSVGFontElement(fontNode))
44        toSVGFontElement(*fontNode).invalidateGlyphCache();
45}
46
47void SVGGlyphElement::parseAttribute(const QualifiedName& name, const AtomicString& value)
48{
49    if (name == SVGNames::dAttr)
50        invalidateGlyphCache();
51    else
52        SVGElement::parseAttribute(name, value);
53}
54
55Node::InsertionNotificationRequest SVGGlyphElement::insertedInto(ContainerNode* rootParent)
56{
57    invalidateGlyphCache();
58    return SVGElement::insertedInto(rootParent);
59}
60
61void SVGGlyphElement::removedFrom(ContainerNode* rootParent)
62{
63    if (rootParent->inDocument())
64        invalidateGlyphCache();
65    SVGElement::removedFrom(rootParent);
66}
67
68static inline SVGGlyph::ArabicForm parseArabicForm(const AtomicString& value)
69{
70    if (value == "medial")
71        return SVGGlyph::Medial;
72    if (value == "terminal")
73        return SVGGlyph::Terminal;
74    if (value == "isolated")
75        return SVGGlyph::Isolated;
76    if (value == "initial")
77        return SVGGlyph::Initial;
78
79    return SVGGlyph::None;
80}
81
82static inline SVGGlyph::Orientation parseOrientation(const AtomicString& value)
83{
84    if (value == "h")
85        return SVGGlyph::Horizontal;
86    if (value == "v")
87        return SVGGlyph::Vertical;
88
89    return SVGGlyph::Both;
90}
91
92void SVGGlyphElement::inheritUnspecifiedAttributes(SVGGlyph& identifier, const SVGFontData* svgFontData)
93{
94    if (identifier.horizontalAdvanceX == SVGGlyph::inheritedValue())
95        identifier.horizontalAdvanceX = svgFontData->horizontalAdvanceX();
96
97    if (identifier.verticalOriginX == SVGGlyph::inheritedValue())
98        identifier.verticalOriginX = svgFontData->verticalOriginX();
99
100    if (identifier.verticalOriginY == SVGGlyph::inheritedValue())
101        identifier.verticalOriginY = svgFontData->verticalOriginY();
102
103    if (identifier.verticalAdvanceY == SVGGlyph::inheritedValue())
104        identifier.verticalAdvanceY = svgFontData->verticalAdvanceY();
105}
106
107static inline float parseSVGGlyphAttribute(const SVGElement* element, const blink::QualifiedName& name)
108{
109    AtomicString value(element->fastGetAttribute(name));
110    if (value.isEmpty())
111        return SVGGlyph::inheritedValue();
112
113    return value.toFloat();
114}
115
116SVGGlyph SVGGlyphElement::buildGenericGlyphIdentifier(const SVGElement* element)
117{
118    SVGGlyph identifier;
119    buildPathFromString(element->fastGetAttribute(SVGNames::dAttr), identifier.pathData);
120
121    // Spec: The horizontal advance after rendering the glyph in horizontal orientation.
122    // If the attribute is not specified, the effect is as if the attribute were set to the
123    // value of the font's horiz-adv-x attribute. Glyph widths are required to be non-negative,
124    // even if the glyph is typically rendered right-to-left, as in Hebrew and Arabic scripts.
125    identifier.horizontalAdvanceX = parseSVGGlyphAttribute(element, SVGNames::horiz_adv_xAttr);
126
127    // Spec: The X-coordinate in the font coordinate system of the origin of the glyph to be
128    // used when drawing vertically oriented text. If the attribute is not specified, the effect
129    // is as if the attribute were set to the value of the font's vert-origin-x attribute.
130    identifier.verticalOriginX = parseSVGGlyphAttribute(element, SVGNames::vert_origin_xAttr);
131
132    // Spec: The Y-coordinate in the font coordinate system of the origin of a glyph to be
133    // used when drawing vertically oriented text. If the attribute is not specified, the effect
134    // is as if the attribute were set to the value of the font's vert-origin-y attribute.
135    identifier.verticalOriginY = parseSVGGlyphAttribute(element, SVGNames::vert_origin_yAttr);
136
137    // Spec: The vertical advance after rendering a glyph in vertical orientation.
138    // If the attribute is not specified, the effect is as if the attribute were set to the
139    // value of the font's vert-adv-y attribute.
140    identifier.verticalAdvanceY = parseSVGGlyphAttribute(element, SVGNames::vert_adv_yAttr);
141
142    return identifier;
143}
144
145SVGGlyph SVGGlyphElement::buildGlyphIdentifier() const
146{
147    SVGGlyph identifier(buildGenericGlyphIdentifier(this));
148    identifier.glyphName = fastGetAttribute(SVGNames::glyph_nameAttr);
149    identifier.orientation = parseOrientation(fastGetAttribute(SVGNames::orientationAttr));
150    identifier.arabicForm = parseArabicForm(fastGetAttribute(SVGNames::arabic_formAttr));
151
152    String language = fastGetAttribute(SVGNames::langAttr);
153    if (!language.isEmpty())
154        identifier.languages = parseDelimitedString(language, ',');
155
156    return identifier;
157}
158
159}
160
161#endif // ENABLE(SVG_FONTS)
162