Skip to content

Commit fcf3a18

Browse files
dbebawyskcq-be@skia-corp.google.com.iam.gserviceaccount.com
authored andcommitted
[skparagraph] Use surrounding run's bidi level in shapeEllipsis
shapeEllipsis passed SkBidiIterator::kLTR to its internal BidiRunIterator regardless of the surrounding paragraph's direction, so in RTL text the affix's neutral and weak characters resolved LTR. For the default "-" and "…" this only gives the shaped run the wrong bidi level; the glyphs are the same. But a custom ellipsis that mixes RTL letters with a neutral, such as "النهاية!", is split into two bidi runs (the "!" resolves LTR), and ShapeHandler supports only one: debug builds fail SkASSERT(!fRun), and release builds keep only the last run. The fix uses the bidi level of the surrounding cluster's run, which is already in scope via the existing 'cluster' parameter. Both call sites (createEllipsis, createSoftHyphen) pass the cluster adjacent to the break, so the affix inherits the correct direction at the shaping boundary. This also unblocks a follow-up that exposes a configurable soft hyphen string, and addresses reviewer feedback on the Flutter design doc for flutter/flutter#18443. Tests: - SkParagraph_SoftHyphenRTLBidiLevel / SoftHyphenLTRBidiLevel assert the shaped hyphen run's bidi level matches the paragraph context. - SkParagraph_EllipsisRTLCustomStringBidiLevel uses "النهاية!" as a custom ellipsis in an RTL paragraph (hits the SkASSERT without this change). - SkParagraph_EllipsisLTRDefaultBidiLevel guards the common LTR path against regression. GM: - paragraph_ellipsis_bidi adds Skia Gold visual coverage across LTR/RTL x default/custom affix combinations. [email protected], [email protected] Change-Id: Id51f71e4506272662a95baae197bca808d27e7d1 Bug: flutter/flutter#189617 Reviewed-on: https://skia-review.googlesource.com/c/skia/+/1217709 Reviewed-by: Julia Lavrova <[email protected]> Auto-Submit: David H Bebawy <[email protected]> Commit-Queue: Alexis Cruz-Ayala <[email protected]> Reviewed-by: Alexis Cruz-Ayala <[email protected]>
1 parent 9954a85 commit fcf3a18

5 files changed

Lines changed: 313 additions & 3 deletions

File tree

