1 // Copyright 2014 The Chromium Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 #include "config.h" 6 #if ENABLE(SVG_FONTS) 7 #include "core/svg/SVGRemoteFontFaceSource.h" 8 9 #include "core/SVGNames.h" 10 #include "core/css/FontLoader.h" 11 #include "core/dom/ElementTraversal.h" 12 #include "core/svg/SVGFontData.h" 13 #include "core/svg/SVGFontElement.h" 14 #include "core/svg/SVGFontFaceElement.h" 15 #include "platform/fonts/FontDescription.h" 16 #include "platform/fonts/SimpleFontData.h" 17 18 namespace WebCore { 19 20 SVGRemoteFontFaceSource::SVGRemoteFontFaceSource(const String& uri, FontResource* font, PassRefPtrWillBeRawPtr<FontLoader> fontLoader) 21 : RemoteFontFaceSource(font, fontLoader) 22 , m_uri(uri) 23 { 24 } 25 26 SVGRemoteFontFaceSource::~SVGRemoteFontFaceSource() 27 { 28 } 29 30 bool SVGRemoteFontFaceSource::ensureFontData() 31 { 32 return resource()->ensureSVGFontData(); 33 } 34 35 PassRefPtr<SimpleFontData> SVGRemoteFontFaceSource::createFontData(const FontDescription& fontDescription) 36 { 37 if (!isLoaded()) 38 return createLoadingFallbackFontData(fontDescription); 39 40 // Parse the external SVG document, and extract the <font> element. 41 if (!resource()->ensureSVGFontData()) 42 return nullptr; 43 44 if (!m_externalSVGFontElement) { 45 String fragmentIdentifier; 46 size_t start = m_uri.find('#'); 47 if (start != kNotFound) 48 fragmentIdentifier = m_uri.substring(start + 1); 49 m_externalSVGFontElement = resource()->getSVGFontById(fragmentIdentifier); 50 } 51 52 if (!m_externalSVGFontElement) 53 return nullptr; 54 55 // Select first <font-face> child 56 if (SVGFontFaceElement* fontFaceElement = Traversal<SVGFontFaceElement>::firstChild(*m_externalSVGFontElement)) { 57 return SimpleFontData::create( 58 SVGFontData::create(fontFaceElement), 59 fontDescription.effectiveFontSize(), 60 fontDescription.isSyntheticBold(), 61 fontDescription.isSyntheticItalic()); 62 } 63 return nullptr; 64 } 65 66 void SVGRemoteFontFaceSource::trace(Visitor* visitor) 67 { 68 visitor->trace(m_externalSVGFontElement); 69 RemoteFontFaceSource::trace(visitor); 70 } 71 72 } // namespace WebCore 73 74 #endif // ENABLE(SVG_FONTS) 75