[Android WebView] Put AndroidStreamReaderURLRequestJob into a namespace
[chromium-blink-merge.git] / ui / gfx / render_text.h
blob8301b82bba2759ea27018bb6fcd9c7cf152fae88
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_H_
6 #define UI_GFX_RENDER_TEXT_H_
8 #include <algorithm>
9 #include <cstring>
10 #include <string>
11 #include <utility>
12 #include <vector>
14 #include "base/gtest_prod_util.h"
15 #include "base/i18n/rtl.h"
16 #include "base/memory/scoped_ptr.h"
17 #include "base/strings/string16.h"
18 #include "skia/ext/refptr.h"
19 #include "third_party/skia/include/core/SkColor.h"
20 #include "third_party/skia/include/core/SkPaint.h"
21 #include "ui/gfx/break_list.h"
22 #include "ui/gfx/font_list.h"
23 #include "ui/gfx/font_render_params.h"
24 #include "ui/gfx/geometry/point.h"
25 #include "ui/gfx/geometry/rect.h"
26 #include "ui/gfx/geometry/size_f.h"
27 #include "ui/gfx/geometry/vector2d.h"
28 #include "ui/gfx/range/range.h"
29 #include "ui/gfx/range/range_f.h"
30 #include "ui/gfx/selection_model.h"
31 #include "ui/gfx/shadow_value.h"
32 #include "ui/gfx/text_constants.h"
34 class SkCanvas;
35 class SkDrawLooper;
36 struct SkPoint;
37 class SkShader;
38 class SkTypeface;
40 namespace gfx {
42 class Canvas;
43 class Font;
44 class RenderTextTest;
46 namespace internal {
48 // Internal helper class used by derived classes to draw text through Skia.
49 class GFX_EXPORT SkiaTextRenderer {
50 public:
51 explicit SkiaTextRenderer(Canvas* canvas);
52 virtual ~SkiaTextRenderer();
54 void SetDrawLooper(SkDrawLooper* draw_looper);
55 void SetFontRenderParams(const FontRenderParams& params,
56 bool subpixel_rendering_suppressed);
57 void SetTypeface(SkTypeface* typeface);
58 void SetTextSize(SkScalar size);
59 void SetFontFamilyWithStyle(const std::string& family, int font_style);
60 void SetForegroundColor(SkColor foreground);
61 void SetShader(SkShader* shader);
62 // Sets underline metrics to use if the text will be drawn with an underline.
63 // If not set, default values based on the size of the text will be used. The
64 // two metrics must be set together.
65 void SetUnderlineMetrics(SkScalar thickness, SkScalar position);
66 void DrawSelection(const std::vector<Rect>& selection, SkColor color);
67 virtual void DrawPosText(const SkPoint* pos,
68 const uint16* glyphs,
69 size_t glyph_count);
70 // Draw underline and strike-through text decorations.
71 // Based on |SkCanvas::DrawTextDecorations()| and constants from:
72 // third_party/skia/src/core/SkTextFormatParams.h
73 virtual void DrawDecorations(int x, int y, int width, bool underline,
74 bool strike, bool diagonal_strike);
75 // Finishes any ongoing diagonal strike run.
76 void EndDiagonalStrike();
77 void DrawUnderline(int x, int y, int width);
78 void DrawStrike(int x, int y, int width) const;
80 private:
81 // Helper class to draw a diagonal line with multiple pieces of different
82 // lengths and colors; to support text selection appearances.
83 class DiagonalStrike {
84 public:
85 DiagonalStrike(Canvas* canvas, Point start, const SkPaint& paint);
86 ~DiagonalStrike();
88 void AddPiece(int length, SkColor color);
89 void Draw();
91 private:
92 typedef std::pair<int, SkColor> Piece;
94 Canvas* canvas_;
95 const Point start_;
96 SkPaint paint_;
97 int total_length_;
98 std::vector<Piece> pieces_;
100 DISALLOW_COPY_AND_ASSIGN(DiagonalStrike);
103 Canvas* canvas_;
104 SkCanvas* canvas_skia_;
105 SkPaint paint_;
106 SkScalar underline_thickness_;
107 SkScalar underline_position_;
108 scoped_ptr<DiagonalStrike> diagonal_;
110 DISALLOW_COPY_AND_ASSIGN(SkiaTextRenderer);
113 // Internal helper class used to iterate colors, baselines, and styles.
114 class StyleIterator {
115 public:
116 StyleIterator(const BreakList<SkColor>& colors,
117 const BreakList<BaselineStyle>& baselines,
118 const std::vector<BreakList<bool>>& styles);
119 ~StyleIterator();
121 // Get the colors and styles at the current iterator position.
122 SkColor color() const { return color_->second; }
123 BaselineStyle baseline() const { return baseline_->second; }
124 bool style(TextStyle s) const { return style_[s]->second; }
126 // Get the intersecting range of the current iterator set.
127 Range GetRange() const;
129 // Update the iterator to point to colors and styles applicable at |position|.
130 void UpdatePosition(size_t position);
132 private:
133 BreakList<SkColor> colors_;
134 BreakList<BaselineStyle> baselines_;
135 std::vector<BreakList<bool> > styles_;
137 BreakList<SkColor>::const_iterator color_;
138 BreakList<BaselineStyle>::const_iterator baseline_;
139 std::vector<BreakList<bool>::const_iterator> style_;
141 DISALLOW_COPY_AND_ASSIGN(StyleIterator);
144 // Line segments are slices of the display text to be rendered on a single line.
145 struct LineSegment {
146 LineSegment();
147 ~LineSegment();
149 // X coordinates of this line segment in text space.
150 RangeF x_range;
152 // The character range this segment corresponds to.
153 Range char_range;
155 // Index of the text run that generated this segment.
156 size_t run;
158 // Returns the width of this line segment in text space.
159 float width() const { return x_range.length(); }
162 // A line of display text, comprised of a line segment list and some metrics.
163 struct Line {
164 Line();
165 ~Line();
167 // Segments that make up this line in visual order.
168 std::vector<LineSegment> segments;
170 // The sum of segment widths and the maximum of segment heights.
171 SizeF size;
173 // Sum of preceding lines' heights.
174 int preceding_heights;
176 // Maximum baseline of all segments on this line.
177 int baseline;
180 // Creates an SkTypeface from a font |family| name and a |gfx::Font::FontStyle|.
181 // May return NULL.
182 skia::RefPtr<SkTypeface> CreateSkiaTypeface(const std::string& family,
183 int style);
185 // Applies the given FontRenderParams to a Skia |paint|.
186 void ApplyRenderParams(const FontRenderParams& params,
187 bool subpixel_rendering_suppressed,
188 SkPaint* paint);
190 } // namespace internal
192 // RenderText represents an abstract model of styled text and its corresponding
193 // visual layout. Support is built in for a cursor, a selection, simple styling,
194 // complex scripts, and bi-directional text. Implementations provide mechanisms
195 // for rendering and translation between logical and visual data.
196 class GFX_EXPORT RenderText {
197 public:
198 virtual ~RenderText();
200 // Creates a platform-specific or cross-platform RenderText instance.
201 static RenderText* CreateInstance();
202 static RenderText* CreateInstanceForEditing();
204 // Creates another instance of the same concrete class.
205 virtual scoped_ptr<RenderText> CreateInstanceOfSameType() const = 0;
207 const base::string16& text() const { return text_; }
208 void SetText(const base::string16& text);
209 void AppendText(const base::string16& text);
211 HorizontalAlignment horizontal_alignment() const {
212 return horizontal_alignment_;
214 void SetHorizontalAlignment(HorizontalAlignment alignment);
216 const FontList& font_list() const { return font_list_; }
217 void SetFontList(const FontList& font_list);
219 bool cursor_enabled() const { return cursor_enabled_; }
220 void SetCursorEnabled(bool cursor_enabled);
222 bool cursor_visible() const { return cursor_visible_; }
223 void set_cursor_visible(bool visible) { cursor_visible_ = visible; }
225 bool insert_mode() const { return insert_mode_; }
226 void ToggleInsertMode();
228 SkColor cursor_color() const { return cursor_color_; }
229 void set_cursor_color(SkColor color) { cursor_color_ = color; }
231 SkColor selection_color() const { return selection_color_; }
232 void set_selection_color(SkColor color) { selection_color_ = color; }
234 SkColor selection_background_focused_color() const {
235 return selection_background_focused_color_;
237 void set_selection_background_focused_color(SkColor color) {
238 selection_background_focused_color_ = color;
241 bool focused() const { return focused_; }
242 void set_focused(bool focused) { focused_ = focused; }
244 bool clip_to_display_rect() const { return clip_to_display_rect_; }
245 void set_clip_to_display_rect(bool clip) { clip_to_display_rect_ = clip; }
247 // In an obscured (password) field, all text is drawn as asterisks or bullets.
248 bool obscured() const { return obscured_; }
249 void SetObscured(bool obscured);
251 // Makes a char in obscured text at |index| to be revealed. |index| should be
252 // a UTF16 text index. If there is a previous revealed index, the previous one
253 // is cleared and only the last set index will be revealed. If |index| is -1
254 // or out of range, no char will be revealed. The revealed index is also
255 // cleared when SetText or SetObscured is called.
256 void SetObscuredRevealIndex(int index);
258 // TODO(ckocagil): Multiline text rendering is not supported on Mac.
259 bool multiline() const { return multiline_; }
260 void SetMultiline(bool multiline);
262 // TODO(mukai): ELIDE_LONG_WORDS is not supported.
263 WordWrapBehavior word_wrap_behavior() const { return word_wrap_behavior_; }
264 void SetWordWrapBehavior(WordWrapBehavior behavior);
266 // Set whether newline characters should be replaced with newline symbols.
267 void SetReplaceNewlineCharsWithSymbols(bool replace);
269 // Returns true if this instance supports multiline rendering.
270 virtual bool MultilineSupported() const = 0;
272 // TODO(ckocagil): Add vertical alignment and line spacing support instead.
273 int min_line_height() const { return min_line_height_; }
274 void SetMinLineHeight(int line_height);
276 // Set the maximum length of the layout text, not the actual text.
277 // A |length| of 0 forgoes a hard limit, but does not guarantee proper
278 // functionality of very long strings. Applies to subsequent SetText calls.
279 // WARNING: Only use this for system limits, it lacks complex text support.
280 void set_truncate_length(size_t length) { truncate_length_ = length; }
282 // The display text will be elided to fit |display_rect| using this behavior.
283 void SetElideBehavior(ElideBehavior elide_behavior);
284 ElideBehavior elide_behavior() const { return elide_behavior_; }
286 const Rect& display_rect() const { return display_rect_; }
287 void SetDisplayRect(const Rect& r);
289 bool subpixel_rendering_suppressed() const {
290 return subpixel_rendering_suppressed_;
292 void set_subpixel_rendering_suppressed(bool suppressed) {
293 subpixel_rendering_suppressed_ = suppressed;
296 const SelectionModel& selection_model() const { return selection_model_; }
298 const Range& selection() const { return selection_model_.selection(); }
300 size_t cursor_position() const { return selection_model_.caret_pos(); }
301 void SetCursorPosition(size_t position);
303 // Moves the cursor left or right. Cursor movement is visual, meaning that
304 // left and right are relative to screen, not the directionality of the text.
305 // If |select| is false, the selection start is moved to the same position.
306 void MoveCursor(BreakType break_type,
307 VisualCursorDirection direction,
308 bool select);
310 // Set the selection_model_ to the value of |selection|.
311 // The selection range is clamped to text().length() if out of range.
312 // Returns true if the cursor position or selection range changed.
313 // If any index in |selection_model| is not a cursorable position (not on a
314 // grapheme boundary), it is a no-op and returns false.
315 bool MoveCursorTo(const SelectionModel& selection_model);
317 // Set the selection_model_ based on |range|.
318 // If the |range| start or end is greater than text length, it is modified
319 // to be the text length.
320 // If the |range| start or end is not a cursorable position (not on grapheme
321 // boundary), it is a NO-OP and returns false. Otherwise, returns true.
322 bool SelectRange(const Range& range);
324 // Returns true if the local point is over selected text.
325 bool IsPointInSelection(const Point& point);
327 // Selects no text, keeping the current cursor position and caret affinity.
328 void ClearSelection();
330 // Select the entire text range. If |reversed| is true, the range will end at
331 // the logical beginning of the text; this generally shows the leading portion
332 // of text that overflows its display area.
333 void SelectAll(bool reversed);
335 // Selects the word at the current cursor position. If there is a non-empty
336 // selection, the selection bounds are extended to their nearest word
337 // boundaries.
338 void SelectWord();
340 void SetCompositionRange(const Range& composition_range);
342 // Set the text color over the entire text or a logical character range.
343 // The |range| should be valid, non-reversed, and within [0, text().length()].
344 void SetColor(SkColor value);
345 void ApplyColor(SkColor value, const Range& range);
347 // Set the baseline style over the entire text or a logical character range.
348 // The |range| should be valid, non-reversed, and within [0, text().length()].
349 void SetBaselineStyle(BaselineStyle value);
350 void ApplyBaselineStyle(BaselineStyle value, const Range& range);
352 // Set various text styles over the entire text or a logical character range.
353 // The respective |style| is applied if |value| is true, or removed if false.
354 // The |range| should be valid, non-reversed, and within [0, text().length()].
355 void SetStyle(TextStyle style, bool value);
356 void ApplyStyle(TextStyle style, bool value, const Range& range);
358 // Returns whether this style is enabled consistently across the entire
359 // RenderText.
360 bool GetStyle(TextStyle style) const;
362 // Set or get the text directionality mode and get the text direction yielded.
363 void SetDirectionalityMode(DirectionalityMode mode);
364 DirectionalityMode directionality_mode() const {
365 return directionality_mode_;
367 base::i18n::TextDirection GetDisplayTextDirection();
369 // Returns the visual movement direction corresponding to the logical end
370 // of the text, considering only the dominant direction returned by
371 // |GetDisplayTextDirection()|, not the direction of a particular run.
372 VisualCursorDirection GetVisualDirectionOfLogicalEnd();
374 // Returns the text used to display, which may be obscured, truncated or
375 // elided. The subclass may compute elided text on the fly, or use
376 // precomputed the elided text.
377 virtual const base::string16& GetDisplayText() = 0;
379 // Returns the size required to display the current string (which is the
380 // wrapped size in multiline mode). The returned size does not include space
381 // reserved for the cursor or the offset text shadows.
382 virtual Size GetStringSize() = 0;
384 // This is same as GetStringSize except that fractional size is returned.
385 // The default implementation is same as GetStringSize. Certain platforms that
386 // compute the text size as floating-point values, like Mac, will override
387 // this method.
388 // See comment in Canvas::GetStringWidthF for its usage.
389 virtual SizeF GetStringSizeF();
391 // Returns the width of the content (which is the wrapped width in multiline
392 // mode). Reserves room for the cursor if |cursor_enabled_| is true.
393 float GetContentWidthF();
395 // Same as GetContentWidthF with the width rounded up.
396 int GetContentWidth();
398 // Returns the common baseline of the text. The return value is the vertical
399 // offset from the top of |display_rect_| to the text baseline, in pixels.
400 // The baseline is determined from the font list and display rect, and does
401 // not depend on the text.
402 int GetBaseline();
404 void Draw(Canvas* canvas);
406 // Draws a cursor at |position|.
407 void DrawCursor(Canvas* canvas, const SelectionModel& position);
409 // Gets the SelectionModel from a visual point in local coordinates.
410 virtual SelectionModel FindCursorPosition(const Point& point) = 0;
412 // Returns true if the position is a valid logical index into text(), and is
413 // also a valid grapheme boundary, which may be used as a cursor position.
414 virtual bool IsValidCursorIndex(size_t index) = 0;
416 // Returns true if the position is a valid logical index into text(). Indices
417 // amid multi-character graphemes are allowed here, unlike IsValidCursorIndex.
418 virtual bool IsValidLogicalIndex(size_t index) const;
420 // Get the visual bounds of a cursor at |caret|. These bounds typically
421 // represent a vertical line if |insert_mode| is true. Pass false for
422 // |insert_mode| to retrieve the bounds of the associated glyph. These bounds
423 // are in local coordinates, but may be outside the visible region if the text
424 // is longer than the textfield. Subsequent text, cursor, or bounds changes
425 // may invalidate returned values. Note that |caret| must be placed at
426 // grapheme boundary, i.e. caret.caret_pos() must be a cursorable position.
427 Rect GetCursorBounds(const SelectionModel& caret, bool insert_mode);
429 // Compute the current cursor bounds, panning the text to show the cursor in
430 // the display rect if necessary. These bounds are in local coordinates.
431 // Subsequent text, cursor, or bounds changes may invalidate returned values.
432 const Rect& GetUpdatedCursorBounds();
434 // Given an |index| in text(), return the next or previous grapheme boundary
435 // in logical order (i.e. the nearest cursorable index). The return value is
436 // in the range 0 to text().length() inclusive (the input is clamped if it is
437 // out of that range). Always moves by at least one character index unless the
438 // supplied index is already at the boundary of the string.
439 size_t IndexOfAdjacentGrapheme(size_t index,
440 LogicalCursorDirection direction);
442 // Return a SelectionModel with the cursor at the current selection's start.
443 // The returned value represents a cursor/caret position without a selection.
444 SelectionModel GetSelectionModelForSelectionStart() const;
446 // Sets shadows to drawn with text.
447 void set_shadows(const ShadowValues& shadows) { shadows_ = shadows; }
448 const ShadowValues& shadows() const { return shadows_; }
450 typedef std::pair<Font, Range> FontSpan;
451 // For testing purposes, returns which fonts were chosen for which parts of
452 // the text by returning a vector of Font and Range pairs, where each range
453 // specifies the character range for which the corresponding font has been
454 // chosen.
455 virtual std::vector<FontSpan> GetFontSpansForTesting() = 0;
457 // Gets the horizontal bounds (relative to the left of the text, not the view)
458 // of the glyph starting at |index|. If the glyph is RTL then the returned
459 // Range will have is_reversed() true. (This does not return a Rect because a
460 // Rect can't have a negative width.)
461 virtual Range GetGlyphBounds(size_t index) = 0;
463 const Vector2d& GetUpdatedDisplayOffset();
464 void SetDisplayOffset(int horizontal_offset);
466 // Returns the line offset from the origin after applying the text alignment
467 // and the display offset.
468 Vector2d GetLineOffset(size_t line_number);
470 protected:
471 RenderText();
473 // NOTE: The value of these accessors may be stale. Please make sure
474 // that these fields are up-to-date before accessing them.
475 const base::string16& layout_text() const { return layout_text_; }
476 const base::string16& display_text() const { return display_text_; }
477 bool text_elided() const { return text_elided_; }
479 const BreakList<SkColor>& colors() const { return colors_; }
480 const BreakList<BaselineStyle>& baselines() const { return baselines_; }
481 const std::vector<BreakList<bool> >& styles() const { return styles_; }
483 const std::vector<internal::Line>& lines() const { return lines_; }
484 void set_lines(std::vector<internal::Line>* lines) { lines_.swap(*lines); }
486 // Returns the baseline of the current text. The return value depends on
487 // the text and its layout while the return value of GetBaseline() doesn't.
488 // GetAlignmentOffset() takes into account the difference between them.
490 // We'd like a RenderText to show the text always on the same baseline
491 // regardless of the text, so the text does not jump up or down depending
492 // on the text. However, underlying layout engines return different baselines
493 // depending on the text. In general, layout engines determine the minimum
494 // bounding box for the text and return the baseline from the top of the
495 // bounding box. So the baseline changes depending on font metrics used to
496 // layout the text.
498 // For example, suppose there are FontA and FontB and the baseline of FontA
499 // is smaller than the one of FontB. If the text is laid out only with FontA,
500 // then the baseline of FontA may be returned. If the text includes some
501 // characters which are laid out with FontB, then the baseline of FontB may
502 // be returned.
504 // GetBaseline() returns the fixed baseline regardless of the text.
505 // GetDisplayTextBaseline() returns the baseline determined by the underlying
506 // layout engine, and it changes depending on the text. GetAlignmentOffset()
507 // returns the difference between them.
508 virtual int GetDisplayTextBaseline() = 0;
510 void set_cached_bounds_and_offset_valid(bool valid) {
511 cached_bounds_and_offset_valid_ = valid;
514 // Get the selection model that visually neighbors |position| by |break_type|.
515 // The returned value represents a cursor/caret position without a selection.
516 SelectionModel GetAdjacentSelectionModel(const SelectionModel& current,
517 BreakType break_type,
518 VisualCursorDirection direction);
520 // Get the selection model visually left/right of |selection| by one grapheme.
521 // The returned value represents a cursor/caret position without a selection.
522 virtual SelectionModel AdjacentCharSelectionModel(
523 const SelectionModel& selection,
524 VisualCursorDirection direction) = 0;
526 // Get the selection model visually left/right of |selection| by one word.
527 // The returned value represents a cursor/caret position without a selection.
528 virtual SelectionModel AdjacentWordSelectionModel(
529 const SelectionModel& selection,
530 VisualCursorDirection direction) = 0;
532 // Get the SelectionModels corresponding to visual text ends.
533 // The returned value represents a cursor/caret position without a selection.
534 SelectionModel EdgeSelectionModel(VisualCursorDirection direction);
536 // Sets the selection model, the argument is assumed to be valid.
537 virtual void SetSelectionModel(const SelectionModel& model);
539 // Get the visual bounds containing the logical substring within the |range|.
540 // If |range| is empty, the result is empty. These bounds could be visually
541 // discontinuous if the substring is split by a LTR/RTL level change.
542 // These bounds are in local coordinates, but may be outside the visible
543 // region if the text is longer than the textfield. Subsequent text, cursor,
544 // or bounds changes may invalidate returned values.
545 virtual std::vector<Rect> GetSubstringBounds(const Range& range) = 0;
547 // Convert between indices into |text_| and indices into
548 // GetDisplayText(), which differ when the text is obscured,
549 // truncated or elided. Regardless of whether or not the text is
550 // obscured, the character (code point) offsets always match.
551 virtual size_t TextIndexToDisplayIndex(size_t index) = 0;
552 virtual size_t DisplayIndexToTextIndex(size_t index) = 0;
554 // Notifies that layout text, or attributes that affect the layout text
555 // shape have changed. |text_changed| is true if the content of the
556 // |layout_text_| has changed, not just attributes.
557 virtual void OnLayoutTextAttributeChanged(bool text_changed) = 0;
559 // Notifies that attributes that affect the display text shape have changed.
560 virtual void OnDisplayTextAttributeChanged() = 0;
562 // Ensure the text is laid out, lines are computed, and |lines_| is valid.
563 virtual void EnsureLayout() = 0;
565 // Draw the text.
566 virtual void DrawVisualText(Canvas* canvas) = 0;
568 // Update the display text.
569 void UpdateDisplayText(float text_width);
571 // Returns display text positions that are suitable for breaking lines.
572 const BreakList<size_t>& GetLineBreaks();
574 // Apply (and undo) temporary composition underlines and selection colors.
575 void ApplyCompositionAndSelectionStyles();
576 void UndoCompositionAndSelectionStyles();
578 // Convert points from the text space to the view space and back. Handles the
579 // display area, display offset, application LTR/RTL mode and multiline.
580 Point ToTextPoint(const Point& point);
581 Point ToViewPoint(const Point& point);
583 // Convert a text space x-coordinate range to rects in view space.
584 std::vector<Rect> TextBoundsToViewBounds(const Range& x);
586 // Get the alignment, resolving ALIGN_TO_HEAD with the current text direction.
587 HorizontalAlignment GetCurrentHorizontalAlignment();
589 // Returns the line offset from the origin, accounts for text alignment only.
590 Vector2d GetAlignmentOffset(size_t line_number);
592 // Applies fade effects to |renderer|.
593 void ApplyFadeEffects(internal::SkiaTextRenderer* renderer);
595 // Applies text shadows to |renderer|.
596 void ApplyTextShadows(internal::SkiaTextRenderer* renderer);
598 // Get the text direction for the current directionality mode and given
599 // |text|.
600 base::i18n::TextDirection GetTextDirection(const base::string16& text);
602 // Convert an index in |text_| to the index in |given_text|. The
603 // |given_text| should be either |display_text_| or |layout_text_|
604 // depending on the elide state.
605 size_t TextIndexToGivenTextIndex(const base::string16& given_text,
606 size_t index) const;
608 // Adjust ranged styles to accommodate a new text length.
609 void UpdateStyleLengths();
611 // A convenience function to check whether the glyph attached to the caret
612 // is within the given range.
613 static bool RangeContainsCaret(const Range& range,
614 size_t caret_pos,
615 LogicalCursorDirection caret_affinity);
617 private:
618 friend class RenderTextTest;
619 FRIEND_TEST_ALL_PREFIXES(RenderTextTest, DefaultStyles);
620 FRIEND_TEST_ALL_PREFIXES(RenderTextTest, SetStyles);
621 FRIEND_TEST_ALL_PREFIXES(RenderTextTest, ApplyStyles);
622 FRIEND_TEST_ALL_PREFIXES(RenderTextTest, AppendTextKeepsStyles);
623 FRIEND_TEST_ALL_PREFIXES(RenderTextTest, ObscuredText);
624 FRIEND_TEST_ALL_PREFIXES(RenderTextTest, RevealObscuredText);
625 FRIEND_TEST_ALL_PREFIXES(RenderTextTest, ElidedText);
626 FRIEND_TEST_ALL_PREFIXES(RenderTextTest, ElidedObscuredText);
627 FRIEND_TEST_ALL_PREFIXES(RenderTextTest, TruncatedText);
628 FRIEND_TEST_ALL_PREFIXES(RenderTextTest, TruncatedObscuredText);
629 FRIEND_TEST_ALL_PREFIXES(RenderTextTest, GraphemePositions);
630 FRIEND_TEST_ALL_PREFIXES(RenderTextTest, MinLineHeight);
631 FRIEND_TEST_ALL_PREFIXES(RenderTextTest, EdgeSelectionModels);
632 FRIEND_TEST_ALL_PREFIXES(RenderTextTest, GetTextOffset);
633 FRIEND_TEST_ALL_PREFIXES(RenderTextTest, GetTextOffsetHorizontalDefaultInRTL);
634 FRIEND_TEST_ALL_PREFIXES(RenderTextTest, Multiline_MinWidth);
635 FRIEND_TEST_ALL_PREFIXES(RenderTextTest, Multiline_NormalWidth);
636 FRIEND_TEST_ALL_PREFIXES(RenderTextTest, Multiline_SufficientWidth);
637 FRIEND_TEST_ALL_PREFIXES(RenderTextTest, Multiline_Newline);
638 FRIEND_TEST_ALL_PREFIXES(RenderTextTest, Multiline_WordWrapBehavior);
639 FRIEND_TEST_ALL_PREFIXES(RenderTextTest, Multiline_LineBreakerBehavior);
640 FRIEND_TEST_ALL_PREFIXES(RenderTextTest,
641 Multiline_SurrogatePairsOrCombiningChars);
642 FRIEND_TEST_ALL_PREFIXES(RenderTextTest, Multiline_ZeroWidthChars);
643 FRIEND_TEST_ALL_PREFIXES(RenderTextTest, NewlineWithoutMultilineFlag);
644 FRIEND_TEST_ALL_PREFIXES(RenderTextTest, GlyphBounds);
645 FRIEND_TEST_ALL_PREFIXES(RenderTextTest, HarfBuzz_GlyphBounds);
646 FRIEND_TEST_ALL_PREFIXES(RenderTextTest,
647 MoveCursorLeftRight_MeiryoUILigatures);
648 FRIEND_TEST_ALL_PREFIXES(RenderTextTest, Win_LogicalClusters);
649 FRIEND_TEST_ALL_PREFIXES(RenderTextTest, SameFontForParentheses);
650 FRIEND_TEST_ALL_PREFIXES(RenderTextTest, BreakRunsByUnicodeBlocks);
651 FRIEND_TEST_ALL_PREFIXES(RenderTextTest, PangoAttributes);
652 FRIEND_TEST_ALL_PREFIXES(RenderTextTest, StringFitsOwnWidth);
654 // Set the cursor to |position|, with the caret trailing the previous
655 // grapheme, or if there is no previous grapheme, leading the cursor position.
656 // If |select| is false, the selection start is moved to the same position.
657 // If the |position| is not a cursorable position (not on grapheme boundary),
658 // it is a NO-OP.
659 void MoveCursorTo(size_t position, bool select);
661 // Updates |layout_text_| and |display_text_| as needed (or marks them dirty).
662 void OnTextAttributeChanged();
664 // Elides |text| as needed to fit in the |available_width| using |behavior|.
665 // |text_width| is the pre-calculated width of the text shaped by this render
666 // text, or pass 0 if the width is unknown.
667 base::string16 Elide(const base::string16& text,
668 float text_width,
669 float available_width,
670 ElideBehavior behavior);
672 // Elides |email| as needed to fit the |available_width|.
673 base::string16 ElideEmail(const base::string16& email, float available_width);
675 // Update the cached bounds and display offset to ensure that the current
676 // cursor is within the visible display area.
677 void UpdateCachedBoundsAndOffset();
679 // Draw the selection.
680 void DrawSelection(Canvas* canvas);
682 // Logical UTF-16 string data to be drawn.
683 base::string16 text_;
685 // Horizontal alignment of the text with respect to |display_rect_|. The
686 // default is to align left if the application UI is LTR and right if RTL.
687 HorizontalAlignment horizontal_alignment_;
689 // The text directionality mode, defaults to DIRECTIONALITY_FROM_TEXT.
690 DirectionalityMode directionality_mode_;
692 // The cached text direction, potentially computed from the text or UI locale.
693 // Use GetTextDirection(), do not use this potentially invalid value directly!
694 base::i18n::TextDirection text_direction_;
696 // A list of fonts used to render |text_|.
697 FontList font_list_;
699 // Logical selection range and visual cursor position.
700 SelectionModel selection_model_;
702 // The cached cursor bounds; get these bounds with GetUpdatedCursorBounds.
703 Rect cursor_bounds_;
705 // Specifies whether the cursor is enabled. If disabled, no space is reserved
706 // for the cursor when positioning text.
707 bool cursor_enabled_;
709 // The cursor visibility and insert mode.
710 bool cursor_visible_;
711 bool insert_mode_;
713 // The color used for the cursor.
714 SkColor cursor_color_;
716 // The color used for drawing selected text.
717 SkColor selection_color_;
719 // The background color used for drawing the selection when focused.
720 SkColor selection_background_focused_color_;
722 // The focus state of the text.
723 bool focused_;
725 // Composition text range.
726 Range composition_range_;
728 // Color, baseline, and style breaks, used to modify ranges of text.
729 // BreakList positions are stored with text indices, not display indices.
730 // TODO(msw): Expand to support cursor, selection, background, etc. colors.
731 BreakList<SkColor> colors_;
732 BreakList<BaselineStyle> baselines_;
733 std::vector<BreakList<bool> > styles_;
735 // Breaks saved without temporary composition and selection styling.
736 BreakList<SkColor> saved_colors_;
737 BreakList<bool> saved_underlines_;
738 bool composition_and_selection_styles_applied_;
740 // A flag to obscure actual text with asterisks for password fields.
741 bool obscured_;
742 // The index at which the char should be revealed in the obscured text.
743 int obscured_reveal_index_;
745 // The maximum length of text to display, 0 forgoes a hard limit.
746 size_t truncate_length_;
748 // The obscured and/or truncated text used to layout the text to display.
749 base::string16 layout_text_;
751 // The elided text displayed visually. This is empty if the text
752 // does not have to be elided, or became empty as a result of eliding.
753 // TODO(oshima): When the text is elided, painting can be done only with
754 // display text info, so it should be able to clear the |layout_text_| and
755 // associated information.
756 base::string16 display_text_;
758 // The behavior for eliding, fading, or truncating.
759 ElideBehavior elide_behavior_;
761 // True if the text is elided given the current behavior and display area.
762 bool text_elided_;
764 // The minimum height a line should have.
765 int min_line_height_;
767 // Whether the text should be broken into multiple lines. Uses the width of
768 // |display_rect_| as the width cap.
769 bool multiline_;
771 // The wrap behavior when the text is broken into lines. Do nothing unless
772 // |multiline_| is set. The default value is IGNORE_LONG_WORDS.
773 WordWrapBehavior word_wrap_behavior_;
775 // Whether newline characters should be replaced with newline symbols.
776 bool replace_newline_chars_with_symbols_;
778 // Set to true to suppress subpixel rendering due to non-font reasons (eg.
779 // if the background is transparent). The default value is false.
780 bool subpixel_rendering_suppressed_;
782 // The local display area for rendering the text.
783 Rect display_rect_;
785 // Flag to work around a Skia bug with the PDF path (http://crbug.com/133548)
786 // that results in incorrect clipping when drawing to the document margins.
787 // This field allows disabling clipping to work around the issue.
788 // TODO(asvitkine): Remove this when the underlying Skia bug is fixed.
789 bool clip_to_display_rect_;
791 // The offset for the text to be drawn, relative to the display area.
792 // Get this point with GetUpdatedDisplayOffset (or risk using a stale value).
793 Vector2d display_offset_;
795 // The baseline of the text. This is determined from the height of the
796 // display area and the cap height of the font list so the text is vertically
797 // centered.
798 int baseline_;
800 // The cached bounds and offset are invalidated by changes to the cursor,
801 // selection, font, and other operations that adjust the visible text bounds.
802 bool cached_bounds_and_offset_valid_;
804 // Text shadows to be drawn.
805 ShadowValues shadows_;
807 // A list of valid display text line break positions.
808 BreakList<size_t> line_breaks_;
810 // Lines computed by EnsureLayout. These should be invalidated upon
811 // OnLayoutTextAttributeChanged and OnDisplayTextAttributeChanged calls.
812 std::vector<internal::Line> lines_;
814 DISALLOW_COPY_AND_ASSIGN(RenderText);
817 } // namespace gfx
819 #endif // UI_GFX_RENDER_TEXT_H_