Skip to content

Commit 9628428

Browse files
Rusinoskcq-be@skia-corp.google.com.iam.gserviceaccount.com
authored andcommitted
[skparagraph] Cap letter spacing line shift
TextLine shifts the whole line right by half the letter spacing of its first cluster, so glyphs look centered in their spaced cells as Flutter expects. Cluster widths keep the whole spacing after the glyph, and line widths do not count the shift. When a line starts with letter spaced text and ends with plain text, negative spacing, or a cursive script (which gets no spacing), the last glyphs and their boxes end up to half the spacing past the line width. Flutter clips text at the max intrinsic width when maxLines is reached, so the last characters disappear. Now the shift is at most the half spacing of the visually rightmost text cluster of the line, and never negative. Glyphs of right to left runs are laid out left to right as well, each with its spacing on the right, so for such a run that cluster is its logical start. Lines with the same spacing at both visual ends keep the old shift, so uniform letter spacing and right to left lines that start letter spaced do not change. Trailing placeholders keep the shift, as the inline placeholder tests expect. Without the fix, SkParagraph_LetterSpacingStaysInsideLineWidth fails 16 asserts, e.g. glyphs end at 348.80 while the max intrinsic width is 333.80. Not covered: a letter spaced line with an ellipsis or a soft hyphen still draws it past the width (they are added after the shift is set). Tested: - Skia dm --src tests --nogpu --nativeFonts --paragraph_fonts extra_fonts -m SkParagraph, Linux x86_64, Clang, FreeType, ICU, Debug (234 tests) and Release (232 tests): all pass except SkParagraph_MultiStyle_Zalgo, which fails the same way without this change (checked in both builds) - Flutter engine paragraph corpus on Linux x86_64 (host_debug_unopt, Clang, FreeType, ICU): 2409 tests (99 C++ tests in txt_unittests and ui_unittests plus 2310 Dart layout, editing, and text field tests), 0 regressions; full framework corpus (20198 tests in 921 files): same results with and without the final version of the change (only the pre-existing cupertino popup_surface_test golden fails in both) - Flutter engine txt_benchmarks A/B on Linux x86_64 (host_release, Clang, FreeType, ICU, 20 interleaved rounds): neutral across all 4 benchmarks (ShortLayout ratio 0.9941, 95% CI [0.9866, 1.0005]; LongLayout 1.0068, [0.9901, 1.0145]; JustifyLayout 1.0032, [0.9966, 1.0121]; TextBigO/1024 0.9908, [0.9824, 1.0041]) - google3 SkParagraph clients (1151 test targets, including slow tier, Linux x86_64): no regressions - Skia trybots, green: Test-Ubuntu24.04-Clang-GCE-CPU-AVX2-x86_64-Debug-All-NativeFonts_FreeType_ASAN_Upload Test-Ubuntu24.04-Clang-GCE-CPU-AVX2-x86_64-Release-All Test-Ubuntu24.04-Clang-GCE-CPU-AVX2-x86_64-Release-All-MSAN Test-Ubuntu24.04-Clang-GCE-CPU-AVX2-x86_64-Release-All-NativeFonts_Fontations Test-Mac14-Clang-MacMini8.1-CPU-AVX2-x86_64-Debug-All-NativeFonts_Fontations Test-Mac15-Clang-MacMini16.10-CPU-AppleM4-arm64-Debug-All-NativeFonts Test-Win11-Clang-GCE-CPU-AVX2-x86_64-Debug-All-NativeFonts Test-Win11-MSVC-GCE-CPU-AVX2-x86_64-Release-All Test-Android-Clang-GalaxyS24-CPU-Adreno750-arm64-Release-All-Android_NativeFonts_AndroidNDKFonts (the new test is skipped there: no paragraph test fonts on Android) Not tested: the new test on Android or iOS, Canary-Chromium, Canary-Android Bug: flutter/flutter#184815 Assisted-by: Claude Opus 5.5 (Medium) Change-Id: I5f1b165d12eae076b3be8e9d51e49a80020b0181 Reviewed-on: https://skia-review.googlesource.com/c/skia/+/1418376 Commit-Queue: Julia Lavrova <[email protected]> Reviewed-by: Florin Malita <[email protected]>
1 parent b11315d commit 9628428

2 files changed

Lines changed: 144 additions & 0 deletions

File tree

