Upstream version 6.35.121.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / core / svg / SVGRemoteFontFaceSource.cpp
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 "SVGNames.h"
10 #include "core/dom/ElementTraversal.h"
11 #include "core/svg/SVGFontData.h"
12 #include "core/svg/SVGFontElement.h"
13 #include "core/svg/SVGFontFaceElement.h"
14 #include "platform/fonts/FontDescription.h"
15 #include "platform/fonts/SimpleFontData.h"
16
17 namespace WebCore {
18
19 SVGRemoteFontFaceSource::SVGRemoteFontFaceSource(const String& uri, FontResource* font)
20     : RemoteFontFaceSource(font)
21     , m_uri(uri)
22 {
23 }
24
25 SVGRemoteFontFaceSource::~SVGRemoteFontFaceSource()
26 {
27 }
28
29 bool SVGRemoteFontFaceSource::ensureFontData()
30 {
31     return resource()->ensureSVGFontData();
32 }
33
34 PassRefPtr<SimpleFontData> SVGRemoteFontFaceSource::createFontData(const FontDescription& fontDescription)
35 {
36     if (!isLoaded())
37         return createLoadingFallbackFontData(fontDescription);
38
39     // Parse the external SVG document, and extract the <font> element.
40     if (!resource()->ensureSVGFontData())
41         return nullptr;
42
43     if (!m_externalSVGFontElement) {
44         String fragmentIdentifier;
45         size_t start = m_uri.find('#');
46         if (start != kNotFound)
47             fragmentIdentifier = m_uri.substring(start + 1);
48         m_externalSVGFontElement = resource()->getSVGFontById(fragmentIdentifier);
49     }
50
51     if (!m_externalSVGFontElement)
52         return nullptr;
53
54     // Select first <font-face> child
55     if (SVGFontFaceElement* fontFaceElement = Traversal<SVGFontFaceElement>::firstChild(*m_externalSVGFontElement)) {
56         return SimpleFontData::create(
57             SVGFontData::create(fontFaceElement),
58             fontDescription.effectiveFontSize(),
59             fontDescription.isSyntheticBold(),
60             fontDescription.isSyntheticItalic());
61     }
62     return nullptr;
63 }
64
65 } // namespace WebCore
66
67 #endif // ENABLE(SVG_FONTS)