summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authormirandac@chromium.org <mirandac@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2010-04-05 18:57:01 +0000
committermirandac@chromium.org <mirandac@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2010-04-05 18:57:01 +0000
commitd849078c2cd734e7ed905a69e231df67f644f401 (patch)
treee1a8b8e7d9cb075d1540396cf2ecfa43e4bf8b94
parent57678648c277ebd697c621d3cdefc9549d26378d (diff)
downloadchromium_src-d849078c2cd734e7ed905a69e231df67f644f401.zip
chromium_src-d849078c2cd734e7ed905a69e231df67f644f401.tar.gz
chromium_src-d849078c2cd734e7ed905a69e231df67f644f401.tar.bz2
Move utility method for embedding links in texts from about_chrome_view to a generic class, for future use in other classes.
BUG= none TEST= chrome "about" dialog still works the same, in ltr and rtl languages. Review URL: http://codereview.chromium.org/1508018 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@43634 0039d316-1c4b-4281-b951-d872f2087c98
-rw-r--r--chrome/browser/views/about_chrome_view.cc175
-rw-r--r--chrome/browser/views/about_chrome_view.h38
-rw-r--r--views/view_text_utils.cc167
-rw-r--r--views/view_text_utils.h75
-rw-r--r--views/views.gyp2
5 files changed, 260 insertions, 197 deletions
diff --git a/chrome/browser/views/about_chrome_view.cc b/chrome/browser/views/about_chrome_view.cc
index 25f944c..2004ac0 100644
--- a/chrome/browser/views/about_chrome_view.cc
+++ b/chrome/browser/views/about_chrome_view.cc
@@ -4,13 +4,11 @@
#include "chrome/browser/views/about_chrome_view.h"
-#include "app/bidi_line_iterator.h"
#include "app/l10n_util.h"
#include "app/resource_bundle.h"
#include "base/callback.h"
#include "base/file_version_info.h"
#include "base/i18n/rtl.h"
-#include "base/i18n/word_iterator.h"
#include "base/utf_string_conversions.h"
#include "chrome/browser/browser_list.h"
#include "chrome/browser/metrics/user_metrics.h"
@@ -18,7 +16,6 @@
#include "chrome/common/platform_util.h"
#include "chrome/common/url_constants.h"
#include "gfx/canvas.h"
-#include "gfx/color_utils.h"
#include "grit/chromium_strings.h"
#include "grit/generated_resources.h"
#include "grit/locale_settings.h"
@@ -26,6 +23,7 @@
#include "views/controls/textfield/textfield.h"
#include "views/controls/throbber.h"
#include "views/standard_layout.h"
+#include "views/view_text_utils.h"
#include "views/widget/widget.h"
#include "views/window/window.h"
#include "webkit/glue/webkit_glue.h"
@@ -461,14 +459,17 @@ void AboutChromeView::Paint(gfx::Canvas* canvas) {
// if we need to wrap.
gfx::Size position;
// Draw the first text chunk and position the Chromium url.
- DrawTextAndPositionUrl(canvas, main_label_chunk1_, link1,
- rect1, &position, label_bounds, font);
+ view_text_utils::DrawTextAndPositionUrl(canvas, main_text_label_,
+ main_label_chunk1_, link1, rect1, &position, text_direction_is_rtl_,
+ label_bounds, font);
// Draw the second text chunk and position the Open Source url.
- DrawTextAndPositionUrl(canvas, main_label_chunk2_, link2,
- rect2, &position, label_bounds, font);
+ view_text_utils::DrawTextAndPositionUrl(canvas, main_text_label_,
+ main_label_chunk2_, link2, rect2, &position, text_direction_is_rtl_,
+ label_bounds, font);
// Draw the third text chunk (which has no URL associated with it).
- DrawTextAndPositionUrl(canvas, main_label_chunk3_, NULL, NULL, &position,
- label_bounds, font);
+ view_text_utils::DrawTextAndPositionUrl(canvas, main_text_label_,
+ main_label_chunk3_, NULL, NULL, &position, text_direction_is_rtl_,
+ label_bounds, font);
#if defined(GOOGLE_CHROME_BUILD)
// Insert a line break and some whitespace.
@@ -476,12 +477,13 @@ void AboutChromeView::Paint(gfx::Canvas* canvas) {
position.Enlarge(0, font.height() + kRelatedControlVerticalSpacing);
// Now the Google Terms of Service and position the TOS url.
- DrawTextAndPositionUrl(canvas, main_label_chunk4_, terms_of_service_url_,
- &terms_of_service_url_rect_, &position, label_bounds,
- font);
+ view_text_utils::DrawTextAndPositionUrl(canvas, main_text_label_,
+ main_label_chunk4_, terms_of_service_url_, &terms_of_service_url_rect_,
+ &position, text_direction_is_rtl_, label_bounds, font);
// The last text chunk doesn't have a URL associated with it.
- DrawTextAndPositionUrl(canvas, main_label_chunk5_, NULL, NULL, &position,
- label_bounds, font);
+ view_text_utils::DrawTextAndPositionUrl(canvas, main_text_label_,
+ main_label_chunk5_, NULL, NULL, &position, text_direction_is_rtl_,
+ label_bounds, font);
// Position the TOS URL within the main label.
terms_of_service_url_->SetBounds(terms_of_service_url_rect_.x(),
@@ -524,151 +526,6 @@ void AboutChromeView::Paint(gfx::Canvas* canvas) {
main_text_label_height_ = position.height() + font.height();
}
-void AboutChromeView::DrawTextAndPositionUrl(gfx::Canvas* canvas,
- const std::wstring& text,
- views::Link* link,
- gfx::Rect* rect,
- gfx::Size* position,
- const gfx::Rect& bounds,
- const gfx::Font& font) {
- DCHECK(canvas && position);
-
- // What we get passed in as |text| is potentially a mix of LTR and RTL "runs"
- // (a run is a sequence of words that share the same directionality). We
- // initialize a bidirectional ICU line iterator and split the text into runs
- // that are either strictly LTR or strictly RTL (and do not contain a mix).
- BiDiLineIterator bidi_line;
- if (!bidi_line.Open(text.c_str(), true, false))
- return;
-
- // Iterate over each run and draw it.
- int run_start = 0;
- int run_end = 0;
- const int runs = bidi_line.CountRuns();
- for (int run = 0; run < runs; ++run) {
- UBiDiLevel level = 0;
- bidi_line.GetLogicalRun(run_start, &run_end, &level);
- std::wstring fragment = StringSubRange(text, run_start, run_end);
-
- // A flag that tells us whether we found LTR text inside RTL text.
- bool ltr_inside_rtl_text =
- ((level & 1) == UBIDI_LTR) && text_direction_is_rtl_;
-
- // Draw the text chunk contained in |fragment|. |position| is relative to
- // the top left corner of the label we draw inside (also when drawing RTL).
- DrawTextStartingFrom(canvas, fragment, position, bounds, font,
- ltr_inside_rtl_text);
-
- run_start = run_end; // Advance over what we just drew.
- }
-
- // If the caller is interested in placing a link after this text blurb, we
- // figure out here where to place it.
- if (link && rect) {
- gfx::Size sz = link->GetPreferredSize();
- gfx::Insets insets = link->GetInsets();
- WrapIfWordDoesntFit(sz.width(), font.height(), position, bounds);
- int x = position->width();
- int y = position->height();
-
- // Links have a border to allow them to be focused.
- y -= insets.top();
-
- *rect = gfx::Rect(x, y, sz.width(), sz.height());
-
- // Go from relative pixel coordinates (within the label we are drawing on)
- // to absolute pixel coordinates (relative to the top left corner of the
- // dialog content).
- rect->Offset(bounds.x(), bounds.y());
- // And leave some space to draw the link in.
- position->Enlarge(sz.width(), 0);
- }
-}
-
-void AboutChromeView::DrawTextStartingFrom(gfx::Canvas* canvas,
- const std::wstring& text,
- gfx::Size* position,
- const gfx::Rect& bounds,
- const gfx::Font& font,
- bool ltr_within_rtl) {
-#if defined(OS_WIN)
- const SkColor text_color = color_utils::GetSysSkColor(COLOR_WINDOWTEXT);
-#else
- // TODO(beng): source from theme provider.
- const SkColor text_color = SK_ColorBLACK;
-#endif
-
- // Iterate through line breaking opportunities (which in English would be
- // spaces and such. This tells us where to wrap.
- WordIterator iter(text, WordIterator::BREAK_LINE);
- if (!iter.Init())
- return;
-
- int flags = (text_direction_is_rtl_ ?
- gfx::Canvas::TEXT_ALIGN_RIGHT :
- gfx::Canvas::TEXT_ALIGN_LEFT) |
- gfx::Canvas::MULTI_LINE |
- gfx::Canvas::HIDE_PREFIX;
-
- // Iterate over each word in the text, or put in a more locale-neutral way:
- // iterate to the next line breaking opportunity.
- while (iter.Advance()) {
- // Get the word and figure out the dimensions.
- std::wstring word;
- if (!ltr_within_rtl)
- word = iter.GetWord(); // Get the next word.
- else
- word = text; // Draw the whole text at once.
-
- int w = font.GetStringWidth(word), h = font.height();
- canvas->SizeStringInt(word, font, &w, &h, flags);
-
- // If we exceed the boundaries, we need to wrap.
- WrapIfWordDoesntFit(w, font.height(), position, bounds);
-
- int x = main_text_label_->MirroredXCoordinateInsideView(position->width()) +
- bounds.x();
- if (text_direction_is_rtl_) {
- x -= w;
- // When drawing LTR strings inside RTL text we need to make sure we draw
- // the trailing space (if one exists after the LTR text) on the left of
- // the LTR string.
- if (ltr_within_rtl && word[word.size() - 1] == L' ') {
- int space_w = font.GetStringWidth(L" "), space_h = font.height();
- canvas->SizeStringInt(L" ", font, &space_w, &space_h, flags);
- x += space_w;
- }
- }
- int y = position->height() + bounds.y();
-
- // Draw the text on the screen (mirrored, if RTL run).
- canvas->DrawStringInt(
- word, font, text_color, x, y, w, font.height(), flags);
-
- if (word.size() > 0 && word[word.size() - 1] == L'\x0a') {
- // When we come across '\n', we move to the beginning of the next line.
- position->set_width(0);
- position->Enlarge(0, font.height());
- } else {
- // Otherwise, we advance position to the next word.
- position->Enlarge(w, 0);
- }
-
- if (ltr_within_rtl)
- break; // LTR within RTL is drawn as one unit, so we are done.
- }
-}
-
-void AboutChromeView::WrapIfWordDoesntFit(int word_width,
- int font_height,
- gfx::Size* position,
- const gfx::Rect& bounds) {
- if (position->width() + word_width > bounds.right()) {
- position->set_width(0);
- position->Enlarge(0, font_height);
- }
-}
-
void AboutChromeView::ViewHierarchyChanged(bool is_add,
views::View* parent,
views::View* child) {
diff --git a/chrome/browser/views/about_chrome_view.h b/chrome/browser/views/about_chrome_view.h
index 1250913..a82841a 100644
--- a/chrome/browser/views/about_chrome_view.h
+++ b/chrome/browser/views/about_chrome_view.h
@@ -100,44 +100,6 @@ class AboutChromeView : public views::View,
std::string version);
#endif
- // Draws a string onto the canvas (wrapping if needed) while also keeping
- // track of where it ends so we can position a URL after the text. The
- // parameter |bounds| represents the boundary we have to work with, |position|
- // specifies where to draw the string (relative to the top left corner of the
- // |bounds| rectangle and |font| specifies the font to use when drawing. When
- // the function returns, the parameter |rect| contains where to draw the URL
- // (to the right of where we just drew the text) and |position| is updated to
- // reflect where to draw the next string after the URL.
- // NOTE: The reason why we need this function is because while Skia knows how
- // to wrap text appropriately, it doesn't tell us where it drew the last
- // character, which we need to position the URLs within the text.
- void DrawTextAndPositionUrl(gfx::Canvas* canvas,
- const std::wstring& text,
- views::Link* link,
- gfx::Rect* rect,
- gfx::Size* position,
- const gfx::Rect& bounds,
- const gfx::Font& font);
-
- // A helper function for DrawTextAndPositionUrl, which simply draws the text
- // from a certain starting point |position| and wraps within bounds.
- // |word_for_word| specifies whether to draw the text word for word or wheter
- // to treat the text as one blurb (similar to the way url's are treated inside
- // RTL text. For details on the other parameters, see DrawTextAndPositionUrl.
- void DrawTextStartingFrom(gfx::Canvas* canvas,
- const std::wstring& text,
- gfx::Size* position,
- const gfx::Rect& bounds,
- const gfx::Font& font,
- bool word_for_word);
-
- // A simply utility function that calculates whether a word of width
- // |word_width| fits at position |position| within the |bounds| rectangle. If
- // not, |position| is updated to wrap to the beginning of the next line.
- void WrapIfWordDoesntFit(int word_width,
- int font_height,
- gfx::Size* position,
- const gfx::Rect& bounds);
Profile* profile_;
diff --git a/views/view_text_utils.cc b/views/view_text_utils.cc
new file mode 100644
index 0000000..2995f26
--- /dev/null
+++ b/views/view_text_utils.cc
@@ -0,0 +1,167 @@
+// Copyright (c) 2010 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 "views/view_text_utils.h"
+
+#include "app/bidi_line_iterator.h"
+#include "base/i18n/rtl.h"
+#include "base/i18n/word_iterator.h"
+#include "base/logging.h"
+#include "gfx/canvas.h"
+#include "gfx/color_utils.h"
+#include "gfx/size.h"
+#include "views/controls/label.h"
+#include "views/controls/link.h"
+
+namespace view_text_utils {
+
+void DrawTextAndPositionUrl(gfx::Canvas* canvas,
+ views::Label* label,
+ const std::wstring& text,
+ views::Link* link,
+ gfx::Rect* rect,
+ gfx::Size* position,
+ bool text_direction_is_rtl,
+ const gfx::Rect& bounds,
+ const gfx::Font& font) {
+ DCHECK(canvas && position);
+
+ // The |text| parameter is potentially a mix of LTR and RTL "runs," where
+ // a run is a sequence of words that share the same directionality. We
+ // initialize a bidirectional ICU line iterator and split the text into
+ // runs that are either strictly LTR or strictly RTL, with no mix.
+ BiDiLineIterator bidi_line;
+ if (!bidi_line.Open(text.c_str(), true, false))
+ return;
+
+ // Iterate over each run and draw it.
+ int run_start = 0;
+ int run_end = 0;
+ const int runs = bidi_line.CountRuns();
+ for (int run = 0; run < runs; ++run) {
+ UBiDiLevel level = 0;
+ bidi_line.GetLogicalRun(run_start, &run_end, &level);
+ DCHECK(run_end > run_start);
+ std::wstring fragment = text.substr(run_start, run_end - run_start);
+
+ // A flag that tells us whether we found LTR text inside RTL text.
+ bool ltr_inside_rtl_text =
+ ((level & 1) == UBIDI_LTR) && text_direction_is_rtl;
+
+ // Draw text chunk contained in |fragment|. |position| is relative to the
+ // top left corner of the label we draw inside, even when drawing RTL.
+ DrawTextStartingFrom(canvas, label, fragment, position, bounds, font,
+ text_direction_is_rtl, ltr_inside_rtl_text);
+
+ run_start = run_end; // Advance over what we just drew.
+ }
+
+ // If the caller is interested in placing a link after this text blurb, we
+ // figure out here where to place it.
+ if (link && rect) {
+ gfx::Size sz = link->GetPreferredSize();
+ gfx::Insets insets = link->GetInsets();
+ WrapIfWordDoesntFit(sz.width(), font.height(), position, bounds);
+ int x = position->width();
+ int y = position->height();
+
+ // Links have a border to allow them to be focused.
+ y -= insets.top();
+
+ *rect = gfx::Rect(x, y, sz.width(), sz.height());
+
+ // Go from relative pixel coordinates (within the label we are drawing
+ // on) to absolute pixel coordinates (relative to the top left corner of
+ // the dialog content).
+ rect->Offset(bounds.x(), bounds.y());
+ // And leave some space to draw the link in.
+ position->Enlarge(sz.width(), 0);
+ }
+}
+
+void DrawTextStartingFrom(gfx::Canvas* canvas,
+ views::Label* label,
+ const std::wstring& text,
+ gfx::Size* position,
+ const gfx::Rect& bounds,
+ const gfx::Font& font,
+ bool text_direction_is_rtl,
+ bool ltr_within_rtl) {
+#if defined(OS_WIN)
+ const SkColor text_color = color_utils::GetSysSkColor(COLOR_WINDOWTEXT);
+#else
+ // TODO(beng): source from theme provider.
+ const SkColor text_color = SK_ColorBLACK;
+#endif
+
+ // Iterate through line breaking opportunities (which in English would be
+ // spaces and such). This tells us where to wrap.
+ WordIterator iter(text, WordIterator::BREAK_LINE);
+ if (!iter.Init())
+ return;
+
+ int flags = (text_direction_is_rtl ? gfx::Canvas::TEXT_ALIGN_RIGHT :
+ gfx::Canvas::TEXT_ALIGN_LEFT);
+ flags |= gfx::Canvas::MULTI_LINE | gfx::Canvas::HIDE_PREFIX;
+
+ // Iterate over each word in the text, or put in a more locale-neutral way:
+ // iterate to the next line breaking opportunity.
+ while (iter.Advance()) {
+ // Get the word and figure out the dimensions.
+ std::wstring word;
+ if (!ltr_within_rtl)
+ word = iter.GetWord(); // Get the next word.
+ else
+ word = text; // Draw the whole text at once.
+
+ int w = font.GetStringWidth(word), h = font.height();
+ canvas->SizeStringInt(word, font, &w, &h, flags);
+
+ // If we exceed the boundaries, we need to wrap.
+ WrapIfWordDoesntFit(w, font.height(), position, bounds);
+
+ int x = label->MirroredXCoordinateInsideView(position->width()) +
+ bounds.x();
+ if (text_direction_is_rtl) {
+ x -= w;
+ // When drawing LTR strings inside RTL text we need to make sure we
+ // draw the trailing space (if one exists after the LTR text) to the
+ // left of the LTR string.
+ if (ltr_within_rtl && word[word.size() - 1] == L' ') {
+ int space_w = font.GetStringWidth(L" "), space_h = font.height();
+ canvas->SizeStringInt(L" ", font, &space_w, &space_h, flags);
+ x += space_w;
+ }
+ }
+ int y = position->height() + bounds.y();
+
+ // Draw the text on the screen (mirrored, if RTL run).
+ canvas->DrawStringInt(word, font, text_color, x, y, w, font.height(),
+ flags);
+
+ if (word.size() > 0 && word[word.size() - 1] == L'\x0a') {
+ // When we come across '\n', we move to the beginning of the next line.
+ position->set_width(0);
+ position->Enlarge(0, font.height());
+ } else {
+ // Otherwise, we advance position to the next word.
+ position->Enlarge(w, 0);
+ }
+
+ if (ltr_within_rtl)
+ break; // LTR within RTL is drawn as one unit, so we are done.
+ }
+}
+
+void WrapIfWordDoesntFit(int word_width,
+ int font_height,
+ gfx::Size* position,
+ const gfx::Rect& bounds) {
+ if (position->width() + word_width > bounds.right()) {
+ position->set_width(0);
+ position->Enlarge(0, font_height);
+ }
+}
+
+} // namespace view_text_utils
diff --git a/views/view_text_utils.h b/views/view_text_utils.h
new file mode 100644
index 0000000..5e292b9
--- /dev/null
+++ b/views/view_text_utils.h
@@ -0,0 +1,75 @@
+// Copyright (c) 2010 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.
+//
+// This file defines utility functions for working with text in views.
+
+#ifndef VIEWS_VIEW_TEXT_UTILS_H_
+#define VIEWS_VIEW_TEXT_UTILS_H_
+
+#include <string>
+
+#include "gfx/rect.h"
+#include "gfx/font.h"
+
+namespace gfx {
+class Canvas;
+class Size;
+}
+
+namespace views {
+class Label;
+class Link;
+}
+
+namespace view_text_utils {
+
+// Draws a string onto the canvas (wrapping if needed) while also keeping
+// track of where it ends so we can position a URL after the text. The
+// parameter |bounds| represents the boundary we have to work with, |position|
+// specifies where to draw the string (relative to the top left corner of the
+// |bounds| rectangle and |font| specifies the font to use when drawing. When
+// the function returns, the parameter |rect| contains where to draw the URL
+// (to the right of where we just drew the text) and |position| is updated to
+// reflect where to draw the next string after the URL. |label| is a dummy
+// label with the correct width and origin for the text to be written; it's
+// used so that the x position can be correctly mirrored in RTL languages.
+// |text_direction_is_rtl| is true if an RTL language is being used.
+// NOTE: The reason why we need this function is because while Skia knows how
+// to wrap text appropriately, it doesn't tell us where it drew the last
+// character, which we need to position the URLs within the text.
+void DrawTextAndPositionUrl(gfx::Canvas* canvas,
+ views::Label* label,
+ const std::wstring& text,
+ views::Link* link,
+ gfx::Rect* rect,
+ gfx::Size* position,
+ bool text_direction_is_rtl,
+ const gfx::Rect& bounds,
+ const gfx::Font& font);
+
+// A helper function for DrawTextAndPositionUrl, which simply draws the text
+// from a certain starting point |position| and wraps within bounds.
+// |word_for_word| specifies whether to draw the text word for word or whether
+// to treat the text as one blurb (similar to the way URL's are treated inside
+// RTL text. For details on the other parameters, see DrawTextAndPositionUrl.
+void DrawTextStartingFrom(gfx::Canvas* canvas,
+ views::Label* label,
+ const std::wstring& text,
+ gfx::Size* position,
+ const gfx::Rect& bounds,
+ const gfx::Font& font,
+ bool text_direction_is_rtl,
+ bool word_for_word);
+
+// A simply utility function that calculates whether a word of width
+// |word_width| fits at position |position| within the |bounds| rectangle. If
+// not, |position| is updated to wrap to the beginning of the next line.
+void WrapIfWordDoesntFit(int word_width,
+ int font_height,
+ gfx::Size* position,
+ const gfx::Rect& bounds);
+
+} // namespace view_text_utils
+
+#endif // CHROME_BROWSER_VIEWS_VIEW_TEXT_UTILS_H_
diff --git a/views/views.gyp b/views/views.gyp
index bde275f..6743959 100644
--- a/views/views.gyp
+++ b/views/views.gyp
@@ -250,6 +250,8 @@
'view.h',
'view_constants.cc',
'view_constants.h',
+ 'view_text_utils.cc',
+ 'view_text_utils.h',
'view_gtk.cc',
'view_win.cc',
'widget/aero_tooltip_manager.cc',