Upstream version 9.38.198.0
[platform/framework/web/crosswalk.git] / src / ui / gfx / render_text_harfbuzz.h
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.
4
5 #ifndef UI_GFX_RENDER_TEXT_HARFBUZZ_H_
6 #define UI_GFX_RENDER_TEXT_HARFBUZZ_H_
7
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"
14
15 namespace base {
16 namespace i18n {
17 class BreakIterator;
18 }
19 }
20
21 namespace gfx {
22
23 namespace internal {
24
25 struct GFX_EXPORT TextRunHarfBuzz {
26   TextRunHarfBuzz();
27   ~TextRunHarfBuzz();
28
29   // Returns the index of the first glyph that corresponds to the character at
30   // |pos|.
31   size_t CharToGlyph(size_t pos) const;
32
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;
37
38   // Returns the number of missing glyphs in the shaped text run.
39   size_t CountMissingGlyphs() const;
40
41   // Writes the character and glyph ranges of the cluster containing |pos|.
42   void GetClusterAt(size_t pos, Range* chars, Range* glyphs) const;
43
44   // Returns the grapheme bounds at |text_index|. Handles multi-grapheme glyphs.
45   Range GetGraphemeBounds(base::i18n::BreakIterator* grapheme_iterator,
46                           size_t text_index);
47
48   // Returns whether the given shaped run contains any missing glyphs.
49   bool HasMissingGlyphs() const;
50
51   float width;
52   float preceding_run_widths;
53   Range range;
54   bool is_rtl;
55   UBiDiLevel level;
56   UScriptCode script;
57
58   scoped_ptr<uint16[]> glyphs;
59   scoped_ptr<SkPoint[]> positions;
60   std::vector<uint32> glyph_to_char;
61   size_t glyph_count;
62
63   skia::RefPtr<SkTypeface> skia_face;
64   int font_size;
65   int font_style;
66   bool strike;
67   bool diagonal_strike;
68   bool underline;
69
70  private:
71   DISALLOW_COPY_AND_ASSIGN(TextRunHarfBuzz);
72 };
73
74 }  // namespace internal
75
76 class GFX_EXPORT RenderTextHarfBuzz : public RenderText {
77  public:
78   RenderTextHarfBuzz();
79   virtual ~RenderTextHarfBuzz();
80
81   // Overridden from RenderText.
82   virtual Size GetStringSize() OVERRIDE;
83   virtual SizeF GetStringSizeF() OVERRIDE;
84   virtual SelectionModel FindCursorPosition(const Point& point) OVERRIDE;
85   virtual std::vector<FontSpan> GetFontSpansForTesting() OVERRIDE;
86   virtual Range GetGlyphBounds(size_t index) OVERRIDE;
87
88  protected:
89   // Overridden from RenderText.
90   virtual int GetLayoutTextBaseline() OVERRIDE;
91   virtual SelectionModel AdjacentCharSelectionModel(
92       const SelectionModel& selection,
93       VisualCursorDirection direction) OVERRIDE;
94   virtual SelectionModel AdjacentWordSelectionModel(
95       const SelectionModel& selection,
96       VisualCursorDirection direction) OVERRIDE;
97   virtual std::vector<Rect> GetSubstringBounds(const Range& range) OVERRIDE;
98   virtual size_t TextIndexToLayoutIndex(size_t index) const OVERRIDE;
99   virtual size_t LayoutIndexToTextIndex(size_t index) const OVERRIDE;
100   virtual bool IsValidCursorIndex(size_t index) OVERRIDE;
101   virtual void ResetLayout() OVERRIDE;
102   virtual void EnsureLayout() OVERRIDE;
103   virtual void DrawVisualText(Canvas* canvas) OVERRIDE;
104
105  private:
106   friend class RenderTextTest;
107   FRIEND_TEST_ALL_PREFIXES(RenderTextTest, HarfBuzz_RunDirection);
108   FRIEND_TEST_ALL_PREFIXES(RenderTextTest, HarfBuzz_BreakRunsByUnicodeBlocks);
109   FRIEND_TEST_ALL_PREFIXES(RenderTextTest, HarfBuzz_SubglyphGraphemeCases);
110   FRIEND_TEST_ALL_PREFIXES(RenderTextTest, HarfBuzz_SubglyphGraphemePartition);
111   FRIEND_TEST_ALL_PREFIXES(RenderTextTest, HarfBuzz_NonExistentFont);
112
113   // Return the run index that contains the argument; or the length of the
114   // |runs_| vector if argument exceeds the text length or width.
115   size_t GetRunContainingCaret(const SelectionModel& caret) const;
116   size_t GetRunContainingXCoord(int x, int* offset) const;
117
118   // Given a |run|, returns the SelectionModel that contains the logical first
119   // or last caret position inside (not at a boundary of) the run.
120   // The returned value represents a cursor/caret position without a selection.
121   SelectionModel FirstSelectionModelInsideRun(
122       const internal::TextRunHarfBuzz* run);
123   SelectionModel LastSelectionModelInsideRun(
124       const internal::TextRunHarfBuzz* run);
125
126   // Break the text into logical runs and populate the visual <-> logical maps.
127   void ItemizeText();
128
129   // Shape the glyphs needed for the text |run|.
130   void ShapeRun(internal::TextRunHarfBuzz* run);
131   bool ShapeRunWithFont(internal::TextRunHarfBuzz* run,
132                         const std::string& font);
133
134   // Text runs in logical order.
135   ScopedVector<internal::TextRunHarfBuzz> runs_;
136
137   // Maps visual run indices to logical run indices and vice versa.
138   std::vector<int32_t> visual_to_logical_;
139   std::vector<int32_t> logical_to_visual_;
140
141   bool needs_layout_;
142
143   // ICU grapheme iterator for the layout text. Valid when |!needs_layout_|. Can
144   // be NULL in case of an error.
145   scoped_ptr<base::i18n::BreakIterator> grapheme_iterator_;
146
147   DISALLOW_COPY_AND_ASSIGN(RenderTextHarfBuzz);
148 };
149
150 }  // namespace gfx
151
152 #endif  // UI_GFX_RENDER_TEXT_HARFBUZZ_H_