Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / third_party / WebKit / Source / platform / fonts / Font.h
blob8885e647041d92a5ce7e8996f004c7a00778a3f9
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, 2010, 2011 Apple Inc. All rights reserved.
6 * Copyright (C) 2008 Holger Hans Peter Freyther
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.
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.
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.
25 #ifndef Font_h
26 #define Font_h
28 #include "platform/LayoutUnit.h"
29 #include "platform/PlatformExport.h"
30 #include "platform/fonts/FontDescription.h"
31 #include "platform/fonts/FontFallbackList.h"
32 #include "platform/fonts/SimpleFontData.h"
33 #include "platform/fonts/TextBlob.h"
34 #include "platform/text/TabSize.h"
35 #include "platform/text/TextDirection.h"
36 #include "platform/text/TextPath.h"
37 #include "wtf/HashMap.h"
38 #include "wtf/HashSet.h"
39 #include "wtf/MathExtras.h"
40 #include "wtf/text/CharacterNames.h"
42 class SkCanvas;
43 class SkPaint;
44 class SkTextBlob;
45 struct SkPoint;
47 namespace blink {
49 class FloatPoint;
50 class FloatRect;
51 class FontData;
52 class FontMetrics;
53 class FontSelector;
54 class GlyphBuffer;
55 class TextRun;
56 struct TextRunPaintInfo;
58 struct GlyphData;
60 class PLATFORM_EXPORT Font {
61 public:
62 Font();
63 Font(const FontDescription&);
64 ~Font();
66 Font(const Font&);
67 Font& operator=(const Font&);
69 bool operator==(const Font& other) const;
70 bool operator!=(const Font& other) const { return !(*this == other); }
72 const FontDescription& fontDescription() const { return m_fontDescription; }
74 void update(PassRefPtrWillBeRawPtr<FontSelector>) const;
76 enum CustomFontNotReadyAction { DoNotPaintIfFontNotReady, UseFallbackIfFontNotReady };
77 void drawText(SkCanvas*, const TextRunPaintInfo&, const FloatPoint&, float deviceScaleFactor, const SkPaint&) const;
78 void drawBidiText(SkCanvas*, const TextRunPaintInfo&, const FloatPoint&, CustomFontNotReadyAction, float deviceScaleFactor, const SkPaint&) const;
79 void drawEmphasisMarks(SkCanvas*, const TextRunPaintInfo&, const AtomicString& mark, const FloatPoint&, float deviceScaleFactor, const SkPaint&) const;
81 // Glyph bounds will be the minimum rect containing all glyph strokes, in coordinates using
82 // (<text run x position>, <baseline position>) as the origin.
83 float width(const TextRun&, HashSet<const SimpleFontData*>* fallbackFonts = nullptr, FloatRect* glyphBounds = nullptr) const;
85 int offsetForPosition(const TextRun&, float position, bool includePartialGlyphs) const;
86 FloatRect selectionRectForText(const TextRun&, const FloatPoint&, int h, int from = 0, int to = -1, bool accountForGlyphBounds = false) const;
88 // Metrics that we query the FontFallbackList for.
89 const FontMetrics& fontMetrics() const { return primaryFont()->fontMetrics(); }
90 float spaceWidth() const { return primaryFont()->spaceWidth() + fontDescription().letterSpacing(); }
91 float tabWidth(const SimpleFontData&, const TabSize&, float position) const;
92 float tabWidth(const TabSize& tabSize, float position) const { return tabWidth(*primaryFont(), tabSize, position); }
94 int emphasisMarkAscent(const AtomicString&) const;
95 int emphasisMarkDescent(const AtomicString&) const;
96 int emphasisMarkHeight(const AtomicString&) const;
98 const SimpleFontData* primaryFont() const;
99 const FontData* fontDataAt(unsigned) const;
101 GlyphData glyphDataForCharacter(UChar32&, bool mirror, bool normalizeSpace = false, FontDataVariant = AutoVariant) const;
102 CodePath codePath(const TextRunPaintInfo&) const;
104 // Whether the font supports shaping word by word instead of shaping the
105 // full run in one go. Allows better caching for fonts where space cannot
106 // participate in kerning and/or ligatures.
107 bool canShapeWordByWord() const;
109 void setCanShapeWordByWordForTesting(bool b)
111 m_canShapeWordByWord = b;
112 m_shapeWordByWordComputed = true;
115 private:
116 enum ForTextEmphasisOrNot { NotForTextEmphasis, ForTextEmphasis };
118 // Returns the total advance.
119 float buildGlyphBuffer(const TextRunPaintInfo&, GlyphBuffer&, const GlyphData* emphasisData = nullptr) const;
120 PassTextBlobPtr buildTextBlob(const GlyphBuffer&) const;
121 void paintGlyphs(SkCanvas*, const SkPaint&, const SimpleFontData*, const Glyph glyphs[], unsigned numGlyphs,
122 const SkPoint pos[], const FloatRect& textRect, float deviceScaleFactor) const;
123 void paintGlyphsHorizontal(SkCanvas*, const SkPaint&, const SimpleFontData*, const Glyph glyphs[], unsigned numGlyphs,
124 const SkScalar xpos[], SkScalar constY, const FloatRect& textRect, float deviceScaleFactor) const;
125 void drawGlyphs(SkCanvas*, const SkPaint&, const SimpleFontData*, const GlyphBuffer&, unsigned from, unsigned numGlyphs,
126 const FloatPoint&, const FloatRect& textRect, float deviceScaleFactor) const;
127 void drawTextBlob(SkCanvas*, const SkPaint&, const SkTextBlob*, const SkPoint& origin) const;
128 void drawGlyphBuffer(SkCanvas*, const SkPaint&, const TextRunPaintInfo&, const GlyphBuffer&, const FloatPoint&, float deviceScaleFactor) const;
129 float floatWidthForSimpleText(const TextRun&, HashSet<const SimpleFontData*>* fallbackFonts = 0, FloatRect* glyphBounds = 0) const;
130 int offsetForPositionForSimpleText(const TextRun&, float position, bool includePartialGlyphs) const;
131 FloatRect selectionRectForSimpleText(const TextRun&, const FloatPoint&, int h, int from, int to, bool accountForGlyphBounds) const;
133 bool getEmphasisMarkGlyphData(const AtomicString&, GlyphData&) const;
135 float floatWidthForComplexText(const TextRun&, HashSet<const SimpleFontData*>* fallbackFonts, FloatRect* glyphBounds) const;
136 int offsetForPositionForComplexText(const TextRun&, float position, bool includePartialGlyphs) const;
137 FloatRect selectionRectForComplexText(const TextRun&, const FloatPoint&, int h, int from, int to) const;
139 bool computeCanShapeWordByWord() const;
141 friend struct SimpleShaper;
143 public:
144 FontSelector* fontSelector() const;
146 void willUseFontData(UChar32) const;
148 bool loadingCustomFonts() const;
149 bool isFallbackValid() const;
151 private:
152 bool shouldSkipDrawing() const
154 return m_fontFallbackList && m_fontFallbackList->shouldSkipDrawing();
157 FontDescription m_fontDescription;
158 mutable RefPtr<FontFallbackList> m_fontFallbackList;
159 mutable unsigned m_canShapeWordByWord : 1;
160 mutable unsigned m_shapeWordByWordComputed : 1;
163 inline Font::~Font()
167 inline const SimpleFontData* Font::primaryFont() const
169 ASSERT(m_fontFallbackList);
170 return m_fontFallbackList->primarySimpleFontData(m_fontDescription);
173 inline const FontData* Font::fontDataAt(unsigned index) const
175 ASSERT(m_fontFallbackList);
176 return m_fontFallbackList->fontDataAt(m_fontDescription, index);
179 inline FontSelector* Font::fontSelector() const
181 return m_fontFallbackList ? m_fontFallbackList->fontSelector() : 0;
184 inline float Font::tabWidth(const SimpleFontData& fontData, const TabSize& tabSize, float position) const
186 float baseTabWidth = tabSize.getPixelSize(fontData.spaceWidth());
187 if (!baseTabWidth)
188 return fontDescription().letterSpacing();
189 float distanceToTabStop = baseTabWidth - fmodf(position, baseTabWidth);
191 // The smallest allowable tab space is letterSpacing() (but must be at least one layout unit).
192 // if the distance to the next tab stop is less than that, advance an additional tab stop.
193 if (distanceToTabStop < std::max(fontDescription().letterSpacing(), LayoutUnit::epsilon()))
194 distanceToTabStop += baseTabWidth;
196 return distanceToTabStop;
199 } // namespace blink
201 #endif