OLD | NEW |
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 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 | 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 "views/view_text_utils.h" | 5 #include "views/view_text_utils.h" |
6 | 6 |
7 #include "app/bidi_line_iterator.h" | 7 #include "app/bidi_line_iterator.h" |
8 #include "base/i18n/word_iterator.h" | 8 #include "base/i18n/word_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" |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
55 text_direction_is_rtl, ltr_inside_rtl_text); | 55 text_direction_is_rtl, ltr_inside_rtl_text); |
56 | 56 |
57 run_start = run_end; // Advance over what we just drew. | 57 run_start = run_end; // Advance over what we just drew. |
58 } | 58 } |
59 | 59 |
60 // If the caller is interested in placing a link after this text blurb, we | 60 // If the caller is interested in placing a link after this text blurb, we |
61 // figure out here where to place it. | 61 // figure out here where to place it. |
62 if (link && rect) { | 62 if (link && rect) { |
63 gfx::Size sz = link->GetPreferredSize(); | 63 gfx::Size sz = link->GetPreferredSize(); |
64 gfx::Insets insets = link->GetInsets(); | 64 gfx::Insets insets = link->GetInsets(); |
65 WrapIfWordDoesntFit(sz.width(), font.height(), position, bounds); | 65 WrapIfWordDoesntFit(sz.width(), font.GetHeight(), position, bounds); |
66 int x = position->width(); | 66 int x = position->width(); |
67 int y = position->height(); | 67 int y = position->height(); |
68 | 68 |
69 // Links have a border to allow them to be focused. | 69 // Links have a border to allow them to be focused. |
70 y -= insets.top(); | 70 y -= insets.top(); |
71 | 71 |
72 *rect = gfx::Rect(x, y, sz.width(), sz.height()); | 72 *rect = gfx::Rect(x, y, sz.width(), sz.height()); |
73 | 73 |
74 // Go from relative pixel coordinates (within the label we are drawing | 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 | 75 // on) to absolute pixel coordinates (relative to the top left corner of |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
109 // Iterate over each word in the text, or put in a more locale-neutral way: | 109 // Iterate over each word in the text, or put in a more locale-neutral way: |
110 // iterate to the next line breaking opportunity. | 110 // iterate to the next line breaking opportunity. |
111 while (iter.Advance()) { | 111 while (iter.Advance()) { |
112 // Get the word and figure out the dimensions. | 112 // Get the word and figure out the dimensions. |
113 std::wstring word; | 113 std::wstring word; |
114 if (!ltr_within_rtl) | 114 if (!ltr_within_rtl) |
115 word = UTF16ToWide(iter.GetWord()); // Get the next word. | 115 word = UTF16ToWide(iter.GetWord()); // Get the next word. |
116 else | 116 else |
117 word = text; // Draw the whole text at once. | 117 word = text; // Draw the whole text at once. |
118 | 118 |
119 int w = font.GetStringWidth(word), h = font.height(); | 119 int w = font.GetStringWidth(word), h = font.GetHeight(); |
120 gfx::CanvasSkia::SizeStringInt(word, font, &w, &h, flags); | 120 gfx::CanvasSkia::SizeStringInt(word, font, &w, &h, flags); |
121 | 121 |
122 // If we exceed the boundaries, we need to wrap. | 122 // If we exceed the boundaries, we need to wrap. |
123 WrapIfWordDoesntFit(w, font.height(), position, bounds); | 123 WrapIfWordDoesntFit(w, font.GetHeight(), position, bounds); |
124 | 124 |
125 int x = label->MirroredXCoordinateInsideView(position->width()) + | 125 int x = label->MirroredXCoordinateInsideView(position->width()) + |
126 bounds.x(); | 126 bounds.x(); |
127 if (text_direction_is_rtl) { | 127 if (text_direction_is_rtl) { |
128 x -= w; | 128 x -= w; |
129 // When drawing LTR strings inside RTL text we need to make sure we | 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 | 130 // draw the trailing space (if one exists after the LTR text) to the |
131 // left of the LTR string. | 131 // left of the LTR string. |
132 if (ltr_within_rtl && word[word.size() - 1] == L' ') { | 132 if (ltr_within_rtl && word[word.size() - 1] == L' ') { |
133 int space_w = font.GetStringWidth(L" "), space_h = font.height(); | 133 int space_w = font.GetStringWidth(L" "), space_h = font.GetHeight(); |
134 gfx::CanvasSkia::SizeStringInt(L" ", font, &space_w, &space_h, flags); | 134 gfx::CanvasSkia::SizeStringInt(L" ", font, &space_w, &space_h, flags); |
135 x += space_w; | 135 x += space_w; |
136 } | 136 } |
137 } | 137 } |
138 int y = position->height() + bounds.y(); | 138 int y = position->height() + bounds.y(); |
139 | 139 |
140 // Draw the text on the screen (mirrored, if RTL run). | 140 // Draw the text on the screen (mirrored, if RTL run). |
141 canvas->DrawStringInt(word, font, text_color, x, y, w, font.height(), | 141 canvas->DrawStringInt(word, font, text_color, x, y, w, font.GetHeight(), |
142 flags); | 142 flags); |
143 | 143 |
144 if (word.size() > 0 && word[word.size() - 1] == L'\x0a') { | 144 if (word.size() > 0 && word[word.size() - 1] == L'\x0a') { |
145 // When we come across '\n', we move to the beginning of the next line. | 145 // When we come across '\n', we move to the beginning of the next line. |
146 position->set_width(0); | 146 position->set_width(0); |
147 position->Enlarge(0, font.height()); | 147 position->Enlarge(0, font.GetHeight()); |
148 } else { | 148 } else { |
149 // Otherwise, we advance position to the next word. | 149 // Otherwise, we advance position to the next word. |
150 position->Enlarge(w, 0); | 150 position->Enlarge(w, 0); |
151 } | 151 } |
152 | 152 |
153 if (ltr_within_rtl) | 153 if (ltr_within_rtl) |
154 break; // LTR within RTL is drawn as one unit, so we are done. | 154 break; // LTR within RTL is drawn as one unit, so we are done. |
155 } | 155 } |
156 } | 156 } |
157 | 157 |
158 void WrapIfWordDoesntFit(int word_width, | 158 void WrapIfWordDoesntFit(int word_width, |
159 int font_height, | 159 int font_height, |
160 gfx::Size* position, | 160 gfx::Size* position, |
161 const gfx::Rect& bounds) { | 161 const gfx::Rect& bounds) { |
162 if (position->width() + word_width > bounds.right()) { | 162 if (position->width() + word_width > bounds.right()) { |
163 position->set_width(0); | 163 position->set_width(0); |
164 position->Enlarge(0, font_height); | 164 position->Enlarge(0, font_height); |
165 } | 165 } |
166 } | 166 } |
167 | 167 |
168 } // namespace view_text_utils | 168 } // namespace view_text_utils |
OLD | NEW |