Separate Simple Backend creation from initialization.
[chromium-blink-merge.git] / ui / gfx / render_text_win.h
bloba419669a0a383ae0eb721d98319e2729893cc71d
1 // Copyright (c) 2012 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_WIN_H_
6 #define UI_GFX_RENDER_TEXT_WIN_H_
8 #include <usp10.h>
10 #include <map>
11 #include <string>
12 #include <vector>
14 #include "base/memory/scoped_ptr.h"
15 #include "base/memory/scoped_vector.h"
16 #include "ui/gfx/render_text.h"
18 namespace gfx {
20 namespace internal {
22 struct TextRun {
23 TextRun();
24 ~TextRun();
26 ui::Range range;
27 Font font;
28 // A gfx::Font::FontStyle flag to specify bold and italic styles.
29 // Supersedes |font.GetFontStyle()|. Stored separately to avoid calling
30 // |font.DeriveFont()|, which is expensive on Windows.
31 int font_style;
33 // TODO(msw): Disambiguate color/style from TextRuns for proper glyph shaping.
34 // See an example: http://www.catch22.net/tuts/uniscribe-mysteries
35 SkColor foreground;
36 bool strike;
37 bool diagonal_strike;
38 bool underline;
40 int width;
41 // The cumulative widths of preceding runs.
42 int preceding_run_widths;
44 SCRIPT_ANALYSIS script_analysis;
46 scoped_ptr<WORD[]> glyphs;
47 scoped_ptr<WORD[]> logical_clusters;
48 scoped_ptr<SCRIPT_VISATTR[]> visible_attributes;
49 int glyph_count;
51 scoped_ptr<int[]> advance_widths;
52 scoped_ptr<GOFFSET[]> offsets;
53 ABC abc_widths;
54 SCRIPT_CACHE script_cache;
56 private:
57 DISALLOW_COPY_AND_ASSIGN(TextRun);
60 } // namespace internal
62 // RenderTextWin is the Windows implementation of RenderText using Uniscribe.
63 class RenderTextWin : public RenderText {
64 public:
65 RenderTextWin();
66 virtual ~RenderTextWin();
68 // Overridden from RenderText:
69 virtual Size GetStringSize() OVERRIDE;
70 virtual int GetBaseline() OVERRIDE;
71 virtual SelectionModel FindCursorPosition(const Point& point) OVERRIDE;
72 virtual std::vector<FontSpan> GetFontSpansForTesting() OVERRIDE;
74 protected:
75 // Overridden from RenderText:
76 virtual SelectionModel AdjacentCharSelectionModel(
77 const SelectionModel& selection,
78 VisualCursorDirection direction) OVERRIDE;
79 virtual SelectionModel AdjacentWordSelectionModel(
80 const SelectionModel& selection,
81 VisualCursorDirection direction) OVERRIDE;
82 virtual void SetSelectionModel(const SelectionModel& model) OVERRIDE;
83 virtual void GetGlyphBounds(size_t index,
84 ui::Range* xspan,
85 int* height) OVERRIDE;
86 virtual std::vector<Rect> GetSubstringBounds(const ui::Range& range) OVERRIDE;
87 virtual size_t TextIndexToLayoutIndex(size_t index) const OVERRIDE;
88 virtual size_t LayoutIndexToTextIndex(size_t index) const OVERRIDE;
89 virtual bool IsCursorablePosition(size_t position) OVERRIDE;
90 virtual void ResetLayout() OVERRIDE;
91 virtual void EnsureLayout() OVERRIDE;
92 virtual void DrawVisualText(Canvas* canvas) OVERRIDE;
94 private:
95 void ItemizeLogicalText();
96 void LayoutVisualText();
97 void LayoutTextRun(internal::TextRun* run);
99 // Helper function that calls |ScriptShape()| on the run, which has logic to
100 // handle E_OUTOFMEMORY return codes.
101 HRESULT ShapeTextRunWithFont(internal::TextRun* run, const Font& font);
103 // Returns the number of characters in |run| that have missing glyphs.
104 int CountCharsWithMissingGlyphs(internal::TextRun* run) const;
106 // Return the run index that contains the argument; or the length of the
107 // |runs_| vector if argument exceeds the text length or width.
108 size_t GetRunContainingCaret(const SelectionModel& caret) const;
109 size_t GetRunContainingPoint(const Point& point) const;
111 // Given a |run|, returns the SelectionModel that contains the logical first
112 // or last caret position inside (not at a boundary of) the run.
113 // The returned value represents a cursor/caret position without a selection.
114 SelectionModel FirstSelectionModelInsideRun(const internal::TextRun* run);
115 SelectionModel LastSelectionModelInsideRun(const internal::TextRun* run);
117 // Cached HDC for performing Uniscribe API calls.
118 static HDC cached_hdc_;
120 // Cached map from font name to the last successful substitute font used.
121 // TODO(asvitkine): Move the caching logic to font_fallback_win.cc.
122 static std::map<std::string, Font> successful_substitute_fonts_;
124 SCRIPT_CONTROL script_control_;
125 SCRIPT_STATE script_state_;
127 ScopedVector<internal::TextRun> runs_;
128 Size string_size_;
130 // A common vertical baseline for all the text runs. This is computed as the
131 // largest baseline over all the runs' fonts.
132 int common_baseline_;
134 scoped_ptr<int[]> visual_to_logical_;
135 scoped_ptr<int[]> logical_to_visual_;
137 bool needs_layout_;
139 DISALLOW_COPY_AND_ASSIGN(RenderTextWin);
142 } // namespace gfx
144 #endif // UI_GFX_RENDER_TEXT_WIN_H_