Skip to content

Commit b6ee622

Browse files
molchanoviaskcq-be@skia-corp.google.com.iam.gserviceaccount.com
authored andcommitted
[skparagraph] Fix bidirectional placeholder layout
Lay out placeholder runs in the visual order produced by the Unicode bidirectional algorithm instead of substituting their source order. Return placeholder boxes in logical insertion order so clients can still associate each box with the corresponding inline object. Translate placeholder glyph positions from their shaping origin to the visual run position when hit testing, avoiding a duplicated offset. Add regression coverage for RTL and mixed-direction placeholders, different widths, reflow, maxLines, range boxes and caret boundaries. Cover Russian, Arabic and mixed-script text using Roboto and Noto Naskh Arabic from the existing skparagraph font asset. No new fonts are needed. Bug: flutter/flutter#54400 Change-Id: I9807f3bced70401e23a2831a93b50d5068a0aab3 Reviewed-on: https://skia-review.googlesource.com/c/skia/+/1366676 Reviewed-by: Julia Lavrova <[email protected]> Commit-Queue: Julia Lavrova <[email protected]> Auto-Submit: Igor Molchanov <[email protected]> Commit-Queue: Florin Malita <[email protected]> Reviewed-by: Florin Malita <[email protected]>
1 parent e64653a commit b6ee622

4 files changed

Lines changed: 253 additions & 35 deletions

File tree

‎AUTHORS‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ Helmut Januschka <[email protected]>
5555
Herb Derby <[email protected]>
5656
Ho Cheung <[email protected]>
5757
Igalia <*@igalia.com>
58+
Igor Molchanov <[email protected]>
5859
Intel <*@intel.com>
5960
Ion Rosca <[email protected]>
6061
Ivan Kubota <[email protected]>

‎modules/skparagraph/include/Paragraph.h‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,8 @@ class Paragraph {
5252
RectHeightStyle rectHeightStyle,
5353
RectWidthStyle rectWidthStyle) = 0;
5454

55+
// Returns boxes for visible placeholders in the order they were added to the builder,
56+
// regardless of their visual order after bidirectional text layout.
5557
virtual std::vector<TextBox> getRectsForPlaceholders() = 0;
5658

5759
// Returns the index of the glyph that corresponds to the provided coordinate,

‎modules/skparagraph/src/TextLine.cpp‎

