Skip to content

Commit e036615

Browse files
dbebawyskcq-be@skia-corp.google.com.iam.gserviceaccount.com
authored andcommitted
Reland "[skparagraph] Use surrounding run's bidi level in shapeEllipsis"
This is a reland of commit fcf3a18 It was reverted for a SkASSERT(!fRun) failure in ShapeHandler (TextLine.cpp:720) on Test-Mac14-Clang-MacMini9.1-CPU-AppleM1-arm64-Debug-All-NativeFonts. That came from the new GM: its "النهاية!" ellipsis needs two fonts on macOS (Geeza Pro has no "!"), so font fallback split it into two runs, and ShapeHandler supports only one. The GM now uses a pure Arabic custom ellipsis. The neutral-character cases are covered by unit tests, which use fixed test fonts. While looking into it I found a second problem with the original change: shaping every affix with the surrounding run's bidi level split an LTR custom ellipsis ending in a neutral character, like "Read more…", into two bidi runs in RTL text. Now the surrounding level is used only when the affix stays a single bidi run at that level; otherwise the affix is shaped in the opposite direction. That keeps "Read more…" in RTL text a single LTR run, as before the original change, and also keeps "النهاية!" a single RTL run in LTR text, which split before either change. New tests: SkParagraph_EllipsisRTLLtrCustomStringSingleRun and SkParagraph_EllipsisLTRRtlCustomStringSingleRun. Original change's description: > [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]> [email protected], [email protected] Bug: flutter/flutter#189617 Change-Id: Iaff6fc64fd0b6f4482a4b09cf58cf271e809dbc6 Cq-Include-Trybots: skia/skia.primary:Test-Mac14-Clang-MacMini9.1-CPU-AppleM1-arm64-Debug-All-NativeFonts;skia/skia.primary:Test-Ubuntu24.04-Clang-GCE-CPU-AVX2-x86_64-Debug-All-NativeFonts Reviewed-on: https://skia-review.googlesource.com/c/skia/+/1394536 Reviewed-by: Alexis Cruz-Ayala <[email protected]> Reviewed-by: Julia Lavrova <[email protected]> Commit-Queue: Julia Lavrova <[email protected]>
1 parent d131b77 commit e036615

5 files changed

