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"
27 #include "NotImplemented.h"
31 void SimpleFontData::determinePitch()
34 m_treatAsFixedPitch = false;
37 bool SimpleFontData::containsCharacters(const UChar* characters, int length) const
39 QRawFont rawFont(m_platformData.rawFont());
41 for (int i = 0; i < length; ++i) {
42 if (!rawFont.supportsCharacter(static_cast<QChar>(characters[i])))
49 float SimpleFontData::platformWidthForGlyph(Glyph glyph) const
51 if (!glyph || !platformData().size())
54 QVector<quint32> glyphIndexes;
55 glyphIndexes.append(glyph);
56 QVector<QPointF> advances = platformData().rawFont().advancesForGlyphIndexes(glyphIndexes);
57 ASSERT(!advances.isEmpty());
58 return advances.at(0).x();
61 PassRefPtr<SimpleFontData> SimpleFontData::createScaledFontData(const FontDescription& fontDescription, float scaleFactor) const
63 const float scaledSize = lroundf(fontDescription.computedSize() * scaleFactor);
64 return SimpleFontData::create(FontPlatformData(m_platformData, scaledSize), isCustomFont(), false);
67 FloatRect SimpleFontData::platformBoundsForGlyph(Glyph glyph) const
69 return m_platformData.rawFont().boundingRect(glyph);
72 void SimpleFontData::platformInit()
74 if (!m_platformData.size()) {
75 m_fontMetrics.reset();
81 QRawFont rawFont(m_platformData.rawFont());
82 float descent = rawFont.descent();
83 float ascent = rawFont.ascent();
84 float xHeight = rawFont.xHeight();
85 float lineSpacing = ascent + descent + rawFont.leading();
87 QVector<quint32> indexes = rawFont.glyphIndexesForString(QLatin1String(" "));
88 QVector<QPointF> advances = rawFont.advancesForGlyphIndexes(indexes);
89 float spaceWidth = advances.at(0).x();
91 indexes = rawFont.glyphIndexesForString(QLatin1String("0"));
92 advances = rawFont.advancesForGlyphIndexes(indexes);
93 float zeroWidth = advances.at(0).x();
95 // The line spacing should always be >= (ascent + descent), but this
96 // may be false in some cases due to misbehaving platform libraries.
97 // Workaround from SimpleFontPango.cpp and SimpleFontFreeType.cpp
98 if (lineSpacing < ascent + descent)
99 lineSpacing = ascent + descent;
101 // QFontMetricsF::leading() may return negative values on platforms
102 // such as FreeType. Calculate the line gap manually instead.
103 float lineGap = lineSpacing - ascent - descent;
105 m_fontMetrics.setAscent(ascent);
106 // WebKit expects the descent to be positive.
107 m_fontMetrics.setDescent(qAbs(descent));
108 m_fontMetrics.setLineSpacing(lineSpacing);
109 m_fontMetrics.setXHeight(xHeight);
110 m_fontMetrics.setLineGap(lineGap);
111 m_fontMetrics.setZeroWidth(zeroWidth);
112 m_spaceWidth = spaceWidth;
115 void SimpleFontData::platformCharWidthInit()
117 if (!m_platformData.size())
119 QRawFont rawFont(m_platformData.rawFont());
120 m_avgCharWidth = rawFont.averageCharWidth();
121 m_maxCharWidth = rawFont.maxCharWidth();
124 void SimpleFontData::platformDestroy()