Home | History | Annotate | Download | only in graphics
      1 /*
      2  * Copyright (C) 2000 Lars Knoll (knoll (at) kde.org)
      3  *           (C) 2000 Antti Koivisto (koivisto (at) kde.org)
      4  *           (C) 2000 Dirk Mueller (mueller (at) 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 "CharacterNames.h"
     29 #include "TextRun.h"
     30 #include "FontDescription.h"
     31 #include "SimpleFontData.h"
     32 #include "TypesettingFeatures.h"
     33 #include <wtf/HashMap.h>
     34 #include <wtf/HashSet.h>
     35 #include <wtf/MathExtras.h>
     36 
     37 #if PLATFORM(QT)
     38 #include <QFont>
     39 #endif
     40 
     41 namespace WebCore {
     42 
     43 class FloatPoint;
     44 class FloatRect;
     45 class FontData;
     46 class FontFallbackList;
     47 class FontPlatformData;
     48 class FontSelector;
     49 class GlyphBuffer;
     50 class GlyphPageTreeNode;
     51 class GraphicsContext;
     52 class IntPoint;
     53 class SVGFontElement;
     54 
     55 struct GlyphData;
     56 
     57 const unsigned defaultUnitsPerEm = 1000;
     58 
     59 class Font {
     60 public:
     61     Font();
     62     Font(const FontDescription&, short letterSpacing, short wordSpacing);
     63     // This constructor is only used if the platform wants to start with a native font.
     64     Font(const FontPlatformData&, bool isPrinting);
     65     ~Font();
     66 
     67     Font(const Font&);
     68     Font& operator=(const Font&);
     69 
     70     bool operator==(const Font& other) const;
     71     bool operator!=(const Font& other) const {
     72         return !(*this == other);
     73     }
     74 
     75     const FontDescription& fontDescription() const { return m_fontDescription; }
     76 
     77     int pixelSize() const { return fontDescription().computedPixelSize(); }
     78     float size() const { return fontDescription().computedSize(); }
     79 
     80     void update(PassRefPtr<FontSelector>) const;
     81 
     82     void drawText(GraphicsContext*, const TextRun&, const FloatPoint&, int from = 0, int to = -1) const;
     83 
     84     int width(const TextRun& run, HashSet<const SimpleFontData*>* fallbackFonts = 0) const { return lroundf(floatWidth(run, fallbackFonts)); }
     85     float floatWidth(const TextRun&, HashSet<const SimpleFontData*>* fallbackFonts = 0) const;
     86     float floatWidth(const TextRun& run, int extraCharsAvailable, int& charsConsumed, String& glyphName) const;
     87 
     88     int offsetForPosition(const TextRun&, int position, bool includePartialGlyphs) const;
     89     FloatRect selectionRectForText(const TextRun&, const IntPoint&, int h, int from = 0, int to = -1) const;
     90 
     91     bool isSmallCaps() const { return m_fontDescription.smallCaps(); }
     92 
     93     short wordSpacing() const { return m_wordSpacing; }
     94     short letterSpacing() const { return m_letterSpacing; }
     95     void setWordSpacing(short s) { m_wordSpacing = s; }
     96     void setLetterSpacing(short s) { m_letterSpacing = s; }
     97     bool isFixedPitch() const;
     98     bool isPrinterFont() const { return m_fontDescription.usePrinterFont(); }
     99 
    100     FontRenderingMode renderingMode() const { return m_fontDescription.renderingMode(); }
    101 
    102     TypesettingFeatures typesettingFeatures() const
    103     {
    104         TextRenderingMode textRenderingMode = m_fontDescription.textRenderingMode();
    105         return textRenderingMode == OptimizeLegibility || textRenderingMode == GeometricPrecision ? Kerning | Ligatures : 0;
    106     }
    107 
    108     FontFamily& firstFamily() { return m_fontDescription.firstFamily(); }
    109     const FontFamily& family() const { return m_fontDescription.family(); }
    110 
    111     bool italic() const { return m_fontDescription.italic(); }
    112     FontWeight weight() const { return m_fontDescription.weight(); }
    113 
    114     bool isPlatformFont() const { return m_isPlatformFont; }
    115 
    116     // Metrics that we query the FontFallbackList for.
    117     int ascent() const { return primaryFont()->ascent(); }
    118     int descent() const { return primaryFont()->descent(); }
    119     int height() const { return ascent() + descent(); }
    120     int lineSpacing() const { return primaryFont()->lineSpacing(); }
    121     int lineGap() const { return primaryFont()->lineGap(); }
    122     float xHeight() const { return primaryFont()->xHeight(); }
    123     unsigned unitsPerEm() const { return primaryFont()->unitsPerEm(); }
    124     int spaceWidth() const { return (int)ceilf(primaryFont()->adjustedSpaceWidth() + m_letterSpacing); }
    125     int tabWidth() const { return 8 * spaceWidth(); }
    126 
    127     const SimpleFontData* primaryFont() const;
    128     const FontData* fontDataAt(unsigned) const;
    129     GlyphData glyphDataForCharacter(UChar32, bool mirror, bool forceSmallCaps = false) const;
    130     // Used for complex text, and does not utilize the glyph map cache.
    131     const FontData* fontDataForCharacters(const UChar*, int length) const;
    132 
    133 #if PLATFORM(QT)
    134     QFont font() const;
    135 #endif
    136 
    137     static void setShouldUseSmoothing(bool);
    138     static bool shouldUseSmoothing();
    139 
    140 private:
    141 #if ENABLE(SVG_FONTS)
    142     void drawTextUsingSVGFont(GraphicsContext*, const TextRun&, const FloatPoint&, int from, int to) const;
    143     float floatWidthUsingSVGFont(const TextRun&) const;
    144     float floatWidthUsingSVGFont(const TextRun&, int extraCharsAvailable, int& charsConsumed, String& glyphName) const;
    145     FloatRect selectionRectForTextUsingSVGFont(const TextRun&, const IntPoint&, int h, int from, int to) const;
    146     int offsetForPositionForTextUsingSVGFont(const TextRun&, int position, bool includePartialGlyphs) const;
    147 #endif
    148 
    149 #if USE(FONT_FAST_PATH)
    150     bool canUseGlyphCache(const TextRun&) const;
    151     void drawSimpleText(GraphicsContext*, const TextRun&, const FloatPoint&, int from, int to) const;
    152     void drawGlyphs(GraphicsContext*, const SimpleFontData*, const GlyphBuffer&, int from, int to, const FloatPoint&) const;
    153     void drawGlyphBuffer(GraphicsContext*, const GlyphBuffer&, const TextRun&, const FloatPoint&) const;
    154     float floatWidthForSimpleText(const TextRun&, GlyphBuffer*, HashSet<const SimpleFontData*>* fallbackFonts = 0) const;
    155     int offsetForPositionForSimpleText(const TextRun&, int position, bool includePartialGlyphs) const;
    156     FloatRect selectionRectForSimpleText(const TextRun&, const IntPoint&, int h, int from, int to) const;
    157 
    158     static bool canReturnFallbackFontsForComplexText();
    159 #endif
    160 
    161     void drawComplexText(GraphicsContext*, const TextRun&, const FloatPoint&, int from, int to) const;
    162     float floatWidthForComplexText(const TextRun&, HashSet<const SimpleFontData*>* fallbackFonts = 0) const;
    163     int offsetForPositionForComplexText(const TextRun&, int position, bool includePartialGlyphs) const;
    164     FloatRect selectionRectForComplexText(const TextRun&, const IntPoint&, int h, int from, int to) const;
    165 
    166     friend struct WidthIterator;
    167 
    168 public:
    169     // Useful for debugging the different font rendering code paths.
    170 #if USE(FONT_FAST_PATH)
    171     enum CodePath { Auto, Simple, Complex };
    172     static void setCodePath(CodePath);
    173     static CodePath codePath();
    174     static CodePath s_codePath;
    175 
    176     static const uint8_t gRoundingHackCharacterTable[256];
    177     static bool isRoundingHackCharacter(UChar32 c)
    178     {
    179         return (((c & ~0xFF) == 0 && gRoundingHackCharacterTable[c]));
    180     }
    181 #endif
    182 
    183     FontSelector* fontSelector() const;
    184     static bool treatAsSpace(UChar c) { return c == ' ' || c == '\t' || c == '\n' || c == 0x00A0; }
    185     static bool treatAsZeroWidthSpace(UChar c) { return c < 0x20 || (c >= 0x7F && c < 0xA0) || c == 0x200e || c == 0x200f || (c >= 0x202a && c <= 0x202e) || c == 0xFFFC; }
    186 
    187     static inline UChar normalizeSpaces(UChar character)
    188     {
    189         if (treatAsSpace(character))
    190             return space;
    191 
    192         if (treatAsZeroWidthSpace(character))
    193             return zeroWidthSpace;
    194 
    195         return character;
    196     }
    197 
    198     static String normalizeSpaces(const String&);
    199 
    200 #if ENABLE(SVG_FONTS)
    201     bool isSVGFont() const;
    202     SVGFontElement* svgFont() const;
    203 #endif
    204 
    205 private:
    206     FontDescription m_fontDescription;
    207     mutable RefPtr<FontFallbackList> m_fontList;
    208     short m_letterSpacing;
    209     short m_wordSpacing;
    210     bool m_isPlatformFont;
    211 };
    212 
    213 }
    214 
    215 #endif
    216