1 /*
2     Copyright (C) 2008 Nokia Corporation and/or its subsidiary(-ies)
3     Copyright (C) 2008 Holger Hans Peter Freyther
4     Copyright (C) 2009 Torch Mobile Inc. http://www.torchmobile.com/
5 
6     This library is free software; you can redistribute it and/or
7     modify it under the terms of the GNU Library General Public
8     License as published by the Free Software Foundation; either
9     version 2 of the License, or (at your option) any later version.
10 
11     This library is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14     Library General Public License for more details.
15 
16     You should have received a copy of the GNU Library General Public License
17     along with this library; see the file COPYING.LIB.  If not, write to
18     the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19     Boston, MA 02110-1301, USA.
20 
21     This class provides all functionality needed for loading images, style sheets and html
22     pages from the web. It has a memory cache for these objects.
23 */
24 #ifndef FontPlatformData_h
25 #define FontPlatformData_h
26 
27 #include "FontDescription.h"
28 #include "FontOrientation.h"
29 #include <QFont>
30 #include <QHash>
31 #if HAVE(QRAWFONT)
32 #include <QRawFont>
33 #endif
34 #include <wtf/Forward.h>
35 #include <wtf/RefCounted.h>
36 
37 namespace WebCore {
38 
toQFontWeight(FontWeight fontWeight)39 static inline QFont::Weight toQFontWeight(FontWeight fontWeight)
40 {
41     switch (fontWeight) {
42     case FontWeight100:
43     case FontWeight200:
44         return QFont::Light; // QFont::Light == Weight of 25
45     case FontWeight600:
46         return QFont::DemiBold; // QFont::DemiBold == Weight of 63
47     case FontWeight700:
48     case FontWeight800:
49         return QFont::Bold; // QFont::Bold == Weight of 75
50     case FontWeight900:
51         return QFont::Black; // QFont::Black == Weight of 87
52     case FontWeight300:
53     case FontWeight400:
54     case FontWeight500:
55     default:
56         return QFont::Normal; // QFont::Normal == Weight of 50
57     }
58 }
59 
60 class FontPlatformDataPrivate : public RefCounted<FontPlatformDataPrivate> {
61     WTF_MAKE_NONCOPYABLE(FontPlatformDataPrivate); WTF_MAKE_FAST_ALLOCATED;
62 public:
FontPlatformDataPrivate()63     FontPlatformDataPrivate()
64         : size(font.pixelSize())
65         , bold(font.bold())
66         , oblique(false)
67         , isDeletedValue(false)
68     { }
FontPlatformDataPrivate(const float size,const bool bold,const bool oblique)69     FontPlatformDataPrivate(const float size, const bool bold, const bool oblique)
70         : size(size)
71         , bold(bold)
72         , oblique(oblique)
73         , isDeletedValue(false)
74     { }
FontPlatformDataPrivate(const QFont & font)75     FontPlatformDataPrivate(const QFont& font)
76         : font(font)
77 #if HAVE(QRAWFONT)
78         , rawFont(QRawFont::fromFont(font, QFontDatabase::Any))
79 #endif
80         , size(font.pixelSize())
81         , bold(font.bold())
82         , oblique(false)
83         , isDeletedValue(false)
84     { }
85 #if HAVE(QRAWFONT)
FontPlatformDataPrivate(const QRawFont & rawFont)86     FontPlatformDataPrivate(const QRawFont& rawFont)
87         : font()
88         , rawFont(rawFont)
89         , size(rawFont.pixelSize())
90         , bold(rawFont.weight() >= QFont::Bold)
91         , oblique(false)
92         , isDeletedValue(false)
93     { }
94 #endif
FontPlatformDataPrivate(WTF::HashTableDeletedValueType)95     FontPlatformDataPrivate(WTF::HashTableDeletedValueType)
96         : isDeletedValue(true)
97     { }
98 
99     QFont font;
100 #if HAVE(QRAWFONT)
101     QRawFont rawFont;
102 #endif
103     float size;
104     bool bold : 1;
105     bool oblique : 1;
106     bool isDeletedValue : 1;
107 };
108 
109 class FontPlatformData {
110     WTF_MAKE_FAST_ALLOCATED;
111 public:
112     FontPlatformData(float size, bool bold, bool oblique);
113     FontPlatformData(const FontDescription&, const AtomicString& familyName, int wordSpacing = 0, int letterSpacing = 0);
FontPlatformData(const QFont & font)114     FontPlatformData(const QFont& font)
115         : m_data(adoptRef(new FontPlatformDataPrivate(font)))
116     { }
117 #if HAVE(QRAWFONT)
118     FontPlatformData(const FontPlatformData&, float size);
FontPlatformData(const QRawFont & rawFont)119     FontPlatformData(const QRawFont& rawFont)
120         : m_data(adoptRef(new FontPlatformDataPrivate(rawFont)))
121     { }
122 #endif
FontPlatformData(WTF::HashTableDeletedValueType)123     FontPlatformData(WTF::HashTableDeletedValueType)
124         : m_data(adoptRef(new FontPlatformDataPrivate()))
125     {
126         m_data->isDeletedValue = true;
127     }
128 
129     bool operator==(const FontPlatformData&) const;
130 
isHashTableDeletedValue()131     bool isHashTableDeletedValue() const
132     {
133         return m_data && m_data->isDeletedValue;
134     }
135 
font()136     QFont font() const
137     {
138         Q_ASSERT(!isHashTableDeletedValue());
139         if (!m_data)
140             return QFont();
141         return m_data->font;
142     }
143 #if HAVE(QRAWFONT)
rawFont()144     QRawFont rawFont() const
145     {
146         Q_ASSERT(!isHashTableDeletedValue());
147         if (!m_data)
148             return QRawFont();
149         return m_data->rawFont;
150     }
151 #endif
size()152     float size() const
153     {
154         Q_ASSERT(!isHashTableDeletedValue());
155         if (!m_data)
156             return 0;
157         return m_data->size;
158     }
family()159     QString family() const
160     {
161         Q_ASSERT(!isHashTableDeletedValue());
162         if (!m_data)
163             return QString();
164         return m_data->font.family();
165     }
bold()166     bool bold() const
167     {
168         Q_ASSERT(!isHashTableDeletedValue());
169         if (!m_data)
170             return false;
171         return m_data->bold;
172     }
italic()173     bool italic() const
174     {
175         Q_ASSERT(!isHashTableDeletedValue());
176         if (!m_data)
177             return false;
178         return m_data->font.italic();
179     }
smallCaps()180     bool smallCaps() const
181     {
182         Q_ASSERT(!isHashTableDeletedValue());
183         if (!m_data)
184             return false;
185         return m_data->font.capitalization() == QFont::SmallCaps;
186     }
187 
orientation()188     FontOrientation orientation() const { return Horizontal; } // FIXME: Implement.
setOrientation(FontOrientation)189     void setOrientation(FontOrientation) { } // FIXME: Implement.
190 
191     unsigned hash() const;
192 
193 #ifndef NDEBUG
194     String description() const;
195 #endif
196 private:
197     RefPtr<FontPlatformDataPrivate> m_data;
198 };
199 
200 } // namespace WebCore
201 
202 #endif // FontPlatformData_h
203