OLD | NEW |
---|---|
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 Loading... | |
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 Loading... | |
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 ScopedVector<TextRunHarfBuzz>& runs() { return runs_; } | |
100 | |
101 // Adds the new |run| to the run list. | |
102 void add(TextRunHarfBuzz* run) { runs_.push_back(run); } | |
103 | |
104 // Reset the run list. | |
105 void reset() { runs_.clear(); } | |
106 | |
107 // Initialize the index mapping. | |
108 void InitIndexMap(); | |
109 | |
110 // Precomputes the offsets for all runs. | |
111 void ComputePrecedingRunWidths(); | |
112 | |
113 // Get the total width of runs. This is not useful when multiline is enabled. | |
msw
2015/02/13 22:19:05
nit: consider // Get the total width of runs, as i
oshima
2015/02/13 23:24:11
Done.
| |
114 float GetWidthF() const; | |
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 DISALLOW_COPY_AND_ASSIGN(TextRunList); | |
125 }; | |
126 | |
82 } // namespace internal | 127 } // namespace internal |
83 | 128 |
84 class GFX_EXPORT RenderTextHarfBuzz : public RenderText { | 129 class GFX_EXPORT RenderTextHarfBuzz : public RenderText { |
85 public: | 130 public: |
86 RenderTextHarfBuzz(); | 131 RenderTextHarfBuzz(); |
87 ~RenderTextHarfBuzz() override; | 132 ~RenderTextHarfBuzz() override; |
88 | 133 |
89 // Overridden from RenderText. | 134 // RenderText: |
90 scoped_ptr<RenderText> CreateInstanceOfSameType() const override; | 135 scoped_ptr<RenderText> CreateInstanceOfSameType() const override; |
136 const base::string16& GetDisplayText() override; | |
91 Size GetStringSize() override; | 137 Size GetStringSize() override; |
92 SizeF GetStringSizeF() override; | 138 SizeF GetStringSizeF() override; |
93 SelectionModel FindCursorPosition(const Point& point) override; | 139 SelectionModel FindCursorPosition(const Point& point) override; |
94 std::vector<FontSpan> GetFontSpansForTesting() override; | 140 std::vector<FontSpan> GetFontSpansForTesting() override; |
95 Range GetGlyphBounds(size_t index) override; | 141 Range GetGlyphBounds(size_t index) override; |
96 | 142 |
97 protected: | 143 protected: |
98 // Overridden from RenderText. | 144 // RenderText: |
99 int GetLayoutTextBaseline() override; | 145 int GetDisplayTextBaseline() override; |
100 SelectionModel AdjacentCharSelectionModel( | 146 SelectionModel AdjacentCharSelectionModel( |
101 const SelectionModel& selection, | 147 const SelectionModel& selection, |
102 VisualCursorDirection direction) override; | 148 VisualCursorDirection direction) override; |
103 SelectionModel AdjacentWordSelectionModel( | 149 SelectionModel AdjacentWordSelectionModel( |
104 const SelectionModel& selection, | 150 const SelectionModel& selection, |
105 VisualCursorDirection direction) override; | 151 VisualCursorDirection direction) override; |
106 std::vector<Rect> GetSubstringBounds(const Range& range) override; | 152 std::vector<Rect> GetSubstringBounds(const Range& range) override; |
107 size_t TextIndexToLayoutIndex(size_t index) const override; | 153 size_t TextIndexToDisplayIndex(size_t index) override; |
108 size_t LayoutIndexToTextIndex(size_t index) const override; | 154 size_t DisplayIndexToTextIndex(size_t index) override; |
109 bool IsValidCursorIndex(size_t index) override; | 155 bool IsValidCursorIndex(size_t index) override; |
110 void ResetLayout() override; | 156 void OnLayoutTextAttributeChanged(bool text_changed) override; |
157 void OnDisplayTextAttributeChanged() override; | |
111 void EnsureLayout() override; | 158 void EnsureLayout() override; |
112 void DrawVisualText(Canvas* canvas) override; | 159 void DrawVisualText(Canvas* canvas) override; |
113 | 160 |
114 private: | 161 private: |
115 friend class RenderTextTest; | 162 friend class RenderTextTest; |
116 FRIEND_TEST_ALL_PREFIXES(RenderTextTest, Multiline_NormalWidth); | 163 FRIEND_TEST_ALL_PREFIXES(RenderTextTest, Multiline_NormalWidth); |
117 FRIEND_TEST_ALL_PREFIXES(RenderTextTest, HarfBuzz_RunDirection); | 164 FRIEND_TEST_ALL_PREFIXES(RenderTextTest, HarfBuzz_RunDirection); |
118 FRIEND_TEST_ALL_PREFIXES(RenderTextTest, HarfBuzz_BreakRunsByUnicodeBlocks); | 165 FRIEND_TEST_ALL_PREFIXES(RenderTextTest, HarfBuzz_BreakRunsByUnicodeBlocks); |
119 FRIEND_TEST_ALL_PREFIXES(RenderTextTest, HarfBuzz_BreakRunsByEmoji); | 166 FRIEND_TEST_ALL_PREFIXES(RenderTextTest, HarfBuzz_BreakRunsByEmoji); |
120 FRIEND_TEST_ALL_PREFIXES(RenderTextTest, HarfBuzz_SubglyphGraphemeCases); | 167 FRIEND_TEST_ALL_PREFIXES(RenderTextTest, HarfBuzz_SubglyphGraphemeCases); |
121 FRIEND_TEST_ALL_PREFIXES(RenderTextTest, HarfBuzz_SubglyphGraphemePartition); | 168 FRIEND_TEST_ALL_PREFIXES(RenderTextTest, HarfBuzz_SubglyphGraphemePartition); |
122 FRIEND_TEST_ALL_PREFIXES(RenderTextTest, HarfBuzz_NonExistentFont); | 169 FRIEND_TEST_ALL_PREFIXES(RenderTextTest, HarfBuzz_NonExistentFont); |
123 FRIEND_TEST_ALL_PREFIXES(RenderTextTest, HarfBuzz_UniscribeFallback); | 170 FRIEND_TEST_ALL_PREFIXES(RenderTextTest, HarfBuzz_UniscribeFallback); |
124 | 171 |
125 // Specify the width of a glyph for test. The width of glyphs is very | 172 // Specify the width of a glyph for test. The width of glyphs is very |
126 // platform-dependent and environment-dependent. Otherwise multiline test | 173 // platform-dependent and environment-dependent. Otherwise multiline test |
127 // will become really flaky. | 174 // will become really flaky. |
128 void set_glyph_width_for_test(uint8 test_width) { | 175 void set_glyph_width_for_test(uint8 test_width) { |
129 glyph_width_for_test_ = test_width; | 176 glyph_width_for_test_ = test_width; |
130 } | 177 } |
131 | 178 |
132 // Return the run index that contains the argument; or the length of the | 179 // Return the run index that contains the argument; or the length of the |
133 // |runs_| vector if argument exceeds the text length or width. | 180 // |runs_| vector if argument exceeds the text length or width. |
134 size_t GetRunContainingCaret(const SelectionModel& caret) const; | 181 size_t GetRunContainingCaret(const SelectionModel& caret); |
135 size_t GetRunContainingXCoord(float x, float* offset) const; | 182 size_t GetRunContainingXCoord(float x, float* offset) const; |
136 | 183 |
137 // Given a |run|, returns the SelectionModel that contains the logical first | 184 // Given a |run|, returns the SelectionModel that contains the logical first |
138 // or last caret position inside (not at a boundary of) the run. | 185 // or last caret position inside (not at a boundary of) the run. |
139 // The returned value represents a cursor/caret position without a selection. | 186 // The returned value represents a cursor/caret position without a selection. |
140 SelectionModel FirstSelectionModelInsideRun( | 187 SelectionModel FirstSelectionModelInsideRun( |
141 const internal::TextRunHarfBuzz* run); | 188 const internal::TextRunHarfBuzz* run); |
142 SelectionModel LastSelectionModelInsideRun( | 189 SelectionModel LastSelectionModelInsideRun( |
143 const internal::TextRunHarfBuzz* run); | 190 const internal::TextRunHarfBuzz* run); |
144 | 191 |
145 // Break the text into logical runs and populate the visual <-> logical maps. | 192 // Break the text into logical runs and populate the visual <-> logical maps |
146 void ItemizeText(); | 193 // into |run_list_out|. |
194 void ItemizeTextToRuns(const base::string16& string, | |
195 internal::TextRunList* run_list_out); | |
147 | 196 |
148 // Helper method for ShapeRun() that calls ShapeRunWithFont() with |run|, | 197 // Helper method for ShapeRun() that calls ShapeRunWithFont() with |text|, |
149 // |family|, and |render_params|, returning true if the family provides all | 198 // |run|, |family|, and |render_params|, returning true if the family provides |
150 // needed glyphs and false otherwise. Additionally updates |best_family|, | 199 // all the glyphs needed for |run|, and false otherwise. Additionally updates |
151 // |best_render_params|, and |best_missing_glyphs| if |family| has fewer than | 200 // |best_family|, |best_render_params|, and |best_missing_glyphs| if |family| |
152 // |best_missing_glyphs| missing glyphs. | 201 // has fewer than |best_missing_glyphs| missing glyphs. |
153 bool CompareFamily(internal::TextRunHarfBuzz* run, | 202 bool CompareFamily(const base::string16& text, |
154 const std::string& family, | 203 const std::string& family, |
155 const gfx::FontRenderParams& render_params, | 204 const gfx::FontRenderParams& render_params, |
205 internal::TextRunHarfBuzz* run, | |
156 std::string* best_family, | 206 std::string* best_family, |
157 gfx::FontRenderParams* best_render_params, | 207 gfx::FontRenderParams* best_render_params, |
158 size_t* best_missing_glyphs); | 208 size_t* best_missing_glyphs); |
159 | 209 |
160 // Shape the glyphs needed for the text |run|. | 210 // Shape the glyphs of all runs in |run_list| using |text|. |
161 void ShapeRun(internal::TextRunHarfBuzz* run); | 211 void ShapeRunList(const base::string16& text, |
162 bool ShapeRunWithFont(internal::TextRunHarfBuzz* run, | 212 internal::TextRunList* run_list); |
213 | |
214 // Shape the glyphs needed for the |run| within the |text|. | |
215 void ShapeRun(const base::string16& text, | |
216 internal::TextRunHarfBuzz* run); | |
217 bool ShapeRunWithFont(const base::string16& text, | |
163 const std::string& font_family, | 218 const std::string& font_family, |
164 const FontRenderParams& params); | 219 const FontRenderParams& params, |
220 internal::TextRunHarfBuzz* run); | |
165 | 221 |
166 // Text runs in logical order. | 222 // Makes sure that text runs for layout text are shaped. |
167 ScopedVector<internal::TextRunHarfBuzz> runs_; | 223 void EnsureLayoutRunList(); |
168 | 224 |
169 // Maps visual run indices to logical run indices and vice versa. | 225 // ICU grapheme iterator for the layout text. Can be NULL in case of an error. |
170 std::vector<int32_t> visual_to_logical_; | 226 base::i18n::BreakIterator* GetGraphemeIterator(); |
171 std::vector<int32_t> logical_to_visual_; | |
172 | 227 |
173 bool needs_layout_; | 228 // Convert an index in |text_| to the index in |given_text|. The |
229 // |given_text| should be either |display_text_| or |layout_text_| | |
230 // depending on the elide state. | |
231 size_t TextIndexToGivenTextIndex(const base::string16& given_text, | |
232 size_t index); | |
174 | 233 |
175 // ICU grapheme iterator for the layout text. Valid when |!needs_layout_|. Can | 234 // Returns the current run list, |display_run_list_| if the text is |
176 // be NULL in case of an error. | 235 // elided, or |layout_run_list_| otherwise. |
236 internal::TextRunList* GetRunList(); | |
237 const internal::TextRunList* GetRunList() const; | |
238 | |
239 // Text run list for |layout_text_| and |display_text_|. | |
240 // |display_run_list_| is created only when the text is elided. | |
241 internal::TextRunList layout_run_list_; | |
242 scoped_ptr<internal::TextRunList> display_run_list_; | |
243 | |
244 bool update_layout_run_list_ : 1; | |
245 bool update_display_run_list_ : 1; | |
246 bool update_grapheme_iterator_ : 1; | |
247 bool update_display_text_ : 1; | |
248 | |
249 // ICU grapheme iterator for the layout text. Use GetGraphemeIterator() | |
250 // to access the iterator. | |
177 scoped_ptr<base::i18n::BreakIterator> grapheme_iterator_; | 251 scoped_ptr<base::i18n::BreakIterator> grapheme_iterator_; |
178 | 252 |
179 // The total size of the layouted text. | 253 // The total size of the layouted text. |
180 SizeF total_size_; | 254 SizeF total_size_; |
181 | 255 |
182 // Fixed width of glyphs. This should only be set in test environments. | 256 // Fixed width of glyphs. This should only be set in test environments. |
183 uint8 glyph_width_for_test_; | 257 uint8 glyph_width_for_test_; |
184 | 258 |
185 DISALLOW_COPY_AND_ASSIGN(RenderTextHarfBuzz); | 259 DISALLOW_COPY_AND_ASSIGN(RenderTextHarfBuzz); |
186 }; | 260 }; |
187 | 261 |
188 } // namespace gfx | 262 } // namespace gfx |
189 | 263 |
190 #endif // UI_GFX_RENDER_TEXT_HARFBUZZ_H_ | 264 #endif // UI_GFX_RENDER_TEXT_HARFBUZZ_H_ |
OLD | NEW |