| 1 | /* |
| 2 | * Copyright (C) 2011 Leo Yang <leoyang@webkit.org> |
| 3 | * |
| 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. |
| 8 | * |
| 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. |
| 13 | * |
| 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. |
| 18 | */ |
| 19 | |
| 20 | #include "config.h" |
| 21 | |
| 22 | #if ENABLE(SVG_FONTS) |
| 23 | #include "SVGAltGlyphDefElement.h" |
| 24 | |
| 25 | #include "ElementIterator.h" |
| 26 | #include "SVGAltGlyphItemElement.h" |
| 27 | #include "SVGGlyphRefElement.h" |
| 28 | #include "SVGNames.h" |
| 29 | #include <wtf/IsoMallocInlines.h> |
| 30 | |
| 31 | namespace WebCore { |
| 32 | |
| 33 | WTF_MAKE_ISO_ALLOCATED_IMPL(SVGAltGlyphDefElement); |
| 34 | |
| 35 | inline SVGAltGlyphDefElement::SVGAltGlyphDefElement(const QualifiedName& tagName, Document& document) |
| 36 | : SVGElement(tagName, document) |
| 37 | { |
| 38 | ASSERT(hasTagName(SVGNames::altGlyphDefTag)); |
| 39 | } |
| 40 | |
| 41 | Ref<SVGAltGlyphDefElement> SVGAltGlyphDefElement::create(const QualifiedName& tagName, Document& document) |
| 42 | { |
| 43 | return adoptRef(*new SVGAltGlyphDefElement(tagName, document)); |
| 44 | } |
| 45 | |
| 46 | bool SVGAltGlyphDefElement::hasValidGlyphElements(Vector<String>& glyphNames) const |
| 47 | { |
| 48 | // Spec: http://www.w3.org/TR/SVG/text.html#AltGlyphDefElement |
| 49 | // An 'altGlyphDef' can contain either of the following: |
| 50 | // |
| 51 | // 1. In the simplest case, an 'altGlyphDef' contains one or more 'glyphRef' elements. |
| 52 | // Each 'glyphRef' element references a single glyph within a particular font. |
| 53 | // If all of the referenced glyphs are available, then these glyphs are rendered |
| 54 | // instead of the character(s) inside of the referencing 'altGlyph' element. |
| 55 | // If any of the referenced glyphs are unavailable, then the character(s) that are |
| 56 | // inside of the 'altGlyph' element are rendered as if there were not an 'altGlyph' |
| 57 | // element surrounding those characters. |
| 58 | // |
| 59 | // 2. In the more complex case, an 'altGlyphDef' contains one or more 'altGlyphItem' elements. |
| 60 | // Each 'altGlyphItem' represents a candidate set of substitute glyphs. Each 'altGlyphItem' |
| 61 | // contains one or more 'glyphRef' elements. Each 'glyphRef' element references a single |
| 62 | // glyph within a particular font. The first 'altGlyphItem' in which all referenced glyphs |
| 63 | // are available is chosen. The glyphs referenced from this 'altGlyphItem' are rendered |
| 64 | // instead of the character(s) that are inside of the referencing 'altGlyph' element. |
| 65 | // If none of the 'altGlyphItem' elements result in a successful match (i.e., none of the |
| 66 | // 'altGlyphItem' elements has all of its referenced glyphs available), then the character(s) |
| 67 | // that are inside of the 'altGlyph' element are rendered as if there were not an 'altGlyph' |
| 68 | // element surrounding those characters. |
| 69 | // |
| 70 | // The spec doesn't tell how to deal with the mixing of <glyphRef> and <altGlyItem>. |
| 71 | // However, we determine content model by the type of the first appearing element |
| 72 | // just like Opera 11 does. After the content model is determined we skip elements |
| 73 | // which don't comform to it. For example: |
| 74 | // a. <altGlyphDef> |
| 75 | // <glyphRef id="g1" /> |
| 76 | // <altGlyphItem id="i1"> ... </altGlyphItem> |
| 77 | // <glyphRef id="g2" /> |
| 78 | // </altGlyphDef> |
| 79 | // |
| 80 | // b. <altGlyphDef> |
| 81 | // <altGlyphItem id="i1"> ... </altGlyphItem> |
| 82 | // <altGlyphItem id="i2"> ... </altGlyphItem> |
| 83 | // <glyphRef id="g1" /> |
| 84 | // <glyphRef id="g2" /> |
| 85 | // </altGlyphDef> |
| 86 | // For a), the content model is 1), so we will use "g1" and "g2" if they are all valid |
| 87 | // and "i1" is skipped. |
| 88 | // For b), the content model is 2), so we will use <glyphRef> elements contained in |
| 89 | // "i1" if they are valid and "g1" and "g2" are skipped. |
| 90 | |
| 91 | // These 2 variables are used to determine content model. |
| 92 | bool fountFirstGlyphRef = false; |
| 93 | bool foundFirstAltGlyphItem = false; |
| 94 | |
| 95 | for (auto& child : childrenOfType<SVGElement>(*this)) { |
| 96 | if (!foundFirstAltGlyphItem && is<SVGGlyphRefElement>(child)) { |
| 97 | fountFirstGlyphRef = true; |
| 98 | String referredGlyphName; |
| 99 | |
| 100 | if (downcast<SVGGlyphRefElement>(child).hasValidGlyphElement(referredGlyphName)) |
| 101 | glyphNames.append(referredGlyphName); |
| 102 | else { |
| 103 | // As the spec says "If any of the referenced glyphs are unavailable, |
| 104 | // then the character(s) that are inside of the 'altGlyph' element are |
| 105 | // rendered as if there were not an 'altGlyph' element surrounding |
| 106 | // those characters.". |
| 107 | glyphNames.clear(); |
| 108 | return false; |
| 109 | } |
| 110 | } else if (!fountFirstGlyphRef && is<SVGAltGlyphItemElement>(child)) { |
| 111 | foundFirstAltGlyphItem = true; |
| 112 | |
| 113 | // As the spec says "The first 'altGlyphItem' in which all referenced glyphs |
| 114 | // are available is chosen." |
| 115 | if (downcast<SVGAltGlyphItemElement>(child).hasValidGlyphElements(glyphNames) && !glyphNames.isEmpty()) |
| 116 | return true; |
| 117 | } |
| 118 | } |
| 119 | return !glyphNames.isEmpty(); |
| 120 | } |
| 121 | |
| 122 | } |
| 123 | |
| 124 | #endif |
| 125 | |