Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / core / css / CSSSegmentedFontFace.cpp
1 /*
2  * Copyright (C) 2008 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/CSSSegmentedFontFace.h"
28
29 #include "core/css/CSSFontFace.h"
30 #include "core/css/CSSFontSelector.h"
31 #include "platform/RuntimeEnabledFeatures.h"
32 #include "platform/fonts/FontCache.h"
33 #include "platform/fonts/FontDescription.h"
34 #include "platform/fonts/FontFaceCreationParams.h"
35 #include "platform/fonts/SegmentedFontData.h"
36 #include "platform/fonts/SimpleFontData.h"
37
38 namespace blink {
39
40 CSSSegmentedFontFace::CSSSegmentedFontFace(CSSFontSelector* fontSelector, FontTraits traits)
41     : m_fontSelector(fontSelector)
42     , m_traits(traits)
43     , m_firstNonCssConnectedFace(m_fontFaces.end())
44 {
45 }
46
47 CSSSegmentedFontFace::~CSSSegmentedFontFace()
48 {
49     pruneTable();
50 #if !ENABLE(OILPAN)
51     for (const auto& fontFace : m_fontFaces)
52         fontFace->cssFontFace()->clearSegmentedFontFace();
53 #endif
54 }
55
56 void CSSSegmentedFontFace::pruneTable()
57 {
58     // Make sure the glyph page tree prunes out all uses of this custom font.
59     if (m_fontDataTable.isEmpty())
60         return;
61
62     m_fontDataTable.clear();
63 }
64
65 bool CSSSegmentedFontFace::isValid() const
66 {
67     // Valid if at least one font face is valid.
68     for (const auto& fontFace : m_fontFaces) {
69         if (fontFace->cssFontFace()->isValid())
70             return true;
71     }
72     return false;
73 }
74
75 void CSSSegmentedFontFace::fontLoaded(CSSFontFace*)
76 {
77     pruneTable();
78 }
79
80 void CSSSegmentedFontFace::fontLoadWaitLimitExceeded(CSSFontFace*)
81 {
82     pruneTable();
83 }
84
85 void CSSSegmentedFontFace::addFontFace(PassRefPtrWillBeRawPtr<FontFace> prpFontFace, bool cssConnected)
86 {
87     RefPtrWillBeRawPtr<FontFace> fontFace = prpFontFace;
88     pruneTable();
89     fontFace->cssFontFace()->setSegmentedFontFace(this);
90     if (cssConnected) {
91         m_fontFaces.insertBefore(m_firstNonCssConnectedFace, fontFace);
92     } else {
93         // This is the only place in Blink that is using addReturnIterator.
94         FontFaceList::iterator iterator = m_fontFaces.addReturnIterator(fontFace);
95         if (m_firstNonCssConnectedFace == m_fontFaces.end())
96             m_firstNonCssConnectedFace = iterator;
97     }
98 }
99
100 void CSSSegmentedFontFace::removeFontFace(PassRefPtrWillBeRawPtr<FontFace> prpFontFace)
101 {
102     RefPtrWillBeRawPtr<FontFace> fontFace = prpFontFace;
103     FontFaceList::iterator it = m_fontFaces.find(fontFace);
104     if (it == m_fontFaces.end())
105         return;
106
107     if (it == m_firstNonCssConnectedFace)
108         ++m_firstNonCssConnectedFace;
109     m_fontFaces.remove(it);
110
111     pruneTable();
112     fontFace->cssFontFace()->clearSegmentedFontFace();
113 }
114
115 static void appendFontData(SegmentedFontData* newFontData, PassRefPtr<SimpleFontData> prpFaceFontData, const CSSFontFace::UnicodeRangeSet& ranges)
116 {
117     RefPtr<SimpleFontData> faceFontData = prpFaceFontData;
118     unsigned numRanges = ranges.size();
119     if (!numRanges) {
120         newFontData->appendRange(FontDataRange(0, 0x7FFFFFFF, faceFontData));
121         return;
122     }
123
124     for (unsigned j = 0; j < numRanges; ++j)
125         newFontData->appendRange(FontDataRange(ranges.rangeAt(j).from(), ranges.rangeAt(j).to(), faceFontData));
126 }
127
128 PassRefPtr<FontData> CSSSegmentedFontFace::getFontData(const FontDescription& fontDescription)
129 {
130     if (!isValid())
131         return nullptr;
132
133     FontTraits desiredTraits = fontDescription.traits();
134     FontCacheKey key = fontDescription.cacheKey(FontFaceCreationParams(), desiredTraits);
135
136     RefPtr<SegmentedFontData>& fontData = m_fontDataTable.add(key.hash(), nullptr).storedValue->value;
137     if (fontData && fontData->numRanges())
138         return fontData; // No release, we have a reference to an object in the cache which should retain the ref count it has.
139
140     if (!fontData)
141         fontData = SegmentedFontData::create();
142
143     FontDescription requestedFontDescription(fontDescription);
144     requestedFontDescription.setTraits(m_traits);
145     requestedFontDescription.setSyntheticBold(m_traits.weight() < FontWeight600 && desiredTraits.weight() >= FontWeight600);
146     requestedFontDescription.setSyntheticItalic(m_traits.style() == FontStyleNormal && desiredTraits.style() == FontStyleItalic);
147
148     for (FontFaceList::reverse_iterator it = m_fontFaces.rbegin(); it != m_fontFaces.rend(); ++it) {
149         if (!(*it)->cssFontFace()->isValid())
150             continue;
151         if (RefPtr<SimpleFontData> faceFontData = (*it)->cssFontFace()->getFontData(requestedFontDescription)) {
152             ASSERT(!faceFontData->isSegmented());
153             appendFontData(fontData.get(), faceFontData.release(), (*it)->cssFontFace()->ranges());
154         }
155     }
156     if (fontData->numRanges())
157         return fontData; // No release, we have a reference to an object in the cache which should retain the ref count it has.
158
159     return nullptr;
160 }
161
162 bool CSSSegmentedFontFace::isLoading() const
163 {
164     for (const auto& fontFace : m_fontFaces) {
165         if (fontFace->loadStatus() == FontFace::Loading)
166             return true;
167     }
168     return false;
169 }
170
171 bool CSSSegmentedFontFace::isLoaded() const
172 {
173     for (const auto& fontFace : m_fontFaces) {
174         if (fontFace->loadStatus() != FontFace::Loaded)
175             return false;
176     }
177     return true;
178 }
179
180 void CSSSegmentedFontFace::willUseFontData(const FontDescription& fontDescription, UChar32 character)
181 {
182     for (FontFaceList::reverse_iterator it = m_fontFaces.rbegin(); it != m_fontFaces.rend(); ++it) {
183         if ((*it)->loadStatus() != FontFace::Unloaded)
184             break;
185         if ((*it)->cssFontFace()->maybeScheduleFontLoad(fontDescription, character))
186             break;
187     }
188 }
189
190 bool CSSSegmentedFontFace::checkFont(const String& text) const
191 {
192     for (const auto& fontFace : m_fontFaces) {
193         if (fontFace->loadStatus() != FontFace::Loaded && fontFace->cssFontFace()->ranges().intersectsWith(text))
194             return false;
195     }
196     return true;
197 }
198
199 void CSSSegmentedFontFace::match(const String& text, WillBeHeapVector<RefPtrWillBeMember<FontFace> >& faces) const
200 {
201     for (const auto& fontFace : m_fontFaces) {
202         if (fontFace->cssFontFace()->ranges().intersectsWith(text))
203             faces.append(fontFace);
204     }
205 }
206
207 void CSSSegmentedFontFace::trace(Visitor* visitor)
208 {
209 #if ENABLE(OILPAN)
210     visitor->trace(m_fontSelector);
211     visitor->trace(m_fontFaces);
212 #endif
213 }
214
215 }