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 "views/view_text_utils.h" | 5 #include "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_skia.h" | 11 #include "ui/gfx/canvas_skia.h" |
12 #include "ui/gfx/color_utils.h" | |
13 #include "ui/gfx/size.h" | 12 #include "ui/gfx/size.h" |
14 #include "views/controls/label.h" | 13 #include "views/controls/label.h" |
15 #include "views/controls/link.h" | 14 #include "views/controls/link.h" |
16 | 15 |
17 namespace view_text_utils { | 16 namespace view_text_utils { |
18 | 17 |
19 void DrawTextAndPositionUrl(gfx::Canvas* canvas, | 18 void DrawTextAndPositionUrl(gfx::Canvas* canvas, |
20 views::Label* label, | 19 views::Label* label, |
21 const std::wstring& text, | 20 const std::wstring& text, |
22 views::Link* link, | 21 views::Link* link, |
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
81 } | 80 } |
82 | 81 |
83 void DrawTextStartingFrom(gfx::Canvas* canvas, | 82 void DrawTextStartingFrom(gfx::Canvas* canvas, |
84 views::Label* label, | 83 views::Label* label, |
85 const std::wstring& text, | 84 const std::wstring& text, |
86 gfx::Size* position, | 85 gfx::Size* position, |
87 const gfx::Rect& bounds, | 86 const gfx::Rect& bounds, |
88 const gfx::Font& font, | 87 const gfx::Font& font, |
89 bool text_direction_is_rtl, | 88 bool text_direction_is_rtl, |
90 bool ltr_within_rtl) { | 89 bool ltr_within_rtl) { |
91 #if defined(OS_WIN) | |
92 const SkColor text_color = color_utils::GetSysSkColor(COLOR_WINDOWTEXT); | |
93 #else | |
94 // TODO(beng): source from theme provider. | |
95 const SkColor text_color = SK_ColorBLACK; | |
96 #endif | |
97 | |
98 // Iterate through line breaking opportunities (which in English would be | 90 // Iterate through line breaking opportunities (which in English would be |
99 // spaces and such). This tells us where to wrap. | 91 // spaces and such). This tells us where to wrap. |
100 string16 text16(WideToUTF16(text)); | 92 string16 text16(WideToUTF16(text)); |
101 base::i18n::BreakIterator iter(text16, | 93 base::i18n::BreakIterator iter(text16, |
102 base::i18n::BreakIterator::BREAK_SPACE); | 94 base::i18n::BreakIterator::BREAK_SPACE); |
103 if (!iter.Init()) | 95 if (!iter.Init()) |
104 return; | 96 return; |
105 | 97 |
106 int flags = (text_direction_is_rtl ? gfx::Canvas::TEXT_ALIGN_RIGHT : | 98 int flags = (text_direction_is_rtl ? gfx::Canvas::TEXT_ALIGN_RIGHT : |
107 gfx::Canvas::TEXT_ALIGN_LEFT); | 99 gfx::Canvas::TEXT_ALIGN_LEFT); |
(...skipping 25 matching lines...) Expand all Loading... |
133 int space_w = font.GetStringWidth(ASCIIToUTF16(" ")); | 125 int space_w = font.GetStringWidth(ASCIIToUTF16(" ")); |
134 int space_h = font.GetHeight(); | 126 int space_h = font.GetHeight(); |
135 gfx::CanvasSkia::SizeStringInt(ASCIIToUTF16(" "), font, &space_w, | 127 gfx::CanvasSkia::SizeStringInt(ASCIIToUTF16(" "), font, &space_w, |
136 &space_h, flags); | 128 &space_h, flags); |
137 x += space_w; | 129 x += space_w; |
138 } | 130 } |
139 } | 131 } |
140 int y = position->height() + bounds.y(); | 132 int y = position->height() + bounds.y(); |
141 | 133 |
142 // Draw the text on the screen (mirrored, if RTL run). | 134 // Draw the text on the screen (mirrored, if RTL run). |
143 canvas->DrawStringInt(word, font, text_color, x, y, w, font.GetHeight(), | 135 canvas->DrawStringInt(word, font, label->enabled_color(), x, y, w, |
144 flags); | 136 font.GetHeight(), flags); |
145 | 137 |
146 if (!word.empty() && word[word.size() - 1] == '\x0a') { | 138 if (!word.empty() && word[word.size() - 1] == '\x0a') { |
147 // When we come across '\n', we move to the beginning of the next line. | 139 // When we come across '\n', we move to the beginning of the next line. |
148 position->set_width(0); | 140 position->set_width(0); |
149 position->Enlarge(0, font.GetHeight()); | 141 position->Enlarge(0, font.GetHeight()); |
150 } else { | 142 } else { |
151 // Otherwise, we advance position to the next word. | 143 // Otherwise, we advance position to the next word. |
152 position->Enlarge(w, 0); | 144 position->Enlarge(w, 0); |
153 } | 145 } |
154 | 146 |
155 if (ltr_within_rtl) | 147 if (ltr_within_rtl) |
156 break; // LTR within RTL is drawn as one unit, so we are done. | 148 break; // LTR within RTL is drawn as one unit, so we are done. |
157 } | 149 } |
158 } | 150 } |
159 | 151 |
160 void WrapIfWordDoesntFit(int word_width, | 152 void WrapIfWordDoesntFit(int word_width, |
161 int font_height, | 153 int font_height, |
162 gfx::Size* position, | 154 gfx::Size* position, |
163 const gfx::Rect& bounds) { | 155 const gfx::Rect& bounds) { |
164 if (position->width() + word_width > bounds.right()) { | 156 if (position->width() + word_width > bounds.right()) { |
165 position->set_width(0); | 157 position->set_width(0); |
166 position->Enlarge(0, font_height); | 158 position->Enlarge(0, font_height); |
167 } | 159 } |
168 } | 160 } |
169 | 161 |
170 } // namespace view_text_utils | 162 } // namespace view_text_utils |
OLD | NEW |