• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2    Copyright (C) 2007 Eric Seidel <eric@webkit.org>
3    Copyright (C) 2009 Apple Inc. All rights reserved.
4 
5    This library is free software; you can redistribute it and/or
6    modify it under the terms of the GNU Library General Public
7    License as published by the Free Software Foundation; either
8    version 2 of the License, or (at your option) any later version.
9 
10    This library is distributed in the hope that it will be useful,
11    but WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13    Library General Public License for more details.
14 
15    You should have received a copy of the GNU Library General Public License
16    along with this library; see the file COPYING.LIB.  If not, write to
17    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18    Boston, MA 02110-1301, USA.
19  */
20 
21 #include "config.h"
22 
23 #if ENABLE(SVG_FONTS)
24 #include "SVGFontFaceUriElement.h"
25 
26 #include "CSSFontFaceSrcValue.h"
27 #include "CachedFont.h"
28 #include "DocLoader.h"
29 #include "Document.h"
30 #include "MappedAttribute.h"
31 #include "SVGFontFaceElement.h"
32 #include "SVGNames.h"
33 #include "XLinkNames.h"
34 
35 namespace WebCore {
36 
37 using namespace SVGNames;
38 
SVGFontFaceUriElement(const QualifiedName & tagName,Document * doc)39 SVGFontFaceUriElement::SVGFontFaceUriElement(const QualifiedName& tagName, Document* doc)
40     : SVGElement(tagName, doc)
41 {
42 }
43 
~SVGFontFaceUriElement()44 SVGFontFaceUriElement::~SVGFontFaceUriElement()
45 {
46     if (m_cachedFont)
47         m_cachedFont->removeClient(this);
48 }
49 
srcValue() const50 PassRefPtr<CSSFontFaceSrcValue> SVGFontFaceUriElement::srcValue() const
51 {
52     RefPtr<CSSFontFaceSrcValue> src = CSSFontFaceSrcValue::create(getAttribute(XLinkNames::hrefAttr));
53     AtomicString value(getAttribute(formatAttr));
54     src->setFormat(value.isEmpty() ? "svg" : value); // Default format
55     return src.release();
56 }
57 
parseMappedAttribute(MappedAttribute * attr)58 void SVGFontFaceUriElement::parseMappedAttribute(MappedAttribute* attr)
59 {
60     const QualifiedName& attrName = attr->name();
61     if (attrName == XLinkNames::hrefAttr)
62         loadFont();
63     else
64         SVGElement::parseMappedAttribute(attr);
65 }
66 
childrenChanged(bool changedByParser,Node * beforeChange,Node * afterChange,int childCountDelta)67 void SVGFontFaceUriElement::childrenChanged(bool changedByParser, Node* beforeChange, Node* afterChange, int childCountDelta)
68 {
69     SVGElement::childrenChanged(changedByParser, beforeChange, afterChange, childCountDelta);
70 
71     if (!parentNode() || !parentNode()->hasTagName(font_face_srcTag))
72         return;
73 
74     Node* grandParent = parentNode()->parentNode();
75     if (grandParent && grandParent->hasTagName(font_faceTag))
76         static_cast<SVGFontFaceElement*>(grandParent)->rebuildFontFace();
77 }
78 
insertedIntoDocument()79 void SVGFontFaceUriElement::insertedIntoDocument()
80 {
81     loadFont();
82     SVGElement::insertedIntoDocument();
83 }
84 
loadFont()85 void SVGFontFaceUriElement::loadFont()
86 {
87     if (m_cachedFont)
88         m_cachedFont->removeClient(this);
89 
90     String href = getAttribute(XLinkNames::hrefAttr);
91     if (!href.isNull()) {
92         DocLoader* docLoader = document()->docLoader();
93         m_cachedFont = docLoader->requestFont(href);
94         if (m_cachedFont) {
95             m_cachedFont->setSVGFont(true);
96             m_cachedFont->addClient(this);
97             m_cachedFont->beginLoadIfNeeded(docLoader);
98         }
99     } else
100         m_cachedFont = 0;
101 }
102 
103 }
104 
105 #endif // ENABLE(SVG_FONTS)
106