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 (FontFaceList::iterator it = m_fontFaces.begin(); it != m_fontFaces.end(); ++it) 52 (*it)->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 (FontFaceList::const_iterator it = m_fontFaces.begin(); it != m_fontFaces.end(); ++it) { 69 if ((*it)->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 #if ENABLE(SVG_FONTS) 154 // For SVG Fonts that specify that they only support the "normal" variant, we will assume they are incapable 155 // of small-caps synthesis and just ignore the font face. 156 if (faceFontData->isSVGFont() && desiredTraits.variant() == FontVariantSmallCaps && m_traits.variant() == FontVariantNormal) 157 continue; 158 #endif 159 appendFontData(fontData.get(), faceFontData.release(), (*it)->cssFontFace()->ranges()); 160 } 161 } 162 if (fontData->numRanges()) 163 return fontData; // No release, we have a reference to an object in the cache which should retain the ref count it has. 164 165 return nullptr; 166 } 167 168 bool CSSSegmentedFontFace::isLoading() const 169 { 170 for (FontFaceList::const_iterator it = m_fontFaces.begin(); it != m_fontFaces.end(); ++it) { 171 if ((*it)->loadStatus() == FontFace::Loading) 172 return true; 173 } 174 return false; 175 } 176 177 bool CSSSegmentedFontFace::isLoaded() const 178 { 179 for (FontFaceList::const_iterator it = m_fontFaces.begin(); it != m_fontFaces.end(); ++it) { 180 if ((*it)->loadStatus() != FontFace::Loaded) 181 return false; 182 } 183 return true; 184 } 185 186 void CSSSegmentedFontFace::willUseFontData(const FontDescription& fontDescription, UChar32 character) 187 { 188 for (FontFaceList::reverse_iterator it = m_fontFaces.rbegin(); it != m_fontFaces.rend(); ++it) { 189 if ((*it)->loadStatus() != FontFace::Unloaded) 190 break; 191 if ((*it)->cssFontFace()->maybeScheduleFontLoad(fontDescription, character)) 192 break; 193 } 194 } 195 196 bool CSSSegmentedFontFace::checkFont(const String& text) const 197 { 198 for (FontFaceList::const_iterator it = m_fontFaces.begin(); it != m_fontFaces.end(); ++it) { 199 if ((*it)->loadStatus() != FontFace::Loaded && (*it)->cssFontFace()->ranges().intersectsWith(text)) 200 return false; 201 } 202 return true; 203 } 204 205 void CSSSegmentedFontFace::match(const String& text, WillBeHeapVector<RefPtrWillBeMember<FontFace> >& faces) const 206 { 207 for (FontFaceList::const_iterator it = m_fontFaces.begin(); it != m_fontFaces.end(); ++it) { 208 if ((*it)->cssFontFace()->ranges().intersectsWith(text)) 209 faces.append(*it); 210 } 211 } 212 213 void CSSSegmentedFontFace::trace(Visitor* visitor) 214 { 215 #if ENABLE(OILPAN) 216 visitor->trace(m_fontSelector); 217 visitor->trace(m_fontFaces); 218 #endif 219 } 220 221 } 222