1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #ifndef UI_GFX_RENDER_TEXT_HARFBUZZ_H_
6 #define UI_GFX_RENDER_TEXT_HARFBUZZ_H_
8 #include "base/memory/scoped_ptr.h"
9 #include "base/memory/scoped_vector.h"
10 #include "third_party/harfbuzz-ng/src/hb.h"
11 #include "third_party/icu/source/common/unicode/ubidi.h"
12 #include "third_party/icu/source/common/unicode/uscript.h"
13 #include "ui/gfx/render_text.h"
25 struct GFX_EXPORT TextRunHarfBuzz {
29 // Returns the index of the first glyph that corresponds to the character at
31 size_t CharToGlyph(size_t pos) const;
33 // Returns the corresponding glyph range of the given character range.
34 // |range| is in text-space (0 corresponds to |GetLayoutText()[0]|). Returned
35 // value is in run-space (0 corresponds to the first glyph in the run).
36 Range CharRangeToGlyphRange(const Range& range) const;
38 // Returns the number of missing glyphs in the shaped text run.
39 size_t CountMissingGlyphs() const;
41 // Writes the character and glyph ranges of the cluster containing |pos|.
42 void GetClusterAt(size_t pos, Range* chars, Range* glyphs) const;
44 // Returns the grapheme bounds at |text_index|. Handles multi-grapheme glyphs.
45 Range GetGraphemeBounds(base::i18n::BreakIterator* grapheme_iterator,
48 // Returns whether the given shaped run contains any missing glyphs.
49 bool HasMissingGlyphs() const;
52 float preceding_run_widths;
58 scoped_ptr<uint16[]> glyphs;
59 scoped_ptr<SkPoint[]> positions;
60 std::vector<uint32> glyph_to_char;
63 skia::RefPtr<SkTypeface> skia_face;
71 DISALLOW_COPY_AND_ASSIGN(TextRunHarfBuzz);
74 } // namespace internal
76 class GFX_EXPORT RenderTextHarfBuzz : public RenderText {
79 virtual ~RenderTextHarfBuzz();
81 // Overridden from RenderText.
82 virtual Size GetStringSize() OVERRIDE;
83 virtual SizeF GetStringSizeF() OVERRIDE;
84 virtual SelectionModel FindCursorPosition(const Point& point) OVERRIDE;
85 virtual std::vector<FontSpan> GetFontSpansForTesting() OVERRIDE;
86 virtual Range GetGlyphBounds(size_t index) OVERRIDE;
89 // Overridden from RenderText.
90 virtual int GetLayoutTextBaseline() OVERRIDE;
91 virtual SelectionModel AdjacentCharSelectionModel(
92 const SelectionModel& selection,
93 VisualCursorDirection direction) OVERRIDE;
94 virtual SelectionModel AdjacentWordSelectionModel(
95 const SelectionModel& selection,
96 VisualCursorDirection direction) OVERRIDE;
97 virtual std::vector<Rect> GetSubstringBounds(const Range& range) OVERRIDE;
98 virtual size_t TextIndexToLayoutIndex(size_t index) const OVERRIDE;
99 virtual size_t LayoutIndexToTextIndex(size_t index) const OVERRIDE;
100 virtual bool IsValidCursorIndex(size_t index) OVERRIDE;
101 virtual void ResetLayout() OVERRIDE;
102 virtual void EnsureLayout() OVERRIDE;
103 virtual void DrawVisualText(Canvas* canvas) OVERRIDE;
106 friend class RenderTextTest;
107 FRIEND_TEST_ALL_PREFIXES(RenderTextTest, HarfBuzz_RunDirection);
108 FRIEND_TEST_ALL_PREFIXES(RenderTextTest, HarfBuzz_BreakRunsByUnicodeBlocks);
109 FRIEND_TEST_ALL_PREFIXES(RenderTextTest, HarfBuzz_SubglyphGraphemeCases);
110 FRIEND_TEST_ALL_PREFIXES(RenderTextTest, HarfBuzz_SubglyphGraphemePartition);
111 FRIEND_TEST_ALL_PREFIXES(RenderTextTest, HarfBuzz_NonExistentFont);
113 // Return the run index that contains the argument; or the length of the
114 // |runs_| vector if argument exceeds the text length or width.
115 size_t GetRunContainingCaret(const SelectionModel& caret) const;
116 size_t GetRunContainingXCoord(int x, int* offset) const;
118 // Given a |run|, returns the SelectionModel that contains the logical first
119 // or last caret position inside (not at a boundary of) the run.
120 // The returned value represents a cursor/caret position without a selection.
121 SelectionModel FirstSelectionModelInsideRun(
122 const internal::TextRunHarfBuzz* run);
123 SelectionModel LastSelectionModelInsideRun(
124 const internal::TextRunHarfBuzz* run);
126 // Break the text into logical runs and populate the visual <-> logical maps.
129 // Shape the glyphs needed for the text |run|.
130 void ShapeRun(internal::TextRunHarfBuzz* run);
131 bool ShapeRunWithFont(internal::TextRunHarfBuzz* run,
132 const std::string& font);
134 // Text runs in logical order.
135 ScopedVector<internal::TextRunHarfBuzz> runs_;
137 // Maps visual run indices to logical run indices and vice versa.
138 std::vector<int32_t> visual_to_logical_;
139 std::vector<int32_t> logical_to_visual_;
143 // ICU grapheme iterator for the layout text. Valid when |!needs_layout_|. Can
144 // be NULL in case of an error.
145 scoped_ptr<base::i18n::BreakIterator> grapheme_iterator_;
147 DISALLOW_COPY_AND_ASSIGN(RenderTextHarfBuzz);
152 #endif // UI_GFX_RENDER_TEXT_HARFBUZZ_H_