OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "ui/views/view_text_utils.h" | 5 #include "ui/views/view_text_utils.h" |
6 | 6 |
7 #include "base/i18n/bidi_line_iterator.h" | 7 #include "base/i18n/bidi_line_iterator.h" |
8 #include "base/i18n/break_iterator.h" | 8 #include "base/i18n/break_iterator.h" |
9 #include "base/logging.h" | 9 #include "base/logging.h" |
10 #include "base/utf_string_conversions.h" | 10 #include "base/utf_string_conversions.h" |
11 #include "ui/gfx/canvas.h" | 11 #include "ui/gfx/canvas.h" |
12 #include "ui/gfx/font.h" | 12 #include "ui/gfx/font.h" |
13 #include "ui/gfx/rect.h" | 13 #include "ui/gfx/rect.h" |
14 #include "ui/gfx/size.h" | 14 #include "ui/gfx/size.h" |
15 #include "ui/views/controls/label.h" | 15 #include "ui/views/controls/label.h" |
16 #include "ui/views/controls/link.h" | 16 #include "ui/views/controls/link.h" |
17 | 17 |
18 namespace view_text_utils { | 18 namespace view_text_utils { |
19 | 19 |
20 void DrawTextAndPositionUrl(gfx::Canvas* canvas, | 20 void DrawTextAndPositionUrl(gfx::Canvas* canvas, |
sky
2012/11/13 17:47:01
A method named DrawText should get a non-null canv
bartfab (slow)
2012/11/13 17:51:25
Because I need a way to measure the text size with
| |
21 views::Label* label, | 21 views::Label* label, |
22 const string16& text, | 22 const string16& text, |
23 views::Link* link, | 23 views::Link* link, |
24 gfx::Rect* rect, | 24 gfx::Rect* rect, |
25 gfx::Size* position, | 25 gfx::Size* position, |
26 bool text_direction_is_rtl, | 26 bool text_direction_is_rtl, |
27 const gfx::Rect& bounds, | 27 const gfx::Rect& bounds, |
28 const gfx::Font& font) { | 28 const gfx::Font& font) { |
29 DCHECK(canvas && position); | 29 DCHECK(position); |
30 | 30 |
31 // The |text| parameter is potentially a mix of LTR and RTL "runs," where | 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 | 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 | 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. | 34 // runs that are either strictly LTR or strictly RTL, with no mix. |
35 base::i18n::BiDiLineIterator bidi_line; | 35 base::i18n::BiDiLineIterator bidi_line; |
36 if (!bidi_line.Open(text, true, false)) | 36 if (!bidi_line.Open(text, true, false)) |
37 return; | 37 return; |
38 | 38 |
39 // Iterate over each run and draw it. | 39 // Iterate over each run and draw it. |
(...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
121 x -= w; | 121 x -= w; |
122 // When drawing LTR strings inside RTL text we need to make sure we | 122 // When drawing LTR strings inside RTL text we need to make sure we |
123 // draw the trailing space (if one exists after the LTR text) to the | 123 // draw the trailing space (if one exists after the LTR text) to the |
124 // left of the LTR string. | 124 // left of the LTR string. |
125 if (ltr_within_rtl && word[word.size() - 1] == ' ') | 125 if (ltr_within_rtl && word[word.size() - 1] == ' ') |
126 x += gfx::Canvas::GetStringWidth(ASCIIToUTF16(" "), font); | 126 x += gfx::Canvas::GetStringWidth(ASCIIToUTF16(" "), font); |
127 } | 127 } |
128 int y = position->height() + bounds.y(); | 128 int y = position->height() + bounds.y(); |
129 | 129 |
130 // Draw the text on the screen (mirrored, if RTL run). | 130 // Draw the text on the screen (mirrored, if RTL run). |
131 canvas->DrawStringInt(word, font, label->enabled_color(), x, y, w, | 131 if (canvas) { |
132 font.GetHeight(), flags); | 132 canvas->DrawStringInt(word, font, label->enabled_color(), x, y, w, |
133 font.GetHeight(), flags); | |
134 } | |
133 | 135 |
134 if (!word.empty() && word[word.size() - 1] == '\x0a') { | 136 if (!word.empty() && word[word.size() - 1] == '\x0a') { |
135 // When we come across '\n', we move to the beginning of the next line. | 137 // When we come across '\n', we move to the beginning of the next line. |
136 position->set_width(0); | 138 position->set_width(0); |
137 position->Enlarge(0, font.GetHeight()); | 139 position->Enlarge(0, font.GetHeight()); |
138 } else { | 140 } else { |
139 // Otherwise, we advance position to the next word. | 141 // Otherwise, we advance position to the next word. |
140 position->Enlarge(w, 0); | 142 position->Enlarge(w, 0); |
141 } | 143 } |
142 | 144 |
143 if (ltr_within_rtl) | 145 if (ltr_within_rtl) |
144 break; // LTR within RTL is drawn as one unit, so we are done. | 146 break; // LTR within RTL is drawn as one unit, so we are done. |
145 } | 147 } |
146 } | 148 } |
147 | 149 |
148 void WrapIfWordDoesntFit(int word_width, | 150 void WrapIfWordDoesntFit(int word_width, |
149 int font_height, | 151 int font_height, |
150 gfx::Size* position, | 152 gfx::Size* position, |
151 const gfx::Rect& bounds) { | 153 const gfx::Rect& bounds) { |
152 if (position->width() + word_width > bounds.right()) { | 154 if (position->width() + word_width > bounds.right()) { |
153 position->set_width(0); | 155 position->set_width(0); |
154 position->Enlarge(0, font_height); | 156 position->Enlarge(0, font_height); |
155 } | 157 } |
156 } | 158 } |
157 | 159 |
158 } // namespace view_text_utils | 160 } // namespace view_text_utils |
OLD | NEW |