| 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> |
| 11 | 11 |
| 12 #include "base/i18n/break_iterator.h" | 12 #include "base/i18n/break_iterator.h" |
| 13 #include "base/logging.h" | 13 #include "base/logging.h" |
| 14 #include "third_party/skia/include/core/SkTypeface.h" | 14 #include "third_party/skia/include/core/SkTypeface.h" |
| 15 #include "ui/base/text/utf16_indexing.h" |
| 15 #include "ui/gfx/canvas_skia.h" | 16 #include "ui/gfx/canvas_skia.h" |
| 16 #include "ui/gfx/font.h" | 17 #include "ui/gfx/font.h" |
| 17 #include "ui/gfx/pango_util.h" | 18 #include "ui/gfx/pango_util.h" |
| 18 #include "unicode/uchar.h" | |
| 19 #include "unicode/ustring.h" | |
| 20 | 19 |
| 21 namespace gfx { | 20 namespace gfx { |
| 22 | 21 |
| 23 namespace { | 22 namespace { |
| 24 | 23 |
| 25 // Returns the preceding element in a GSList (O(n)). | 24 // Returns the preceding element in a GSList (O(n)). |
| 26 GSList* GSListPrevious(GSList* head, GSList* item) { | 25 GSList* GSListPrevious(GSList* head, GSList* item) { |
| 27 GSList* prev = NULL; | 26 GSList* prev = NULL; |
| 28 for (GSList* cur = head; cur != item; cur = cur->next) { | 27 for (GSList* cur = head; cur != item; cur = cur->next) { |
| 29 DCHECK(cur); | 28 DCHECK(cur); |
| (...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 106 size_t selection_end = caret_pos; | 105 size_t selection_end = caret_pos; |
| 107 if (trailing > 0) { | 106 if (trailing > 0) { |
| 108 const char* ch = g_utf8_offset_to_pointer(layout_text_ + caret_pos, | 107 const char* ch = g_utf8_offset_to_pointer(layout_text_ + caret_pos, |
| 109 trailing); | 108 trailing); |
| 110 DCHECK_GE(ch, layout_text_); | 109 DCHECK_GE(ch, layout_text_); |
| 111 DCHECK_LE(ch, layout_text_ + layout_text_len_); | 110 DCHECK_LE(ch, layout_text_ + layout_text_len_); |
| 112 selection_end = ch - layout_text_; | 111 selection_end = ch - layout_text_; |
| 113 } | 112 } |
| 114 | 113 |
| 115 return SelectionModel( | 114 return SelectionModel( |
| 116 Utf8IndexToUtf16Index(selection_end), | 115 LayoutIndexToTextIndex(selection_end), |
| 117 Utf8IndexToUtf16Index(caret_pos), | 116 LayoutIndexToTextIndex(caret_pos), |
| 118 trailing > 0 ? SelectionModel::TRAILING : SelectionModel::LEADING); | 117 trailing > 0 ? SelectionModel::TRAILING : SelectionModel::LEADING); |
| 119 } | 118 } |
| 120 | 119 |
| 121 Rect RenderTextLinux::GetCursorBounds(const SelectionModel& selection, | 120 Rect RenderTextLinux::GetCursorBounds(const SelectionModel& selection, |
| 122 bool insert_mode) { | 121 bool insert_mode) { |
| 123 EnsureLayout(); | 122 EnsureLayout(); |
| 124 | 123 |
| 125 size_t caret_pos = insert_mode ? selection.caret_pos() : | 124 size_t caret_pos = insert_mode ? selection.caret_pos() : |
| 126 selection.selection_end(); | 125 selection.selection_end(); |
| 127 PangoRectangle pos; | 126 PangoRectangle pos; |
| 128 pango_layout_index_to_pos(layout_, Utf16IndexToUtf8Index(caret_pos), &pos); | 127 pango_layout_index_to_pos(layout_, TextIndexToLayoutIndex(caret_pos), &pos); |
| 129 | 128 |
| 130 SelectionModel::CaretPlacement caret_placement = selection.caret_placement(); | 129 SelectionModel::CaretPlacement caret_placement = selection.caret_placement(); |
| 131 int x = pos.x; | 130 int x = pos.x; |
| 132 if ((insert_mode && caret_placement == SelectionModel::TRAILING) || | 131 if ((insert_mode && caret_placement == SelectionModel::TRAILING) || |
| 133 (!insert_mode && pos.width < 0)) | 132 (!insert_mode && pos.width < 0)) |
| 134 x += pos.width; | 133 x += pos.width; |
| 135 x = PANGO_PIXELS(x); | 134 x = PANGO_PIXELS(x); |
| 136 | 135 |
| 137 int h = std::min(display_rect().height(), PANGO_PIXELS(pos.height)); | 136 int h = std::min(display_rect().height(), PANGO_PIXELS(pos.height)); |
| 138 Rect bounds(x, (display_rect().height() - h) / 2, 0, h); | 137 Rect bounds(x, (display_rect().height() - h) / 2, 0, h); |
| (...skipping 26 matching lines...) Expand all Loading... |
| 165 SelectionModel RenderTextLinux::AdjacentCharSelectionModel( | 164 SelectionModel RenderTextLinux::AdjacentCharSelectionModel( |
| 166 const SelectionModel& selection, | 165 const SelectionModel& selection, |
| 167 VisualCursorDirection direction) { | 166 VisualCursorDirection direction) { |
| 168 size_t caret = selection.caret_pos(); | 167 size_t caret = selection.caret_pos(); |
| 169 SelectionModel::CaretPlacement caret_placement = selection.caret_placement(); | 168 SelectionModel::CaretPlacement caret_placement = selection.caret_placement(); |
| 170 GSList* run = GetRunContainingPosition(caret); | 169 GSList* run = GetRunContainingPosition(caret); |
| 171 DCHECK(run); | 170 DCHECK(run); |
| 172 | 171 |
| 173 PangoLayoutRun* layout_run = reinterpret_cast<PangoLayoutRun*>(run->data); | 172 PangoLayoutRun* layout_run = reinterpret_cast<PangoLayoutRun*>(run->data); |
| 174 PangoItem* item = layout_run->item; | 173 PangoItem* item = layout_run->item; |
| 175 size_t run_start = Utf8IndexToUtf16Index(item->offset); | 174 size_t run_start = LayoutIndexToTextIndex(item->offset); |
| 176 size_t run_end = Utf8IndexToUtf16Index(item->offset + item->length); | 175 size_t run_end = LayoutIndexToTextIndex(item->offset + item->length); |
| 177 | |
| 178 if (!IsForwardMotion(direction, item)) { | 176 if (!IsForwardMotion(direction, item)) { |
| 179 if (caret_placement == SelectionModel::TRAILING) | 177 if (caret_placement == SelectionModel::TRAILING) |
| 180 return SelectionModel(caret, caret, SelectionModel::LEADING); | 178 return SelectionModel(caret, caret, SelectionModel::LEADING); |
| 181 else if (caret > run_start) { | 179 else if (caret > run_start) { |
| 182 caret = IndexOfAdjacentGrapheme(caret, CURSOR_BACKWARD); | 180 caret = IndexOfAdjacentGrapheme(caret, CURSOR_BACKWARD); |
| 183 return SelectionModel(caret, caret, SelectionModel::LEADING); | 181 return SelectionModel(caret, caret, SelectionModel::LEADING); |
| 184 } | 182 } |
| 185 } else { | 183 } else { |
| 186 if (caret_placement == SelectionModel::LEADING) { | 184 if (caret_placement == SelectionModel::LEADING) { |
| 187 size_t cursor = IndexOfAdjacentGrapheme(caret, CURSOR_FORWARD); | 185 size_t cursor = IndexOfAdjacentGrapheme(caret, CURSOR_FORWARD); |
| (...skipping 14 matching lines...) Expand all Loading... |
| 202 return EdgeSelectionModel(direction); | 200 return EdgeSelectionModel(direction); |
| 203 | 201 |
| 204 item = reinterpret_cast<PangoLayoutRun*>(adjacent_run->data)->item; | 202 item = reinterpret_cast<PangoLayoutRun*>(adjacent_run->data)->item; |
| 205 return IsForwardMotion(direction, item) ? | 203 return IsForwardMotion(direction, item) ? |
| 206 FirstSelectionModelInsideRun(item) : LastSelectionModelInsideRun(item); | 204 FirstSelectionModelInsideRun(item) : LastSelectionModelInsideRun(item); |
| 207 } | 205 } |
| 208 | 206 |
| 209 SelectionModel RenderTextLinux::AdjacentWordSelectionModel( | 207 SelectionModel RenderTextLinux::AdjacentWordSelectionModel( |
| 210 const SelectionModel& selection, | 208 const SelectionModel& selection, |
| 211 VisualCursorDirection direction) { | 209 VisualCursorDirection direction) { |
| 210 if (is_obscured()) |
| 211 return EdgeSelectionModel(direction); |
| 212 |
| 212 base::i18n::BreakIterator iter(text(), base::i18n::BreakIterator::BREAK_WORD); | 213 base::i18n::BreakIterator iter(text(), base::i18n::BreakIterator::BREAK_WORD); |
| 213 bool success = iter.Init(); | 214 bool success = iter.Init(); |
| 214 DCHECK(success); | 215 DCHECK(success); |
| 215 if (!success) | 216 if (!success) |
| 216 return selection; | 217 return selection; |
| 217 | 218 |
| 218 SelectionModel end = EdgeSelectionModel(direction); | 219 SelectionModel end = EdgeSelectionModel(direction); |
| 219 SelectionModel cur(selection); | 220 SelectionModel cur(selection); |
| 220 while (!cur.Equals(end)) { | 221 while (!cur.Equals(end)) { |
| 221 cur = AdjacentCharSelectionModel(cur, direction); | 222 cur = AdjacentCharSelectionModel(cur, direction); |
| (...skipping 14 matching lines...) Expand all Loading... |
| 236 VisualCursorDirection direction) { | 237 VisualCursorDirection direction) { |
| 237 if (direction == GetVisualDirectionOfLogicalEnd()) { | 238 if (direction == GetVisualDirectionOfLogicalEnd()) { |
| 238 // Advance to the logical end of the text. | 239 // Advance to the logical end of the text. |
| 239 GSList* run = current_line_->runs; | 240 GSList* run = current_line_->runs; |
| 240 if (direction == CURSOR_RIGHT) | 241 if (direction == CURSOR_RIGHT) |
| 241 run = g_slist_last(run); | 242 run = g_slist_last(run); |
| 242 if (run) { | 243 if (run) { |
| 243 PangoLayoutRun* end_run = reinterpret_cast<PangoLayoutRun*>(run->data); | 244 PangoLayoutRun* end_run = reinterpret_cast<PangoLayoutRun*>(run->data); |
| 244 PangoItem* item = end_run->item; | 245 PangoItem* item = end_run->item; |
| 245 if (IsForwardMotion(direction, item)) { | 246 if (IsForwardMotion(direction, item)) { |
| 246 size_t caret = Utf8IndexToUtf16Index( | 247 size_t caret = LayoutIndexToTextIndex(LayoutIndexOfAdjacentGrapheme( |
| 247 Utf8IndexOfAdjacentGrapheme(item->offset + item->length, | 248 item->offset + item->length, CURSOR_BACKWARD)); |
| 248 CURSOR_BACKWARD)); | |
| 249 return SelectionModel(text().length(), caret, SelectionModel::TRAILING); | 249 return SelectionModel(text().length(), caret, SelectionModel::TRAILING); |
| 250 } else { | 250 } else { |
| 251 size_t caret = Utf8IndexToUtf16Index(item->offset); | 251 size_t caret = LayoutIndexToTextIndex(item->offset); |
| 252 return SelectionModel(text().length(), caret, SelectionModel::LEADING); | 252 return SelectionModel(text().length(), caret, SelectionModel::LEADING); |
| 253 } | 253 } |
| 254 } | 254 } |
| 255 } | 255 } |
| 256 return SelectionModel(0, 0, SelectionModel::LEADING); | 256 return SelectionModel(0, 0, SelectionModel::LEADING); |
| 257 } | 257 } |
| 258 | 258 |
| 259 void RenderTextLinux::SetSelectionModel(const SelectionModel& model) { | 259 void RenderTextLinux::SetSelectionModel(const SelectionModel& model) { |
| 260 if (GetSelectionStart() != model.selection_start() || | 260 if (GetSelectionStart() != model.selection_start() || |
| 261 GetCursorPosition() != model.selection_end()) { | 261 GetCursorPosition() != model.selection_end()) { |
| (...skipping 16 matching lines...) Expand all Loading... |
| 278 return GetSelectionBounds(); | 278 return GetSelectionBounds(); |
| 279 else | 279 else |
| 280 return CalculateSubstringBounds(from, to); | 280 return CalculateSubstringBounds(from, to); |
| 281 } | 281 } |
| 282 | 282 |
| 283 bool RenderTextLinux::IsCursorablePosition(size_t position) { | 283 bool RenderTextLinux::IsCursorablePosition(size_t position) { |
| 284 if (position == 0 && text().empty()) | 284 if (position == 0 && text().empty()) |
| 285 return true; | 285 return true; |
| 286 | 286 |
| 287 EnsureLayout(); | 287 EnsureLayout(); |
| 288 return (position < static_cast<size_t>(num_log_attrs_) && | 288 ptrdiff_t offset = ui::UTF16IndexToOffset(text(), 0, position); |
| 289 log_attrs_[position].is_cursor_position); | 289 return (offset < num_log_attrs_ && log_attrs_[offset].is_cursor_position); |
| 290 } | 290 } |
| 291 | 291 |
| 292 void RenderTextLinux::UpdateLayout() { | 292 void RenderTextLinux::UpdateLayout() { |
| 293 ResetLayout(); | 293 ResetLayout(); |
| 294 } | 294 } |
| 295 | 295 |
| 296 void RenderTextLinux::EnsureLayout() { | 296 void RenderTextLinux::EnsureLayout() { |
| 297 if (layout_ == NULL) { | 297 if (layout_ == NULL) { |
| 298 CanvasSkia canvas(display_rect().size(), false); | 298 CanvasSkia canvas(display_rect().size(), false); |
| 299 skia::ScopedPlatformPaint scoped_platform_paint(canvas.sk_canvas()); | 299 skia::ScopedPlatformPaint scoped_platform_paint(canvas.sk_canvas()); |
| 300 cairo_t* cr = scoped_platform_paint.GetPlatformSurface(); | 300 cairo_t* cr = scoped_platform_paint.GetPlatformSurface(); |
| 301 | 301 |
| 302 layout_ = pango_cairo_create_layout(cr); | 302 layout_ = pango_cairo_create_layout(cr); |
| 303 SetupPangoLayoutWithFontDescription( | 303 SetupPangoLayoutWithFontDescription( |
| 304 layout_, | 304 layout_, |
| 305 text(), | 305 GetDisplayText(), |
| 306 font_list().GetFontDescriptionString(), | 306 font_list().GetFontDescriptionString(), |
| 307 display_rect().width(), | 307 display_rect().width(), |
| 308 base::i18n::GetFirstStrongCharacterDirection(text()), | 308 base::i18n::GetFirstStrongCharacterDirection(text()), |
| 309 CanvasSkia::DefaultCanvasTextAlignment()); | 309 CanvasSkia::DefaultCanvasTextAlignment()); |
| 310 | 310 |
| 311 // No width set so that the x-axis position is relative to the start of the | 311 // No width set so that the x-axis position is relative to the start of the |
| 312 // text. ToViewPoint and ToTextPoint take care of the position conversion | 312 // text. ToViewPoint and ToTextPoint take care of the position conversion |
| 313 // between text space and view spaces. | 313 // between text space and view spaces. |
| 314 pango_layout_set_width(layout_, -1); | 314 pango_layout_set_width(layout_, -1); |
| 315 // TODO(xji): If RenderText will be used for displaying purpose, such as | 315 // TODO(xji): If RenderText will be used for displaying purpose, such as |
| 316 // label, we will need to remove the single-line-mode setting. | 316 // label, we will need to remove the single-line-mode setting. |
| 317 pango_layout_set_single_paragraph_mode(layout_, true); | 317 pango_layout_set_single_paragraph_mode(layout_, true); |
| 318 |
| 319 // These are used by SetupPangoAttributes. |
| 320 layout_text_ = pango_layout_get_text(layout_); |
| 321 layout_text_len_ = strlen(layout_text_); |
| 322 |
| 318 SetupPangoAttributes(layout_); | 323 SetupPangoAttributes(layout_); |
| 319 | 324 |
| 320 current_line_ = pango_layout_get_line_readonly(layout_, 0); | 325 current_line_ = pango_layout_get_line_readonly(layout_, 0); |
| 321 pango_layout_line_ref(current_line_); | 326 pango_layout_line_ref(current_line_); |
| 322 | 327 |
| 323 pango_layout_get_log_attrs(layout_, &log_attrs_, &num_log_attrs_); | 328 pango_layout_get_log_attrs(layout_, &log_attrs_, &num_log_attrs_); |
| 324 | |
| 325 layout_text_ = pango_layout_get_text(layout_); | |
| 326 layout_text_len_ = strlen(layout_text_); | |
| 327 } | 329 } |
| 328 } | 330 } |
| 329 | 331 |
| 330 void RenderTextLinux::SetupPangoAttributes(PangoLayout* layout) { | 332 void RenderTextLinux::SetupPangoAttributes(PangoLayout* layout) { |
| 331 PangoAttrList* attrs = pango_attr_list_new(); | 333 PangoAttrList* attrs = pango_attr_list_new(); |
| 332 | 334 |
| 333 int default_font_style = font_list().GetFontStyle(); | 335 int default_font_style = font_list().GetFontStyle(); |
| 334 for (StyleRanges::const_iterator i = style_ranges().begin(); | 336 for (StyleRanges::const_iterator i = style_ranges().begin(); |
| 335 i < style_ranges().end(); ++i) { | 337 i < style_ranges().end(); ++i) { |
| 336 // In Pango, different fonts means different runs, and it breaks Arabic | 338 // In Pango, different fonts means different runs, and it breaks Arabic |
| 337 // shaping across run boundaries. So, set font only when it is different | 339 // shaping across run boundaries. So, set font only when it is different |
| 338 // from the default font. | 340 // from the default font. |
| 339 // TODO(xji): We'll eventually need to split up StyleRange into components | 341 // TODO(xji): We'll eventually need to split up StyleRange into components |
| 340 // (ColorRange, FontRange, etc.) so that we can combine adjacent ranges | 342 // (ColorRange, FontRange, etc.) so that we can combine adjacent ranges |
| 341 // with the same Fonts (to avoid unnecessarily splitting up runs). | 343 // with the same Fonts (to avoid unnecessarily splitting up runs). |
| 342 if (i->font_style != default_font_style) { | 344 if (i->font_style != default_font_style) { |
| 343 FontList derived_font_list = font_list().DeriveFontList(i->font_style); | 345 FontList derived_font_list = font_list().DeriveFontList(i->font_style); |
| 344 PangoFontDescription* desc = pango_font_description_from_string( | 346 PangoFontDescription* desc = pango_font_description_from_string( |
| 345 derived_font_list.GetFontDescriptionString().c_str()); | 347 derived_font_list.GetFontDescriptionString().c_str()); |
| 346 | 348 |
| 347 PangoAttribute* pango_attr = pango_attr_font_desc_new(desc); | 349 PangoAttribute* pango_attr = pango_attr_font_desc_new(desc); |
| 348 pango_attr->start_index = Utf16IndexToUtf8Index(i->range.start()); | 350 pango_attr->start_index = TextIndexToLayoutIndex(i->range.start()); |
| 349 pango_attr->end_index = Utf16IndexToUtf8Index(i->range.end()); | 351 pango_attr->end_index = TextIndexToLayoutIndex(i->range.end()); |
| 350 pango_attr_list_insert(attrs, pango_attr); | 352 pango_attr_list_insert(attrs, pango_attr); |
| 351 pango_font_description_free(desc); | 353 pango_font_description_free(desc); |
| 352 } | 354 } |
| 353 } | 355 } |
| 354 | 356 |
| 355 pango_layout_set_attributes(layout, attrs); | 357 pango_layout_set_attributes(layout, attrs); |
| 356 pango_attr_list_unref(attrs); | 358 pango_attr_list_unref(attrs); |
| 357 } | 359 } |
| 358 | 360 |
| 359 void RenderTextLinux::DrawVisualText(Canvas* canvas) { | 361 void RenderTextLinux::DrawVisualText(Canvas* canvas) { |
| 360 DCHECK(layout_); | 362 DCHECK(layout_); |
| 361 | 363 |
| 362 Point offset(GetOriginForSkiaDrawing()); | 364 Point offset(GetOriginForSkiaDrawing()); |
| 363 SkScalar x = SkIntToScalar(offset.x()); | 365 SkScalar x = SkIntToScalar(offset.x()); |
| 364 SkScalar y = SkIntToScalar(offset.y()); | 366 SkScalar y = SkIntToScalar(offset.y()); |
| 365 | 367 |
| 366 std::vector<SkPoint> pos; | 368 std::vector<SkPoint> pos; |
| 367 std::vector<uint16> glyphs; | 369 std::vector<uint16> glyphs; |
| 368 | 370 |
| 369 StyleRanges styles(style_ranges()); | 371 StyleRanges styles(style_ranges()); |
| 370 ApplyCompositionAndSelectionStyles(&styles); | 372 ApplyCompositionAndSelectionStyles(&styles); |
| 371 | 373 |
| 372 // Pre-calculate UTF8 indices from UTF16 indices. | 374 // Pre-calculate UTF8 indices from UTF16 indices. |
| 373 // TODO(asvitkine): Can we cache these? | 375 // TODO(asvitkine): Can we cache these? |
| 374 std::vector<ui::Range> style_ranges_utf8; | 376 std::vector<ui::Range> style_ranges_utf8; |
| 375 style_ranges_utf8.reserve(styles.size()); | 377 style_ranges_utf8.reserve(styles.size()); |
| 376 size_t start_index = 0; | 378 size_t start_index = 0; |
| 377 for (size_t i = 0; i < styles.size(); ++i) { | 379 for (size_t i = 0; i < styles.size(); ++i) { |
| 378 size_t end_index = Utf16IndexToUtf8Index(styles[i].range.end()); | 380 size_t end_index = TextIndexToLayoutIndex(styles[i].range.end()); |
| 379 style_ranges_utf8.push_back(ui::Range(start_index, end_index)); | 381 style_ranges_utf8.push_back(ui::Range(start_index, end_index)); |
| 380 start_index = end_index; | 382 start_index = end_index; |
| 381 } | 383 } |
| 382 | 384 |
| 383 internal::SkiaTextRenderer renderer(canvas); | 385 internal::SkiaTextRenderer renderer(canvas); |
| 384 ApplyFadeEffects(&renderer); | 386 ApplyFadeEffects(&renderer); |
| 385 renderer.SetFontSmoothingSettings(true, !background_is_transparent()); | 387 renderer.SetFontSmoothingSettings(true, !background_is_transparent()); |
| 386 | 388 |
| 387 for (GSList* it = current_line_->runs; it; it = it->next) { | 389 for (GSList* it = current_line_->runs; it; it = it->next) { |
| 388 PangoLayoutRun* run = reinterpret_cast<PangoLayoutRun*>(it->data); | 390 PangoLayoutRun* run = reinterpret_cast<PangoLayoutRun*>(it->data); |
| (...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 463 x = glyph_x; | 465 x = glyph_x; |
| 464 } | 466 } |
| 465 } | 467 } |
| 466 | 468 |
| 467 size_t RenderTextLinux::IndexOfAdjacentGrapheme( | 469 size_t RenderTextLinux::IndexOfAdjacentGrapheme( |
| 468 size_t index, | 470 size_t index, |
| 469 LogicalCursorDirection direction) { | 471 LogicalCursorDirection direction) { |
| 470 if (index > text().length()) | 472 if (index > text().length()) |
| 471 return text().length(); | 473 return text().length(); |
| 472 EnsureLayout(); | 474 EnsureLayout(); |
| 473 return Utf8IndexToUtf16Index( | 475 return LayoutIndexToTextIndex( |
| 474 Utf8IndexOfAdjacentGrapheme(Utf16IndexToUtf8Index(index), direction)); | 476 LayoutIndexOfAdjacentGrapheme(TextIndexToLayoutIndex(index), direction)); |
| 475 } | 477 } |
| 476 | 478 |
| 477 GSList* RenderTextLinux::GetRunContainingPosition(size_t position) const { | 479 GSList* RenderTextLinux::GetRunContainingPosition(size_t position) const { |
| 478 GSList* run = current_line_->runs; | 480 GSList* run = current_line_->runs; |
| 479 while (run) { | 481 while (run) { |
| 480 PangoItem* item = reinterpret_cast<PangoLayoutRun*>(run->data)->item; | 482 PangoItem* item = reinterpret_cast<PangoLayoutRun*>(run->data)->item; |
| 481 size_t run_start = Utf8IndexToUtf16Index(item->offset); | 483 size_t run_start = LayoutIndexToTextIndex(item->offset); |
| 482 size_t run_end = Utf8IndexToUtf16Index(item->offset + item->length); | 484 size_t run_end = LayoutIndexToTextIndex(item->offset + item->length); |
| 483 | 485 |
| 484 if (position >= run_start && position < run_end) | 486 if (position >= run_start && position < run_end) |
| 485 return run; | 487 return run; |
| 486 run = run->next; | 488 run = run->next; |
| 487 } | 489 } |
| 488 return NULL; | 490 return NULL; |
| 489 } | 491 } |
| 490 | 492 |
| 491 size_t RenderTextLinux::Utf8IndexOfAdjacentGrapheme( | 493 size_t RenderTextLinux::LayoutIndexOfAdjacentGrapheme( |
| 492 size_t utf8_index_of_current_grapheme, | 494 size_t layout_index_of_current_grapheme, |
| 493 LogicalCursorDirection direction) const { | 495 LogicalCursorDirection direction) const { |
| 494 const char* ch = layout_text_ + utf8_index_of_current_grapheme; | 496 const char* ch = layout_text_ + layout_index_of_current_grapheme; |
| 495 int char_offset = static_cast<int>(g_utf8_pointer_to_offset(layout_text_, | 497 int char_offset = static_cast<int>(g_utf8_pointer_to_offset(layout_text_, |
| 496 ch)); | 498 ch)); |
| 497 int start_char_offset = char_offset; | 499 int start_char_offset = char_offset; |
| 498 if (direction == CURSOR_BACKWARD) { | 500 if (direction == CURSOR_BACKWARD) { |
| 499 if (char_offset > 0) { | 501 if (char_offset > 0) { |
| 500 do { | 502 do { |
| 501 --char_offset; | 503 --char_offset; |
| 502 } while (char_offset > 0 && !log_attrs_[char_offset].is_cursor_position); | 504 } while (char_offset > 0 && !log_attrs_[char_offset].is_cursor_position); |
| 503 } | 505 } |
| 504 } else { // direction == CURSOR_FORWARD | 506 } else { // direction == CURSOR_FORWARD |
| 505 if (char_offset < num_log_attrs_ - 1) { | 507 if (char_offset < num_log_attrs_ - 1) { |
| 506 do { | 508 do { |
| 507 ++char_offset; | 509 ++char_offset; |
| 508 } while (char_offset < num_log_attrs_ - 1 && | 510 } while (char_offset < num_log_attrs_ - 1 && |
| 509 !log_attrs_[char_offset].is_cursor_position); | 511 !log_attrs_[char_offset].is_cursor_position); |
| 510 } | 512 } |
| 511 } | 513 } |
| 512 | 514 |
| 513 ch = g_utf8_offset_to_pointer(ch, char_offset - start_char_offset); | 515 ch = g_utf8_offset_to_pointer(ch, char_offset - start_char_offset); |
| 514 return static_cast<size_t>(ch - layout_text_); | 516 return static_cast<size_t>(ch - layout_text_); |
| 515 } | 517 } |
| 516 | 518 |
| 517 SelectionModel RenderTextLinux::FirstSelectionModelInsideRun( | 519 SelectionModel RenderTextLinux::FirstSelectionModelInsideRun( |
| 518 const PangoItem* item) const { | 520 const PangoItem* item) const { |
| 519 size_t caret = Utf8IndexToUtf16Index(item->offset); | 521 size_t caret = LayoutIndexToTextIndex(item->offset); |
| 520 size_t cursor = Utf8IndexToUtf16Index( | 522 size_t cursor = LayoutIndexToTextIndex( |
| 521 Utf8IndexOfAdjacentGrapheme(item->offset, CURSOR_FORWARD)); | 523 LayoutIndexOfAdjacentGrapheme(item->offset, CURSOR_FORWARD)); |
| 522 return SelectionModel(cursor, caret, SelectionModel::TRAILING); | 524 return SelectionModel(cursor, caret, SelectionModel::TRAILING); |
| 523 } | 525 } |
| 524 | 526 |
| 525 SelectionModel RenderTextLinux::LastSelectionModelInsideRun( | 527 SelectionModel RenderTextLinux::LastSelectionModelInsideRun( |
| 526 const PangoItem* item) const { | 528 const PangoItem* item) const { |
| 527 size_t caret = Utf8IndexToUtf16Index(Utf8IndexOfAdjacentGrapheme( | 529 size_t caret = LayoutIndexToTextIndex(LayoutIndexOfAdjacentGrapheme( |
| 528 item->offset + item->length, CURSOR_BACKWARD)); | 530 item->offset + item->length, CURSOR_BACKWARD)); |
| 529 return SelectionModel(caret, caret, SelectionModel::LEADING); | 531 return SelectionModel(caret, caret, SelectionModel::LEADING); |
| 530 } | 532 } |
| 531 | 533 |
| 532 void RenderTextLinux::ResetLayout() { | 534 void RenderTextLinux::ResetLayout() { |
| 533 // set_cached_bounds_and_offset_valid(false) is done in RenderText for every | 535 // set_cached_bounds_and_offset_valid(false) is done in RenderText for every |
| 534 // operation that triggers ResetLayout(). | 536 // operation that triggers ResetLayout(). |
| 535 if (layout_) { | 537 if (layout_) { |
| 536 g_object_unref(layout_); | 538 g_object_unref(layout_); |
| 537 layout_ = NULL; | 539 layout_ = NULL; |
| 538 } | 540 } |
| 539 if (current_line_) { | 541 if (current_line_) { |
| 540 pango_layout_line_unref(current_line_); | 542 pango_layout_line_unref(current_line_); |
| 541 current_line_ = NULL; | 543 current_line_ = NULL; |
| 542 } | 544 } |
| 543 if (log_attrs_) { | 545 if (log_attrs_) { |
| 544 g_free(log_attrs_); | 546 g_free(log_attrs_); |
| 545 log_attrs_ = NULL; | 547 log_attrs_ = NULL; |
| 546 num_log_attrs_ = 0; | 548 num_log_attrs_ = 0; |
| 547 } | 549 } |
| 548 if (!selection_visual_bounds_.empty()) | 550 if (!selection_visual_bounds_.empty()) |
| 549 selection_visual_bounds_.clear(); | 551 selection_visual_bounds_.clear(); |
| 550 layout_text_ = NULL; | 552 layout_text_ = NULL; |
| 551 layout_text_len_ = 0; | 553 layout_text_len_ = 0; |
| 552 } | 554 } |
| 553 | 555 |
| 554 size_t RenderTextLinux::Utf16IndexToUtf8Index(size_t index) const { | 556 size_t RenderTextLinux::TextIndexToLayoutIndex(size_t text_index) const { |
| 555 int32_t utf8_index = 0; | 557 // If the text is obscured then |layout_text_| is not the same as |text()|, |
| 556 UErrorCode ec = U_ZERO_ERROR; | 558 // but whether or not the text is obscured, the character (code point) offset |
| 557 u_strToUTF8(NULL, 0, &utf8_index, text().data(), index, &ec); | 559 // in |layout_text_| is the same as that in |text()|. |
| 558 // Even given a destination buffer as NULL and destination capacity as 0, | 560 DCHECK(layout_); |
| 559 // if the output length is equal to or greater than the capacity, then the | 561 ptrdiff_t offset = ui::UTF16IndexToOffset(text(), 0, text_index); |
| 560 // UErrorCode is set to U_STRING_NOT_TERMINATED_WARNING or | 562 const char* layout_pointer = g_utf8_offset_to_pointer(layout_text_, offset); |
| 561 // U_BUFFER_OVERFLOW_ERROR respectively. | 563 return (layout_pointer - layout_text_); |
| 562 // Please refer to | |
| 563 // http://userguide.icu-project.org/strings#TOC-Using-C-Strings:-NUL-Terminate
d-vs | |
| 564 // for detail (search for "Note that" below "Preflighting"). | |
| 565 DCHECK(ec == U_BUFFER_OVERFLOW_ERROR || | |
| 566 ec == U_STRING_NOT_TERMINATED_WARNING); | |
| 567 return utf8_index; | |
| 568 } | 564 } |
| 569 | 565 |
| 570 size_t RenderTextLinux::Utf8IndexToUtf16Index(size_t index) const { | 566 size_t RenderTextLinux::LayoutIndexToTextIndex(size_t layout_index) const { |
| 571 int32_t utf16_index = 0; | 567 // See |TextIndexToLayoutIndex()|. |
| 572 UErrorCode ec = U_ZERO_ERROR; | 568 DCHECK(layout_); |
| 573 u_strFromUTF8(NULL, 0, &utf16_index, layout_text_, index, &ec); | 569 const char* layout_pointer = layout_text_ + layout_index; |
| 574 DCHECK(ec == U_BUFFER_OVERFLOW_ERROR || | 570 long offset = g_utf8_pointer_to_offset(layout_text_, layout_pointer); |
| 575 ec == U_STRING_NOT_TERMINATED_WARNING); | 571 return ui::UTF16OffsetToIndex(text(), 0, offset); |
| 576 return utf16_index; | |
| 577 } | 572 } |
| 578 | 573 |
| 579 std::vector<Rect> RenderTextLinux::CalculateSubstringBounds(size_t from, | 574 std::vector<Rect> RenderTextLinux::CalculateSubstringBounds(size_t from, |
| 580 size_t to) { | 575 size_t to) { |
| 581 int* ranges; | 576 int* ranges; |
| 582 int n_ranges; | 577 int n_ranges; |
| 583 size_t from_in_utf8 = Utf16IndexToUtf8Index(from); | 578 size_t from_in_utf8 = TextIndexToLayoutIndex(from); |
| 584 size_t to_in_utf8 = Utf16IndexToUtf8Index(to); | 579 size_t to_in_utf8 = TextIndexToLayoutIndex(to); |
| 585 pango_layout_line_get_x_ranges( | 580 pango_layout_line_get_x_ranges( |
| 586 current_line_, | 581 current_line_, |
| 587 std::min(from_in_utf8, to_in_utf8), | 582 std::min(from_in_utf8, to_in_utf8), |
| 588 std::max(from_in_utf8, to_in_utf8), | 583 std::max(from_in_utf8, to_in_utf8), |
| 589 &ranges, | 584 &ranges, |
| 590 &n_ranges); | 585 &n_ranges); |
| 591 | 586 |
| 592 int height; | 587 int height; |
| 593 pango_layout_get_pixel_size(layout_, NULL, &height); | 588 pango_layout_get_pixel_size(layout_, NULL, &height); |
| 594 | 589 |
| (...skipping 12 matching lines...) Expand all Loading... |
| 607 } | 602 } |
| 608 | 603 |
| 609 std::vector<Rect> RenderTextLinux::GetSelectionBounds() { | 604 std::vector<Rect> RenderTextLinux::GetSelectionBounds() { |
| 610 if (selection_visual_bounds_.empty()) | 605 if (selection_visual_bounds_.empty()) |
| 611 selection_visual_bounds_ = | 606 selection_visual_bounds_ = |
| 612 CalculateSubstringBounds(GetSelectionStart(), GetCursorPosition()); | 607 CalculateSubstringBounds(GetSelectionStart(), GetCursorPosition()); |
| 613 return selection_visual_bounds_; | 608 return selection_visual_bounds_; |
| 614 } | 609 } |
| 615 | 610 |
| 616 } // namespace gfx | 611 } // namespace gfx |
| OLD | NEW |