Upstream version 11.40.277.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / core / css / CSSFontFace.cpp
1 /*
2  * Copyright (C) 2007, 2008, 2011 Apple Inc. All rights reserved.
3  *
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.
12  *
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.
24  */
25
26 #include "config.h"
27 #include "core/css/CSSFontFace.h"
28
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"
37
38 namespace blink {
39
40 void CSSFontFace::addSource(PassOwnPtrWillBeRawPtr<CSSFontFaceSource> source)
41 {
42     source->setFontFace(this);
43     m_sources.append(source);
44 }
45
46 void CSSFontFace::setSegmentedFontFace(CSSSegmentedFontFace* segmentedFontFace)
47 {
48     ASSERT(!m_segmentedFontFace);
49     m_segmentedFontFace = segmentedFontFace;
50 }
51
52 void CSSFontFace::didBeginLoad()
53 {
54     if (loadStatus() == FontFace::Unloaded)
55         setLoadStatus(FontFace::Loading);
56 }
57
58 void CSSFontFace::fontLoaded(RemoteFontFaceSource* source)
59 {
60     if (!isValid() || source != m_sources.first())
61         return;
62
63     if (loadStatus() == FontFace::Loading) {
64         if (source->ensureFontData()) {
65             setLoadStatus(FontFace::Loaded);
66         } else {
67             m_sources.removeFirst();
68             load();
69         }
70     }
71
72     if (m_segmentedFontFace)
73         m_segmentedFontFace->fontLoaded(this);
74 }
75
76 void CSSFontFace::fontLoadWaitLimitExceeded(RemoteFontFaceSource* source)
77 {
78     if (!isValid() || source != m_sources.first())
79         return;
80     if (m_segmentedFontFace)
81         m_segmentedFontFace->fontLoadWaitLimitExceeded(this);
82 }
83
84 PassRefPtr<SimpleFontData> CSSFontFace::getFontData(const FontDescription& fontDescription)
85 {
86     if (!isValid())
87         return nullptr;
88
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();
97         }
98         m_sources.removeFirst();
99     }
100
101     if (loadStatus() == FontFace::Unloaded)
102         setLoadStatus(FontFace::Loading);
103     if (loadStatus() == FontFace::Loading)
104         setLoadStatus(FontFace::Error);
105     return nullptr;
106 }
107
108 bool CSSFontFace::maybeScheduleFontLoad(const FontDescription& fontDescription, UChar32 character)
109 {
110     if (m_ranges.contains(character)) {
111         if (loadStatus() == FontFace::Unloaded)
112             load(fontDescription);
113         return true;
114     }
115     return false;
116 }
117
118 void CSSFontFace::load()
119 {
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);
126 }
127
128 void CSSFontFace::load(const FontDescription& fontDescription)
129 {
130     if (loadStatus() == FontFace::Unloaded)
131         setLoadStatus(FontFace::Loading);
132     ASSERT(loadStatus() == FontFace::Loading);
133
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;
141                 }
142             } else {
143                 if (!source->isLoaded())
144                     source->beginLoadIfNeeded();
145                 else
146                     setLoadStatus(FontFace::Loaded);
147                 return;
148             }
149         }
150         m_sources.removeFirst();
151     }
152     setLoadStatus(FontFace::Error);
153 }
154
155 void CSSFontFace::setLoadStatus(FontFace::LoadStatus newStatus)
156 {
157     ASSERT(m_fontFace);
158     if (newStatus == FontFace::Error)
159         m_fontFace->setError();
160     else
161         m_fontFace->setLoadStatus(newStatus);
162
163     if (!m_segmentedFontFace)
164         return;
165     Document* document = m_segmentedFontFace->fontSelector()->document();
166     if (!document)
167         return;
168
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;
181     }
182 }
183
184 CSSFontFace::UnicodeRangeSet::UnicodeRangeSet(const Vector<UnicodeRange>& ranges)
185     : m_ranges(ranges)
186 {
187     if (m_ranges.isEmpty())
188         return;
189
190     std::sort(m_ranges.begin(), m_ranges.end());
191
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();
203         }
204     }
205     m_ranges[targetIndex++] = UnicodeRange(from, to);
206     m_ranges.shrink(targetIndex);
207 }
208
209 bool CSSFontFace::UnicodeRangeSet::contains(UChar32 c) const
210 {
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);
215 }
216
217 bool CSSFontFace::UnicodeRangeSet::intersectsWith(const String& text) const
218 {
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;
225
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;
232     }
233     return false;
234 }
235
236 void CSSFontFace::trace(Visitor* visitor)
237 {
238     visitor->trace(m_segmentedFontFace);
239     visitor->trace(m_sources);
240     visitor->trace(m_fontFace);
241 }
242
243 }