Lines changed: 35 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -144,19 +144,12 @@ TextLine::TextLine(ParagraphImpl* owner,
144144
// This is just chosen to catch the common/fast cases. Feel free to tweak.
145145
constexpr int kPreallocCount = 4;
146146
AutoSTArray<kPreallocCount, SkUnicode::BidiLevel> runLevels(numRuns);
147-
std::vector<RunIndex> placeholdersInOriginalOrder;
148147
size_t runLevelsIndex = 0;
149-
// Placeholders must be laid out using the original order in which they were added
150-
// in the input. The API does not provide a way to indicate that a placeholder
151-
// position was moved due to bidi reordering.
152148
for (auto runIndex = start.runIndex(); runIndex <= end.runIndex(); ++runIndex) {
153149
auto& run = fOwner->run(runIndex);
154150
runLevels[runLevelsIndex++] = run.fBidiLevel;
155151
fMaxRunMetrics.add(
156152
InternalLineMetrics(run.correctAscent(), run.correctDescent(), run.fFontMetrics.fLeading));
157-
if (run.isPlaceholder()) {
158-
placeholdersInOriginalOrder.push_back(runIndex);
159-
}
160153
}
161154
SkASSERT(runLevelsIndex == numRuns);
162155

@@ -165,14 +158,8 @@ TextLine::TextLine(ParagraphImpl* owner,
165158
// TODO: hide all these logic in SkUnicode?
166159
fOwner->getUnicode()->reorderVisual(runLevels.data(), numRuns, logicalOrder.data());
167160
auto firstRunIndex = start.runIndex();
168-
auto placeholderIter = placeholdersInOriginalOrder.begin();
169161
for (auto index : logicalOrder) {
170-
auto runIndex = firstRunIndex + index;
171-
if (fOwner->run(runIndex).isPlaceholder()) {
172-
fRunsInVisualOrder.push_back(*placeholderIter++);
173-
} else {
174-
fRunsInVisualOrder.push_back(runIndex);
175-
}
162+
fRunsInVisualOrder.push_back(firstRunIndex + index);
176163
}
177164

178165
// TODO: This is the fix for flutter. Must be removed...
@@ -817,7 +804,9 @@ TextLine::ClipContext TextLine::measureTextInsideOneRun(TextRange textRange,
817804
run->calculateHeight(this->fAscentStyle,this->fDescentStyle));
818805
return result;
819806
} else if (run->isPlaceholder()) {
820-
result.fTextShift = runOffsetInLine;
807+
// Placeholder glyph positions retain their origin from shaping. Translate
808+
// that origin to the visual run position, just as for regular text runs.
809+
result.fTextShift = runOffsetInLine - run->positionX(0);
821810
if (SkIsFinite(run->fFontMetrics.fAscent)) {
822811
result.clip = SkRect::MakeXYWH(runOffsetInLine,
823812
sizes().runTop(run, this->fAscentStyle),
@@ -1570,33 +1559,44 @@ PositionWithAffinity TextLine::getGlyphPositionAtCoordinate(SkScalar dx) {
15701559
}
15711560

15721561
void TextLine::getRectsForPlaceholders(std::vector<TextBox>& boxes) {
1562+
std::vector<std::pair<TextIndex, TextBox>> placeholders;
15731563
this->iterateThroughVisualRuns(
1574-
true,
1575-
[&boxes, this](const Run* run, SkScalar runOffset, TextRange textRange,
1576-
SkScalar* width) {
1564+
true,
1565+
[&placeholders, this](
1566+
const Run* run, SkScalar runOffset, TextRange textRange, SkScalar* width) {
15771567
auto context = this->measureTextInsideOneRun(
15781568
textRange, run, runOffset, 0, true, TextAdjustment::GraphemeGluster);
15791569
*width = context.clip.width();
15801570

1581-
if (textRange.width() == 0) {
1582-
return true;
1583-
}
1584-
if (!run->isPlaceholder()) {
1585-
return true;
1586-
}
1571+
if (textRange.width() == 0) {
1572+
return true;
1573+
}
1574+
if (!run->isPlaceholder()) {
1575+
return true;
1576+
}
15871577

1588-
SkRect clip = context.clip;
1589-
clip.offset(this->offset());
1578+
SkRect clip = context.clip;
1579+
clip.offset(this->offset());
15901580

1591-
if (fOwner->getApplyRoundingHack()) {
1592-
clip.fLeft = littleRound(clip.fLeft);
1593-
clip.fRight = littleRound(clip.fRight);
1594-
clip.fTop = littleRound(clip.fTop);
1595-
clip.fBottom = littleRound(clip.fBottom);
1596-
}
1597-
boxes.emplace_back(clip, run->getTextDirection());
1598-
return true;
1599-
});
1581+
if (fOwner->getApplyRoundingHack()) {
1582+
clip.fLeft = littleRound(clip.fLeft);
1583+
clip.fRight = littleRound(clip.fRight);
1584+
clip.fTop = littleRound(clip.fTop);
1585+
clip.fBottom = littleRound(clip.fBottom);
1586+
}
1587+
placeholders.emplace_back(run->textRange().start,
1588+
TextBox(clip, run->getTextDirection()));
1589+
return true;
1590+
});
1591+
1592+
// Measure in visual order, but return boxes in the order placeholders were added.
1593+
// Sorting by their text offsets also handles mixed-direction runs within a line.
1594+
std::sort(placeholders.begin(), placeholders.end(), [](const auto& a, const auto& b) {
1595+
return a.first < b.first;
1596+
});
1597+
for (const auto& placeholder : placeholders) {
1598+
boxes.push_back(placeholder.second);
1599+
}
16001600
}
16011601
} // namespace textlayout
16021602
} // namespace skia

‎modules/skparagraph/tests/SkParagraphTest.cpp‎

Lines changed: 215 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -370,6 +370,221 @@ UNIX_ONLY_TEST(SkParagraph_Rounding_Off_LineBreaks, reporter) {
370370
}
371371
}
372372