‎modules/skparagraph/src/TextLine.cpp‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,23 @@ TextLine::TextLine(ParagraphImpl* owner,
222222
break;
223223
}
224224
}
225+
// Glyphs of every run, right to left ones too, keep their whole letter spacing on their
226+
// right side, so the shift keeps the ink inside the line width only up to the half spacing
227+
// of the visually rightmost text cluster (same as with uniform spacing). Trailing
228+
// placeholders keep the shift (InlinePlaceholder tests pin it)
229+
if (fShift > 0) {
230+
for (auto visual = fRunsInVisualOrder.size(); visual > 0; --visual) {
231+
const Run& run = owner->run(fRunsInVisualOrder[visual - 1]);
232+
ClusterRange inLine = intersected(run.clusterRange(), fClusterRange);
233+
if (run.isPlaceholder() || inLine.width() == 0) {
234+
continue;
235+
}
236+
const Cluster& right =
237+
owner->cluster(run.leftToRight() ? inLine.end - 1 : inLine.start);
238+
fShift = std::min(fShift, std::max(right.getHalfLetterSpacing(), 0.0f));
239+
break;
240+
}
241+
}
225242
}
226243

227244
void TextLine::paint(ParagraphPainter* painter, SkScalar x, SkScalar y) {

‎modules/skparagraph/tests/SkParagraphTest.cpp‎

Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12762,3 +12762,130 @@ DEF_TEST(SkParagraph_GlyphOffsetsOnlyWhenNeeded, reporter) {
1276212762
});
1276312763
REPORTER_ASSERT(reporter, movedUpOrDown);
1276412764
}
12765+
12766+
// Letter spacing is part of the cluster widths that line breaking and intrinsic widths add up,
12767+
// so a left aligned line must draw and report its glyphs inside that width, even when only the
12768+
// start of the line is letter spaced
12769+
DEF_TEST(SkParagraph_LetterSpacingStaysInsideLineWidth, reporter) {
12770+
sk_sp<ResourceFontCollection> fontCollection = sk_make_sp<ResourceFontCollection>();
12771+
SKIP_IF_FONTS_NOT_FOUND(reporter, fontCollection)
12772+
fontCollection->disableFontFallback();
12773+
12774+
TextStyle plain;
12775+
plain.setFontFamilies({SkString("Roboto"), SkString("Noto Naskh Arabic")});
12776+
plain.setFontSize(14);
12777+
plain.setColor(SK_ColorBLACK);
12778+
TextStyle spaced = plain;
12779+
spaced.setLetterSpacing(30);
12780+
TextStyle tight = plain;
12781+
tight.setLetterSpacing(-4);
12782+
12783+
using Segments = std::vector<std::pair<const TextStyle*, const char*>>;
12784+
auto build = [&](size_t maxLines, const Segments& segments, int count,
12785+
TextDirection direction = TextDirection::kLtr) {
12786+
ParagraphStyle paragraphStyle;
12787+
paragraphStyle.setTextStyle(plain);
12788+
paragraphStyle.setMaxLines(maxLines);
12789+
paragraphStyle.setTextDirection(direction);
12790+
// Right or centered lines replace the shift with the alignment offset
12791+
paragraphStyle.setTextAlign(TextAlign::kLeft);
12792+
ParagraphBuilderImpl builder(paragraphStyle, fontCollection, get_unicode());
12793+
for (int i = 0; i < count; ++i) {
12794+
for (auto& [style, text] : segments) {
12795+
builder.pushStyle(*style);
12796+
builder.addText(text);
12797+
builder.pop();
12798+
}
12799+
}
12800+
return builder.Build();
12801+
};
12802+
12803+
auto checkInside = [&](Paragraph* paragraph, SkScalar width, bool checkBoxes = true) {
12804+
const SkScalar epsilon = 0.01f;
12805+
SkScalar drawnLeft = 0;
12806+
SkScalar drawnRight = 0;
12807+
paragraph->visit([&](int, const Paragraph::VisitorInfo* info) {
12808+
if (!info) {
12809+
return;
12810+
}
12811+
std::vector<SkScalar> advances(SkToSizeT(info->count));
12812+
info->font.getWidths({info->glyphs, SkToSizeT(info->count)}, advances);
12813+
for (int i = 0; i < info->count; ++i) {
12814+
SkScalar x = info->origin.fX + info->positions[i].fX;
12815+
drawnLeft = std::min(drawnLeft, x);
12816+
drawnRight = std::max(drawnRight, x + advances[i]);
12817+
}
12818+
});
12819+
REPORTER_ASSERT(reporter, drawnLeft >= -epsilon, "%f", drawnLeft);
12820+
REPORTER_ASSERT(reporter, drawnRight <= width + epsilon, "%f > %f", drawnRight, width);
12821+
if (!checkBoxes) {
12822+
return;
12823+
}
12824+
auto boxes = paragraph->getRectsForRange(
12825+
0, 1000, RectHeightStyle::kTight, RectWidthStyle::kTight);
12826+
REPORTER_ASSERT(reporter, !boxes.empty());
12827+
for (auto& box : boxes) {
12828+
REPORTER_ASSERT(reporter, box.rect.fLeft >= -epsilon, "%f", box.rect.fLeft);
12829+
REPORTER_ASSERT(reporter, box.rect.fRight <= width + epsilon,
12830+
"%f > %f", box.rect.fRight, width);
12831+
}
12832+
};
12833+
12834+
// Lines end with plain text after letter spaced text; maxLines makes Flutter clip the text
12835+
// at the max intrinsic width
12836+
auto clipped = build(3, {{&spaced, "-1 12345"}, {&plain, "67890\n"}}, 5);
12837+
clipped->layout(1000);
12838+
REPORTER_ASSERT(reporter, clipped->didExceedMaxLines());
12839+
checkInside(clipped.get(), clipped->getMaxIntrinsicWidth());
12840+
checkInside(clipped.get(), clipped->getLongestLine());
12841+
12842+
// A letter spaced start followed by a long plain word that wraps at the paragraph width
12843+
auto wrapped = build(std::numeric_limits<size_t>::max(),
12844+
{{&spaced, "12345"},
12845+
{&plain, "678901234567890123456789012345678901234567890"}}, 1);
12846+
wrapped->layout(300);
12847+
REPORTER_ASSERT(reporter, wrapped->lineNumber() > 1);
12848+
checkInside(wrapped.get(), 300);
12849+
12850+
// The line ends with negative letter spacing, which must not move the line left; the last
12851+
// glyph still draws its whole advance, 4 px past its tight cell
12852+
auto negative = build(1, {{&spaced, "12345"}, {&tight, "67890"}}, 1);
12853+
negative->layout(1000);
12854+
checkInside(negative.get(), negative->getMaxIntrinsicWidth() + 4);
12855+
12856+
// The line ends with a cursive script, which gets no letter spacing
12857+
auto cursive = build(1, {{&spaced, "12345 \u0645\u0631\u062D\u0628\u0627"}}, 1);
12858+
cursive->layout(1000);
12859+
checkInside(cursive.get(), cursive->getMaxIntrinsicWidth());
12860+
12861+
// Glyphs of right to left runs are laid out left to right too, each with its spacing on the
12862+
// right, so the visually rightmost cluster decides whether the shift fits
12863+
const char* spacedHebrew = "\u05D0\u05D1\u05D2\u05D3\u05D4";
12864+
const char* plainHebrew = "\u05D5\u05D6\u05D7\u05D8\u05D9";
12865+
auto drawnLeft = [](Paragraph* paragraph) {
12866+
SkScalar left = SK_ScalarMax;
12867+
paragraph->visit([&](int, const Paragraph::VisitorInfo* info) {
12868+
if (info) {
12869+
left = std::min(left, info->origin.fX + info->positions[0].fX);
12870+
}
12871+
});
12872+
return left;
12873+
};
12874+
12875+
// Right to left text with a letter spaced logical start: that start is the visually
12876+
// rightmost cluster, so the line fits with the shift and keeps it. Only the ink is checked:
12877+
// the box of a right to left cluster includes its spacing even at the visual line end
12878+
auto rtl = build(1, {{&spaced, spacedHebrew}, {&plain, plainHebrew}}, 1,
12879+
TextDirection::kRtl);
12880+
rtl->layout(1000);
12881+
checkInside(rtl.get(), rtl->getMaxIntrinsicWidth(), /*checkBoxes=*/false);
12882+
REPORTER_ASSERT(reporter, SkScalarNearlyEqual(drawnLeft(rtl.get()), 15), "%f",
12883+
drawnLeft(rtl.get()));
12884+
12885+
// A right to left run at the end of a left to right line, letter spaced only at its logical
12886+
// end: its visually rightmost cluster is plain
12887+
auto mixed = build(1, {{&spaced, "12345"}, {&plain, plainHebrew}, {&spaced, spacedHebrew}},
12888+
1);
12889+
mixed->layout(1000);
12890+
checkInside(mixed.get(), mixed->getMaxIntrinsicWidth());
12891+
}

0 commit comments

Comments
 (0)