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_
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/selection_model.h"
30 #include "ui/gfx/shadow_value.h"
31 #include "ui/gfx/text_constants.h"
47 // Internal helper class used by derived classes to draw text through Skia.
48 class GFX_EXPORT SkiaTextRenderer
{
50 explicit SkiaTextRenderer(Canvas
* canvas
);
51 virtual ~SkiaTextRenderer();
53 void SetDrawLooper(SkDrawLooper
* draw_looper
);
54 void SetFontRenderParams(const FontRenderParams
& params
,
55 bool subpixel_rendering_suppressed
);
56 void SetTypeface(SkTypeface
* typeface
);
57 void SetTextSize(SkScalar size
);
58 void SetFontFamilyWithStyle(const std::string
& family
, int font_style
);
59 void SetForegroundColor(SkColor foreground
);
60 void SetShader(SkShader
* shader
);
61 // Sets underline metrics to use if the text will be drawn with an underline.
62 // If not set, default values based on the size of the text will be used. The
63 // two metrics must be set together.
64 void SetUnderlineMetrics(SkScalar thickness
, SkScalar position
);
65 void DrawSelection(const std::vector
<Rect
>& selection
, SkColor color
);
66 virtual void DrawPosText(const SkPoint
* pos
,
69 // Draw underline and strike-through text decorations.
70 // Based on |SkCanvas::DrawTextDecorations()| and constants from:
71 // third_party/skia/src/core/SkTextFormatParams.h
72 virtual void DrawDecorations(int x
, int y
, int width
, bool underline
,
73 bool strike
, bool diagonal_strike
);
74 // Finishes any ongoing diagonal strike run.
75 void EndDiagonalStrike();
76 void DrawUnderline(int x
, int y
, int width
);
77 void DrawStrike(int x
, int y
, int width
) const;
80 // Helper class to draw a diagonal line with multiple pieces of different
81 // lengths and colors; to support text selection appearances.
82 class DiagonalStrike
{
84 DiagonalStrike(Canvas
* canvas
, Point start
, const SkPaint
& paint
);
87 void AddPiece(int length
, SkColor color
);
91 typedef std::pair
<int, SkColor
> Piece
;
97 std::vector
<Piece
> pieces_
;
99 DISALLOW_COPY_AND_ASSIGN(DiagonalStrike
);
103 SkCanvas
* canvas_skia_
;
105 SkScalar underline_thickness_
;
106 SkScalar underline_position_
;
107 scoped_ptr
<DiagonalStrike
> diagonal_
;
109 DISALLOW_COPY_AND_ASSIGN(SkiaTextRenderer
);
112 // Internal helper class used to iterate colors, baselines, and styles.
113 class StyleIterator
{
115 StyleIterator(const BreakList
<SkColor
>& colors
,
116 const BreakList
<BaselineStyle
>& baselines
,
117 const std::vector
<BreakList
<bool>>& styles
);
120 // Get the colors and styles at the current iterator position.
121 SkColor
color() const { return color_
->second
; }
122 BaselineStyle
baseline() const { return baseline_
->second
; }
123 bool style(TextStyle s
) const { return style_
[s
]->second
; }
125 // Get the intersecting range of the current iterator set.
126 Range
GetRange() const;
128 // Update the iterator to point to colors and styles applicable at |position|.
129 void UpdatePosition(size_t position
);
132 BreakList
<SkColor
> colors_
;
133 BreakList
<BaselineStyle
> baselines_
;
134 std::vector
<BreakList
<bool> > styles_
;
136 BreakList
<SkColor
>::const_iterator color_
;
137 BreakList
<BaselineStyle
>::const_iterator baseline_
;
138 std::vector
<BreakList
<bool>::const_iterator
> style_
;
140 DISALLOW_COPY_AND_ASSIGN(StyleIterator
);
143 // Line segments are slices of the display text to be rendered on a single line.
148 // X coordinates of this line segment in text space.
151 // The character range this segment corresponds to.
154 // The width of this line segment in text space. This could be slightly
155 // different from x_range.length().
156 // TODO(mukai): Fix Range to support float values and merge it into x_range.
159 // Index of the text run that generated this segment.
163 // A line of display text, comprised of a line segment list and some metrics.
168 // Segments that make up this line in visual order.
169 std::vector
<LineSegment
> segments
;
171 // The sum of segment widths and the maximum of segment heights.
174 // Sum of preceding lines' heights.
175 int preceding_heights
;
177 // Maximum baseline of all segments on this line.
181 // Creates an SkTypeface from a font |family| name and a |gfx::Font::FontStyle|.
183 skia::RefPtr
<SkTypeface
> CreateSkiaTypeface(const std::string
& family
,
186 // Applies the given FontRenderParams to a Skia |paint|.
187 void ApplyRenderParams(const FontRenderParams
& params
,
188 bool subpixel_rendering_suppressed
,
191 } // namespace internal
193 // RenderText represents an abstract model of styled text and its corresponding
194 // visual layout. Support is built in for a cursor, a selection, simple styling,
195 // complex scripts, and bi-directional text. Implementations provide mechanisms
196 // for rendering and translation between logical and visual data.
197 class GFX_EXPORT RenderText
{
199 virtual ~RenderText();
201 // Creates a platform-specific or cross-platform RenderText instance.
202 static RenderText
* CreateInstance();
203 static RenderText
* CreateInstanceForEditing();
205 // Creates another instance of the same concrete class.
206 virtual scoped_ptr
<RenderText
> CreateInstanceOfSameType() const = 0;
208 const base::string16
& text() const { return text_
; }
209 void SetText(const base::string16
& text
);
210 void AppendText(const base::string16
& text
);
212 HorizontalAlignment
horizontal_alignment() const {
213 return horizontal_alignment_
;
215 void SetHorizontalAlignment(HorizontalAlignment alignment
);
217 const FontList
& font_list() const { return font_list_
; }
218 void SetFontList(const FontList
& font_list
);
220 bool cursor_enabled() const { return cursor_enabled_
; }
221 void SetCursorEnabled(bool cursor_enabled
);
223 bool cursor_visible() const { return cursor_visible_
; }
224 void set_cursor_visible(bool visible
) { cursor_visible_
= visible
; }
226 bool insert_mode() const { return insert_mode_
; }
227 void ToggleInsertMode();
229 SkColor
cursor_color() const { return cursor_color_
; }
230 void set_cursor_color(SkColor color
) { cursor_color_
= color
; }
232 SkColor
selection_color() const { return selection_color_
; }
233 void set_selection_color(SkColor color
) { selection_color_
= color
; }
235 SkColor
selection_background_focused_color() const {
236 return selection_background_focused_color_
;
238 void set_selection_background_focused_color(SkColor color
) {
239 selection_background_focused_color_
= color
;
242 bool focused() const { return focused_
; }
243 void set_focused(bool focused
) { focused_
= focused
; }
245 bool clip_to_display_rect() const { return clip_to_display_rect_
; }
246 void set_clip_to_display_rect(bool clip
) { clip_to_display_rect_
= clip
; }
248 // In an obscured (password) field, all text is drawn as asterisks or bullets.
249 bool obscured() const { return obscured_
; }
250 void SetObscured(bool obscured
);
252 // Makes a char in obscured text at |index| to be revealed. |index| should be
253 // a UTF16 text index. If there is a previous revealed index, the previous one
254 // is cleared and only the last set index will be revealed. If |index| is -1
255 // or out of range, no char will be revealed. The revealed index is also
256 // cleared when SetText or SetObscured is called.
257 void SetObscuredRevealIndex(int index
);
259 // TODO(ckocagil): Multiline text rendering is currently only supported on
260 // Windows. Support other platforms.
261 bool multiline() const { return multiline_
; }
262 void SetMultiline(bool multiline
);
264 // Set whether newline characters should be replaced with newline symbols.
265 void SetReplaceNewlineCharsWithSymbols(bool replace
);
267 // Returns true if this instance supports multiline rendering.
268 virtual bool MultilineSupported() const = 0;
270 // TODO(ckocagil): Add vertical alignment and line spacing support instead.
271 int min_line_height() const { return min_line_height_
; }
272 void SetMinLineHeight(int line_height
);
274 // Set the maximum length of the layout text, not the actual text.
275 // A |length| of 0 forgoes a hard limit, but does not guarantee proper
276 // functionality of very long strings. Applies to subsequent SetText calls.
277 // WARNING: Only use this for system limits, it lacks complex text support.
278 void set_truncate_length(size_t length
) { truncate_length_
= length
; }
280 // The display text will be elided to fit |display_rect| using this behavior.
281 void SetElideBehavior(ElideBehavior elide_behavior
);
282 ElideBehavior
elide_behavior() const { return elide_behavior_
; }
284 const Rect
& display_rect() const { return display_rect_
; }
285 void SetDisplayRect(const Rect
& r
);
287 bool subpixel_rendering_suppressed() const {
288 return subpixel_rendering_suppressed_
;
290 void set_subpixel_rendering_suppressed(bool suppressed
) {
291 subpixel_rendering_suppressed_
= suppressed
;
294 const SelectionModel
& selection_model() const { return selection_model_
; }
296 const Range
& selection() const { return selection_model_
.selection(); }
298 size_t cursor_position() const { return selection_model_
.caret_pos(); }
299 void SetCursorPosition(size_t position
);
301 // Moves the cursor left or right. Cursor movement is visual, meaning that
302 // left and right are relative to screen, not the directionality of the text.
303 // If |select| is false, the selection start is moved to the same position.
304 void MoveCursor(BreakType break_type
,
305 VisualCursorDirection direction
,
308 // Set the selection_model_ to the value of |selection|.
309 // The selection range is clamped to text().length() if out of range.
310 // Returns true if the cursor position or selection range changed.
311 // If any index in |selection_model| is not a cursorable position (not on a
312 // grapheme boundary), it is a no-op and returns false.
313 bool MoveCursorTo(const SelectionModel
& selection_model
);
315 // Set the selection_model_ based on |range|.
316 // If the |range| start or end is greater than text length, it is modified
317 // to be the text length.
318 // If the |range| start or end is not a cursorable position (not on grapheme
319 // boundary), it is a NO-OP and returns false. Otherwise, returns true.
320 bool SelectRange(const Range
& range
);
322 // Returns true if the local point is over selected text.
323 bool IsPointInSelection(const Point
& point
);
325 // Selects no text, keeping the current cursor position and caret affinity.
326 void ClearSelection();
328 // Select the entire text range. If |reversed| is true, the range will end at
329 // the logical beginning of the text; this generally shows the leading portion
330 // of text that overflows its display area.
331 void SelectAll(bool reversed
);
333 // Selects the word at the current cursor position. If there is a non-empty
334 // selection, the selection bounds are extended to their nearest word
338 const Range
& GetCompositionRange() const;
339 void SetCompositionRange(const Range
& composition_range
);
341 // Set the text color over the entire text or a logical character range.
342 // The |range| should be valid, non-reversed, and within [0, text().length()].
343 void SetColor(SkColor value
);
344 void ApplyColor(SkColor value
, const Range
& range
);
346 // Set the baseline style over the entire text or a logical character range.
347 // The |range| should be valid, non-reversed, and within [0, text().length()].
348 void SetBaselineStyle(BaselineStyle value
);
349 void ApplyBaselineStyle(BaselineStyle value
, const Range
& range
);
351 // Set various text styles over the entire text or a logical character range.
352 // The respective |style| is applied if |value| is true, or removed if false.
353 // The |range| should be valid, non-reversed, and within [0, text().length()].
354 void SetStyle(TextStyle style
, bool value
);
355 void ApplyStyle(TextStyle style
, bool value
, const Range
& range
);
357 // Returns whether this style is enabled consistently across the entire
359 bool GetStyle(TextStyle style
) const;
361 // Set or get the text directionality mode and get the text direction yielded.
362 void SetDirectionalityMode(DirectionalityMode mode
);
363 DirectionalityMode
directionality_mode() const {
364 return directionality_mode_
;
366 base::i18n::TextDirection
GetDisplayTextDirection();
368 // Returns the visual movement direction corresponding to the logical end
369 // of the text, considering only the dominant direction returned by
370 // |GetDisplayTextDirection()|, not the direction of a particular run.
371 VisualCursorDirection
GetVisualDirectionOfLogicalEnd();
373 // Returns the text used to display, which may be obscured, truncated or
374 // elided. The subclass may compute elided text on the fly, or use
375 // precomputed the elided text.
376 virtual const base::string16
& GetDisplayText() = 0;
378 // Returns the size required to display the current string (which is the
379 // wrapped size in multiline mode). The returned size does not include space
380 // reserved for the cursor or the offset text shadows.
381 virtual Size
GetStringSize() = 0;
383 // This is same as GetStringSize except that fractional size is returned.
384 // The default implementation is same as GetStringSize. Certain platforms that
385 // compute the text size as floating-point values, like Mac, will override
387 // See comment in Canvas::GetStringWidthF for its usage.
388 virtual SizeF
GetStringSizeF();
390 // Returns the width of the content (which is the wrapped width in multiline
391 // mode). Reserves room for the cursor if |cursor_enabled_| is true.
392 float GetContentWidthF();
394 // Same as GetContentWidthF with the width rounded up.
395 int GetContentWidth();
397 // Returns the common baseline of the text. The return value is the vertical
398 // offset from the top of |display_rect_| to the text baseline, in pixels.
399 // The baseline is determined from the font list and display rect, and does
400 // not depend on the text.
403 void Draw(Canvas
* canvas
);
405 // Draws a cursor at |position|.
406 void DrawCursor(Canvas
* canvas
, const SelectionModel
& position
);
408 // Gets the SelectionModel from a visual point in local coordinates.
409 virtual SelectionModel
FindCursorPosition(const Point
& point
) = 0;
411 // Returns true if the position is a valid logical index into text(), and is
412 // also a valid grapheme boundary, which may be used as a cursor position.
413 virtual bool IsValidCursorIndex(size_t index
) = 0;
415 // Returns true if the position is a valid logical index into text(). Indices
416 // amid multi-character graphemes are allowed here, unlike IsValidCursorIndex.
417 virtual bool IsValidLogicalIndex(size_t index
);
419 // Get the visual bounds of a cursor at |caret|. These bounds typically
420 // represent a vertical line if |insert_mode| is true. Pass false for
421 // |insert_mode| to retrieve the bounds of the associated glyph. These bounds
422 // are in local coordinates, but may be outside the visible region if the text
423 // is longer than the textfield. Subsequent text, cursor, or bounds changes
424 // may invalidate returned values. Note that |caret| must be placed at
425 // grapheme boundary, i.e. caret.caret_pos() must be a cursorable position.
426 Rect
GetCursorBounds(const SelectionModel
& caret
, bool insert_mode
);
428 // Compute the current cursor bounds, panning the text to show the cursor in
429 // the display rect if necessary. These bounds are in local coordinates.
430 // Subsequent text, cursor, or bounds changes may invalidate returned values.
431 const Rect
& GetUpdatedCursorBounds();
433 // Given an |index| in text(), return the next or previous grapheme boundary
434 // in logical order (i.e. the nearest cursorable index). The return value is
435 // in the range 0 to text().length() inclusive (the input is clamped if it is
436 // out of that range). Always moves by at least one character index unless the
437 // supplied index is already at the boundary of the string.
438 size_t IndexOfAdjacentGrapheme(size_t index
,
439 LogicalCursorDirection direction
);
441 // Return a SelectionModel with the cursor at the current selection's start.
442 // The returned value represents a cursor/caret position without a selection.
443 SelectionModel
GetSelectionModelForSelectionStart();
445 // Sets shadows to drawn with text.
446 void set_shadows(const ShadowValues
& shadows
) { shadows_
= shadows
; }
447 const ShadowValues
& shadows() const { return shadows_
; }
449 typedef std::pair
<Font
, Range
> FontSpan
;
450 // For testing purposes, returns which fonts were chosen for which parts of
451 // the text by returning a vector of Font and Range pairs, where each range
452 // specifies the character range for which the corresponding font has been
454 virtual std::vector
<FontSpan
> GetFontSpansForTesting() = 0;
456 // Gets the horizontal bounds (relative to the left of the text, not the view)
457 // of the glyph starting at |index|. If the glyph is RTL then the returned
458 // Range will have is_reversed() true. (This does not return a Rect because a
459 // Rect can't have a negative width.)
460 virtual Range
GetGlyphBounds(size_t index
) = 0;
462 const Vector2d
& GetUpdatedDisplayOffset();
463 void SetDisplayOffset(int horizontal_offset
);
465 // Returns the line offset from the origin after applying the text alignment
466 // and the display offset.
467 Vector2d
GetLineOffset(size_t line_number
);
472 // NOTE: The value of these accessors may be stale. Please make sure
473 // that these fields are up-to-date before accessing them.
474 const base::string16
& layout_text() const { return layout_text_
; }
475 const base::string16
& display_text() const { return display_text_
; }
476 bool text_elided() const { return text_elided_
; }
478 const BreakList
<SkColor
>& colors() const { return colors_
; }
479 const BreakList
<BaselineStyle
>& baselines() const { return baselines_
; }
480 const std::vector
<BreakList
<bool> >& styles() const { return styles_
; }
482 const std::vector
<internal::Line
>& lines() const { return lines_
; }
483 void set_lines(std::vector
<internal::Line
>* lines
) { lines_
.swap(*lines
); }
485 // Returns the baseline of the current text. The return value depends on
486 // the text and its layout while the return value of GetBaseline() doesn't.
487 // GetAlignmentOffset() takes into account the difference between them.
489 // We'd like a RenderText to show the text always on the same baseline
490 // regardless of the text, so the text does not jump up or down depending
491 // on the text. However, underlying layout engines return different baselines
492 // depending on the text. In general, layout engines determine the minimum
493 // bounding box for the text and return the baseline from the top of the
494 // bounding box. So the baseline changes depending on font metrics used to
497 // For example, suppose there are FontA and FontB and the baseline of FontA
498 // is smaller than the one of FontB. If the text is laid out only with FontA,
499 // then the baseline of FontA may be returned. If the text includes some
500 // characters which are laid out with FontB, then the baseline of FontB may
503 // GetBaseline() returns the fixed baseline regardless of the text.
504 // GetDisplayTextBaseline() returns the baseline determined by the underlying
505 // layout engine, and it changes depending on the text. GetAlignmentOffset()
506 // returns the difference between them.
507 virtual int GetDisplayTextBaseline() = 0;
509 void set_cached_bounds_and_offset_valid(bool valid
) {
510 cached_bounds_and_offset_valid_
= valid
;
513 // Get the selection model that visually neighbors |position| by |break_type|.
514 // The returned value represents a cursor/caret position without a selection.
515 SelectionModel
GetAdjacentSelectionModel(const SelectionModel
& current
,
516 BreakType break_type
,
517 VisualCursorDirection direction
);
519 // Get the selection model visually left/right of |selection| by one grapheme.
520 // The returned value represents a cursor/caret position without a selection.
521 virtual SelectionModel
AdjacentCharSelectionModel(
522 const SelectionModel
& selection
,
523 VisualCursorDirection direction
) = 0;
525 // Get the selection model visually left/right of |selection| by one word.
526 // The returned value represents a cursor/caret position without a selection.
527 virtual SelectionModel
AdjacentWordSelectionModel(
528 const SelectionModel
& selection
,
529 VisualCursorDirection direction
) = 0;
531 // Get the SelectionModels corresponding to visual text ends.
532 // The returned value represents a cursor/caret position without a selection.
533 SelectionModel
EdgeSelectionModel(VisualCursorDirection direction
);
535 // Sets the selection model, the argument is assumed to be valid.
536 virtual void SetSelectionModel(const SelectionModel
& model
);
538 // Get the visual bounds containing the logical substring within the |range|.
539 // If |range| is empty, the result is empty. These bounds could be visually
540 // discontinuous if the substring is split by a LTR/RTL level change.
541 // These bounds are in local coordinates, but may be outside the visible
542 // region if the text is longer than the textfield. Subsequent text, cursor,
543 // or bounds changes may invalidate returned values.
544 virtual std::vector
<Rect
> GetSubstringBounds(const Range
& range
) = 0;
546 // Convert between indices into |text_| and indices into
547 // GetDisplayText(), which differ when the text is obscured,
548 // truncated or elided. Regardless of whether or not the text is
549 // obscured, the character (code point) offsets always match.
550 virtual size_t TextIndexToDisplayIndex(size_t index
) = 0;
551 virtual size_t DisplayIndexToTextIndex(size_t index
) = 0;
553 // Notifies that layout text, or attributes that affect the layout text
554 // shape have changed. |text_changed| is true if the content of the
555 // |layout_text_| has changed, not just attributes.
556 virtual void OnLayoutTextAttributeChanged(bool text_changed
) = 0;
558 // Notifies that attributes that affect the display text shape have changed.
559 virtual void OnDisplayTextAttributeChanged() = 0;
561 // Ensure the text is laid out, lines are computed, and |lines_| is valid.
562 virtual void EnsureLayout() = 0;
565 virtual void DrawVisualText(Canvas
* canvas
) = 0;
567 // Update the display text.
568 void UpdateDisplayText(float text_width
);
570 // Returns display text positions that are suitable for breaking lines.
571 const BreakList
<size_t>& GetLineBreaks();
573 // Apply (and undo) temporary composition underlines and selection colors.
574 void ApplyCompositionAndSelectionStyles();
575 void UndoCompositionAndSelectionStyles();
577 // Convert points from the text space to the view space and back. Handles the
578 // display area, display offset, application LTR/RTL mode and multiline.
579 Point
ToTextPoint(const Point
& point
);
580 Point
ToViewPoint(const Point
& point
);
582 // Convert a text space x-coordinate range to rects in view space.
583 std::vector
<Rect
> TextBoundsToViewBounds(const Range
& x
);
585 // Get the alignment, resolving ALIGN_TO_HEAD with the current text direction.
586 HorizontalAlignment
GetCurrentHorizontalAlignment();
588 // Returns the line offset from the origin, accounts for text alignment only.
589 Vector2d
GetAlignmentOffset(size_t line_number
);
591 // Applies fade effects to |renderer|.
592 void ApplyFadeEffects(internal::SkiaTextRenderer
* renderer
);
594 // Applies text shadows to |renderer|.
595 void ApplyTextShadows(internal::SkiaTextRenderer
* renderer
);
597 // Get the text direction for the current directionality mode and given
599 base::i18n::TextDirection
GetTextDirection(const base::string16
& text
);
601 // Convert an index in |text_| to the index in |given_text|. The
602 // |given_text| should be either |display_text_| or |layout_text_|
603 // depending on the elide state.
604 size_t TextIndexToGivenTextIndex(const base::string16
& given_text
,
607 // Adjust ranged styles to accommodate a new text length.
608 void UpdateStyleLengths();
610 // A convenience function to check whether the glyph attached to the caret
611 // is within the given range.
612 static bool RangeContainsCaret(const Range
& range
,
614 LogicalCursorDirection caret_affinity
);
617 friend class RenderTextTest
;
618 FRIEND_TEST_ALL_PREFIXES(RenderTextTest
, DefaultStyles
);
619 FRIEND_TEST_ALL_PREFIXES(RenderTextTest
, SetStyles
);
620 FRIEND_TEST_ALL_PREFIXES(RenderTextTest
, ApplyStyles
);
621 FRIEND_TEST_ALL_PREFIXES(RenderTextTest
, AppendTextKeepsStyles
);
622 FRIEND_TEST_ALL_PREFIXES(RenderTextTest
, ObscuredText
);
623 FRIEND_TEST_ALL_PREFIXES(RenderTextTest
, RevealObscuredText
);
624 FRIEND_TEST_ALL_PREFIXES(RenderTextTest
, ElidedText
);
625 FRIEND_TEST_ALL_PREFIXES(RenderTextTest
, ElidedObscuredText
);
626 FRIEND_TEST_ALL_PREFIXES(RenderTextTest
, TruncatedText
);
627 FRIEND_TEST_ALL_PREFIXES(RenderTextTest
, TruncatedObscuredText
);
628 FRIEND_TEST_ALL_PREFIXES(RenderTextTest
, GraphemePositions
);
629 FRIEND_TEST_ALL_PREFIXES(RenderTextTest
, MinLineHeight
);
630 FRIEND_TEST_ALL_PREFIXES(RenderTextTest
, EdgeSelectionModels
);
631 FRIEND_TEST_ALL_PREFIXES(RenderTextTest
, GetTextOffset
);
632 FRIEND_TEST_ALL_PREFIXES(RenderTextTest
, GetTextOffsetHorizontalDefaultInRTL
);
633 FRIEND_TEST_ALL_PREFIXES(RenderTextTest
, Multiline_MinWidth
);
634 FRIEND_TEST_ALL_PREFIXES(RenderTextTest
, Multiline_NormalWidth
);
635 FRIEND_TEST_ALL_PREFIXES(RenderTextTest
, Multiline_SufficientWidth
);
636 FRIEND_TEST_ALL_PREFIXES(RenderTextTest
, Multiline_Newline
);
637 FRIEND_TEST_ALL_PREFIXES(RenderTextTest
, NewlineWithoutMultilineFlag
);
638 FRIEND_TEST_ALL_PREFIXES(RenderTextTest
, GlyphBounds
);
639 FRIEND_TEST_ALL_PREFIXES(RenderTextTest
, HarfBuzz_GlyphBounds
);
640 FRIEND_TEST_ALL_PREFIXES(RenderTextTest
,
641 MoveCursorLeftRight_MeiryoUILigatures
);
642 FRIEND_TEST_ALL_PREFIXES(RenderTextTest
, Win_LogicalClusters
);
643 FRIEND_TEST_ALL_PREFIXES(RenderTextTest
, SameFontForParentheses
);
644 FRIEND_TEST_ALL_PREFIXES(RenderTextTest
, BreakRunsByUnicodeBlocks
);
645 FRIEND_TEST_ALL_PREFIXES(RenderTextTest
, PangoAttributes
);
646 FRIEND_TEST_ALL_PREFIXES(RenderTextTest
, StringFitsOwnWidth
);
648 // Set the cursor to |position|, with the caret trailing the previous
649 // grapheme, or if there is no previous grapheme, leading the cursor position.
650 // If |select| is false, the selection start is moved to the same position.
651 // If the |position| is not a cursorable position (not on grapheme boundary),
653 void MoveCursorTo(size_t position
, bool select
);
655 // Updates |layout_text_| and |display_text_| as needed (or marks them dirty).
656 void OnTextAttributeChanged();
658 // Elides |text| as needed to fit in the |available_width| using |behavior|.
659 // |text_width| is the pre-calculated width of the text shaped by this render
660 // text, or pass 0 if the width is unknown.
661 base::string16
Elide(const base::string16
& text
,
663 float available_width
,
664 ElideBehavior behavior
);
666 // Elides |email| as needed to fit the |available_width|.
667 base::string16
ElideEmail(const base::string16
& email
, float available_width
);
669 // Update the cached bounds and display offset to ensure that the current
670 // cursor is within the visible display area.
671 void UpdateCachedBoundsAndOffset();
673 // Draw the selection.
674 void DrawSelection(Canvas
* canvas
);
676 // Logical UTF-16 string data to be drawn.
677 base::string16 text_
;
679 // Horizontal alignment of the text with respect to |display_rect_|. The
680 // default is to align left if the application UI is LTR and right if RTL.
681 HorizontalAlignment horizontal_alignment_
;
683 // The text directionality mode, defaults to DIRECTIONALITY_FROM_TEXT.
684 DirectionalityMode directionality_mode_
;
686 // The cached text direction, potentially computed from the text or UI locale.
687 // Use GetTextDirection(), do not use this potentially invalid value directly!
688 base::i18n::TextDirection text_direction_
;
690 // A list of fonts used to render |text_|.
693 // Logical selection range and visual cursor position.
694 SelectionModel selection_model_
;
696 // The cached cursor bounds; get these bounds with GetUpdatedCursorBounds.
699 // Specifies whether the cursor is enabled. If disabled, no space is reserved
700 // for the cursor when positioning text.
701 bool cursor_enabled_
;
703 // The cursor visibility and insert mode.
704 bool cursor_visible_
;
707 // The color used for the cursor.
708 SkColor cursor_color_
;
710 // The color used for drawing selected text.
711 SkColor selection_color_
;
713 // The background color used for drawing the selection when focused.
714 SkColor selection_background_focused_color_
;
716 // The focus state of the text.
719 // Composition text range.
720 Range composition_range_
;
722 // Color, baseline, and style breaks, used to modify ranges of text.
723 // BreakList positions are stored with text indices, not display indices.
724 // TODO(msw): Expand to support cursor, selection, background, etc. colors.
725 BreakList
<SkColor
> colors_
;
726 BreakList
<BaselineStyle
> baselines_
;
727 std::vector
<BreakList
<bool> > styles_
;
729 // Breaks saved without temporary composition and selection styling.
730 BreakList
<SkColor
> saved_colors_
;
731 BreakList
<bool> saved_underlines_
;
732 bool composition_and_selection_styles_applied_
;
734 // A flag to obscure actual text with asterisks for password fields.
736 // The index at which the char should be revealed in the obscured text.
737 int obscured_reveal_index_
;
739 // The maximum length of text to display, 0 forgoes a hard limit.
740 size_t truncate_length_
;
742 // The obscured and/or truncated text used to layout the text to display.
743 base::string16 layout_text_
;
745 // The elided text displayed visually. This is empty if the text
746 // does not have to be elided, or became empty as a result of eliding.
747 // TODO(oshima): When the text is elided, painting can be done only with
748 // display text info, so it should be able to clear the |layout_text_| and
749 // associated information.
750 base::string16 display_text_
;
752 // The behavior for eliding, fading, or truncating.
753 ElideBehavior elide_behavior_
;
755 // True if the text is elided given the current behavior and display area.
758 // The minimum height a line should have.
759 int min_line_height_
;
761 // Whether the text should be broken into multiple lines. Uses the width of
762 // |display_rect_| as the width cap.
765 // Whether newline characters should be replaced with newline symbols.
766 bool replace_newline_chars_with_symbols_
;
768 // Set to true to suppress subpixel rendering due to non-font reasons (eg.
769 // if the background is transparent). The default value is false.
770 bool subpixel_rendering_suppressed_
;
772 // The local display area for rendering the text.
775 // Flag to work around a Skia bug with the PDF path (http://crbug.com/133548)
776 // that results in incorrect clipping when drawing to the document margins.
777 // This field allows disabling clipping to work around the issue.
778 // TODO(asvitkine): Remove this when the underlying Skia bug is fixed.
779 bool clip_to_display_rect_
;
781 // The offset for the text to be drawn, relative to the display area.
782 // Get this point with GetUpdatedDisplayOffset (or risk using a stale value).
783 Vector2d display_offset_
;
785 // The baseline of the text. This is determined from the height of the
786 // display area and the cap height of the font list so the text is vertically
790 // The cached bounds and offset are invalidated by changes to the cursor,
791 // selection, font, and other operations that adjust the visible text bounds.
792 bool cached_bounds_and_offset_valid_
;
794 // Text shadows to be drawn.
795 ShadowValues shadows_
;
797 // A list of valid display text line break positions.
798 BreakList
<size_t> line_breaks_
;
800 // Lines computed by EnsureLayout. These should be invalidated upon
801 // OnLayoutTextAttributeChanged and OnDisplayTextAttributeChanged calls.
802 std::vector
<internal::Line
> lines_
;
804 DISALLOW_COPY_AND_ASSIGN(RenderText
);
809 #endif // UI_GFX_RENDER_TEXT_H_