Lines changed: 394 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: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
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: LTR and RTL
25+
// paragraphs with the default "…" and with custom ellipses. The custom ellipses
26+
// avoid neutral characters, which can need a second font (macOS's Arabic font
27+
// has no "!") and ShapeHandler supports only one run. The neutral-character
28+
// cases are covered by unit tests that use fixed test fonts.
29+
30+
namespace {
31+
32+
constexpr SkScalar kRowHeight = 80;
33+
// Narrower than every row's text, so each row truncates and shows its ellipsis.
34+
constexpr SkScalar kLayoutWidth = 180;
35+
constexpr SkScalar kCanvasWidth = 640;
36+
constexpr int kRowCount = 4;
37+
// Visual gap between row baselines and the debug border so descenders aren't
38+
// clipped by the border rectangle drawn around each paragraph's layout box.
39+
constexpr SkScalar kRowBorderInset = 10;
40+
41+
} // namespace
42+
43+
class EllipsisBidiGM : public skiagm::GM {
44+
public:
45+
EllipsisBidiGM() = default;
46+
47+
protected:
48+
SkString getName() const override { return SkString("paragraph_ellipsis_bidi"); }
49+
50+
SkISize getISize() override {
51+
return SkISize::Make(static_cast<int>(kCanvasWidth),
52+
static_cast<int>(kRowHeight * kRowCount + 20));
53+
}
54+
55+
DrawResult onDraw(SkCanvas* canvas, SkString* errorMsg) override {
56+
sk_sp<SkFontMgr> fontmgr = ToolUtils::TestFontMgr();
57+
if (fontmgr->countFamilies() == 0) {
58+
*errorMsg = "No fonts available.";
59+
return DrawResult::kSkip;
60+
}
61+
auto collection = sk_make_sp<skia::textlayout::FontCollection>();
62+
collection->setDefaultFontManager(fontmgr);
63+
collection->enableFontFallback();
64+
65+
auto unicode = SkShapers::BestAvailableUnicode();
66+
if (!unicode) {
67+
*errorMsg = "No SkUnicode implementation available.";
68+
return DrawResult::kSkip;
69+
}
70+
71+
canvas->clear(SK_ColorWHITE);
72+
73+
SkScalar y = 10;
74+
75+
// Row 1: LTR paragraph, default "…" ellipsis (ON-class, regression guard).
76+
drawRow(canvas, 10, y, collection, unicode,
77+
skia::textlayout::TextDirection::kLtr,
78+
u"The quick brown fox jumps over the lazy dog",
79+
u"\u2026");
80+
y += kRowHeight;
81+
82+
// Row 2: LTR paragraph, custom ellipsis containing strong-LTR letters.
83+
drawRow(canvas, 10, y, collection, unicode,
84+
skia::textlayout::TextDirection::kLtr,
85+
u"The quick brown fox jumps over the lazy dog",
86+
u"[more]");
87+
y += kRowHeight;
88+
89+
// Row 3: RTL paragraph, default "…" ellipsis.
90+
drawRow(canvas, 10, y, collection, unicode,
91+
skia::textlayout::TextDirection::kRtl,
92+
u"\u062A\u0638\u0627\u0647\u0631\u0627\u062A \u0648 "
93+
u"\u062A\u062C\u0645\u0639\u0627\u062A",
94+
u"\u2026");
95+
y += kRowHeight;
96+
97+
// Row 4: RTL paragraph, custom Arabic ellipsis "النهاية".
98+
drawRow(canvas, 10, y, collection, unicode,
99+
skia::textlayout::TextDirection::kRtl,
100+
u"\u062A\u0638\u0627\u0647\u0631\u0627\u062A \u0648 "
101+
u"\u062A\u062C\u0645\u0639\u0627\u062A",
102+
u"\u0627\u0644\u0646\u0647\u0627\u064A\u0629");
103+
104+
return DrawResult::kOk;
105+
}
106+
107+
private:
108+
static void drawRow(SkCanvas* canvas,
109+
SkScalar x,
110+
SkScalar y,
111+
const sk_sp<skia::textlayout::FontCollection>& collection,
112+
const sk_sp<SkUnicode>& unicode,
113+
skia::textlayout::TextDirection direction,
114+
const char16_t* text,
115+
const char16_t* ellipsis) {
116+
skia::textlayout::TextStyle textStyle;
117+
textStyle.setFontFamilies({SkString("Noto Naskh Arabic"), SkString("Roboto")});
118+
textStyle.setFontSize(30);
119+
textStyle.setColor(SK_ColorBLACK);
120+
121+
skia::textlayout::ParagraphStyle paragraphStyle;
122+
paragraphStyle.setTextStyle(textStyle);
123+
paragraphStyle.setTextDirection(direction);
124+
paragraphStyle.setEllipsis(std::u16string(ellipsis));
125+
paragraphStyle.setMaxLines(1);
126+
127+
auto builder = skia::textlayout::ParagraphBuilderImpl::make(
128+
paragraphStyle, collection, unicode);
129+
if (!builder) {
130+
return;
131+
}
132+
builder->addText(std::u16string(text));
133+
auto paragraph = builder->Build();
134+
paragraph->layout(kLayoutWidth);
135+
paragraph->paint(canvas, x, y);
136+
137+
SkPaint border;
138+
border.setStroke(true);
139+
border.setColor(0xFFCCCCCC);
140+
canvas->drawRect(SkRect::MakeXYWH(x, y, kLayoutWidth, kRowHeight - kRowBorderInset),
141+
border);
142+
}
143+
};
144+
145+
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: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
#include "modules/skshaper/include/SkShaper.h"
2626
#include "modules/skshaper/include/SkShaper_harfbuzz.h"
2727
#include "modules/skshaper/include/SkShaper_skunicode.h"
28+
#include "modules/skunicode/include/SkUnicode.h"
2829

