| OLD | NEW |
| (Empty) |
| 1 /* | |
| 2 * This file is part of the internal font implementation. | |
| 3 * | |
| 4 * Copyright (C) 2006, 2008, 2010 Apple Inc. All rights reserved. | |
| 5 * Copyright (C) 2007-2008 Torch Mobile, Inc. | |
| 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 #ifndef SimpleFontData_h | |
| 25 #define SimpleFontData_h | |
| 26 | |
| 27 #include "core/platform/graphics/CustomFontData.h" | |
| 28 #include "core/platform/graphics/FontPlatformData.h" | |
| 29 #include "core/platform/graphics/GlyphPageTreeNode.h" | |
| 30 #include "platform/fonts/FontBaseline.h" | |
| 31 #include "platform/fonts/FontData.h" | |
| 32 #include "platform/fonts/FontMetrics.h" | |
| 33 #include "platform/fonts/GlyphBuffer.h" | |
| 34 #include "platform/fonts/GlyphMetricsMap.h" | |
| 35 #if ENABLE(OPENTYPE_VERTICAL) | |
| 36 #include "core/platform/graphics/opentype/OpenTypeVerticalData.h" | |
| 37 #endif | |
| 38 #include "platform/fonts/TypesettingFeatures.h" | |
| 39 #include "platform/geometry/FloatRect.h" | |
| 40 #include "wtf/OwnPtr.h" | |
| 41 #include "wtf/PassOwnPtr.h" | |
| 42 #include "wtf/text/StringHash.h" | |
| 43 #include "wtf/UnusedParam.h" | |
| 44 | |
| 45 #if OS(MACOSX) | |
| 46 #include "wtf/RetainPtr.h" | |
| 47 #endif | |
| 48 | |
| 49 namespace WebCore { | |
| 50 | |
| 51 class CSSFontFaceSource; | |
| 52 class FontDescription; | |
| 53 class SharedBuffer; | |
| 54 struct WidthIterator; | |
| 55 | |
| 56 enum FontDataVariant { AutoVariant, NormalVariant, SmallCapsVariant, EmphasisMar
kVariant, BrokenIdeographVariant }; | |
| 57 enum Pitch { UnknownPitch, FixedPitch, VariablePitch }; | |
| 58 | |
| 59 class SimpleFontData : public FontData { | |
| 60 public: | |
| 61 // Used to create platform fonts. | |
| 62 static PassRefPtr<SimpleFontData> create(const FontPlatformData& platformDat
a, PassRefPtr<CustomFontData> customData = 0, bool isTextOrientationFallback = f
alse) | |
| 63 { | |
| 64 return adoptRef(new SimpleFontData(platformData, customData, isTextOrien
tationFallback)); | |
| 65 } | |
| 66 | |
| 67 // Used to create SVG Fonts. | |
| 68 static PassRefPtr<SimpleFontData> create(PassRefPtr<CustomFontData> customDa
ta, float fontSize, bool syntheticBold, bool syntheticItalic) | |
| 69 { | |
| 70 return adoptRef(new SimpleFontData(customData, fontSize, syntheticBold,
syntheticItalic)); | |
| 71 } | |
| 72 | |
| 73 virtual ~SimpleFontData(); | |
| 74 | |
| 75 static const SimpleFontData* systemFallback() { return reinterpret_cast<cons
t SimpleFontData*>(-1); } | |
| 76 | |
| 77 const FontPlatformData& platformData() const { return m_platformData; } | |
| 78 #if ENABLE(OPENTYPE_VERTICAL) | |
| 79 const OpenTypeVerticalData* verticalData() const { return m_verticalData.get
(); } | |
| 80 #endif | |
| 81 | |
| 82 PassRefPtr<SimpleFontData> smallCapsFontData(const FontDescription&) const; | |
| 83 PassRefPtr<SimpleFontData> emphasisMarkFontData(const FontDescription&) cons
t; | |
| 84 PassRefPtr<SimpleFontData> brokenIdeographFontData() const; | |
| 85 | |
| 86 PassRefPtr<SimpleFontData> variantFontData(const FontDescription& descriptio
n, FontDataVariant variant) const | |
| 87 { | |
| 88 switch (variant) { | |
| 89 case SmallCapsVariant: | |
| 90 return smallCapsFontData(description); | |
| 91 case EmphasisMarkVariant: | |
| 92 return emphasisMarkFontData(description); | |
| 93 case BrokenIdeographVariant: | |
| 94 return brokenIdeographFontData(); | |
| 95 case AutoVariant: | |
| 96 case NormalVariant: | |
| 97 break; | |
| 98 } | |
| 99 ASSERT_NOT_REACHED(); | |
| 100 return const_cast<SimpleFontData*>(this); | |
| 101 } | |
| 102 | |
| 103 PassRefPtr<SimpleFontData> verticalRightOrientationFontData() const; | |
| 104 PassRefPtr<SimpleFontData> uprightOrientationFontData() const; | |
| 105 | |
| 106 bool hasVerticalGlyphs() const { return m_hasVerticalGlyphs; } | |
| 107 bool isTextOrientationFallback() const { return m_isTextOrientationFallback;
} | |
| 108 | |
| 109 FontMetrics& fontMetrics() { return m_fontMetrics; } | |
| 110 const FontMetrics& fontMetrics() const { return m_fontMetrics; } | |
| 111 float sizePerUnit() const { return platformData().size() / (fontMetrics().un
itsPerEm() ? fontMetrics().unitsPerEm() : 1); } | |
| 112 | |
| 113 float maxCharWidth() const { return m_maxCharWidth; } | |
| 114 void setMaxCharWidth(float maxCharWidth) { m_maxCharWidth = maxCharWidth; } | |
| 115 | |
| 116 float avgCharWidth() const { return m_avgCharWidth; } | |
| 117 void setAvgCharWidth(float avgCharWidth) { m_avgCharWidth = avgCharWidth; } | |
| 118 | |
| 119 FloatRect boundsForGlyph(Glyph) const; | |
| 120 float widthForGlyph(Glyph glyph) const; | |
| 121 FloatRect platformBoundsForGlyph(Glyph) const; | |
| 122 float platformWidthForGlyph(Glyph) const; | |
| 123 | |
| 124 float spaceWidth() const { return m_spaceWidth; } | |
| 125 float adjustedSpaceWidth() const { return m_adjustedSpaceWidth; } | |
| 126 void setSpaceWidth(float spaceWidth) { m_spaceWidth = spaceWidth; } | |
| 127 | |
| 128 #if OS(MACOSX) | |
| 129 float syntheticBoldOffset() const { return m_syntheticBoldOffset; } | |
| 130 #endif | |
| 131 | |
| 132 Glyph spaceGlyph() const { return m_spaceGlyph; } | |
| 133 void setSpaceGlyph(Glyph spaceGlyph) { m_spaceGlyph = spaceGlyph; } | |
| 134 Glyph zeroWidthSpaceGlyph() const { return m_zeroWidthSpaceGlyph; } | |
| 135 void setZeroWidthSpaceGlyph(Glyph spaceGlyph) { m_zeroWidthSpaceGlyph = spac
eGlyph; } | |
| 136 bool isZeroWidthSpaceGlyph(Glyph glyph) const { return glyph == m_zeroWidthS
paceGlyph && glyph; } | |
| 137 Glyph zeroGlyph() const { return m_zeroGlyph; } | |
| 138 void setZeroGlyph(Glyph zeroGlyph) { m_zeroGlyph = zeroGlyph; } | |
| 139 | |
| 140 virtual const SimpleFontData* fontDataForCharacter(UChar32) const OVERRIDE; | |
| 141 virtual bool containsCharacters(const UChar*, int length) const OVERRIDE; | |
| 142 | |
| 143 Glyph glyphForCharacter(UChar32) const; | |
| 144 | |
| 145 void determinePitch(); | |
| 146 Pitch pitch() const { return m_treatAsFixedPitch ? FixedPitch : VariablePitc
h; } | |
| 147 | |
| 148 bool isSVGFont() const { return m_customFontData && m_customFontData->isSVGF
ont(); } | |
| 149 virtual bool isCustomFont() const OVERRIDE { return m_customFontData; } | |
| 150 virtual bool isLoading() const OVERRIDE { return m_customFontData ? m_custom
FontData->isLoading() : false; } | |
| 151 virtual bool isLoadingFallback() const OVERRIDE { return m_customFontData ?
m_customFontData->isLoadingFallback() : false; } | |
| 152 virtual bool isSegmented() const OVERRIDE; | |
| 153 | |
| 154 const GlyphData& missingGlyphData() const { return m_missingGlyphData; } | |
| 155 void setMissingGlyphData(const GlyphData& glyphData) { m_missingGlyphData =
glyphData; } | |
| 156 | |
| 157 #ifndef NDEBUG | |
| 158 virtual String description() const; | |
| 159 #endif | |
| 160 | |
| 161 #if OS(MACOSX) | |
| 162 const SimpleFontData* getCompositeFontReferenceFontData(NSFont *key) const; | |
| 163 NSFont* getNSFont() const { return m_platformData.font(); } | |
| 164 #endif | |
| 165 | |
| 166 #if OS(MACOSX) | |
| 167 CFDictionaryRef getCFStringAttributes(TypesettingFeatures, FontOrientation)
const; | |
| 168 #endif | |
| 169 | |
| 170 #if OS(MACOSX) || USE(HARFBUZZ) | |
| 171 bool canRenderCombiningCharacterSequence(const UChar*, size_t) const; | |
| 172 #endif | |
| 173 | |
| 174 bool applyTransforms(GlyphBufferGlyph* glyphs, GlyphBufferAdvance* advances,
size_t glyphCount, TypesettingFeatures typesettingFeatures) const | |
| 175 { | |
| 176 UNUSED_PARAM(glyphs); | |
| 177 UNUSED_PARAM(advances); | |
| 178 UNUSED_PARAM(glyphCount); | |
| 179 UNUSED_PARAM(typesettingFeatures); | |
| 180 return false; | |
| 181 } | |
| 182 | |
| 183 PassRefPtr<CustomFontData> customFontData() const { return m_customFontData;
} | |
| 184 | |
| 185 private: | |
| 186 SimpleFontData(const FontPlatformData&, PassRefPtr<CustomFontData> customDat
a, bool isTextOrientationFallback = false); | |
| 187 | |
| 188 SimpleFontData(PassRefPtr<CustomFontData> customData, float fontSize, bool s
yntheticBold, bool syntheticItalic); | |
| 189 | |
| 190 void platformInit(); | |
| 191 void platformGlyphInit(); | |
| 192 void platformCharWidthInit(); | |
| 193 void platformDestroy(); | |
| 194 | |
| 195 void initCharWidths(); | |
| 196 | |
| 197 PassRefPtr<SimpleFontData> createScaledFontData(const FontDescription&, floa
t scaleFactor) const; | |
| 198 PassRefPtr<SimpleFontData> platformCreateScaledFontData(const FontDescriptio
n&, float scaleFactor) const; | |
| 199 | |
| 200 FontMetrics m_fontMetrics; | |
| 201 float m_maxCharWidth; | |
| 202 float m_avgCharWidth; | |
| 203 | |
| 204 FontPlatformData m_platformData; | |
| 205 | |
| 206 mutable OwnPtr<GlyphMetricsMap<FloatRect> > m_glyphToBoundsMap; | |
| 207 mutable GlyphMetricsMap<float> m_glyphToWidthMap; | |
| 208 | |
| 209 bool m_treatAsFixedPitch; | |
| 210 | |
| 211 bool m_isTextOrientationFallback; | |
| 212 bool m_isBrokenIdeographFallback; | |
| 213 #if ENABLE(OPENTYPE_VERTICAL) | |
| 214 RefPtr<OpenTypeVerticalData> m_verticalData; | |
| 215 #endif | |
| 216 bool m_hasVerticalGlyphs; | |
| 217 | |
| 218 Glyph m_spaceGlyph; | |
| 219 float m_spaceWidth; | |
| 220 Glyph m_zeroGlyph; | |
| 221 float m_adjustedSpaceWidth; | |
| 222 | |
| 223 Glyph m_zeroWidthSpaceGlyph; | |
| 224 | |
| 225 GlyphData m_missingGlyphData; | |
| 226 | |
| 227 struct DerivedFontData { | |
| 228 static PassOwnPtr<DerivedFontData> create(bool forCustomFont); | |
| 229 ~DerivedFontData(); | |
| 230 | |
| 231 bool forCustomFont; | |
| 232 RefPtr<SimpleFontData> smallCaps; | |
| 233 RefPtr<SimpleFontData> emphasisMark; | |
| 234 RefPtr<SimpleFontData> brokenIdeograph; | |
| 235 RefPtr<SimpleFontData> verticalRightOrientation; | |
| 236 RefPtr<SimpleFontData> uprightOrientation; | |
| 237 #if OS(MACOSX) | |
| 238 mutable RetainPtr<CFMutableDictionaryRef> compositeFontReferences; | |
| 239 #endif | |
| 240 | |
| 241 private: | |
| 242 DerivedFontData(bool custom) | |
| 243 : forCustomFont(custom) | |
| 244 { | |
| 245 } | |
| 246 }; | |
| 247 | |
| 248 mutable OwnPtr<DerivedFontData> m_derivedFontData; | |
| 249 | |
| 250 RefPtr<CustomFontData> m_customFontData; | |
| 251 | |
| 252 #if OS(MACOSX) | |
| 253 float m_syntheticBoldOffset; | |
| 254 | |
| 255 mutable HashMap<unsigned, RetainPtr<CFDictionaryRef> > m_CFStringAttributes; | |
| 256 #endif | |
| 257 | |
| 258 #if OS(MACOSX) || USE(HARFBUZZ) | |
| 259 mutable OwnPtr<HashMap<String, bool> > m_combiningCharacterSequenceSupport; | |
| 260 #endif | |
| 261 }; | |
| 262 | |
| 263 ALWAYS_INLINE FloatRect SimpleFontData::boundsForGlyph(Glyph glyph) const | |
| 264 { | |
| 265 if (isZeroWidthSpaceGlyph(glyph)) | |
| 266 return FloatRect(); | |
| 267 | |
| 268 FloatRect bounds; | |
| 269 if (m_glyphToBoundsMap) { | |
| 270 bounds = m_glyphToBoundsMap->metricsForGlyph(glyph); | |
| 271 if (bounds.width() != cGlyphSizeUnknown) | |
| 272 return bounds; | |
| 273 } | |
| 274 | |
| 275 bounds = platformBoundsForGlyph(glyph); | |
| 276 if (!m_glyphToBoundsMap) | |
| 277 m_glyphToBoundsMap = adoptPtr(new GlyphMetricsMap<FloatRect>); | |
| 278 m_glyphToBoundsMap->setMetricsForGlyph(glyph, bounds); | |
| 279 return bounds; | |
| 280 } | |
| 281 | |
| 282 ALWAYS_INLINE float SimpleFontData::widthForGlyph(Glyph glyph) const | |
| 283 { | |
| 284 if (isZeroWidthSpaceGlyph(glyph)) | |
| 285 return 0; | |
| 286 | |
| 287 float width = m_glyphToWidthMap.metricsForGlyph(glyph); | |
| 288 if (width != cGlyphSizeUnknown) | |
| 289 return width; | |
| 290 | |
| 291 if (isSVGFont()) | |
| 292 width = m_customFontData->widthForSVGGlyph(glyph, m_platformData.size())
; | |
| 293 #if ENABLE(OPENTYPE_VERTICAL) | |
| 294 else if (m_verticalData) | |
| 295 #if OS(MACOSX) | |
| 296 width = m_verticalData->advanceHeight(this, glyph) + m_syntheticBoldOffs
et; | |
| 297 #else | |
| 298 width = m_verticalData->advanceHeight(this, glyph); | |
| 299 #endif | |
| 300 #endif | |
| 301 else | |
| 302 width = platformWidthForGlyph(glyph); | |
| 303 | |
| 304 m_glyphToWidthMap.setMetricsForGlyph(glyph, width); | |
| 305 return width; | |
| 306 } | |
| 307 | |
| 308 } // namespace WebCore | |
| 309 #endif // SimpleFontData_h | |
| OLD | NEW |