@@ -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+
373588UNIX_ONLY_TEST (SkParagraph_InlinePlaceholderParagraph, reporter) {
374589 sk_sp<ResourceFontCollection> fontCollection = sk_make_sp<ResourceFontCollection>();
375590 TestCanvas canvas (" SkParagraph_InlinePlaceholderParagraph.png" );
0 commit comments