diff options
author | msw@chromium.org <msw@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-07-25 08:42:52 +0000 |
---|---|---|
committer | msw@chromium.org <msw@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-07-25 08:42:52 +0000 |
commit | ff44d71a49c7ddc98787ed0a49ab827176164bbe (patch) | |
tree | 20a679864ce8626bb2fd2c67f0a7add7f09db73f /ui/gfx | |
parent | b186a239f3298710e717d3adc234aefbaad04dfc (diff) | |
download | chromium_src-ff44d71a49c7ddc98787ed0a49ab827176164bbe.zip chromium_src-ff44d71a49c7ddc98787ed0a49ab827176164bbe.tar.gz chromium_src-ff44d71a49c7ddc98787ed0a49ab827176164bbe.tar.bz2 |
Add gfx::RenderText and support code.
RenderText is NativeTextFieldViews' interface to platform-specific text rendering engines.
This change doesn't hook in any new Pango or Uniscribe functionality, it will just setup the necessary API.
Committed: http://src.chromium.org/viewvc/chrome?view=rev&revision=93840
Review URL: http://codereview.chromium.org/7265011
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@93855 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'ui/gfx')
-rw-r--r-- | ui/gfx/render_text.cc | 506 | ||||
-rw-r--r-- | ui/gfx/render_text.h | 224 | ||||
-rw-r--r-- | ui/gfx/render_text_linux.cc | 20 | ||||
-rw-r--r-- | ui/gfx/render_text_linux.h | 25 | ||||
-rw-r--r-- | ui/gfx/render_text_unittest.cc | 180 | ||||
-rw-r--r-- | ui/gfx/render_text_win.cc | 20 | ||||
-rw-r--r-- | ui/gfx/render_text_win.h | 25 |
7 files changed, 1000 insertions, 0 deletions
diff --git a/ui/gfx/render_text.cc b/ui/gfx/render_text.cc new file mode 100644 index 0000000..c656792 --- /dev/null +++ b/ui/gfx/render_text.cc @@ -0,0 +1,506 @@ +// 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. + +#include "ui/gfx/render_text.h" + +#include <algorithm> + +#include "base/i18n/break_iterator.h" +#include "base/logging.h" +#include "base/stl_util.h" +#include "ui/gfx/canvas.h" +#include "ui/gfx/canvas_skia.h" + +namespace { + +#ifndef NDEBUG +// Check StyleRanges invariant conditions: sorted and non-overlapping ranges. +void CheckStyleRanges(const gfx::StyleRanges& style_ranges, size_t length) { + if (length == 0) { + DCHECK(style_ranges.empty()) << "Style ranges exist for empty text."; + return; + } + for (gfx::StyleRanges::size_type i = 0; i < style_ranges.size() - 1; i++) { + const ui::Range& former = style_ranges[i].range; + const ui::Range& latter = style_ranges[i + 1].range; + DCHECK(!former.is_empty()) << "Empty range at " << i << ":" << former; + DCHECK(former.IsValid()) << "Invalid range at " << i << ":" << former; + DCHECK(!former.is_reversed()) << "Reversed range at " << i << ":" << former; + DCHECK(former.end() == latter.start()) << "Ranges gap/overlap/unsorted." << + "former:" << former << ", latter:" << latter; + } + const gfx::StyleRange& end_style = *style_ranges.rbegin(); + DCHECK(!end_style.range.is_empty()) << "Empty range at end."; + DCHECK(end_style.range.IsValid()) << "Invalid range at end."; + DCHECK(!end_style.range.is_reversed()) << "Reversed range at end."; + DCHECK(end_style.range.end() == length) << "Style and text length mismatch."; +} +#endif + +void ApplyStyleRangeImpl(gfx::StyleRanges& style_ranges, + gfx::StyleRange style_range) { + const ui::Range& new_range = style_range.range; + // Follow StyleRanges invariant conditions: sorted and non-overlapping ranges. + gfx::StyleRanges::iterator i; + for (i = style_ranges.begin(); i != style_ranges.end();) { + if (i->range.end() < new_range.start()) { + i++; + } else if (i->range.start() == new_range.end()) { + break; + } else if (new_range.Contains(i->range)) { + i = style_ranges.erase(i); + if (i == style_ranges.end()) + break; + } else if (i->range.start() < new_range.start() && + i->range.end() > new_range.end()) { + // Split the current style into two styles. + gfx::StyleRange split_style = gfx::StyleRange(*i); + split_style.range.set_end(new_range.start()); + i = style_ranges.insert(i, split_style) + 1; + i->range.set_start(new_range.end()); + break; + } else if (i->range.start() < new_range.start()) { + i->range.set_end(new_range.start()); + i++; + } else if (i->range.end() > new_range.end()) { + i->range.set_start(new_range.end()); + break; + } else + NOTREACHED(); + } + // Add the new range in its sorted location. + style_ranges.insert(i, style_range); +} + +} // namespace + +namespace gfx { + +StyleRange::StyleRange() + : font(), + foreground(SK_ColorBLACK), + strike(false), + underline(false), + range() { +} + +void RenderText::SetText(const string16& text) { + size_t old_text_length = text_.length(); + text_ = text; + + // Update the style ranges as needed. + if (text_.empty()) { + style_ranges_.clear(); + } else if (style_ranges_.empty()) { + ApplyDefaultStyle(); + } else if (text_.length() > old_text_length) { + style_ranges_.back().range.set_end(text_.length()); + } else if (text_.length() < old_text_length) { + StyleRanges::iterator i; + for (i = style_ranges_.begin(); i != style_ranges_.end(); i++) { + if (i->range.start() >= text_.length()) { + i = style_ranges_.erase(i); + if (i == style_ranges_.end()) + break; + } else if (i->range.end() > text_.length()) { + i->range.set_end(text_.length()); + } + } + style_ranges_.back().range.set_end(text_.length()); + } +#ifndef NDEBUG + CheckStyleRanges(style_ranges_, text_.length()); +#endif +} + +size_t RenderText::GetCursorPosition() const { + return GetSelection().end(); +} + +void RenderText::SetCursorPosition(const size_t position) { + SetSelection(ui::Range(position, position)); +} + +void RenderText::MoveCursorLeft(BreakType break_type, bool select) { + if (break_type == LINE_BREAK) { + MoveCursorTo(0, select); + return; + } + size_t position = GetCursorPosition(); + // Cancelling a selection moves to the edge of the selection. + if (!GetSelection().is_empty() && !select) { + // Use the selection start if it is left of the selection end. + if (GetCursorBounds(GetSelection().start(), false).x() < + GetCursorBounds(position, false).x()) + position = GetSelection().start(); + // If |move_by_word|, use the nearest word boundary left of the selection. + if (break_type == WORD_BREAK) + position = GetLeftCursorPosition(position, true); + } else { + position = GetLeftCursorPosition(position, break_type == WORD_BREAK); + } + MoveCursorTo(position, select); +} + +void RenderText::MoveCursorRight(BreakType break_type, bool select) { + if (break_type == LINE_BREAK) { + MoveCursorTo(text().length(), select); + return; + } + size_t position = GetCursorPosition(); + // Cancelling a selection moves to the edge of the selection. + if (!GetSelection().is_empty() && !select) { + // Use the selection start if it is right of the selection end. + if (GetCursorBounds(GetSelection().start(), false).x() > + GetCursorBounds(position, false).x()) + position = GetSelection().start(); + // If |move_by_word|, use the nearest word boundary right of the selection. + if (break_type == WORD_BREAK) + position = GetRightCursorPosition(position, true); + } else { + position = GetRightCursorPosition(position, break_type == WORD_BREAK); + } + MoveCursorTo(position, select); +} + +bool RenderText::MoveCursorTo(size_t position, bool select) { + bool changed = GetCursorPosition() != position || + select == GetSelection().is_empty(); + if (select) + SetSelection(ui::Range(GetSelection().start(), position)); + else + SetSelection(ui::Range(position, position)); + return changed; +} + +bool RenderText::MoveCursorTo(const gfx::Point& point, bool select) { + // TODO(msw): Make this function support cursor placement via mouse near BiDi + // level changes. The visual cursor appearance will depend on the location + // clicked, not solely the resulting logical cursor position. See the TODO + // note pertaining to selection_range_ for more information. + return MoveCursorTo(FindCursorPosition(point), select); +} + +const ui::Range& RenderText::GetSelection() const { + return selection_range_; +} + +void RenderText::SetSelection(const ui::Range& range) { + selection_range_.set_end(std::min(range.end(), text().length())); + selection_range_.set_start(std::min(range.start(), text().length())); + + // Update |display_offset_| to ensure the current cursor is visible. + gfx::Rect cursor_bounds(GetCursorBounds(GetCursorPosition(), insert_mode())); + int display_width = display_rect_.width(); + int string_width = GetStringWidth(); + if (string_width < display_width) { + // Show all text whenever the text fits to the size. + display_offset_.set_x(0); + } else if ((display_offset_.x() + cursor_bounds.right()) > display_width) { + // Pan to show the cursor when it overflows to the right, + display_offset_.set_x(display_width - cursor_bounds.right()); + } else if ((display_offset_.x() + cursor_bounds.x()) < 0) { + // Pan to show the cursor when it overflows to the left. + display_offset_.set_x(-cursor_bounds.x()); + } +} + +bool RenderText::IsPointInSelection(const gfx::Point& point) const { + size_t pos = FindCursorPosition(point); + return (pos >= GetSelection().GetMin() && pos < GetSelection().GetMax()); +} + +void RenderText::ClearSelection() { + SetCursorPosition(GetCursorPosition()); +} + +void RenderText::SelectAll() { + SetSelection(ui::Range(0, text().length())); +} + +void RenderText::SelectWord() { + size_t selection_start = GetSelection().start(); + size_t cursor_position = GetCursorPosition(); + // First we setup selection_start_ and cursor_pos_. There are so many cases + // because we try to emulate what select-word looks like in a gtk textfield. + // See associated testcase for different cases. + if (cursor_position > 0 && cursor_position < text().length()) { + if (isalnum(text()[cursor_position])) { + selection_start = cursor_position; + cursor_position++; + } else + selection_start = cursor_position - 1; + } else if (cursor_position == 0) { + selection_start = cursor_position; + if (text().length() > 0) + cursor_position++; + } else { + selection_start = cursor_position - 1; + } + + // Now we move selection_start_ to beginning of selection. Selection boundary + // is defined as the position where we have alpha-num character on one side + // and non-alpha-num char on the other side. + for (; selection_start > 0; selection_start--) { + if (IsPositionAtWordSelectionBoundary(selection_start)) + break; + } + + // Now we move cursor_pos_ to end of selection. Selection boundary + // is defined as the position where we have alpha-num character on one side + // and non-alpha-num char on the other side. + for (; cursor_position < text().length(); cursor_position++) { + if (IsPositionAtWordSelectionBoundary(cursor_position)) + break; + } + + SetSelection(ui::Range(selection_start, cursor_position)); +} + +const ui::Range& RenderText::GetCompositionRange() const { + return composition_range_; +} + +void RenderText::SetCompositionRange(const ui::Range& composition_range) { + CHECK(!composition_range.IsValid() || + ui::Range(0, text_.length()).Contains(composition_range)); + composition_range_.set_end(composition_range.end()); + composition_range_.set_start(composition_range.start()); +} + +void RenderText::ApplyStyleRange(StyleRange style_range) { + const ui::Range& new_range = style_range.range; + if (!new_range.IsValid() || new_range.is_empty()) + return; + CHECK(!new_range.is_reversed()); + CHECK(ui::Range(0, text_.length()).Contains(new_range)); + ApplyStyleRangeImpl(style_ranges_, style_range); +#ifndef NDEBUG + CheckStyleRanges(style_ranges_, text_.length()); +#endif +} + +void RenderText::ApplyDefaultStyle() { + style_ranges_.clear(); + StyleRange style = StyleRange(default_style_); + style.range.set_end(text_.length()); + style_ranges_.push_back(style); +} + +base::i18n::TextDirection RenderText::GetTextDirection() const { + // TODO(msw): Bidi implementation, intended to replace the functionality added + // in crrev.com/91881 (discussed in codereview.chromium.org/7324011). + return base::i18n::LEFT_TO_RIGHT; +} + +int RenderText::GetStringWidth() const { + return GetSubstringBounds(ui::Range(0, text_.length()))[0].width(); +} + +void RenderText::Draw(gfx::Canvas* canvas) { + // Clip the canvas to the text display area. + canvas->ClipRectInt(display_rect_.x(), display_rect_.y(), + display_rect_.width(), display_rect_.height()); + + // Draw the selection. + std::vector<gfx::Rect> selection(GetSubstringBounds(GetSelection())); + SkColor selection_color = + focused() ? kFocusedSelectionColor : kUnfocusedSelectionColor; + for (std::vector<gfx::Rect>::const_iterator i = selection.begin(); + i < selection.end(); ++i) { + gfx::Rect r(*i); + r.Offset(display_offset_); + canvas->FillRectInt(selection_color, r.x(), r.y(), r.width(), r.height()); + } + + // Create a temporary copy of the style ranges for composition and selection. + // TODO(msw): This pattern ought to be reconsidered; what about composition + // and selection overlaps, retain existing local style features? + StyleRanges style_ranges(style_ranges_); + // Apply a composition style override to a copy of the style ranges. + if (composition_range_.IsValid() && !composition_range_.is_empty()) { + StyleRange composition_style(default_style_); + composition_style.underline = true; + composition_style.range.set_start(composition_range_.start()); + composition_style.range.set_end(composition_range_.end()); + ApplyStyleRangeImpl(style_ranges, composition_style); + } + // Apply a selection style override to a copy of the style ranges. + if (selection_range_.IsValid() && !selection_range_.is_empty()) { + StyleRange selection_style(default_style_); + selection_style.foreground = kSelectedTextColor; + selection_style.range.set_start(selection_range_.GetMin()); + selection_style.range.set_end(selection_range_.GetMax()); + ApplyStyleRangeImpl(style_ranges, selection_style); + } + + // Draw the text. + gfx::Rect bounds(display_rect_); + bounds.Offset(display_offset_); + for (StyleRanges::const_iterator i = style_ranges.begin(); + i < style_ranges.end(); ++i) { + Font font = !i->underline ? i->font : + i->font.DeriveFont(0, i->font.GetStyle() | Font::UNDERLINED); + string16 text = text_.substr(i->range.start(), i->range.length()); + bounds.set_width(font.GetStringWidth(text)); + canvas->DrawStringInt(text, font, i->foreground, bounds); + + // Draw the strikethrough. + if (i->strike) { + SkPaint paint; + paint.setAntiAlias(true); + paint.setStyle(SkPaint::kFill_Style); + paint.setColor(i->foreground); + paint.setStrokeWidth(kStrikeWidth); + canvas->AsCanvasSkia()->drawLine(SkIntToScalar(bounds.x()), + SkIntToScalar(bounds.bottom()), + SkIntToScalar(bounds.right()), + SkIntToScalar(bounds.y()), + paint); + } + + bounds.set_x(bounds.x() + bounds.width()); + } + + // Paint cursor. Replace cursor is drawn as rectangle for now. + if (cursor_visible() && focused()) { + bounds = GetCursorBounds(GetCursorPosition(), insert_mode()); + bounds.Offset(display_offset_); + if (!bounds.IsEmpty()) + canvas->DrawRectInt(kCursorColor, + bounds.x(), + bounds.y(), + bounds.width(), + bounds.height()); + } +} + +size_t RenderText::FindCursorPosition(const gfx::Point& point) const { + const gfx::Font& font = Font(); + int left = 0; + int left_pos = 0; + int right = font.GetStringWidth(text()); + int right_pos = text().length(); + + int x = point.x(); + if (x <= left) return left_pos; + if (x >= right) return right_pos; + // binary searching the cursor position. + // TODO(oshima): use the center of character instead of edge. + // Binary search may not work for language like arabic. + while (std::abs(static_cast<long>(right_pos - left_pos) > 1)) { + int pivot_pos = left_pos + (right_pos - left_pos) / 2; + int pivot = font.GetStringWidth(text().substr(0, pivot_pos)); + if (pivot < x) { + left = pivot; + left_pos = pivot_pos; + } else if (pivot == x) { + return pivot_pos; + } else { + right = pivot; + right_pos = pivot_pos; + } + } + return left_pos; +} + +std::vector<gfx::Rect> RenderText::GetSubstringBounds( + const ui::Range& range) const { + size_t start = range.GetMin(); + size_t end = range.GetMax(); + gfx::Font font; + int start_x = font.GetStringWidth(text().substr(0, start)); + int end_x = font.GetStringWidth(text().substr(0, end)); + std::vector<gfx::Rect> bounds; + bounds.push_back(gfx::Rect(start_x, 0, end_x - start_x, font.GetHeight())); + return bounds; +} + +gfx::Rect RenderText::GetCursorBounds(size_t cursor_pos, + bool insert_mode) const { + gfx::Font font; + int x = font.GetStringWidth(text_.substr(0U, cursor_pos)); + DCHECK_GE(x, 0); + int h = std::min(display_rect_.height(), font.GetHeight()); + gfx::Rect bounds(x, (display_rect_.height() - h) / 2, 1, h); + if (!insert_mode && text_.length() != cursor_pos) + bounds.set_width(font.GetStringWidth(text_.substr(0, cursor_pos + 1)) - x); + return bounds; +} + +size_t RenderText::GetLeftCursorPosition(size_t position, + bool move_by_word) const { + if (!move_by_word) + return position == 0? position : position - 1; + // Notes: We always iterate words from the begining. + // This is probably fast enough for our usage, but we may + // want to modify WordIterator so that it can start from the + // middle of string and advance backwards. + base::i18n::BreakIterator iter(text(), base::i18n::BreakIterator::BREAK_WORD); + bool success = iter.Init(); + DCHECK(success); + if (!success) + return position; + int last = 0; + while (iter.Advance()) { + if (iter.IsWord()) { + size_t begin = iter.pos() - iter.GetString().length(); + if (begin == position) { + // The cursor is at the beginning of a word. + // Move to previous word. + break; + } else if(iter.pos() >= position) { + // The cursor is in the middle or at the end of a word. + // Move to the top of current word. + last = begin; + break; + } else { + last = iter.pos() - iter.GetString().length(); + } + } + } + + return last; +} + +size_t RenderText::GetRightCursorPosition(size_t position, + bool move_by_word) const { + if (!move_by_word) + return std::min(position + 1, text().length()); + base::i18n::BreakIterator iter(text(), base::i18n::BreakIterator::BREAK_WORD); + bool success = iter.Init(); + DCHECK(success); + if (!success) + return position; + size_t pos = 0; + while (iter.Advance()) { + pos = iter.pos(); + if (iter.IsWord() && pos > position) { + break; + } + } + return pos; +} + +RenderText::RenderText() + : text_(), + selection_range_(), + cursor_visible_(false), + insert_mode_(true), + composition_range_(), + style_ranges_(), + default_style_(), + display_rect_(), + display_offset_() { +} + +RenderText::~RenderText() { +} + +bool RenderText::IsPositionAtWordSelectionBoundary(size_t pos) { + return pos == 0 || (isalnum(text()[pos - 1]) && !isalnum(text()[pos])) || + (!isalnum(text()[pos - 1]) && isalnum(text()[pos])); +} + +} // namespace gfx diff --git a/ui/gfx/render_text.h b/ui/gfx/render_text.h new file mode 100644 index 0000000..e0cdc0b --- /dev/null +++ b/ui/gfx/render_text.h @@ -0,0 +1,224 @@ +// 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 <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 { + +// Strike line width. +const int kStrikeWidth = 2; + +// Color settings for text, backgrounds and cursor. +// These are tentative, and should be derived from theme, system +// settings and current settings. +// TODO(oshima): Change this to match the standard chrome +// before dogfooding textfield views. +const SkColor kSelectedTextColor = SK_ColorWHITE; +const SkColor kFocusedSelectionColor = SK_ColorCYAN; +const SkColor kUnfocusedSelectionColor = SK_ColorLTGRAY; +const SkColor kCursorColor = SK_ColorBLACK; + +} // namespace + +namespace gfx { + +class Canvas; +class RenderTextTest; + +// A visual style applicable to a range of text. +struct UI_API StyleRange { + StyleRange(); + + Font font; + SkColor foreground; + bool strike; + bool underline; + ui::Range range; +}; + +typedef std::vector<StyleRange> StyleRanges; + +// TODO(msw): Distinguish between logical character and glyph? +enum BreakType { + CHARACTER_BREAK, + WORD_BREAK, + LINE_BREAK, +}; + +// 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 UI_API 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); + + bool cursor_visible() const { return cursor_visible_; } + void set_cursor_visible(bool visible) { cursor_visible_ = visible; } + + bool insert_mode() const { return insert_mode_; } + void toggle_insert_mode() { insert_mode_ = !insert_mode_; } + + bool focused() const { return focused_; } + void set_focused(bool focused) { focused_ = focused; } + + const StyleRange& default_style() const { return default_style_; } + void set_default_style(StyleRange style) { default_style_ = style; } + + const gfx::Rect& display_rect() const { return display_rect_; } + void set_display_rect(const gfx::Rect& r) { display_rect_ = r; } + + size_t GetCursorPosition() const; + void SetCursorPosition(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 |break_type| is CHARACTER_BREAK, move to the neighboring character. + // If |break_type| is WORD_BREAK, move to the nearest word boundary. + // If |break_type| is LINE_BREAK, move to text edge as shown on screen. + void MoveCursorLeft(BreakType break_type, bool select); + void MoveCursorRight(BreakType break_type, 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); + + // Move the cursor to the position associated with the clicked point. + // If |select| is false, the selection range is emptied at the new position. + bool MoveCursorTo(const gfx::Point& point, bool select); + + const ui::Range& GetSelection() const; + void SetSelection(const ui::Range& range); + + // Returns true if the local point is over selected text. + bool IsPointInSelection(const gfx::Point& point) const; + + // 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. + virtual void ApplyStyleRange(StyleRange style_range); + + // Apply |default_style_| over the entire text range. + virtual void ApplyDefaultStyle(); + + base::i18n::TextDirection GetTextDirection() const; + + // Get the width of the entire string. + int GetStringWidth() const; + + virtual void Draw(gfx::Canvas* canvas); + + // TODO(msw): Deprecate this function. Logical and visual cursors are not + // mapped one-to-one. See the selection_range_ TODO for more information. + // 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 an odd number of 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(); + + const StyleRanges& style_ranges() const { return style_ranges_; } + + const gfx::Point& display_offset() const { return display_offset_; } + + // 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); + + // Clear out |style_ranges_|. + void ClearStyleRanges(); + + bool IsPositionAtWordSelectionBoundary(size_t pos); + + // Logical UTF-16 string data to be drawn. + string16 text_; + + // TODO(msw): A single logical cursor position doesn't support two potential + // visual cursor positions. For example, clicking right of 'c' & 'D' yeilds: + // (visually: 'abc|FEDghi' and 'abcFED|ghi', both logically: 'abc|DEFghi'). + // Similarly, one visual position may have two associated logical positions. + // For example, clicking the right side of 'D' and left side of 'g' yields: + // (both visually: 'abcFED|ghi', logically: 'abc|DEFghi' and 'abcDEF|ghi'). + // Update the cursor model with a leading/trailing flag, a level association, + // or a disjoint visual position to satisfy the proposed visual behavior. + // Logical selection range; the range end is also the logical cursor position. + ui::Range selection_range_; + + // The cursor visibility and insert mode. + bool cursor_visible_; + bool insert_mode_; + + // The focus state of the text. + bool 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_ diff --git a/ui/gfx/render_text_linux.cc b/ui/gfx/render_text_linux.cc new file mode 100644 index 0000000..21b3ac4 --- /dev/null +++ b/ui/gfx/render_text_linux.cc @@ -0,0 +1,20 @@ +// 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. + +#include "ui/gfx/render_text_linux.h" + +namespace gfx { + +RenderTextLinux::RenderTextLinux() + : RenderText() { +} + +RenderTextLinux::~RenderTextLinux() { +} + +RenderText* RenderText::CreateRenderText() { + return new RenderTextLinux; +} + +} // namespace gfx diff --git a/ui/gfx/render_text_linux.h b/ui/gfx/render_text_linux.h new file mode 100644 index 0000000..2709bea --- /dev/null +++ b/ui/gfx/render_text_linux.h @@ -0,0 +1,25 @@ +// 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_LINUX_H_ +#define UI_GFX_RENDER_TEXT_LINUX_H_ +#pragma once + +#include "ui/gfx/render_text.h" + +namespace gfx { + +// RenderTextLinux is the Linux implementation of RenderText using Pango. +class RenderTextLinux : public RenderText { + public: + RenderTextLinux(); + virtual ~RenderTextLinux(); + +private: + DISALLOW_COPY_AND_ASSIGN(RenderTextLinux); +}; + +} // namespace gfx; + +#endif // UI_GFX_RENDER_TEXT_LINUX_H_ diff --git a/ui/gfx/render_text_unittest.cc b/ui/gfx/render_text_unittest.cc new file mode 100644 index 0000000..e809d41 --- /dev/null +++ b/ui/gfx/render_text_unittest.cc @@ -0,0 +1,180 @@ +// 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. + +#include "ui/gfx/render_text.h" + +#include "base/utf_string_conversions.h" +#include "testing/gtest/include/gtest/gtest.h" + +namespace gfx { + +class RenderTextTest : public testing::Test { +}; + +TEST_F(RenderTextTest, DefaultStyle) { + // Defaults to empty text with no styles. + gfx::RenderText* render_text = gfx::RenderText::CreateRenderText(); + EXPECT_TRUE(render_text->text().empty()); + EXPECT_TRUE(render_text->style_ranges().empty()); + + // Test that the built-in default style is applied for new text. + render_text->SetText(ASCIIToUTF16("abc")); + EXPECT_EQ(1U, render_text->style_ranges().size()); + gfx::StyleRange style; + EXPECT_EQ(style.font.GetFontName(), + render_text->style_ranges()[0].font.GetFontName()); + EXPECT_EQ(style.foreground, render_text->style_ranges()[0].foreground); + EXPECT_EQ(ui::Range(0, 3), render_text->style_ranges()[0].range); + EXPECT_EQ(style.strike, render_text->style_ranges()[0].strike); + EXPECT_EQ(style.underline, render_text->style_ranges()[0].underline); + + // Test that clearing the text also clears the styles. + render_text->SetText(string16()); + EXPECT_TRUE(render_text->text().empty()); + EXPECT_TRUE(render_text->style_ranges().empty()); +} + +TEST_F(RenderTextTest, CustomDefaultStyle) { + // Test a custom default style. + gfx::RenderText* render_text = gfx::RenderText::CreateRenderText(); + gfx::StyleRange color; + color.foreground = SK_ColorRED; + render_text->set_default_style(color); + render_text->SetText(ASCIIToUTF16("abc")); + EXPECT_EQ(1U, render_text->style_ranges().size()); + EXPECT_EQ(color.foreground, render_text->style_ranges()[0].foreground); + + // Test that the custom default style persists across clearing text. + render_text->SetText(string16()); + EXPECT_TRUE(render_text->style_ranges().empty()); + render_text->SetText(ASCIIToUTF16("abc")); + EXPECT_EQ(1U, render_text->style_ranges().size()); + EXPECT_EQ(color.foreground, render_text->style_ranges()[0].foreground); + + // Test ApplyDefaultStyle after setting a new default. + gfx::StyleRange strike; + strike.strike = true; + render_text->set_default_style(strike); + render_text->ApplyDefaultStyle(); + EXPECT_EQ(1U, render_text->style_ranges().size()); + EXPECT_EQ(true, render_text->style_ranges()[0].strike); + EXPECT_EQ(strike.foreground, render_text->style_ranges()[0].foreground); +} + +TEST_F(RenderTextTest, ApplyStyleRange) { + gfx::RenderText* render_text = gfx::RenderText::CreateRenderText(); + render_text->SetText(ASCIIToUTF16("01234")); + EXPECT_EQ(1U, render_text->style_ranges().size()); + + // Test ApplyStyleRange (no-op on empty range). + gfx::StyleRange empty; + empty.range = ui::Range(1, 1); + render_text->ApplyStyleRange(empty); + EXPECT_EQ(1U, render_text->style_ranges().size()); + + // Test ApplyStyleRange (no-op on invalid range). + gfx::StyleRange invalid; + invalid.range = ui::Range::InvalidRange(); + render_text->ApplyStyleRange(invalid); + EXPECT_EQ(1U, render_text->style_ranges().size()); + + // Apply a style with a range contained by an existing range. + gfx::StyleRange underline; + underline.underline = true; + underline.range = ui::Range(2, 3); + render_text->ApplyStyleRange(underline); + EXPECT_EQ(3U, render_text->style_ranges().size()); + EXPECT_EQ(ui::Range(0, 2), render_text->style_ranges()[0].range); + EXPECT_FALSE(render_text->style_ranges()[0].underline); + EXPECT_EQ(ui::Range(2, 3), render_text->style_ranges()[1].range); + EXPECT_TRUE(render_text->style_ranges()[1].underline); + EXPECT_EQ(ui::Range(3, 5), render_text->style_ranges()[2].range); + EXPECT_FALSE(render_text->style_ranges()[2].underline); + + // Apply a style with a range equal to another range. + gfx::StyleRange color; + color.foreground = SK_ColorWHITE; + color.range = ui::Range(2, 3); + render_text->ApplyStyleRange(color); + EXPECT_EQ(3U, render_text->style_ranges().size()); + EXPECT_EQ(ui::Range(0, 2), render_text->style_ranges()[0].range); + EXPECT_NE(SK_ColorWHITE, render_text->style_ranges()[0].foreground); + EXPECT_FALSE(render_text->style_ranges()[0].underline); + EXPECT_EQ(ui::Range(2, 3), render_text->style_ranges()[1].range); + EXPECT_EQ(SK_ColorWHITE, render_text->style_ranges()[1].foreground); + EXPECT_FALSE(render_text->style_ranges()[1].underline); + EXPECT_EQ(ui::Range(3, 5), render_text->style_ranges()[2].range); + EXPECT_NE(SK_ColorWHITE, render_text->style_ranges()[2].foreground); + EXPECT_FALSE(render_text->style_ranges()[2].underline); + + // Apply a style with a range containing an existing range. + // This new style also overlaps portions of neighboring ranges. + gfx::StyleRange strike; + strike.strike = true; + strike.range = ui::Range(1, 4); + render_text->ApplyStyleRange(strike); + EXPECT_EQ(3U, render_text->style_ranges().size()); + EXPECT_EQ(ui::Range(0, 1), render_text->style_ranges()[0].range); + EXPECT_FALSE(render_text->style_ranges()[0].strike); + EXPECT_EQ(ui::Range(1, 4), render_text->style_ranges()[1].range); + EXPECT_TRUE(render_text->style_ranges()[1].strike); + EXPECT_EQ(ui::Range(4, 5), render_text->style_ranges()[2].range); + EXPECT_FALSE(render_text->style_ranges()[2].strike); + + // Apply a style overlapping all ranges. + gfx::StyleRange strike_underline; + strike_underline.strike = true; + strike_underline.underline = true; + strike_underline.range = ui::Range(0, render_text->text().length()); + render_text->ApplyStyleRange(strike_underline); + EXPECT_EQ(1U, render_text->style_ranges().size()); + EXPECT_EQ(ui::Range(0, 5), render_text->style_ranges()[0].range); + EXPECT_TRUE(render_text->style_ranges()[0].underline); + EXPECT_TRUE(render_text->style_ranges()[0].strike); + + // Apply the default style. + render_text->ApplyDefaultStyle(); + EXPECT_EQ(1U, render_text->style_ranges().size()); + EXPECT_EQ(ui::Range(0, 5), render_text->style_ranges()[0].range); + EXPECT_FALSE(render_text->style_ranges()[0].underline); + EXPECT_FALSE(render_text->style_ranges()[0].strike); +} + +TEST_F(RenderTextTest, StyleRangesAdjust) { + // Test that style ranges adjust to the text size. + gfx::RenderText* render_text = gfx::RenderText::CreateRenderText(); + render_text->SetText(ASCIIToUTF16("abcdef")); + EXPECT_EQ(1U, render_text->style_ranges().size()); + EXPECT_EQ(ui::Range(0, 6), render_text->style_ranges()[0].range); + + // Test that the range is clipped to the length of shorter text. + render_text->SetText(ASCIIToUTF16("abc")); + EXPECT_EQ(1U, render_text->style_ranges().size()); + EXPECT_EQ(ui::Range(0, 3), render_text->style_ranges()[0].range); + + // Test that the last range extends to the length of longer text. + gfx::StyleRange strike; + strike.strike = true; + strike.range = ui::Range(2, 3); + render_text->ApplyStyleRange(strike); + render_text->SetText(ASCIIToUTF16("abcdefghi")); + EXPECT_EQ(2U, render_text->style_ranges().size()); + EXPECT_EQ(ui::Range(0, 2), render_text->style_ranges()[0].range); + EXPECT_EQ(ui::Range(2, 9), render_text->style_ranges()[1].range); + EXPECT_TRUE(render_text->style_ranges()[1].strike); + + // Test that ranges are removed if they're outside the range of shorter text. + render_text->SetText(ASCIIToUTF16("ab")); + EXPECT_EQ(1U, render_text->style_ranges().size()); + EXPECT_EQ(ui::Range(0, 2), render_text->style_ranges()[0].range); + EXPECT_FALSE(render_text->style_ranges()[0].strike); + + // Test that previously removed ranges don't return. + render_text->SetText(ASCIIToUTF16("abcdef")); + EXPECT_EQ(1U, render_text->style_ranges().size()); + EXPECT_EQ(ui::Range(0, 6), render_text->style_ranges()[0].range); + EXPECT_FALSE(render_text->style_ranges()[0].strike); +} + +} // namespace gfx diff --git a/ui/gfx/render_text_win.cc b/ui/gfx/render_text_win.cc new file mode 100644 index 0000000..9f47946 --- /dev/null +++ b/ui/gfx/render_text_win.cc @@ -0,0 +1,20 @@ +// 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. + +#include "ui/gfx/render_text_win.h" + +namespace gfx { + +RenderTextWin::RenderTextWin() + : RenderText() { +} + +RenderTextWin::~RenderTextWin() { +} + +RenderText* RenderText::CreateRenderText() { + return new RenderTextWin; +} + +} // namespace gfx diff --git a/ui/gfx/render_text_win.h b/ui/gfx/render_text_win.h new file mode 100644 index 0000000..829888b --- /dev/null +++ b/ui/gfx/render_text_win.h @@ -0,0 +1,25 @@ +// 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_WIN_H_ +#define UI_GFX_RENDER_TEXT_WIN_H_ +#pragma once + +#include "ui/gfx/render_text.h" + +namespace gfx { + +// RenderTextWin is the Windows implementation of RenderText using Uniscribe. +class RenderTextWin : public RenderText { + public: + RenderTextWin(); + virtual ~RenderTextWin(); + + private: + DISALLOW_COPY_AND_ASSIGN(RenderTextWin); +}; + +} // namespace gfx; + +#endif // UI_GFX_RENDER_TEXT_WIN_H_ |