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