• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include <QFontMetricsF>
28 
29 namespace WebCore {
30 
determinePitch()31 void SimpleFontData::determinePitch()
32 {
33     m_treatAsFixedPitch = m_platformData.font().fixedPitch();
34 }
35 
containsCharacters(const UChar *,int) const36 bool SimpleFontData::containsCharacters(const UChar*, int) const
37 {
38     return true;
39 }
40 
platformInit()41 void SimpleFontData::platformInit()
42 {
43     if (!m_platformData.size()) {
44          m_fontMetrics.reset();
45          m_avgCharWidth = 0;
46          m_maxCharWidth = 0;
47          return;
48     }
49 
50     QFontMetricsF fm(m_platformData.font());
51 
52     // Qt subtracts 1 from the descent to account for the baseline,
53     // we add it back here to get correct metrics for WebKit.
54     float descent = fm.descent() + 1;
55     float ascent = fm.ascent();
56 
57     float lineSpacing = fm.lineSpacing();
58 
59     // The line spacing should always be >= (ascent + descent), but this
60     // may be false in some cases due to misbehaving platform libraries.
61     // Workaround from SimpleFontPango.cpp and SimpleFontFreeType.cpp
62     if (lineSpacing < ascent + descent)
63         lineSpacing = ascent + descent;
64 
65     // QFontMetricsF::leading() may return negative values on platforms
66     // such as FreeType. Calculate the line gap manually instead.
67     float lineGap = lineSpacing - ascent - descent;
68 
69     m_fontMetrics.setAscent(ascent);
70     m_fontMetrics.setDescent(descent);
71     m_fontMetrics.setLineSpacing(lineSpacing);
72     m_fontMetrics.setXHeight(fm.xHeight());
73     m_fontMetrics.setLineGap(lineGap);
74     m_spaceWidth = fm.width(QLatin1Char(' '));
75 }
76 
platformGlyphInit()77 void SimpleFontData::platformGlyphInit()
78 {
79     if (!m_platformData.size())
80         return;
81     m_spaceGlyph = 0;
82     determinePitch();
83     m_missingGlyphData.fontData = this;
84     m_missingGlyphData.glyph = 0;
85 }
86 
platformCharWidthInit()87 void SimpleFontData::platformCharWidthInit()
88 {
89     if (!m_platformData.size())
90         return;
91     QFontMetrics fm(m_platformData.font());
92     m_avgCharWidth = fm.averageCharWidth();
93     m_maxCharWidth = fm.maxWidth();
94 }
95 
platformDestroy()96 void SimpleFontData::platformDestroy()
97 {
98 }
99 
100 }
101