1 /*
2 Copyright (C) 2008 Holger Hans Peter Freyther
3 Copyright (C) 2009 Torch Mobile Inc. http://www.torchmobile.com/
4 Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies)
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 */
22
23 #include "config.h"
24 #include "FontPlatformData.h"
25
26 #include "PlatformString.h"
27
28 namespace WebCore {
29
isEmptyValue(const float size,const bool bold,const bool oblique)30 static inline bool isEmptyValue(const float size, const bool bold, const bool oblique)
31 {
32 // this is the empty value by definition of the trait FontDataCacheKeyTraits
33 return !bold && !oblique && size == 0.f;
34 }
35
FontPlatformData(float size,bool bold,bool oblique)36 FontPlatformData::FontPlatformData(float size, bool bold, bool oblique)
37 {
38 if (!isEmptyValue(size, bold, oblique))
39 m_data = adoptRef(new FontPlatformDataPrivate(size, bold, oblique));
40 }
41
FontPlatformData(const FontDescription & description,const AtomicString & familyName,int wordSpacing,int letterSpacing)42 FontPlatformData::FontPlatformData(const FontDescription& description, const AtomicString& familyName, int wordSpacing, int letterSpacing)
43 : m_data(adoptRef(new FontPlatformDataPrivate()))
44 {
45 QFont& font = m_data->font;
46 int requestedSize = qRound(description.computedPixelSize());
47 font.setFamily(familyName);
48 font.setPixelSize(qRound(requestedSize));
49 font.setItalic(description.italic());
50 font.setWeight(toQFontWeight(description.weight()));
51 font.setWordSpacing(wordSpacing);
52 font.setLetterSpacing(QFont::AbsoluteSpacing, letterSpacing);
53 const bool smallCaps = description.smallCaps();
54 font.setCapitalization(smallCaps ? QFont::SmallCaps : QFont::MixedCase);
55 // Commented out to work around webkit bug 93263
56 //font.setStyleStrategy(QFont::ForceIntegerMetrics);
57
58 m_data->bold = font.bold();
59 // WebKit allows font size zero but QFont does not. We will return
60 // m_data->size if a font size of zero is requested and pixelSize()
61 // otherwise.
62 m_data->size = (!requestedSize) ? requestedSize : font.pixelSize();
63 #if HAVE(QRAWFONT)
64 m_data->rawFont = QRawFont::fromFont(font, QFontDatabase::Any);
65 #endif
66 }
67
68 #if HAVE(QRAWFONT)
FontPlatformData(const FontPlatformData & other,float size)69 FontPlatformData::FontPlatformData(const FontPlatformData& other, float size)
70 : m_data(adoptRef(new FontPlatformDataPrivate()))
71 {
72 m_data->font = other.m_data->font;
73 m_data->rawFont = other.m_data->rawFont;
74 m_data->bold = other.m_data->bold;
75 m_data->oblique = other.m_data->oblique;
76 m_data->font.setPixelSize(size);
77 m_data->rawFont.setPixelSize(size);
78 m_data->size = size ? m_data->font.pixelSize() : 0;
79 }
80 #endif
81
operator ==(const FontPlatformData & other) const82 bool FontPlatformData::operator==(const FontPlatformData& other) const
83 {
84 if (m_data == other.m_data)
85 return true;
86
87 if (!m_data || !other.m_data || m_data->isDeletedValue || other.m_data->isDeletedValue)
88 return false;
89
90 const bool equals = (m_data->size == other.m_data->size
91 && m_data->bold == other.m_data->bold
92 && m_data->oblique == other.m_data->oblique
93 && m_data->font == other.m_data->font);
94 return equals;
95 }
96
hash() const97 unsigned FontPlatformData::hash() const
98 {
99 if (!m_data)
100 return 0;
101 if (m_data->isDeletedValue)
102 return 1;
103 return qHash(m_data->font.toString())
104 ^ qHash(*reinterpret_cast<quint32*>(&m_data->size))
105 ^ qHash(m_data->bold)
106 ^ qHash(m_data->oblique);
107 }
108
109 #ifndef NDEBUG
description() const110 String FontPlatformData::description() const
111 {
112 return String();
113 }
114 #endif
115
116 }
117