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/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" |
(...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
103 return; | 103 return; |
104 | 104 |
105 int flags = (text_direction_is_rtl ? gfx::Canvas::TEXT_ALIGN_RIGHT : | 105 int flags = (text_direction_is_rtl ? gfx::Canvas::TEXT_ALIGN_RIGHT : |
106 gfx::Canvas::TEXT_ALIGN_LEFT); | 106 gfx::Canvas::TEXT_ALIGN_LEFT); |
107 flags |= gfx::Canvas::MULTI_LINE | gfx::Canvas::HIDE_PREFIX; | 107 flags |= gfx::Canvas::MULTI_LINE | gfx::Canvas::HIDE_PREFIX; |
108 | 108 |
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 string16 word; |
114 if (!ltr_within_rtl) | 114 if (!ltr_within_rtl) |
115 word = UTF16ToWide(iter.GetString()); // Get the next word. | 115 word = iter.GetString(); // Get the next word. |
116 else | 116 else |
117 word = text; // Draw the whole text at once. | 117 word = text16; // Draw the whole text at once. |
118 | 118 |
119 int w = font.GetStringWidth(WideToUTF16Hack(word)), h = font.GetHeight(); | 119 int w = font.GetStringWidth(word), h = font.GetHeight(); |
120 gfx::CanvasSkia::SizeStringInt(WideToUTF16Hack(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.GetHeight(), 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] == ' ') { |
133 int space_w = font.GetStringWidth(ASCIIToUTF16(" ")); | 133 int space_w = font.GetStringWidth(ASCIIToUTF16(" ")); |
134 int space_h = font.GetHeight(); | 134 int space_h = font.GetHeight(); |
135 gfx::CanvasSkia::SizeStringInt(ASCIIToUTF16(" "), font, &space_w, | 135 gfx::CanvasSkia::SizeStringInt(ASCIIToUTF16(" "), font, &space_w, |
136 &space_h, flags); | 136 &space_h, flags); |
137 x += space_w; | 137 x += space_w; |
138 } | 138 } |
139 } | 139 } |
140 int y = position->height() + bounds.y(); | 140 int y = position->height() + bounds.y(); |
141 | 141 |
142 // Draw the text on the screen (mirrored, if RTL run). | 142 // Draw the text on the screen (mirrored, if RTL run). |
143 canvas->DrawStringInt(word, font, text_color, x, y, w, font.GetHeight(), | 143 canvas->DrawStringInt(word, font, text_color, x, y, w, font.GetHeight(), |
144 flags); | 144 flags); |
145 | 145 |
146 if (word.size() > 0 && word[word.size() - 1] == L'\x0a') { | 146 if (word.size() > 0 && word[word.size() - 1] == '\x0a') { |
147 // When we come across '\n', we move to the beginning of the next line. | 147 // When we come across '\n', we move to the beginning of the next line. |
148 position->set_width(0); | 148 position->set_width(0); |
149 position->Enlarge(0, font.GetHeight()); | 149 position->Enlarge(0, font.GetHeight()); |
150 } else { | 150 } else { |
151 // Otherwise, we advance position to the next word. | 151 // Otherwise, we advance position to the next word. |
152 position->Enlarge(w, 0); | 152 position->Enlarge(w, 0); |
153 } | 153 } |
154 | 154 |
155 if (ltr_within_rtl) | 155 if (ltr_within_rtl) |
156 break; // LTR within RTL is drawn as one unit, so we are done. | 156 break; // LTR within RTL is drawn as one unit, so we are done. |
157 } | 157 } |
158 } | 158 } |
159 | 159 |
160 void WrapIfWordDoesntFit(int word_width, | 160 void WrapIfWordDoesntFit(int word_width, |
161 int font_height, | 161 int font_height, |
162 gfx::Size* position, | 162 gfx::Size* position, |
163 const gfx::Rect& bounds) { | 163 const gfx::Rect& bounds) { |
164 if (position->width() + word_width > bounds.right()) { | 164 if (position->width() + word_width > bounds.right()) { |
165 position->set_width(0); | 165 position->set_width(0); |
166 position->Enlarge(0, font_height); | 166 position->Enlarge(0, font_height); |
167 } | 167 } |
168 } | 168 } |
169 | 169 |
170 } // namespace view_text_utils | 170 } // namespace view_text_utils |
OLD | NEW |