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 "SVGNames.h" 27#include "XLinkNames.h" 28#include "core/css/CSSFontFaceSrcValue.h" 29#include "core/dom/Document.h" 30#include "core/loader/cache/FetchRequest.h" 31#include "core/loader/cache/FontResource.h" 32#include "core/loader/cache/ResourceFetcher.h" 33#include "core/svg/SVGFontFaceElement.h" 34 35namespace WebCore { 36 37using namespace SVGNames; 38 39inline SVGFontFaceUriElement::SVGFontFaceUriElement(const QualifiedName& tagName, Document* document) 40 : SVGElement(tagName, document) 41{ 42 ASSERT(hasTagName(font_face_uriTag)); 43 ScriptWrappable::init(this); 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_resource) 54 m_resource->removeClient(this); 55} 56 57PassRefPtr<CSSFontFaceSrcValue> SVGFontFaceUriElement::srcValue() const 58{ 59 RefPtr<CSSFontFaceSrcValue> src = CSSFontFaceSrcValue::create(getAttribute(XLinkNames::hrefAttr)); 60 AtomicString value(fastGetAttribute(formatAttr)); 61 src->setFormat(value.isEmpty() ? "svg" : value); // Default format 62 return src.release(); 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(bool changedByParser, Node* beforeChange, Node* afterChange, int childCountDelta) 74{ 75 SVGElement::childrenChanged(changedByParser, beforeChange, afterChange, childCountDelta); 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_resource) 94 m_resource->removeClient(this); 95 96 const AtomicString& href = getAttribute(XLinkNames::hrefAttr); 97 if (!href.isNull()) { 98 ResourceFetcher* fetcher = document()->fetcher(); 99 FetchRequest request(ResourceRequest(document()->completeURL(href)), localName()); 100 m_resource = fetcher->requestFont(request); 101 if (m_resource) { 102 m_resource->addClient(this); 103 m_resource->beginLoadIfNeeded(fetcher); 104 } 105 } else { 106 m_resource = 0; 107 } 108} 109 110} 111 112#endif // ENABLE(SVG_FONTS) 113