373+
DEF_TEST(SkParagraph_PlaceholderLogicalOrder, reporter) {
374+
// No external fonts are required: the paragraph contains only placeholders.
375+
// Regression test for https://github.com/flutter/flutter/issues/54400.
376+
for (auto direction : {TextDirection::kLtr, TextDirection::kRtl}) {
377+
ParagraphStyle style;
378+
style.setTextDirection(direction);
379+
ParagraphBuilderImpl builder(style, sk_make_sp<FontCollection>(), get_unicode());
380+
for (SkScalar width : {30, 50, 70}) {
381+
builder.addPlaceholder(PlaceholderStyle(
382+
width, 20, PlaceholderAlignment::kBottom, TextBaseline::kAlphabetic, 0));
383+
}
384+
auto paragraph = builder.Build();
385+
// Re-layout the same paragraph to exercise both wrapping and cached shaping.
386+
for (SkScalar width : {500, 80, 500}) {
387+
paragraph->layout(width);
388+
auto boxes = paragraph->getRectsForPlaceholders();
389+
REPORTER_ASSERT(reporter, boxes.size() == 3);
390+
if (boxes.size() != 3) {
391+
continue;
392+
}
393+
for (size_t i = 0; i < boxes.size(); ++i) {
394+
REPORTER_ASSERT(reporter, boxes[i].rect.width() == 30 + 20 * i);
395+
REPORTER_ASSERT(reporter, boxes[i].rect.height() == 20);
396+
REPORTER_ASSERT(reporter, boxes[i].direction == direction);
397+
auto range = paragraph->getRectsForRange(
398+
i, i + 1, RectHeightStyle::kTight, RectWidthStyle::kTight);
399+
REPORTER_ASSERT(reporter, range.size() == 1);
400+
if (range.size() == 1) {
401+
REPORTER_ASSERT(reporter, range[0].rect == boxes[i].rect);
402+
}
403+
}
404+
if (direction == TextDirection::kRtl) {
405+
REPORTER_ASSERT(reporter, boxes[0].rect.left() == width - 30);
406+
REPORTER_ASSERT(reporter, boxes[1].rect.left() == width - 80);
407+
REPORTER_ASSERT(reporter, boxes[2].rect.left() == width - (width == 80 ? 70 : 150));
408+
} else {
409+
REPORTER_ASSERT(reporter, boxes[0].rect.left() == 0);
410+
REPORTER_ASSERT(reporter, boxes[1].rect.left() == 30);
411+
REPORTER_ASSERT(reporter, boxes[2].rect.left() == (width == 80 ? 0 : 80));
412+
}
413+
REPORTER_ASSERT(reporter, boxes[0].rect.top() == boxes[1].rect.top());
414+
if (width == 80) {
415+
REPORTER_ASSERT(reporter, boxes[2].rect.top() > boxes[1].rect.top());
416+
} else {
417+
REPORTER_ASSERT(reporter, boxes[2].rect.top() == boxes[1].rect.top());
418+
}
419+
}
420+
}
421+
}
422+
423+
DEF_TEST(SkParagraph_PlaceholderLogicalOrderMixedDirections, reporter) {
424+
for (auto direction : {TextDirection::kLtr, TextDirection::kRtl}) {
425+
auto fonts = sk_make_sp<FontCollection>();
426+
fonts->setDefaultFontManager(ToolUtils::TestFontMgr());
427+
ParagraphStyle style;
428+
style.setTextDirection(direction);
429+
ParagraphBuilderImpl builder(style, fonts, get_unicode());
430+
auto add = [&builder](SkScalar width) {
431+
builder.addPlaceholder(PlaceholderStyle(
432+
width, 20, PlaceholderAlignment::kBottom, TextBaseline::kAlphabetic, 0));
433+
};
434+
add(30);
435+
// Put two placeholders in an embedding opposite to the paragraph direction.
436+
builder.addText(direction == TextDirection::kRtl ? u"\u202a" : u"\u202b");
437+
add(50);
438+
add(70);
439+
builder.addText(u"\u202c");
440+
add(90);
441+
auto paragraph = builder.Build();
442+
paragraph->layout(500);
443+
auto boxes = paragraph->getRectsForPlaceholders();
444+
REPORTER_ASSERT(reporter, boxes.size() == 4);
445+
if (boxes.size() != 4) {
446+
continue;
447+
}
448+
const SkScalar ltrLeft[] = {0, 100, 30, 150};
449+
const SkScalar rtlLeft[] = {470, 350, 400, 260};
450+
const unsigned offsets[] = {0, 2, 3, 5};
451+
for (size_t i = 0; i < boxes.size(); ++i) {
452+
REPORTER_ASSERT(reporter,
453+
boxes[i].rect.left() ==
454+
(direction == TextDirection::kRtl ? rtlLeft[i] : ltrLeft[i]));
455+
REPORTER_ASSERT(reporter, boxes[i].rect.width() == 30 + 20 * i);
456+
auto range = paragraph->getRectsForRange(
457+
offsets[i], offsets[i] + 1, RectHeightStyle::kTight, RectWidthStyle::kTight);
458+
REPORTER_ASSERT(reporter, range.size() == 1);
459+
if (range.size() == 1) {
460+
REPORTER_ASSERT(reporter, range[0].rect == boxes[i].rect);
461+
REPORTER_ASSERT(reporter, range[0].direction == boxes[i].direction);
462+
}
463+
const auto& box = boxes[i];
464+
SkScalar startX = box.direction == TextDirection::kRtl
465+
? box.rect.right() - box.rect.width() / 4
466+
: box.rect.left() + box.rect.width() / 4;
467+
auto position = paragraph->getGlyphPositionAtCoordinate(startX, box.rect.centerY());
468+
REPORTER_ASSERT(reporter,
469+
position.position == SkToS32(offsets[i]),
470+
"base=%d placeholder=%zu expected=%u actual=%d x=%g direction=%d",
471+
static_cast<int>(direction),
472+
i,
473+
offsets[i],
474+
position.position,
475+
startX,
476+
static_cast<int>(box.direction));
477+
SkScalar endX = box.rect.left() + box.rect.right() - startX;
478+
auto endPosition = paragraph->getGlyphPositionAtCoordinate(endX, box.rect.centerY());
479+
REPORTER_ASSERT(reporter, endPosition.position == SkToS32(offsets[i] + 1));
480+
}
481+
}
482+
}
483+
484+
DEF_TEST(SkParagraph_PlaceholderLogicalOrderMaxLines, reporter) {
485+
ParagraphStyle style;
486+
style.setTextDirection(TextDirection::kRtl);
487+
style.setMaxLines(1);
488+
ParagraphBuilderImpl builder(style, sk_make_sp<FontCollection>(), get_unicode());
489+
for (SkScalar width : {30, 50, 70}) {
490+
builder.addPlaceholder(PlaceholderStyle(
491+
width, 20, PlaceholderAlignment::kBottom, TextBaseline::kAlphabetic, 0));
492+
}
493+
auto paragraph = builder.Build();
494+
paragraph->layout(80);
495+
auto boxes = paragraph->getRectsForPlaceholders();
496+
REPORTER_ASSERT(reporter, boxes.size() == 2);
497+
if (boxes.size() == 2) {
498+
REPORTER_ASSERT(reporter, boxes[0].rect.left() == 50);
499+
REPORTER_ASSERT(reporter, boxes[0].rect.width() == 30);
500+
REPORTER_ASSERT(reporter, boxes[1].rect.left() == 0);
501+
REPORTER_ASSERT(reporter, boxes[1].rect.width() == 50);
502+
}
503+
}
504+
505+
DEF_TEST(SkParagraph_PlaceholderLogicalOrderLanguages, reporter) {
506+
auto fonts = sk_make_sp<ResourceFontCollection>();
507+
SKIP_IF_FONTS_NOT_FOUND(reporter, fonts)
508+
struct Sample {
509+
std::u16string word;
510+
TextDirection direction;
511+
};
512+
const Sample russian{u"мир", TextDirection::kLtr};
513+
const Sample arabic{u"نص", TextDirection::kRtl};
514+
const std::vector<std::vector<Sample>> samples = {{russian}, {arabic}, {russian, arabic}};
515+
for (const auto& sample : samples) {
516+
for (auto direction : {TextDirection::kLtr, TextDirection::kRtl}) {
517+
ParagraphStyle style;
518+
style.setTextDirection(direction);
519+
TextStyle textStyle;
520+
textStyle.setFontSize(10);
521+
textStyle.setFontStyle(
522+
SkFontStyle(500, SkFontStyle::kNormal_Width, SkFontStyle::kUpright_Slant));
523+
// Reuse fonts already present in the skparagraph asset.
524+
textStyle.setFontFamilies({SkString("Roboto"), SkString("Noto Naskh Arabic")});
525+
style.setTextStyle(textStyle);
526+
ParagraphBuilderImpl builder(style, fonts, get_unicode());
527+
std::vector<unsigned> offsets;
528+
unsigned offset = 0;
529+
for (const auto& script : sample) {
530+
for (int i = 0; i < 3; ++i) {
531+
auto text = script.word + u" ";
532+
builder.addText(text);
533+
offset += text.size();
534+
if (i < 2) {
535+
offsets.push_back(offset++);
536+
builder.addPlaceholder(PlaceholderStyle(10 + 10 * offsets.size(),
537+
20,
538+
PlaceholderAlignment::kBottom,
539+
TextBaseline::kAlphabetic,
540+
0));
541+
}
542+
}
543+
}
544+
auto paragraph = builder.Build();
545+
for (SkScalar width : {800, 80, 800}) {
546+
paragraph->layout(width);
547+
// The pinned font asset must cover all scripts, with no system fallback.
548+
REPORTER_ASSERT(reporter, paragraph->unresolvedGlyphs() == 0);
549+
auto boxes = paragraph->getRectsForPlaceholders();
550+
REPORTER_ASSERT(reporter, boxes.size() == offsets.size());
551+
if (boxes.size() != offsets.size()) {
552+
continue;
553+
}
554+
for (size_t i = 0; i < boxes.size(); ++i) {
555+
REPORTER_ASSERT(
556+
reporter,
557+
SkScalarNearlyEqual(boxes[i].rect.width(), 20 + 10 * i, EPSILON1000));
558+
REPORTER_ASSERT(reporter, boxes[i].direction == sample[i / 2].direction);
559+
auto range = paragraph->getRectsForRange(offsets[i],
560+
offsets[i] + 1,
561+
RectHeightStyle::kTight,
562+
RectWidthStyle::kTight);
563+
REPORTER_ASSERT(reporter, range.size() == 1);
564+
if (range.size() == 1) {
565+
REPORTER_ASSERT(reporter, range[0].rect == boxes[i].rect);
566+
}
567+
const auto& box = boxes[i];
568+
SkScalar startX = box.direction == TextDirection::kRtl
569+
? box.rect.right() - box.rect.width() / 4
570+
: box.rect.left() + box.rect.width() / 4;
571+
auto start =
572+
paragraph->getGlyphPositionAtCoordinate(startX, box.rect.centerY());
573+
auto end = paragraph->getGlyphPositionAtCoordinate(
574+
box.rect.left() + box.rect.right() - startX, box.rect.centerY());
575+
REPORTER_ASSERT(reporter, start.position == SkToS32(offsets[i]));
576+
REPORTER_ASSERT(reporter, end.position == SkToS32(offsets[i] + 1));
577+
if (width == 800 && i % 2 == 0) {
578+
REPORTER_ASSERT(reporter,
579+
(boxes[i].rect.left() < boxes[i + 1].rect.left()) ==
580+
(sample[i / 2].direction == TextDirection::kLtr));
581+
}
582+
}
583+
}
584+
}
585+
}
586+
}
587+
373588
UNIX_ONLY_TEST(SkParagraph_InlinePlaceholderParagraph, reporter) {
374589
sk_sp<ResourceFontCollection> fontCollection = sk_make_sp<ResourceFontCollection>();
375590
TestCanvas canvas("SkParagraph_InlinePlaceholderParagraph.png");

0 commit comments

Comments
 (0)