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 // TODO(ckocagil): Make Range a template class and RangeF an instance of it.
26 typedef std::pair
<float, float> RangeF
;
28 // Applies std::round to the start and end values of the given RangeF.
29 Range GFX_EXPORT
RoundRangeF(const RangeF
& range_f
);
31 struct GFX_EXPORT TextRunHarfBuzz
{
35 // Returns the index of the first glyph that corresponds to the character at
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 |GetLayoutText()[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
,
54 // Returns whether the given shaped run contains any missing glyphs.
55 bool HasMissingGlyphs() const;
58 float preceding_run_widths
;
64 scoped_ptr
<uint16
[]> glyphs
;
65 scoped_ptr
<SkPoint
[]> positions
;
66 std::vector
<uint32
> glyph_to_char
;
70 skia::RefPtr
<SkTypeface
> skia_face
;
71 FontRenderParams render_params
;
79 DISALLOW_COPY_AND_ASSIGN(TextRunHarfBuzz
);
82 } // namespace internal
84 class GFX_EXPORT RenderTextHarfBuzz
: public RenderText
{
87 ~RenderTextHarfBuzz() override
;
89 // Overridden from RenderText.
90 scoped_ptr
<RenderText
> CreateInstanceOfSameType() const override
;
91 Size
GetStringSize() override
;
92 SizeF
GetStringSizeF() override
;
93 SelectionModel
FindCursorPosition(const Point
& point
) override
;
94 std::vector
<FontSpan
> GetFontSpansForTesting() override
;
95 Range
GetGlyphBounds(size_t index
) override
;
98 // Overridden from RenderText.
99 int GetLayoutTextBaseline() override
;
100 SelectionModel
AdjacentCharSelectionModel(
101 const SelectionModel
& selection
,
102 VisualCursorDirection direction
) override
;
103 SelectionModel
AdjacentWordSelectionModel(
104 const SelectionModel
& selection
,
105 VisualCursorDirection direction
) override
;
106 std::vector
<Rect
> GetSubstringBounds(const Range
& range
) override
;
107 size_t TextIndexToLayoutIndex(size_t index
) const override
;
108 size_t LayoutIndexToTextIndex(size_t index
) const override
;
109 bool IsValidCursorIndex(size_t index
) override
;
110 void ResetLayout() override
;
111 void EnsureLayout() override
;
112 void DrawVisualText(Canvas
* canvas
) override
;
115 friend class RenderTextTest
;
116 FRIEND_TEST_ALL_PREFIXES(RenderTextTest
, HarfBuzz_RunDirection
);
117 FRIEND_TEST_ALL_PREFIXES(RenderTextTest
, HarfBuzz_BreakRunsByUnicodeBlocks
);
118 FRIEND_TEST_ALL_PREFIXES(RenderTextTest
, HarfBuzz_SubglyphGraphemeCases
);
119 FRIEND_TEST_ALL_PREFIXES(RenderTextTest
, HarfBuzz_SubglyphGraphemePartition
);
120 FRIEND_TEST_ALL_PREFIXES(RenderTextTest
, HarfBuzz_NonExistentFont
);
121 FRIEND_TEST_ALL_PREFIXES(RenderTextTest
, HarfBuzz_UniscribeFallback
);
123 // Return the run index that contains the argument; or the length of the
124 // |runs_| vector if argument exceeds the text length or width.
125 size_t GetRunContainingCaret(const SelectionModel
& caret
) const;
126 size_t GetRunContainingXCoord(float x
, float* offset
) const;
128 // Given a |run|, returns the SelectionModel that contains the logical first
129 // or last caret position inside (not at a boundary of) the run.
130 // The returned value represents a cursor/caret position without a selection.
131 SelectionModel
FirstSelectionModelInsideRun(
132 const internal::TextRunHarfBuzz
* run
);
133 SelectionModel
LastSelectionModelInsideRun(
134 const internal::TextRunHarfBuzz
* run
);
136 // Break the text into logical runs and populate the visual <-> logical maps.
139 // Helper method for ShapeRun() that calls ShapeRunWithFont() with |run|,
140 // |family|, and |render_params|, returning true if the family provides all
141 // needed glyphs and false otherwise. Additionally updates |best_family|,
142 // |best_render_params|, and |best_missing_glyphs| if |family| has fewer than
143 // |best_missing_glyphs| missing glyphs.
144 bool CompareFamily(internal::TextRunHarfBuzz
* run
,
145 const std::string
& family
,
146 const gfx::FontRenderParams
& render_params
,
147 std::string
* best_family
,
148 gfx::FontRenderParams
* best_render_params
,
149 size_t* best_missing_glyphs
);
151 // Shape the glyphs needed for the text |run|.
152 void ShapeRun(internal::TextRunHarfBuzz
* run
);
153 bool ShapeRunWithFont(internal::TextRunHarfBuzz
* run
,
154 const std::string
& font_family
,
155 const FontRenderParams
& params
);
157 // Text runs in logical order.
158 ScopedVector
<internal::TextRunHarfBuzz
> runs_
;
160 // Maps visual run indices to logical run indices and vice versa.
161 std::vector
<int32_t> visual_to_logical_
;
162 std::vector
<int32_t> logical_to_visual_
;
166 // ICU grapheme iterator for the layout text. Valid when |!needs_layout_|. Can
167 // be NULL in case of an error.
168 scoped_ptr
<base::i18n::BreakIterator
> grapheme_iterator_
;
170 DISALLOW_COPY_AND_ASSIGN(RenderTextHarfBuzz
);
175 #endif // UI_GFX_RENDER_TEXT_HARFBUZZ_H_