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
;
64 skia::RefPtr
<SkTypeface
> skia_face
;
65 FontRenderParams render_params
;
73 DISALLOW_COPY_AND_ASSIGN(TextRunHarfBuzz
);
76 } // namespace internal
78 class GFX_EXPORT RenderTextHarfBuzz
: public RenderText
{
81 ~RenderTextHarfBuzz() override
;
83 // Overridden from RenderText.
84 Size
GetStringSize() override
;
85 SizeF
GetStringSizeF() override
;
86 SelectionModel
FindCursorPosition(const Point
& point
) override
;
87 std::vector
<FontSpan
> GetFontSpansForTesting() override
;
88 Range
GetGlyphBounds(size_t index
) override
;
91 // Overridden from RenderText.
92 int GetLayoutTextBaseline() override
;
93 SelectionModel
AdjacentCharSelectionModel(
94 const SelectionModel
& selection
,
95 VisualCursorDirection direction
) override
;
96 SelectionModel
AdjacentWordSelectionModel(
97 const SelectionModel
& selection
,
98 VisualCursorDirection direction
) override
;
99 std::vector
<Rect
> GetSubstringBounds(const Range
& range
) override
;
100 size_t TextIndexToLayoutIndex(size_t index
) const override
;
101 size_t LayoutIndexToTextIndex(size_t index
) const override
;
102 bool IsValidCursorIndex(size_t index
) override
;
103 void ResetLayout() override
;
104 void EnsureLayout() override
;
105 void DrawVisualText(Canvas
* canvas
) override
;
108 friend class RenderTextTest
;
109 FRIEND_TEST_ALL_PREFIXES(RenderTextTest
, HarfBuzz_RunDirection
);
110 FRIEND_TEST_ALL_PREFIXES(RenderTextTest
, HarfBuzz_BreakRunsByUnicodeBlocks
);
111 FRIEND_TEST_ALL_PREFIXES(RenderTextTest
, HarfBuzz_SubglyphGraphemeCases
);
112 FRIEND_TEST_ALL_PREFIXES(RenderTextTest
, HarfBuzz_SubglyphGraphemePartition
);
113 FRIEND_TEST_ALL_PREFIXES(RenderTextTest
, HarfBuzz_NonExistentFont
);
114 FRIEND_TEST_ALL_PREFIXES(RenderTextTest
, HarfBuzz_UniscribeFallback
);
116 // Return the run index that contains the argument; or the length of the
117 // |runs_| vector if argument exceeds the text length or width.
118 size_t GetRunContainingCaret(const SelectionModel
& caret
) const;
119 size_t GetRunContainingXCoord(int x
, int* offset
) const;
121 // Given a |run|, returns the SelectionModel that contains the logical first
122 // or last caret position inside (not at a boundary of) the run.
123 // The returned value represents a cursor/caret position without a selection.
124 SelectionModel
FirstSelectionModelInsideRun(
125 const internal::TextRunHarfBuzz
* run
);
126 SelectionModel
LastSelectionModelInsideRun(
127 const internal::TextRunHarfBuzz
* run
);
129 // Break the text into logical runs and populate the visual <-> logical maps.
132 // Helper method for ShapeRun() that calls ShapeRunWithFont() with |run|,
133 // |family|, and |render_params|, returning true if the family provides all
134 // needed glyphs and false otherwise. Additionally updates |best_family|,
135 // |best_render_params|, and |best_missing_glyphs| if |family| has fewer than
136 // |best_missing_glyphs| missing glyphs.
137 bool CompareFamily(internal::TextRunHarfBuzz
* run
,
138 const std::string
& family
,
139 const gfx::FontRenderParams
& render_params
,
140 std::string
* best_family
,
141 gfx::FontRenderParams
* best_render_params
,
142 size_t* best_missing_glyphs
);
144 // Shape the glyphs needed for the text |run|.
145 void ShapeRun(internal::TextRunHarfBuzz
* run
);
146 bool ShapeRunWithFont(internal::TextRunHarfBuzz
* run
,
147 const std::string
& font_family
,
148 const FontRenderParams
& params
);
150 // Text runs in logical order.
151 ScopedVector
<internal::TextRunHarfBuzz
> runs_
;
153 // Maps visual run indices to logical run indices and vice versa.
154 std::vector
<int32_t> visual_to_logical_
;
155 std::vector
<int32_t> logical_to_visual_
;
159 // ICU grapheme iterator for the layout text. Valid when |!needs_layout_|. Can
160 // be NULL in case of an error.
161 scoped_ptr
<base::i18n::BreakIterator
> grapheme_iterator_
;
163 DISALLOW_COPY_AND_ASSIGN(RenderTextHarfBuzz
);
168 #endif // UI_GFX_RENDER_TEXT_HARFBUZZ_H_