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