Skip to content

Commit 84068eb

Browse files
rphilliskcq-be@skia-corp.google.com.iam.gserviceaccount.com
authored andcommitted
Revert "[skparagraph] Fix bidirectional placeholder layout"
This reverts commit b6ee622. Reason for revert: Experimental revert to see if this is the cause of the ASAN failures on the tree Failure Link: <LINK TO FAILURE> Original change's description: > [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]> Bug: flutter/flutter#54400 No-Presubmit: true No-Tree-Checks: true No-Try: true Change-Id: Ibaf5dcf8d77e7a56510c9fe95975b05b4666f459 Reviewed-on: https://skia-review.googlesource.com/c/skia/+/1384836 Bot-Commit: [email protected] <[email protected]> Commit-Queue: Robert Phillips <[email protected]>
1 parent 3b5a959 commit 84068eb

4 files changed

Lines changed: 35 additions & 253 deletions

File tree

‎AUTHORS‎

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

‎modules/skparagraph/include/Paragraph.h‎

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,6 @@ 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.
5755
virtual std::vector<TextBox> getRectsForPlaceholders() = 0;
5856

5957
// 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,12 +144,19 @@ 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;
147148
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.
148152
for (auto runIndex = start.runIndex(); runIndex <= end.runIndex(); ++runIndex) {
149153
auto& run = fOwner->run(runIndex);
150154
runLevels[runLevelsIndex++] = run.fBidiLevel;
151155
fMaxRunMetrics.add(
152156
InternalLineMetrics(run.correctAscent(), run.correctDescent(), run.fFontMetrics.fLeading));
157+
if (run.isPlaceholder()) {
158+
placeholdersInOriginalOrder.push_back(runIndex);
159+
}
153160
}
154161
SkASSERT(runLevelsIndex == numRuns);
155162

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

165178
// TODO: This is the fix for flutter. Must be removed...
@@ -804,9 +817,7 @@ TextLine::ClipContext TextLine::measureTextInsideOneRun(TextRange textRange,
804817
run->calculateHeight(this->fAscentStyle,this->fDescentStyle));
805818
return result;
806819
} else if (run->isPlaceholder()) {
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);
820+
result.fTextShift = runOffsetInLine;
810821
if (SkIsFinite(run->fFontMetrics.fAscent)) {
811822
result.clip = SkRect::MakeXYWH(runOffsetInLine,
812823
sizes().runTop(run, this->fAscentStyle),
@@ -1559,44 +1570,33 @@ PositionWithAffinity TextLine::getGlyphPositionAtCoordinate(SkScalar dx) {
15591570
}
15601571

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

1571-
if (textRange.width() == 0) {
1572-
return true;
1573-
}
1574-
if (!run->isPlaceholder()) {
1575-
return true;
1576-
}
1577-
1578-
SkRect clip = context.clip;
1579-
clip.offset(this->offset());
1580-
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()));
1581+
if (textRange.width() == 0) {
15891582
return true;
1590-
});
1583+
}
1584+
if (!run->isPlaceholder()) {
1585+
return true;
1586+
}
15911587

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-
}
1588+
SkRect clip = context.clip;
1589+
clip.offset(this->offset());
1590+
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+
});
16001600
}
16011601
} // namespace textlayout
16021602
} // namespace skia

‎modules/skparagraph/tests/SkParagraphTest.cpp‎

Lines changed: 0 additions & 215 deletions
Original file line numberDiff line numberDiff line change
@@ -370,221 +370,6 @@ 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-
588373
UNIX_ONLY_TEST(SkParagraph_InlinePlaceholderParagraph, reporter) {
589374
sk_sp<ResourceFontCollection> fontCollection = sk_make_sp<ResourceFontCollection>();
590375
TestCanvas canvas("SkParagraph_InlinePlaceholderParagraph.png");

0 commit comments

Comments
 (0)