• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 1999 Lars Knoll (knoll@kde.org)
3  *           (C) 1999 Antti Koivisto (koivisto@kde.org)
4  *           (C) 2000 Dirk Mueller (mueller@kde.org)
5  * Copyright (C) 2003, 2006 Apple Inc. All rights reserved.
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Library General Public
9  * License as published by the Free Software Foundation; either
10  * version 2 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Library General Public License for more details.
16  *
17  * You should have received a copy of the GNU Library General Public License
18  * along with this library; see the file COPYING.LIB.  If not, write to
19  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
20  * Boston, MA 02110-1301, USA.
21  *
22  */
23 
24 #include "config.h"
25 #include "Font.h"
26 
27 #include "CharacterNames.h"
28 #include "FloatRect.h"
29 #include "FontCache.h"
30 #include "FontFallbackList.h"
31 #include "IntPoint.h"
32 #include "GlyphBuffer.h"
33 #include "WidthIterator.h"
34 #include <wtf/MathExtras.h>
35 #include <wtf/UnusedParam.h>
36 
37 using namespace WTF;
38 using namespace Unicode;
39 
40 namespace WebCore {
41 
42 #if USE(FONT_FAST_PATH)
43 const uint8_t Font::gRoundingHackCharacterTable[256] = {
44     0, 0, 0, 0, 0, 0, 0, 0, 0, 1 /*\t*/, 1 /*\n*/, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
45     1 /*space*/, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 /*-*/, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 /*?*/,
46     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
47     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
48     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
49     1 /*no-break space*/, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
50     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
51     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
52 };
53 
54 Font::CodePath Font::s_codePath = Auto;
55 #endif
56 
57 // ============================================================================================
58 // Font Implementation (Cross-Platform Portion)
59 // ============================================================================================
60 
Font()61 Font::Font()
62     : m_letterSpacing(0)
63     , m_wordSpacing(0)
64     , m_isPlatformFont(false)
65 {
66 }
67 
Font(const FontDescription & fd,short letterSpacing,short wordSpacing)68 Font::Font(const FontDescription& fd, short letterSpacing, short wordSpacing)
69     : m_fontDescription(fd)
70     , m_letterSpacing(letterSpacing)
71     , m_wordSpacing(wordSpacing)
72     , m_isPlatformFont(false)
73 {
74 }
75 
Font(const FontPlatformData & fontData,bool isPrinterFont)76 Font::Font(const FontPlatformData& fontData, bool isPrinterFont)
77     : m_fontList(FontFallbackList::create())
78     , m_letterSpacing(0)
79     , m_wordSpacing(0)
80     , m_isPlatformFont(true)
81 {
82     m_fontDescription.setUsePrinterFont(isPrinterFont);
83     m_fontList->setPlatformFont(fontData);
84 }
85 
Font(const Font & other)86 Font::Font(const Font& other)
87     : m_fontDescription(other.m_fontDescription)
88     , m_fontList(other.m_fontList)
89     , m_letterSpacing(other.m_letterSpacing)
90     , m_wordSpacing(other.m_wordSpacing)
91     , m_isPlatformFont(other.m_isPlatformFont)
92 {
93 }
94 
operator =(const Font & other)95 Font& Font::operator=(const Font& other)
96 {
97     m_fontDescription = other.m_fontDescription;
98     m_fontList = other.m_fontList;
99     m_letterSpacing = other.m_letterSpacing;
100     m_wordSpacing = other.m_wordSpacing;
101     m_isPlatformFont = other.m_isPlatformFont;
102     return *this;
103 }
104 
~Font()105 Font::~Font()
106 {
107 }
108 
operator ==(const Font & other) const109 bool Font::operator==(const Font& other) const
110 {
111     // Our FontData don't have to be checked, since checking the font description will be fine.
112     // FIXME: This does not work if the font was made with the FontPlatformData constructor.
113     if ((m_fontList && m_fontList->loadingCustomFonts()) ||
114         (other.m_fontList && other.m_fontList->loadingCustomFonts()))
115         return false;
116 
117     FontSelector* first = m_fontList ? m_fontList->fontSelector() : 0;
118     FontSelector* second = other.m_fontList ? other.m_fontList->fontSelector() : 0;
119 
120     return first == second
121            && m_fontDescription == other.m_fontDescription
122            && m_letterSpacing == other.m_letterSpacing
123            && m_wordSpacing == other.m_wordSpacing
124            && (m_fontList ? m_fontList->generation() : 0) == (other.m_fontList ? other.m_fontList->generation() : 0);
125 }
126 
primaryFont() const127 const SimpleFontData* Font::primaryFont() const
128 {
129     ASSERT(m_fontList);
130     return m_fontList->primarySimpleFontData(this);
131 }
132 
fontDataAt(unsigned index) const133 const FontData* Font::fontDataAt(unsigned index) const
134 {
135     ASSERT(m_fontList);
136     return m_fontList->fontDataAt(this, index);
137 }
138 
fontDataForCharacters(const UChar * characters,int length) const139 const FontData* Font::fontDataForCharacters(const UChar* characters, int length) const
140 {
141     ASSERT(m_fontList);
142     return m_fontList->fontDataForCharacters(this, characters, length);
143 }
144 
update(PassRefPtr<FontSelector> fontSelector) const145 void Font::update(PassRefPtr<FontSelector> fontSelector) const
146 {
147     // FIXME: It is pretty crazy that we are willing to just poke into a RefPtr, but it ends up
148     // being reasonably safe (because inherited fonts in the render tree pick up the new
149     // style anyway. Other copies are transient, e.g., the state in the GraphicsContext, and
150     // won't stick around long enough to get you in trouble). Still, this is pretty disgusting,
151     // and could eventually be rectified by using RefPtrs for Fonts themselves.
152     if (!m_fontList)
153         m_fontList = FontFallbackList::create();
154     m_fontList->invalidate(fontSelector);
155 }
156 
isFixedPitch() const157 bool Font::isFixedPitch() const
158 {
159     ASSERT(m_fontList);
160     return m_fontList->isFixedPitch(this);
161 }
162 
drawText(GraphicsContext * context,const TextRun & run,const FloatPoint & point,int from,int to) const163 void Font::drawText(GraphicsContext* context, const TextRun& run, const FloatPoint& point, int from, int to) const
164 {
165     // Don't draw anything while we are using custom fonts that are in the process of loading.
166     if (m_fontList && m_fontList->loadingCustomFonts())
167         return;
168 
169     to = (to == -1 ? run.length() : to);
170 
171 #if ENABLE(SVG_FONTS)
172     if (primaryFont()->isSVGFont()) {
173         drawTextUsingSVGFont(context, run, point, from, to);
174         return;
175     }
176 #endif
177 
178 #if USE(FONT_FAST_PATH)
179     if (canUseGlyphCache(run))
180         return drawSimpleText(context, run, point, from, to);
181 #endif
182 
183     return drawComplexText(context, run, point, from, to);
184 }
185 
floatWidth(const TextRun & run,HashSet<const SimpleFontData * > * fallbackFonts) const186 float Font::floatWidth(const TextRun& run, HashSet<const SimpleFontData*>* fallbackFonts) const
187 {
188 #if ENABLE(SVG_FONTS)
189     if (primaryFont()->isSVGFont())
190         return floatWidthUsingSVGFont(run);
191 #endif
192 
193 #if USE(FONT_FAST_PATH)
194     if (canUseGlyphCache(run)) {
195         // If the complex text implementation cannot return fallback fonts, avoid
196         // returning them for simple text as well.
197         static bool returnFallbackFonts = canReturnFallbackFontsForComplexText();
198         return floatWidthForSimpleText(run, 0, returnFallbackFonts ? fallbackFonts : 0);
199     }
200 #endif
201 
202     return floatWidthForComplexText(run, fallbackFonts);
203 }
204 
floatWidth(const TextRun & run,int extraCharsAvailable,int & charsConsumed,String & glyphName) const205 float Font::floatWidth(const TextRun& run, int extraCharsAvailable, int& charsConsumed, String& glyphName) const
206 {
207 #if !ENABLE(SVG_FONTS)
208     UNUSED_PARAM(extraCharsAvailable);
209 #else
210     if (primaryFont()->isSVGFont())
211         return floatWidthUsingSVGFont(run, extraCharsAvailable, charsConsumed, glyphName);
212 #endif
213 
214     charsConsumed = run.length();
215     glyphName = "";
216 
217 #if USE(FONT_FAST_PATH)
218     if (canUseGlyphCache(run))
219         return floatWidthForSimpleText(run, 0);
220 #endif
221 
222     return floatWidthForComplexText(run);
223 }
224 
selectionRectForText(const TextRun & run,const IntPoint & point,int h,int from,int to) const225 FloatRect Font::selectionRectForText(const TextRun& run, const IntPoint& point, int h, int from, int to) const
226 {
227 #if ENABLE(SVG_FONTS)
228     if (primaryFont()->isSVGFont())
229         return selectionRectForTextUsingSVGFont(run, point, h, from, to);
230 #endif
231 
232     to = (to == -1 ? run.length() : to);
233 
234 #if USE(FONT_FAST_PATH)
235     if (canUseGlyphCache(run))
236         return selectionRectForSimpleText(run, point, h, from, to);
237 #endif
238 
239     return selectionRectForComplexText(run, point, h, from, to);
240 }
241 
offsetForPosition(const TextRun & run,int x,bool includePartialGlyphs) const242 int Font::offsetForPosition(const TextRun& run, int x, bool includePartialGlyphs) const
243 {
244 #if ENABLE(SVG_FONTS)
245     if (primaryFont()->isSVGFont())
246         return offsetForPositionForTextUsingSVGFont(run, x, includePartialGlyphs);
247 #endif
248 
249 #if USE(FONT_FAST_PATH)
250     if (canUseGlyphCache(run))
251         return offsetForPositionForSimpleText(run, x, includePartialGlyphs);
252 #endif
253 
254     return offsetForPositionForComplexText(run, x, includePartialGlyphs);
255 }
256 
257 #if ENABLE(SVG_FONTS)
isSVGFont() const258 bool Font::isSVGFont() const
259 {
260     return primaryFont()->isSVGFont();
261 }
262 #endif
263 
fontSelector() const264 FontSelector* Font::fontSelector() const
265 {
266     return m_fontList ? m_fontList->fontSelector() : 0;
267 }
268 
269 static bool shouldUseFontSmoothing = true;
270 
setShouldUseSmoothing(bool shouldUseSmoothing)271 void Font::setShouldUseSmoothing(bool shouldUseSmoothing)
272 {
273     ASSERT(isMainThread());
274     shouldUseFontSmoothing = shouldUseSmoothing;
275 }
276 
shouldUseSmoothing()277 bool Font::shouldUseSmoothing()
278 {
279     return shouldUseFontSmoothing;
280 }
281 
282 }
283