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 "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
43PassRefPtr<SVGFontFaceUriElement> SVGFontFaceUriElement::create(Document& document)
44{
45    return adoptRef(new SVGFontFaceUriElement(document));
46}
47
48SVGFontFaceUriElement::~SVGFontFaceUriElement()
49{
50    if (m_resource)
51        m_resource->removeClient(this);
52}
53
54PassRefPtr<CSSFontFaceSrcValue> SVGFontFaceUriElement::srcValue() const
55{
56    RefPtr<CSSFontFaceSrcValue> src = CSSFontFaceSrcValue::create(getAttribute(XLinkNames::hrefAttr));
57    AtomicString value(fastGetAttribute(formatAttr));
58    src->setFormat(value.isEmpty() ? "svg" : value); // Default format
59    return src.release();
60}
61
62void SVGFontFaceUriElement::parseAttribute(const QualifiedName& name, const AtomicString& value)
63{
64    if (name == XLinkNames::hrefAttr)
65        loadFont();
66    else
67        SVGElement::parseAttribute(name, value);
68}
69
70void SVGFontFaceUriElement::childrenChanged(bool changedByParser, Node* beforeChange, Node* afterChange, int childCountDelta)
71{
72    SVGElement::childrenChanged(changedByParser, beforeChange, afterChange, childCountDelta);
73
74    if (!parentNode() || !parentNode()->hasTagName(font_face_srcTag))
75        return;
76
77    ContainerNode* grandparent = parentNode()->parentNode();
78    if (grandparent && grandparent->hasTagName(font_faceTag))
79        toSVGFontFaceElement(grandparent)->rebuildFontFace();
80}
81
82Node::InsertionNotificationRequest SVGFontFaceUriElement::insertedInto(ContainerNode* rootParent)
83{
84    loadFont();
85    return SVGElement::insertedInto(rootParent);
86}
87
88void SVGFontFaceUriElement::loadFont()
89{
90    if (m_resource)
91        m_resource->removeClient(this);
92
93    const AtomicString& href = getAttribute(XLinkNames::hrefAttr);
94    if (!href.isNull()) {
95        ResourceFetcher* fetcher = document().fetcher();
96        FetchRequest request(ResourceRequest(document().completeURL(href)), localName());
97        m_resource = fetcher->fetchFont(request);
98        if (m_resource) {
99            m_resource->addClient(this);
100            m_resource->beginLoadIfNeeded(fetcher);
101        }
102    } else {
103        m_resource = 0;
104    }
105}
106
107}
108
109#endif // ENABLE(SVG_FONTS)
110