@@ -10432,6 +10432,221 @@ UNIX_ONLY_TEST(SkParagraph_SoftHyphenCustomString, reporter) {
1043210432 REPORTER_ASSERT (reporter, implEmpty->lines ()[0 ].hyphen () == nullptr );
1043310433}
1043410434
10435+ // Returns `text` with each '~' replaced by a soft hyphen (U+00AD).
10436+ static std::string withSoftHyphens (const char * text) {
10437+ std::string result;
10438+ for (const char * c = text; *c != ' \0 ' ; ++c) {
10439+ if (*c == ' ~' ) {
10440+ result += " \xC2\xAD " ;
10441+ } else {
10442+ result += *c;
10443+ }
10444+ }
10445+ return result;
10446+ }
10447+
10448+ // Lays out `text`, with '~' for each soft hyphen, in Roboto 20 with rendered soft hyphens at
10449+ // `width`. The rounding hack is off (as in Flutter) so that line widths can be compared exactly.
10450+ static std::unique_ptr<Paragraph> layoutWithSoftHyphens (
10451+ sk_sp<ResourceFontCollection> fontCollection,
10452+ const char * text,
10453+ SkScalar width,
10454+ const std::function<void (ParagraphStyle&)>& configure = nullptr) {
10455+ ParagraphStyle paragraph_style;
10456+ paragraph_style.turnHintingOff ();
10457+ paragraph_style.setApplyRoundingHack (false );
10458+ paragraph_style.setRenderSoftHyphens (true );
10459+ if (configure) {
10460+ configure (paragraph_style);
10461+ }
10462+ ParagraphBuilderImpl builder (paragraph_style, fontCollection, get_unicode ());
10463+ TextStyle text_style;
10464+ text_style.setFontFamilies ({SkString (" Roboto" )});
10465+ text_style.setFontSize (20 );
10466+ text_style.setColor (SK_ColorBLACK);
10467+ builder.pushStyle (text_style);
10468+ std::string utf8 = withSoftHyphens (text);
10469+ builder.addText (utf8.c_str (), utf8.size ());
10470+ builder.pop ();
10471+ auto paragraph = builder.Build ();
10472+ paragraph->layout (width);
10473+ return paragraph;
10474+ }
10475+
10476+ // The width of `text` on a single line.
10477+ static SkScalar singleLineWidth (sk_sp<ResourceFontCollection> fontCollection, const char * text) {
10478+ return layoutWithSoftHyphens (fontCollection, text, 10000 )->getMaxIntrinsicWidth ();
10479+ }
10480+
10481+ // Each line's text and whether it ends in a rendered hyphen, e.g. "xy|abcd-|efgh".
10482+ static SkString describeLines (Paragraph* paragraph) {
10483+ auto impl = static_cast <ParagraphImpl*>(paragraph);
10484+ SkString result;
10485+ for (auto & line : impl->lines ()) {
10486+ if (!result.isEmpty ()) {
10487+ result.append (" |" );
10488+ }
10489+ auto range = line.trimmedText ();
10490+ for (size_t i = range.start ; i < range.end ; ++i) {
10491+ char c = impl->text ()[i];
10492+ // Leave out the soft hyphen's two UTF-8 bytes.
10493+ if ((c & 0x80 ) == 0 ) {
10494+ result.append (&c, 1 );
10495+ }
10496+ }
10497+ if (line.hyphen () != nullptr ) {
10498+ result.append (" -" );
10499+ }
10500+ }
10501+ return result;
10502+ }
10503+
10504+ // Checks describeLines(paragraph) against `expected`, reporting the actual lines if they differ.
10505+ static void checkLines (skiatest::Reporter* reporter, Paragraph* paragraph, const char * expected) {
10506+ SkString lines = describeLines (paragraph);
10507+ REPORTER_ASSERT (reporter, lines.equals (expected), " lines: %s" , lines.c_str ());
10508+ }
10509+
10510+ // A soft hyphen break also draws a hyphen, so it only fits if the hyphen does too. If it
10511+ // doesn't, the line breaks at the earlier space instead, like browsers do.
10512+ UNIX_ONLY_TEST (SkParagraph_SoftHyphenBreakReservesHyphenWidth, reporter) {
10513+ sk_sp<ResourceFontCollection> fontCollection = sk_make_sp<ResourceFontCollection>(true );
10514+ SKIP_IF_FONTS_NOT_FOUND (reporter, fontCollection)
10515+
10516+ SkScalar hyphen = singleLineWidth (fontCollection, " -" );
10517+ SkScalar width = singleLineWidth (fontCollection, " xy abcd" ) + hyphen / 2 ;
10518+ auto paragraph = layoutWithSoftHyphens (fontCollection, " xy abcd~efgh" , width);
10519+ checkLines (reporter, paragraph.get (), " xy|abcd-|efgh" );
10520+ REPORTER_ASSERT (reporter, paragraph->getLongestLine () <= width);
10521+ }
10522+
10523+ // Likewise, an earlier soft hyphen is used when the later one doesn't fit with its hyphen.
10524+ UNIX_ONLY_TEST (SkParagraph_SoftHyphenBreakFallsBackToEarlierSoftHyphen, reporter) {
10525+ sk_sp<ResourceFontCollection> fontCollection = sk_make_sp<ResourceFontCollection>(true );
10526+ SKIP_IF_FONTS_NOT_FOUND (reporter, fontCollection)
10527+
10528+ SkScalar hyphen = singleLineWidth (fontCollection, " -" );
10529+ SkScalar width = singleLineWidth (fontCollection, " abcd" ) + hyphen / 2 ;
10530+ auto paragraph = layoutWithSoftHyphens (fontCollection, " ab~cd~efgh" , width);
10531+ checkLines (reporter, paragraph.get (), " ab-|cd-|efgh" );
10532+ REPORTER_ASSERT (reporter, paragraph->getLongestLine () <= width);
10533+ }
10534+
10535+ // When several soft hyphen breaks don't fit with their hyphens, the line ends at the last break
10536+ // that does: here, the space.
10537+ UNIX_ONLY_TEST (SkParagraph_SoftHyphenBreakFallsBackPastSoftHyphensThatDontFit, reporter) {
10538+ sk_sp<ResourceFontCollection> fontCollection = sk_make_sp<ResourceFontCollection>(true );
10539+ SKIP_IF_FONTS_NOT_FOUND (reporter, fontCollection)
10540+
10541+ SkScalar hyphen = singleLineWidth (fontCollection, " -" );
10542+ SkScalar width = singleLineWidth (fontCollection, " x ab" ) + hyphen / 2 ;
10543+ auto paragraph = layoutWithSoftHyphens (fontCollection, " x ab~cd~ef" , width);
10544+ SkString lines = describeLines (paragraph.get ());
10545+ REPORTER_ASSERT (reporter, lines.startsWith (" x|ab-|" ), " lines: %s" , lines.c_str ());
10546+ REPORTER_ASSERT (reporter, paragraph->getLongestLine () <= width);
10547+ }
10548+
10549+ // The room reserved is the width of the hyphen string that's drawn, not of "-".
10550+ UNIX_ONLY_TEST (SkParagraph_SoftHyphenBreakReservesCustomHyphenWidth, reporter) {
10551+ sk_sp<ResourceFontCollection> fontCollection = sk_make_sp<ResourceFontCollection>(true );
10552+ SKIP_IF_FONTS_NOT_FOUND (reporter, fontCollection)
10553+
10554+ SkScalar hyphen = singleLineWidth (fontCollection, " -" );
10555+ SkScalar width = singleLineWidth (fontCollection, " xy abcd" ) + hyphen * 1 .5f ;
10556+ const char * text = " xy abcd~efgh" ;
10557+ auto paragraph = layoutWithSoftHyphens (fontCollection, text, width);
10558+ checkLines (reporter, paragraph.get (), " xy abcd-|efgh" );
10559+ paragraph = layoutWithSoftHyphens (fontCollection, text, width, [](ParagraphStyle& style) {
10560+ style.setSoftHyphenString (SkString (" --" ));
10561+ });
10562+ checkLines (reporter, paragraph.get (), " xy|abcd-|efgh" );
10563+ REPORTER_ASSERT (reporter, paragraph->getLongestLine () <= width);
10564+ }
10565+
10566+ // A line that gets an ellipsis draws no hyphen, so it breaks the same whether or not soft
10567+ // hyphens are rendered.
10568+ UNIX_ONLY_TEST (SkParagraph_SoftHyphenBreakOnEllipsisLineReservesNoRoom, reporter) {
10569+ sk_sp<ResourceFontCollection> fontCollection = sk_make_sp<ResourceFontCollection>(true );
10570+ SKIP_IF_FONTS_NOT_FOUND (reporter, fontCollection)
10571+
10572+ SkScalar hyphen = singleLineWidth (fontCollection, " -" );
10573+ SkScalar width = singleLineWidth (fontCollection, " xy abcd" ) + hyphen / 2 ;
10574+ const char * text = " xy abcd~efgh ijkl" ;
10575+ auto lines = [&](bool renderSoftHyphens) {
10576+ auto paragraph = layoutWithSoftHyphens (
10577+ fontCollection, text, width, [renderSoftHyphens](ParagraphStyle& style) {
10578+ style.setRenderSoftHyphens (renderSoftHyphens);
10579+ style.setMaxLines (1 );
10580+ style.setEllipsis (SkString (" ..." ));
10581+ });
10582+ auto impl = static_cast <ParagraphImpl*>(paragraph.get ());
10583+ REPORTER_ASSERT (reporter, impl->lines ().size () == 1 );
10584+ REPORTER_ASSERT (reporter, impl->lines ()[0 ].ellipsis () != nullptr );
10585+ REPORTER_ASSERT (reporter, impl->lines ()[0 ].hyphen () == nullptr );
10586+ return impl->lines ()[0 ].trimmedText ();
10587+ };
10588+ REPORTER_ASSERT (reporter, lines (true ) == lines (false ));
10589+ }
10590+
10591+ // A placeholder that doesn't fit ends the line after the word before it, so an earlier soft
10592+ // hyphen that wouldn't fit with its hyphen doesn't matter.
10593+ UNIX_ONLY_TEST (SkParagraph_SoftHyphenBreakBeforePlaceholderNeedsNoRoom, reporter) {
10594+ sk_sp<ResourceFontCollection> fontCollection = sk_make_sp<ResourceFontCollection>(true );
10595+ SKIP_IF_FONTS_NOT_FOUND (reporter, fontCollection)
10596+
10597+ const char * text = " xy abcd~i" ;
10598+ SkScalar width = singleLineWidth (fontCollection, text) + 0 .3f ;
10599+ SkScalar hyphenated = singleLineWidth (fontCollection, " xy abcd-" );
10600+ REPORTER_ASSERT (reporter, hyphenated > width + 0 .3f , " %g vs %g" , hyphenated, width);
10601+
10602+ ParagraphStyle paragraph_style;
10603+ paragraph_style.turnHintingOff ();
10604+ paragraph_style.setApplyRoundingHack (false );
10605+ paragraph_style.setRenderSoftHyphens (true );
10606+ ParagraphBuilderImpl builder (paragraph_style, fontCollection, get_unicode ());
10607+ TextStyle text_style;
10608+ text_style.setFontFamilies ({SkString (" Roboto" )});
10609+ text_style.setFontSize (20 );
10610+ text_style.setColor (SK_ColorBLACK);
10611+ builder.pushStyle (text_style);
10612+ std::string utf8 = withSoftHyphens (text);
10613+ builder.addText (utf8.c_str (), utf8.size ());
10614+ builder.addPlaceholder (PlaceholderStyle (
10615+ 20 , 20 , PlaceholderAlignment::kBaseline , TextBaseline::kAlphabetic , 0 ));
10616+ builder.pop ();
10617+ auto paragraph = builder.Build ();
10618+ paragraph->layout (width);
10619+ // The second line holds only the placeholder.
10620+ checkLines (reporter, paragraph.get (), " xy abcdi|" );
10621+ }
10622+
10623+ // With no earlier break opportunity on the line, the soft hyphen break is still used and the
10624+ // hyphen overflows, rather than breaking inside the word (also like browsers).
10625+ UNIX_ONLY_TEST (SkParagraph_SoftHyphenOnlyBreakMayOverflow, reporter) {
10626+ sk_sp<ResourceFontCollection> fontCollection = sk_make_sp<ResourceFontCollection>(true );
10627+ SKIP_IF_FONTS_NOT_FOUND (reporter, fontCollection)
10628+
10629+ SkScalar hyphen = singleLineWidth (fontCollection, " -" );
10630+ SkScalar width = singleLineWidth (fontCollection, " abcd" ) + hyphen / 2 ;
10631+ auto paragraph = layoutWithSoftHyphens (fontCollection, " abcd~efgh" , width);
10632+ checkLines (reporter, paragraph.get (), " abcd-|efgh" );
10633+ REPORTER_ASSERT (reporter, paragraph->getLongestLine () > width);
10634+ }
10635+
10636+ // The hyphen only counts if the line ends at the soft hyphen. Here the line would overflow if it
10637+ // ended there, but the text after the soft hyphen is narrower than the hyphen, so the line can
10638+ // continue past it to the next space.
10639+ UNIX_ONLY_TEST (SkParagraph_SoftHyphenBreakNotTakenNeedsNoRoom, reporter) {
10640+ sk_sp<ResourceFontCollection> fontCollection = sk_make_sp<ResourceFontCollection>(true );
10641+ SKIP_IF_FONTS_NOT_FOUND (reporter, fontCollection)
10642+
10643+ SkScalar width = singleLineWidth (fontCollection, " xy abcd~i" ) + 0 .3f ;
10644+ SkScalar hyphenated = singleLineWidth (fontCollection, " xy abcd-" );
10645+ REPORTER_ASSERT (reporter, hyphenated > width + 0 .3f , " %g vs %g" , hyphenated, width);
10646+ auto paragraph = layoutWithSoftHyphens (fontCollection, " xy abcd~i jklm" , width);
10647+ checkLines (reporter, paragraph.get (), " xy abcdi|jklm" );
10648+ }
10649+
1043510650#if defined(SK_UNICODE_ICU_IMPLEMENTATION)
1043610651UNIX_ONLY_TEST (SkParagraph_ICU_EmojiRuns, reporter) {
1043710652 SkUnicode_Emoji (SkUnicodes::ICU::Make (), reporter);
0 commit comments