2 Copyright (C) 2008, 2009, 2010, 2011 Nokia Corporation and/or its subsidiary(-ies)
3 Copyright (C) 2008 Holger Hans Peter Freyther
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.
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.
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.
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.
25 #include "SimpleFontData.h"
28 #include "NotImplemented.h"
30 #include <QFontMetricsF>
35 void SimpleFontData::determinePitch()
39 m_treatAsFixedPitch = false;
41 m_treatAsFixedPitch = m_platformData.font().fixedPitch();
47 static const float smallCapsFraction = 0.7;
48 static const float emphasisMarkFraction = 0.5;
50 bool SimpleFontData::containsCharacters(const UChar* characters, int length) const
52 QRawFont rawFont(m_platformData.rawFont());
54 for (int i = 0; i < length; ++i) {
55 if (!rawFont.supportsCharacter(static_cast<QChar>(characters[i])))
62 float SimpleFontData::platformWidthForGlyph(Glyph glyph) const
64 if (!platformData().size())
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();
74 PassOwnPtr<SimpleFontData> SimpleFontData::createScaledFontData(const FontDescription& fontDescription, float scaleFactor) const
76 const float scaledSize = lroundf(fontDescription.computedSize() * scaleFactor);
77 return adoptPtr(new SimpleFontData(FontPlatformData(m_platformData, scaledSize), isCustomFont(), false));
80 SimpleFontData* SimpleFontData::smallCapsFontData(const FontDescription& fontDescription) const
82 if (!m_derivedFontData)
83 m_derivedFontData = DerivedFontData::create(isCustomFont());
84 if (!m_derivedFontData->smallCaps)
85 m_derivedFontData->smallCaps = createScaledFontData(fontDescription, smallCapsFraction);
87 return m_derivedFontData->smallCaps.get();
90 SimpleFontData* SimpleFontData::emphasisMarkFontData(const FontDescription& fontDescription) const
92 if (!m_derivedFontData)
93 m_derivedFontData = DerivedFontData::create(isCustomFont());
94 if (!m_derivedFontData->emphasisMark)
95 m_derivedFontData->emphasisMark = createScaledFontData(fontDescription, emphasisMarkFraction);
97 return m_derivedFontData->emphasisMark.get();
100 FloatRect SimpleFontData::platformBoundsForGlyph(Glyph) const
106 bool SimpleFontData::containsCharacters(const UChar*, int) const
111 void SimpleFontData::platformGlyphInit()
113 if (!m_platformData.size())
116 m_adjustedSpaceWidth = m_spaceWidth;
118 m_missingGlyphData.fontData = this;
119 m_missingGlyphData.glyph = 0;
123 void SimpleFontData::platformInit()
125 if (!m_platformData.size()) {
126 m_fontMetrics.reset();
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;
139 QVector<quint32> indexes = rawFont.glyphIndexesForString(QLatin1String(" "));
140 QVector<QPointF> advances = rawFont.advancesForGlyphIndexes(indexes);
141 float spaceWidth = advances.at(0).x();
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(' '));
151 // Qt subtracts 1 from the descent to account for the baseline,
152 // we add it back here to get correct metrics for WebKit.
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;
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;
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;
173 void SimpleFontData::platformCharWidthInit()
175 if (!m_platformData.size())
178 QRawFont rawFont(m_platformData.rawFont());
179 m_avgCharWidth = rawFont.averageCharWidth();
180 m_maxCharWidth = rawFont.maxCharWidth();
182 QFontMetricsF fm(m_platformData.font());
183 m_avgCharWidth = fm.averageCharWidth();
184 m_maxCharWidth = fm.maxWidth();
188 void SimpleFontData::platformDestroy()