[email protected] | d849078c2 | 2010-04-05 18:57:01 | [diff] [blame] | 1 | // Copyright (c) 2010 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. |
| 4 | |
| 5 | #include "views/view_text_utils.h" |
| 6 | |
| 7 | #include "app/bidi_line_iterator.h" |
[email protected] | d849078c2 | 2010-04-05 18:57:01 | [diff] [blame] | 8 | #include "base/i18n/word_iterator.h" |
| 9 | #include "base/logging.h" |
[email protected] | e5366896 | 2010-06-23 15:35:25 | [diff] [blame] | 10 | #include "base/utf_string_conversions.h" |
[email protected] | 74db48eb | 2010-06-25 16:33:23 | [diff] [blame] | 11 | #include "gfx/canvas_skia.h" |
[email protected] | d849078c2 | 2010-04-05 18:57:01 | [diff] [blame] | 12 | #include "gfx/color_utils.h" |
| 13 | #include "gfx/size.h" |
| 14 | #include "views/controls/label.h" |
| 15 | #include "views/controls/link.h" |
| 16 | |
| 17 | namespace view_text_utils { |
| 18 | |
| 19 | void DrawTextAndPositionUrl(gfx::Canvas* canvas, |
| 20 | views::Label* label, |
| 21 | const std::wstring& text, |
| 22 | views::Link* link, |
| 23 | gfx::Rect* rect, |
| 24 | gfx::Size* position, |
| 25 | bool text_direction_is_rtl, |
| 26 | const gfx::Rect& bounds, |
| 27 | const gfx::Font& font) { |
| 28 | DCHECK(canvas && position); |
| 29 | |
| 30 | // The |text| parameter is potentially a mix of LTR and RTL "runs," where |
| 31 | // a run is a sequence of words that share the same directionality. We |
| 32 | // initialize a bidirectional ICU line iterator and split the text into |
| 33 | // runs that are either strictly LTR or strictly RTL, with no mix. |
| 34 | BiDiLineIterator bidi_line; |
| 35 | if (!bidi_line.Open(text.c_str(), true, false)) |
| 36 | return; |
| 37 | |
| 38 | // Iterate over each run and draw it. |
| 39 | int run_start = 0; |
| 40 | int run_end = 0; |
| 41 | const int runs = bidi_line.CountRuns(); |
| 42 | for (int run = 0; run < runs; ++run) { |
| 43 | UBiDiLevel level = 0; |
| 44 | bidi_line.GetLogicalRun(run_start, &run_end, &level); |
| 45 | DCHECK(run_end > run_start); |
| 46 | std::wstring fragment = text.substr(run_start, run_end - run_start); |
| 47 | |
| 48 | // A flag that tells us whether we found LTR text inside RTL text. |
| 49 | bool ltr_inside_rtl_text = |
| 50 | ((level & 1) == UBIDI_LTR) && text_direction_is_rtl; |
| 51 | |
| 52 | // Draw text chunk contained in |fragment|. |position| is relative to the |
| 53 | // top left corner of the label we draw inside, even when drawing RTL. |
| 54 | DrawTextStartingFrom(canvas, label, fragment, position, bounds, font, |
| 55 | text_direction_is_rtl, ltr_inside_rtl_text); |
| 56 | |
| 57 | run_start = run_end; // Advance over what we just drew. |
| 58 | } |
| 59 | |
| 60 | // If the caller is interested in placing a link after this text blurb, we |
| 61 | // figure out here where to place it. |
| 62 | if (link && rect) { |
| 63 | gfx::Size sz = link->GetPreferredSize(); |
| 64 | gfx::Insets insets = link->GetInsets(); |
[email protected] | c6ac841 | 2010-08-13 16:43:03 | [diff] [blame] | 65 | WrapIfWordDoesntFit(sz.width(), font.GetHeight(), position, bounds); |
[email protected] | d849078c2 | 2010-04-05 18:57:01 | [diff] [blame] | 66 | int x = position->width(); |
| 67 | int y = position->height(); |
| 68 | |
| 69 | // Links have a border to allow them to be focused. |
| 70 | y -= insets.top(); |
| 71 | |
| 72 | *rect = gfx::Rect(x, y, sz.width(), sz.height()); |
| 73 | |
| 74 | // Go from relative pixel coordinates (within the label we are drawing |
| 75 | // on) to absolute pixel coordinates (relative to the top left corner of |
| 76 | // the dialog content). |
| 77 | rect->Offset(bounds.x(), bounds.y()); |
| 78 | // And leave some space to draw the link in. |
| 79 | position->Enlarge(sz.width(), 0); |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | void DrawTextStartingFrom(gfx::Canvas* canvas, |
| 84 | views::Label* label, |
| 85 | const std::wstring& text, |
| 86 | gfx::Size* position, |
| 87 | const gfx::Rect& bounds, |
| 88 | const gfx::Font& font, |
| 89 | bool text_direction_is_rtl, |
| 90 | bool ltr_within_rtl) { |
| 91 | #if defined(OS_WIN) |
| 92 | const SkColor text_color = color_utils::GetSysSkColor(COLOR_WINDOWTEXT); |
| 93 | #else |
| 94 | // TODO(beng): source from theme provider. |
| 95 | const SkColor text_color = SK_ColorBLACK; |
| 96 | #endif |
| 97 | |
| 98 | // Iterate through line breaking opportunities (which in English would be |
| 99 | // spaces and such). This tells us where to wrap. |
[email protected] | e5366896 | 2010-06-23 15:35:25 | [diff] [blame] | 100 | string16 text16(WideToUTF16(text)); |
| 101 | WordIterator iter(&text16, WordIterator::BREAK_LINE); |
[email protected] | d849078c2 | 2010-04-05 18:57:01 | [diff] [blame] | 102 | if (!iter.Init()) |
| 103 | return; |
| 104 | |
| 105 | int flags = (text_direction_is_rtl ? gfx::Canvas::TEXT_ALIGN_RIGHT : |
| 106 | gfx::Canvas::TEXT_ALIGN_LEFT); |
| 107 | flags |= gfx::Canvas::MULTI_LINE | gfx::Canvas::HIDE_PREFIX; |
| 108 | |
| 109 | // Iterate over each word in the text, or put in a more locale-neutral way: |
| 110 | // iterate to the next line breaking opportunity. |
| 111 | while (iter.Advance()) { |
| 112 | // Get the word and figure out the dimensions. |
| 113 | std::wstring word; |
| 114 | if (!ltr_within_rtl) |
[email protected] | e5366896 | 2010-06-23 15:35:25 | [diff] [blame] | 115 | word = UTF16ToWide(iter.GetWord()); // Get the next word. |
[email protected] | d849078c2 | 2010-04-05 18:57:01 | [diff] [blame] | 116 | else |
| 117 | word = text; // Draw the whole text at once. |
| 118 | |
[email protected] | c6ac841 | 2010-08-13 16:43:03 | [diff] [blame] | 119 | int w = font.GetStringWidth(word), h = font.GetHeight(); |
[email protected] | 36f1e27 | 2010-11-22 18:15:25 | [diff] [blame] | 120 | gfx::CanvasSkia::SizeStringInt(WideToUTF16Hack(word), font, &w, &h, flags); |
[email protected] | d849078c2 | 2010-04-05 18:57:01 | [diff] [blame] | 121 | |
| 122 | // If we exceed the boundaries, we need to wrap. |
[email protected] | c6ac841 | 2010-08-13 16:43:03 | [diff] [blame] | 123 | WrapIfWordDoesntFit(w, font.GetHeight(), position, bounds); |
[email protected] | d849078c2 | 2010-04-05 18:57:01 | [diff] [blame] | 124 | |
| 125 | int x = label->MirroredXCoordinateInsideView(position->width()) + |
| 126 | bounds.x(); |
| 127 | if (text_direction_is_rtl) { |
| 128 | x -= w; |
| 129 | // When drawing LTR strings inside RTL text we need to make sure we |
| 130 | // draw the trailing space (if one exists after the LTR text) to the |
| 131 | // left of the LTR string. |
| 132 | if (ltr_within_rtl && word[word.size() - 1] == L' ') { |
[email protected] | c6ac841 | 2010-08-13 16:43:03 | [diff] [blame] | 133 | int space_w = font.GetStringWidth(L" "), space_h = font.GetHeight(); |
[email protected] | 36f1e27 | 2010-11-22 18:15:25 | [diff] [blame] | 134 | gfx::CanvasSkia::SizeStringInt(UTF8ToUTF16(" "), font, &space_w, |
| 135 | &space_h, flags); |
[email protected] | d849078c2 | 2010-04-05 18:57:01 | [diff] [blame] | 136 | x += space_w; |
| 137 | } |
| 138 | } |
| 139 | int y = position->height() + bounds.y(); |
| 140 | |
| 141 | // Draw the text on the screen (mirrored, if RTL run). |
[email protected] | c6ac841 | 2010-08-13 16:43:03 | [diff] [blame] | 142 | canvas->DrawStringInt(word, font, text_color, x, y, w, font.GetHeight(), |
[email protected] | d849078c2 | 2010-04-05 18:57:01 | [diff] [blame] | 143 | flags); |
| 144 | |
| 145 | if (word.size() > 0 && word[word.size() - 1] == L'\x0a') { |
| 146 | // When we come across '\n', we move to the beginning of the next line. |
| 147 | position->set_width(0); |
[email protected] | c6ac841 | 2010-08-13 16:43:03 | [diff] [blame] | 148 | position->Enlarge(0, font.GetHeight()); |
[email protected] | d849078c2 | 2010-04-05 18:57:01 | [diff] [blame] | 149 | } else { |
| 150 | // Otherwise, we advance position to the next word. |
| 151 | position->Enlarge(w, 0); |
| 152 | } |
| 153 | |
| 154 | if (ltr_within_rtl) |
| 155 | break; // LTR within RTL is drawn as one unit, so we are done. |
| 156 | } |
| 157 | } |
| 158 | |
| 159 | void WrapIfWordDoesntFit(int word_width, |
| 160 | int font_height, |
| 161 | gfx::Size* position, |
| 162 | const gfx::Rect& bounds) { |
| 163 | if (position->width() + word_width > bounds.right()) { |
| 164 | position->set_width(0); |
| 165 | position->Enlarge(0, font_height); |
| 166 | } |
| 167 | } |
| 168 | |
| 169 | } // namespace view_text_utils |