Split script runs at 'COMMON' blocks except for "white space" character. RTHB merges...
[chromium-blink-merge.git] / ui / gfx / render_text.h
blob405dcd66b300723acd04f5a60e542a0fb3057a94
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/selection_model.h"
30 #include "ui/gfx/shadow_value.h"
31 #include "ui/gfx/text_constants.h"
33 class SkCanvas;
34 class SkDrawLooper;
35 struct SkPoint;
36 class SkShader;
37 class SkTypeface;
39 namespace gfx {
41 class Canvas;
42 class Font;
43 class RenderTextTest;
45 namespace internal {
47 // Internal helper class used by derived classes to draw text through Skia.
48 class GFX_EXPORT SkiaTextRenderer {
49 public:
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,
67 const uint16* glyphs,
68 size_t glyph_count);
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;
79 private:
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 {
83 public:
84 DiagonalStrike(Canvas* canvas, Point start, const SkPaint& paint);
85 ~DiagonalStrike();
87 void AddPiece(int length, SkColor color);
88 void Draw();
90 private:
91 typedef std::pair<int, SkColor> Piece;
93 Canvas* canvas_;
94 const Point start_;
95 SkPaint paint_;
96 int total_length_;
97 std::vector<Piece> pieces_;
99 DISALLOW_COPY_AND_ASSIGN(DiagonalStrike);
102 Canvas* canvas_;
103 SkCanvas* canvas_skia_;
104 SkPaint paint_;
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 {
114 public:
115 StyleIterator(const BreakList<SkColor>& colors,
116 const BreakList<BaselineStyle>& baselines,
117 const std::vector<BreakList<bool>>& styles);
118 ~StyleIterator();
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);
131 private:
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.
144 struct LineSegment {
145 LineSegment();
146 ~LineSegment();
148 // X coordinates of this line segment in text space.
149 Range x_range;
151 // The character range this segment corresponds to.
152 Range char_range;
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.
157 float width;
159 // Index of the text run that generated this segment.
160 size_t run;
163 // A line of display text, comprised of a line segment list and some metrics.
164 struct Line {
165 Line();
166 ~Line();
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.
172 SizeF size;
174 // Sum of preceding lines' heights.
175 int preceding_heights;
177 // Maximum baseline of all segments on this line.
178 int baseline;
181 // Creates an SkTypeface from a font |family| name and a |gfx::Font::FontStyle|.
182 // May return NULL.
183 skia::RefPtr<SkTypeface> CreateSkiaTypeface(const std::string& family,
184 int style);
186 // Applies the given FontRenderParams to a Skia |paint|.
187 void ApplyRenderParams(const FontRenderParams& params,
188 bool subpixel_rendering_suppressed,
189 SkPaint* paint);
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 {
198 public:
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 not supported on Mac.
260 bool multiline() const { return multiline_; }
261 void SetMultiline(bool multiline);
263 // TODO(mukai): ELIDE_LONG_WORDS is not supported.
264 WordWrapBehavior word_wrap_behavior() const { return word_wrap_behavior_; }
265 void SetWordWrapBehavior(WordWrapBehavior behavior);
267 // Set whether newline characters should be replaced with newline symbols.
268 void SetReplaceNewlineCharsWithSymbols(bool replace);
270 // Returns true if this instance supports multiline rendering.
271 virtual bool MultilineSupported() const = 0;
273 // TODO(ckocagil): Add vertical alignment and line spacing support instead.
274 int min_line_height() const { return min_line_height_; }
275 void SetMinLineHeight(int line_height);
277 // Set the maximum length of the layout text, not the actual text.
278 // A |length| of 0 forgoes a hard limit, but does not guarantee proper
279 // functionality of very long strings. Applies to subsequent SetText calls.
280 // WARNING: Only use this for system limits, it lacks complex text support.
281 void set_truncate_length(size_t length) { truncate_length_ = length; }
283 // The display text will be elided to fit |display_rect| using this behavior.
284 void SetElideBehavior(ElideBehavior elide_behavior);
285 ElideBehavior elide_behavior() const { return elide_behavior_; }
287 const Rect& display_rect() const { return display_rect_; }
288 void SetDisplayRect(const Rect& r);
290 bool subpixel_rendering_suppressed() const {
291 return subpixel_rendering_suppressed_;
293 void set_subpixel_rendering_suppressed(bool suppressed) {
294 subpixel_rendering_suppressed_ = suppressed;
297 const SelectionModel& selection_model() const { return selection_model_; }
299 const Range& selection() const { return selection_model_.selection(); }
301 size_t cursor_position() const { return selection_model_.caret_pos(); }
302 void SetCursorPosition(size_t position);
304 // Moves the cursor left or right. Cursor movement is visual, meaning that
305 // left and right are relative to screen, not the directionality of the text.
306 // If |select| is false, the selection start is moved to the same position.
307 void MoveCursor(BreakType break_type,
308 VisualCursorDirection direction,
309 bool select);
311 // Set the selection_model_ to the value of |selection|.
312 // The selection range is clamped to text().length() if out of range.
313 // Returns true if the cursor position or selection range changed.
314 // If any index in |selection_model| is not a cursorable position (not on a
315 // grapheme boundary), it is a no-op and returns false.
316 bool MoveCursorTo(const SelectionModel& selection_model);
318 // Set the selection_model_ based on |range|.
319 // If the |range| start or end is greater than text length, it is modified
320 // to be the text length.
321 // If the |range| start or end is not a cursorable position (not on grapheme
322 // boundary), it is a NO-OP and returns false. Otherwise, returns true.
323 bool SelectRange(const Range& range);
325 // Returns true if the local point is over selected text.
326 bool IsPointInSelection(const Point& point);
328 // Selects no text, keeping the current cursor position and caret affinity.
329 void ClearSelection();
331 // Select the entire text range. If |reversed| is true, the range will end at
332 // the logical beginning of the text; this generally shows the leading portion
333 // of text that overflows its display area.
334 void SelectAll(bool reversed);
336 // Selects the word at the current cursor position. If there is a non-empty
337 // selection, the selection bounds are extended to their nearest word
338 // boundaries.
339 void SelectWord();
341 void SetCompositionRange(const Range& composition_range);
343 // Set the text color over the entire text or a logical character range.
344 // The |range| should be valid, non-reversed, and within [0, text().length()].
345 void SetColor(SkColor value);
346 void ApplyColor(SkColor value, const Range& range);
348 // Set the baseline style over the entire text or a logical character range.
349 // The |range| should be valid, non-reversed, and within [0, text().length()].
350 void SetBaselineStyle(BaselineStyle value);
351 void ApplyBaselineStyle(BaselineStyle value, const Range& range);
353 // Set various text styles over the entire text or a logical character range.
354 // The respective |style| is applied if |value| is true, or removed if false.
355 // The |range| should be valid, non-reversed, and within [0, text().length()].
356 void SetStyle(TextStyle style, bool value);
357 void ApplyStyle(TextStyle style, bool value, const Range& range);
359 // Returns whether this style is enabled consistently across the entire
360 // RenderText.
361 bool GetStyle(TextStyle style) const;
363 // Set or get the text directionality mode and get the text direction yielded.
364 void SetDirectionalityMode(DirectionalityMode mode);
365 DirectionalityMode directionality_mode() const {
366 return directionality_mode_;
368 base::i18n::TextDirection GetDisplayTextDirection();
370 // Returns the visual movement direction corresponding to the logical end
371 // of the text, considering only the dominant direction returned by
372 // |GetDisplayTextDirection()|, not the direction of a particular run.
373 VisualCursorDirection GetVisualDirectionOfLogicalEnd();
375 // Returns the text used to display, which may be obscured, truncated or
376 // elided. The subclass may compute elided text on the fly, or use
377 // precomputed the elided text.
378 virtual const base::string16& GetDisplayText() = 0;
380 // Returns the size required to display the current string (which is the
381 // wrapped size in multiline mode). The returned size does not include space
382 // reserved for the cursor or the offset text shadows.
383 virtual Size GetStringSize() = 0;
385 // This is same as GetStringSize except that fractional size is returned.
386 // The default implementation is same as GetStringSize. Certain platforms that
387 // compute the text size as floating-point values, like Mac, will override
388 // this method.
389 // See comment in Canvas::GetStringWidthF for its usage.
390 virtual SizeF GetStringSizeF();
392 // Returns the width of the content (which is the wrapped width in multiline
393 // mode). Reserves room for the cursor if |cursor_enabled_| is true.
394 float GetContentWidthF();
396 // Same as GetContentWidthF with the width rounded up.
397 int GetContentWidth();
399 // Returns the common baseline of the text. The return value is the vertical
400 // offset from the top of |display_rect_| to the text baseline, in pixels.
401 // The baseline is determined from the font list and display rect, and does
402 // not depend on the text.
403 int GetBaseline();
405 void Draw(Canvas* canvas);
407 // Draws a cursor at |position|.
408 void DrawCursor(Canvas* canvas, const SelectionModel& position);
410 // Gets the SelectionModel from a visual point in local coordinates.
411 virtual SelectionModel FindCursorPosition(const Point& point) = 0;
413 // Returns true if the position is a valid logical index into text(), and is
414 // also a valid grapheme boundary, which may be used as a cursor position.
415 virtual bool IsValidCursorIndex(size_t index) = 0;
417 // Returns true if the position is a valid logical index into text(). Indices
418 // amid multi-character graphemes are allowed here, unlike IsValidCursorIndex.
419 virtual bool IsValidLogicalIndex(size_t index) const;
421 // Get the visual bounds of a cursor at |caret|. These bounds typically
422 // represent a vertical line if |insert_mode| is true. Pass false for
423 // |insert_mode| to retrieve the bounds of the associated glyph. These bounds
424 // are in local coordinates, but may be outside the visible region if the text
425 // is longer than the textfield. Subsequent text, cursor, or bounds changes
426 // may invalidate returned values. Note that |caret| must be placed at
427 // grapheme boundary, i.e. caret.caret_pos() must be a cursorable position.
428 Rect GetCursorBounds(const SelectionModel& caret, bool insert_mode);
430 // Compute the current cursor bounds, panning the text to show the cursor in
431 // the display rect if necessary. These bounds are in local coordinates.
432 // Subsequent text, cursor, or bounds changes may invalidate returned values.
433 const Rect& GetUpdatedCursorBounds();
435 // Given an |index| in text(), return the next or previous grapheme boundary
436 // in logical order (i.e. the nearest cursorable index). The return value is
437 // in the range 0 to text().length() inclusive (the input is clamped if it is
438 // out of that range). Always moves by at least one character index unless the
439 // supplied index is already at the boundary of the string.
440 size_t IndexOfAdjacentGrapheme(size_t index,
441 LogicalCursorDirection direction);
443 // Return a SelectionModel with the cursor at the current selection's start.
444 // The returned value represents a cursor/caret position without a selection.
445 SelectionModel GetSelectionModelForSelectionStart() const;
447 // Sets shadows to drawn with text.
448 void set_shadows(const ShadowValues& shadows) { shadows_ = shadows; }
449 const ShadowValues& shadows() const { return shadows_; }
451 typedef std::pair<Font, Range> FontSpan;
452 // For testing purposes, returns which fonts were chosen for which parts of
453 // the text by returning a vector of Font and Range pairs, where each range
454 // specifies the character range for which the corresponding font has been
455 // chosen.
456 virtual std::vector<FontSpan> GetFontSpansForTesting() = 0;
458 // Gets the horizontal bounds (relative to the left of the text, not the view)
459 // of the glyph starting at |index|. If the glyph is RTL then the returned
460 // Range will have is_reversed() true. (This does not return a Rect because a
461 // Rect can't have a negative width.)
462 virtual Range GetGlyphBounds(size_t index) = 0;
464 const Vector2d& GetUpdatedDisplayOffset();
465 void SetDisplayOffset(int horizontal_offset);
467 // Returns the line offset from the origin after applying the text alignment
468 // and the display offset.
469 Vector2d GetLineOffset(size_t line_number);
471 protected:
472 RenderText();
474 // NOTE: The value of these accessors may be stale. Please make sure
475 // that these fields are up-to-date before accessing them.
476 const base::string16& layout_text() const { return layout_text_; }
477 const base::string16& display_text() const { return display_text_; }
478 bool text_elided() const { return text_elided_; }
480 const BreakList<SkColor>& colors() const { return colors_; }
481 const BreakList<BaselineStyle>& baselines() const { return baselines_; }
482 const std::vector<BreakList<bool> >& styles() const { return styles_; }
484 const std::vector<internal::Line>& lines() const { return lines_; }
485 void set_lines(std::vector<internal::Line>* lines) { lines_.swap(*lines); }
487 // Returns the baseline of the current text. The return value depends on
488 // the text and its layout while the return value of GetBaseline() doesn't.
489 // GetAlignmentOffset() takes into account the difference between them.
491 // We'd like a RenderText to show the text always on the same baseline
492 // regardless of the text, so the text does not jump up or down depending
493 // on the text. However, underlying layout engines return different baselines
494 // depending on the text. In general, layout engines determine the minimum
495 // bounding box for the text and return the baseline from the top of the
496 // bounding box. So the baseline changes depending on font metrics used to
497 // layout the text.
499 // For example, suppose there are FontA and FontB and the baseline of FontA
500 // is smaller than the one of FontB. If the text is laid out only with FontA,
501 // then the baseline of FontA may be returned. If the text includes some
502 // characters which are laid out with FontB, then the baseline of FontB may
503 // be returned.
505 // GetBaseline() returns the fixed baseline regardless of the text.
506 // GetDisplayTextBaseline() returns the baseline determined by the underlying
507 // layout engine, and it changes depending on the text. GetAlignmentOffset()
508 // returns the difference between them.
509 virtual int GetDisplayTextBaseline() = 0;
511 void set_cached_bounds_and_offset_valid(bool valid) {
512 cached_bounds_and_offset_valid_ = valid;
515 // Get the selection model that visually neighbors |position| by |break_type|.
516 // The returned value represents a cursor/caret position without a selection.
517 SelectionModel GetAdjacentSelectionModel(const SelectionModel& current,
518 BreakType break_type,
519 VisualCursorDirection direction);
521 // Get the selection model visually left/right of |selection| by one grapheme.
522 // The returned value represents a cursor/caret position without a selection.
523 virtual SelectionModel AdjacentCharSelectionModel(
524 const SelectionModel& selection,
525 VisualCursorDirection direction) = 0;
527 // Get the selection model visually left/right of |selection| by one word.
528 // The returned value represents a cursor/caret position without a selection.
529 virtual SelectionModel AdjacentWordSelectionModel(
530 const SelectionModel& selection,
531 VisualCursorDirection direction) = 0;
533 // Get the SelectionModels corresponding to visual text ends.
534 // The returned value represents a cursor/caret position without a selection.
535 SelectionModel EdgeSelectionModel(VisualCursorDirection direction);
537 // Sets the selection model, the argument is assumed to be valid.
538 virtual void SetSelectionModel(const SelectionModel& model);
540 // Get the visual bounds containing the logical substring within the |range|.
541 // If |range| is empty, the result is empty. These bounds could be visually
542 // discontinuous if the substring is split by a LTR/RTL level change.
543 // These bounds are in local coordinates, but may be outside the visible
544 // region if the text is longer than the textfield. Subsequent text, cursor,
545 // or bounds changes may invalidate returned values.
546 virtual std::vector<Rect> GetSubstringBounds(const Range& range) = 0;
548 // Convert between indices into |text_| and indices into
549 // GetDisplayText(), which differ when the text is obscured,
550 // truncated or elided. Regardless of whether or not the text is
551 // obscured, the character (code point) offsets always match.
552 virtual size_t TextIndexToDisplayIndex(size_t index) = 0;
553 virtual size_t DisplayIndexToTextIndex(size_t index) = 0;
555 // Notifies that layout text, or attributes that affect the layout text
556 // shape have changed. |text_changed| is true if the content of the
557 // |layout_text_| has changed, not just attributes.
558 virtual void OnLayoutTextAttributeChanged(bool text_changed) = 0;
560 // Notifies that attributes that affect the display text shape have changed.
561 virtual void OnDisplayTextAttributeChanged() = 0;
563 // Ensure the text is laid out, lines are computed, and |lines_| is valid.
564 virtual void EnsureLayout() = 0;
566 // Draw the text.
567 virtual void DrawVisualText(Canvas* canvas) = 0;
569 // Update the display text.
570 void UpdateDisplayText(float text_width);
572 // Returns display text positions that are suitable for breaking lines.
573 const BreakList<size_t>& GetLineBreaks();
575 // Apply (and undo) temporary composition underlines and selection colors.
576 void ApplyCompositionAndSelectionStyles();
577 void UndoCompositionAndSelectionStyles();
579 // Convert points from the text space to the view space and back. Handles the
580 // display area, display offset, application LTR/RTL mode and multiline.
581 Point ToTextPoint(const Point& point);
582 Point ToViewPoint(const Point& point);
584 // Convert a text space x-coordinate range to rects in view space.
585 std::vector<Rect> TextBoundsToViewBounds(const Range& x);
587 // Get the alignment, resolving ALIGN_TO_HEAD with the current text direction.
588 HorizontalAlignment GetCurrentHorizontalAlignment();
590 // Returns the line offset from the origin, accounts for text alignment only.
591 Vector2d GetAlignmentOffset(size_t line_number);
593 // Applies fade effects to |renderer|.
594 void ApplyFadeEffects(internal::SkiaTextRenderer* renderer);
596 // Applies text shadows to |renderer|.
597 void ApplyTextShadows(internal::SkiaTextRenderer* renderer);
599 // Get the text direction for the current directionality mode and given
600 // |text|.
601 base::i18n::TextDirection GetTextDirection(const base::string16& text);
603 // Convert an index in |text_| to the index in |given_text|. The
604 // |given_text| should be either |display_text_| or |layout_text_|
605 // depending on the elide state.
606 size_t TextIndexToGivenTextIndex(const base::string16& given_text,
607 size_t index) const;
609 // Adjust ranged styles to accommodate a new text length.
610 void UpdateStyleLengths();
612 // A convenience function to check whether the glyph attached to the caret
613 // is within the given range.
614 static bool RangeContainsCaret(const Range& range,
615 size_t caret_pos,
616 LogicalCursorDirection caret_affinity);
618 private:
619 friend class RenderTextTest;
620 FRIEND_TEST_ALL_PREFIXES(RenderTextTest, DefaultStyles);
621 FRIEND_TEST_ALL_PREFIXES(RenderTextTest, SetStyles);
622 FRIEND_TEST_ALL_PREFIXES(RenderTextTest, ApplyStyles);
623 FRIEND_TEST_ALL_PREFIXES(RenderTextTest, AppendTextKeepsStyles);
624 FRIEND_TEST_ALL_PREFIXES(RenderTextTest, ObscuredText);
625 FRIEND_TEST_ALL_PREFIXES(RenderTextTest, RevealObscuredText);
626 FRIEND_TEST_ALL_PREFIXES(RenderTextTest, ElidedText);
627 FRIEND_TEST_ALL_PREFIXES(RenderTextTest, ElidedObscuredText);
628 FRIEND_TEST_ALL_PREFIXES(RenderTextTest, TruncatedText);
629 FRIEND_TEST_ALL_PREFIXES(RenderTextTest, TruncatedObscuredText);
630 FRIEND_TEST_ALL_PREFIXES(RenderTextTest, GraphemePositions);
631 FRIEND_TEST_ALL_PREFIXES(RenderTextTest, MinLineHeight);
632 FRIEND_TEST_ALL_PREFIXES(RenderTextTest, EdgeSelectionModels);
633 FRIEND_TEST_ALL_PREFIXES(RenderTextTest, GetTextOffset);
634 FRIEND_TEST_ALL_PREFIXES(RenderTextTest, GetTextOffsetHorizontalDefaultInRTL);
635 FRIEND_TEST_ALL_PREFIXES(RenderTextTest, Multiline_MinWidth);
636 FRIEND_TEST_ALL_PREFIXES(RenderTextTest, Multiline_NormalWidth);
637 FRIEND_TEST_ALL_PREFIXES(RenderTextTest, Multiline_SufficientWidth);
638 FRIEND_TEST_ALL_PREFIXES(RenderTextTest, Multiline_Newline);
639 FRIEND_TEST_ALL_PREFIXES(RenderTextTest, Multiline_WordWrapBehavior);
640 FRIEND_TEST_ALL_PREFIXES(RenderTextTest, Multiline_LineBreakerBehavior);
641 FRIEND_TEST_ALL_PREFIXES(RenderTextTest,
642 Multiline_SurrogatePairsOrCombiningChars);
643 FRIEND_TEST_ALL_PREFIXES(RenderTextTest, Multiline_ZeroWidthChars);
644 FRIEND_TEST_ALL_PREFIXES(RenderTextTest, NewlineWithoutMultilineFlag);
645 FRIEND_TEST_ALL_PREFIXES(RenderTextTest, GlyphBounds);
646 FRIEND_TEST_ALL_PREFIXES(RenderTextTest, HarfBuzz_GlyphBounds);
647 FRIEND_TEST_ALL_PREFIXES(RenderTextTest,
648 MoveCursorLeftRight_MeiryoUILigatures);
649 FRIEND_TEST_ALL_PREFIXES(RenderTextTest, Win_LogicalClusters);
650 FRIEND_TEST_ALL_PREFIXES(RenderTextTest, SameFontForParentheses);
651 FRIEND_TEST_ALL_PREFIXES(RenderTextTest, BreakRunsByUnicodeBlocks);
652 FRIEND_TEST_ALL_PREFIXES(RenderTextTest, PangoAttributes);
653 FRIEND_TEST_ALL_PREFIXES(RenderTextTest, StringFitsOwnWidth);
655 // Set the cursor to |position|, with the caret trailing the previous
656 // grapheme, or if there is no previous grapheme, leading the cursor position.
657 // If |select| is false, the selection start is moved to the same position.
658 // If the |position| is not a cursorable position (not on grapheme boundary),
659 // it is a NO-OP.
660 void MoveCursorTo(size_t position, bool select);
662 // Updates |layout_text_| and |display_text_| as needed (or marks them dirty).
663 void OnTextAttributeChanged();
665 // Elides |text| as needed to fit in the |available_width| using |behavior|.
666 // |text_width| is the pre-calculated width of the text shaped by this render
667 // text, or pass 0 if the width is unknown.
668 base::string16 Elide(const base::string16& text,
669 float text_width,
670 float available_width,
671 ElideBehavior behavior);
673 // Elides |email| as needed to fit the |available_width|.
674 base::string16 ElideEmail(const base::string16& email, float available_width);
676 // Update the cached bounds and display offset to ensure that the current
677 // cursor is within the visible display area.
678 void UpdateCachedBoundsAndOffset();
680 // Draw the selection.
681 void DrawSelection(Canvas* canvas);
683 // Logical UTF-16 string data to be drawn.
684 base::string16 text_;
686 // Horizontal alignment of the text with respect to |display_rect_|. The
687 // default is to align left if the application UI is LTR and right if RTL.
688 HorizontalAlignment horizontal_alignment_;
690 // The text directionality mode, defaults to DIRECTIONALITY_FROM_TEXT.
691 DirectionalityMode directionality_mode_;
693 // The cached text direction, potentially computed from the text or UI locale.
694 // Use GetTextDirection(), do not use this potentially invalid value directly!
695 base::i18n::TextDirection text_direction_;
697 // A list of fonts used to render |text_|.
698 FontList font_list_;
700 // Logical selection range and visual cursor position.
701 SelectionModel selection_model_;
703 // The cached cursor bounds; get these bounds with GetUpdatedCursorBounds.
704 Rect cursor_bounds_;
706 // Specifies whether the cursor is enabled. If disabled, no space is reserved
707 // for the cursor when positioning text.
708 bool cursor_enabled_;
710 // The cursor visibility and insert mode.
711 bool cursor_visible_;
712 bool insert_mode_;
714 // The color used for the cursor.
715 SkColor cursor_color_;
717 // The color used for drawing selected text.
718 SkColor selection_color_;
720 // The background color used for drawing the selection when focused.
721 SkColor selection_background_focused_color_;
723 // The focus state of the text.
724 bool focused_;
726 // Composition text range.
727 Range composition_range_;
729 // Color, baseline, and style breaks, used to modify ranges of text.
730 // BreakList positions are stored with text indices, not display indices.
731 // TODO(msw): Expand to support cursor, selection, background, etc. colors.
732 BreakList<SkColor> colors_;
733 BreakList<BaselineStyle> baselines_;
734 std::vector<BreakList<bool> > styles_;
736 // Breaks saved without temporary composition and selection styling.
737 BreakList<SkColor> saved_colors_;
738 BreakList<bool> saved_underlines_;
739 bool composition_and_selection_styles_applied_;
741 // A flag to obscure actual text with asterisks for password fields.
742 bool obscured_;
743 // The index at which the char should be revealed in the obscured text.
744 int obscured_reveal_index_;
746 // The maximum length of text to display, 0 forgoes a hard limit.
747 size_t truncate_length_;
749 // The obscured and/or truncated text used to layout the text to display.
750 base::string16 layout_text_;
752 // The elided text displayed visually. This is empty if the text
753 // does not have to be elided, or became empty as a result of eliding.
754 // TODO(oshima): When the text is elided, painting can be done only with
755 // display text info, so it should be able to clear the |layout_text_| and
756 // associated information.
757 base::string16 display_text_;
759 // The behavior for eliding, fading, or truncating.
760 ElideBehavior elide_behavior_;
762 // True if the text is elided given the current behavior and display area.
763 bool text_elided_;
765 // The minimum height a line should have.
766 int min_line_height_;
768 // Whether the text should be broken into multiple lines. Uses the width of
769 // |display_rect_| as the width cap.
770 bool multiline_;
772 // The wrap behavior when the text is broken into lines. Do nothing unless
773 // |multiline_| is set. The default value is IGNORE_LONG_WORDS.
774 WordWrapBehavior word_wrap_behavior_;
776 // Whether newline characters should be replaced with newline symbols.
777 bool replace_newline_chars_with_symbols_;
779 // Set to true to suppress subpixel rendering due to non-font reasons (eg.
780 // if the background is transparent). The default value is false.
781 bool subpixel_rendering_suppressed_;
783 // The local display area for rendering the text.
784 Rect display_rect_;
786 // Flag to work around a Skia bug with the PDF path (http://crbug.com/133548)
787 // that results in incorrect clipping when drawing to the document margins.
788 // This field allows disabling clipping to work around the issue.
789 // TODO(asvitkine): Remove this when the underlying Skia bug is fixed.
790 bool clip_to_display_rect_;
792 // The offset for the text to be drawn, relative to the display area.
793 // Get this point with GetUpdatedDisplayOffset (or risk using a stale value).
794 Vector2d display_offset_;
796 // The baseline of the text. This is determined from the height of the
797 // display area and the cap height of the font list so the text is vertically
798 // centered.
799 int baseline_;
801 // The cached bounds and offset are invalidated by changes to the cursor,
802 // selection, font, and other operations that adjust the visible text bounds.
803 bool cached_bounds_and_offset_valid_;
805 // Text shadows to be drawn.
806 ShadowValues shadows_;
808 // A list of valid display text line break positions.
809 BreakList<size_t> line_breaks_;
811 // Lines computed by EnsureLayout. These should be invalidated upon
812 // OnLayoutTextAttributeChanged and OnDisplayTextAttributeChanged calls.
813 std::vector<internal::Line> lines_;
815 DISALLOW_COPY_AND_ASSIGN(RenderText);
818 } // namespace gfx
820 #endif // UI_GFX_RENDER_TEXT_H_