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    Copyright (C) 2013 Digia Plc. and/or its subsidiary(-ies)
6
7    This library is free software; you can redistribute it and/or
8    modify it under the terms of the GNU Library General Public
9    License as published by the Free Software Foundation; either
10    version 2 of the License, or (at your option) any later version.
11
12    This library is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15    Library General Public License for more details.
16
17    You should have received a copy of the GNU Library General Public License
18    along with this library; see the file COPYING.LIB.  If not, write to
19    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
20    Boston, MA 02110-1301, USA.
21
22*/
23
24#include "config.h"
25#include "FontPlatformData.h"
26
27#include "Font.h"
28#include <wtf/text/WTFString.h>
29
30namespace WebCore {
31
32static inline QFont::Weight toQFontWeight(FontWeight fontWeight)
33{
34    switch (fontWeight) {
35    case FontWeight100:
36    case FontWeight200:
37        return QFont::Light; // QFont::Light == Weight of 25
38    case FontWeight600:
39        return QFont::DemiBold; // QFont::DemiBold == Weight of 63
40    case FontWeight700:
41    case FontWeight800:
42        return QFont::Bold; // QFont::Bold == Weight of 75
43    case FontWeight900:
44        return QFont::Black; // QFont::Black == Weight of 87
45    case FontWeight300:
46    case FontWeight400:
47    case FontWeight500:
48    default:
49        return QFont::Normal; // QFont::Normal == Weight of 50
50    }
51}
52
53static inline bool isEmptyValue(const float size, const bool bold, const bool oblique)
54{
55     // this is the empty value by definition of the trait FontDataCacheKeyTraits
56    return !bold && !oblique && size == 0.f;
57}
58
59FontPlatformData::FontPlatformData(float size, bool bold, bool oblique)
60{
61    if (!isEmptyValue(size, bold, oblique))
62        m_data = adoptRef(new FontPlatformDataPrivate(size, bold, oblique));
63}
64
65FontPlatformData::FontPlatformData(const FontDescription& description, const AtomicString& familyName, int wordSpacing, int letterSpacing)
66    : m_data(adoptRef(new FontPlatformDataPrivate()))
67{
68    QFont font;
69    int requestedSize = description.computedPixelSize();
70    font.setFamily(familyName);
71    if (requestedSize)
72        font.setPixelSize(requestedSize);
73    font.setItalic(description.italic());
74    font.setWeight(toQFontWeight(description.weight()));
75    font.setWordSpacing(wordSpacing);
76    font.setLetterSpacing(QFont::AbsoluteSpacing, letterSpacing);
77    if (description.fontSmoothing() == NoSmoothing
78        || (description.fontSmoothing() == AutoSmoothing && !Font::shouldUseSmoothing()))
79        font.setStyleStrategy(QFont::NoAntialias);
80
81    m_data->bold = font.bold();
82    // WebKit allows font size zero but QFont does not. We will return
83    // m_data->size if a font size of zero is requested and pixelSize()
84    // otherwise.
85    m_data->size = (!requestedSize) ? requestedSize : font.pixelSize();
86    m_data->rawFont = QRawFont::fromFont(font, QFontDatabase::Any);
87}
88
89FontPlatformData::FontPlatformData(const FontPlatformData& other, float size)
90    : m_data(adoptRef(new FontPlatformDataPrivate()))
91{
92    m_data->rawFont = other.m_data->rawFont;
93    m_data->bold = other.m_data->bold;
94    m_data->oblique = other.m_data->oblique;
95    m_data->rawFont.setPixelSize(size);
96    m_data->size = m_data->rawFont.pixelSize();
97}
98
99bool FontPlatformData::operator==(const FontPlatformData& other) const
100{
101    if (m_data == other.m_data)
102        return true;
103
104    if (!m_data || !other.m_data || m_data->isDeletedValue || other.m_data->isDeletedValue)
105        return false;
106
107    const bool equals = (m_data->size == other.m_data->size
108                         && m_data->bold == other.m_data->bold
109                         && m_data->oblique == other.m_data->oblique
110                         && m_data->rawFont == other.m_data->rawFont);
111    return equals;
112}
113
114unsigned FontPlatformData::hash() const
115{
116    if (!m_data)
117        return 0;
118    if (m_data->isDeletedValue)
119        return 1;
120    return qHash(m_data->rawFont.familyName()) ^ qHash(m_data->rawFont.style())
121            ^ qHash(m_data->rawFont.weight())
122            ^ qHash(*reinterpret_cast<quint32*>(&m_data->size));
123}
124
125#ifndef NDEBUG
126String FontPlatformData::description() const
127{
128    return String();
129}
130#endif
131
132}
133