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/gfx/render_text.h" | 5 #include "ui/gfx/render_text.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 | 8 |
9 #include "base/i18n/break_iterator.h" | 9 #include "base/i18n/break_iterator.h" |
10 #include "base/logging.h" | 10 #include "base/logging.h" |
11 #include "base/stl_util.h" | 11 #include "base/stl_util.h" |
12 #include "ui/gfx/canvas.h" | 12 #include "ui/gfx/canvas.h" |
13 #include "ui/gfx/canvas_skia.h" | 13 #include "ui/gfx/canvas_skia.h" |
14 #include "unicode/uchar.h" | 14 #include "unicode/uchar.h" |
15 | 15 |
16 namespace { | 16 namespace { |
17 | 17 |
| 18 // All chars are replaced by this char when the password style is set. |
| 19 // TODO(benrg): GTK uses the first of U+25CF, U+2022, U+2731, U+273A, '*' |
| 20 // that's available in the font (find_invisible_char() in gtkentry.c). |
| 21 const char16 PASSWORD_REPLACEMENT_CHAR = '*'; |
| 22 |
18 #ifndef NDEBUG | 23 #ifndef NDEBUG |
19 // Check StyleRanges invariant conditions: sorted and non-overlapping ranges. | 24 // Check StyleRanges invariant conditions: sorted and non-overlapping ranges. |
20 void CheckStyleRanges(const gfx::StyleRanges& style_ranges, size_t length) { | 25 void CheckStyleRanges(const gfx::StyleRanges& style_ranges, size_t length) { |
21 if (length == 0) { | 26 if (length == 0) { |
22 DCHECK(style_ranges.empty()) << "Style ranges exist for empty text."; | 27 DCHECK(style_ranges.empty()) << "Style ranges exist for empty text."; |
23 return; | 28 return; |
24 } | 29 } |
25 for (gfx::StyleRanges::size_type i = 0; i < style_ranges.size() - 1; i++) { | 30 for (gfx::StyleRanges::size_type i = 0; i < style_ranges.size() - 1; i++) { |
26 const ui::Range& former = style_ranges[i].range; | 31 const ui::Range& former = style_ranges[i].range; |
27 const ui::Range& latter = style_ranges[i + 1].range; | 32 const ui::Range& latter = style_ranges[i + 1].range; |
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
76 | 81 |
77 } // namespace | 82 } // namespace |
78 | 83 |
79 namespace gfx { | 84 namespace gfx { |
80 | 85 |
81 StyleRange::StyleRange() | 86 StyleRange::StyleRange() |
82 : font(), | 87 : font(), |
83 foreground(SK_ColorBLACK), | 88 foreground(SK_ColorBLACK), |
84 strike(false), | 89 strike(false), |
85 underline(false), | 90 underline(false), |
| 91 password(false), |
86 range() { | 92 range() { |
87 } | 93 } |
88 | 94 |
89 RenderText::~RenderText() { | 95 RenderText::~RenderText() { |
90 } | 96 } |
91 | 97 |
92 void RenderText::SetText(const string16& text) { | 98 void RenderText::SetText(const string16& text) { |
93 DCHECK(!composition_range_.IsValid()); | 99 DCHECK(!composition_range_.IsValid()); |
94 size_t old_text_length = text_.length(); | 100 size_t old_text_length = text_.length(); |
95 text_ = text; | 101 text_ = text; |
(...skipping 24 matching lines...) Expand all Loading... |
120 #endif | 126 #endif |
121 cached_bounds_and_offset_valid_ = false; | 127 cached_bounds_and_offset_valid_ = false; |
122 | 128 |
123 // Reset selection model. SetText should always followed by SetSelectionModel | 129 // Reset selection model. SetText should always followed by SetSelectionModel |
124 // or SetCursorPosition in upper layer. | 130 // or SetCursorPosition in upper layer. |
125 SetSelectionModel(SelectionModel(0, 0, SelectionModel::LEADING)); | 131 SetSelectionModel(SelectionModel(0, 0, SelectionModel::LEADING)); |
126 | 132 |
127 UpdateLayout(); | 133 UpdateLayout(); |
128 } | 134 } |
129 | 135 |
| 136 string16 RenderText::GetCensoredText() const { |
| 137 string16 txt = text(); |
| 138 for (gfx::StyleRanges::const_iterator i = style_ranges().begin(); |
| 139 i != style_ranges().end(); ++i) { |
| 140 if (i->password) { |
| 141 // TODO(benrg): There should probably be one bullet/asterisk per |
| 142 // cursorable character, not one per UTF-16 word. However, I'm not sure |
| 143 // it's worth the effort. GTK appears to use one per code point. Do people |
| 144 // use non-BMP code points and combining diacritics in their passwords? |
| 145 std::fill(txt.begin() + i->range.start(), |
| 146 txt.begin() + i->range.end(), PASSWORD_REPLACEMENT_CHAR); |
| 147 } |
| 148 } |
| 149 return txt; |
| 150 } |
| 151 |
130 void RenderText::ToggleInsertMode() { | 152 void RenderText::ToggleInsertMode() { |
131 insert_mode_ = !insert_mode_; | 153 insert_mode_ = !insert_mode_; |
132 cached_bounds_and_offset_valid_ = false; | 154 cached_bounds_and_offset_valid_ = false; |
133 } | 155 } |
134 | 156 |
135 void RenderText::SetDisplayRect(const Rect& r) { | 157 void RenderText::SetDisplayRect(const Rect& r) { |
136 display_rect_ = r; | 158 display_rect_ = r; |
137 cached_bounds_and_offset_valid_ = false; | 159 cached_bounds_and_offset_valid_ = false; |
138 UpdateLayout(); | 160 UpdateLayout(); |
139 } | 161 } |
(...skipping 471 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
611 void RenderText::DrawCursor(Canvas* canvas) { | 633 void RenderText::DrawCursor(Canvas* canvas) { |
612 // Paint cursor. Replace cursor is drawn as rectangle for now. | 634 // Paint cursor. Replace cursor is drawn as rectangle for now. |
613 // TODO(msw): Draw a better cursor with a better indication of association. | 635 // TODO(msw): Draw a better cursor with a better indication of association. |
614 if (cursor_visible() && focused()) { | 636 if (cursor_visible() && focused()) { |
615 Rect r(GetUpdatedCursorBounds()); | 637 Rect r(GetUpdatedCursorBounds()); |
616 canvas->DrawRectInt(kCursorColor, r.x(), r.y(), r.width(), r.height()); | 638 canvas->DrawRectInt(kCursorColor, r.x(), r.y(), r.width(), r.height()); |
617 } | 639 } |
618 } | 640 } |
619 | 641 |
620 } // namespace gfx | 642 } // namespace gfx |
OLD | NEW |