Home | History | Annotate | Download | only in qt
      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 
     30 static inline QFont::Weight toQFontWeight(FontWeight fontWeight)
     31 {
     32     switch (fontWeight) {
     33     case FontWeight100:
     34     case FontWeight200:
     35         return QFont::Light; // QFont::Light == Weight of 25
     36     case FontWeight600:
     37         return QFont::DemiBold; // QFont::DemiBold == Weight of 63
     38     case FontWeight700:
     39     case FontWeight800:
     40         return QFont::Bold; // QFont::Bold == Weight of 75
     41     case FontWeight900:
     42         return QFont::Black; // QFont::Black == Weight of 87
     43     case FontWeight300:
     44     case FontWeight400:
     45     case FontWeight500:
     46     default:
     47         return QFont::Normal; // QFont::Normal == Weight of 50
     48     }
     49 }
     50 
     51 static inline bool isEmptyValue(const float size, const bool bold, const bool oblique)
     52 {
     53      // this is the empty value by definition of the trait FontDataCacheKeyTraits
     54     return !bold && !oblique && size == 0.f;
     55 }
     56 
     57 FontPlatformData::FontPlatformData(float size, bool bold, bool oblique)
     58 {
     59     if (!isEmptyValue(size, bold, oblique))
     60         m_data = adoptRef(new FontPlatformDataPrivate(size, bold, oblique));
     61 }
     62 
     63 FontPlatformData::FontPlatformData(const FontDescription& description, const AtomicString& familyName, int wordSpacing, int letterSpacing)
     64     : m_data(adoptRef(new FontPlatformDataPrivate()))
     65 {
     66     QFont& font = m_data->font;
     67     int requestedSize = qRound(description.computedPixelSize());
     68     font.setFamily(familyName);
     69     font.setPixelSize(qRound(requestedSize));
     70     font.setItalic(description.italic());
     71     font.setWeight(toQFontWeight(description.weight()));
     72     font.setWordSpacing(wordSpacing);
     73     font.setLetterSpacing(QFont::AbsoluteSpacing, letterSpacing);
     74     const bool smallCaps = description.smallCaps();
     75     font.setCapitalization(smallCaps ? QFont::SmallCaps : QFont::MixedCase);
     76     font.setStyleStrategy(QFont::ForceIntegerMetrics);
     77 
     78     m_data->bold = font.bold();
     79     // WebKit allows font size zero but QFont does not. We will return
     80     // m_data->size if a font size of zero is requested and pixelSize()
     81     // otherwise.
     82     m_data->size = (!requestedSize) ? requestedSize : font.pixelSize();
     83 }
     84 
     85 bool FontPlatformData::operator==(const FontPlatformData& other) const
     86 {
     87     if (m_data == other.m_data)
     88         return true;
     89 
     90     if (!m_data || !other.m_data || m_data->isDeletedValue || other.m_data->isDeletedValue)
     91         return false;
     92 
     93     const bool equals = (m_data->size == other.m_data->size
     94                          && m_data->bold == other.m_data->bold
     95                          && m_data->oblique == other.m_data->oblique
     96                          && m_data->font == other.m_data->font);
     97     return equals;
     98 }
     99 
    100 unsigned FontPlatformData::hash() const
    101 {
    102     if (!m_data)
    103         return 0;
    104     if (m_data->isDeletedValue)
    105         return 1;
    106     return qHash(m_data->font.toString())
    107            ^ qHash(*reinterpret_cast<quint32*>(&m_data->size))
    108            ^ qHash(m_data->bold)
    109            ^ qHash(m_data->oblique);
    110 }
    111 
    112 #ifndef NDEBUG
    113 String FontPlatformData::description() const
    114 {
    115     return String();
    116 }
    117 #endif
    118 
    119 }
    120