Chromium Code Reviews| 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 "base/utf_offset_string_conversions.h" | |
| 14 #include "third_party/skia/include/core/SkTypeface.h" | 15 #include "third_party/skia/include/core/SkTypeface.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 = Utf16IndexToOffset(text(), 0, position); |
|
msw
2012/03/01 22:48:54
Why not use TextIndexToLayoutIndex here?
benrg
2012/03/02 00:06:08
log_attrs_ has one entry per code point, not per U
| |
| 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 | 387 |
| 386 for (GSList* it = current_line_->runs; it; it = it->next) { | 388 for (GSList* it = current_line_->runs; it; it = it->next) { |
| 387 PangoLayoutRun* run = reinterpret_cast<PangoLayoutRun*>(it->data); | 389 PangoLayoutRun* run = reinterpret_cast<PangoLayoutRun*>(it->data); |
| 388 int glyph_count = run->glyphs->num_glyphs; | 390 int glyph_count = run->glyphs->num_glyphs; |
| (...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 462 x = glyph_x; | 464 x = glyph_x; |
| 463 } | 465 } |
| 464 } | 466 } |
| 465 | 467 |
| 466 size_t RenderTextLinux::IndexOfAdjacentGrapheme( | 468 size_t RenderTextLinux::IndexOfAdjacentGrapheme( |
| 467 size_t index, | 469 size_t index, |
| 468 LogicalCursorDirection direction) { | 470 LogicalCursorDirection direction) { |
| 469 if (index > text().length()) | 471 if (index > text().length()) |
| 470 return text().length(); | 472 return text().length(); |
| 471 EnsureLayout(); | 473 EnsureLayout(); |
| 472 return Utf8IndexToUtf16Index( | 474 return LayoutIndexToTextIndex( |
| 473 Utf8IndexOfAdjacentGrapheme(Utf16IndexToUtf8Index(index), direction)); | 475 LayoutIndexOfAdjacentGrapheme(TextIndexToLayoutIndex(index), direction)); |
| 474 } | 476 } |
| 475 | 477 |
| 476 GSList* RenderTextLinux::GetRunContainingPosition(size_t position) const { | 478 GSList* RenderTextLinux::GetRunContainingPosition(size_t position) const { |
| 477 GSList* run = current_line_->runs; | 479 GSList* run = current_line_->runs; |
| 478 while (run) { | 480 while (run) { |
| 479 PangoItem* item = reinterpret_cast<PangoLayoutRun*>(run->data)->item; | 481 PangoItem* item = reinterpret_cast<PangoLayoutRun*>(run->data)->item; |
| 480 size_t run_start = Utf8IndexToUtf16Index(item->offset); | 482 size_t run_start = LayoutIndexToTextIndex(item->offset); |
| 481 size_t run_end = Utf8IndexToUtf16Index(item->offset + item->length); | 483 size_t run_end = LayoutIndexToTextIndex(item->offset + item->length); |
| 482 | 484 |
| 483 if (position >= run_start && position < run_end) | 485 if (position >= run_start && position < run_end) |
| 484 return run; | 486 return run; |
| 485 run = run->next; | 487 run = run->next; |
| 486 } | 488 } |
| 487 return NULL; | 489 return NULL; |
| 488 } | 490 } |
| 489 | 491 |
| 490 size_t RenderTextLinux::Utf8IndexOfAdjacentGrapheme( | 492 size_t RenderTextLinux::LayoutIndexOfAdjacentGrapheme( |
| 491 size_t utf8_index_of_current_grapheme, | 493 size_t layout_index_of_current_grapheme, |
| 492 LogicalCursorDirection direction) const { | 494 LogicalCursorDirection direction) const { |
| 493 const char* ch = layout_text_ + utf8_index_of_current_grapheme; | 495 const char* ch = layout_text_ + layout_index_of_current_grapheme; |
| 494 int char_offset = static_cast<int>(g_utf8_pointer_to_offset(layout_text_, | 496 int char_offset = static_cast<int>(g_utf8_pointer_to_offset(layout_text_, |
| 495 ch)); | 497 ch)); |
| 496 int start_char_offset = char_offset; | 498 int start_char_offset = char_offset; |
| 497 if (direction == CURSOR_BACKWARD) { | 499 if (direction == CURSOR_BACKWARD) { |
| 498 if (char_offset > 0) { | 500 if (char_offset > 0) { |
| 499 do { | 501 do { |
| 500 --char_offset; | 502 --char_offset; |
| 501 } while (char_offset > 0 && !log_attrs_[char_offset].is_cursor_position); | 503 } while (char_offset > 0 && !log_attrs_[char_offset].is_cursor_position); |
| 502 } | 504 } |
| 503 } else { // direction == CURSOR_FORWARD | 505 } else { // direction == CURSOR_FORWARD |
| 504 if (char_offset < num_log_attrs_ - 1) { | 506 if (char_offset < num_log_attrs_ - 1) { |
| 505 do { | 507 do { |
| 506 ++char_offset; | 508 ++char_offset; |
| 507 } while (char_offset < num_log_attrs_ - 1 && | 509 } while (char_offset < num_log_attrs_ - 1 && |
| 508 !log_attrs_[char_offset].is_cursor_position); | 510 !log_attrs_[char_offset].is_cursor_position); |
| 509 } | 511 } |
| 510 } | 512 } |
| 511 | 513 |
| 512 ch = g_utf8_offset_to_pointer(ch, char_offset - start_char_offset); | 514 ch = g_utf8_offset_to_pointer(ch, char_offset - start_char_offset); |
| 513 return static_cast<size_t>(ch - layout_text_); | 515 return static_cast<size_t>(ch - layout_text_); |
| 514 } | 516 } |
| 515 | 517 |
| 516 SelectionModel RenderTextLinux::FirstSelectionModelInsideRun( | 518 SelectionModel RenderTextLinux::FirstSelectionModelInsideRun( |
| 517 const PangoItem* item) const { | 519 const PangoItem* item) const { |
| 518 size_t caret = Utf8IndexToUtf16Index(item->offset); | 520 size_t caret = LayoutIndexToTextIndex(item->offset); |
| 519 size_t cursor = Utf8IndexToUtf16Index( | 521 size_t cursor = LayoutIndexToTextIndex( |
| 520 Utf8IndexOfAdjacentGrapheme(item->offset, CURSOR_FORWARD)); | 522 LayoutIndexOfAdjacentGrapheme(item->offset, CURSOR_FORWARD)); |
| 521 return SelectionModel(cursor, caret, SelectionModel::TRAILING); | 523 return SelectionModel(cursor, caret, SelectionModel::TRAILING); |
| 522 } | 524 } |
| 523 | 525 |
| 524 SelectionModel RenderTextLinux::LastSelectionModelInsideRun( | 526 SelectionModel RenderTextLinux::LastSelectionModelInsideRun( |
| 525 const PangoItem* item) const { | 527 const PangoItem* item) const { |
| 526 size_t caret = Utf8IndexToUtf16Index(Utf8IndexOfAdjacentGrapheme( | 528 size_t caret = LayoutIndexToTextIndex(LayoutIndexOfAdjacentGrapheme( |
| 527 item->offset + item->length, CURSOR_BACKWARD)); | 529 item->offset + item->length, CURSOR_BACKWARD)); |
| 528 return SelectionModel(caret, caret, SelectionModel::LEADING); | 530 return SelectionModel(caret, caret, SelectionModel::LEADING); |
| 529 } | 531 } |
| 530 | 532 |
| 531 void RenderTextLinux::ResetLayout() { | 533 void RenderTextLinux::ResetLayout() { |
| 532 // set_cached_bounds_and_offset_valid(false) is done in RenderText for every | 534 // set_cached_bounds_and_offset_valid(false) is done in RenderText for every |
| 533 // operation that triggers ResetLayout(). | 535 // operation that triggers ResetLayout(). |
| 534 if (layout_) { | 536 if (layout_) { |
| 535 g_object_unref(layout_); | 537 g_object_unref(layout_); |
| 536 layout_ = NULL; | 538 layout_ = NULL; |
| 537 } | 539 } |
| 538 if (current_line_) { | 540 if (current_line_) { |
| 539 pango_layout_line_unref(current_line_); | 541 pango_layout_line_unref(current_line_); |
| 540 current_line_ = NULL; | 542 current_line_ = NULL; |
| 541 } | 543 } |
| 542 if (log_attrs_) { | 544 if (log_attrs_) { |
| 543 g_free(log_attrs_); | 545 g_free(log_attrs_); |
| 544 log_attrs_ = NULL; | 546 log_attrs_ = NULL; |
| 545 num_log_attrs_ = 0; | 547 num_log_attrs_ = 0; |
| 546 } | 548 } |
| 547 if (!selection_visual_bounds_.empty()) | 549 if (!selection_visual_bounds_.empty()) |
| 548 selection_visual_bounds_.clear(); | 550 selection_visual_bounds_.clear(); |
| 549 layout_text_ = NULL; | 551 layout_text_ = NULL; |
| 550 layout_text_len_ = 0; | 552 layout_text_len_ = 0; |
| 551 } | 553 } |
| 552 | 554 |
| 553 size_t RenderTextLinux::Utf16IndexToUtf8Index(size_t index) const { | 555 size_t RenderTextLinux::TextIndexToLayoutIndex(size_t text_index) const { |
| 554 int32_t utf8_index = 0; | 556 // If the text is obscured then |layout_text_| is not the same as |text()|, |
| 555 UErrorCode ec = U_ZERO_ERROR; | 557 // but whether or not the text is obscured, the character (code point) offset |
| 556 u_strToUTF8(NULL, 0, &utf8_index, text().data(), index, &ec); | 558 // in |layout_text_| is the same as that in |text()|. |
| 557 // Even given a destination buffer as NULL and destination capacity as 0, | 559 DCHECK(layout_); |
| 558 // if the output length is equal to or greater than the capacity, then the | 560 ptrdiff_t offset = Utf16IndexToOffset(text(), 0, text_index); |
| 559 // UErrorCode is set to U_STRING_NOT_TERMINATED_WARNING or | 561 const char* layout_pointer = g_utf8_offset_to_pointer(layout_text_, offset); |
| 560 // U_BUFFER_OVERFLOW_ERROR respectively. | 562 return (layout_pointer - layout_text_); |
| 561 // Please refer to | |
| 562 // http://userguide.icu-project.org/strings#TOC-Using-C-Strings:-NUL-Terminate d-vs | |
| 563 // for detail (search for "Note that" below "Preflighting"). | |
| 564 DCHECK(ec == U_BUFFER_OVERFLOW_ERROR || | |
| 565 ec == U_STRING_NOT_TERMINATED_WARNING); | |
| 566 return utf8_index; | |
| 567 } | 563 } |
| 568 | 564 |
| 569 size_t RenderTextLinux::Utf8IndexToUtf16Index(size_t index) const { | 565 size_t RenderTextLinux::LayoutIndexToTextIndex(size_t layout_index) const { |
| 570 int32_t utf16_index = 0; | 566 // See |TextIndexToLayoutIndex()|. |
| 571 UErrorCode ec = U_ZERO_ERROR; | 567 DCHECK(layout_); |
| 572 u_strFromUTF8(NULL, 0, &utf16_index, layout_text_, index, &ec); | 568 const char* layout_pointer = layout_text_ + layout_index; |
| 573 DCHECK(ec == U_BUFFER_OVERFLOW_ERROR || | 569 glong offset = g_utf8_pointer_to_offset(layout_text_, layout_pointer); |
|
msw
2012/03/01 22:48:54
Just use long here.
benrg
2012/03/02 00:06:08
Done.
| |
| 574 ec == U_STRING_NOT_TERMINATED_WARNING); | 570 return Utf16OffsetToIndex(text(), 0, offset); |
| 575 return utf16_index; | |
| 576 } | 571 } |
| 577 | 572 |
| 578 std::vector<Rect> RenderTextLinux::CalculateSubstringBounds(size_t from, | 573 std::vector<Rect> RenderTextLinux::CalculateSubstringBounds(size_t from, |
| 579 size_t to) { | 574 size_t to) { |
| 580 int* ranges; | 575 int* ranges; |
| 581 int n_ranges; | 576 int n_ranges; |
| 582 size_t from_in_utf8 = Utf16IndexToUtf8Index(from); | 577 size_t from_in_utf8 = TextIndexToLayoutIndex(from); |
| 583 size_t to_in_utf8 = Utf16IndexToUtf8Index(to); | 578 size_t to_in_utf8 = TextIndexToLayoutIndex(to); |
| 584 pango_layout_line_get_x_ranges( | 579 pango_layout_line_get_x_ranges( |
| 585 current_line_, | 580 current_line_, |
| 586 std::min(from_in_utf8, to_in_utf8), | 581 std::min(from_in_utf8, to_in_utf8), |
| 587 std::max(from_in_utf8, to_in_utf8), | 582 std::max(from_in_utf8, to_in_utf8), |
| 588 &ranges, | 583 &ranges, |
| 589 &n_ranges); | 584 &n_ranges); |
| 590 | 585 |
| 591 int height; | 586 int height; |
| 592 pango_layout_get_pixel_size(layout_, NULL, &height); | 587 pango_layout_get_pixel_size(layout_, NULL, &height); |
| 593 | 588 |
| (...skipping 12 matching lines...) Expand all Loading... | |
| 606 } | 601 } |
| 607 | 602 |
| 608 std::vector<Rect> RenderTextLinux::GetSelectionBounds() { | 603 std::vector<Rect> RenderTextLinux::GetSelectionBounds() { |
| 609 if (selection_visual_bounds_.empty()) | 604 if (selection_visual_bounds_.empty()) |
| 610 selection_visual_bounds_ = | 605 selection_visual_bounds_ = |
| 611 CalculateSubstringBounds(GetSelectionStart(), GetCursorPosition()); | 606 CalculateSubstringBounds(GetSelectionStart(), GetCursorPosition()); |
| 612 return selection_visual_bounds_; | 607 return selection_visual_bounds_; |
| 613 } | 608 } |
| 614 | 609 |
| 615 } // namespace gfx | 610 } // namespace gfx |
| OLD | NEW |