Skip to content

Commit f326a1e

Browse files
dbebawyskcq-be@skia-corp.google.com.iam.gserviceaccount.com
authored andcommitted
[skparagraph] Report a hit on the ellipsis or hyphen as upstream
TextLine::getGlyphPositionAtCoordinate returns the end of the line's text for a hit on the ellipsis or on a rendered soft hyphen, but with downstream affinity. For a soft hyphen that position is also the start of the next line, and for an ellipsis it's past the line's text, so: - getClosestUTF16GlyphInfoAt returns the next line's first glyph for a hit on the hyphen, and no glyph at all for a hit on the ellipsis. - Flutter's Paragraph.getLineBoundary puts a hit on the hyphen on the next line. Return upstream instead, as for a hit past the end of any other line. For "inter­national" broken after the soft hyphen, a hit on the hyphen now returns {6, upstream}, and its glyph is the soft hyphen [5, 6) instead of the "n" [6, 7). For "abcdef" ellipsized to "abc...", a hit on the ellipsis returns {3, upstream} and the glyph "c" [2, 3). This came up in flutter/flutter#195366, where WebParagraph now returns upstream here too. [email protected], [email protected] Bug: flutter/flutter#189234 Change-Id: If514d73dd39fd4a6cb17633d6818e496ade07ca8 Reviewed-on: https://skia-review.googlesource.com/c/skia/+/1406081 Commit-Queue: Julia Lavrova <[email protected]> Reviewed-by: Julia Lavrova <[email protected]> Reviewed-by: Alexis Cruz-Ayala <[email protected]>
1 parent ce0b85b commit f326a1e

2 files changed

Lines changed: 61 additions & 1 deletion

File tree

‎modules/skparagraph/src/TextLine.cpp‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1595,8 +1595,10 @@ PositionWithAffinity TextLine::getGlyphPositionAtCoordinate(SkScalar dx) {
15951595
*runWidthInLine = run->advance().fX;
15961596
if (fOwner->paragraphStyle().getTextDirection() == TextDirection::kLtr ||
15971597
dx <= this->offset().fX + runOffsetInLine + run->advance().fX) {
1598+
// Like any other end of line, the position belongs to this line, not to
1599+
// the start of the next one
15981600
auto utf16Index = fOwner->getUTF16Index(this->fText.end);
1599-
result = { SkToS32(utf16Index) , kDownstream };
1601+
result = {SkToS32(utf16Index), utf16Index > 0 ? kUpstream : kDownstream};
16001602
return false;
16011603
}
16021604
return true;

‎modules/skparagraph/tests/SkParagraphTest.cpp‎

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11466,6 +11466,64 @@ UNIX_ONLY_TEST(SkParagraph_SoftHyphenFallbackFontWidth, reporter) {
1146611466
minWidth);
1146711467
}
1146811468

11469+
// A hit on the rendered hyphen is at the end of its line, upstream, as at the end of any other
11470+
// line. Downstream would put it at the start of the next line, and getClosestUTF16GlyphInfoAt
11471+
// would return the next line's first glyph instead of the soft hyphen.
11472+
UNIX_ONLY_TEST(SkParagraph_SoftHyphenHitIsUpstream, reporter) {
11473+
sk_sp<ResourceFontCollection> fontCollection = sk_make_sp<ResourceFontCollection>(true);
11474+
SKIP_IF_FONTS_NOT_FOUND(reporter, fontCollection)
11475+
11476+
const size_t softHyphen = 5;
11477+
auto paragraph = layoutWithSoftHyphens(fontCollection, "inter~national", 80);
11478+
checkLines(reporter, paragraph.get(), "inter-|national");
11479+
auto& line = static_cast<ParagraphImpl*>(paragraph.get())->lines()[0];
11480+
SkScalar x = line.offset().fX + (line.widthWithoutEllipsis() + line.width()) / 2;
11481+
SkScalar y = line.offset().fY + line.height() / 2;
11482+
11483+
auto position = paragraph->getGlyphPositionAtCoordinate(x, y);
11484+
REPORTER_ASSERT(reporter, position.position == softHyphen + 1, "%d", position.position);
11485+
REPORTER_ASSERT(reporter, position.affinity == Affinity::kUpstream);
11486+
11487+
Paragraph::GlyphInfo glyphInfo;
11488+
REPORTER_ASSERT(reporter, paragraph->getClosestUTF16GlyphInfoAt(x, y, &glyphInfo));
11489+
REPORTER_ASSERT(reporter,
11490+
glyphInfo.fGraphemeClusterTextRange == TextRange(softHyphen, softHyphen + 1),
11491+
"[%zu, %zu)",
11492+
glyphInfo.fGraphemeClusterTextRange.start,
11493+
glyphInfo.fGraphemeClusterTextRange.end);
11494+
}
11495+
11496+
// Likewise for a hit on the ellipsis, which is at the end of the truncated text. Downstream there
11497+
// is past the line's text, so getClosestUTF16GlyphInfoAt found no glyph at all.
11498+
UNIX_ONLY_TEST(SkParagraph_EllipsisHitIsUpstream, reporter) {
11499+
sk_sp<ResourceFontCollection> fontCollection = sk_make_sp<ResourceFontCollection>(true);
11500+
SKIP_IF_FONTS_NOT_FOUND(reporter, fontCollection)
11501+
11502+
SkScalar width = singleLineWidth(fontCollection, "abc...") + 1;
11503+
auto paragraph =
11504+
layoutWithSoftHyphens(fontCollection, "abcdef", width, [](ParagraphStyle& style) {
11505+
style.setMaxLines(1);
11506+
style.setEllipsis(SkString("..."));
11507+
});
11508+
auto& line = static_cast<ParagraphImpl*>(paragraph.get())->lines()[0];
11509+
REPORTER_ASSERT(reporter, line.ellipsis() != nullptr);
11510+
REPORTER_ASSERT(reporter, line.trimmedText() == TextRange(0, 3));
11511+
SkScalar x = line.offset().fX + (line.widthWithoutEllipsis() + line.width()) / 2;
11512+
SkScalar y = line.offset().fY + line.height() / 2;
11513+
11514+
auto position = paragraph->getGlyphPositionAtCoordinate(x, y);
11515+
REPORTER_ASSERT(reporter, position.position == 3, "%d", position.position);
11516+
REPORTER_ASSERT(reporter, position.affinity == Affinity::kUpstream);
11517+
11518+
Paragraph::GlyphInfo glyphInfo;
11519+
REPORTER_ASSERT(reporter, paragraph->getClosestUTF16GlyphInfoAt(x, y, &glyphInfo));
11520+
REPORTER_ASSERT(reporter,
11521+
glyphInfo.fGraphemeClusterTextRange == TextRange(2, 3),
11522+
"[%zu, %zu)",
11523+
glyphInfo.fGraphemeClusterTextRange.start,
11524+
glyphInfo.fGraphemeClusterTextRange.end);
11525+
}
11526+
1146911527
// Only a soft hyphen's own width changes how a too-long word is measured. Other soft breaks, such
1147011528
// as the end of the text, still end the word before their cluster, so a word too long for the
1147111529
// line at the end of the text moves to the next line before it's split, as it always has

0 commit comments

Comments
 (0)