Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(63)

Side by Side Diff: ui/gfx/render_text_harfbuzz.h

Issue 916203002: Reduce the number of text reshaping in RenderText (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 5 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #ifndef UI_GFX_RENDER_TEXT_HARFBUZZ_H_ 5 #ifndef UI_GFX_RENDER_TEXT_HARFBUZZ_H_
6 #define UI_GFX_RENDER_TEXT_HARFBUZZ_H_ 6 #define UI_GFX_RENDER_TEXT_HARFBUZZ_H_
7 7
8 #include "base/memory/scoped_ptr.h" 8 #include "base/memory/scoped_ptr.h"
9 #include "base/memory/scoped_vector.h" 9 #include "base/memory/scoped_vector.h"
10 #include "third_party/harfbuzz-ng/src/hb.h" 10 #include "third_party/harfbuzz-ng/src/hb.h"
(...skipping 19 matching lines...) Expand all
30 30
31 struct GFX_EXPORT TextRunHarfBuzz { 31 struct GFX_EXPORT TextRunHarfBuzz {
32 TextRunHarfBuzz(); 32 TextRunHarfBuzz();
33 ~TextRunHarfBuzz(); 33 ~TextRunHarfBuzz();
34 34
35 // Returns the index of the first glyph that corresponds to the character at 35 // Returns the index of the first glyph that corresponds to the character at
36 // |pos|. 36 // |pos|.
37 size_t CharToGlyph(size_t pos) const; 37 size_t CharToGlyph(size_t pos) const;
38 38
39 // Returns the corresponding glyph range of the given character range. 39 // Returns the corresponding glyph range of the given character range.
40 // |range| is in text-space (0 corresponds to |GetLayoutText()[0]|). Returned 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). 41 // value is in run-space (0 corresponds to the first glyph in the run).
42 Range CharRangeToGlyphRange(const Range& range) const; 42 Range CharRangeToGlyphRange(const Range& range) const;
43 43
44 // Returns the number of missing glyphs in the shaped text run. 44 // Returns the number of missing glyphs in the shaped text run.
45 size_t CountMissingGlyphs() const; 45 size_t CountMissingGlyphs() const;
46 46
47 // Writes the character and glyph ranges of the cluster containing |pos|. 47 // Writes the character and glyph ranges of the cluster containing |pos|.
48 void GetClusterAt(size_t pos, Range* chars, Range* glyphs) const; 48 void GetClusterAt(size_t pos, Range* chars, Range* glyphs) const;
49 49
50 // Returns the grapheme bounds at |text_index|. Handles multi-grapheme glyphs. 50 // Returns the grapheme bounds at |text_index|. Handles multi-grapheme glyphs.
(...skipping 21 matching lines...) Expand all
72 int font_size; 72 int font_size;
73 int font_style; 73 int font_style;
74 bool strike; 74 bool strike;
75 bool diagonal_strike; 75 bool diagonal_strike;
76 bool underline; 76 bool underline;
77 77
78 private: 78 private:
79 DISALLOW_COPY_AND_ASSIGN(TextRunHarfBuzz); 79 DISALLOW_COPY_AND_ASSIGN(TextRunHarfBuzz);
80 }; 80 };
81 81
82 // Manages the list of TextRunHarfBuzz and its logical <-> visual index mapping.
83 class TextRunList {
84 public:
85 TextRunList();
86 ~TextRunList();
87
88 size_t size() const { return runs_.size(); }
89
90 // Converts the index between logical and visual index.
91 size_t visual_to_logical(size_t index) const {
92 return visual_to_logical_[index];
93 }
94 size_t logical_to_visual(size_t index) const {
95 return logical_to_visual_[index];
96 }
97
98 const ScopedVector<TextRunHarfBuzz>& runs() const { return runs_; }
99
100 // Adds the new |run| to the run list.
101 void add(TextRunHarfBuzz* run) { runs_.push_back(run); }
102
103 // Reset the run list.
104 void Reset();
105
106 // Initialize the index mapping.
107 void InitIndexMap();
108
109 // Precomputes the offsets for all runs.
110 void ComputePrecedingRunWidths();
111
112 // Get the total width of runs, as if they were shown on one line.
113 // This is not useful when multiline is enabled.
msw 2015/02/14 00:11:26 I was hoping that you'd remove this part of the co
oshima 2015/02/14 00:40:51 No, it's not. This is not used when multi-line is
msw 2015/02/14 02:02:15 Ah, I suppose that you're right.
114 float width() const { return width_; }
115
116 private:
117 // Text runs in logical order.
118 ScopedVector<TextRunHarfBuzz> runs_;
119
120 // Maps visual run indices to logical run indices and vice versa.
121 std::vector<int32_t> visual_to_logical_;
122 std::vector<int32_t> logical_to_visual_;
123
124 float width_;
125
126 DISALLOW_COPY_AND_ASSIGN(TextRunList);
127 };
128
82 } // namespace internal 129 } // namespace internal
83 130
84 class GFX_EXPORT RenderTextHarfBuzz : public RenderText { 131 class GFX_EXPORT RenderTextHarfBuzz : public RenderText {
85 public: 132 public:
86 RenderTextHarfBuzz(); 133 RenderTextHarfBuzz();
87 ~RenderTextHarfBuzz() override; 134 ~RenderTextHarfBuzz() override;
88 135
89 // Overridden from RenderText. 136 // RenderText:
90 scoped_ptr<RenderText> CreateInstanceOfSameType() const override; 137 scoped_ptr<RenderText> CreateInstanceOfSameType() const override;
138 const base::string16& GetDisplayText() override;
91 Size GetStringSize() override; 139 Size GetStringSize() override;
92 SizeF GetStringSizeF() override; 140 SizeF GetStringSizeF() override;
93 SelectionModel FindCursorPosition(const Point& point) override; 141 SelectionModel FindCursorPosition(const Point& point) override;
94 std::vector<FontSpan> GetFontSpansForTesting() override; 142 std::vector<FontSpan> GetFontSpansForTesting() override;
95 Range GetGlyphBounds(size_t index) override; 143 Range GetGlyphBounds(size_t index) override;
96 144
97 protected: 145 protected:
98 // Overridden from RenderText. 146 // RenderText:
99 int GetLayoutTextBaseline() override; 147 int GetDisplayTextBaseline() override;
100 SelectionModel AdjacentCharSelectionModel( 148 SelectionModel AdjacentCharSelectionModel(
101 const SelectionModel& selection, 149 const SelectionModel& selection,
102 VisualCursorDirection direction) override; 150 VisualCursorDirection direction) override;
103 SelectionModel AdjacentWordSelectionModel( 151 SelectionModel AdjacentWordSelectionModel(
104 const SelectionModel& selection, 152 const SelectionModel& selection,
105 VisualCursorDirection direction) override; 153 VisualCursorDirection direction) override;
106 std::vector<Rect> GetSubstringBounds(const Range& range) override; 154 std::vector<Rect> GetSubstringBounds(const Range& range) override;
107 size_t TextIndexToLayoutIndex(size_t index) const override; 155 size_t TextIndexToDisplayIndex(size_t index) override;
108 size_t LayoutIndexToTextIndex(size_t index) const override; 156 size_t DisplayIndexToTextIndex(size_t index) override;
109 bool IsValidCursorIndex(size_t index) override; 157 bool IsValidCursorIndex(size_t index) override;
110 void ResetLayout() override; 158 void OnLayoutTextAttributeChanged(bool text_changed) override;
159 void OnDisplayTextAttributeChanged() override;
111 void EnsureLayout() override; 160 void EnsureLayout() override;
112 void DrawVisualText(Canvas* canvas) override; 161 void DrawVisualText(Canvas* canvas) override;
113 162
114 private: 163 private:
115 friend class RenderTextTest; 164 friend class RenderTextTest;
116 FRIEND_TEST_ALL_PREFIXES(RenderTextTest, Multiline_NormalWidth); 165 FRIEND_TEST_ALL_PREFIXES(RenderTextTest, Multiline_NormalWidth);
117 FRIEND_TEST_ALL_PREFIXES(RenderTextTest, HarfBuzz_RunDirection); 166 FRIEND_TEST_ALL_PREFIXES(RenderTextTest, HarfBuzz_RunDirection);
118 FRIEND_TEST_ALL_PREFIXES(RenderTextTest, HarfBuzz_BreakRunsByUnicodeBlocks); 167 FRIEND_TEST_ALL_PREFIXES(RenderTextTest, HarfBuzz_BreakRunsByUnicodeBlocks);
119 FRIEND_TEST_ALL_PREFIXES(RenderTextTest, HarfBuzz_BreakRunsByEmoji); 168 FRIEND_TEST_ALL_PREFIXES(RenderTextTest, HarfBuzz_BreakRunsByEmoji);
120 FRIEND_TEST_ALL_PREFIXES(RenderTextTest, HarfBuzz_SubglyphGraphemeCases); 169 FRIEND_TEST_ALL_PREFIXES(RenderTextTest, HarfBuzz_SubglyphGraphemeCases);
121 FRIEND_TEST_ALL_PREFIXES(RenderTextTest, HarfBuzz_SubglyphGraphemePartition); 170 FRIEND_TEST_ALL_PREFIXES(RenderTextTest, HarfBuzz_SubglyphGraphemePartition);
122 FRIEND_TEST_ALL_PREFIXES(RenderTextTest, HarfBuzz_NonExistentFont); 171 FRIEND_TEST_ALL_PREFIXES(RenderTextTest, HarfBuzz_NonExistentFont);
123 FRIEND_TEST_ALL_PREFIXES(RenderTextTest, HarfBuzz_UniscribeFallback); 172 FRIEND_TEST_ALL_PREFIXES(RenderTextTest, HarfBuzz_UniscribeFallback);
124 173
125 // Specify the width of a glyph for test. The width of glyphs is very 174 // Specify the width of a glyph for test. The width of glyphs is very
126 // platform-dependent and environment-dependent. Otherwise multiline test 175 // platform-dependent and environment-dependent. Otherwise multiline test
127 // will become really flaky. 176 // will become really flaky.
128 void set_glyph_width_for_test(uint8 test_width) { 177 void set_glyph_width_for_test(uint8 test_width) {
129 glyph_width_for_test_ = test_width; 178 glyph_width_for_test_ = test_width;
130 } 179 }
131 180
132 // Return the run index that contains the argument; or the length of the 181 // Return the run index that contains the argument; or the length of the
133 // |runs_| vector if argument exceeds the text length or width. 182 // |runs_| vector if argument exceeds the text length or width.
134 size_t GetRunContainingCaret(const SelectionModel& caret) const; 183 size_t GetRunContainingCaret(const SelectionModel& caret);
135 size_t GetRunContainingXCoord(float x, float* offset) const; 184 size_t GetRunContainingXCoord(float x, float* offset) const;
136 185
137 // Given a |run|, returns the SelectionModel that contains the logical first 186 // Given a |run|, returns the SelectionModel that contains the logical first
138 // or last caret position inside (not at a boundary of) the run. 187 // or last caret position inside (not at a boundary of) the run.
139 // The returned value represents a cursor/caret position without a selection. 188 // The returned value represents a cursor/caret position without a selection.
140 SelectionModel FirstSelectionModelInsideRun( 189 SelectionModel FirstSelectionModelInsideRun(
141 const internal::TextRunHarfBuzz* run); 190 const internal::TextRunHarfBuzz* run);
142 SelectionModel LastSelectionModelInsideRun( 191 SelectionModel LastSelectionModelInsideRun(
143 const internal::TextRunHarfBuzz* run); 192 const internal::TextRunHarfBuzz* run);
144 193
145 // Break the text into logical runs and populate the visual <-> logical maps. 194 // Break the text into logical runs and populate the visual <-> logical maps
146 void ItemizeText(); 195 // into |run_list_out|.
196 void ItemizeTextToRuns(const base::string16& string,
197 internal::TextRunList* run_list_out);
147 198
148 // Helper method for ShapeRun() that calls ShapeRunWithFont() with |run|, 199 // Helper method for ShapeRun() that calls ShapeRunWithFont() with |text|,
149 // |family|, and |render_params|, returning true if the family provides all 200 // |run|, |family|, and |render_params|, returning true if the family provides
150 // needed glyphs and false otherwise. Additionally updates |best_family|, 201 // all the glyphs needed for |run|, and false otherwise. Additionally updates
151 // |best_render_params|, and |best_missing_glyphs| if |family| has fewer than 202 // |best_family|, |best_render_params|, and |best_missing_glyphs| if |family|
152 // |best_missing_glyphs| missing glyphs. 203 // has fewer than |best_missing_glyphs| missing glyphs.
153 bool CompareFamily(internal::TextRunHarfBuzz* run, 204 bool CompareFamily(const base::string16& text,
154 const std::string& family, 205 const std::string& family,
155 const gfx::FontRenderParams& render_params, 206 const gfx::FontRenderParams& render_params,
207 internal::TextRunHarfBuzz* run,
156 std::string* best_family, 208 std::string* best_family,
157 gfx::FontRenderParams* best_render_params, 209 gfx::FontRenderParams* best_render_params,
158 size_t* best_missing_glyphs); 210 size_t* best_missing_glyphs);
159 211
160 // Shape the glyphs needed for the text |run|. 212 // Shape the glyphs of all runs in |run_list| using |text|.
161 void ShapeRun(internal::TextRunHarfBuzz* run); 213 void ShapeRunList(const base::string16& text,
162 bool ShapeRunWithFont(internal::TextRunHarfBuzz* run, 214 internal::TextRunList* run_list);
215
216 // Shape the glyphs needed for the |run| within the |text|.
217 void ShapeRun(const base::string16& text,
218 internal::TextRunHarfBuzz* run);
219 bool ShapeRunWithFont(const base::string16& text,
163 const std::string& font_family, 220 const std::string& font_family,
164 const FontRenderParams& params); 221 const FontRenderParams& params,
222 internal::TextRunHarfBuzz* run);
165 223
166 // Text runs in logical order. 224 // Makes sure that text runs for layout text are shaped.
167 ScopedVector<internal::TextRunHarfBuzz> runs_; 225 void EnsureLayoutRunList();
168 226
169 // Maps visual run indices to logical run indices and vice versa. 227 // ICU grapheme iterator for the layout text. Can be NULL in case of an error.
170 std::vector<int32_t> visual_to_logical_; 228 base::i18n::BreakIterator* GetGraphemeIterator();
171 std::vector<int32_t> logical_to_visual_;
172 229
173 bool needs_layout_; 230 // Convert an index in |text_| to the index in |given_text|. The
231 // |given_text| should be either |display_text_| or |layout_text_|
232 // depending on the elide state.
233 size_t TextIndexToGivenTextIndex(const base::string16& given_text,
234 size_t index);
174 235
175 // ICU grapheme iterator for the layout text. Valid when |!needs_layout_|. Can 236 // Returns the current run list, |display_run_list_| if the text is
176 // be NULL in case of an error. 237 // elided, or |layout_run_list_| otherwise.
238 internal::TextRunList* GetRunList();
239 const internal::TextRunList* GetRunList() const;
240
241 // Text run list for |layout_text_| and |display_text_|.
242 // |display_run_list_| is created only when the text is elided.
243 internal::TextRunList layout_run_list_;
244 scoped_ptr<internal::TextRunList> display_run_list_;
245
246 bool update_layout_run_list_ : 1;
247 bool update_display_run_list_ : 1;
248 bool update_grapheme_iterator_ : 1;
249 bool update_display_text_ : 1;
250
251 // ICU grapheme iterator for the layout text. Use GetGraphemeIterator()
252 // to access the iterator.
177 scoped_ptr<base::i18n::BreakIterator> grapheme_iterator_; 253 scoped_ptr<base::i18n::BreakIterator> grapheme_iterator_;
178 254
179 // The total size of the layouted text. 255 // The total size of the layouted text.
180 SizeF total_size_; 256 SizeF total_size_;
181 257
182 // Fixed width of glyphs. This should only be set in test environments. 258 // Fixed width of glyphs. This should only be set in test environments.
183 uint8 glyph_width_for_test_; 259 uint8 glyph_width_for_test_;
184 260
185 DISALLOW_COPY_AND_ASSIGN(RenderTextHarfBuzz); 261 DISALLOW_COPY_AND_ASSIGN(RenderTextHarfBuzz);
186 }; 262 };
187 263
188 } // namespace gfx 264 } // namespace gfx
189 265
190 #endif // UI_GFX_RENDER_TEXT_HARFBUZZ_H_ 266 #endif // UI_GFX_RENDER_TEXT_HARFBUZZ_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698