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