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 "core/svg/SVGFontFaceUriElement.h"
25
26#include "core/XLinkNames.h"
27#include "core/css/CSSFontFaceSrcValue.h"
28#include "core/dom/Document.h"
29#include "core/fetch/FetchRequest.h"
30#include "core/fetch/ResourceFetcher.h"
31#include "core/svg/SVGFontFaceElement.h"
32
33namespace WebCore {
34
35using namespace SVGNames;
36
37inline SVGFontFaceUriElement::SVGFontFaceUriElement(Document& document)
38    : SVGElement(font_face_uriTag, document)
39{
40    ScriptWrappable::init(this);
41}
42
43DEFINE_NODE_FACTORY(SVGFontFaceUriElement)
44
45SVGFontFaceUriElement::~SVGFontFaceUriElement()
46{
47    if (m_resource)
48        m_resource->removeClient(this);
49}
50
51PassRefPtrWillBeRawPtr<CSSFontFaceSrcValue> SVGFontFaceUriElement::srcValue() const
52{
53    RefPtrWillBeRawPtr<CSSFontFaceSrcValue> src = CSSFontFaceSrcValue::create(getAttribute(XLinkNames::hrefAttr));
54    AtomicString value(fastGetAttribute(formatAttr));
55    src->setFormat(value.isEmpty() ? "svg" : value); // Default format
56    return src.release();
57}
58
59void SVGFontFaceUriElement::parseAttribute(const QualifiedName& name, const AtomicString& value)
60{
61    if (name.matches(XLinkNames::hrefAttr))
62        loadFont();
63    else
64        SVGElement::parseAttribute(name, value);
65}
66
67void SVGFontFaceUriElement::childrenChanged(bool changedByParser, Node* beforeChange, Node* afterChange, int childCountDelta)
68{
69    SVGElement::childrenChanged(changedByParser, beforeChange, afterChange, childCountDelta);
70
71    if (!isSVGFontFaceSrcElement(parentNode()))
72        return;
73
74    ContainerNode* grandparent = parentNode()->parentNode();
75    if (isSVGFontFaceElement(grandparent))
76        toSVGFontFaceElement(*grandparent).rebuildFontFace();
77}
78
79Node::InsertionNotificationRequest SVGFontFaceUriElement::insertedInto(ContainerNode* rootParent)
80{
81    loadFont();
82    return SVGElement::insertedInto(rootParent);
83}
84
85void SVGFontFaceUriElement::loadFont()
86{
87    if (m_resource)
88        m_resource->removeClient(this);
89
90    const AtomicString& href = getAttribute(XLinkNames::hrefAttr);
91    if (!href.isNull()) {
92        ResourceFetcher* fetcher = document().fetcher();
93        FetchRequest request(ResourceRequest(document().completeURL(href)), localName());
94        m_resource = fetcher->fetchFont(request);
95        if (m_resource) {
96            m_resource->addClient(this);
97            m_resource->beginLoadIfNeeded(fetcher);
98        }
99    } else {
100        m_resource = 0;
101    }
102}
103
104}
105
106#endif // ENABLE(SVG_FONTS)
107