1 /* 2 * Copyright (C) 2000 Lars Knoll (knoll@kde.org) 3 * (C) 2000 Antti Koivisto (koivisto@kde.org) 4 * (C) 2000 Dirk Mueller (mueller@kde.org) 5 * Copyright (C) 2003, 2006, 2007 Apple Computer, Inc. 6 * Copyright (C) 2008 Holger Hans Peter Freyther 7 * 8 * This library is free software; you can redistribute it and/or 9 * modify it under the terms of the GNU Library General Public 10 * License as published by the Free Software Foundation; either 11 * version 2 of the License, or (at your option) any later version. 12 * 13 * This library is distributed in the hope that it will be useful, 14 * but WITHOUT ANY WARRANTY; without even the implied warranty of 15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 16 * Library General Public License for more details. 17 * 18 * You should have received a copy of the GNU Library General Public License 19 * along with this library; see the file COPYING.LIB. If not, write to 20 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 21 * Boston, MA 02110-1301, USA. 22 * 23 */ 24 25 #ifndef Font_h 26 #define Font_h 27 28 #include "TextRun.h" 29 #include "FontDescription.h" 30 #include "SimpleFontData.h" 31 #include <wtf/HashMap.h> 32 #include <wtf/MathExtras.h> 33 34 #if PLATFORM(QT) 35 #include <QFont> 36 #endif 37 38 namespace WebCore { 39 40 class FloatPoint; 41 class FloatRect; 42 class FontData; 43 class FontFallbackList; 44 class FontPlatformData; 45 class FontSelector; 46 class GlyphBuffer; 47 class GlyphPageTreeNode; 48 class GraphicsContext; 49 class IntPoint; 50 class SVGFontElement; 51 52 struct GlyphData; 53 54 const unsigned defaultUnitsPerEm = 1000; 55 56 class Font { 57 public: 58 Font(); 59 Font(const FontDescription&, short letterSpacing, short wordSpacing); 60 // This constructor is only used if the platform wants to start with a native font. 61 Font(const FontPlatformData&, bool isPrinting); 62 ~Font(); 63 64 Font(const Font&); 65 Font& operator=(const Font&); 66 67 bool operator==(const Font& other) const; 68 bool operator!=(const Font& other) const { 69 return !(*this == other); 70 } 71 fontDescription()72 const FontDescription& fontDescription() const { return m_fontDescription; } 73 pixelSize()74 int pixelSize() const { return fontDescription().computedPixelSize(); } size()75 float size() const { return fontDescription().computedSize(); } 76 77 void update(PassRefPtr<FontSelector>) const; 78 79 void drawText(GraphicsContext*, const TextRun&, const FloatPoint&, int from = 0, int to = -1) const; 80 81 int width(const TextRun& run, HashSet<const SimpleFontData*>* fallbackFonts = 0) const { return lroundf(floatWidth(run, fallbackFonts)); } 82 float floatWidth(const TextRun&, HashSet<const SimpleFontData*>* fallbackFonts = 0) const; 83 float floatWidth(const TextRun& run, int extraCharsAvailable, int& charsConsumed, String& glyphName) const; 84 85 int offsetForPosition(const TextRun&, int position, bool includePartialGlyphs) const; 86 FloatRect selectionRectForText(const TextRun&, const IntPoint&, int h, int from = 0, int to = -1) const; 87 isSmallCaps()88 bool isSmallCaps() const { return m_fontDescription.smallCaps(); } 89 wordSpacing()90 short wordSpacing() const { return m_wordSpacing; } letterSpacing()91 short letterSpacing() const { return m_letterSpacing; } setWordSpacing(short s)92 void setWordSpacing(short s) { m_wordSpacing = s; } setLetterSpacing(short s)93 void setLetterSpacing(short s) { m_letterSpacing = s; } 94 bool isFixedPitch() const; isPrinterFont()95 bool isPrinterFont() const { return m_fontDescription.usePrinterFont(); } 96 renderingMode()97 FontRenderingMode renderingMode() const { return m_fontDescription.renderingMode(); } 98 firstFamily()99 FontFamily& firstFamily() { return m_fontDescription.firstFamily(); } family()100 const FontFamily& family() const { return m_fontDescription.family(); } 101 italic()102 bool italic() const { return m_fontDescription.italic(); } weight()103 FontWeight weight() const { return m_fontDescription.weight(); } 104 isPlatformFont()105 bool isPlatformFont() const { return m_isPlatformFont; } 106 107 // Metrics that we query the FontFallbackList for. ascent()108 int ascent() const { return primaryFont()->ascent(); } descent()109 int descent() const { return primaryFont()->descent(); } height()110 int height() const { return ascent() + descent(); } lineSpacing()111 int lineSpacing() const { return primaryFont()->lineSpacing(); } lineGap()112 int lineGap() const { return primaryFont()->lineGap(); } xHeight()113 float xHeight() const { return primaryFont()->xHeight(); } unitsPerEm()114 unsigned unitsPerEm() const { return primaryFont()->unitsPerEm(); } spaceWidth()115 int spaceWidth() const { return (int)ceilf(primaryFont()->adjustedSpaceWidth() + m_letterSpacing); } tabWidth()116 int tabWidth() const { return 8 * spaceWidth(); } 117 118 const SimpleFontData* primaryFont() const; 119 const FontData* fontDataAt(unsigned) const; 120 GlyphData glyphDataForCharacter(UChar32, bool mirror, bool forceSmallCaps = false) const; 121 // Used for complex text, and does not utilize the glyph map cache. 122 const FontData* fontDataForCharacters(const UChar*, int length) const; 123 124 #if PLATFORM(QT) 125 QFont font() const; 126 #endif 127 128 static void setShouldUseSmoothing(bool); 129 static bool shouldUseSmoothing(); 130 131 private: 132 #if ENABLE(SVG_FONTS) 133 void drawTextUsingSVGFont(GraphicsContext*, const TextRun&, const FloatPoint&, int from, int to) const; 134 float floatWidthUsingSVGFont(const TextRun&) const; 135 float floatWidthUsingSVGFont(const TextRun&, int extraCharsAvailable, int& charsConsumed, String& glyphName) const; 136 FloatRect selectionRectForTextUsingSVGFont(const TextRun&, const IntPoint&, int h, int from, int to) const; 137 int offsetForPositionForTextUsingSVGFont(const TextRun&, int position, bool includePartialGlyphs) const; 138 #endif 139 140 #if USE(FONT_FAST_PATH) 141 bool canUseGlyphCache(const TextRun&) const; 142 void drawSimpleText(GraphicsContext*, const TextRun&, const FloatPoint&, int from, int to) const; 143 void drawGlyphs(GraphicsContext*, const SimpleFontData*, const GlyphBuffer&, int from, int to, const FloatPoint&) const; 144 void drawGlyphBuffer(GraphicsContext*, const GlyphBuffer&, const TextRun&, const FloatPoint&) const; 145 float floatWidthForSimpleText(const TextRun&, GlyphBuffer*, HashSet<const SimpleFontData*>* fallbackFonts = 0) const; 146 int offsetForPositionForSimpleText(const TextRun&, int position, bool includePartialGlyphs) const; 147 FloatRect selectionRectForSimpleText(const TextRun&, const IntPoint&, int h, int from, int to) const; 148 149 static bool canReturnFallbackFontsForComplexText(); 150 #endif 151 152 void drawComplexText(GraphicsContext*, const TextRun&, const FloatPoint&, int from, int to) const; 153 float floatWidthForComplexText(const TextRun&, HashSet<const SimpleFontData*>* fallbackFonts = 0) const; 154 int offsetForPositionForComplexText(const TextRun&, int position, bool includePartialGlyphs) const; 155 FloatRect selectionRectForComplexText(const TextRun&, const IntPoint&, int h, int from, int to) const; 156 157 friend struct WidthIterator; 158 159 public: 160 // Useful for debugging the different font rendering code paths. 161 #if USE(FONT_FAST_PATH) 162 enum CodePath { Auto, Simple, Complex }; 163 static void setCodePath(CodePath); 164 static CodePath codePath(); 165 static CodePath s_codePath; 166 167 static const uint8_t gRoundingHackCharacterTable[256]; isRoundingHackCharacter(UChar32 c)168 static bool isRoundingHackCharacter(UChar32 c) 169 { 170 return (((c & ~0xFF) == 0 && gRoundingHackCharacterTable[c])); 171 } 172 #endif 173 174 FontSelector* fontSelector() const; treatAsSpace(UChar c)175 static bool treatAsSpace(UChar c) { return c == ' ' || c == '\t' || c == '\n' || c == 0x00A0; } treatAsZeroWidthSpace(UChar c)176 static bool treatAsZeroWidthSpace(UChar c) { return c < 0x20 || (c >= 0x7F && c < 0xA0) || c == 0x200e || c == 0x200f || (c >= 0x202a && c <= 0x202e) || c == 0xFFFC; } 177 178 #if ENABLE(SVG_FONTS) 179 bool isSVGFont() const; 180 SVGFontElement* svgFont() const; 181 #endif 182 183 private: 184 FontDescription m_fontDescription; 185 mutable RefPtr<FontFallbackList> m_fontList; 186 short m_letterSpacing; 187 short m_wordSpacing; 188 bool m_isPlatformFont; 189 }; 190 191 } 192 193 #endif 194