| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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_linux.h" | 5 #include "ui/gfx/render_text_linux.h" |
| 6 | 6 |
| 7 #include <pango/pangocairo.h> | 7 #include <pango/pangocairo.h> |
| 8 #include <algorithm> | 8 #include <algorithm> |
| 9 #include <string> | 9 #include <string> |
| 10 #include <vector> | 10 #include <vector> |
| (...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 70 | 70 |
| 71 base::i18n::TextDirection RenderTextLinux::GetTextDirection() { | 71 base::i18n::TextDirection RenderTextLinux::GetTextDirection() { |
| 72 EnsureLayout(); | 72 EnsureLayout(); |
| 73 | 73 |
| 74 PangoDirection base_dir = pango_find_base_dir(layout_text_, -1); | 74 PangoDirection base_dir = pango_find_base_dir(layout_text_, -1); |
| 75 if (base_dir == PANGO_DIRECTION_RTL || base_dir == PANGO_DIRECTION_WEAK_RTL) | 75 if (base_dir == PANGO_DIRECTION_RTL || base_dir == PANGO_DIRECTION_WEAK_RTL) |
| 76 return base::i18n::RIGHT_TO_LEFT; | 76 return base::i18n::RIGHT_TO_LEFT; |
| 77 return base::i18n::LEFT_TO_RIGHT; | 77 return base::i18n::LEFT_TO_RIGHT; |
| 78 } | 78 } |
| 79 | 79 |
| 80 int RenderTextLinux::GetStringWidth() { | 80 Size RenderTextLinux::GetStringSize() { |
| 81 EnsureLayout(); | 81 EnsureLayout(); |
| 82 int width; | 82 int width = 0, height = 0; |
| 83 pango_layout_get_pixel_size(layout_, &width, NULL); | 83 pango_layout_get_pixel_size(layout_, &width, &height); |
| 84 return width; | 84 return Size(width, height); |
| 85 } | 85 } |
| 86 | 86 |
| 87 SelectionModel RenderTextLinux::FindCursorPosition(const Point& point) { | 87 SelectionModel RenderTextLinux::FindCursorPosition(const Point& point) { |
| 88 EnsureLayout(); | 88 EnsureLayout(); |
| 89 | 89 |
| 90 if (text().empty()) | 90 if (text().empty()) |
| 91 return SelectionModel(0, 0, SelectionModel::LEADING); | 91 return SelectionModel(0, CURSOR_FORWARD); |
| 92 | 92 |
| 93 Point p(ToTextPoint(point)); | 93 Point p(ToTextPoint(point)); |
| 94 | 94 |
| 95 // When the point is outside of text, return HOME/END position. | 95 // When the point is outside of text, return HOME/END position. |
| 96 if (p.x() < 0) | 96 if (p.x() < 0) |
| 97 return EdgeSelectionModel(CURSOR_LEFT); | 97 return EdgeSelectionModel(CURSOR_LEFT); |
| 98 else if (p.x() > GetStringWidth()) | 98 else if (p.x() > GetStringSize().width()) |
| 99 return EdgeSelectionModel(CURSOR_RIGHT); | 99 return EdgeSelectionModel(CURSOR_RIGHT); |
| 100 | 100 |
| 101 int caret_pos, trailing; | 101 int caret_pos = 0, trailing = 0; |
| 102 pango_layout_xy_to_index(layout_, p.x() * PANGO_SCALE, p.y() * PANGO_SCALE, | 102 pango_layout_xy_to_index(layout_, p.x() * PANGO_SCALE, p.y() * PANGO_SCALE, |
| 103 &caret_pos, &trailing); | 103 &caret_pos, &trailing); |
| 104 | 104 |
| 105 size_t selection_end = caret_pos; | 105 DCHECK_GE(trailing, 0); |
| 106 if (trailing > 0) { | 106 if (trailing > 0) { |
| 107 const char* ch = g_utf8_offset_to_pointer(layout_text_ + caret_pos, | 107 caret_pos = g_utf8_offset_to_pointer(layout_text_ + caret_pos, |
| 108 trailing); | 108 trailing) - layout_text_; |
| 109 DCHECK_GE(ch, layout_text_); | 109 DCHECK_LE(static_cast<size_t>(caret_pos), layout_text_len_); |
| 110 DCHECK_LE(ch, layout_text_ + layout_text_len_); | |
| 111 selection_end = ch - layout_text_; | |
| 112 } | 110 } |
| 113 | 111 |
| 114 return SelectionModel( | 112 return SelectionModel(LayoutIndexToTextIndex(caret_pos), |
| 115 LayoutIndexToTextIndex(selection_end), | 113 (trailing > 0) ? CURSOR_BACKWARD : CURSOR_FORWARD); |
| 116 LayoutIndexToTextIndex(caret_pos), | |
| 117 trailing > 0 ? SelectionModel::TRAILING : SelectionModel::LEADING); | |
| 118 } | 114 } |
| 119 | 115 |
| 120 Rect RenderTextLinux::GetCursorBounds(const SelectionModel& selection, | |
| 121 bool insert_mode) { | |
| 122 EnsureLayout(); | |
| 123 | |
| 124 size_t caret_pos = insert_mode ? selection.caret_pos() : | |
| 125 selection.selection_end(); | |
| 126 PangoRectangle pos; | |
| 127 pango_layout_index_to_pos(layout_, TextIndexToLayoutIndex(caret_pos), &pos); | |
| 128 | |
| 129 SelectionModel::CaretPlacement caret_placement = selection.caret_placement(); | |
| 130 int x = pos.x; | |
| 131 if ((insert_mode && caret_placement == SelectionModel::TRAILING) || | |
| 132 (!insert_mode && pos.width < 0)) | |
| 133 x += pos.width; | |
| 134 x = PANGO_PIXELS(x); | |
| 135 | |
| 136 int h = std::min(display_rect().height(), PANGO_PIXELS(pos.height)); | |
| 137 Rect bounds(x, (display_rect().height() - h) / 2, 0, h); | |
| 138 bounds.set_origin(ToViewPoint(bounds.origin())); | |
| 139 | |
| 140 if (!insert_mode) | |
| 141 bounds.set_width(PANGO_PIXELS(std::abs(pos.width))); | |
| 142 | |
| 143 return bounds; | |
| 144 } | |
| 145 | |
| 146 // Assume caret_pos in |current| is n, 'l' represents leading in | |
| 147 // caret_placement and 't' represents trailing in caret_placement. Following | |
| 148 // is the calculation from (caret_pos, caret_placement) in |current| to | |
| 149 // (selection_end, caret_pos, caret_placement) when moving cursor left/right by | |
| 150 // one grapheme (for simplicity, assume each grapheme is one character). | |
| 151 // If n is in LTR (if moving left) or RTL (if moving right) run, | |
| 152 // (n, t) --> (n, n, l). | |
| 153 // (n, l) --> (n-1, n-1, l) if n is inside run (not at boundary). | |
| 154 // (n, l) --> goto across run case if n is at run boundary. | |
| 155 // Otherwise, | |
| 156 // (n, l) --> (n+1, n, t). | |
| 157 // (n, t) --> (n+2, n+1, t) if n is inside run. | |
| 158 // (n, t) --> goto across run case if n is at run boundary. | |
| 159 // If n is at run boundary, get its visually left/right run, | |
| 160 // If left/right run is LTR/RTL run, | |
| 161 // (n, t) --> (left/right run's end, left/right run's end, l). | |
| 162 // Otherwise, | |
| 163 // (n, t) --> (left/right run's begin + 1, left/right run's begin, t). | |
| 164 SelectionModel RenderTextLinux::AdjacentCharSelectionModel( | 116 SelectionModel RenderTextLinux::AdjacentCharSelectionModel( |
| 165 const SelectionModel& selection, | 117 const SelectionModel& selection, |
| 166 VisualCursorDirection direction) { | 118 VisualCursorDirection direction) { |
| 167 size_t caret = selection.caret_pos(); | 119 GSList* run = GetRunContainingCaret(selection); |
| 168 SelectionModel::CaretPlacement caret_placement = selection.caret_placement(); | 120 if (!run) { |
| 169 GSList* run = GetRunContainingPosition(caret); | 121 // The cursor is not in any run: we're at the visual and logical edge. |
| 170 DCHECK(run); | 122 SelectionModel edge = EdgeSelectionModel(direction); |
| 171 | 123 if (edge.caret_pos() == selection.caret_pos()) |
| 172 PangoLayoutRun* layout_run = reinterpret_cast<PangoLayoutRun*>(run->data); | 124 return edge; |
| 173 PangoItem* item = layout_run->item; | 125 else |
| 174 size_t run_start = LayoutIndexToTextIndex(item->offset); | 126 run = (direction == CURSOR_RIGHT) ? |
| 175 size_t run_end = LayoutIndexToTextIndex(item->offset + item->length); | 127 current_line_->runs : g_slist_last(current_line_->runs); |
| 176 if (!IsForwardMotion(direction, item)) { | 128 } else { |
| 177 if (caret_placement == SelectionModel::TRAILING) | 129 // If the cursor is moving within the current run, just move it by one |
| 178 return SelectionModel(caret, caret, SelectionModel::LEADING); | 130 // grapheme in the appropriate direction. |
| 179 else if (caret > run_start) { | 131 PangoItem* item = reinterpret_cast<PangoLayoutRun*>(run->data)->item; |
| 180 caret = IndexOfAdjacentGrapheme(caret, CURSOR_BACKWARD); | 132 size_t caret = selection.caret_pos(); |
| 181 return SelectionModel(caret, caret, SelectionModel::LEADING); | 133 if (IsForwardMotion(direction, item)) { |
| 134 if (caret < LayoutIndexToTextIndex(item->offset + item->length)) { |
| 135 caret = IndexOfAdjacentGrapheme(caret, CURSOR_FORWARD); |
| 136 return SelectionModel(caret, CURSOR_BACKWARD); |
| 137 } |
| 138 } else { |
| 139 if (caret > LayoutIndexToTextIndex(item->offset)) { |
| 140 caret = IndexOfAdjacentGrapheme(caret, CURSOR_BACKWARD); |
| 141 return SelectionModel(caret, CURSOR_FORWARD); |
| 142 } |
| 182 } | 143 } |
| 183 } else { | 144 // The cursor is at the edge of a run; move to the visually adjacent run. |
| 184 if (caret_placement == SelectionModel::LEADING) { | 145 // TODO(xji): Keep a vector of runs to avoid using a singly-linked list. |
| 185 size_t cursor = IndexOfAdjacentGrapheme(caret, CURSOR_FORWARD); | 146 run = (direction == CURSOR_RIGHT) ? |
| 186 return SelectionModel(cursor, caret, SelectionModel::TRAILING); | 147 run->next : GSListPrevious(current_line_->runs, run); |
| 187 } else if (selection.selection_end() < run_end) { | 148 if (!run) |
| 188 caret = IndexOfAdjacentGrapheme(caret, CURSOR_FORWARD); | 149 return EdgeSelectionModel(direction); |
| 189 size_t cursor = IndexOfAdjacentGrapheme(caret, CURSOR_FORWARD); | |
| 190 return SelectionModel(cursor, caret, SelectionModel::TRAILING); | |
| 191 } | |
| 192 } | 150 } |
| 193 | 151 PangoItem* item = reinterpret_cast<PangoLayoutRun*>(run->data)->item; |
| 194 // The character is at the edge of its run; advance to the adjacent visual | |
| 195 // run. | |
| 196 // TODO(xji): Keep a vector of runs to avoid using a singly-linked list. | |
| 197 GSList* adjacent_run = (direction == CURSOR_RIGHT) ? | |
| 198 run->next : GSListPrevious(current_line_->runs, run); | |
| 199 if (!adjacent_run) | |
| 200 return EdgeSelectionModel(direction); | |
| 201 | |
| 202 item = reinterpret_cast<PangoLayoutRun*>(adjacent_run->data)->item; | |
| 203 return IsForwardMotion(direction, item) ? | 152 return IsForwardMotion(direction, item) ? |
| 204 FirstSelectionModelInsideRun(item) : LastSelectionModelInsideRun(item); | 153 FirstSelectionModelInsideRun(item) : LastSelectionModelInsideRun(item); |
| 205 } | 154 } |
| 206 | 155 |
| 207 SelectionModel RenderTextLinux::AdjacentWordSelectionModel( | 156 SelectionModel RenderTextLinux::AdjacentWordSelectionModel( |
| 208 const SelectionModel& selection, | 157 const SelectionModel& selection, |
| 209 VisualCursorDirection direction) { | 158 VisualCursorDirection direction) { |
| 210 if (is_obscured()) | 159 if (is_obscured()) |
| 211 return EdgeSelectionModel(direction); | 160 return EdgeSelectionModel(direction); |
| 212 | 161 |
| 213 base::i18n::BreakIterator iter(text(), base::i18n::BreakIterator::BREAK_WORD); | 162 base::i18n::BreakIterator iter(text(), base::i18n::BreakIterator::BREAK_WORD); |
| 214 bool success = iter.Init(); | 163 bool success = iter.Init(); |
| 215 DCHECK(success); | 164 DCHECK(success); |
| 216 if (!success) | 165 if (!success) |
| 217 return selection; | 166 return selection; |
| 218 | 167 |
| 219 SelectionModel end = EdgeSelectionModel(direction); | |
| 220 SelectionModel cur(selection); | 168 SelectionModel cur(selection); |
| 221 while (!cur.Equals(end)) { | 169 for (;;) { |
| 222 cur = AdjacentCharSelectionModel(cur, direction); | 170 cur = AdjacentCharSelectionModel(cur, direction); |
| 223 size_t caret = cur.caret_pos(); | 171 GSList* run = GetRunContainingCaret(cur); |
| 224 GSList* run = GetRunContainingPosition(caret); | 172 if (!run) |
| 225 DCHECK(run); | 173 break; |
| 226 PangoItem* item = reinterpret_cast<PangoLayoutRun*>(run->data)->item; | 174 PangoItem* item = reinterpret_cast<PangoLayoutRun*>(run->data)->item; |
| 227 size_t cursor = cur.selection_end(); | 175 size_t cursor = cur.caret_pos(); |
| 228 if (IsForwardMotion(direction, item) ? | 176 if (IsForwardMotion(direction, item) ? |
| 229 iter.IsEndOfWord(cursor) : iter.IsStartOfWord(cursor)) | 177 iter.IsEndOfWord(cursor) : iter.IsStartOfWord(cursor)) |
| 230 return cur; | 178 break; |
| 231 } | 179 } |
| 232 | 180 |
| 233 return end; | 181 return cur; |
| 234 } | |
| 235 | |
| 236 SelectionModel RenderTextLinux::EdgeSelectionModel( | |
| 237 VisualCursorDirection direction) { | |
| 238 if (direction == GetVisualDirectionOfLogicalEnd()) { | |
| 239 // Advance to the logical end of the text. | |
| 240 GSList* run = current_line_->runs; | |
| 241 if (direction == CURSOR_RIGHT) | |
| 242 run = g_slist_last(run); | |
| 243 if (run) { | |
| 244 PangoLayoutRun* end_run = reinterpret_cast<PangoLayoutRun*>(run->data); | |
| 245 PangoItem* item = end_run->item; | |
| 246 if (IsForwardMotion(direction, item)) { | |
| 247 size_t caret = LayoutIndexToTextIndex(LayoutIndexOfAdjacentGrapheme( | |
| 248 item->offset + item->length, CURSOR_BACKWARD)); | |
| 249 return SelectionModel(text().length(), caret, SelectionModel::TRAILING); | |
| 250 } else { | |
| 251 size_t caret = LayoutIndexToTextIndex(item->offset); | |
| 252 return SelectionModel(text().length(), caret, SelectionModel::LEADING); | |
| 253 } | |
| 254 } | |
| 255 } | |
| 256 return SelectionModel(0, 0, SelectionModel::LEADING); | |
| 257 } | 182 } |
| 258 | 183 |
| 259 void RenderTextLinux::SetSelectionModel(const SelectionModel& model) { | 184 void RenderTextLinux::SetSelectionModel(const SelectionModel& model) { |
| 260 if (GetSelectionStart() != model.selection_start() || | 185 if (selection() != model.selection()) |
| 261 GetCursorPosition() != model.selection_end()) { | |
| 262 selection_visual_bounds_.clear(); | 186 selection_visual_bounds_.clear(); |
| 263 } | |
| 264 | 187 |
| 265 RenderText::SetSelectionModel(model); | 188 RenderText::SetSelectionModel(model); |
| 266 } | 189 } |
| 267 | 190 |
| 268 std::vector<Rect> RenderTextLinux::GetSubstringBounds(size_t from, size_t to) { | 191 void RenderTextLinux::GetGlyphBounds(size_t index, |
| 269 DCHECK(from <= text().length()); | 192 ui::Range* xspan, |
| 270 DCHECK(to <= text().length()); | 193 int* height) { |
| 194 PangoRectangle pos; |
| 195 pango_layout_index_to_pos(layout_, TextIndexToLayoutIndex(index), &pos); |
| 196 *xspan = ui::Range(PANGO_PIXELS(pos.x), PANGO_PIXELS(pos.x + pos.width)); |
| 197 *height = PANGO_PIXELS(pos.height); |
| 198 } |
| 271 | 199 |
| 272 if (from == to) | 200 std::vector<Rect> RenderTextLinux::GetSubstringBounds(ui::Range range) { |
| 201 DCHECK_LE(range.GetMax(), text().length()); |
| 202 |
| 203 if (range.is_empty()) |
| 273 return std::vector<Rect>(); | 204 return std::vector<Rect>(); |
| 274 | 205 |
| 275 EnsureLayout(); | 206 EnsureLayout(); |
| 276 | 207 if (range == selection()) |
| 277 if (from == GetSelectionStart() && to == GetCursorPosition()) | |
| 278 return GetSelectionBounds(); | 208 return GetSelectionBounds(); |
| 279 else | 209 return CalculateSubstringBounds(range); |
| 280 return CalculateSubstringBounds(from, to); | |
| 281 } | 210 } |
| 282 | 211 |
| 283 bool RenderTextLinux::IsCursorablePosition(size_t position) { | 212 bool RenderTextLinux::IsCursorablePosition(size_t position) { |
| 284 if (position == 0 && text().empty()) | 213 if (position == 0 && text().empty()) |
| 285 return true; | 214 return true; |
| 286 | 215 |
| 287 EnsureLayout(); | 216 EnsureLayout(); |
| 288 ptrdiff_t offset = ui::UTF16IndexToOffset(text(), 0, position); | 217 ptrdiff_t offset = ui::UTF16IndexToOffset(text(), 0, position); |
| 289 return (offset < num_log_attrs_ && log_attrs_[offset].is_cursor_position); | 218 return (offset < num_log_attrs_ && log_attrs_[offset].is_cursor_position); |
| 290 } | 219 } |
| (...skipping 178 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 469 size_t RenderTextLinux::IndexOfAdjacentGrapheme( | 398 size_t RenderTextLinux::IndexOfAdjacentGrapheme( |
| 470 size_t index, | 399 size_t index, |
| 471 LogicalCursorDirection direction) { | 400 LogicalCursorDirection direction) { |
| 472 if (index > text().length()) | 401 if (index > text().length()) |
| 473 return text().length(); | 402 return text().length(); |
| 474 EnsureLayout(); | 403 EnsureLayout(); |
| 475 return LayoutIndexToTextIndex( | 404 return LayoutIndexToTextIndex( |
| 476 LayoutIndexOfAdjacentGrapheme(TextIndexToLayoutIndex(index), direction)); | 405 LayoutIndexOfAdjacentGrapheme(TextIndexToLayoutIndex(index), direction)); |
| 477 } | 406 } |
| 478 | 407 |
| 479 GSList* RenderTextLinux::GetRunContainingPosition(size_t position) const { | 408 GSList* RenderTextLinux::GetRunContainingCaret( |
| 409 const SelectionModel& caret) const { |
| 410 size_t position = TextIndexToLayoutIndex(caret.caret_pos()); |
| 411 LogicalCursorDirection affinity = caret.caret_affinity(); |
| 480 GSList* run = current_line_->runs; | 412 GSList* run = current_line_->runs; |
| 481 while (run) { | 413 while (run) { |
| 482 PangoItem* item = reinterpret_cast<PangoLayoutRun*>(run->data)->item; | 414 PangoItem* item = reinterpret_cast<PangoLayoutRun*>(run->data)->item; |
| 483 size_t run_start = LayoutIndexToTextIndex(item->offset); | 415 ui::Range item_range(item->offset, item->offset + item->length); |
| 484 size_t run_end = LayoutIndexToTextIndex(item->offset + item->length); | 416 if (RangeContainsCaret(item_range, position, affinity)) |
| 485 | |
| 486 if (position >= run_start && position < run_end) | |
| 487 return run; | 417 return run; |
| 488 run = run->next; | 418 run = run->next; |
| 489 } | 419 } |
| 490 return NULL; | 420 return NULL; |
| 491 } | 421 } |
| 492 | 422 |
| 493 size_t RenderTextLinux::LayoutIndexOfAdjacentGrapheme( | 423 size_t RenderTextLinux::LayoutIndexOfAdjacentGrapheme( |
| 494 size_t layout_index_of_current_grapheme, | 424 size_t layout_index_of_current_grapheme, |
| 495 LogicalCursorDirection direction) const { | 425 LogicalCursorDirection direction) const { |
| 496 const char* ch = layout_text_ + layout_index_of_current_grapheme; | 426 const char* ch = layout_text_ + layout_index_of_current_grapheme; |
| (...skipping 14 matching lines...) Expand all Loading... |
| 511 !log_attrs_[char_offset].is_cursor_position); | 441 !log_attrs_[char_offset].is_cursor_position); |
| 512 } | 442 } |
| 513 } | 443 } |
| 514 | 444 |
| 515 ch = g_utf8_offset_to_pointer(ch, char_offset - start_char_offset); | 445 ch = g_utf8_offset_to_pointer(ch, char_offset - start_char_offset); |
| 516 return static_cast<size_t>(ch - layout_text_); | 446 return static_cast<size_t>(ch - layout_text_); |
| 517 } | 447 } |
| 518 | 448 |
| 519 SelectionModel RenderTextLinux::FirstSelectionModelInsideRun( | 449 SelectionModel RenderTextLinux::FirstSelectionModelInsideRun( |
| 520 const PangoItem* item) const { | 450 const PangoItem* item) const { |
| 521 size_t caret = LayoutIndexToTextIndex(item->offset); | |
| 522 size_t cursor = LayoutIndexToTextIndex( | 451 size_t cursor = LayoutIndexToTextIndex( |
| 523 LayoutIndexOfAdjacentGrapheme(item->offset, CURSOR_FORWARD)); | 452 LayoutIndexOfAdjacentGrapheme(item->offset, CURSOR_FORWARD)); |
| 524 return SelectionModel(cursor, caret, SelectionModel::TRAILING); | 453 return SelectionModel(cursor, CURSOR_BACKWARD); |
| 525 } | 454 } |
| 526 | 455 |
| 527 SelectionModel RenderTextLinux::LastSelectionModelInsideRun( | 456 SelectionModel RenderTextLinux::LastSelectionModelInsideRun( |
| 528 const PangoItem* item) const { | 457 const PangoItem* item) const { |
| 529 size_t caret = LayoutIndexToTextIndex(LayoutIndexOfAdjacentGrapheme( | 458 size_t caret = LayoutIndexToTextIndex(LayoutIndexOfAdjacentGrapheme( |
| 530 item->offset + item->length, CURSOR_BACKWARD)); | 459 item->offset + item->length, CURSOR_BACKWARD)); |
| 531 return SelectionModel(caret, caret, SelectionModel::LEADING); | 460 return SelectionModel(caret, CURSOR_FORWARD); |
| 532 } | 461 } |
| 533 | 462 |
| 534 void RenderTextLinux::ResetLayout() { | 463 void RenderTextLinux::ResetLayout() { |
| 535 // set_cached_bounds_and_offset_valid(false) is done in RenderText for every | 464 // set_cached_bounds_and_offset_valid(false) is done in RenderText for every |
| 536 // operation that triggers ResetLayout(). | 465 // operation that triggers ResetLayout(). |
| 537 if (layout_) { | 466 if (layout_) { |
| 538 g_object_unref(layout_); | 467 g_object_unref(layout_); |
| 539 layout_ = NULL; | 468 layout_ = NULL; |
| 540 } | 469 } |
| 541 if (current_line_) { | 470 if (current_line_) { |
| (...skipping 22 matching lines...) Expand all Loading... |
| 564 } | 493 } |
| 565 | 494 |
| 566 size_t RenderTextLinux::LayoutIndexToTextIndex(size_t layout_index) const { | 495 size_t RenderTextLinux::LayoutIndexToTextIndex(size_t layout_index) const { |
| 567 // See |TextIndexToLayoutIndex()|. | 496 // See |TextIndexToLayoutIndex()|. |
| 568 DCHECK(layout_); | 497 DCHECK(layout_); |
| 569 const char* layout_pointer = layout_text_ + layout_index; | 498 const char* layout_pointer = layout_text_ + layout_index; |
| 570 long offset = g_utf8_pointer_to_offset(layout_text_, layout_pointer); | 499 long offset = g_utf8_pointer_to_offset(layout_text_, layout_pointer); |
| 571 return ui::UTF16OffsetToIndex(text(), 0, offset); | 500 return ui::UTF16OffsetToIndex(text(), 0, offset); |
| 572 } | 501 } |
| 573 | 502 |
| 574 std::vector<Rect> RenderTextLinux::CalculateSubstringBounds(size_t from, | 503 std::vector<Rect> RenderTextLinux::CalculateSubstringBounds(ui::Range range) { |
| 575 size_t to) { | |
| 576 int* ranges; | 504 int* ranges; |
| 577 int n_ranges; | 505 int n_ranges; |
| 578 size_t from_in_utf8 = TextIndexToLayoutIndex(from); | |
| 579 size_t to_in_utf8 = TextIndexToLayoutIndex(to); | |
| 580 pango_layout_line_get_x_ranges( | 506 pango_layout_line_get_x_ranges( |
| 581 current_line_, | 507 current_line_, |
| 582 std::min(from_in_utf8, to_in_utf8), | 508 TextIndexToLayoutIndex(range.GetMin()), |
| 583 std::max(from_in_utf8, to_in_utf8), | 509 TextIndexToLayoutIndex(range.GetMax()), |
| 584 &ranges, | 510 &ranges, |
| 585 &n_ranges); | 511 &n_ranges); |
| 586 | 512 |
| 587 int height; | 513 int height; |
| 588 pango_layout_get_pixel_size(layout_, NULL, &height); | 514 pango_layout_get_pixel_size(layout_, NULL, &height); |
| 589 | 515 |
| 590 int y = (display_rect().height() - height) / 2; | 516 int y = (display_rect().height() - height) / 2; |
| 591 | 517 |
| 592 std::vector<Rect> bounds; | 518 std::vector<Rect> bounds; |
| 593 for (int i = 0; i < n_ranges; ++i) { | 519 for (int i = 0; i < n_ranges; ++i) { |
| 594 int x = PANGO_PIXELS(ranges[2 * i]); | 520 int x = PANGO_PIXELS(ranges[2 * i]); |
| 595 int width = PANGO_PIXELS(ranges[2 * i + 1]) - x; | 521 int width = PANGO_PIXELS(ranges[2 * i + 1]) - x; |
| 596 Rect rect(x, y, width, height); | 522 Rect rect(x, y, width, height); |
| 597 rect.set_origin(ToViewPoint(rect.origin())); | 523 rect.set_origin(ToViewPoint(rect.origin())); |
| 598 bounds.push_back(rect); | 524 bounds.push_back(rect); |
| 599 } | 525 } |
| 600 g_free(ranges); | 526 g_free(ranges); |
| 601 return bounds; | 527 return bounds; |
| 602 } | 528 } |
| 603 | 529 |
| 604 std::vector<Rect> RenderTextLinux::GetSelectionBounds() { | 530 std::vector<Rect> RenderTextLinux::GetSelectionBounds() { |
| 605 if (selection_visual_bounds_.empty()) | 531 if (selection_visual_bounds_.empty()) |
| 606 selection_visual_bounds_ = | 532 selection_visual_bounds_ = CalculateSubstringBounds(selection()); |
| 607 CalculateSubstringBounds(GetSelectionStart(), GetCursorPosition()); | |
| 608 return selection_visual_bounds_; | 533 return selection_visual_bounds_; |
| 609 } | 534 } |
| 610 | 535 |
| 611 } // namespace gfx | 536 } // namespace gfx |
| OLD | NEW |