Skip to content

Commit a040fd4

Browse files
Rusinoskcq-be@skia-corp.google.com.iam.gserviceaccount.com
authored andcommitted
Reland "[skparagraph] Cap letter spacing line shift"
This is a reland of commit 9628428 The original change broke the Flutter roll: the SkParagraph kitchensink test in the web engine CanvasKit suite pinned LineMetrics.left to 2.5 on a line that starts with letter spacing 5 and ends with an unspaced "!", where the capped shift is 0. That test now accepts both values (flutter/flutter#196148). The code is the same as in the original change. Original change's description: > [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. > > Bug: flutter/flutter#184815 > Reviewed-on: https://skia-review.googlesource.com/c/skia/+/1418376 > Reviewed-by: Florin Malita <[email protected]> Without the fix, SkParagraph_LetterSpacingStaysInsideLineWidth fails 16 asserts, e.g. glyphs end at 348.80 while the max intrinsic width is 333.80. Tested: - Skia dm unit tests, SkParagraph (Linux x86_64, Debug and Release, Clang 22.1.0, FreeType + Fontconfig + HarfBuzz, ICU, extra_fonts): 235/235 pass in both builds (234/234 before, plus the new test). - Flutter engine text tests (Linux x86_64 host_debug_unopt, ICU), Flutter 7ce4746c92ea with this change vs the parent Skia: 2413/2413 pass on both sides (103 txt/ui C++ unit tests, 2310 Dart framework and engine tests). - Flutter web CanvasKit suite (chrome-dart2js-canvaskit-canvaskit, wasm release rebuilt with each Skia): 362 passed, 1 skipped, 0 failed on both sides, including the SkParagraph kitchensink test. - google3 SkParagraph clients (1151 test targets): no regressions (one golden test is flaky with and without this change). - Flutter txt_benchmarks (Linux x86_64, 20 interleaved rounds on one core): ShortLayout, LongLayout, JustifyLayout and TextBigO/1024 are within 2% of the parent Skia. - Skia trybots pass: Test-Ubuntu24.04-Clang-GCE-CPU-AVX2-x86_64-Debug-All-NativeFonts_FreeType_ASAN_Upload Test-Mac14-Clang-MacMini8.1-CPU-AVX2-x86_64-Debug-All-NativeFonts_Fontations Not tested: Windows, Android, iOS. Bug: flutter/flutter#184815 Assisted-by: Claude Opus 5.5 (Medium) Change-Id: Id0e890c4aeaa64f0ed54554ae0cca341987a4d35 Reviewed-on: https://skia-review.googlesource.com/c/skia/+/1422517 Commit-Queue: Julia Lavrova <[email protected]> Reviewed-by: Florin Malita <[email protected]>
1 parent 108e8ef commit a040fd4

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
@@ -211,6 +211,23 @@ TextLine::TextLine(ParagraphImpl* owner,
211211
break;
212212
}
213213
}
214+
// Glyphs of every run, right to left ones too, keep their whole letter spacing on their
215+
// right side, so the shift keeps the ink inside the line width only up to the half spacing
216+
// of the visually rightmost text cluster (same as with uniform spacing). Trailing
217+
// placeholders keep the shift (InlinePlaceholder tests pin it)
218+
if (fShift > 0) {
219+
for (auto visual = fRunsInVisualOrder.size(); visual > 0; --visual) {
220+
const Run& run = owner->run(fRunsInVisualOrder[visual - 1]);
221+
ClusterRange inLine = intersected(run.clusterRange(), fClusterRange);
222+
if (run.isPlaceholder() || inLine.width() == 0) {
223+
continue;
224+
}
225+
const Cluster& right =
226+
owner->cluster(run.leftToRight() ? inLine.end - 1 : inLine.start);
227+
fShift = std::min(fShift, std::max(right.getHalfLetterSpacing(), 0.0f));
228+
break;
229+
}
230+
}
214231
}
215232

