2 * Copyright (C) 2007, 2008, 2016 Apple Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 #include "FontSelectionValueInlines.h"
29 #include "FontTaggedSettings.h"
30 #include "StyleRule.h"
31 #include "TextFlags.h"
34 #include <wtf/Forward.h>
35 #include <wtf/HashSet.h>
36 #include <wtf/RefCounted.h>
37 #include <wtf/Vector.h>
38 #include <wtf/WeakPtr.h>
46 class CSSFontFaceSource;
47 class CSSFontSelector;
48 class CSSSegmentedFontFace;
52 class FontDescription;
55 enum class ExternalResourceDownloadPolicy;
57 class CSSFontFace final : public RefCounted<CSSFontFace> {
59 static Ref<CSSFontFace> create(CSSFontSelector* fontSelector, StyleRuleFontFace* cssConnection = nullptr, FontFace* wrapper = nullptr, bool isLocalFallback = false)
61 return adoptRef(*new CSSFontFace(fontSelector, cssConnection, wrapper, isLocalFallback));
63 virtual ~CSSFontFace();
65 // FIXME: These functions don't need to have boolean return values.
66 // Callers only call this with known-valid CSS values.
67 bool setFamilies(CSSValue&);
68 void setStyle(CSSValue&);
69 void setWeight(CSSValue&);
70 void setStretch(CSSValue&);
71 bool setUnicodeRange(CSSValue&);
72 bool setVariantLigatures(CSSValue&);
73 bool setVariantPosition(CSSValue&);
74 bool setVariantCaps(CSSValue&);
75 bool setVariantNumeric(CSSValue&);
76 bool setVariantAlternates(CSSValue&);
77 bool setVariantEastAsian(CSSValue&);
78 void setFeatureSettings(CSSValue&);
79 void setLoadingBehavior(CSSValue&);
83 const CSSValueList* families() const { return m_families.get(); }
84 FontSelectionRange weight() const { return m_fontSelectionCapabilities.computeWeight(); }
85 FontSelectionRange stretch() const { return m_fontSelectionCapabilities.computeWidth(); }
86 FontSelectionRange italic() const { return m_fontSelectionCapabilities.computeSlope(); }
87 FontSelectionCapabilities fontSelectionCapabilities() const { return m_fontSelectionCapabilities.computeFontSelectionCapabilities(); }
88 const Vector<UnicodeRange>& ranges() const { return m_ranges; }
89 const FontFeatureSettings& featureSettings() const { return m_featureSettings; }
90 const FontVariantSettings& variantSettings() const { return m_variantSettings; }
91 void setVariantSettings(const FontVariantSettings& variantSettings) { m_variantSettings = variantSettings; }
92 void setWeight(FontSelectionRange weight) { m_fontSelectionCapabilities.weight = weight; }
93 void setStretch(FontSelectionRange stretch) { m_fontSelectionCapabilities.width = stretch; }
94 void setStyle(FontSelectionRange italic) { m_fontSelectionCapabilities.slope = italic; }
95 void setFontSelectionCapabilities(FontSelectionCapabilities capabilities) { m_fontSelectionCapabilities = capabilities; }
96 bool isLocalFallback() const { return m_isLocalFallback; }
97 Status status() const { return m_status; }
98 StyleRuleFontFace* cssConnection() const { return m_cssConnection.get(); }
101 void addClient(Client&);
102 void removeClient(Client&);
104 bool allSourcesFailed() const;
106 void adoptSource(std::unique_ptr<CSSFontFaceSource>&&);
107 void sourcesPopulated() { m_sourcesPopulated = true; }
109 void fontLoaded(CSSFontFaceSource&);
113 RefPtr<Font> font(const FontDescription&, bool syntheticBold, bool syntheticItalic, ExternalResourceDownloadPolicy);
115 static void appendSources(CSSFontFace&, CSSValueList&, Document*, bool isInitiatingElementInUserAgentShadowTree);
119 virtual ~Client() { }
120 virtual void fontLoaded(CSSFontFace&) { }
121 virtual void fontStateChanged(CSSFontFace&, Status /*oldState*/, Status /*newState*/) { }
122 virtual void fontPropertyChanged(CSSFontFace&, CSSValueList* /*oldFamilies*/ = nullptr) { }
123 virtual void ref() = 0;
124 virtual void deref() = 0;
127 // Pending => Loading => TimedOut
136 enum class Status { Pending, Loading, TimedOut, Success, Failure };
138 struct UnicodeRange {
141 bool operator==(const UnicodeRange& other) const { return from == other.from && to == other.to; }
142 bool operator!=(const UnicodeRange& other) const { return !(*this == other); }
145 bool rangesMatchCodePoint(UChar32) const;
147 // We don't guarantee that the FontFace wrapper will be the same every time you ask for it.
148 Ref<FontFace> wrapper();
149 void setWrapper(FontFace&);
150 FontFace* existingWrapper() { return m_wrapper.get(); }
152 bool webFontsShouldAlwaysFallBack() const;
154 bool purgeable() const;
156 void updateStyleIfNeeded();
158 #if ENABLE(SVG_FONTS)
159 bool hasSVGFontFaceSource() const;
163 CSSFontFace(CSSFontSelector*, StyleRuleFontFace*, FontFace*, bool isLocalFallback);
165 size_t pump(ExternalResourceDownloadPolicy);
166 void setStatus(Status);
167 void notifyClientsOfFontPropertyChange();
169 void initializeWrapper();
171 void fontLoadEventOccurred();
174 RefPtr<CSSValueList> m_families;
175 Vector<UnicodeRange> m_ranges;
176 FontFeatureSettings m_featureSettings;
177 FontVariantSettings m_variantSettings;
178 Timer m_timeoutTimer;
179 Vector<std::unique_ptr<CSSFontFaceSource>> m_sources;
180 RefPtr<CSSFontSelector> m_fontSelector;
181 RefPtr<StyleRuleFontFace> m_cssConnection;
182 HashSet<Client*> m_clients;
183 WeakPtr<FontFace> m_wrapper;
184 FontSelectionSpecifiedCapabilities m_fontSelectionCapabilities;
185 FontLoadingBehavior m_loadingBehavior { FontLoadingBehavior::Auto };
186 Status m_status { Status::Pending };
187 bool m_isLocalFallback { false };
188 bool m_sourcesPopulated { false };
189 bool m_mayBePurged { true };