From 1946e649c726424c72bbfb373cfd1a58f3531383 Mon Sep 17 00:00:00 2001 From: Gary Qian Date: Tue, 25 Jul 2017 15:40:09 -0700 Subject: [PATCH] Add middle-of-glyph bounding for coord-to-index system and fix alignment bug. Change-Id: I1f772886949131468407bdc12f1dc6e0ba03b68e --- engine/src/flutter/src/paragraph.cc | 15 ++++++++++++--- engine/src/flutter/src/paragraph.h | 8 +++++++- 2 files changed, 19 insertions(+), 4 deletions(-) diff --git a/engine/src/flutter/src/paragraph.cc b/engine/src/flutter/src/paragraph.cc index 10c5df6e51..f20ad92c2c 100644 --- a/engine/src/flutter/src/paragraph.cc +++ b/engine/src/flutter/src/paragraph.cc @@ -202,7 +202,6 @@ void Paragraph::Layout(double width, bool force) { // Reset member variables so Layout still works when called more than once lines_ = 0; - width_ = 0.0f; line_widths_ = std::vector(); line_heights_ = std::vector(); line_heights_.push_back(0); @@ -798,7 +797,10 @@ SkRect Paragraph::GetCoordinatesForGlyphPosition(size_t pos) const { line_heights_[line]); } -size_t Paragraph::GetGlyphPositionAtCoordinate(double dx, double dy) const { +size_t Paragraph::GetGlyphPositionAtCoordinate( + double dx, + double dy, + bool using_glyph_center_as_boundary) const { size_t offset = 0; size_t y_index = 1; size_t prev_count = 0; @@ -814,7 +816,14 @@ size_t Paragraph::GetGlyphPositionAtCoordinate(double dx, double dy) const { prev_count = 0; for (size_t x_index = 1; x_index < glyph_position_x_[y_index].size() - 1; ++x_index) { - if (dx < glyph_position_x_[y_index][x_index]) { + // TODO(garyq): Resolve edge case where second to last glyph position is + // skipped/unreachable. + if (dx < glyph_position_x_[y_index][x_index] - + (using_glyph_center_as_boundary + ? (glyph_position_x_[y_index][x_index + 1] - + glyph_position_x_[y_index][x_index]) / + 2.0f + : 0)) { break; } else { offset += prev_count; diff --git a/engine/src/flutter/src/paragraph.h b/engine/src/flutter/src/paragraph.h index df90a916a3..b6d8dd6643 100644 --- a/engine/src/flutter/src/paragraph.h +++ b/engine/src/flutter/src/paragraph.h @@ -83,7 +83,13 @@ class Paragraph { // Returns the index of the glyph that corresponds to the provided coordinate, // with the top left corner as the origin, and +y direction as down. - size_t GetGlyphPositionAtCoordinate(double dx, double dy) const; + // + // When using_glyph_center_as_boundary == true, coords to the + direction of + // the center x-position of the glyph will be considered as the next glyph. + size_t GetGlyphPositionAtCoordinate( + double dx, + double dy, + bool using_glyph_center_as_boundary = false) const; // Returns a bounding box that encloses the glyph at the index pos. SkRect GetCoordinatesForGlyphPosition(size_t pos) const;