Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / third_party / WebKit / Source / core / css / CSSFontFace.cpp
blob73fa060b927e041399ddd4e69fa9adb3e425dbdd
1 /*
2 * Copyright (C) 2007, 2008, 2011 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
6 * are met:
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 COMPUTER, 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 COMPUTER, 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.
26 #include "config.h"
27 #include "core/css/CSSFontFace.h"
29 #include "core/css/CSSFontFaceSource.h"
30 #include "core/css/CSSFontSelector.h"
31 #include "core/css/CSSSegmentedFontFace.h"
32 #include "core/css/FontFaceSet.h"
33 #include "core/css/RemoteFontFaceSource.h"
34 #include "core/frame/UseCounter.h"
35 #include "platform/fonts/FontDescription.h"
36 #include "platform/fonts/SimpleFontData.h"
38 namespace blink {
40 void CSSFontFace::addSource(PassOwnPtrWillBeRawPtr<CSSFontFaceSource> source)
42 source->setFontFace(this);
43 m_sources.append(source);
46 void CSSFontFace::setSegmentedFontFace(CSSSegmentedFontFace* segmentedFontFace)
48 ASSERT(!m_segmentedFontFace);
49 m_segmentedFontFace = segmentedFontFace;
52 void CSSFontFace::didBeginLoad()
54 if (loadStatus() == FontFace::Unloaded)
55 setLoadStatus(FontFace::Loading);
58 void CSSFontFace::fontLoaded(RemoteFontFaceSource* source)
60 if (!isValid() || source != m_sources.first())
61 return;
63 if (loadStatus() == FontFace::Loading) {
64 if (source->isValid()) {
65 setLoadStatus(FontFace::Loaded);
66 } else {
67 m_sources.removeFirst();
68 load();
72 if (m_segmentedFontFace)
73 m_segmentedFontFace->fontLoaded(this);
76 void CSSFontFace::fontLoadWaitLimitExceeded(RemoteFontFaceSource* source)
78 if (!isValid() || source != m_sources.first())
79 return;
80 if (m_segmentedFontFace)
81 m_segmentedFontFace->fontLoadWaitLimitExceeded(this);
84 PassRefPtr<SimpleFontData> CSSFontFace::getFontData(const FontDescription& fontDescription)
86 if (!isValid())
87 return nullptr;
89 while (!m_sources.isEmpty()) {
90 OwnPtrWillBeMember<CSSFontFaceSource>& source = m_sources.first();
91 if (RefPtr<SimpleFontData> result = source->getFontData(fontDescription)) {
92 if (loadStatus() == FontFace::Unloaded && (source->isLoading() || source->isLoaded()))
93 setLoadStatus(FontFace::Loading);
94 if (loadStatus() == FontFace::Loading && source->isLoaded())
95 setLoadStatus(FontFace::Loaded);
96 return result.release();
98 m_sources.removeFirst();
101 if (loadStatus() == FontFace::Unloaded)
102 setLoadStatus(FontFace::Loading);
103 if (loadStatus() == FontFace::Loading)
104 setLoadStatus(FontFace::Error);
105 return nullptr;
108 bool CSSFontFace::maybeScheduleFontLoad(const FontDescription& fontDescription, UChar32 character)
110 if (m_ranges.contains(character)) {
111 if (loadStatus() == FontFace::Unloaded)
112 load(fontDescription);
113 return true;
115 return false;
118 void CSSFontFace::load()
120 FontDescription fontDescription;
121 FontFamily fontFamily;
122 fontFamily.setFamily(m_fontFace->family());
123 fontDescription.setFamily(fontFamily);
124 fontDescription.setTraits(m_fontFace->traits());
125 load(fontDescription);
128 void CSSFontFace::load(const FontDescription& fontDescription)
130 if (loadStatus() == FontFace::Unloaded)
131 setLoadStatus(FontFace::Loading);
132 ASSERT(loadStatus() == FontFace::Loading);
134 while (!m_sources.isEmpty()) {
135 OwnPtrWillBeMember<CSSFontFaceSource>& source = m_sources.first();
136 if (source->isValid()) {
137 if (source->isLocal()) {
138 if (source->isLocalFontAvailable(fontDescription)) {
139 setLoadStatus(FontFace::Loaded);
140 return;
142 } else {
143 if (!source->isLoaded())
144 source->beginLoadIfNeeded();
145 else
146 setLoadStatus(FontFace::Loaded);
147 return;
150 m_sources.removeFirst();
152 setLoadStatus(FontFace::Error);
155 void CSSFontFace::setLoadStatus(FontFace::LoadStatus newStatus)
157 ASSERT(m_fontFace);
158 if (newStatus == FontFace::Error)
159 m_fontFace->setError();
160 else
161 m_fontFace->setLoadStatus(newStatus);
163 if (!m_segmentedFontFace)
164 return;
165 Document* document = m_segmentedFontFace->fontSelector()->document();
166 if (!document)
167 return;
169 switch (newStatus) {
170 case FontFace::Loading:
171 FontFaceSet::from(*document)->beginFontLoading(m_fontFace);
172 break;
173 case FontFace::Loaded:
174 FontFaceSet::from(*document)->fontLoaded(m_fontFace);
175 break;
176 case FontFace::Error:
177 FontFaceSet::from(*document)->loadError(m_fontFace);
178 break;
179 default:
180 break;
184 CSSFontFace::UnicodeRangeSet::UnicodeRangeSet(const Vector<UnicodeRange>& ranges)
185 : m_ranges(ranges)
187 if (m_ranges.isEmpty())
188 return;
190 std::sort(m_ranges.begin(), m_ranges.end());
192 // Unify overlapping ranges.
193 UChar32 from = m_ranges[0].from();
194 UChar32 to = m_ranges[0].to();
195 size_t targetIndex = 0;
196 for (size_t i = 1; i < m_ranges.size(); i++) {
197 if (to + 1 >= m_ranges[i].from()) {
198 to = std::max(to, m_ranges[i].to());
199 } else {
200 m_ranges[targetIndex++] = UnicodeRange(from, to);
201 from = m_ranges[i].from();
202 to = m_ranges[i].to();
205 m_ranges[targetIndex++] = UnicodeRange(from, to);
206 m_ranges.shrink(targetIndex);
209 bool CSSFontFace::UnicodeRangeSet::contains(UChar32 c) const
211 if (isEntireRange())
212 return true;
213 Vector<UnicodeRange>::const_iterator it = std::lower_bound(m_ranges.begin(), m_ranges.end(), c);
214 return it != m_ranges.end() && it->contains(c);
217 bool CSSFontFace::UnicodeRangeSet::intersectsWith(const String& text) const
219 if (text.isEmpty())
220 return false;
221 if (isEntireRange())
222 return true;
223 if (text.is8Bit() && m_ranges[0].from() >= 0x100)
224 return false;
226 unsigned index = 0;
227 while (index < text.length()) {
228 UChar32 c = text.characterStartingAt(index);
229 index += U16_LENGTH(c);
230 if (contains(c))
231 return true;
233 return false;
236 DEFINE_TRACE(CSSFontFace)
238 visitor->trace(m_segmentedFontFace);
239 visitor->trace(m_sources);
240 visitor->trace(m_fontFace);