[Title] Average Char width fix for Search box of google.com when the region is Arabic.
[framework/web/webkit-efl.git] / Source / WebCore / platform / graphics / SimpleFontData.cpp
1 /*
2  * Copyright (C) 2005, 2008, 2010 Apple Inc. All rights reserved.
3  * Copyright (C) 2006 Alexey Proskuryakov
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * 1.  Redistributions of source code must retain the above copyright
10  *     notice, this list of conditions and the following disclaimer. 
11  * 2.  Redistributions in binary form must reproduce the above copyright
12  *     notice, this list of conditions and the following disclaimer in the
13  *     documentation and/or other materials provided with the distribution. 
14  * 3.  Neither the name of Apple Computer, Inc. ("Apple") nor the names of
15  *     its contributors may be used to endorse or promote products derived
16  *     from this software without specific prior written permission. 
17  *
18  * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
19  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21  * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
22  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
24  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
25  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28  */
29
30 #include "config.h"
31 #include "SimpleFontData.h"
32
33 #include "Font.h"
34 #include "FontCache.h"
35
36 #include <wtf/MathExtras.h>
37 #include <wtf/UnusedParam.h>
38
39 using namespace std;
40
41 namespace WebCore {
42
43 SimpleFontData::SimpleFontData(const FontPlatformData& platformData, bool isCustomFont, bool isLoading, bool isTextOrientationFallback)
44     : m_maxCharWidth(-1)
45     , m_avgCharWidth(-1)
46     , m_platformData(platformData)
47     , m_treatAsFixedPitch(false)
48     , m_isCustomFont(isCustomFont)
49     , m_isLoading(isLoading)
50     , m_isTextOrientationFallback(isTextOrientationFallback)
51     , m_isBrokenIdeographFallback(false)
52     , m_hasVerticalGlyphs(false)
53 {
54     platformInit();
55     platformGlyphInit();
56     platformCharWidthInit();
57 }
58
59 SimpleFontData::SimpleFontData(PassOwnPtr<AdditionalFontData> fontData, float fontSize, bool syntheticBold, bool syntheticItalic)
60     : m_platformData(FontPlatformData(fontSize, syntheticBold, syntheticItalic))
61     , m_fontData(fontData)
62     , m_treatAsFixedPitch(false)
63     , m_isCustomFont(true)
64     , m_isLoading(false)
65     , m_isTextOrientationFallback(false)
66     , m_isBrokenIdeographFallback(false)
67     , m_hasVerticalGlyphs(false)
68 {
69     m_fontData->initializeFontData(this, fontSize);
70 }
71
72 #if !(PLATFORM(QT) && !HAVE(QRAWFONT))
73 // Estimates of avgCharWidth and maxCharWidth for platforms that don't support accessing these values from the font.
74 void SimpleFontData::initCharWidths()
75 {
76     GlyphPage* glyphPageZero = GlyphPageTreeNode::getRootChild(this, 0)->page();
77
78     // Treat the width of a '0' as the avgCharWidth.
79     if (m_avgCharWidth <= 0.f && glyphPageZero) {
80         static const UChar32 digitZeroChar = '0';
81         Glyph digitZeroGlyph = glyphPageZero->glyphDataForCharacter(digitZeroChar).glyph;
82         if (digitZeroGlyph)
83             m_avgCharWidth = widthForGlyph(digitZeroGlyph);
84     }
85
86     // If we can't retrieve the width of a '0', fall back to the x height.
87     if (m_avgCharWidth <= 0.f)
88         m_avgCharWidth = m_fontMetrics.xHeight();
89
90 #if ENABLE(TIZEN_CHECK_SPACE_FOR_AVG_CHAR_WIDTH)
91     if (m_avgCharWidth <= 0.f) {
92         Glyph spaceGlyph = glyphPageZero->glyphDataForCharacter(' ').glyph;
93         m_avgCharWidth = widthForGlyph(spaceGlyph);
94     }
95 #endif
96
97     if (m_maxCharWidth <= 0.f)
98         m_maxCharWidth = max(m_avgCharWidth, m_fontMetrics.floatAscent());
99 }
100
101 void SimpleFontData::platformGlyphInit()
102 {
103     GlyphPage* glyphPageZero = GlyphPageTreeNode::getRootChild(this, 0)->page();
104     if (!glyphPageZero) {
105         LOG_ERROR("Failed to get glyph page zero.");
106         m_spaceGlyph = 0;
107         m_spaceWidth = 0;
108         m_adjustedSpaceWidth = 0;
109         determinePitch();
110         m_zeroWidthSpaceGlyph = 0;
111         m_missingGlyphData.fontData = this;
112         m_missingGlyphData.glyph = 0;
113         return;
114     }
115
116     m_zeroWidthSpaceGlyph = glyphPageZero->glyphDataForCharacter(0).glyph;
117
118     // Nasty hack to determine if we should round or ceil space widths.
119     // If the font is monospace or fake monospace we ceil to ensure that 
120     // every character and the space are the same width.  Otherwise we round.
121     m_spaceGlyph = glyphPageZero->glyphDataForCharacter(' ').glyph;
122     float width = widthForGlyph(m_spaceGlyph);
123     m_spaceWidth = width;
124     determinePitch();
125     m_adjustedSpaceWidth = m_treatAsFixedPitch ? ceilf(width) : roundf(width);
126
127     // Force the glyph for ZERO WIDTH SPACE to have zero width, unless it is shared with SPACE.
128     // Helvetica is an example of a non-zero width ZERO WIDTH SPACE glyph.
129     // See <http://bugs.webkit.org/show_bug.cgi?id=13178>
130     // Ask for the glyph for 0 to avoid paging in ZERO WIDTH SPACE. Control characters, including 0,
131     // are mapped to the ZERO WIDTH SPACE glyph.
132     if (m_zeroWidthSpaceGlyph == m_spaceGlyph) {
133         m_zeroWidthSpaceGlyph = 0;
134         LOG_ERROR("Font maps SPACE and ZERO WIDTH SPACE to the same glyph. Glyph width will not be overridden.");
135     }
136
137     m_missingGlyphData.fontData = this;
138     m_missingGlyphData.glyph = 0;
139 }
140 #endif
141
142 SimpleFontData::~SimpleFontData()
143 {
144 #if ENABLE(SVG_FONTS)
145     if (!m_fontData)
146 #endif
147         platformDestroy();
148
149     if (!isCustomFont())
150         GlyphPageTreeNode::pruneTreeFontData(this);
151 }
152
153 const SimpleFontData* SimpleFontData::fontDataForCharacter(UChar32) const
154 {
155     return this;
156 }
157
158 bool SimpleFontData::isSegmented() const
159 {
160     return false;
161 }
162
163 SimpleFontData* SimpleFontData::verticalRightOrientationFontData() const
164 {
165     if (!m_derivedFontData)
166         m_derivedFontData = DerivedFontData::create(isCustomFont());
167     if (!m_derivedFontData->verticalRightOrientation) {
168         FontPlatformData verticalRightPlatformData(m_platformData);
169         verticalRightPlatformData.setOrientation(Horizontal);
170         m_derivedFontData->verticalRightOrientation = adoptPtr(new SimpleFontData(verticalRightPlatformData, isCustomFont(), false, true));
171     }
172     return m_derivedFontData->verticalRightOrientation.get();
173 }
174
175 SimpleFontData* SimpleFontData::uprightOrientationFontData() const
176 {
177     if (!m_derivedFontData)
178         m_derivedFontData = DerivedFontData::create(isCustomFont());
179     if (!m_derivedFontData->uprightOrientation)
180         m_derivedFontData->uprightOrientation = adoptPtr(new SimpleFontData(m_platformData, isCustomFont(), false, true));
181     return m_derivedFontData->uprightOrientation.get();
182 }
183
184 SimpleFontData* SimpleFontData::brokenIdeographFontData() const
185 {
186     if (!m_derivedFontData)
187         m_derivedFontData = DerivedFontData::create(isCustomFont());
188     if (!m_derivedFontData->brokenIdeograph) {
189         m_derivedFontData->brokenIdeograph = adoptPtr(new SimpleFontData(m_platformData, isCustomFont(), false));
190         m_derivedFontData->brokenIdeograph->m_isBrokenIdeographFallback = true;
191     }
192     return m_derivedFontData->brokenIdeograph.get();
193 }
194
195 #ifndef NDEBUG
196 String SimpleFontData::description() const
197 {
198     if (isSVGFont())
199         return "[SVG font]";
200     if (isCustomFont())
201         return "[custom font]";
202
203     return platformData().description();
204 }
205 #endif
206
207 PassOwnPtr<SimpleFontData::DerivedFontData> SimpleFontData::DerivedFontData::create(bool forCustomFont)
208 {
209     return adoptPtr(new DerivedFontData(forCustomFont));
210 }
211
212 SimpleFontData::DerivedFontData::~DerivedFontData()
213 {
214     if (!forCustomFont)
215         return;
216
217     if (smallCaps)
218         GlyphPageTreeNode::pruneTreeCustomFontData(smallCaps.get());
219     if (emphasisMark)
220         GlyphPageTreeNode::pruneTreeCustomFontData(emphasisMark.get());
221     if (brokenIdeograph)
222         GlyphPageTreeNode::pruneTreeCustomFontData(brokenIdeograph.get());
223     if (verticalRightOrientation)
224         GlyphPageTreeNode::pruneTreeCustomFontData(verticalRightOrientation.get());
225     if (uprightOrientation)
226         GlyphPageTreeNode::pruneTreeCustomFontData(uprightOrientation.get());
227 #if PLATFORM(MAC)
228     if (compositeFontReferences) {
229         CFDictionaryRef dictionary = CFDictionaryRef(compositeFontReferences.get());
230         CFIndex count = CFDictionaryGetCount(dictionary);
231         if (count > 0) {
232             Vector<SimpleFontData*, 2> stash(count);
233             SimpleFontData** fonts = stash.data();
234             CFDictionaryGetKeysAndValues(dictionary, 0, (const void **)fonts);
235             while (count-- > 0 && *fonts) {
236                 OwnPtr<SimpleFontData> afont = adoptPtr(*fonts++);
237                 GlyphPageTreeNode::pruneTreeCustomFontData(afont.get());
238             }
239         }
240     }
241 #endif
242 }
243
244 } // namespace WebCore