2930
#include <algorithm>
3031
#include <iterator>
@@ -34,6 +35,7 @@
3435
#include <tuple>
3536
#include <type_traits>
3637
#include <utility>
38+
#include <vector>
3739

3840
using namespace skia_private;
3941

@@ -42,6 +44,13 @@ namespace textlayout {
4244

4345
namespace {
4446

47+
// Whether `text` resolves to a single bidi run when its base direction is `dir`.
48+
bool isSingleBidiRun(SkUnicode* unicode, const SkString& text, SkUnicode::TextDirection dir) {
49+
std::vector<SkUnicode::BidiRegion> regions;
50+
return unicode->getBidiRegions(text.c_str(), SkToInt(text.size()), dir, &regions) &&
51+
regions.size() <= 1;
52+
}
53+
4554
// TODO: deal with all the intersection functionality
4655
TextRange intersected(const TextRange& a, const TextRange& b) {
4756
if (a.start == b.start && a.end == b.end) return a;
@@ -753,6 +762,22 @@ std::unique_ptr<Run> TextLine::shapeEllipsis(const SkString& ellipsis,
753762
}
754763
}
755764

765+
// Shape the affix in the surrounding run's direction, so neutral and weak
766+
// characters in it (like a "!" after Arabic letters, or the default "-" and
767+
// "…") take that direction instead of LTR. ShapeHandler only supports a
768+
// single run, so if the affix would split in that direction (like
769+
// "Read more…" in RTL text), shape it in the opposite direction instead.
770+
SkBidiIterator::Level bidiLevel = run.bidiLevel();
771+
if (sk_sp<SkUnicode> unicode = fOwner->getUnicode()) {
772+
const bool ltr = run.leftToRight();
773+
const auto runDir = ltr ? SkUnicode::TextDirection::kLTR : SkUnicode::TextDirection::kRTL;
774+
const auto otherDir = ltr ? SkUnicode::TextDirection::kRTL : SkUnicode::TextDirection::kLTR;
775+
if (!isSingleBidiRun(unicode.get(), ellipsis, runDir) &&
776+
isSingleBidiRun(unicode.get(), ellipsis, otherDir)) {
777+
bidiLevel = ltr ? SkBidiIterator::kRTL : SkBidiIterator::kLTR;
778+
}
779+
}
780+
756781
auto shaped = [&](sk_sp<SkTypeface> typeface, sk_sp<SkFontMgr> fallback) -> std::unique_ptr<Run> {
757782
ShapeHandler handler(run.heightMultiplier(),
758783
run.useHalfLeading(),
@@ -767,12 +792,11 @@ std::unique_ptr<Run> TextLine::shapeEllipsis(const SkString& ellipsis,
767792
std::unique_ptr<SkShaper> shaper = SkShapers::HB::ShapeDontWrapOrReorder(
768793
fOwner->getUnicode(), fallback ? fallback : SkFontMgr::RefEmpty());
769794

770-
const SkBidiIterator::Level defaultLevel = SkBidiIterator::kLTR;
771795
const char* utf8 = ellipsis.c_str();
772796
size_t utf8Bytes = ellipsis.size();
773797

774798
std::unique_ptr<SkShaper::BiDiRunIterator> bidi = SkShapers::unicode::BidiRunIterator(
775-
fOwner->getUnicode(), utf8, utf8Bytes, defaultLevel);
799+
fOwner->getUnicode(), utf8, utf8Bytes, bidiLevel);
776800
SkASSERT(bidi);
777801

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

0 commit comments

Comments
 (0)