OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "ui/gfx/render_text.h" |
| 6 |
| 7 #include <algorithm> |
| 8 |
| 9 #include "base/i18n/break_iterator.h" |
| 10 #include "base/logging.h" |
| 11 #include "base/stl_util.h" |
| 12 #include "ui/gfx/canvas.h" |
| 13 #include "ui/gfx/canvas_skia.h" |
| 14 |
| 15 namespace { |
| 16 |
| 17 #ifndef NDEBUG |
| 18 // Check StyleRanges invariant conditions: sorted and non-overlapping ranges. |
| 19 void CheckStyleRanges(const gfx::StyleRanges& style_ranges, size_t length) { |
| 20 if (length == 0) { |
| 21 DCHECK(style_ranges.empty()) << "Style ranges exist for empty text."; |
| 22 return; |
| 23 } |
| 24 for (gfx::StyleRanges::size_type i = 0; i < style_ranges.size() - 1; i++) { |
| 25 const ui::Range& former = style_ranges[i].range; |
| 26 const ui::Range& latter = style_ranges[i + 1].range; |
| 27 DCHECK(!former.is_empty()) << "Empty range at " << i << ":" << former; |
| 28 DCHECK(former.IsValid()) << "Invalid range at " << i << ":" << former; |
| 29 DCHECK(!former.is_reversed()) << "Reversed range at " << i << ":" << former; |
| 30 DCHECK(former.end() == latter.start()) << "Ranges gap/overlap/unsorted." << |
| 31 "former:" << former << ", latter:" << latter; |
| 32 } |
| 33 const gfx::StyleRange& end_style = *style_ranges.rbegin(); |
| 34 DCHECK(!end_style.range.is_empty()) << "Empty range at end."; |
| 35 DCHECK(end_style.range.IsValid()) << "Invalid range at end."; |
| 36 DCHECK(!end_style.range.is_reversed()) << "Reversed range at end."; |
| 37 DCHECK(end_style.range.end() == length) << "Style and text length mismatch."; |
| 38 } |
| 39 #endif |
| 40 |
| 41 void ApplyStyleRangeImpl(gfx::StyleRanges& style_ranges, |
| 42 gfx::StyleRange style_range) { |
| 43 const ui::Range& new_range = style_range.range; |
| 44 // Follow StyleRanges invariant conditions: sorted and non-overlapping ranges. |
| 45 gfx::StyleRanges::iterator i; |
| 46 for (i = style_ranges.begin(); i != style_ranges.end();) { |
| 47 if (i->range.end() < new_range.start()) { |
| 48 i++; |
| 49 } else if (i->range.start() == new_range.end()) { |
| 50 break; |
| 51 } else if (new_range.Contains(i->range)) { |
| 52 i = style_ranges.erase(i); |
| 53 if (i == style_ranges.end()) |
| 54 break; |
| 55 } else if (i->range.start() < new_range.start() && |
| 56 i->range.end() > new_range.end()) { |
| 57 // Split the current style into two styles. |
| 58 gfx::StyleRange split_style = gfx::StyleRange(*i); |
| 59 split_style.range.set_end(new_range.start()); |
| 60 i = style_ranges.insert(i, split_style) + 1; |
| 61 i->range.set_start(new_range.end()); |
| 62 break; |
| 63 } else if (i->range.start() < new_range.start()) { |
| 64 i->range.set_end(new_range.start()); |
| 65 i++; |
| 66 } else if (i->range.end() > new_range.end()) { |
| 67 i->range.set_start(new_range.end()); |
| 68 break; |
| 69 } else |
| 70 NOTREACHED(); |
| 71 } |
| 72 // Add the new range in its sorted location. |
| 73 style_ranges.insert(i, style_range); |
| 74 } |
| 75 |
| 76 } // namespace |
| 77 |
| 78 namespace gfx { |
| 79 |
| 80 void RenderText::SetText(const string16& text) { |
| 81 size_t old_text_length = text_.length(); |
| 82 text_ = text; |
| 83 |
| 84 // Update the style ranges as needed. |
| 85 if (text_.empty()) { |
| 86 style_ranges_.clear(); |
| 87 } else if (style_ranges_.empty()) { |
| 88 ApplyDefaultStyle(); |
| 89 } else if (text_.length() > old_text_length) { |
| 90 style_ranges_.back().range.set_end(text_.length()); |
| 91 } else if (text_.length() < old_text_length) { |
| 92 StyleRanges::iterator i; |
| 93 for (i = style_ranges_.begin(); i != style_ranges_.end(); i++) { |
| 94 if (i->range.start() >= text_.length()) { |
| 95 i = style_ranges_.erase(i); |
| 96 if (i == style_ranges_.end()) |
| 97 break; |
| 98 } else if (i->range.end() > text_.length()) { |
| 99 i->range.set_end(text_.length()); |
| 100 } |
| 101 } |
| 102 style_ranges_.back().range.set_end(text_.length()); |
| 103 } |
| 104 #ifndef NDEBUG |
| 105 CheckStyleRanges(style_ranges_, text_.length()); |
| 106 #endif |
| 107 } |
| 108 |
| 109 size_t RenderText::GetCursor() const { |
| 110 return GetSelection().end(); |
| 111 } |
| 112 |
| 113 void RenderText::SetCursor(const size_t position) { |
| 114 SetSelection(ui::Range(position, position)); |
| 115 } |
| 116 |
| 117 void RenderText::MoveCursorLeft(BreakType break_type, bool select) { |
| 118 if (break_type == LINE_BREAK) { |
| 119 MoveCursorTo(0, select); |
| 120 return; |
| 121 } |
| 122 size_t position = GetCursor(); |
| 123 // Cancelling a selection moves to the edge of the selection. |
| 124 if (!GetSelection().is_empty() && !select) { |
| 125 // Use the selection start if it is left of the selection end. |
| 126 if (GetCursorBounds(GetSelection().start(), false).x() < |
| 127 GetCursorBounds(position, false).x()) |
| 128 position = GetSelection().start(); |
| 129 // If |move_by_word|, use the nearest word boundary left of the selection. |
| 130 if (break_type == WORD_BREAK) |
| 131 position = GetLeftCursorPosition(position, true); |
| 132 } else { |
| 133 position = GetLeftCursorPosition(position, break_type == WORD_BREAK); |
| 134 } |
| 135 MoveCursorTo(position, select); |
| 136 } |
| 137 |
| 138 void RenderText::MoveCursorRight(BreakType break_type, bool select) { |
| 139 if (break_type == LINE_BREAK) { |
| 140 MoveCursorTo(text().length(), select); |
| 141 return; |
| 142 } |
| 143 size_t position = GetCursor(); |
| 144 // Cancelling a selection moves to the edge of the selection. |
| 145 if (!GetSelection().is_empty() && !select) { |
| 146 // Use the selection start if it is right of the selection end. |
| 147 if (GetCursorBounds(GetSelection().start(), false).x() > |
| 148 GetCursorBounds(position, false).x()) |
| 149 position = GetSelection().start(); |
| 150 // If |move_by_word|, use the nearest word boundary right of the selection. |
| 151 if (break_type == WORD_BREAK) |
| 152 position = GetRightCursorPosition(position, true); |
| 153 } else { |
| 154 position = GetRightCursorPosition(position, break_type == WORD_BREAK); |
| 155 } |
| 156 MoveCursorTo(position, select); |
| 157 } |
| 158 |
| 159 bool RenderText::MoveCursorTo(size_t position, bool select) { |
| 160 bool changed = GetCursor() != position || select == GetSelection().is_empty(); |
| 161 if (select) |
| 162 SetSelection(ui::Range(GetSelection().start(), position)); |
| 163 else |
| 164 SetSelection(ui::Range(position, position)); |
| 165 return changed; |
| 166 } |
| 167 |
| 168 bool RenderText::MoveCursorTo(const gfx::Point& point, bool select) { |
| 169 // TODO(msw): Make this function support cursor placement via mouse near BiDi |
| 170 // level changes. The visual cursor appearance will depend on the location |
| 171 // clicked, not solely the resulting logical cursor position. See the TODO |
| 172 // note pertaining to selection_range_ for more information. |
| 173 return MoveCursorTo(FindCursorPosition(point), select); |
| 174 } |
| 175 |
| 176 const ui::Range& RenderText::GetSelection() const { |
| 177 return selection_range_; |
| 178 } |
| 179 |
| 180 void RenderText::SetSelection(const ui::Range& range) { |
| 181 selection_range_.set_end(std::min(range.end(), text().length())); |
| 182 selection_range_.set_start(std::min(range.start(), text().length())); |
| 183 |
| 184 // Update |display_offset_| to ensure the current cursor is visible. |
| 185 gfx::Rect cursor_bounds(GetCursorBounds(GetCursor(), get_insert_mode())); |
| 186 int display_width = display_rect_.width(); |
| 187 int string_width = GetStringWidth(); |
| 188 if (string_width < display_width) { |
| 189 // Show all text whenever the text fits to the size. |
| 190 display_offset_.set_x(0); |
| 191 } else if ((display_offset_.x() + cursor_bounds.right()) > display_width) { |
| 192 // Pan to show the cursor when it overflows to the right, |
| 193 display_offset_.set_x(display_width - cursor_bounds.right()); |
| 194 } else if ((display_offset_.x() + cursor_bounds.x()) < 0) { |
| 195 // Pan to show the cursor when it overflows to the left. |
| 196 display_offset_.set_x(-cursor_bounds.x()); |
| 197 } |
| 198 } |
| 199 |
| 200 bool RenderText::IsPointInSelection(const gfx::Point& point) const { |
| 201 size_t pos = FindCursorPosition(point); |
| 202 return (pos >= GetSelection().GetMin() && pos < GetSelection().GetMax()); |
| 203 } |
| 204 |
| 205 void RenderText::ClearSelection() { |
| 206 SetCursor(GetCursor()); |
| 207 } |
| 208 |
| 209 void RenderText::SelectAll() { |
| 210 SetSelection(ui::Range(0, text().length())); |
| 211 } |
| 212 |
| 213 void RenderText::SelectWord() { |
| 214 size_t selection_start = GetSelection().start(); |
| 215 size_t cursor_position = GetCursor(); |
| 216 // First we setup selection_start_ and cursor_pos_. There are so many cases |
| 217 // because we try to emulate what select-word looks like in a gtk textfield. |
| 218 // See associated testcase for different cases. |
| 219 if (cursor_position > 0 && cursor_position < text().length()) { |
| 220 if (isalnum(text()[cursor_position])) { |
| 221 selection_start = cursor_position; |
| 222 cursor_position++; |
| 223 } else |
| 224 selection_start = cursor_position - 1; |
| 225 } else if (cursor_position == 0) { |
| 226 selection_start = cursor_position; |
| 227 if (text().length() > 0) |
| 228 cursor_position++; |
| 229 } else { |
| 230 selection_start = cursor_position - 1; |
| 231 } |
| 232 |
| 233 // Now we move selection_start_ to beginning of selection. Selection boundary |
| 234 // is defined as the position where we have alpha-num character on one side |
| 235 // and non-alpha-num char on the other side. |
| 236 for (; selection_start > 0; selection_start--) { |
| 237 if (IsPositionAtWordSelectionBoundary(selection_start)) |
| 238 break; |
| 239 } |
| 240 |
| 241 // Now we move cursor_pos_ to end of selection. Selection boundary |
| 242 // is defined as the position where we have alpha-num character on one side |
| 243 // and non-alpha-num char on the other side. |
| 244 for (; cursor_position < text().length(); cursor_position++) { |
| 245 if (IsPositionAtWordSelectionBoundary(cursor_position)) |
| 246 break; |
| 247 } |
| 248 |
| 249 SetSelection(ui::Range(selection_start, cursor_position)); |
| 250 } |
| 251 |
| 252 const ui::Range& RenderText::GetCompositionRange() const { |
| 253 return composition_range_; |
| 254 } |
| 255 |
| 256 void RenderText::SetCompositionRange(const ui::Range& composition_range) { |
| 257 CHECK(!composition_range.IsValid() || |
| 258 ui::Range(0, text_.length()).Contains(composition_range)); |
| 259 composition_range_.set_end(composition_range.end()); |
| 260 composition_range_.set_start(composition_range.start()); |
| 261 } |
| 262 |
| 263 void RenderText::ApplyStyleRange(StyleRange style_range) { |
| 264 const ui::Range& new_range = style_range.range; |
| 265 if (!new_range.IsValid() || new_range.is_empty()) |
| 266 return; |
| 267 CHECK(!new_range.is_reversed()); |
| 268 CHECK(ui::Range(0, text_.length()).Contains(new_range)); |
| 269 ApplyStyleRangeImpl(style_ranges_, style_range); |
| 270 #ifndef NDEBUG |
| 271 CheckStyleRanges(style_ranges_, text_.length()); |
| 272 #endif |
| 273 } |
| 274 |
| 275 void RenderText::ApplyDefaultStyle() { |
| 276 style_ranges_.clear(); |
| 277 StyleRange style = StyleRange(default_style_); |
| 278 style.range.set_end(text_.length()); |
| 279 style_ranges_.push_back(style); |
| 280 } |
| 281 |
| 282 base::i18n::TextDirection RenderText::GetTextDirection() const { |
| 283 // TODO(msw): Bidi implementation, intended to replace the functionality added |
| 284 // in crrev.com/91881 (discussed in codereview.chromium.org/7324011). |
| 285 return base::i18n::LEFT_TO_RIGHT; |
| 286 } |
| 287 |
| 288 int RenderText::GetStringWidth() const { |
| 289 return GetSubstringBounds(ui::Range(0, text_.length()))[0].width(); |
| 290 } |
| 291 |
| 292 void RenderText::Draw(gfx::Canvas* canvas) { |
| 293 // Clip the canvas to the text display area. |
| 294 canvas->ClipRectInt(display_rect_.x(), display_rect_.y(), |
| 295 display_rect_.width(), display_rect_.height()); |
| 296 |
| 297 // Draw the selection. |
| 298 std::vector<gfx::Rect> selection(GetSubstringBounds(GetSelection())); |
| 299 SkColor selection_color = |
| 300 get_focused() ? kFocusedSelectionColor : kUnfocusedSelectionColor; |
| 301 for (std::vector<gfx::Rect>::const_iterator i = selection.begin(); |
| 302 i < selection.end(); ++i) { |
| 303 gfx::Rect r(*i); |
| 304 r.Offset(display_offset_); |
| 305 canvas->FillRectInt(selection_color, r.x(), r.y(), r.width(), r.height()); |
| 306 } |
| 307 |
| 308 // Create a temporary copy of the style ranges for composition and selection. |
| 309 // TODO(msw): This pattern ought to be reconsidered; what about composition |
| 310 // and selection overlaps, retain existing local style features? |
| 311 StyleRanges style_ranges(style_ranges_); |
| 312 // Apply a composition style override to a copy of the style ranges. |
| 313 if (composition_range_.IsValid() && !composition_range_.is_empty()) { |
| 314 StyleRange composition_style(default_style_); |
| 315 composition_style.underline = true; |
| 316 composition_style.range.set_start(composition_range_.start()); |
| 317 composition_style.range.set_end(composition_range_.end()); |
| 318 ApplyStyleRangeImpl(style_ranges, composition_style); |
| 319 } |
| 320 // Apply a selection style override to a copy of the style ranges. |
| 321 if (selection_range_.IsValid() && !selection_range_.is_empty()) { |
| 322 StyleRange selection_style(default_style_); |
| 323 selection_style.foreground = kSelectedTextColor; |
| 324 selection_style.range.set_start(selection_range_.GetMin()); |
| 325 selection_style.range.set_end(selection_range_.GetMax()); |
| 326 ApplyStyleRangeImpl(style_ranges, selection_style); |
| 327 } |
| 328 |
| 329 // Draw the text. |
| 330 gfx::Rect bounds(display_rect_); |
| 331 bounds.Offset(display_offset_); |
| 332 for (StyleRanges::const_iterator i = style_ranges.begin(); |
| 333 i < style_ranges.end(); ++i) { |
| 334 Font font = !i->underline ? i->font : |
| 335 i->font.DeriveFont(0, i->font.GetStyle() | Font::UNDERLINED); |
| 336 string16 text = text_.substr(i->range.start(), i->range.length()); |
| 337 bounds.set_width(font.GetStringWidth(text)); |
| 338 canvas->DrawStringInt(text, font, i->foreground, bounds); |
| 339 |
| 340 // Draw the strikethrough. |
| 341 if (i->strike) { |
| 342 SkPaint paint; |
| 343 paint.setAntiAlias(true); |
| 344 paint.setStyle(SkPaint::kFill_Style); |
| 345 paint.setColor(i->foreground); |
| 346 paint.setStrokeWidth(kStrikeWidth); |
| 347 canvas->AsCanvasSkia()->drawLine(SkIntToScalar(bounds.x()), |
| 348 SkIntToScalar(bounds.bottom()), |
| 349 SkIntToScalar(bounds.right()), |
| 350 SkIntToScalar(bounds.y()), |
| 351 paint); |
| 352 } |
| 353 |
| 354 bounds.set_x(bounds.x() + bounds.width()); |
| 355 } |
| 356 |
| 357 // Paint cursor. Replace cursor is drawn as rectangle for now. |
| 358 if (get_cursor_visible() && get_focused()) { |
| 359 gfx::Rect cursor_bounds(GetCursorBounds(GetCursor(), get_insert_mode())); |
| 360 cursor_bounds.Offset(display_offset_); |
| 361 if (!cursor_bounds.IsEmpty()) |
| 362 canvas->DrawRectInt(kCursorColor, |
| 363 cursor_bounds.x(), |
| 364 cursor_bounds.y(), |
| 365 cursor_bounds.width(), |
| 366 cursor_bounds.height()); |
| 367 } |
| 368 } |
| 369 |
| 370 size_t RenderText::FindCursorPosition(const gfx::Point& point) const { |
| 371 const gfx::Font& font = Font(); |
| 372 int left = 0; |
| 373 int left_pos = 0; |
| 374 int right = font.GetStringWidth(text()); |
| 375 int right_pos = text().length(); |
| 376 |
| 377 int x = point.x(); |
| 378 if (x <= left) return left_pos; |
| 379 if (x >= right) return right_pos; |
| 380 // binary searching the cursor position. |
| 381 // TODO(oshima): use the center of character instead of edge. |
| 382 // Binary search may not work for language like arabic. |
| 383 while (std::abs(static_cast<long>(right_pos - left_pos) > 1)) { |
| 384 int pivot_pos = left_pos + (right_pos - left_pos) / 2; |
| 385 int pivot = font.GetStringWidth(text().substr(0, pivot_pos)); |
| 386 if (pivot < x) { |
| 387 left = pivot; |
| 388 left_pos = pivot_pos; |
| 389 } else if (pivot == x) { |
| 390 return pivot_pos; |
| 391 } else { |
| 392 right = pivot; |
| 393 right_pos = pivot_pos; |
| 394 } |
| 395 } |
| 396 return left_pos; |
| 397 } |
| 398 |
| 399 std::vector<gfx::Rect> RenderText::GetSubstringBounds( |
| 400 const ui::Range& range) const { |
| 401 size_t start = range.GetMin(); |
| 402 size_t end = range.GetMax(); |
| 403 gfx::Font font; |
| 404 int start_x = font.GetStringWidth(text().substr(0, start)); |
| 405 int end_x = font.GetStringWidth(text().substr(0, end)); |
| 406 std::vector<gfx::Rect> bounds; |
| 407 bounds.push_back(gfx::Rect(start_x, 0, end_x - start_x, font.GetHeight())); |
| 408 return bounds; |
| 409 } |
| 410 |
| 411 gfx::Rect RenderText::GetCursorBounds(size_t cursor_pos, |
| 412 bool insert_mode) const { |
| 413 gfx::Font font; |
| 414 int x = font.GetStringWidth(text_.substr(0U, cursor_pos)); |
| 415 DCHECK_GE(x, 0); |
| 416 int h = std::min(display_rect_.height(), font.GetHeight()); |
| 417 gfx::Rect bounds(x, (display_rect_.height() - h) / 2, 1, h); |
| 418 if (!insert_mode && text_.length() != cursor_pos) |
| 419 bounds.set_width(font.GetStringWidth(text_.substr(0, cursor_pos + 1)) - x); |
| 420 return bounds; |
| 421 } |
| 422 |
| 423 size_t RenderText::GetLeftCursorPosition(size_t position, |
| 424 bool move_by_word) const { |
| 425 if (!move_by_word) |
| 426 return position == 0? position : position - 1; |
| 427 // Notes: We always iterate words from the begining. |
| 428 // This is probably fast enough for our usage, but we may |
| 429 // want to modify WordIterator so that it can start from the |
| 430 // middle of string and advance backwards. |
| 431 base::i18n::BreakIterator iter(text(), base::i18n::BreakIterator::BREAK_WORD); |
| 432 bool success = iter.Init(); |
| 433 DCHECK(success); |
| 434 if (!success) |
| 435 return position; |
| 436 int last = 0; |
| 437 while (iter.Advance()) { |
| 438 if (iter.IsWord()) { |
| 439 size_t begin = iter.pos() - iter.GetString().length(); |
| 440 if (begin == position) { |
| 441 // The cursor is at the beginning of a word. |
| 442 // Move to previous word. |
| 443 break; |
| 444 } else if(iter.pos() >= position) { |
| 445 // The cursor is in the middle or at the end of a word. |
| 446 // Move to the top of current word. |
| 447 last = begin; |
| 448 break; |
| 449 } else { |
| 450 last = iter.pos() - iter.GetString().length(); |
| 451 } |
| 452 } |
| 453 } |
| 454 |
| 455 return last; |
| 456 } |
| 457 |
| 458 size_t RenderText::GetRightCursorPosition(size_t position, |
| 459 bool move_by_word) const { |
| 460 if (!move_by_word) |
| 461 return std::min(position + 1, text().length()); |
| 462 base::i18n::BreakIterator iter(text(), base::i18n::BreakIterator::BREAK_WORD); |
| 463 bool success = iter.Init(); |
| 464 DCHECK(success); |
| 465 if (!success) |
| 466 return position; |
| 467 size_t pos = 0; |
| 468 while (iter.Advance()) { |
| 469 pos = iter.pos(); |
| 470 if (iter.IsWord() && pos > position) { |
| 471 break; |
| 472 } |
| 473 } |
| 474 return pos; |
| 475 } |
| 476 |
| 477 RenderText::RenderText() |
| 478 : text_(), |
| 479 selection_range_(), |
| 480 is_cursor_visible_(false), |
| 481 insert_mode_(true), |
| 482 composition_range_(), |
| 483 style_ranges_(), |
| 484 default_style_(), |
| 485 display_rect_(), |
| 486 display_offset_() { |
| 487 } |
| 488 |
| 489 RenderText::~RenderText() { |
| 490 } |
| 491 |
| 492 bool RenderText::IsPositionAtWordSelectionBoundary(size_t pos) { |
| 493 return pos == 0 || (isalnum(text()[pos - 1]) && !isalnum(text()[pos])) || |
| 494 (!isalnum(text()[pos - 1]) && isalnum(text()[pos])); |
| 495 } |
| 496 |
| 497 } // namespace gfx |
OLD | NEW |