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