1 /* 2 * Copyright (c) 2006, 2007, 2008, Google Inc. All rights reserved. 3 * 4 * Redistribution and use in source and binary forms, with or without 5 * modification, are permitted provided that the following conditions are 6 * met: 7 * 8 * * Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * * Redistributions in binary form must reproduce the above 11 * copyright notice, this list of conditions and the following disclaimer 12 * in the documentation and/or other materials provided with the 13 * distribution. 14 * * Neither the name of Google Inc. nor the names of its 15 * contributors may be used to endorse or promote products derived from 16 * this software without specific prior written permission. 17 * 18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 29 */ 30 31 #include "config.h" 32 #include "platform/fonts/harfbuzz/FontPlatformDataHarfBuzz.h" 33 34 #include "SkTypeface.h" 35 #include "platform/fonts/harfbuzz/HarfBuzzFace.h" 36 #include "wtf/text/WTFString.h" 37 38 namespace WebCore { 39 40 FontPlatformData::FontPlatformData(WTF::HashTableDeletedValueType) 41 : m_textSize(0) 42 , m_syntheticBold(false) 43 , m_syntheticItalic(false) 44 , m_orientation(Horizontal) 45 , m_isHashTableDeletedValue(true) 46 #if OS(WIN) 47 , m_paintTextFlags(0) 48 , m_minSizeForAntiAlias(0) 49 , m_useSubpixelPositioning(false) 50 #endif 51 { 52 } 53 54 FontPlatformData::FontPlatformData() 55 : m_textSize(0) 56 , m_syntheticBold(false) 57 , m_syntheticItalic(false) 58 , m_orientation(Horizontal) 59 , m_isHashTableDeletedValue(false) 60 #if OS(WIN) 61 , m_paintTextFlags(0) 62 , m_minSizeForAntiAlias(0) 63 , m_useSubpixelPositioning(false) 64 #endif 65 { 66 } 67 68 FontPlatformData::FontPlatformData(float textSize, bool syntheticBold, bool syntheticItalic) 69 : m_textSize(textSize) 70 , m_syntheticBold(syntheticBold) 71 , m_syntheticItalic(syntheticItalic) 72 , m_orientation(Horizontal) 73 , m_isHashTableDeletedValue(false) 74 #if OS(WIN) 75 , m_paintTextFlags(0) 76 , m_minSizeForAntiAlias(0) 77 , m_useSubpixelPositioning(false) 78 #endif 79 { 80 } 81 82 FontPlatformData::FontPlatformData(const FontPlatformData& src) 83 : m_typeface(src.m_typeface) 84 #if !OS(WIN) 85 , m_family(src.m_family) 86 #endif 87 , m_textSize(src.m_textSize) 88 , m_syntheticBold(src.m_syntheticBold) 89 , m_syntheticItalic(src.m_syntheticItalic) 90 , m_orientation(src.m_orientation) 91 , m_style(src.m_style) 92 , m_harfBuzzFace(nullptr) 93 , m_isHashTableDeletedValue(false) 94 #if OS(WIN) 95 , m_paintTextFlags(src.m_paintTextFlags) 96 , m_minSizeForAntiAlias(src.m_minSizeForAntiAlias) 97 , m_useSubpixelPositioning(src.m_useSubpixelPositioning) 98 #endif 99 { 100 } 101 102 FontPlatformData::FontPlatformData(PassRefPtr<SkTypeface> tf, const char* family, float textSize, bool syntheticBold, bool syntheticItalic, FontOrientation orientation, bool subpixelTextPosition) 103 : m_typeface(tf) 104 #if !OS(WIN) 105 , m_family(family) 106 #endif 107 , m_textSize(textSize) 108 , m_syntheticBold(syntheticBold) 109 , m_syntheticItalic(syntheticItalic) 110 , m_orientation(orientation) 111 , m_isHashTableDeletedValue(false) 112 #if OS(WIN) 113 , m_paintTextFlags(0) 114 , m_minSizeForAntiAlias(0) 115 , m_useSubpixelPositioning(subpixelTextPosition) 116 #endif 117 { 118 querySystemForRenderStyle(subpixelTextPosition); 119 } 120 121 FontPlatformData::FontPlatformData(const FontPlatformData& src, float textSize) 122 : m_typeface(src.m_typeface) 123 #if !OS(WIN) 124 , m_family(src.m_family) 125 #endif 126 , m_textSize(textSize) 127 , m_syntheticBold(src.m_syntheticBold) 128 , m_syntheticItalic(src.m_syntheticItalic) 129 , m_orientation(src.m_orientation) 130 , m_harfBuzzFace(nullptr) 131 , m_isHashTableDeletedValue(false) 132 #if OS(WIN) 133 , m_paintTextFlags(src.m_paintTextFlags) 134 , m_minSizeForAntiAlias(src.m_minSizeForAntiAlias) 135 , m_useSubpixelPositioning(src.m_useSubpixelPositioning) 136 #endif 137 { 138 querySystemForRenderStyle(FontDescription::subpixelPositioning()); 139 } 140 141 FontPlatformData::~FontPlatformData() 142 { 143 } 144 145 FontPlatformData& FontPlatformData::operator=(const FontPlatformData& src) 146 { 147 m_typeface = src.m_typeface; 148 #if !OS(WIN) 149 m_family = src.m_family; 150 #endif 151 m_textSize = src.m_textSize; 152 m_syntheticBold = src.m_syntheticBold; 153 m_syntheticItalic = src.m_syntheticItalic; 154 m_harfBuzzFace = nullptr; 155 m_orientation = src.m_orientation; 156 m_style = src.m_style; 157 #if OS(WIN) 158 m_paintTextFlags = 0; 159 m_minSizeForAntiAlias = src.m_minSizeForAntiAlias; 160 m_useSubpixelPositioning = src.m_useSubpixelPositioning; 161 #endif 162 163 return *this; 164 } 165 166 #ifndef NDEBUG 167 String FontPlatformData::description() const 168 { 169 return String(); 170 } 171 #endif 172 173 SkFontID FontPlatformData::uniqueID() const 174 { 175 return m_typeface->uniqueID(); 176 } 177 178 String FontPlatformData::fontFamilyName() const 179 { 180 // FIXME(crbug.com/326582): come up with a proper way of handling SVG. 181 if (!this->typeface()) 182 return ""; 183 SkTypeface::LocalizedStrings* fontFamilyIterator = this->typeface()->createFamilyNameIterator(); 184 SkTypeface::LocalizedString localizedString; 185 while (fontFamilyIterator->next(&localizedString) && !localizedString.fString.size()) { } 186 fontFamilyIterator->unref(); 187 return String(localizedString.fString.c_str()); 188 } 189 190 bool FontPlatformData::operator==(const FontPlatformData& a) const 191 { 192 // If either of the typeface pointers are null then we test for pointer 193 // equality. Otherwise, we call SkTypeface::Equal on the valid pointers. 194 bool typefacesEqual; 195 if (!m_typeface || !a.m_typeface) 196 typefacesEqual = m_typeface == a.m_typeface; 197 else 198 typefacesEqual = SkTypeface::Equal(m_typeface.get(), a.m_typeface.get()); 199 200 return typefacesEqual 201 && m_textSize == a.m_textSize 202 && m_syntheticBold == a.m_syntheticBold 203 && m_syntheticItalic == a.m_syntheticItalic 204 && m_orientation == a.m_orientation 205 && m_style == a.m_style 206 && m_isHashTableDeletedValue == a.m_isHashTableDeletedValue; 207 } 208 209 bool FontPlatformData::isFixedPitch() const 210 { 211 return typeface() && typeface()->isFixedPitch(); 212 } 213 214 HarfBuzzFace* FontPlatformData::harfBuzzFace() const 215 { 216 if (!m_harfBuzzFace) 217 m_harfBuzzFace = HarfBuzzFace::create(const_cast<FontPlatformData*>(this), uniqueID()); 218 219 return m_harfBuzzFace.get(); 220 } 221 222 } // namespace WebCore 223