1 /* 2 Copyright (C) 2008 Holger Hans Peter Freyther 3 Copyright (C) 2009 Torch Mobile Inc. http://www.torchmobile.com/ 4 5 This library is free software; you can redistribute it and/or 6 modify it under the terms of the GNU Library General Public 7 License as published by the Free Software Foundation; either 8 version 2 of the License, or (at your option) any later version. 9 10 This library is distributed in the hope that it will be useful, 11 but WITHOUT ANY WARRANTY; without even the implied warranty of 12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 13 Library General Public License for more details. 14 15 You should have received a copy of the GNU Library General Public License 16 along with this library; see the file COPYING.LIB. If not, write to 17 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 18 Boston, MA 02110-1301, USA. 19 20 */ 21 22 #include "config.h" 23 #include "FontPlatformData.h" 24 25 #include "PlatformString.h" 26 27 namespace WebCore { 28 29 static inline bool isEmtpyValue(const float size, const bool bold, const bool oblique) 30 { 31 // this is the empty value by definition of the trait FontDataCacheKeyTraits 32 return !bold && !oblique && size == 0.f; 33 } 34 35 FontPlatformData::FontPlatformData(float size, bool bold, bool oblique) 36 { 37 if (isEmtpyValue(size, bold, oblique)) 38 m_data = 0; 39 else 40 m_data = new FontPlatformDataPrivate(size, bold, oblique); 41 } 42 43 FontPlatformData::FontPlatformData(const FontPlatformData &other) : m_data(other.m_data) 44 { 45 if (m_data && m_data != reinterpret_cast<FontPlatformDataPrivate*>(-1)) 46 ++m_data->refCount; 47 } 48 49 FontPlatformData::FontPlatformData(const FontDescription& description, const AtomicString& familyName, int wordSpacing, int letterSpacing) 50 : m_data(new FontPlatformDataPrivate()) 51 { 52 QFont& font = m_data->font; 53 font.setFamily(familyName); 54 font.setPixelSize(qRound(description.computedSize())); 55 font.setItalic(description.italic()); 56 font.setWeight(toQFontWeight(description.weight())); 57 font.setWordSpacing(wordSpacing); 58 font.setLetterSpacing(QFont::AbsoluteSpacing, letterSpacing); 59 const bool smallCaps = description.smallCaps(); 60 font.setCapitalization(smallCaps ? QFont::SmallCaps : QFont::MixedCase); 61 62 m_data->bold = font.bold(); 63 m_data->size = font.pointSizeF(); 64 } 65 66 FontPlatformData::~FontPlatformData() 67 { 68 if (!m_data || m_data == reinterpret_cast<FontPlatformDataPrivate*>(-1)) 69 return; 70 --m_data->refCount; 71 if (!m_data->refCount) 72 delete m_data; 73 } 74 75 FontPlatformData& FontPlatformData::operator=(const FontPlatformData& other) 76 { 77 if (m_data == other.m_data) 78 return *this; 79 if (m_data && m_data != reinterpret_cast<FontPlatformDataPrivate*>(-1)) { 80 --m_data->refCount; 81 if (!m_data->refCount) 82 delete m_data; 83 } 84 m_data = other.m_data; 85 if (m_data && m_data != reinterpret_cast<FontPlatformDataPrivate*>(-1)) 86 ++m_data->refCount; 87 return *this; 88 } 89 90 bool FontPlatformData::operator==(const FontPlatformData& other) const 91 { 92 if (m_data == other.m_data) 93 return true; 94 95 if (!m_data || !other.m_data 96 || m_data == reinterpret_cast<FontPlatformDataPrivate*>(-1) || other.m_data == reinterpret_cast<FontPlatformDataPrivate*>(-1)) 97 return false; 98 99 const bool equals = (m_data->size == other.m_data->size 100 && m_data->bold == other.m_data->bold 101 && m_data->oblique == other.m_data->oblique 102 && m_data->font == other.m_data->font); 103 return equals; 104 } 105 106 unsigned FontPlatformData::hash() const 107 { 108 if (!m_data) 109 return 0; 110 if (m_data == reinterpret_cast<FontPlatformDataPrivate*>(-1)) 111 return 1; 112 return qHash(m_data->font.toString()) 113 ^ qHash(*reinterpret_cast<quint32*>(&m_data->size)) 114 ^ qHash(m_data->bold) 115 ^ qHash(m_data->oblique); 116 } 117 118 #ifndef NDEBUG 119 String FontPlatformData::description() const 120 { 121 return String(); 122 } 123 #endif 124 125 } 126