summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorbartfab@chromium.org <bartfab@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2012-11-05 20:22:12 +0000
committerbartfab@chromium.org <bartfab@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2012-11-05 20:22:12 +0000
commit71b808a3a1e1ae7ce37bd60500d81eb9cdd48410 (patch)
tree880e39d948434b53d394a5eb0e0a58524444ccb0
parentf57ac3ab2913dc2fced453cb74997de6128c9037 (diff)
downloadchromium_src-71b808a3a1e1ae7ce37bd60500d81eb9cdd48410.zip
chromium_src-71b808a3a1e1ae7ce37bd60500d81eb9cdd48410.tar.gz
chromium_src-71b808a3a1e1ae7ce37bd60500d81eb9cdd48410.tar.bz2
Revert 165832 - Nix unused views_text_utils.[cc|h].
As discussed with msw@, reverting this change because the files will actually be needed for issue 152928. The files appear unused for the last 7 months. (except for cursory routine updates) BUG=none TEST=No build problems, no complaints. R=sky@chromium.org,asvitkine@chromium.org Review URL: https://chromiumcodereview.appspot.com/11368065 TBR=msw@chromium.org Review URL: https://codereview.chromium.org/11361106 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@166015 0039d316-1c4b-4281-b951-d872f2087c98
-rw-r--r--ui/views/view_text_utils.cc158
-rw-r--r--ui/views/view_text_utils.h75
-rw-r--r--ui/views/views.gyp2
3 files changed, 235 insertions, 0 deletions
diff --git a/ui/views/view_text_utils.cc b/ui/views/view_text_utils.cc
new file mode 100644
index 0000000..0ea27d7
--- /dev/null
+++ b/ui/views/view_text_utils.cc
@@ -0,0 +1,158 @@
+// Copyright (c) 2012 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/views/view_text_utils.h"
+
+#include "base/i18n/bidi_line_iterator.h"
+#include "base/i18n/break_iterator.h"
+#include "base/logging.h"
+#include "base/utf_string_conversions.h"
+#include "ui/gfx/canvas.h"
+#include "ui/gfx/font.h"
+#include "ui/gfx/rect.h"
+#include "ui/gfx/size.h"
+#include "ui/views/controls/label.h"
+#include "ui/views/controls/link.h"
+
+namespace view_text_utils {
+
+void DrawTextAndPositionUrl(gfx::Canvas* canvas,
+ views::Label* label,
+ const string16& 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.
+ base::i18n::BiDiLineIterator bidi_line;
+ if (!bidi_line.Open(text, 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);
+ string16 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.GetHeight(), 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 string16& text,
+ gfx::Size* position,
+ const gfx::Rect& bounds,
+ const gfx::Font& font,
+ bool text_direction_is_rtl,
+ bool ltr_within_rtl) {
+ // Iterate through line breaking opportunities (which in English would be
+ // spaces and such). This tells us where to wrap.
+ base::i18n::BreakIterator iter(text,
+ base::i18n::BreakIterator::BREAK_SPACE);
+ if (!iter.Init())
+ return;
+
+ int flags = (text_direction_is_rtl ? gfx::Canvas::TEXT_ALIGN_RIGHT :
+ gfx::Canvas::TEXT_ALIGN_LEFT);
+ flags |= gfx::Canvas::HIDE_PREFIX | gfx::Canvas::NO_ELLIPSIS;
+
+ // 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.
+ string16 word;
+ if (!ltr_within_rtl)
+ word = iter.GetString(); // Get the next word.
+ else
+ word = text; // Draw the whole text at once.
+
+ int w = 0, h = 0;
+ gfx::Canvas::SizeStringInt(word, font, &w, &h, flags);
+
+ // If we exceed the boundaries, we need to wrap.
+ WrapIfWordDoesntFit(w, font.GetHeight(), position, bounds);
+
+ int x = label->GetMirroredXInView(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] == ' ')
+ x += gfx::Canvas::GetStringWidth(ASCIIToUTF16(" "), font);
+ }
+ int y = position->height() + bounds.y();
+
+ // Draw the text on the screen (mirrored, if RTL run).
+ canvas->DrawStringInt(word, font, label->enabled_color(), x, y, w,
+ font.GetHeight(), flags);
+
+ if (!word.empty() && word[word.size() - 1] == '\x0a') {
+ // When we come across '\n', we move to the beginning of the next line.
+ position->set_width(0);
+ position->Enlarge(0, font.GetHeight());
+ } 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/ui/views/view_text_utils.h b/ui/views/view_text_utils.h
new file mode 100644
index 0000000..0d7f1b0
--- /dev/null
+++ b/ui/views/view_text_utils.h
@@ -0,0 +1,75 @@
+// 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_VIEWS_VIEW_TEXT_UTILS_H_
+#define UI_VIEWS_VIEW_TEXT_UTILS_H_
+
+#include "base/string16.h"
+#include "ui/views/views_export.h"
+
+namespace gfx {
+class Canvas;
+class Font;
+class Rect;
+class Size;
+}
+
+namespace views {
+class Label;
+class Link;
+}
+
+// This file defines utility functions for working with text in views.
+
+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.
+VIEWS_EXPORT void DrawTextAndPositionUrl(gfx::Canvas* canvas,
+ views::Label* label,
+ const string16& 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 string16& 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 // UI_VIEWS_VIEW_TEXT_UTILS_H_
diff --git a/ui/views/views.gyp b/ui/views/views.gyp
index 6b5b0d7..19124c6 100644
--- a/ui/views/views.gyp
+++ b/ui/views/views.gyp
@@ -309,6 +309,8 @@
'view_model.h',
'view_model_utils.cc',
'view_model_utils.h',
+ 'view_text_utils.cc',
+ 'view_text_utils.h',
'view_win.cc',
'views_delegate.h',
'widget/aero_tooltip_manager.cc',