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 21 matching lines...) Expand all Loading... | |
| 51 // TODO(xji): index saved in upper layer is utf16 index. Pango uses utf8 index. | 50 // TODO(xji): index saved in upper layer is utf16 index. Pango uses utf8 index. |
| 52 // Since caret_pos is used internally, we could save utf8 index for caret_pos | 51 // Since caret_pos is used internally, we could save utf8 index for caret_pos |
| 53 // to avoid conversion. | 52 // to avoid conversion. |
| 54 | 53 |
| 55 RenderTextLinux::RenderTextLinux() | 54 RenderTextLinux::RenderTextLinux() |
| 56 : layout_(NULL), | 55 : layout_(NULL), |
| 57 current_line_(NULL), | 56 current_line_(NULL), |
| 58 log_attrs_(NULL), | 57 log_attrs_(NULL), |
| 59 num_log_attrs_(0), | 58 num_log_attrs_(0), |
| 60 layout_text_(NULL), | 59 layout_text_(NULL), |
| 61 layout_text_len_(0) { | 60 layout_text_len_(0), |
| 61 last_text_index_(0), | |
| 62 last_layout_pointer_(NULL) { | |
| 62 } | 63 } |
| 63 | 64 |
| 64 RenderTextLinux::~RenderTextLinux() { | 65 RenderTextLinux::~RenderTextLinux() { |
| 65 ResetLayout(); | 66 ResetLayout(); |
| 66 } | 67 } |
| 67 | 68 |
| 68 RenderText* RenderText::CreateRenderText() { | 69 RenderText* RenderText::CreateRenderText() { |
| 69 return new RenderTextLinux; | 70 return new RenderTextLinux; |
| 70 } | 71 } |
| 71 | 72 |
| (...skipping 20 matching lines...) Expand all Loading... | |
| 92 return SelectionModel(0, 0, SelectionModel::LEADING); | 93 return SelectionModel(0, 0, SelectionModel::LEADING); |
| 93 | 94 |
| 94 Point p(ToTextPoint(point)); | 95 Point p(ToTextPoint(point)); |
| 95 | 96 |
| 96 // When the point is outside of text, return HOME/END position. | 97 // When the point is outside of text, return HOME/END position. |
| 97 if (p.x() < 0) | 98 if (p.x() < 0) |
| 98 return EdgeSelectionModel(CURSOR_LEFT); | 99 return EdgeSelectionModel(CURSOR_LEFT); |
| 99 else if (p.x() > GetStringWidth()) | 100 else if (p.x() > GetStringWidth()) |
| 100 return EdgeSelectionModel(CURSOR_RIGHT); | 101 return EdgeSelectionModel(CURSOR_RIGHT); |
| 101 | 102 |
| 102 int caret_pos, trailing; | 103 int caret_pos_in_layout, trailing; |
|
msw
2012/02/22 00:33:26
Init these locals to 0, please.
benrg
2012/02/24 19:07:44
I backed out all changes to this code; should I st
| |
| 103 pango_layout_xy_to_index(layout_, p.x() * PANGO_SCALE, p.y() * PANGO_SCALE, | 104 pango_layout_xy_to_index(layout_, p.x() * PANGO_SCALE, p.y() * PANGO_SCALE, |
| 104 &caret_pos, &trailing); | 105 &caret_pos_in_layout, &trailing); |
| 105 | 106 size_t caret_pos = LayoutIndexToTextIndex(caret_pos_in_layout); |
| 106 size_t selection_end = caret_pos; | 107 size_t selection_end = Utf16OffsetToIndex(text(), caret_pos, trailing); |
| 107 if (trailing > 0) { | 108 DCHECK_LE(caret_pos, selection_end); |
| 108 const char* ch = g_utf8_offset_to_pointer(layout_text_ + caret_pos, | 109 DCHECK_LE(selection_end, text().length()); |
| 109 trailing); | |
| 110 DCHECK_GE(ch, layout_text_); | |
| 111 DCHECK_LE(ch, layout_text_ + layout_text_len_); | |
| 112 selection_end = ch - layout_text_; | |
| 113 } | |
| 114 | |
| 115 return SelectionModel( | 110 return SelectionModel( |
| 116 Utf8IndexToUtf16Index(selection_end), | 111 selection_end, caret_pos, |
|
msw
2012/02/22 00:33:26
|caret_pos| belongs on its own line.
| |
| 117 Utf8IndexToUtf16Index(caret_pos), | |
| 118 trailing > 0 ? SelectionModel::TRAILING : SelectionModel::LEADING); | 112 trailing > 0 ? SelectionModel::TRAILING : SelectionModel::LEADING); |
| 119 } | 113 } |
| 120 | 114 |
| 121 Rect RenderTextLinux::GetCursorBounds(const SelectionModel& selection, | 115 Rect RenderTextLinux::GetCursorBounds(const SelectionModel& selection, |
| 122 bool insert_mode) { | 116 bool insert_mode) { |
| 123 EnsureLayout(); | 117 EnsureLayout(); |
| 124 | 118 |
| 125 size_t caret_pos = insert_mode ? selection.caret_pos() : | 119 size_t caret_pos = insert_mode ? selection.caret_pos() : |
| 126 selection.selection_end(); | 120 selection.selection_end(); |
| 127 PangoRectangle pos; | 121 PangoRectangle pos; |
| 128 pango_layout_index_to_pos(layout_, Utf16IndexToUtf8Index(caret_pos), &pos); | 122 pango_layout_index_to_pos(layout_, TextIndexToLayoutIndex(caret_pos), &pos); |
| 129 | 123 |
| 130 SelectionModel::CaretPlacement caret_placement = selection.caret_placement(); | 124 SelectionModel::CaretPlacement caret_placement = selection.caret_placement(); |
| 131 int x = pos.x; | 125 int x = pos.x; |
| 132 if ((insert_mode && caret_placement == SelectionModel::TRAILING) || | 126 if ((insert_mode && caret_placement == SelectionModel::TRAILING) || |
| 133 (!insert_mode && pos.width < 0)) | 127 (!insert_mode && pos.width < 0)) |
| 134 x += pos.width; | 128 x += pos.width; |
| 135 x = PANGO_PIXELS(x); | 129 x = PANGO_PIXELS(x); |
| 136 | 130 |
| 137 int h = std::min(display_rect().height(), PANGO_PIXELS(pos.height)); | 131 int h = std::min(display_rect().height(), PANGO_PIXELS(pos.height)); |
| 138 Rect bounds(x, (display_rect().height() - h) / 2, 0, h); | 132 Rect bounds(x, (display_rect().height() - h) / 2, 0, h); |
| (...skipping 26 matching lines...) Expand all Loading... | |
| 165 SelectionModel RenderTextLinux::AdjacentCharSelectionModel( | 159 SelectionModel RenderTextLinux::AdjacentCharSelectionModel( |
| 166 const SelectionModel& selection, | 160 const SelectionModel& selection, |
| 167 VisualCursorDirection direction) { | 161 VisualCursorDirection direction) { |
| 168 size_t caret = selection.caret_pos(); | 162 size_t caret = selection.caret_pos(); |
| 169 SelectionModel::CaretPlacement caret_placement = selection.caret_placement(); | 163 SelectionModel::CaretPlacement caret_placement = selection.caret_placement(); |
| 170 GSList* run = GetRunContainingPosition(caret); | 164 GSList* run = GetRunContainingPosition(caret); |
| 171 DCHECK(run); | 165 DCHECK(run); |
| 172 | 166 |
| 173 PangoLayoutRun* layout_run = reinterpret_cast<PangoLayoutRun*>(run->data); | 167 PangoLayoutRun* layout_run = reinterpret_cast<PangoLayoutRun*>(run->data); |
| 174 PangoItem* item = layout_run->item; | 168 PangoItem* item = layout_run->item; |
| 175 size_t run_start = Utf8IndexToUtf16Index(item->offset); | 169 size_t run_start = LayoutIndexToTextIndex(item->offset); |
| 176 size_t run_end = Utf8IndexToUtf16Index(item->offset + item->length); | 170 size_t run_end = LayoutIndexToTextIndex(item->offset + item->length); |
| 177 | 171 |
| 178 if (!IsForwardMotion(direction, item)) { | 172 if (!IsForwardMotion(direction, item)) { |
| 179 if (caret_placement == SelectionModel::TRAILING) | 173 if (caret_placement == SelectionModel::TRAILING) |
| 180 return SelectionModel(caret, caret, SelectionModel::LEADING); | 174 return SelectionModel(caret, caret, SelectionModel::LEADING); |
| 181 else if (caret > run_start) { | 175 else if (caret > run_start) { |
| 182 caret = IndexOfAdjacentGrapheme(caret, CURSOR_BACKWARD); | 176 caret = IndexOfAdjacentGrapheme(caret, CURSOR_BACKWARD); |
| 183 return SelectionModel(caret, caret, SelectionModel::LEADING); | 177 return SelectionModel(caret, caret, SelectionModel::LEADING); |
| 184 } | 178 } |
| 185 } else { | 179 } else { |
| 186 if (caret_placement == SelectionModel::LEADING) { | 180 if (caret_placement == SelectionModel::LEADING) { |
| (...skipping 15 matching lines...) Expand all Loading... | |
| 202 return EdgeSelectionModel(direction); | 196 return EdgeSelectionModel(direction); |
| 203 | 197 |
| 204 item = reinterpret_cast<PangoLayoutRun*>(adjacent_run->data)->item; | 198 item = reinterpret_cast<PangoLayoutRun*>(adjacent_run->data)->item; |
| 205 return IsForwardMotion(direction, item) ? | 199 return IsForwardMotion(direction, item) ? |
| 206 FirstSelectionModelInsideRun(item) : LastSelectionModelInsideRun(item); | 200 FirstSelectionModelInsideRun(item) : LastSelectionModelInsideRun(item); |
| 207 } | 201 } |
| 208 | 202 |
| 209 SelectionModel RenderTextLinux::AdjacentWordSelectionModel( | 203 SelectionModel RenderTextLinux::AdjacentWordSelectionModel( |
| 210 const SelectionModel& selection, | 204 const SelectionModel& selection, |
| 211 VisualCursorDirection direction) { | 205 VisualCursorDirection direction) { |
| 206 if (is_obscured()) | |
| 207 return EdgeSelectionModel(direction); | |
| 208 | |
| 212 base::i18n::BreakIterator iter(text(), base::i18n::BreakIterator::BREAK_WORD); | 209 base::i18n::BreakIterator iter(text(), base::i18n::BreakIterator::BREAK_WORD); |
| 213 bool success = iter.Init(); | 210 bool success = iter.Init(); |
| 214 DCHECK(success); | 211 DCHECK(success); |
| 215 if (!success) | 212 if (!success) |
| 216 return selection; | 213 return selection; |
| 217 | 214 |
| 218 SelectionModel end = EdgeSelectionModel(direction); | 215 SelectionModel end = EdgeSelectionModel(direction); |
| 219 SelectionModel cur(selection); | 216 SelectionModel cur(selection); |
| 220 while (!cur.Equals(end)) { | 217 while (!cur.Equals(end)) { |
| 221 cur = AdjacentCharSelectionModel(cur, direction); | 218 cur = AdjacentCharSelectionModel(cur, direction); |
| (...skipping 14 matching lines...) Expand all Loading... | |
| 236 VisualCursorDirection direction) { | 233 VisualCursorDirection direction) { |
| 237 if (direction == GetVisualDirectionOfLogicalEnd()) { | 234 if (direction == GetVisualDirectionOfLogicalEnd()) { |
| 238 // Advance to the logical end of the text. | 235 // Advance to the logical end of the text. |
| 239 GSList* run = current_line_->runs; | 236 GSList* run = current_line_->runs; |
| 240 if (direction == CURSOR_RIGHT) | 237 if (direction == CURSOR_RIGHT) |
| 241 run = g_slist_last(run); | 238 run = g_slist_last(run); |
| 242 if (run) { | 239 if (run) { |
| 243 PangoLayoutRun* end_run = reinterpret_cast<PangoLayoutRun*>(run->data); | 240 PangoLayoutRun* end_run = reinterpret_cast<PangoLayoutRun*>(run->data); |
| 244 PangoItem* item = end_run->item; | 241 PangoItem* item = end_run->item; |
| 245 if (IsForwardMotion(direction, item)) { | 242 if (IsForwardMotion(direction, item)) { |
| 246 size_t caret = Utf8IndexToUtf16Index( | 243 size_t caret = IndexOfAdjacentGrapheme( |
| 247 Utf8IndexOfAdjacentGrapheme(item->offset + item->length, | 244 LayoutIndexToTextIndex(item->offset + item->length), |
| 248 CURSOR_BACKWARD)); | 245 CURSOR_BACKWARD); |
| 249 return SelectionModel(text().length(), caret, SelectionModel::TRAILING); | 246 return SelectionModel(text().length(), caret, SelectionModel::TRAILING); |
| 250 } else { | 247 } else { |
| 251 size_t caret = Utf8IndexToUtf16Index(item->offset); | 248 size_t caret = LayoutIndexToTextIndex(item->offset); |
| 252 return SelectionModel(text().length(), caret, SelectionModel::LEADING); | 249 return SelectionModel(text().length(), caret, SelectionModel::LEADING); |
| 253 } | 250 } |
| 254 } | 251 } |
| 255 } | 252 } |
| 256 return SelectionModel(0, 0, SelectionModel::LEADING); | 253 return SelectionModel(0, 0, SelectionModel::LEADING); |
| 257 } | 254 } |
| 258 | 255 |
| 259 void RenderTextLinux::SetSelectionModel(const SelectionModel& model) { | 256 void RenderTextLinux::SetSelectionModel(const SelectionModel& model) { |
| 260 if (GetSelectionStart() != model.selection_start() || | 257 if (GetSelectionStart() != model.selection_start() || |
| 261 GetCursorPosition() != model.selection_end()) { | 258 GetCursorPosition() != model.selection_end()) { |
| (...skipping 12 matching lines...) Expand all Loading... | |
| 274 | 271 |
| 275 EnsureLayout(); | 272 EnsureLayout(); |
| 276 | 273 |
| 277 if (from == GetSelectionStart() && to == GetCursorPosition()) | 274 if (from == GetSelectionStart() && to == GetCursorPosition()) |
| 278 return GetSelectionBounds(); | 275 return GetSelectionBounds(); |
| 279 else | 276 else |
| 280 return CalculateSubstringBounds(from, to); | 277 return CalculateSubstringBounds(from, to); |
| 281 } | 278 } |
| 282 | 279 |
| 283 bool RenderTextLinux::IsCursorablePosition(size_t position) { | 280 bool RenderTextLinux::IsCursorablePosition(size_t position) { |
| 284 if (position == 0 && text().empty()) | 281 if (position == 0) |
| 285 return true; | 282 return true; |
| 286 | 283 if (position > text().length()) |
| 284 return false; | |
| 287 EnsureLayout(); | 285 EnsureLayout(); |
| 288 return (position < static_cast<size_t>(num_log_attrs_) && | 286 return log_attrs_[Utf16IndexToOffset(text(), 0, position)].is_cursor_position; |
| 289 log_attrs_[position].is_cursor_position); | |
| 290 } | 287 } |
| 291 | 288 |
| 292 void RenderTextLinux::UpdateLayout() { | 289 void RenderTextLinux::UpdateLayout() { |
| 293 ResetLayout(); | 290 ResetLayout(); |
| 294 } | 291 } |
| 295 | 292 |
| 296 void RenderTextLinux::EnsureLayout() { | 293 void RenderTextLinux::EnsureLayout() { |
| 297 if (layout_ == NULL) { | 294 if (layout_ == NULL) { |
| 298 CanvasSkia canvas(display_rect().size(), false); | 295 CanvasSkia canvas(display_rect().size(), false); |
| 299 skia::ScopedPlatformPaint scoped_platform_paint(canvas.sk_canvas()); | 296 skia::ScopedPlatformPaint scoped_platform_paint(canvas.sk_canvas()); |
| 300 cairo_t* cr = scoped_platform_paint.GetPlatformSurface(); | 297 cairo_t* cr = scoped_platform_paint.GetPlatformSurface(); |
| 301 | 298 |
| 302 layout_ = pango_cairo_create_layout(cr); | 299 layout_ = pango_cairo_create_layout(cr); |
| 303 SetupPangoLayoutWithFontDescription( | 300 SetupPangoLayoutWithFontDescription( |
| 304 layout_, | 301 layout_, |
| 305 text(), | 302 GetDisplayText(), |
| 306 font_list().GetFontDescriptionString(), | 303 font_list().GetFontDescriptionString(), |
| 307 display_rect().width(), | 304 display_rect().width(), |
| 308 base::i18n::GetFirstStrongCharacterDirection(text()), | 305 base::i18n::GetFirstStrongCharacterDirection(text()), |
| 309 CanvasSkia::DefaultCanvasTextAlignment()); | 306 CanvasSkia::DefaultCanvasTextAlignment()); |
| 310 | 307 |
| 311 // No width set so that the x-axis position is relative to the start of the | 308 // 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 | 309 // text. ToViewPoint and ToTextPoint take care of the position conversion |
| 313 // between text space and view spaces. | 310 // between text space and view spaces. |
| 314 pango_layout_set_width(layout_, -1); | 311 pango_layout_set_width(layout_, -1); |
| 315 // TODO(xji): If RenderText will be used for displaying purpose, such as | 312 // TODO(xji): If RenderText will be used for displaying purpose, such as |
| 316 // label, we will need to remove the single-line-mode setting. | 313 // label, we will need to remove the single-line-mode setting. |
| 317 pango_layout_set_single_paragraph_mode(layout_, true); | 314 pango_layout_set_single_paragraph_mode(layout_, true); |
| 318 SetupPangoAttributes(layout_); | |
| 319 | 315 |
| 320 current_line_ = pango_layout_get_line_readonly(layout_, 0); | 316 current_line_ = pango_layout_get_line_readonly(layout_, 0); |
| 321 pango_layout_line_ref(current_line_); | 317 pango_layout_line_ref(current_line_); |
| 322 | 318 |
| 323 pango_layout_get_log_attrs(layout_, &log_attrs_, &num_log_attrs_); | 319 pango_layout_get_log_attrs(layout_, &log_attrs_, &num_log_attrs_); |
| 324 | 320 |
| 325 layout_text_ = pango_layout_get_text(layout_); | 321 layout_text_ = pango_layout_get_text(layout_); |
| 326 layout_text_len_ = strlen(layout_text_); | 322 layout_text_len_ = strlen(layout_text_); |
| 323 | |
| 324 last_text_index_ = 0; | |
| 325 last_layout_pointer_ = layout_text_; | |
| 326 | |
| 327 SetupPangoAttributes(layout_); | |
| 327 } | 328 } |
| 328 } | 329 } |
| 329 | 330 |
| 330 void RenderTextLinux::SetupPangoAttributes(PangoLayout* layout) { | 331 void RenderTextLinux::SetupPangoAttributes(PangoLayout* layout) { |
| 331 PangoAttrList* attrs = pango_attr_list_new(); | 332 PangoAttrList* attrs = pango_attr_list_new(); |
| 332 | 333 |
| 333 int default_font_style = font_list().GetFontStyle(); | 334 int default_font_style = font_list().GetFontStyle(); |
| 334 for (StyleRanges::const_iterator i = style_ranges().begin(); | 335 for (StyleRanges::const_iterator i = style_ranges().begin(); |
| 335 i < style_ranges().end(); ++i) { | 336 i < style_ranges().end(); ++i) { |
| 336 // In Pango, different fonts means different runs, and it breaks Arabic | 337 // In Pango, different fonts means different runs, and it breaks Arabic |
| 337 // shaping across run boundaries. So, set font only when it is different | 338 // shaping across run boundaries. So, set font only when it is different |
| 338 // from the default font. | 339 // from the default font. |
| 339 // TODO(xji): We'll eventually need to split up StyleRange into components | 340 // TODO(xji): We'll eventually need to split up StyleRange into components |
| 340 // (ColorRange, FontRange, etc.) so that we can combine adjacent ranges | 341 // (ColorRange, FontRange, etc.) so that we can combine adjacent ranges |
| 341 // with the same Fonts (to avoid unnecessarily splitting up runs). | 342 // with the same Fonts (to avoid unnecessarily splitting up runs). |
| 342 if (i->font_style != default_font_style) { | 343 if (i->font_style != default_font_style) { |
| 343 FontList derived_font_list = font_list().DeriveFontList(i->font_style); | 344 FontList derived_font_list = font_list().DeriveFontList(i->font_style); |
| 344 PangoFontDescription* desc = pango_font_description_from_string( | 345 PangoFontDescription* desc = pango_font_description_from_string( |
| 345 derived_font_list.GetFontDescriptionString().c_str()); | 346 derived_font_list.GetFontDescriptionString().c_str()); |
| 346 | 347 |
| 347 PangoAttribute* pango_attr = pango_attr_font_desc_new(desc); | 348 PangoAttribute* pango_attr = pango_attr_font_desc_new(desc); |
| 348 pango_attr->start_index = Utf16IndexToUtf8Index(i->range.start()); | 349 pango_attr->start_index = TextIndexToLayoutIndex(i->range.start()); |
| 349 pango_attr->end_index = Utf16IndexToUtf8Index(i->range.end()); | 350 pango_attr->end_index = TextIndexToLayoutIndex(i->range.end()); |
| 350 pango_attr_list_insert(attrs, pango_attr); | 351 pango_attr_list_insert(attrs, pango_attr); |
| 351 pango_font_description_free(desc); | 352 pango_font_description_free(desc); |
| 352 } | 353 } |
| 353 } | 354 } |
| 354 | 355 |
| 355 pango_layout_set_attributes(layout, attrs); | 356 pango_layout_set_attributes(layout, attrs); |
| 356 pango_attr_list_unref(attrs); | 357 pango_attr_list_unref(attrs); |
| 357 } | 358 } |
| 358 | 359 |
| 359 void RenderTextLinux::DrawVisualText(Canvas* canvas) { | 360 void RenderTextLinux::DrawVisualText(Canvas* canvas) { |
| 360 DCHECK(layout_); | 361 DCHECK(layout_); |
| 361 | 362 |
| 362 Point offset(GetOriginForSkiaDrawing()); | 363 Point offset(GetOriginForSkiaDrawing()); |
| 363 SkScalar x = SkIntToScalar(offset.x()); | 364 SkScalar x = SkIntToScalar(offset.x()); |
| 364 SkScalar y = SkIntToScalar(offset.y()); | 365 SkScalar y = SkIntToScalar(offset.y()); |
| 365 | 366 |
| 366 std::vector<SkPoint> pos; | 367 std::vector<SkPoint> pos; |
| 367 std::vector<uint16> glyphs; | 368 std::vector<uint16> glyphs; |
| 368 | 369 |
| 369 StyleRanges styles(style_ranges()); | 370 StyleRanges styles(style_ranges()); |
| 370 ApplyCompositionAndSelectionStyles(&styles); | 371 ApplyCompositionAndSelectionStyles(&styles); |
| 371 | 372 |
| 372 // Pre-calculate UTF8 indices from UTF16 indices. | |
| 373 // TODO(asvitkine): Can we cache these? | |
| 374 std::vector<ui::Range> style_ranges_utf8; | |
| 375 style_ranges_utf8.reserve(styles.size()); | |
| 376 size_t start_index = 0; | |
| 377 for (size_t i = 0; i < styles.size(); ++i) { | |
| 378 size_t end_index = Utf16IndexToUtf8Index(styles[i].range.end()); | |
| 379 style_ranges_utf8.push_back(ui::Range(start_index, end_index)); | |
| 380 start_index = end_index; | |
| 381 } | |
|
xji
2012/02/18 01:28:47
will removing this cause performance hit if we do
| |
| 382 | |
| 383 internal::SkiaTextRenderer renderer(canvas); | 373 internal::SkiaTextRenderer renderer(canvas); |
| 384 ApplyFadeEffects(&renderer); | 374 ApplyFadeEffects(&renderer); |
| 385 | 375 |
| 386 for (GSList* it = current_line_->runs; it; it = it->next) { | 376 for (GSList* it = current_line_->runs; it; it = it->next) { |
| 387 PangoLayoutRun* run = reinterpret_cast<PangoLayoutRun*>(it->data); | 377 PangoLayoutRun* run = reinterpret_cast<PangoLayoutRun*>(it->data); |
| 388 int glyph_count = run->glyphs->num_glyphs; | 378 int glyph_count = run->glyphs->num_glyphs; |
| 389 if (glyph_count == 0) | 379 if (glyph_count == 0) |
| 390 continue; | 380 continue; |
| 391 | 381 |
| 392 size_t run_start = run->item->offset; | 382 size_t run_start = run->item->offset; |
| 393 size_t first_glyph_byte_index = run_start + run->glyphs->log_clusters[0]; | 383 size_t first_glyph_index = |
| 384 LayoutIndexToTextIndex(run_start + run->glyphs->log_clusters[0]); | |
| 394 size_t style_increment = IsForwardMotion(CURSOR_RIGHT, run->item) ? 1 : -1; | 385 size_t style_increment = IsForwardMotion(CURSOR_RIGHT, run->item) ? 1 : -1; |
| 395 | 386 |
| 396 // Find the initial style for this run. | 387 // Find the initial style for this run. |
| 397 // TODO(asvitkine): Can we avoid looping here, e.g. by caching this per run? | 388 // TODO(asvitkine): Can we avoid looping here, e.g. by caching this per run? |
| 398 int style = -1; | 389 int style = -1; |
| 399 for (size_t i = 0; i < style_ranges_utf8.size(); ++i) { | 390 for (size_t i = 0; i < styles.size(); ++i) { |
| 400 if (IndexInRange(style_ranges_utf8[i], first_glyph_byte_index)) { | 391 if (IndexInRange(styles[i].range, first_glyph_index)) { |
| 401 style = i; | 392 style = i; |
| 402 break; | 393 break; |
| 403 } | 394 } |
| 404 } | 395 } |
| 405 DCHECK_GE(style, 0); | 396 DCHECK_GE(style, 0); |
| 406 | 397 |
| 407 PangoFontDescription* native_font = | 398 PangoFontDescription* native_font = |
| 408 pango_font_describe(run->item->analysis.font); | 399 pango_font_describe(run->item->analysis.font); |
| 409 | 400 |
| 410 const char* family_name = pango_font_description_get_family(native_font); | 401 const char* family_name = pango_font_description_get_family(native_font); |
| (...skipping 13 matching lines...) Expand all Loading... | |
| 424 | 415 |
| 425 for (int i = 0; i < glyph_count; ++i) { | 416 for (int i = 0; i < glyph_count; ++i) { |
| 426 const PangoGlyphInfo& glyph = run->glyphs->glyphs[i]; | 417 const PangoGlyphInfo& glyph = run->glyphs->glyphs[i]; |
| 427 glyphs[i] = static_cast<uint16>(glyph.glyph); | 418 glyphs[i] = static_cast<uint16>(glyph.glyph); |
| 428 pos[i].set(glyph_x + PANGO_PIXELS(glyph.geometry.x_offset), | 419 pos[i].set(glyph_x + PANGO_PIXELS(glyph.geometry.x_offset), |
| 429 y + PANGO_PIXELS(glyph.geometry.y_offset)); | 420 y + PANGO_PIXELS(glyph.geometry.y_offset)); |
| 430 glyph_x += PANGO_PIXELS(glyph.geometry.width); | 421 glyph_x += PANGO_PIXELS(glyph.geometry.width); |
| 431 | 422 |
| 432 // If this glyph is beyond the current style, draw the glyphs so far and | 423 // If this glyph is beyond the current style, draw the glyphs so far and |
| 433 // advance to the next style. | 424 // advance to the next style. |
| 434 size_t glyph_byte_index = run_start + run->glyphs->log_clusters[i]; | 425 size_t glyph_index = |
| 426 LayoutIndexToTextIndex(run_start + run->glyphs->log_clusters[i]); | |
| 435 DCHECK_GE(style, 0); | 427 DCHECK_GE(style, 0); |
| 436 DCHECK_LT(style, static_cast<int>(styles.size())); | 428 DCHECK_LT(style, static_cast<int>(styles.size())); |
| 437 if (!IndexInRange(style_ranges_utf8[style], glyph_byte_index)) { | 429 if (!IndexInRange(styles[style].range, glyph_index)) { |
| 438 // TODO(asvitkine): For cases like "fi", where "fi" is a single glyph | 430 // TODO(asvitkine): For cases like "fi", where "fi" is a single glyph |
| 439 // but can span multiple styles, Pango splits the | 431 // but can span multiple styles, Pango splits the |
| 440 // styles evenly over the glyph. We can do this too by | 432 // styles evenly over the glyph. We can do this too by |
| 441 // clipping and drawing the glyph several times. | 433 // clipping and drawing the glyph several times. |
| 442 renderer.SetForegroundColor(styles[style].foreground); | 434 renderer.SetForegroundColor(styles[style].foreground); |
| 443 renderer.SetFontStyle(styles[style].font_style); | 435 renderer.SetFontStyle(styles[style].font_style); |
| 444 renderer.DrawPosText(&pos[start], &glyphs[start], i - start); | 436 renderer.DrawPosText(&pos[start], &glyphs[start], i - start); |
| 445 renderer.DrawDecorations(start_x, y, glyph_x - start_x, styles[style]); | 437 renderer.DrawDecorations(start_x, y, glyph_x - start_x, styles[style]); |
| 446 | 438 |
| 447 start = i; | 439 start = i; |
| 448 start_x = glyph_x; | 440 start_x = glyph_x; |
| 449 // Loop to find the next style, in case the glyph spans multiple styles. | 441 // Loop to find the next style, in case the glyph spans multiple styles. |
| 450 do { | 442 do { |
| 451 style += style_increment; | 443 style += style_increment; |
| 452 } while (style >= 0 && style < static_cast<int>(styles.size()) && | 444 } while (style >= 0 && style < static_cast<int>(styles.size()) && |
| 453 !IndexInRange(style_ranges_utf8[style], glyph_byte_index)); | 445 !IndexInRange(styles[style].range, glyph_index)); |
| 454 } | 446 } |
| 455 } | 447 } |
| 456 | 448 |
| 457 // Draw the remaining glyphs. | 449 // Draw the remaining glyphs. |
| 458 renderer.SetForegroundColor(styles[style].foreground); | 450 renderer.SetForegroundColor(styles[style].foreground); |
| 459 renderer.SetFontStyle(styles[style].font_style); | 451 renderer.SetFontStyle(styles[style].font_style); |
| 460 renderer.DrawPosText(&pos[start], &glyphs[start], glyph_count - start); | 452 renderer.DrawPosText(&pos[start], &glyphs[start], glyph_count - start); |
| 461 renderer.DrawDecorations(start_x, y, glyph_x - start_x, styles[style]); | 453 renderer.DrawDecorations(start_x, y, glyph_x - start_x, styles[style]); |
| 462 x = glyph_x; | 454 x = glyph_x; |
| 463 } | 455 } |
| 464 } | 456 } |
| 465 | 457 |
| 466 size_t RenderTextLinux::IndexOfAdjacentGrapheme( | 458 size_t RenderTextLinux::IndexOfAdjacentGrapheme( |
| 467 size_t index, | 459 size_t index, |
| 468 LogicalCursorDirection direction) { | 460 LogicalCursorDirection direction) { |
| 469 if (index > text().length()) | 461 if (index > text().length()) |
| 470 return text().length(); | 462 return text().length(); |
| 471 EnsureLayout(); | 463 EnsureLayout(); |
| 472 return Utf8IndexToUtf16Index( | |
| 473 Utf8IndexOfAdjacentGrapheme(Utf16IndexToUtf8Index(index), direction)); | |
| 474 } | |
| 475 | 464 |
| 476 GSList* RenderTextLinux::GetRunContainingPosition(size_t position) const { | 465 ptrdiff_t char_offset = Utf16IndexToOffset(text(), 0, index); |
| 477 GSList* run = current_line_->runs; | |
| 478 while (run) { | |
| 479 PangoItem* item = reinterpret_cast<PangoLayoutRun*>(run->data)->item; | |
| 480 size_t run_start = Utf8IndexToUtf16Index(item->offset); | |
| 481 size_t run_end = Utf8IndexToUtf16Index(item->offset + item->length); | |
| 482 | |
| 483 if (position >= run_start && position < run_end) | |
| 484 return run; | |
| 485 run = run->next; | |
| 486 } | |
| 487 return NULL; | |
| 488 } | |
| 489 | |
| 490 size_t RenderTextLinux::Utf8IndexOfAdjacentGrapheme( | |
| 491 size_t utf8_index_of_current_grapheme, | |
| 492 LogicalCursorDirection direction) const { | |
| 493 const char* ch = layout_text_ + utf8_index_of_current_grapheme; | |
| 494 int char_offset = static_cast<int>(g_utf8_pointer_to_offset(layout_text_, | |
| 495 ch)); | |
| 496 int start_char_offset = char_offset; | |
| 497 if (direction == CURSOR_BACKWARD) { | 466 if (direction == CURSOR_BACKWARD) { |
| 498 if (char_offset > 0) { | 467 if (char_offset > 0) { |
| 499 do { | 468 do { |
| 500 --char_offset; | 469 --char_offset; |
| 501 } while (char_offset > 0 && !log_attrs_[char_offset].is_cursor_position); | 470 } while (char_offset > 0 && !log_attrs_[char_offset].is_cursor_position); |
| 502 } | 471 } |
| 503 } else { // direction == CURSOR_FORWARD | 472 } else { // direction == CURSOR_FORWARD |
| 504 if (char_offset < num_log_attrs_ - 1) { | 473 if (char_offset < num_log_attrs_ - 1) { |
| 505 do { | 474 do { |
| 506 ++char_offset; | 475 ++char_offset; |
| 507 } while (char_offset < num_log_attrs_ - 1 && | 476 } while (char_offset < num_log_attrs_ - 1 && |
| 508 !log_attrs_[char_offset].is_cursor_position); | 477 !log_attrs_[char_offset].is_cursor_position); |
| 509 } | 478 } |
| 510 } | 479 } |
| 480 return Utf16OffsetToIndex(text(), 0, char_offset); | |
| 481 } | |
| 511 | 482 |
| 512 ch = g_utf8_offset_to_pointer(ch, char_offset - start_char_offset); | 483 GSList* RenderTextLinux::GetRunContainingPosition(size_t position) const { |
| 513 return static_cast<size_t>(ch - layout_text_); | 484 position = TextIndexToLayoutIndex(position); |
| 485 for (GSList* run = current_line_->runs; run; run = run->next) { | |
| 486 PangoItem* item = reinterpret_cast<PangoLayoutRun*>(run->data)->item; | |
| 487 if (position >= static_cast<size_t>(item->offset) && | |
| 488 position < static_cast<size_t>(item->offset + item->length)) | |
| 489 return run; | |
| 490 } | |
| 491 return NULL; | |
|
xji
2012/02/18 01:28:47
Ah, nice!
| |
| 514 } | 492 } |
| 515 | 493 |
| 516 SelectionModel RenderTextLinux::FirstSelectionModelInsideRun( | 494 SelectionModel RenderTextLinux::FirstSelectionModelInsideRun( |
| 517 const PangoItem* item) const { | 495 const PangoItem* item) { |
| 518 size_t caret = Utf8IndexToUtf16Index(item->offset); | 496 size_t caret = LayoutIndexToTextIndex(item->offset); |
| 519 size_t cursor = Utf8IndexToUtf16Index( | 497 size_t cursor = IndexOfAdjacentGrapheme(caret, CURSOR_FORWARD); |
| 520 Utf8IndexOfAdjacentGrapheme(item->offset, CURSOR_FORWARD)); | |
| 521 return SelectionModel(cursor, caret, SelectionModel::TRAILING); | 498 return SelectionModel(cursor, caret, SelectionModel::TRAILING); |
| 522 } | 499 } |
| 523 | 500 |
| 524 SelectionModel RenderTextLinux::LastSelectionModelInsideRun( | 501 SelectionModel RenderTextLinux::LastSelectionModelInsideRun( |
| 525 const PangoItem* item) const { | 502 const PangoItem* item) { |
| 526 size_t caret = Utf8IndexToUtf16Index(Utf8IndexOfAdjacentGrapheme( | 503 size_t caret = IndexOfAdjacentGrapheme( |
| 527 item->offset + item->length, CURSOR_BACKWARD)); | 504 LayoutIndexToTextIndex(item->offset + item->length), CURSOR_BACKWARD); |
| 528 return SelectionModel(caret, caret, SelectionModel::LEADING); | 505 return SelectionModel(caret, caret, SelectionModel::LEADING); |
| 529 } | 506 } |
| 530 | 507 |
| 531 void RenderTextLinux::ResetLayout() { | 508 void RenderTextLinux::ResetLayout() { |
| 532 // set_cached_bounds_and_offset_valid(false) is done in RenderText for every | 509 // set_cached_bounds_and_offset_valid(false) is done in RenderText for every |
| 533 // operation that triggers ResetLayout(). | 510 // operation that triggers ResetLayout(). |
| 534 if (layout_) { | 511 if (layout_) { |
| 535 g_object_unref(layout_); | 512 g_object_unref(layout_); |
| 536 layout_ = NULL; | 513 layout_ = NULL; |
| 537 } | 514 } |
| 538 if (current_line_) { | 515 if (current_line_) { |
| 539 pango_layout_line_unref(current_line_); | 516 pango_layout_line_unref(current_line_); |
| 540 current_line_ = NULL; | 517 current_line_ = NULL; |
| 541 } | 518 } |
| 542 if (log_attrs_) { | 519 if (log_attrs_) { |
| 543 g_free(log_attrs_); | 520 g_free(log_attrs_); |
| 544 log_attrs_ = NULL; | 521 log_attrs_ = NULL; |
| 545 num_log_attrs_ = 0; | 522 num_log_attrs_ = 0; |
| 546 } | 523 } |
| 547 if (!selection_visual_bounds_.empty()) | 524 if (!selection_visual_bounds_.empty()) |
| 548 selection_visual_bounds_.clear(); | 525 selection_visual_bounds_.clear(); |
| 549 layout_text_ = NULL; | 526 layout_text_ = NULL; |
| 550 layout_text_len_ = 0; | 527 layout_text_len_ = 0; |
| 528 last_text_index_ = 0; | |
| 529 last_layout_pointer_ = NULL; | |
| 551 } | 530 } |
| 552 | 531 |
| 553 size_t RenderTextLinux::Utf16IndexToUtf8Index(size_t index) const { | 532 size_t RenderTextLinux::TextIndexToLayoutIndex(size_t text_index) const { |
| 554 int32_t utf8_index = 0; | 533 // If the text is obscured then |layout_text_| is not the same as |text()|, |
| 555 UErrorCode ec = U_ZERO_ERROR; | 534 // but whether or not the text is obscured, the character (code point) offset |
| 556 u_strToUTF8(NULL, 0, &utf8_index, text().data(), index, &ec); | 535 // in |layout_text_| is the same as that in |text()|. |
| 557 // Even given a destination buffer as NULL and destination capacity as 0, | 536 DCHECK(layout_); |
| 558 // if the output length is equal to or greater than the capacity, then the | 537 ptrdiff_t offset = Utf16IndexToOffset(text(), last_text_index_, text_index); |
| 559 // UErrorCode is set to U_STRING_NOT_TERMINATED_WARNING or | 538 const char* layout_pointer = g_utf8_offset_to_pointer(last_layout_pointer_, |
| 560 // U_BUFFER_OVERFLOW_ERROR respectively. | 539 offset); |
| 561 // Please refer to | 540 last_text_index_ = text_index; |
| 562 // http://userguide.icu-project.org/strings#TOC-Using-C-Strings:-NUL-Terminate d-vs | 541 last_layout_pointer_ = layout_pointer; |
| 563 // for detail (search for "Note that" below "Preflighting"). | 542 return (layout_pointer - layout_text_); |
| 564 DCHECK(ec == U_BUFFER_OVERFLOW_ERROR || | |
| 565 ec == U_STRING_NOT_TERMINATED_WARNING); | |
| 566 return utf8_index; | |
| 567 } | 543 } |
| 568 | 544 |
| 569 size_t RenderTextLinux::Utf8IndexToUtf16Index(size_t index) const { | 545 size_t RenderTextLinux::LayoutIndexToTextIndex(size_t layout_index) const { |
| 570 int32_t utf16_index = 0; | 546 // See |TextIndexToLayoutIndex()|. |
| 571 UErrorCode ec = U_ZERO_ERROR; | 547 DCHECK(layout_); |
| 572 u_strFromUTF8(NULL, 0, &utf16_index, layout_text_, index, &ec); | 548 const char* layout_pointer = layout_text_ + layout_index; |
| 573 DCHECK(ec == U_BUFFER_OVERFLOW_ERROR || | 549 glong offset = g_utf8_pointer_to_offset(last_layout_pointer_, layout_pointer); |
| 574 ec == U_STRING_NOT_TERMINATED_WARNING); | 550 size_t text_index = Utf16OffsetToIndex(text(), last_text_index_, offset); |
| 575 return utf16_index; | 551 last_text_index_ = text_index; |
| 552 last_layout_pointer_ = layout_pointer; | |
| 553 return text_index; | |
| 576 } | 554 } |
| 577 | 555 |
| 578 std::vector<Rect> RenderTextLinux::CalculateSubstringBounds(size_t from, | 556 std::vector<Rect> RenderTextLinux::CalculateSubstringBounds(size_t from, |
| 579 size_t to) { | 557 size_t to) { |
| 580 int* ranges; | 558 int* ranges; |
| 581 int n_ranges; | 559 int n_ranges; |
| 582 size_t from_in_utf8 = Utf16IndexToUtf8Index(from); | 560 size_t from_in_utf8 = TextIndexToLayoutIndex(from); |
| 583 size_t to_in_utf8 = Utf16IndexToUtf8Index(to); | 561 size_t to_in_utf8 = TextIndexToLayoutIndex(to); |
| 584 pango_layout_line_get_x_ranges( | 562 pango_layout_line_get_x_ranges( |
| 585 current_line_, | 563 current_line_, |
| 586 std::min(from_in_utf8, to_in_utf8), | 564 std::min(from_in_utf8, to_in_utf8), |
| 587 std::max(from_in_utf8, to_in_utf8), | 565 std::max(from_in_utf8, to_in_utf8), |
| 588 &ranges, | 566 &ranges, |
| 589 &n_ranges); | 567 &n_ranges); |
| 590 | 568 |
| 591 int height; | 569 int height; |
| 592 pango_layout_get_pixel_size(layout_, NULL, &height); | 570 pango_layout_get_pixel_size(layout_, NULL, &height); |
| 593 | 571 |
| (...skipping 12 matching lines...) Expand all Loading... | |
| 606 } | 584 } |
| 607 | 585 |
| 608 std::vector<Rect> RenderTextLinux::GetSelectionBounds() { | 586 std::vector<Rect> RenderTextLinux::GetSelectionBounds() { |
| 609 if (selection_visual_bounds_.empty()) | 587 if (selection_visual_bounds_.empty()) |
| 610 selection_visual_bounds_ = | 588 selection_visual_bounds_ = |
| 611 CalculateSubstringBounds(GetSelectionStart(), GetCursorPosition()); | 589 CalculateSubstringBounds(GetSelectionStart(), GetCursorPosition()); |
| 612 return selection_visual_bounds_; | 590 return selection_visual_bounds_; |
| 613 } | 591 } |
| 614 | 592 |
| 615 } // namespace gfx | 593 } // namespace gfx |
| OLD | NEW |