[Android WebView] Fix webview perf bot switchover to use org.chromium.webview_shell...
[chromium-blink-merge.git] / ui / gfx / render_text_harfbuzz.h
blobe8b28e430e4a1c5dc7f3a7fab2709fb6573f8ab5
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"
15 namespace base {
16 namespace i18n {
17 class BreakIterator;
21 namespace gfx {
23 class Range;
24 class RangeF;
26 namespace internal {
28 // Applies std::round to the start and end values of the given RangeF.
29 GFX_EXPORT Range RoundRangeF(const RangeF& range_f);
31 struct GFX_EXPORT TextRunHarfBuzz {
32 TextRunHarfBuzz();
33 ~TextRunHarfBuzz();
35 // Returns the index of the first glyph that corresponds to the character at
36 // |pos|.
37 size_t CharToGlyph(size_t pos) const;
39 // Returns the corresponding glyph range of the given character range.
40 // |range| is in text-space (0 corresponds to |GetDisplayText()[0]|). Returned
41 // value is in run-space (0 corresponds to the first glyph in the run).
42 Range CharRangeToGlyphRange(const Range& range) const;
44 // Returns the number of missing glyphs in the shaped text run.
45 size_t CountMissingGlyphs() const;
47 // Writes the character and glyph ranges of the cluster containing |pos|.
48 void GetClusterAt(size_t pos, Range* chars, Range* glyphs) const;
50 // Returns the grapheme bounds at |text_index|. Handles multi-grapheme glyphs.
51 RangeF GetGraphemeBounds(base::i18n::BreakIterator* grapheme_iterator,
52 size_t text_index);
54 // Returns whether the given shaped run contains any missing glyphs.
55 bool HasMissingGlyphs() const;
57 // Returns the glyph width for the given character range. |char_range| is in
58 // text-space (0 corresponds to |GetDisplayText()[0]|).
59 SkScalar GetGlyphWidthForCharRange(const Range& char_range) const;
61 float width;
62 float preceding_run_widths;
63 Range range;
64 bool is_rtl;
65 UBiDiLevel level;
66 UScriptCode script;
68 scoped_ptr<uint16[]> glyphs;
69 scoped_ptr<SkPoint[]> positions;
70 std::vector<uint32> glyph_to_char;
71 size_t glyph_count;
73 std::string family;
74 skia::RefPtr<SkTypeface> skia_face;
75 FontRenderParams render_params;
76 int font_size;
77 int baseline_offset;
78 int baseline_type;
79 int font_style;
80 bool strike;
81 bool diagonal_strike;
82 bool underline;
84 private:
85 DISALLOW_COPY_AND_ASSIGN(TextRunHarfBuzz);
88 // Manages the list of TextRunHarfBuzz and its logical <-> visual index mapping.
89 class TextRunList {
90 public:
91 TextRunList();
92 ~TextRunList();
94 size_t size() const { return runs_.size(); }
96 // Converts the index between logical and visual index.
97 size_t visual_to_logical(size_t index) const {
98 return visual_to_logical_[index];
100 size_t logical_to_visual(size_t index) const {
101 return logical_to_visual_[index];
104 const ScopedVector<TextRunHarfBuzz>& runs() const { return runs_; }
106 // Adds the new |run| to the run list.
107 void add(TextRunHarfBuzz* run) { runs_.push_back(run); }
109 // Reset the run list.
110 void Reset();
112 // Initialize the index mapping.
113 void InitIndexMap();
115 // Precomputes the offsets for all runs.
116 void ComputePrecedingRunWidths();
118 // Get the total width of runs, as if they were shown on one line.
119 // Do not use this when multiline is enabled.
120 float width() const { return width_; }
122 // Get the run index applicable to |position| (at or preceeding |position|).
123 size_t GetRunIndexAt(size_t position) const;
125 private:
126 // Text runs in logical order.
127 ScopedVector<TextRunHarfBuzz> runs_;
129 // Maps visual run indices to logical run indices and vice versa.
130 std::vector<int32_t> visual_to_logical_;
131 std::vector<int32_t> logical_to_visual_;
133 float width_;
135 DISALLOW_COPY_AND_ASSIGN(TextRunList);
138 } // namespace internal
140 class GFX_EXPORT RenderTextHarfBuzz : public RenderText {
141 public:
142 RenderTextHarfBuzz();
143 ~RenderTextHarfBuzz() override;
145 // RenderText:
146 scoped_ptr<RenderText> CreateInstanceOfSameType() const override;
147 bool MultilineSupported() const override;
148 const base::string16& GetDisplayText() override;
149 Size GetStringSize() override;
150 SizeF GetStringSizeF() override;
151 SelectionModel FindCursorPosition(const Point& point) override;
152 std::vector<FontSpan> GetFontSpansForTesting() override;
153 Range GetGlyphBounds(size_t index) override;
155 protected:
156 // RenderText:
157 int GetDisplayTextBaseline() override;
158 SelectionModel AdjacentCharSelectionModel(
159 const SelectionModel& selection,
160 VisualCursorDirection direction) override;
161 SelectionModel AdjacentWordSelectionModel(
162 const SelectionModel& selection,
163 VisualCursorDirection direction) override;
164 std::vector<Rect> GetSubstringBounds(const Range& range) override;
165 size_t TextIndexToDisplayIndex(size_t index) override;
166 size_t DisplayIndexToTextIndex(size_t index) override;
167 bool IsValidCursorIndex(size_t index) override;
168 void OnLayoutTextAttributeChanged(bool text_changed) override;
169 void OnDisplayTextAttributeChanged() override;
170 void EnsureLayout() override;
171 void DrawVisualText(Canvas* canvas) override;
173 private:
174 friend class RenderTextTest;
175 FRIEND_TEST_ALL_PREFIXES(RenderTextTest, Multiline_HorizontalAlignment);
176 FRIEND_TEST_ALL_PREFIXES(RenderTextTest, Multiline_NormalWidth);
177 FRIEND_TEST_ALL_PREFIXES(RenderTextTest, Multiline_WordWrapBehavior);
178 FRIEND_TEST_ALL_PREFIXES(RenderTextTest, HarfBuzz_RunDirection);
179 FRIEND_TEST_ALL_PREFIXES(RenderTextTest, HarfBuzz_HorizontalPositions);
180 FRIEND_TEST_ALL_PREFIXES(RenderTextTest,
181 HarfBuzz_TextPositionWithFractionalSize);
182 FRIEND_TEST_ALL_PREFIXES(RenderTextTest, HarfBuzz_BreakRunsByUnicodeBlocks);
183 FRIEND_TEST_ALL_PREFIXES(RenderTextTest, HarfBuzz_BreakRunsByEmoji);
184 FRIEND_TEST_ALL_PREFIXES(RenderTextTest, HarfBuzz_SubglyphGraphemeCases);
185 FRIEND_TEST_ALL_PREFIXES(RenderTextTest, HarfBuzz_SubglyphGraphemePartition);
186 FRIEND_TEST_ALL_PREFIXES(RenderTextTest, HarfBuzz_NonExistentFont);
187 FRIEND_TEST_ALL_PREFIXES(RenderTextTest, HarfBuzz_UniscribeFallback);
188 FRIEND_TEST_ALL_PREFIXES(RenderTextTest, HarfBuzz_UnicodeFallback);
189 FRIEND_TEST_ALL_PREFIXES(RenderTextTest, Multiline_LineBreakerBehavior);
190 FRIEND_TEST_ALL_PREFIXES(RenderTextTest,
191 Multiline_SurrogatePairsOrCombiningChars);
192 FRIEND_TEST_ALL_PREFIXES(RenderTextTest, Multiline_ZeroWidthChars);
194 // Specify the width of a glyph for test. The width of glyphs is very
195 // platform-dependent and environment-dependent. Otherwise multiline test
196 // will become really flaky.
197 void set_glyph_width_for_test(float test_width) {
198 glyph_width_for_test_ = test_width;
201 // The actual implementation of the text drawing.
202 void DrawVisualTextInternal(internal::SkiaTextRenderer* renderer);
204 // Return the run index that contains the argument; or the length of the
205 // |runs_| vector if argument exceeds the text length or width.
206 size_t GetRunContainingCaret(const SelectionModel& caret);
207 size_t GetRunContainingXCoord(float x, float* offset) const;
209 // Given a |run|, returns the SelectionModel that contains the logical first
210 // or last caret position inside (not at a boundary of) the run.
211 // The returned value represents a cursor/caret position without a selection.
212 SelectionModel FirstSelectionModelInsideRun(
213 const internal::TextRunHarfBuzz* run);
214 SelectionModel LastSelectionModelInsideRun(
215 const internal::TextRunHarfBuzz* run);
217 // Break the text into logical runs and populate the visual <-> logical maps
218 // into |run_list_out|.
219 void ItemizeTextToRuns(const base::string16& string,
220 internal::TextRunList* run_list_out);
222 // Helper method for ShapeRun() that calls ShapeRunWithFont() with |text|,
223 // |run|, |family|, and |render_params|, returning true if the family provides
224 // all the glyphs needed for |run|, and false otherwise. Additionally updates
225 // |best_family|, |best_render_params|, and |best_missing_glyphs| if |family|
226 // has fewer than |best_missing_glyphs| missing glyphs.
227 bool CompareFamily(const base::string16& text,
228 const std::string& family,
229 const gfx::FontRenderParams& render_params,
230 internal::TextRunHarfBuzz* run,
231 std::string* best_family,
232 gfx::FontRenderParams* best_render_params,
233 size_t* best_missing_glyphs);
235 // Shape the glyphs of all runs in |run_list| using |text|.
236 void ShapeRunList(const base::string16& text,
237 internal::TextRunList* run_list);
239 // Shape the glyphs needed for the |run| within the |text|.
240 void ShapeRun(const base::string16& text,
241 internal::TextRunHarfBuzz* run);
242 bool ShapeRunWithFont(const base::string16& text,
243 const std::string& font_family,
244 const FontRenderParams& params,
245 internal::TextRunHarfBuzz* run);
247 // Makes sure that text runs for layout text are shaped.
248 void EnsureLayoutRunList();
250 // ICU grapheme iterator for the layout text. Can be NULL in case of an error.
251 base::i18n::BreakIterator* GetGraphemeIterator();
253 // Returns the current run list, |display_run_list_| if the text is
254 // elided, or |layout_run_list_| otherwise.
255 internal::TextRunList* GetRunList();
256 const internal::TextRunList* GetRunList() const;
258 // Text run list for |layout_text_| and |display_text_|.
259 // |display_run_list_| is created only when the text is elided.
260 internal::TextRunList layout_run_list_;
261 scoped_ptr<internal::TextRunList> display_run_list_;
263 bool update_layout_run_list_ : 1;
264 bool update_display_run_list_ : 1;
265 bool update_grapheme_iterator_ : 1;
266 bool update_display_text_ : 1;
268 // ICU grapheme iterator for the layout text. Use GetGraphemeIterator()
269 // to access the iterator.
270 scoped_ptr<base::i18n::BreakIterator> grapheme_iterator_;
272 // The total size of the layouted text.
273 SizeF total_size_;
275 // Fixed width of glyphs. This should only be set in test environments.
276 float glyph_width_for_test_;
278 DISALLOW_COPY_AND_ASSIGN(RenderTextHarfBuzz);
281 } // namespace gfx
283 #endif // UI_GFX_RENDER_TEXT_HARFBUZZ_H_