| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 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 | 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" |
| (...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 84 void DrawTextStartingFrom(gfx::Canvas* canvas, | 84 void DrawTextStartingFrom(gfx::Canvas* canvas, |
| 85 views::Label* label, | 85 views::Label* label, |
| 86 const string16& text, | 86 const string16& text, |
| 87 gfx::Size* position, | 87 gfx::Size* position, |
| 88 const gfx::Rect& bounds, | 88 const gfx::Rect& bounds, |
| 89 const gfx::Font& font, | 89 const gfx::Font& font, |
| 90 bool text_direction_is_rtl, | 90 bool text_direction_is_rtl, |
| 91 bool ltr_within_rtl) { | 91 bool ltr_within_rtl) { |
| 92 // Iterate through line breaking opportunities (which in English would be | 92 // Iterate through line breaking opportunities (which in English would be |
| 93 // spaces and such). This tells us where to wrap. | 93 // spaces and such). This tells us where to wrap. |
| 94 string16 text16(text); | 94 base::i18n::BreakIterator iter(text, |
| 95 base::i18n::BreakIterator iter(text16, | |
| 96 base::i18n::BreakIterator::BREAK_SPACE); | 95 base::i18n::BreakIterator::BREAK_SPACE); |
| 97 if (!iter.Init()) | 96 if (!iter.Init()) |
| 98 return; | 97 return; |
| 99 | 98 |
| 100 int flags = (text_direction_is_rtl ? gfx::Canvas::TEXT_ALIGN_RIGHT : | 99 int flags = (text_direction_is_rtl ? gfx::Canvas::TEXT_ALIGN_RIGHT : |
| 101 gfx::Canvas::TEXT_ALIGN_LEFT); | 100 gfx::Canvas::TEXT_ALIGN_LEFT); |
| 102 flags |= gfx::Canvas::MULTI_LINE | gfx::Canvas::HIDE_PREFIX; | 101 flags |= gfx::Canvas::MULTI_LINE | gfx::Canvas::HIDE_PREFIX | |
| 102 gfx::Canvas::NO_ELLIPSIS; |
| 103 | 103 |
| 104 // Iterate over each word in the text, or put in a more locale-neutral way: | 104 // Iterate over each word in the text, or put in a more locale-neutral way: |
| 105 // iterate to the next line breaking opportunity. | 105 // iterate to the next line breaking opportunity. |
| 106 while (iter.Advance()) { | 106 while (iter.Advance()) { |
| 107 // Get the word and figure out the dimensions. | 107 // Get the word and figure out the dimensions. |
| 108 string16 word; | 108 string16 word; |
| 109 if (!ltr_within_rtl) | 109 if (!ltr_within_rtl) |
| 110 word = iter.GetString(); // Get the next word. | 110 word = iter.GetString(); // Get the next word. |
| 111 else | 111 else |
| 112 word = text16; // Draw the whole text at once. | 112 word = text; // Draw the whole text at once. |
| 113 | 113 |
| 114 int w = font.GetStringWidth(word), h = font.GetHeight(); | 114 int w = 0, h = 0; |
| 115 gfx::CanvasSkia::SizeStringInt(word, font, &w, &h, flags); | 115 gfx::CanvasSkia::SizeStringInt(word, font, &w, &h, flags); |
| 116 | 116 |
| 117 // If we exceed the boundaries, we need to wrap. | 117 // If we exceed the boundaries, we need to wrap. |
| 118 WrapIfWordDoesntFit(w, font.GetHeight(), position, bounds); | 118 WrapIfWordDoesntFit(w, font.GetHeight(), position, bounds); |
| 119 | 119 |
| 120 int x = label->GetMirroredXInView(position->width()) + bounds.x(); | 120 int x = label->GetMirroredXInView(position->width()) + bounds.x(); |
| 121 if (text_direction_is_rtl) { | 121 if (text_direction_is_rtl) { |
| 122 x -= w; | 122 x -= w; |
| 123 // When drawing LTR strings inside RTL text we need to make sure we | 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 | 124 // draw the trailing space (if one exists after the LTR text) to the |
| 125 // left of the LTR string. | 125 // left of the LTR string. |
| 126 if (ltr_within_rtl && word[word.size() - 1] == ' ') { | 126 if (ltr_within_rtl && word[word.size() - 1] == ' ') |
| 127 int space_w = font.GetStringWidth(ASCIIToUTF16(" ")); | 127 x += gfx::CanvasSkia::GetStringWidth(ASCIIToUTF16(" "), font); |
| 128 int space_h = font.GetHeight(); | |
| 129 gfx::CanvasSkia::SizeStringInt(ASCIIToUTF16(" "), font, &space_w, | |
| 130 &space_h, flags); | |
| 131 x += space_w; | |
| 132 } | |
| 133 } | 128 } |
| 134 int y = position->height() + bounds.y(); | 129 int y = position->height() + bounds.y(); |
| 135 | 130 |
| 136 // Draw the text on the screen (mirrored, if RTL run). | 131 // Draw the text on the screen (mirrored, if RTL run). |
| 137 canvas->DrawStringInt(word, font, label->enabled_color(), x, y, w, | 132 canvas->DrawStringInt(word, font, label->enabled_color(), x, y, w, |
| 138 font.GetHeight(), flags); | 133 font.GetHeight(), flags); |
| 139 | 134 |
| 140 if (!word.empty() && word[word.size() - 1] == '\x0a') { | 135 if (!word.empty() && word[word.size() - 1] == '\x0a') { |
| 141 // When we come across '\n', we move to the beginning of the next line. | 136 // When we come across '\n', we move to the beginning of the next line. |
| 142 position->set_width(0); | 137 position->set_width(0); |
| (...skipping 12 matching lines...) Expand all Loading... |
| 155 int font_height, | 150 int font_height, |
| 156 gfx::Size* position, | 151 gfx::Size* position, |
| 157 const gfx::Rect& bounds) { | 152 const gfx::Rect& bounds) { |
| 158 if (position->width() + word_width > bounds.right()) { | 153 if (position->width() + word_width > bounds.right()) { |
| 159 position->set_width(0); | 154 position->set_width(0); |
| 160 position->Enlarge(0, font_height); | 155 position->Enlarge(0, font_height); |
| 161 } | 156 } |
| 162 } | 157 } |
| 163 | 158 |
| 164 } // namespace view_text_utils | 159 } // namespace view_text_utils |
| OLD | NEW |