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 "Attribute.h"
27#include "CSSFontFaceSrcValue.h"
28#include "CachedFont.h"
29#include "CachedResourceLoader.h"
30#include "Document.h"
31#include "SVGFontFaceElement.h"
32#include "SVGNames.h"
33#include "XLinkNames.h"
34
35namespace WebCore {
36
37using namespace SVGNames;
38
39inline SVGFontFaceUriElement::SVGFontFaceUriElement(const QualifiedName& tagName, Document* document)
40    : SVGElement(tagName, document)
41{
42}
43
44PassRefPtr<SVGFontFaceUriElement> SVGFontFaceUriElement::create(const QualifiedName& tagName, Document* document)
45{
46    return adoptRef(new SVGFontFaceUriElement(tagName, document));
47}
48
49SVGFontFaceUriElement::~SVGFontFaceUriElement()
50{
51    if (m_cachedFont)
52        m_cachedFont->removeClient(this);
53}
54
55PassRefPtr<CSSFontFaceSrcValue> SVGFontFaceUriElement::srcValue() const
56{
57    RefPtr<CSSFontFaceSrcValue> src = CSSFontFaceSrcValue::create(getAttribute(XLinkNames::hrefAttr));
58    AtomicString value(getAttribute(formatAttr));
59    src->setFormat(value.isEmpty() ? "svg" : value); // Default format
60    return src.release();
61}
62
63void SVGFontFaceUriElement::parseMappedAttribute(Attribute* attr)
64{
65    const QualifiedName& attrName = attr->name();
66    if (attrName == XLinkNames::hrefAttr)
67        loadFont();
68    else
69        SVGElement::parseMappedAttribute(attr);
70}
71
72void SVGFontFaceUriElement::childrenChanged(bool changedByParser, Node* beforeChange, Node* afterChange, int childCountDelta)
73{
74    SVGElement::childrenChanged(changedByParser, beforeChange, afterChange, childCountDelta);
75
76    if (!parentNode() || !parentNode()->hasTagName(font_face_srcTag))
77        return;
78
79    ContainerNode* grandparent = parentNode()->parentNode();
80    if (grandparent && grandparent->hasTagName(font_faceTag))
81        static_cast<SVGFontFaceElement*>(grandparent)->rebuildFontFace();
82}
83
84void SVGFontFaceUriElement::insertedIntoDocument()
85{
86    loadFont();
87    SVGElement::insertedIntoDocument();
88}
89
90void SVGFontFaceUriElement::loadFont()
91{
92    if (m_cachedFont)
93        m_cachedFont->removeClient(this);
94
95    String href = getAttribute(XLinkNames::hrefAttr);
96    if (!href.isNull()) {
97        CachedResourceLoader* cachedResourceLoader = document()->cachedResourceLoader();
98        m_cachedFont = cachedResourceLoader->requestFont(href);
99        if (m_cachedFont) {
100            m_cachedFont->addClient(this);
101            m_cachedFont->beginLoadIfNeeded(cachedResourceLoader);
102        }
103    } else
104        m_cachedFont = 0;
105}
106
107}
108
109#endif // ENABLE(SVG_FONTS)
110