Chromium Code Reviews| Index: ui/gfx/render_text.h |
| diff --git a/ui/gfx/render_text.h b/ui/gfx/render_text.h |
| new file mode 100755 |
| index 0000000000000000000000000000000000000000..9e1b95d03450e5c28387acaf4d556dd25469157c |
| --- /dev/null |
| +++ b/ui/gfx/render_text.h |
| @@ -0,0 +1,186 @@ |
| +// Copyright (c) 2011 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#ifndef UI_GFX_RENDER_TEXT_H_ |
| +#define UI_GFX_RENDER_TEXT_H_ |
| +#pragma once |
| + |
| +#include <string> |
| +#include <vector> |
| + |
| +#include "base/gtest_prod_util.h" |
| +#include "base/i18n/rtl.h" |
| +#include "base/string16.h" |
| +#include "third_party/skia/include/core/SkColor.h" |
| +#include "ui/base/range/range.h" |
| +#include "ui/gfx/font.h" |
| +#include "ui/gfx/rect.h" |
| +#include "ui/gfx/point.h" |
| + |
| +namespace gfx { |
| + |
| +class Canvas; |
| +class RenderTextTest; |
| + |
| +struct StyleRange { |
| + StyleRange() |
| + : font(), |
| + foreground(SK_ColorBLACK), |
| + strike(false), |
| + underline(false), |
| + range() { |
| + } |
| + |
|
oshima
2011/07/23 09:51:12
move constructor to .cc as this has non trivial ob
|
| + Font font; |
| + SkColor foreground; |
| + bool strike; |
| + bool underline; |
| + ui::Range range; |
| +}; |
| + |
| +typedef std::vector<StyleRange> StyleRanges; |
| + |
| +// TODO(msw): Implement RenderText[Win|Linux] for Uniscribe/Pango BiDi... |
| + |
| +// RenderText represents an abstract model of styled text and its corresponding |
| +// visual layout. Support is built in for a cursor, a selection, simple styling, |
| +// complex scripts, and bi-directional text. Implementations provide mechanisms |
| +// for rendering and translation between logical and visual data. |
| +class RenderText { |
| + public: |
| + virtual ~RenderText(); |
| + |
| + // Creates a platform-specific RenderText instance. |
| + static RenderText* CreateRenderText(); |
| + |
| + const string16& text() const { return text_; } |
| + virtual void SetText(const string16& text); |
| + |
| + void set_cursor_visible(bool visible) { is_cursor_visible_ = visible; } |
| + |
| + bool get_insert_mode() const { return insert_mode_; } |
| + void toggle_insert_mode() { insert_mode_ = !insert_mode_; } |
| + |
| + void set_focused(bool focused) { is_focused_ = focused; } |
| + |
| + const StyleRange& get_default_style() { return default_style_; } |
| + void set_default_style(StyleRange style) { default_style_ = style; } |
| + |
| + const gfx::Rect& get_display_rect() const { return display_rect_; } |
| + void set_display_rect(const gfx::Rect& r) { display_rect_ = r; } |
| + |
| + size_t GetCursor() const; |
| + void SetCursor(const size_t position); |
| + |
| + // Moves the cursor left or right. Cursor movement is visual, meaning that |
| + // left and right are relative to screen, not the directionality of the text. |
| + // If |select| is false, the selection range is emptied at the new position. |
| + // If |move_by_word| is true, the cursor moves to the nearest word boundary. |
| + void MoveCursorLeft(bool select, bool move_by_word); |
| + void MoveCursorRight(bool select, bool move_by_word); |
| + |
| + // Moves the cursor to left or right end of the text as shown on screen. |
| + // If |select| is false, the selection range is emptied at the new position. |
| + void MoveCursorToLeftEnd(bool select); |
| + void MoveCursorToRightEnd(bool select); |
| + |
| + // Moves the cursor to the specified logical |position|. |
| + // If |select| is false, the selection range is emptied at the new position. |
| + // Returns true if the cursor position or selection range changed. |
| + bool MoveCursorTo(size_t position, bool select); |
| + |
| + const ui::Range& GetSelection() const; |
| + void SetSelection(const ui::Range& range); |
| + |
| + // Selects no text, all text, or the word at the current cursor position. |
| + void ClearSelection(); |
| + void SelectAll(); |
| + void SelectWord(); |
| + |
| + const ui::Range& GetCompositionRange() const; |
| + void SetCompositionRange(const ui::Range& composition_range); |
| + |
| + // Apply |style_range| to the internal style model. |
| + void ApplyStyleRange(StyleRange style_range); |
| + // Apply |default_style_| over the entire text range. |
| + void ApplyDefaultStyle(); |
| + |
| + base::i18n::TextDirection GetTextDirection() const; |
| + |
| + // Get the width of the entire string. |
| + int GetStringWidth() const; |
| + |
| + virtual void Draw(gfx::Canvas* canvas) const; |
| + |
| + // Get the logical cursor position from a visual point in local coordinates. |
| + virtual size_t FindCursorPosition(const gfx::Point& point) const; |
| + |
| + // Get the visual bounds containing the logical substring within |range|. |
| + // These bounds could be visually discontiguous if the logical selection range |
| + // is split by a single LTR/RTL level change. |
| + virtual std::vector<gfx::Rect> GetSubstringBounds( |
| + const ui::Range& range) const; |
| + |
| + // Get the visual bounds describing the cursor at |position|. These bounds |
| + // typically represent a vertical line, but if |insert_mode| is true they |
| + // contain the bounds of the associated glyph. |
| + virtual gfx::Rect GetCursorBounds(size_t position, bool insert_mode) const; |
| + |
| + protected: |
| + RenderText(); |
| + |
| + // Get the cursor position that visually neighbors |position|. |
| + // If |move_by_word| is true, return the neighboring word delimiter position. |
| + virtual size_t GetLeftCursorPosition(size_t position, |
| + bool move_by_word) const; |
| + virtual size_t GetRightCursorPosition(size_t position, |
| + bool move_by_word) const; |
| + |
| + private: |
| + friend class RenderTextTest; |
| + |
| + FRIEND_TEST_ALL_PREFIXES(RenderTextTest, DefaultStyle); |
| + FRIEND_TEST_ALL_PREFIXES(RenderTextTest, CustomDefaultStyle); |
| + FRIEND_TEST_ALL_PREFIXES(RenderTextTest, ApplyStyleRange); |
| + FRIEND_TEST_ALL_PREFIXES(RenderTextTest, StyleRangesAdjust); |
| + |
| + const StyleRanges& get_style_ranges() const { return style_ranges_; } |
| + |
| + // Clear out |style_ranges_|. |
| + void ClearStyleRanges(); |
| + |
| + bool IsPositionAtWordSelectionBoundary(size_t pos); |
| + |
| + // Logical UTF-16 string data to be drawn. |
| + string16 text_; |
| + |
| + // Logical selection range; the range end is also the logical cursor position. |
| + ui::Range selection_range_; |
|
xji
2011/07/20 01:33:33
I think we will need the leading or trailing infor
msw
2011/07/20 09:22:46
You're right, this model doesn't currently support
xji
2011/07/20 18:39:51
in bidi text, the mapping between logical and visu
xji
2011/07/20 19:59:36
I'am resending/republishing my comments since it w
msw
2011/07/21 00:19:55
Yes, the non insert-mode cursor is essentially a m
|
| + |
| + // The cursor visibility and insert mode. |
| + bool is_cursor_visible_; |
| + bool insert_mode_; |
| + |
| + // The focus state of the text. |
| + bool is_focused_; |
| + |
| + // Composition text range. |
| + ui::Range composition_range_; |
| + |
| + // List of style ranges. Elements in the list never overlap each other. |
| + StyleRanges style_ranges_; |
| + // The default text style. |
| + StyleRange default_style_; |
| + |
| + // The local display area for rendering the text. |
| + gfx::Rect display_rect_; |
| + // The offset for the text to be drawn, relative to the display area. |
| + gfx::Point display_offset_; |
| + |
| + DISALLOW_COPY_AND_ASSIGN(RenderText); |
| +}; |
| + |
| +} // namespace gfx |
| + |
| +#endif // UI_GFX_RENDER_TEXT_H_ |