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