Home | History | Annotate | Download | only in qt
      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 #include <wtf/Forward.h>
     32 #include <wtf/RefCounted.h>
     33 
     34 namespace WebCore {
     35 
     36 class FontPlatformDataPrivate : public RefCounted<FontPlatformDataPrivate> {
     37     WTF_MAKE_NONCOPYABLE(FontPlatformDataPrivate); WTF_MAKE_FAST_ALLOCATED;
     38 public:
     39     FontPlatformDataPrivate()
     40         : size(font.pixelSize())
     41         , bold(font.bold())
     42         , oblique(false)
     43         , isDeletedValue(false)
     44     { }
     45     FontPlatformDataPrivate(const float size, const bool bold, const bool oblique)
     46         : size(size)
     47         , bold(bold)
     48         , oblique(oblique)
     49         , isDeletedValue(false)
     50     { }
     51     FontPlatformDataPrivate(const QFont& font)
     52         : font(font)
     53         , size(font.pixelSize())
     54         , bold(font.bold())
     55         , oblique(false)
     56         , isDeletedValue(false)
     57     { }
     58     FontPlatformDataPrivate(WTF::HashTableDeletedValueType)
     59         : isDeletedValue(true)
     60     { }
     61 
     62     QFont font;
     63     float size;
     64     bool bold : 1;
     65     bool oblique : 1;
     66     bool isDeletedValue : 1;
     67 };
     68 
     69 class FontPlatformData {
     70     WTF_MAKE_FAST_ALLOCATED;
     71 public:
     72     FontPlatformData(float size, bool bold, bool oblique);
     73     FontPlatformData(const FontDescription&, const AtomicString& familyName, int wordSpacing = 0, int letterSpacing = 0);
     74     FontPlatformData(const QFont& font)
     75         : m_data(adoptRef(new FontPlatformDataPrivate(font)))
     76     { }
     77     FontPlatformData(WTF::HashTableDeletedValueType)
     78         : m_data(adoptRef(new FontPlatformDataPrivate()))
     79     {
     80         m_data->isDeletedValue = true;
     81     }
     82 
     83     bool operator==(const FontPlatformData&) const;
     84 
     85     bool isHashTableDeletedValue() const
     86     {
     87         return m_data && m_data->isDeletedValue;
     88     }
     89 
     90     QFont font() const
     91     {
     92         Q_ASSERT(!isHashTableDeletedValue());
     93         if (!m_data)
     94             return QFont();
     95         return m_data->font;
     96     }
     97     float size() const
     98     {
     99         Q_ASSERT(!isHashTableDeletedValue());
    100         if (!m_data)
    101             return 0;
    102         return m_data->size;
    103     }
    104     QString family() const
    105     {
    106         Q_ASSERT(!isHashTableDeletedValue());
    107         if (!m_data)
    108             return QString();
    109         return m_data->font.family();
    110     }
    111     bool bold() const
    112     {
    113         Q_ASSERT(!isHashTableDeletedValue());
    114         if (!m_data)
    115             return false;
    116         return m_data->bold;
    117     }
    118     bool italic() const
    119     {
    120         Q_ASSERT(!isHashTableDeletedValue());
    121         if (!m_data)
    122             return false;
    123         return m_data->font.italic();
    124     }
    125     bool smallCaps() const
    126     {
    127         Q_ASSERT(!isHashTableDeletedValue());
    128         if (!m_data)
    129             return false;
    130         return m_data->font.capitalization() == QFont::SmallCaps;
    131     }
    132     int pixelSize() const
    133     {
    134         Q_ASSERT(!isHashTableDeletedValue());
    135         if (!m_data)
    136             return 0;
    137         // WebCore allows a font size of zero, but QFont does not.
    138         if (!m_data->size)
    139             return 0;
    140         return m_data->font.pixelSize();
    141     }
    142 
    143     FontOrientation orientation() const { return Horizontal; } // FIXME: Implement.
    144     void setOrientation(FontOrientation) { } // FIXME: Implement.
    145 
    146     unsigned hash() const;
    147 
    148 #ifndef NDEBUG
    149     String description() const;
    150 #endif
    151 private:
    152     RefPtr<FontPlatformDataPrivate> m_data;
    153 };
    154 
    155 } // namespace WebCore
    156 
    157 #endif // FontPlatformData_h
    158