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 by derived classes to iterate colors and styles.
113 class StyleIterator
{
115 StyleIterator(const BreakList
<SkColor
>& colors
,
116 const std::vector
<BreakList
<bool> >& styles
);
119 // Get the colors and styles at the current iterator position.
120 SkColor
color() const { return color_
->second
; }
121 bool style(TextStyle s
) const { return style_
[s
]->second
; }
123 // Get the intersecting range of the current iterator set.
124 Range
GetRange() const;
126 // Update the iterator to point to colors and styles applicable at |position|.
127 void UpdatePosition(size_t position
);
130 BreakList
<SkColor
> colors_
;
131 std::vector
<BreakList
<bool> > styles_
;
133 BreakList
<SkColor
>::const_iterator color_
;
134 std::vector
<BreakList
<bool>::const_iterator
> style_
;
136 DISALLOW_COPY_AND_ASSIGN(StyleIterator
);
139 // Line segments are slices of the display text to be rendered on a single line.
144 // X coordinates of this line segment in text space.
147 // The character range this segment corresponds to.
150 // The width of this line segment in text space. This could be slightly
151 // different from x_range.length().
152 // TODO(mukai): Fix Range to support float values and merge it into x_range.
155 // Index of the text run that generated this segment.
159 // A line of display text, comprised of a line segment list and some metrics.
164 // Segments that make up this line in visual order.
165 std::vector
<LineSegment
> segments
;
167 // The sum of segment widths and the maximum of segment heights.
170 // Sum of preceding lines' heights.
171 int preceding_heights
;
173 // Maximum baseline of all segments on this line.
177 // Creates an SkTypeface from a font |family| name and a |gfx::Font::FontStyle|.
179 skia::RefPtr
<SkTypeface
> CreateSkiaTypeface(const std::string
& family
,
182 // Applies the given FontRenderParams to a Skia |paint|.
183 void ApplyRenderParams(const FontRenderParams
& params
,
184 bool subpixel_rendering_suppressed
,
187 } // namespace internal
189 // RenderText represents an abstract model of styled text and its corresponding
190 // visual layout. Support is built in for a cursor, a selection, simple styling,
191 // complex scripts, and bi-directional text. Implementations provide mechanisms
192 // for rendering and translation between logical and visual data.
193 class GFX_EXPORT RenderText
{
195 virtual ~RenderText();
197 // Creates a platform-specific or cross-platform RenderText instance.
198 static RenderText
* CreateInstance();
199 static RenderText
* CreateInstanceForEditing();
201 // Creates another instance of the same concrete class.
202 virtual scoped_ptr
<RenderText
> CreateInstanceOfSameType() const = 0;
204 const base::string16
& text() const { return text_
; }
205 void SetText(const base::string16
& text
);
207 HorizontalAlignment
horizontal_alignment() const {
208 return horizontal_alignment_
;
210 void SetHorizontalAlignment(HorizontalAlignment alignment
);
212 const FontList
& font_list() const { return font_list_
; }
213 void SetFontList(const FontList
& font_list
);
215 bool cursor_enabled() const { return cursor_enabled_
; }
216 void SetCursorEnabled(bool cursor_enabled
);
218 bool cursor_visible() const { return cursor_visible_
; }
219 void set_cursor_visible(bool visible
) { cursor_visible_
= visible
; }
221 bool insert_mode() const { return insert_mode_
; }
222 void ToggleInsertMode();
224 SkColor
cursor_color() const { return cursor_color_
; }
225 void set_cursor_color(SkColor color
) { cursor_color_
= color
; }
227 SkColor
selection_color() const { return selection_color_
; }
228 void set_selection_color(SkColor color
) { selection_color_
= color
; }
230 SkColor
selection_background_focused_color() const {
231 return selection_background_focused_color_
;
233 void set_selection_background_focused_color(SkColor color
) {
234 selection_background_focused_color_
= color
;
237 bool focused() const { return focused_
; }
238 void set_focused(bool focused
) { focused_
= focused
; }
240 bool clip_to_display_rect() const { return clip_to_display_rect_
; }
241 void set_clip_to_display_rect(bool clip
) { clip_to_display_rect_
= clip
; }
243 // In an obscured (password) field, all text is drawn as asterisks or bullets.
244 bool obscured() const { return obscured_
; }
245 void SetObscured(bool obscured
);
247 // Makes a char in obscured text at |index| to be revealed. |index| should be
248 // a UTF16 text index. If there is a previous revealed index, the previous one
249 // is cleared and only the last set index will be revealed. If |index| is -1
250 // or out of range, no char will be revealed. The revealed index is also
251 // cleared when SetText or SetObscured is called.
252 void SetObscuredRevealIndex(int index
);
254 // TODO(ckocagil): Multiline text rendering is currently only supported on
255 // Windows. Support other platforms.
256 bool multiline() const { return multiline_
; }
257 void SetMultiline(bool multiline
);
259 // TODO(ckocagil): Add vertical alignment and line spacing support instead.
260 int min_line_height() const { return min_line_height_
; }
261 void SetMinLineHeight(int line_height
);
263 // Set the maximum length of the layout text, not the actual text.
264 // A |length| of 0 forgoes a hard limit, but does not guarantee proper
265 // functionality of very long strings. Applies to subsequent SetText calls.
266 // WARNING: Only use this for system limits, it lacks complex text support.
267 void set_truncate_length(size_t length
) { truncate_length_
= length
; }
269 // The layout text will be elided to fit |display_rect| using this behavior.
270 void SetElideBehavior(ElideBehavior elide_behavior
);
271 ElideBehavior
elide_behavior() const { return elide_behavior_
; }
273 const Rect
& display_rect() const { return display_rect_
; }
274 void SetDisplayRect(const Rect
& r
);
276 bool subpixel_rendering_suppressed() const {
277 return subpixel_rendering_suppressed_
;
279 void set_subpixel_rendering_suppressed(bool suppressed
) {
280 subpixel_rendering_suppressed_
= suppressed
;
283 const SelectionModel
& selection_model() const { return selection_model_
; }
285 const Range
& selection() const { return selection_model_
.selection(); }
287 size_t cursor_position() const { return selection_model_
.caret_pos(); }
288 void SetCursorPosition(size_t position
);
290 // Moves the cursor left or right. Cursor movement is visual, meaning that
291 // left and right are relative to screen, not the directionality of the text.
292 // If |select| is false, the selection start is moved to the same position.
293 void MoveCursor(BreakType break_type
,
294 VisualCursorDirection direction
,
297 // Set the selection_model_ to the value of |selection|.
298 // The selection range is clamped to text().length() if out of range.
299 // Returns true if the cursor position or selection range changed.
300 // If any index in |selection_model| is not a cursorable position (not on a
301 // grapheme boundary), it is a no-op and returns false.
302 bool MoveCursorTo(const SelectionModel
& selection_model
);
304 // Set the selection_model_ based on |range|.
305 // If the |range| start or end is greater than text length, it is modified
306 // to be the text length.
307 // If the |range| start or end is not a cursorable position (not on grapheme
308 // boundary), it is a NO-OP and returns false. Otherwise, returns true.
309 bool SelectRange(const Range
& range
);
311 // Returns true if the local point is over selected text.
312 bool IsPointInSelection(const Point
& point
);
314 // Selects no text, keeping the current cursor position and caret affinity.
315 void ClearSelection();
317 // Select the entire text range. If |reversed| is true, the range will end at
318 // the logical beginning of the text; this generally shows the leading portion
319 // of text that overflows its display area.
320 void SelectAll(bool reversed
);
322 // Selects the word at the current cursor position. If there is a non-empty
323 // selection, the selection bounds are extended to their nearest word
327 const Range
& GetCompositionRange() const;
328 void SetCompositionRange(const Range
& composition_range
);
330 // Set the text color over the entire text or a logical character range.
331 // The |range| should be valid, non-reversed, and within [0, text().length()].
332 void SetColor(SkColor value
);
333 void ApplyColor(SkColor value
, const Range
& range
);
335 // Set various text styles over the entire text or a logical character range.
336 // The respective |style| is applied if |value| is true, or removed if false.
337 // The |range| should be valid, non-reversed, and within [0, text().length()].
338 void SetStyle(TextStyle style
, bool value
);
339 void ApplyStyle(TextStyle style
, bool value
, const Range
& range
);
341 // Returns whether this style is enabled consistently across the entire
343 bool GetStyle(TextStyle style
) const;
345 // Set or get the text directionality mode and get the text direction yielded.
346 void SetDirectionalityMode(DirectionalityMode mode
);
347 DirectionalityMode
directionality_mode() const {
348 return directionality_mode_
;
350 base::i18n::TextDirection
GetDisplayTextDirection();
352 // Returns the visual movement direction corresponding to the logical end
353 // of the text, considering only the dominant direction returned by
354 // |GetDisplayTextDirection()|, not the direction of a particular run.
355 VisualCursorDirection
GetVisualDirectionOfLogicalEnd();
357 // Returns the text used to display, which may be obscured, truncated or
358 // elided. The subclass may compute elided text on the fly, or use
359 // precomputed the elided text.
360 virtual const base::string16
& GetDisplayText() = 0;
362 // Returns the size required to display the current string (which is the
363 // wrapped size in multiline mode). The returned size does not include space
364 // reserved for the cursor or the offset text shadows.
365 virtual Size
GetStringSize() = 0;
367 // This is same as GetStringSize except that fractional size is returned.
368 // The default implementation is same as GetStringSize. Certain platforms that
369 // compute the text size as floating-point values, like Mac, will override
371 // See comment in Canvas::GetStringWidthF for its usage.
372 virtual SizeF
GetStringSizeF();
374 // Returns the width of the content (which is the wrapped width in multiline
375 // mode). Reserves room for the cursor if |cursor_enabled_| is true.
376 float GetContentWidthF();
378 // Same as GetContentWidthF with the width rounded up.
379 int GetContentWidth();
381 // Returns the common baseline of the text. The return value is the vertical
382 // offset from the top of |display_rect_| to the text baseline, in pixels.
383 // The baseline is determined from the font list and display rect, and does
384 // not depend on the text.
387 void Draw(Canvas
* canvas
);
389 // Draws a cursor at |position|.
390 void DrawCursor(Canvas
* canvas
, const SelectionModel
& position
);
392 // Gets the SelectionModel from a visual point in local coordinates.
393 virtual SelectionModel
FindCursorPosition(const Point
& point
) = 0;
395 // Returns true if the position is a valid logical index into text(), and is
396 // also a valid grapheme boundary, which may be used as a cursor position.
397 virtual bool IsValidCursorIndex(size_t index
) = 0;
399 // Returns true if the position is a valid logical index into text(). Indices
400 // amid multi-character graphemes are allowed here, unlike IsValidCursorIndex.
401 virtual bool IsValidLogicalIndex(size_t index
);
403 // Get the visual bounds of a cursor at |caret|. These bounds typically
404 // represent a vertical line if |insert_mode| is true. Pass false for
405 // |insert_mode| to retrieve the bounds of the associated glyph. These bounds
406 // are in local coordinates, but may be outside the visible region if the text
407 // is longer than the textfield. Subsequent text, cursor, or bounds changes
408 // may invalidate returned values. Note that |caret| must be placed at
409 // grapheme boundary, i.e. caret.caret_pos() must be a cursorable position.
410 Rect
GetCursorBounds(const SelectionModel
& caret
, bool insert_mode
);
412 // Compute the current cursor bounds, panning the text to show the cursor in
413 // the display rect if necessary. These bounds are in local coordinates.
414 // Subsequent text, cursor, or bounds changes may invalidate returned values.
415 const Rect
& GetUpdatedCursorBounds();
417 // Given an |index| in text(), return the next or previous grapheme boundary
418 // in logical order (i.e. the nearest cursorable index). The return value is
419 // in the range 0 to text().length() inclusive (the input is clamped if it is
420 // out of that range). Always moves by at least one character index unless the
421 // supplied index is already at the boundary of the string.
422 size_t IndexOfAdjacentGrapheme(size_t index
,
423 LogicalCursorDirection direction
);
425 // Return a SelectionModel with the cursor at the current selection's start.
426 // The returned value represents a cursor/caret position without a selection.
427 SelectionModel
GetSelectionModelForSelectionStart();
429 // Sets shadows to drawn with text.
430 void set_shadows(const ShadowValues
& shadows
) { shadows_
= shadows
; }
431 const ShadowValues
& shadows() { return shadows_
; }
433 typedef std::pair
<Font
, Range
> FontSpan
;
434 // For testing purposes, returns which fonts were chosen for which parts of
435 // the text by returning a vector of Font and Range pairs, where each range
436 // specifies the character range for which the corresponding font has been
438 virtual std::vector
<FontSpan
> GetFontSpansForTesting() = 0;
440 // Gets the horizontal bounds (relative to the left of the text, not the view)
441 // of the glyph starting at |index|. If the glyph is RTL then the returned
442 // Range will have is_reversed() true. (This does not return a Rect because a
443 // Rect can't have a negative width.)
444 virtual Range
GetGlyphBounds(size_t index
) = 0;
446 const Vector2d
& GetUpdatedDisplayOffset();
447 void SetDisplayOffset(int horizontal_offset
);
452 // NOTE: The value of these accessors may be stale. Please make sure
453 // that these fields are up-to-date before accessing them.
454 const base::string16
& layout_text() const { return layout_text_
; }
455 const base::string16
& display_text() const { return display_text_
; }
456 bool text_elided() const { return text_elided_
; }
458 const BreakList
<SkColor
>& colors() const { return colors_
; }
459 const std::vector
<BreakList
<bool> >& styles() const { return styles_
; }
461 const std::vector
<internal::Line
>& lines() const { return lines_
; }
462 void set_lines(std::vector
<internal::Line
>* lines
) { lines_
.swap(*lines
); }
464 // Returns the baseline of the current text. The return value depends on
465 // the text and its layout while the return value of GetBaseline() doesn't.
466 // GetAlignmentOffset() takes into account the difference between them.
468 // We'd like a RenderText to show the text always on the same baseline
469 // regardless of the text, so the text does not jump up or down depending
470 // on the text. However, underlying layout engines return different baselines
471 // depending on the text. In general, layout engines determine the minimum
472 // bounding box for the text and return the baseline from the top of the
473 // bounding box. So the baseline changes depending on font metrics used to
476 // For example, suppose there are FontA and FontB and the baseline of FontA
477 // is smaller than the one of FontB. If the text is laid out only with FontA,
478 // then the baseline of FontA may be returned. If the text includes some
479 // characters which are laid out with FontB, then the baseline of FontB may
482 // GetBaseline() returns the fixed baseline regardless of the text.
483 // GetDisplayTextBaseline() returns the baseline determined by the underlying
484 // layout engine, and it changes depending on the text. GetAlignmentOffset()
485 // returns the difference between them.
486 virtual int GetDisplayTextBaseline() = 0;
488 void set_cached_bounds_and_offset_valid(bool valid
) {
489 cached_bounds_and_offset_valid_
= valid
;
492 // Get the selection model that visually neighbors |position| by |break_type|.
493 // The returned value represents a cursor/caret position without a selection.
494 SelectionModel
GetAdjacentSelectionModel(const SelectionModel
& current
,
495 BreakType break_type
,
496 VisualCursorDirection direction
);
498 // Get the selection model visually left/right of |selection| by one grapheme.
499 // The returned value represents a cursor/caret position without a selection.
500 virtual SelectionModel
AdjacentCharSelectionModel(
501 const SelectionModel
& selection
,
502 VisualCursorDirection direction
) = 0;
504 // Get the selection model visually left/right of |selection| by one word.
505 // The returned value represents a cursor/caret position without a selection.
506 virtual SelectionModel
AdjacentWordSelectionModel(
507 const SelectionModel
& selection
,
508 VisualCursorDirection direction
) = 0;
510 // Get the SelectionModels corresponding to visual text ends.
511 // The returned value represents a cursor/caret position without a selection.
512 SelectionModel
EdgeSelectionModel(VisualCursorDirection direction
);
514 // Sets the selection model, the argument is assumed to be valid.
515 virtual void SetSelectionModel(const SelectionModel
& model
);
517 // Get the visual bounds containing the logical substring within the |range|.
518 // If |range| is empty, the result is empty. These bounds could be visually
519 // discontinuous if the substring is split by a LTR/RTL level change.
520 // These bounds are in local coordinates, but may be outside the visible
521 // region if the text is longer than the textfield. Subsequent text, cursor,
522 // or bounds changes may invalidate returned values.
523 virtual std::vector
<Rect
> GetSubstringBounds(const Range
& range
) = 0;
525 // Convert between indices into |text_| and indices into
526 // GetDisplayText(), which differ when the text is obscured,
527 // truncated or elided. Regardless of whether or not the text is
528 // obscured, the character (code point) offsets always match.
529 virtual size_t TextIndexToDisplayIndex(size_t index
) = 0;
530 virtual size_t DisplayIndexToTextIndex(size_t index
) = 0;
532 // Notifies that layout text, or attributes that affect the layout text
533 // shape have changed. |text_changed| is true if the content of the
534 // |layout_text_| has changed, not just attributes.
535 virtual void OnLayoutTextAttributeChanged(bool text_changed
) = 0;
537 // Notifies that attributes that affect the display text shape have changed.
538 virtual void OnDisplayTextAttributeChanged() = 0;
540 // Ensure the text is laid out, lines are computed, and |lines_| is valid.
541 virtual void EnsureLayout() = 0;
544 virtual void DrawVisualText(Canvas
* canvas
) = 0;
546 // Update the display text.
547 void UpdateDisplayText(float text_width
);
549 // Returns display text positions that are suitable for breaking lines.
550 const BreakList
<size_t>& GetLineBreaks();
552 // Apply (and undo) temporary composition underlines and selection colors.
553 void ApplyCompositionAndSelectionStyles();
554 void UndoCompositionAndSelectionStyles();
556 // Returns the line offset from the origin after applying the text alignment
557 // and the display offset.
558 Vector2d
GetLineOffset(size_t line_number
);
560 // Convert points from the text space to the view space and back. Handles the
561 // display area, display offset, application LTR/RTL mode and multiline.
562 Point
ToTextPoint(const Point
& point
);
563 Point
ToViewPoint(const Point
& point
);
565 // Convert a text space x-coordinate range to rects in view space.
566 std::vector
<Rect
> TextBoundsToViewBounds(const Range
& x
);
568 // Get the alignment, resolving ALIGN_TO_HEAD with the current text direction.
569 HorizontalAlignment
GetCurrentHorizontalAlignment();
571 // Returns the line offset from the origin, accounts for text alignment only.
572 Vector2d
GetAlignmentOffset(size_t line_number
);
574 // Applies fade effects to |renderer|.
575 void ApplyFadeEffects(internal::SkiaTextRenderer
* renderer
);
577 // Applies text shadows to |renderer|.
578 void ApplyTextShadows(internal::SkiaTextRenderer
* renderer
);
580 // Get the text direction for the current directionality mode and given
582 base::i18n::TextDirection
GetTextDirection(const base::string16
& text
);
584 // A convenience function to check whether the glyph attached to the caret
585 // is within the given range.
586 static bool RangeContainsCaret(const Range
& range
,
588 LogicalCursorDirection caret_affinity
);
591 friend class RenderTextTest
;
592 FRIEND_TEST_ALL_PREFIXES(RenderTextTest
, DefaultStyle
);
593 FRIEND_TEST_ALL_PREFIXES(RenderTextTest
, SetColorAndStyle
);
594 FRIEND_TEST_ALL_PREFIXES(RenderTextTest
, ApplyColorAndStyle
);
595 FRIEND_TEST_ALL_PREFIXES(RenderTextTest
, ObscuredText
);
596 FRIEND_TEST_ALL_PREFIXES(RenderTextTest
, RevealObscuredText
);
597 FRIEND_TEST_ALL_PREFIXES(RenderTextTest
, ElidedText
);
598 FRIEND_TEST_ALL_PREFIXES(RenderTextTest
, ElidedObscuredText
);
599 FRIEND_TEST_ALL_PREFIXES(RenderTextTest
, TruncatedText
);
600 FRIEND_TEST_ALL_PREFIXES(RenderTextTest
, TruncatedObscuredText
);
601 FRIEND_TEST_ALL_PREFIXES(RenderTextTest
, GraphemePositions
);
602 FRIEND_TEST_ALL_PREFIXES(RenderTextTest
, MinLineHeight
);
603 FRIEND_TEST_ALL_PREFIXES(RenderTextTest
, EdgeSelectionModels
);
604 FRIEND_TEST_ALL_PREFIXES(RenderTextTest
, GetTextOffset
);
605 FRIEND_TEST_ALL_PREFIXES(RenderTextTest
, GetTextOffsetHorizontalDefaultInRTL
);
606 FRIEND_TEST_ALL_PREFIXES(RenderTextTest
, Multiline_MinWidth
);
607 FRIEND_TEST_ALL_PREFIXES(RenderTextTest
, Multiline_NormalWidth
);
608 FRIEND_TEST_ALL_PREFIXES(RenderTextTest
, Multiline_SufficientWidth
);
609 FRIEND_TEST_ALL_PREFIXES(RenderTextTest
, Multiline_Newline
);
610 FRIEND_TEST_ALL_PREFIXES(RenderTextTest
, NewlineWithoutMultilineFlag
);
611 FRIEND_TEST_ALL_PREFIXES(RenderTextTest
, GlyphBounds
);
612 FRIEND_TEST_ALL_PREFIXES(RenderTextTest
, HarfBuzz_GlyphBounds
);
613 FRIEND_TEST_ALL_PREFIXES(RenderTextTest
,
614 MoveCursorLeftRight_MeiryoUILigatures
);
615 FRIEND_TEST_ALL_PREFIXES(RenderTextTest
, Win_LogicalClusters
);
616 FRIEND_TEST_ALL_PREFIXES(RenderTextTest
, SameFontForParentheses
);
617 FRIEND_TEST_ALL_PREFIXES(RenderTextTest
, BreakRunsByUnicodeBlocks
);
618 FRIEND_TEST_ALL_PREFIXES(RenderTextTest
, PangoAttributes
);
619 FRIEND_TEST_ALL_PREFIXES(RenderTextTest
, StringFitsOwnWidth
);
621 // Set the cursor to |position|, with the caret trailing the previous
622 // grapheme, or if there is no previous grapheme, leading the cursor position.
623 // If |select| is false, the selection start is moved to the same position.
624 // If the |position| is not a cursorable position (not on grapheme boundary),
626 void MoveCursorTo(size_t position
, bool select
);
628 // Updates |layout_text_| and |display_text_| as needed (or marks them dirty).
629 void OnTextAttributeChanged();
631 // Elides |text| as needed to fit in the |available_width| using |behavior|.
632 // |text_width| is the pre-calculated width of the text shaped by this render
633 // text, or pass 0 if the width is unknown.
634 base::string16
Elide(const base::string16
& text
,
636 float available_width
,
637 ElideBehavior behavior
);
639 // Elides |email| as needed to fit the |available_width|.
640 base::string16
ElideEmail(const base::string16
& email
, float available_width
);
642 // Update the cached bounds and display offset to ensure that the current
643 // cursor is within the visible display area.
644 void UpdateCachedBoundsAndOffset();
646 // Draw the selection.
647 void DrawSelection(Canvas
* canvas
);
649 // Logical UTF-16 string data to be drawn.
650 base::string16 text_
;
652 // Horizontal alignment of the text with respect to |display_rect_|. The
653 // default is to align left if the application UI is LTR and right if RTL.
654 HorizontalAlignment horizontal_alignment_
;
656 // The text directionality mode, defaults to DIRECTIONALITY_FROM_TEXT.
657 DirectionalityMode directionality_mode_
;
659 // The cached text direction, potentially computed from the text or UI locale.
660 // Use GetTextDirection(), do not use this potentially invalid value directly!
661 base::i18n::TextDirection text_direction_
;
663 // A list of fonts used to render |text_|.
666 // Logical selection range and visual cursor position.
667 SelectionModel selection_model_
;
669 // The cached cursor bounds; get these bounds with GetUpdatedCursorBounds.
672 // Specifies whether the cursor is enabled. If disabled, no space is reserved
673 // for the cursor when positioning text.
674 bool cursor_enabled_
;
676 // The cursor visibility and insert mode.
677 bool cursor_visible_
;
680 // The color used for the cursor.
681 SkColor cursor_color_
;
683 // The color used for drawing selected text.
684 SkColor selection_color_
;
686 // The background color used for drawing the selection when focused.
687 SkColor selection_background_focused_color_
;
689 // The focus state of the text.
692 // Composition text range.
693 Range composition_range_
;
695 // Color and style breaks, used to color and stylize ranges of text.
696 // BreakList positions are stored with text indices, not display indices.
697 // TODO(msw): Expand to support cursor, selection, background, etc. colors.
698 BreakList
<SkColor
> colors_
;
699 std::vector
<BreakList
<bool> > styles_
;
701 // Breaks saved without temporary composition and selection styling.
702 BreakList
<SkColor
> saved_colors_
;
703 BreakList
<bool> saved_underlines_
;
704 bool composition_and_selection_styles_applied_
;
706 // A flag to obscure actual text with asterisks for password fields.
708 // The index at which the char should be revealed in the obscured text.
709 int obscured_reveal_index_
;
711 // The maximum length of text to display, 0 forgoes a hard limit.
712 size_t truncate_length_
;
714 // The obscured and/or truncated text used to layout the text to display.
715 base::string16 layout_text_
;
717 // The elided text displayed visually. This is empty if the text
718 // does not have to be elided, or became empty as a result of eliding.
719 // TODO(oshima): When the text is elided, painting can be done only with
720 // display text info, so it should be able to clear the |layout_text_| and
721 // associated information.
722 base::string16 display_text_
;
724 // The behavior for eliding, fading, or truncating.
725 ElideBehavior elide_behavior_
;
727 // True if the text is elided given the current behavior and display area.
730 // The minimum height a line should have.
731 int min_line_height_
;
733 // Whether the text should be broken into multiple lines. Uses the width of
734 // |display_rect_| as the width cap.
737 // Set to true to suppress subpixel rendering due to non-font reasons (eg.
738 // if the background is transparent). The default value is false.
739 bool subpixel_rendering_suppressed_
;
741 // The local display area for rendering the text.
744 // Flag to work around a Skia bug with the PDF path (http://crbug.com/133548)
745 // that results in incorrect clipping when drawing to the document margins.
746 // This field allows disabling clipping to work around the issue.
747 // TODO(asvitkine): Remove this when the underlying Skia bug is fixed.
748 bool clip_to_display_rect_
;
750 // The offset for the text to be drawn, relative to the display area.
751 // Get this point with GetUpdatedDisplayOffset (or risk using a stale value).
752 Vector2d display_offset_
;
754 // The baseline of the text. This is determined from the height of the
755 // display area and the cap height of the font list so the text is vertically
759 // The cached bounds and offset are invalidated by changes to the cursor,
760 // selection, font, and other operations that adjust the visible text bounds.
761 bool cached_bounds_and_offset_valid_
;
763 // Text shadows to be drawn.
764 ShadowValues shadows_
;
766 // A list of valid display text line break positions.
767 BreakList
<size_t> line_breaks_
;
769 // Lines computed by EnsureLayout. These should be invalidated upon
770 // OnLayoutTextAttributeChanged and OnDisplayTextAttributeChanged calls.
771 std::vector
<internal::Line
> lines_
;
773 DISALLOW_COPY_AND_ASSIGN(RenderText
);
778 #endif // UI_GFX_RENDER_TEXT_H_