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/font.h" |
| 13 #include "ui/gfx/rect.h" |
12 #include "ui/gfx/size.h" | 14 #include "ui/gfx/size.h" |
13 #include "views/controls/label.h" | 15 #include "views/controls/label.h" |
14 #include "views/controls/link.h" | 16 #include "views/controls/link.h" |
15 | 17 |
16 namespace view_text_utils { | 18 namespace view_text_utils { |
17 | 19 |
18 void DrawTextAndPositionUrl(gfx::Canvas* canvas, | 20 void DrawTextAndPositionUrl(gfx::Canvas* canvas, |
19 views::Label* label, | 21 views::Label* label, |
20 const std::wstring& text, | 22 const string16& text, |
21 views::Link* link, | 23 views::Link* link, |
22 gfx::Rect* rect, | 24 gfx::Rect* rect, |
23 gfx::Size* position, | 25 gfx::Size* position, |
24 bool text_direction_is_rtl, | 26 bool text_direction_is_rtl, |
25 const gfx::Rect& bounds, | 27 const gfx::Rect& bounds, |
26 const gfx::Font& font) { | 28 const gfx::Font& font) { |
27 DCHECK(canvas && position); | 29 DCHECK(canvas && position); |
28 | 30 |
29 // 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 |
30 // 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 |
31 // initialize a bidirectional ICU line iterator and split the text into | 33 // initialize a bidirectional ICU line iterator and split the text into |
32 // 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. |
33 base::i18n::BiDiLineIterator bidi_line; | 35 base::i18n::BiDiLineIterator bidi_line; |
34 if (!bidi_line.Open(WideToUTF16Hack(text), true, false)) | 36 if (!bidi_line.Open(text, true, false)) |
35 return; | 37 return; |
36 | 38 |
37 // Iterate over each run and draw it. | 39 // Iterate over each run and draw it. |
38 int run_start = 0; | 40 int run_start = 0; |
39 int run_end = 0; | 41 int run_end = 0; |
40 const int runs = bidi_line.CountRuns(); | 42 const int runs = bidi_line.CountRuns(); |
41 for (int run = 0; run < runs; ++run) { | 43 for (int run = 0; run < runs; ++run) { |
42 UBiDiLevel level = 0; | 44 UBiDiLevel level = 0; |
43 bidi_line.GetLogicalRun(run_start, &run_end, &level); | 45 bidi_line.GetLogicalRun(run_start, &run_end, &level); |
44 DCHECK(run_end > run_start); | 46 DCHECK(run_end > run_start); |
45 std::wstring fragment = text.substr(run_start, run_end - run_start); | 47 string16 fragment = text.substr(run_start, run_end - run_start); |
46 | 48 |
47 // A flag that tells us whether we found LTR text inside RTL text. | 49 // A flag that tells us whether we found LTR text inside RTL text. |
48 bool ltr_inside_rtl_text = | 50 bool ltr_inside_rtl_text = |
49 ((level & 1) == UBIDI_LTR) && text_direction_is_rtl; | 51 ((level & 1) == UBIDI_LTR) && text_direction_is_rtl; |
50 | 52 |
51 // Draw text chunk contained in |fragment|. |position| is relative to the | 53 // Draw text chunk contained in |fragment|. |position| is relative to the |
52 // top left corner of the label we draw inside, even when drawing RTL. | 54 // top left corner of the label we draw inside, even when drawing RTL. |
53 DrawTextStartingFrom(canvas, label, fragment, position, bounds, font, | 55 DrawTextStartingFrom(canvas, label, fragment, position, bounds, font, |
54 text_direction_is_rtl, ltr_inside_rtl_text); | 56 text_direction_is_rtl, ltr_inside_rtl_text); |
55 | 57 |
(...skipping 18 matching lines...) Expand all Loading... |
74 // on) to absolute pixel coordinates (relative to the top left corner of | 76 // on) to absolute pixel coordinates (relative to the top left corner of |
75 // the dialog content). | 77 // the dialog content). |
76 rect->Offset(bounds.x(), bounds.y()); | 78 rect->Offset(bounds.x(), bounds.y()); |
77 // And leave some space to draw the link in. | 79 // And leave some space to draw the link in. |
78 position->Enlarge(sz.width(), 0); | 80 position->Enlarge(sz.width(), 0); |
79 } | 81 } |
80 } | 82 } |
81 | 83 |
82 void DrawTextStartingFrom(gfx::Canvas* canvas, | 84 void DrawTextStartingFrom(gfx::Canvas* canvas, |
83 views::Label* label, | 85 views::Label* label, |
84 const std::wstring& text, | 86 const string16& text, |
85 gfx::Size* position, | 87 gfx::Size* position, |
86 const gfx::Rect& bounds, | 88 const gfx::Rect& bounds, |
87 const gfx::Font& font, | 89 const gfx::Font& font, |
88 bool text_direction_is_rtl, | 90 bool text_direction_is_rtl, |
89 bool ltr_within_rtl) { | 91 bool ltr_within_rtl) { |
90 // Iterate through line breaking opportunities (which in English would be | 92 // Iterate through line breaking opportunities (which in English would be |
91 // spaces and such). This tells us where to wrap. | 93 // spaces and such). This tells us where to wrap. |
92 string16 text16(WideToUTF16(text)); | 94 string16 text16(text); |
93 base::i18n::BreakIterator iter(text16, | 95 base::i18n::BreakIterator iter(text16, |
94 base::i18n::BreakIterator::BREAK_SPACE); | 96 base::i18n::BreakIterator::BREAK_SPACE); |
95 if (!iter.Init()) | 97 if (!iter.Init()) |
96 return; | 98 return; |
97 | 99 |
98 int flags = (text_direction_is_rtl ? gfx::Canvas::TEXT_ALIGN_RIGHT : | 100 int flags = (text_direction_is_rtl ? gfx::Canvas::TEXT_ALIGN_RIGHT : |
99 gfx::Canvas::TEXT_ALIGN_LEFT); | 101 gfx::Canvas::TEXT_ALIGN_LEFT); |
100 flags |= gfx::Canvas::MULTI_LINE | gfx::Canvas::HIDE_PREFIX; | 102 flags |= gfx::Canvas::MULTI_LINE | gfx::Canvas::HIDE_PREFIX; |
101 | 103 |
102 // 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: |
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
153 int font_height, | 155 int font_height, |
154 gfx::Size* position, | 156 gfx::Size* position, |
155 const gfx::Rect& bounds) { | 157 const gfx::Rect& bounds) { |
156 if (position->width() + word_width > bounds.right()) { | 158 if (position->width() + word_width > bounds.right()) { |
157 position->set_width(0); | 159 position->set_width(0); |
158 position->Enlarge(0, font_height); | 160 position->Enlarge(0, font_height); |
159 } | 161 } |
160 } | 162 } |
161 | 163 |
162 } // namespace view_text_utils | 164 } // namespace view_text_utils |
OLD | NEW |