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