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