2 * Copyright (C) 2011 Leo Yang <leoyang@webkit.org>
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
14 * You should have received a copy of the GNU Library General Public License
15 * along with this library; see the file COPYING.LIB. If not, write to
16 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
23 #include "core/svg/SVGAltGlyphDefElement.h"
25 #include "core/SVGNames.h"
26 #include "core/dom/ElementTraversal.h"
27 #include "core/svg/SVGAltGlyphItemElement.h"
28 #include "core/svg/SVGGlyphRefElement.h"
32 inline SVGAltGlyphDefElement::SVGAltGlyphDefElement(Document& document)
33 : SVGElement(SVGNames::altGlyphDefTag, document)
35 ScriptWrappable::init(this);
38 DEFINE_NODE_FACTORY(SVGAltGlyphDefElement)
40 bool SVGAltGlyphDefElement::hasValidGlyphElements(Vector<AtomicString>& glyphNames) const
42 // Spec: http://www.w3.org/TR/SVG/text.html#AltGlyphDefElement
43 // An 'altGlyphDef' can contain either of the following:
45 // 1. In the simplest case, an 'altGlyphDef' contains one or more 'glyphRef' elements.
46 // Each 'glyphRef' element references a single glyph within a particular font.
47 // If all of the referenced glyphs are available, then these glyphs are rendered
48 // instead of the character(s) inside of the referencing 'altGlyph' element.
49 // If any of the referenced glyphs are unavailable, then the character(s) that are
50 // inside of the 'altGlyph' element are rendered as if there were not an 'altGlyph'
51 // element surrounding those characters.
53 // 2. In the more complex case, an 'altGlyphDef' contains one or more 'altGlyphItem' elements.
54 // Each 'altGlyphItem' represents a candidate set of substitute glyphs. Each 'altGlyphItem'
55 // contains one or more 'glyphRef' elements. Each 'glyphRef' element references a single
56 // glyph within a particular font. The first 'altGlyphItem' in which all referenced glyphs
57 // are available is chosen. The glyphs referenced from this 'altGlyphItem' are rendered
58 // instead of the character(s) that are inside of the referencing 'altGlyph' element.
59 // If none of the 'altGlyphItem' elements result in a successful match (i.e., none of the
60 // 'altGlyphItem' elements has all of its referenced glyphs available), then the character(s)
61 // that are inside of the 'altGlyph' element are rendered as if there were not an 'altGlyph'
62 // element surrounding those characters.
64 // The spec doesn't tell how to deal with the mixing of <glyphRef> and <altGlyItem>.
65 // However, we determine content model by the the type of the first appearing element
66 // just like Opera 11 does. After the content model is determined we skip elements
67 // which don't comform to it. For example:
69 // <glyphRef id="g1" />
70 // <altGlyphItem id="i1"> ... </altGlyphItem>
71 // <glyphRef id="g2" />
75 // <altGlyphItem id="i1"> ... </altGlyphItem>
76 // <altGlyphItem id="i2"> ... </altGlyphItem>
77 // <glyphRef id="g1" />
78 // <glyphRef id="g2" />
80 // For a), the content model is 1), so we will use "g1" and "g2" if they are all valid
81 // and "i1" is skipped.
82 // For b), the content model is 2), so we will use <glyphRef> elements contained in
83 // "i1" if they are valid and "g1" and "g2" are skipped.
85 // These 2 variables are used to determine content model.
86 bool fountFirstGlyphRef = false;
87 bool foundFirstAltGlyphItem = false;
89 for (SVGElement* child = Traversal<SVGElement>::firstChild(*this); child; child = Traversal<SVGElement>::nextSibling(*child)) {
90 if (!foundFirstAltGlyphItem && isSVGGlyphRefElement(*child)) {
91 fountFirstGlyphRef = true;
92 AtomicString referredGlyphName;
94 if (toSVGGlyphRefElement(child)->hasValidGlyphElement(referredGlyphName))
95 glyphNames.append(referredGlyphName);
97 // As the spec says "If any of the referenced glyphs are unavailable,
98 // then the character(s) that are inside of the 'altGlyph' element are
99 // rendered as if there were not an 'altGlyph' element surrounding
100 // those characters.".
104 } else if (!fountFirstGlyphRef && isSVGAltGlyphItemElement(*child)) {
105 foundFirstAltGlyphItem = true;
106 Vector<AtomicString> referredGlyphNames;
108 // As the spec says "The first 'altGlyphItem' in which all referenced glyphs
109 // are available is chosen."
110 if (toSVGAltGlyphItemElement(child)->hasValidGlyphElements(glyphNames) && !glyphNames.isEmpty())
114 return !glyphNames.isEmpty();