initial import
[vuplus_webkit] / Source / WebCore / platform / graphics / qt / SimpleFontDataQt.cpp
1 /*
2     Copyright (C) 2008, 2009, 2010, 2011 Nokia Corporation and/or its subsidiary(-ies)
3     Copyright (C) 2008 Holger Hans Peter Freyther
4
5     This library is free software; you can redistribute it and/or
6     modify it under the terms of the GNU Library General Public
7     License as published by the Free Software Foundation; either
8     version 2 of the License, or (at your option) any later version.
9
10     This library is distributed in the hope that it will be useful,
11     but WITHOUT ANY WARRANTY; without even the implied warranty of
12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13     Library General Public License for more details.
14
15     You should have received a copy of the GNU Library General Public License
16     along with this library; see the file COPYING.LIB.  If not, write to
17     the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18     Boston, MA 02110-1301, USA.
19
20     This class provides all functionality needed for loading images, style sheets and html
21     pages from the web. It has a memory cache for these objects.
22 */
23
24 #include "config.h"
25 #include "SimpleFontData.h"
26
27 #if HAVE(QRAWFONT)
28 #include "NotImplemented.h"
29 #else
30 #include <QFontMetricsF>
31 #endif
32
33 namespace WebCore {
34
35 void SimpleFontData::determinePitch()
36 {
37 #if HAVE(QRAWFONT)
38     notImplemented();
39     m_treatAsFixedPitch = false;
40 #else
41     m_treatAsFixedPitch = m_platformData.font().fixedPitch();
42 #endif
43 }
44
45 #if HAVE(QRAWFONT)
46
47 static const float smallCapsFraction = 0.7;
48 static const float emphasisMarkFraction = 0.5;
49
50 bool SimpleFontData::containsCharacters(const UChar* characters, int length) const
51 {
52     QRawFont rawFont(m_platformData.rawFont());
53
54     for (int i = 0; i < length; ++i) {
55         if (!rawFont.supportsCharacter(static_cast<QChar>(characters[i])))
56             return false;
57     }
58
59     return true;
60 }
61
62 float SimpleFontData::platformWidthForGlyph(Glyph glyph) const
63 {
64     if (!platformData().size())
65         return 0;
66
67     QVector<quint32> glyphIndexes;
68     glyphIndexes.append(glyph);
69     QVector<QPointF> advances = platformData().rawFont().advancesForGlyphIndexes(glyphIndexes);
70     ASSERT(!advances.isEmpty());
71     return advances.at(0).x();
72 }
73
74 PassOwnPtr<SimpleFontData> SimpleFontData::createScaledFontData(const FontDescription& fontDescription, float scaleFactor) const
75 {
76     const float scaledSize = lroundf(fontDescription.computedSize() * scaleFactor);
77     return adoptPtr(new SimpleFontData(FontPlatformData(m_platformData, scaledSize), isCustomFont(), false));
78 }
79
80 SimpleFontData* SimpleFontData::smallCapsFontData(const FontDescription& fontDescription) const
81 {
82     if (!m_derivedFontData)
83         m_derivedFontData = DerivedFontData::create(isCustomFont());
84     if (!m_derivedFontData->smallCaps)
85         m_derivedFontData->smallCaps = createScaledFontData(fontDescription, smallCapsFraction);
86
87     return m_derivedFontData->smallCaps.get();
88 }
89
90 SimpleFontData* SimpleFontData::emphasisMarkFontData(const FontDescription& fontDescription) const
91 {
92     if (!m_derivedFontData)
93         m_derivedFontData = DerivedFontData::create(isCustomFont());
94     if (!m_derivedFontData->emphasisMark)
95         m_derivedFontData->emphasisMark = createScaledFontData(fontDescription, emphasisMarkFraction);
96
97     return m_derivedFontData->emphasisMark.get();
98 }
99
100 FloatRect SimpleFontData::platformBoundsForGlyph(Glyph) const
101 {
102     notImplemented();
103     return FloatRect();
104 }
105 #else
106 bool SimpleFontData::containsCharacters(const UChar*, int) const
107 {
108     return true;
109 }
110
111 void SimpleFontData::platformGlyphInit()
112 {
113     if (!m_platformData.size())
114         return;
115     m_spaceGlyph = 0;
116     m_adjustedSpaceWidth = m_spaceWidth;
117     determinePitch();
118     m_missingGlyphData.fontData = this;
119     m_missingGlyphData.glyph = 0;
120 }
121 #endif
122
123 void SimpleFontData::platformInit()
124 {
125     if (!m_platformData.size()) {
126          m_fontMetrics.reset();
127          m_avgCharWidth = 0;
128          m_maxCharWidth = 0;
129          return;
130     }
131
132 #if HAVE(QRAWFONT)
133     QRawFont rawFont(m_platformData.rawFont());
134     float descent = rawFont.descent();
135     float ascent = rawFont.ascent();
136     float xHeight = rawFont.xHeight();
137     float lineSpacing = ascent + descent + rawFont.leading() + 1;
138
139     QVector<quint32> indexes = rawFont.glyphIndexesForString(QLatin1String(" "));
140     QVector<QPointF> advances = rawFont.advancesForGlyphIndexes(indexes);
141     float spaceWidth = advances.at(0).x();
142 #else
143     QFontMetricsF fm(m_platformData.font());
144     float descent = fm.descent();
145     float ascent = fm.ascent();
146     float xHeight = fm.xHeight();
147     float lineSpacing = fm.lineSpacing();
148     float spaceWidth = fm.width(QLatin1Char(' '));
149 #endif
150
151     // Qt subtracts 1 from the descent to account for the baseline,
152     // we add it back here to get correct metrics for WebKit.
153     descent += 1;
154
155     // The line spacing should always be >= (ascent + descent), but this
156     // may be false in some cases due to misbehaving platform libraries.
157     // Workaround from SimpleFontPango.cpp and SimpleFontFreeType.cpp
158     if (lineSpacing < ascent + descent)
159         lineSpacing = ascent + descent;
160
161     // QFontMetricsF::leading() may return negative values on platforms
162     // such as FreeType. Calculate the line gap manually instead.
163     float lineGap = lineSpacing - ascent - descent;
164
165     m_fontMetrics.setAscent(ascent);
166     m_fontMetrics.setDescent(descent);
167     m_fontMetrics.setLineSpacing(lineSpacing);
168     m_fontMetrics.setXHeight(xHeight);
169     m_fontMetrics.setLineGap(lineGap);
170     m_spaceWidth = spaceWidth;
171 }
172
173 void SimpleFontData::platformCharWidthInit()
174 {
175     if (!m_platformData.size())
176         return;
177 #if HAVE(QRAWFONT)
178     QRawFont rawFont(m_platformData.rawFont());
179     m_avgCharWidth = rawFont.averageCharWidth();
180     m_maxCharWidth = rawFont.maxCharWidth();
181 #else
182     QFontMetricsF fm(m_platformData.font());
183     m_avgCharWidth = fm.averageCharWidth();
184     m_maxCharWidth = fm.maxWidth();
185 #endif
186 }
187
188 void SimpleFontData::platformDestroy()
189 {
190 }
191
192 }