216233
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
@@ -12811,6 +12811,133 @@ DEF_TEST(SkParagraph_GlyphOffsetsOnlyWhenNeeded, reporter) {
1281112811
REPORTER_ASSERT(reporter, movedUpOrDown);
1281212812
}
1281312813

12814+
// Letter spacing is part of the cluster widths that line breaking and intrinsic widths add up,
12815+
// so a left aligned line must draw and report its glyphs inside that width, even when only the
12816+
// start of the line is letter spaced
12817+
DEF_TEST(SkParagraph_LetterSpacingStaysInsideLineWidth, reporter) {
12818+
sk_sp<ResourceFontCollection> fontCollection = sk_make_sp<ResourceFontCollection>();
12819+
SKIP_IF_FONTS_NOT_FOUND(reporter, fontCollection)
12820+
fontCollection->disableFontFallback();
12821+
12822+
TextStyle plain;
12823+
plain.setFontFamilies({SkString("Roboto"), SkString("Noto Naskh Arabic")});
12824+
plain.setFontSize(14);
12825+
plain.setColor(SK_ColorBLACK);
12826+
TextStyle spaced = plain;
12827+
spaced.setLetterSpacing(30);
12828+
TextStyle tight = plain;
12829+
tight.setLetterSpacing(-4);
12830+
12831+
using Segments = std::vector<std::pair<const TextStyle*, const char*>>;
12832+
auto build = [&](size_t maxLines, const Segments& segments, int count,
12833+
TextDirection direction = TextDirection::kLtr) {
12834+
ParagraphStyle paragraphStyle;
12835+
paragraphStyle.setTextStyle(plain);
12836+
paragraphStyle.setMaxLines(maxLines);
12837+
paragraphStyle.setTextDirection(direction);
12838+
// Right or centered lines replace the shift with the alignment offset
12839+
paragraphStyle.setTextAlign(TextAlign::kLeft);
12840+
ParagraphBuilderImpl builder(paragraphStyle, fontCollection, get_unicode());
12841+
for (int i = 0; i < count; ++i) {
12842+
for (auto& [style, text] : segments) {
12843+
builder.pushStyle(*style);
12844+
builder.addText(text);
12845+
builder.pop();
12846+
}
12847+
}
12848+
return builder.Build();
12849+
};
12850+
12851+
auto checkInside = [&](Paragraph* paragraph, SkScalar width, bool checkBoxes = true) {
12852+
const SkScalar epsilon = 0.01f;
12853+
SkScalar drawnLeft = 0;
12854+
SkScalar drawnRight = 0;
12855+
paragraph->visit([&](int, const Paragraph::VisitorInfo* info) {
12856+
if (!info) {
12857+
return;
12858+
}
12859+
std::vector<SkScalar> advances(SkToSizeT(info->count));
12860+
info->font.getWidths({info->glyphs, SkToSizeT(info->count)}, advances);
12861+
for (int i = 0; i < info->count; ++i) {
12862+
SkScalar x = info->origin.fX + info->positions[i].fX;
12863+
drawnLeft = std::min(drawnLeft, x);
12864+
drawnRight = std::max(drawnRight, x + advances[i]);
12865+
}
12866+
});
12867+
REPORTER_ASSERT(reporter, drawnLeft >= -epsilon, "%f", drawnLeft);
12868+
REPORTER_ASSERT(reporter, drawnRight <= width + epsilon, "%f > %f", drawnRight, width);
12869+
if (!checkBoxes) {
12870+
return;
12871+
}
12872+
auto boxes = paragraph->getRectsForRange(
12873+
0, 1000, RectHeightStyle::kTight, RectWidthStyle::kTight);
12874+
REPORTER_ASSERT(reporter, !boxes.empty());
12875+
for (auto& box : boxes) {
12876+
REPORTER_ASSERT(reporter, box.rect.fLeft >= -epsilon, "%f", box.rect.fLeft);
12877+
REPORTER_ASSERT(reporter, box.rect.fRight <= width + epsilon,
12878+
"%f > %f", box.rect.fRight, width);
12879+
}
12880+
};
12881+
12882+
// Lines end with plain text after letter spaced text; maxLines makes Flutter clip the text
12883+
// at the max intrinsic width
12884+
auto clipped = build(3, {{&spaced, "-1 12345"}, {&plain, "67890\n"}}, 5);
12885+
clipped->layout(1000);
12886+
REPORTER_ASSERT(reporter, clipped->didExceedMaxLines());
12887+
checkInside(clipped.get(), clipped->getMaxIntrinsicWidth());
12888+
checkInside(clipped.get(), clipped->getLongestLine());
12889+
12890+
// A letter spaced start followed by a long plain word that wraps at the paragraph width
12891+
auto wrapped = build(std::numeric_limits<size_t>::max(),
12892+
{{&spaced, "12345"},
12893+
{&plain, "678901234567890123456789012345678901234567890"}}, 1);
12894+
wrapped->layout(300);
12895+
REPORTER_ASSERT(reporter, wrapped->lineNumber() > 1);
12896+
checkInside(wrapped.get(), 300);
12897+
12898+
// The line ends with negative letter spacing, which must not move the line left; the last
12899+
// glyph still draws its whole advance, 4 px past its tight cell
12900+
auto negative = build(1, {{&spaced, "12345"}, {&tight, "67890"}}, 1);
12901+
negative->layout(1000);
12902+
checkInside(negative.get(), negative->getMaxIntrinsicWidth() + 4);
12903+
12904+
// The line ends with a cursive script, which gets no letter spacing
12905+
auto cursive = build(1, {{&spaced, "12345 \u0645\u0631\u062D\u0628\u0627"}}, 1);
12906+
cursive->layout(1000);
12907+
checkInside(cursive.get(), cursive->getMaxIntrinsicWidth());
12908+
12909+
// Glyphs of right to left runs are laid out left to right too, each with its spacing on the
12910+
// right, so the visually rightmost cluster decides whether the shift fits
12911+
const char* spacedHebrew = "\u05D0\u05D1\u05D2\u05D3\u05D4";
12912+
const char* plainHebrew = "\u05D5\u05D6\u05D7\u05D8\u05D9";
12913+
auto drawnLeft = [](Paragraph* paragraph) {
12914+
SkScalar left = SK_ScalarMax;
12915+
paragraph->visit([&](int, const Paragraph::VisitorInfo* info) {
12916+
if (info) {
12917+
left = std::min(left, info->origin.fX + info->positions[0].fX);
12918+
}
12919+
});
12920+
return left;
12921+
};
12922+
12923+
// Right to left text with a letter spaced logical start: that start is the visually
12924+
// rightmost cluster, so the line fits with the shift and keeps it. Only the ink is checked:
12925+
// the box of a right to left cluster includes its spacing even at the visual line end
12926+
auto rtl = build(1, {{&spaced, spacedHebrew}, {&plain, plainHebrew}}, 1,
12927+
TextDirection::kRtl);
12928+
rtl->layout(1000);
12929+
checkInside(rtl.get(), rtl->getMaxIntrinsicWidth(), /*checkBoxes=*/false);
12930+
REPORTER_ASSERT(reporter, SkScalarNearlyEqual(drawnLeft(rtl.get()), 15), "%f",
12931+
drawnLeft(rtl.get()));
12932+
12933+
// A right to left run at the end of a left to right line, letter spaced only at its logical
12934+
// end: its visually rightmost cluster is plain
12935+
auto mixed = build(1, {{&spaced, "12345"}, {&plain, plainHebrew}, {&spaced, spacedHebrew}},
12936+
1);
12937+
mixed->layout(1000);
12938+
checkInside(mixed.get(), mixed->getMaxIntrinsicWidth());
12939+
}
12940+
1281412941
// When an unresolved combining mark attaches to the non-initial codepoint of a multi-char
1281512942
// ligature, clusteredText must expand both the glyph range and text range to the whole ligature
1281612943
DEF_TEST(SkParagraph_LigatureWithUnresolvedMark, reporter) {

0 commit comments

Comments
 (0)