‎modules/skparagraph/BUILD.gn‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,10 @@ if (skia_enable_skparagraph && skia_enable_skshaper && skia_enable_skunicode &&
5454
if (paragraph_gms_enabled &&
5555
(skia_use_icu || skia_use_libgrapheme || skia_use_icu4x)) {
5656
testonly = true
57-
sources = [ "gm/simple_gm.cpp" ]
57+
sources = [
58+
"gm/ellipsis_bidi_gm.cpp",
59+
"gm/simple_gm.cpp",
60+
]
5861
deps = [
5962
":skparagraph",
6063
"../..:skia",
Lines changed: 149 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,149 @@
1+
/*
2+
* Copyright 2026 Google LLC
3+
*
4+
* Use of this source code is governed by a BSD-style license that can be
5+
* found in the LICENSE file.
6+
*/
7+
8+
#include "gm/gm.h"
9+
#include "include/core/SkCanvas.h"
10+
#include "include/core/SkColor.h"
11+
#include "include/core/SkFont.h"
12+
#include "include/core/SkFontMgr.h"
13+
#include "include/core/SkPaint.h"
14+
#include "include/core/SkRect.h"
15+
#include "include/core/SkString.h"
16+
#include "modules/skparagraph/include/FontCollection.h"
17+
#include "modules/skparagraph/include/Paragraph.h"
18+
#include "modules/skparagraph/include/ParagraphStyle.h"
19+
#include "modules/skparagraph/include/TextStyle.h"
20+
#include "modules/skparagraph/src/ParagraphBuilderImpl.h"
21+
#include "modules/skshaper/utils/FactoryHelpers.h"
22+
#include "tools/fonts/FontToolUtils.h"
23+
24+
// Visual coverage for ellipsis shaping across bidi contexts. The ellipsis is
25+
// shaped with the surrounding run's bidi level, so neutral characters in a
26+
// custom ellipsis (like the "!" in the Arabic row) take the paragraph's
27+
// direction instead of LTR, which would split the ellipsis into two bidi runs.
28+
// The default "…" (ON-class) rows act as regression guards for the common
29+
// path.
30+
31+
namespace {
32+
33+
constexpr SkScalar kRowHeight = 80;
34+
// Narrower than every row's text, so each row truncates and shows its ellipsis.
35+
constexpr SkScalar kLayoutWidth = 180;
36+
constexpr SkScalar kCanvasWidth = 640;
37+
constexpr int kRowCount = 4;
38+
// Visual gap between row baselines and the debug border so descenders aren't
39+
// clipped by the border rectangle drawn around each paragraph's layout box.
40+
constexpr SkScalar kRowBorderInset = 10;
41+
42+
} // namespace
43+
44+
class EllipsisBidiGM : public skiagm::GM {
45+
public:
46+
EllipsisBidiGM() = default;
47+
48+
protected:
49+
SkString getName() const override { return SkString("paragraph_ellipsis_bidi"); }
50+
51+
SkISize getISize() override {
52+
return SkISize::Make(static_cast<int>(kCanvasWidth),
53+
static_cast<int>(kRowHeight * kRowCount + 20));
54+
}
55+
56+
DrawResult onDraw(SkCanvas* canvas, SkString* errorMsg) override {
57+
sk_sp<SkFontMgr> fontmgr = ToolUtils::TestFontMgr();
58+
if (fontmgr->countFamilies() == 0) {
59+
*errorMsg = "No fonts available.";
60+
return DrawResult::kSkip;
61+
}
62+
auto collection = sk_make_sp<skia::textlayout::FontCollection>();
63+
collection->setDefaultFontManager(fontmgr);
64+
collection->enableFontFallback();
65+
66+
auto unicode = SkShapers::BestAvailableUnicode();
67+
if (!unicode) {
68+
*errorMsg = "No SkUnicode implementation available.";
69+
return DrawResult::kSkip;
70+
}
71+
72+
canvas->clear(SK_ColorWHITE);
73+
74+
SkScalar y = 10;
75+
76+
// Row 1: LTR paragraph, default "…" ellipsis (ON-class, regression guard).
77+
drawRow(canvas, 10, y, collection, unicode,
78+
skia::textlayout::TextDirection::kLtr,
79+
u"The quick brown fox jumps over the lazy dog",
80+
u"\u2026");
81+
y += kRowHeight;
82+
83+
// Row 2: LTR paragraph, custom ellipsis containing strong-LTR letters.
84+
drawRow(canvas, 10, y, collection, unicode,
85+
skia::textlayout::TextDirection::kLtr,
86+
u"The quick brown fox jumps over the lazy dog",
87+
u"[more]");
88+
y += kRowHeight;
89+
90+
// Row 3: RTL paragraph, default "…" ellipsis.
91+
drawRow(canvas, 10, y, collection, unicode,
92+
skia::textlayout::TextDirection::kRtl,
93+
u"\u062A\u0638\u0627\u0647\u0631\u0627\u062A \u0648 "
94+
u"\u062A\u062C\u0645\u0639\u0627\u062A",
95+
u"\u2026");
96+
y += kRowHeight;
97+
98+
// Row 4: RTL paragraph, custom Arabic ellipsis "النهاية!".
99+
// This is the case fixed by using the surrounding cluster's bidi level
100+
// in shapeEllipsis — with a hardcoded LTR level, the trailing "!"
101+
// resolved LTR and split the ellipsis into two runs.
102+
drawRow(canvas, 10, y, collection, unicode,
103+
skia::textlayout::TextDirection::kRtl,
104+
u"\u062A\u0638\u0627\u0647\u0631\u0627\u062A \u0648 "
105+
u"\u062A\u062C\u0645\u0639\u0627\u062A",
106+
u"\u0627\u0644\u0646\u0647\u0627\u064A\u0629!");
107+
108+
return DrawResult::kOk;
109+
}
110+
111+
private:
112+
static void drawRow(SkCanvas* canvas,
113+
SkScalar x,
114+
SkScalar y,
115+
const sk_sp<skia::textlayout::FontCollection>& collection,
116+
const sk_sp<SkUnicode>& unicode,
117+
skia::textlayout::TextDirection direction,
118+
const char16_t* text,
119+
const char16_t* ellipsis) {
120+
skia::textlayout::TextStyle textStyle;
121+
textStyle.setFontFamilies({SkString("Noto Naskh Arabic"), SkString("Roboto")});
122+
textStyle.setFontSize(30);
123+
textStyle.setColor(SK_ColorBLACK);
124+
125+
skia::textlayout::ParagraphStyle paragraphStyle;
126+
paragraphStyle.setTextStyle(textStyle);
127+
paragraphStyle.setTextDirection(direction);
128+
paragraphStyle.setEllipsis(std::u16string(ellipsis));
129+
paragraphStyle.setMaxLines(1);
130+
131+
auto builder = skia::textlayout::ParagraphBuilderImpl::make(
132+
paragraphStyle, collection, unicode);
133+
if (!builder) {
134+
return;
135+
}
136+
builder->addText(std::u16string(text));
137+
auto paragraph = builder->Build();
138+
paragraph->layout(kLayoutWidth);
139+
paragraph->paint(canvas, x, y);
140+
141+
SkPaint border;
142+
border.setStroke(true);
143+
border.setColor(0xFFCCCCCC);
144+
canvas->drawRect(SkRect::MakeXYWH(x, y, kLayoutWidth, kRowHeight - kRowBorderInset),
145+
border);
146+
}
147+
};
148+
149+
DEF_GM(return new EllipsisBidiGM();)

‎modules/skparagraph/src/Run.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ class Run {
9494
SkScalar correctLeading() const { return fCorrectLeading; }
9595
const SkFont& font() const { return fFont; }
9696
bool leftToRight() const { return fBidiLevel % 2 == 0; }
97+
uint8_t bidiLevel() const { return fBidiLevel; }
9798
TextDirection getTextDirection() const { return leftToRight() ? TextDirection::kLtr : TextDirection::kRtl; }
9899
size_t index() const { return fIndex; }
99100
SkScalar heightMultiplier() const { return fHeightMultiplier; }

‎modules/skparagraph/src/TextLine.cpp‎

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -767,12 +767,18 @@ std::unique_ptr<Run> TextLine::shapeEllipsis(const SkString& ellipsis,
767767
std::unique_ptr<SkShaper> shaper = SkShapers::HB::ShapeDontWrapOrReorder(
768768
fOwner->getUnicode(), fallback ? fallback : SkFontMgr::RefEmpty());
769769

770-
const SkBidiIterator::Level defaultLevel = SkBidiIterator::kLTR;
770+
// Shape the affix with the surrounding run's bidi level, so neutral and
771+
// weak characters in a custom affix (like a "!" after Arabic letters)
772+
// take the surrounding direction instead of LTR. Resolved as LTR, they
773+
// can split the affix into two bidi runs, which ShapeHandler doesn't
774+
// support. The default "-" (ES-class) and "…" (ON-class) produce the
775+
// same glyphs regardless of the level.
776+
const SkBidiIterator::Level bidiLevel = run.bidiLevel();
771777
const char* utf8 = ellipsis.c_str();
772778
size_t utf8Bytes = ellipsis.size();
773779

774780
std::unique_ptr<SkShaper::BiDiRunIterator> bidi = SkShapers::unicode::BidiRunIterator(
775-
fOwner->getUnicode(), utf8, utf8Bytes, defaultLevel);
781+
fOwner->getUnicode(), utf8, utf8Bytes, bidiLevel);
776782
SkASSERT(bidi);
777783

778784
std::unique_ptr<SkShaper::LanguageRunIterator> language =

‎modules/skparagraph/tests/SkParagraphTest.cpp‎

Lines changed: 151 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9086,6 +9086,157 @@ UNIX_ONLY_TEST(SkParagraph_SoftHyphenRTL, reporter) {
90869086
REPORTER_ASSERT(reporter, firstLine.width() > firstLine.widthWithoutEllipsis());
90879087
}
90889088

9089+
// Soft hyphen affix must be shaped at the bidi level of the run it follows, not
9090+
// a hardcoded LTR level. In an RTL paragraph the last cluster before the break
9091+
// is RTL, so the shaped hyphen run must report leftToRight() == false.
9092+
UNIX_ONLY_TEST(SkParagraph_SoftHyphenRTLBidiLevel, reporter) {
9093+
sk_sp<ResourceFontCollection> fontCollection = sk_make_sp<ResourceFontCollection>(true);
9094+
SKIP_IF_FONTS_NOT_FOUND(reporter, fontCollection)
9095+
9096+
// Arabic "كلمة\u00ADطويلة" — same text as SkParagraph_SoftHyphenRTL.
9097+
const char* text =
9098+
"\xD9\x83\xD9\x84\xD9\x85\xD8\xA9"
9099+
"\xC2\xAD"
9100+
"\xD8\xB7\xD9\x88\xD9\x8A\xD9\x84\xD8\xA9";
9101+
9102+
ParagraphStyle paragraph_style;
9103+
paragraph_style.turnHintingOff();
9104+
paragraph_style.setRenderSoftHyphens(true);
9105+
paragraph_style.setTextDirection(TextDirection::kRtl);
9106+
ParagraphBuilderImpl builder(paragraph_style, fontCollection, get_unicode());
9107+
9108+
TextStyle text_style;
9109+
text_style.setFontFamilies({SkString("Noto Naskh Arabic")});
9110+
text_style.setFontSize(20);
9111+
text_style.setColor(SK_ColorBLACK);
9112+
builder.pushStyle(text_style);
9113+
builder.addText(text, strlen(text));
9114+
builder.pop();
9115+
9116+
auto paragraph = builder.Build();
9117+
paragraph->layout(40);
9118+
9119+
auto impl = static_cast<ParagraphImpl*>(paragraph.get());
9120+
REPORTER_ASSERT(reporter, !impl->lines().empty());
9121+
9122+
auto& firstLine = impl->lines()[0];
9123+
REPORTER_ASSERT(reporter, firstLine.hyphen() != nullptr);
9124+
if (firstLine.hyphen() == nullptr) return;
9125+
// The shaped hyphen run inherits the surrounding cluster's bidi level.
9126+
REPORTER_ASSERT(reporter, !firstLine.hyphen()->leftToRight());
9127+
REPORTER_ASSERT(reporter, (firstLine.hyphen()->bidiLevel() % 2) == 1);
9128+
}
9129+
9130+
// Regression guard: the default LTR case must still produce an LTR hyphen run.
9131+
UNIX_ONLY_TEST(SkParagraph_SoftHyphenLTRBidiLevel, reporter) {
9132+
sk_sp<ResourceFontCollection> fontCollection = sk_make_sp<ResourceFontCollection>(true);
9133+
SKIP_IF_FONTS_NOT_FOUND(reporter, fontCollection)
9134+
9135+
const char* text =
9136+
"inter\xC2\xAD"
9137+
"national";
9138+
9139+
ParagraphStyle paragraph_style;
9140+
paragraph_style.turnHintingOff();
9141+
paragraph_style.setRenderSoftHyphens(true);
9142+
ParagraphBuilderImpl builder(paragraph_style, fontCollection, get_unicode());
9143+
9144+
TextStyle text_style;
9145+
text_style.setFontFamilies({SkString("Roboto")});
9146+
text_style.setFontSize(20);
9147+
text_style.setColor(SK_ColorBLACK);
9148+
builder.pushStyle(text_style);
9149+
builder.addText(text, strlen(text));
9150+
builder.pop();
9151+
9152+
auto paragraph = builder.Build();
9153+
paragraph->layout(80);
9154+
9155+
auto impl = static_cast<ParagraphImpl*>(paragraph.get());
9156+
REPORTER_ASSERT(reporter, !impl->lines().empty());
9157+
9158+
auto& firstLine = impl->lines()[0];
9159+
REPORTER_ASSERT(reporter, firstLine.hyphen() != nullptr);
9160+
if (firstLine.hyphen() == nullptr) return;
9161+
REPORTER_ASSERT(reporter, firstLine.hyphen()->leftToRight());
9162+
REPORTER_ASSERT(reporter, (firstLine.hyphen()->bidiLevel() % 2) == 0);
9163+
REPORTER_ASSERT(reporter, firstLine.hyphen()->advance().fX > 0);
9164+
}
9165+
9166+
// Custom ellipsis strings must be shaped in the surrounding bidi context. With
9167+
// a hardcoded LTR level, the trailing "!" of an Arabic custom ellipsis resolves
9168+
// LTR and splits the ellipsis into two bidi runs, which shapeEllipsis doesn't
9169+
// support (it expects a single run).
9170+
UNIX_ONLY_TEST(SkParagraph_EllipsisRTLCustomStringBidiLevel, reporter) {
9171+
sk_sp<ResourceFontCollection> fontCollection = sk_make_sp<ResourceFontCollection>(true);
9172+
SKIP_IF_FONTS_NOT_FOUND(reporter, fontCollection)
9173+
9174+
TextStyle text_style;
9175+
text_style.setFontFamilies({SkString("Noto Naskh Arabic"), SkString("Roboto")});
9176+
text_style.setFontSize(40);
9177+
text_style.setColor(SK_ColorBLACK);
9178+
9179+
ParagraphStyle paragraph_style;
9180+
paragraph_style.setTextStyle(text_style);
9181+
paragraph_style.setTextDirection(TextDirection::kRtl);
9182+
// Arabic word "النهاية" (nihaya = "the end") plus "!", used as a custom ellipsis.
9183+
paragraph_style.setEllipsis(u"\u0627\u0644\u0646\u0647\u0627\u064A\u0629!");
9184+
paragraph_style.setMaxLines(1);
9185+
9186+
ParagraphBuilderImpl builder(paragraph_style, fontCollection, get_unicode());
9187+
builder.pushStyle(text_style);
9188+
builder.addText(u"\u062A\u0638\u0627\u0647\u0631\u0627\u062A \u0648 \u062A\u062C\u0645\u0639\u0627\u062A");
9189+
auto paragraph = builder.Build();
9190+
// Layout width chosen so that (a) the paragraph text (about 270px wide)
9191+
// exceeds it and truncation is forced, and (b) the custom Arabic ellipsis
9192+
// comfortably fits — otherwise shapeEllipsis falls back to "ellipsis did
9193+
// not fit" and line.ellipsis() is nullptr, which would mask the regression
9194+
// being tested.
9195+
paragraph->layout(200);
9196+
9197+
auto impl = static_cast<ParagraphImpl*>(paragraph.get());
9198+
REPORTER_ASSERT(reporter, paragraph->lineNumber() == 1);
9199+
auto& line = impl->lines()[0];
9200+
REPORTER_ASSERT(reporter, line.ellipsis() != nullptr);
9201+
if (line.ellipsis() == nullptr) return;
9202+
// Surrounded by RTL Arabic content; the ellipsis must be shaped as RTL.
9203+
REPORTER_ASSERT(reporter, !line.ellipsis()->leftToRight());
9204+
REPORTER_ASSERT(reporter, (line.ellipsis()->bidiLevel() % 2) == 1);
9205+
REPORTER_ASSERT(reporter, line.ellipsis()->advance().fX > 0);
9206+
}
9207+
9208+
// Regression guard for the common path: default "…" (ON-class) ellipsis in an
9209+
// LTR paragraph must still produce an LTR ellipsis run.
9210+
UNIX_ONLY_TEST(SkParagraph_EllipsisLTRDefaultBidiLevel, reporter) {
9211+
sk_sp<ResourceFontCollection> fontCollection = sk_make_sp<ResourceFontCollection>(true);
9212+
SKIP_IF_FONTS_NOT_FOUND(reporter, fontCollection)
9213+
9214+
TextStyle text_style;
9215+
text_style.setFontFamilies({SkString("Roboto")});
9216+
text_style.setFontSize(20);
9217+
text_style.setColor(SK_ColorBLACK);
9218+
9219+
ParagraphStyle paragraph_style;
9220+
paragraph_style.setTextStyle(text_style);
9221+
paragraph_style.setEllipsis(u"\u2026");
9222+
paragraph_style.setMaxLines(1);
9223+
9224+
ParagraphBuilderImpl builder(paragraph_style, fontCollection, get_unicode());
9225+
builder.pushStyle(text_style);
9226+
builder.addText("The quick brown fox jumps over the lazy dog");
9227+
auto paragraph = builder.Build();
9228+
paragraph->layout(100);
9229+
9230+
auto impl = static_cast<ParagraphImpl*>(paragraph.get());
9231+
REPORTER_ASSERT(reporter, paragraph->lineNumber() == 1);
9232+
auto& line = impl->lines()[0];
9233+
REPORTER_ASSERT(reporter, line.ellipsis() != nullptr);
9234+
if (line.ellipsis() == nullptr) return;
9235+
REPORTER_ASSERT(reporter, line.ellipsis()->leftToRight());
9236+
REPORTER_ASSERT(reporter, (line.ellipsis()->bidiLevel() % 2) == 0);
9237+
REPORTER_ASSERT(reporter, line.ellipsis()->advance().fX > 0);
9238+
}
9239+
90899240
// Verify that with the default (setRenderSoftHyphens(false)), no visible hyphen
90909241
// is rendered even when the line break occurs at a soft hyphen position.
90919242
UNIX_ONLY_TEST(SkParagraph_SoftHyphenDefaultOff, reporter) {

0 commit comments

Comments
 (0)