Skip to content

Commit ce0b85b

Browse files
dbebawyskcq-be@skia-corp.google.com.iam.gserviceaccount.com
authored andcommitted
[skparagraph] Leave room for the soft hyphen when justifying
A justified line that breaks at a soft hyphen spreads its text across the whole paragraph width, and the rendered hyphen is then drawn after the text, so it overflows the paragraph width by the hyphen's advance. In an RTL paragraph, a justified line with no spaces is right aligned instead, and its hyphen overflows the same way. TextLine::justify now spreads the text over the paragraph width minus the hyphen's advance, so the text and the hyphen together fill the line. It also skips soft hyphen clusters when it counts and spreads the justification patches. They aren't rendered in the text run, but they ended the patch after an ideograph and advanced prevShift, so a justified line of ideographs that broke at a soft hyphen left a gap before the hyphen, and an unbroken soft hyphen between two ideographs doubled the gap after it. Leading clusters are now tracked from the start instead of by index 0, so a leading soft hyphen doesn't open a patch either, and neither do leading spaces before an ideograph. For example, "xy abcd­efgh" justified at width("xy abcd-") plus 15 in Roboto 20 drew the hyphen at [88.69, 94.21] on an 88.69 wide line; it now ends at 88.69. [email protected], [email protected] Bug: flutter/flutter#194582 Change-Id: I8e97de2429ac3dd4e4e656dbd897bea1e9535963 Reviewed-on: https://skia-review.googlesource.com/c/skia/+/1406297 Reviewed-by: Julia Lavrova <[email protected]> Auto-Submit: David H Bebawy <[email protected]> Reviewed-by: Alexis Cruz-Ayala <[email protected]> Commit-Queue: Julia Lavrova <[email protected]>
1 parent ed76513 commit ce0b85b

2 files changed

Lines changed: 176 additions & 16 deletions

File tree

‎modules/skparagraph/src/TextLine.cpp‎

Lines changed: 27 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -490,26 +490,32 @@ void TextLine::paintDecorations(ParagraphPainter* painter, SkScalar x, SkScalar
490490
}
491491

492492
void TextLine::justify(SkScalar maxWidth) {
493+
if (fHyphen != nullptr) {
494+
// The rendered soft hyphen follows the text, so the text fills the rest of the line
495+
maxWidth -= fHyphen->advance().fX;
496+
}
493497
int whitespacePatches = 0;
494498
SkScalar textLen = 0;
495499
SkScalar whitespaceLen = 0;
496500
bool whitespacePatch = false;
497-
// Take leading whitespaces width but do not increment a whitespace patch number
498-
bool leadingWhitespaces = false;
501+
// Take leading whitespaces width but do not increment a whitespace patch number; start true so
502+
// any leading soft hyphen before the first visible cluster does not open a leading patch
503+
bool leadingWhitespaces = true;
499504
this->iterateThroughClustersInGlyphsOrder(false, false,
500505
[&](const Cluster* cluster, ClusterIndex index, bool ghost) {
501-
if (cluster->isWhitespaceBreak()) {
502-
if (index == 0) {
503-
leadingWhitespaces = true;
504-
} else if (!whitespacePatch && !leadingWhitespaces) {
506+
if (cluster->isSoftHyphen()) {
507+
// Soft-hyphen clusters are unrendered in the text run and must not split an
508+
// adjacent ideographic or whitespace justification patch
509+
} else if (cluster->isWhitespaceBreak()) {
510+
if (!whitespacePatch && !leadingWhitespaces) {
505511
// We only count patches BETWEEN words, not before
506512
++whitespacePatches;
507513
}
508514
whitespacePatch = !leadingWhitespaces;
509515
whitespaceLen += cluster->width();
510516
} else if (cluster->isIdeographic()) {
511-
// Whitespace break before and after
512-
if (!whitespacePatch && index != 0) {
517+
// Whitespace break before and after, except before the first visible cluster
518+
if (!whitespacePatch && !leadingWhitespaces) {
513519
// We only count patches BETWEEN words, not before
514520
++whitespacePatches; // before
515521
}
@@ -544,8 +550,8 @@ void TextLine::justify(SkScalar maxWidth) {
544550
auto ghostShift = maxWidth - this->fAdvance.fX;
545551
// Spread the extra whitespaces
546552
whitespacePatch = false;
547-
// Do not break on leading whitespaces
548-
leadingWhitespaces = false;
553+
// Do not break on leading whitespaces or leading soft hyphens
554+
leadingWhitespaces = true;
549555
this->iterateThroughClustersInGlyphsOrder(false, true, [&](const Cluster* cluster, ClusterIndex index, bool ghost) {
550556

551557
if (ghost) {
@@ -555,17 +561,18 @@ void TextLine::justify(SkScalar maxWidth) {
555561
return true;
556562
}
557563

558-
if (cluster->isWhitespaceBreak()) {
559-
if (index == 0) {
560-
leadingWhitespaces = true;
561-
} else if (!whitespacePatch && !leadingWhitespaces) {
564+
if (cluster->isSoftHyphen()) {
565+
// Keep whitespacePatch and leadingWhitespaces intact across soft hyphens
566+
} else if (cluster->isWhitespaceBreak()) {
567+
if (!whitespacePatch && !leadingWhitespaces) {
562568
shift += step;
563569
whitespacePatch = true;
564570
--whitespacePatches;
565571
}
566572
shift -= cluster->width();
567573
} else if (cluster->isIdeographic()) {
568-
if (!whitespacePatch && index != 0) {
574+
// Only insert a before-patch once a visible cluster has preceded this ideograph
575+
if (!whitespacePatch && !leadingWhitespaces) {
569576
shift += step;
570577
--whitespacePatches;
571578
}
@@ -576,7 +583,11 @@ void TextLine::justify(SkScalar maxWidth) {
576583
leadingWhitespaces = false;
577584
}
578585
this->shiftCluster(cluster, shift, prevShift);
579-
prevShift = shift;
586+
// shiftCluster still records the run-end shift for a trailing soft hyphen, but an
587+
// unrendered soft hyphen must not advance prevShift for the following visible cluster
588+
if (!cluster->isSoftHyphen()) {
589+
prevShift = shift;
590+
}
580591
// We skip ideographic whitespaces
581592
if (!cluster->isWhitespaceBreak() && cluster->isIdeographic()) {
582593
shift += step;

‎modules/skparagraph/tests/SkParagraphTest.cpp‎

Lines changed: 149 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11084,6 +11084,155 @@ UNIX_ONLY_TEST(SkParagraph_SoftHyphenBreakNotTakenNeedsNoRoom, reporter) {
1108411084
checkLines(reporter, paragraph.get(), "xy abcdi|jklm");
1108511085
}
1108611086

11087+
// A justified line that breaks at a soft hyphen spreads its text so that the text and the drawn
11088+
// hyphen together fill the line, in either paragraph direction.
11089+
UNIX_ONLY_TEST(SkParagraph_SoftHyphenJustifiedLineFitsHyphen, reporter) {
11090+
sk_sp<ResourceFontCollection> fontCollection = sk_make_sp<ResourceFontCollection>(true);
11091+
SKIP_IF_FONTS_NOT_FOUND(reporter, fontCollection)
11092+
11093+
SkScalar width = singleLineWidth(fontCollection, "xy abcd-") + 15;
11094+
for (TextDirection direction : {TextDirection::kLtr, TextDirection::kRtl}) {
11095+
auto paragraph = layoutWithSoftHyphens(
11096+
fontCollection, "xy abcd~efgh", width, [direction](ParagraphStyle& style) {
11097+
style.setTextAlign(TextAlign::kJustify);
11098+
style.setTextDirection(direction);
11099+
});
11100+
checkLines(reporter, paragraph.get(), "xy abcd-|efgh");
11101+
auto& line = static_cast<ParagraphImpl*>(paragraph.get())->lines()[0];
11102+
auto extent = lineRectExtent(paragraph.get(), line, 0, 12);
11103+
REPORTER_ASSERT(
11104+
reporter, SkScalarNearlyEqual(extent.first, 0, EPSILON100), "%g", extent.first);
11105+
REPORTER_ASSERT(reporter,
11106+
SkScalarNearlyEqual(extent.second, width, EPSILON100),
11107+
"%g vs %g",
11108+
extent.second,
11109+
width);
11110+
}
11111+
}
11112+
11113+
// A justified RTL line with no spaces to spread is right aligned, and its hyphen stays in the line.
11114+
UNIX_ONLY_TEST(SkParagraph_SoftHyphenJustifiedRtlLineWithoutSpacesFitsHyphen, reporter) {
11115+
sk_sp<ResourceFontCollection> fontCollection = sk_make_sp<ResourceFontCollection>(true);
11116+
SKIP_IF_FONTS_NOT_FOUND(reporter, fontCollection)
11117+
11118+
SkScalar width = singleLineWidth(fontCollection, "abcd-") + 15;
11119+
auto paragraph =
11120+
layoutWithSoftHyphens(fontCollection, "abcd~efgh", width, [](ParagraphStyle& style) {
11121+
style.setTextAlign(TextAlign::kJustify);
11122+
style.setTextDirection(TextDirection::kRtl);
11123+
});
11124+
checkLines(reporter, paragraph.get(), "abcd-|efgh");
11125+
auto& line = static_cast<ParagraphImpl*>(paragraph.get())->lines()[0];
11126+
auto extent = lineRectExtent(paragraph.get(), line, 0, 9);
11127+
REPORTER_ASSERT(reporter,
11128+
SkScalarNearlyEqual(extent.first, width - line.width(), EPSILON100),
11129+
"%g vs %g",
11130+
extent.first,
11131+
width - line.width());
11132+
REPORTER_ASSERT(reporter,
11133+
SkScalarNearlyEqual(extent.second, width, EPSILON100),
11134+
"%g vs %g",
11135+
extent.second,
11136+
width);
11137+
}
11138+
11139+
// An ideographic line breaking at a soft hyphen counts justification patches only between
11140+
// visible characters, not between an ideograph and an adjacent soft hyphen
11141+
UNIX_ONLY_TEST(SkParagraph_SoftHyphenJustifiedIdeographicFitsHyphen, reporter) {
11142+
sk_sp<ResourceFontCollection> fontCollection = sk_make_sp<ResourceFontCollection>(true);
11143+
SKIP_IF_FONTS_NOT_FOUND(reporter, fontCollection)
11144+
11145+
auto layoutCJK = [&](const char* text, SkScalar width, TextDirection direction) {
11146+
ParagraphStyle paragraph_style;
11147+
paragraph_style.turnHintingOff();
11148+
paragraph_style.setApplyRoundingHack(false);
11149+
paragraph_style.setRenderSoftHyphens(true);
11150+
paragraph_style.setTextAlign(TextAlign::kJustify);
11151+
paragraph_style.setTextDirection(direction);
11152+
ParagraphBuilderImpl builder(paragraph_style, fontCollection, get_unicode());
11153+
TextStyle text_style;
11154+
text_style.setFontFamilies({SkString("Source Han Serif CN"), SkString("Roboto")});
11155+
text_style.setFontSize(20);
11156+
text_style.setColor(SK_ColorBLACK);
11157+
builder.pushStyle(text_style);
11158+
std::string utf8 = withSoftHyphens(text);
11159+
builder.addText(utf8.c_str(), utf8.size());
11160+
builder.pop();
11161+
auto paragraph = builder.Build();
11162+
paragraph->layout(width);
11163+
return paragraph;
11164+
};
11165+
11166+
// "漢字~語文" breaks after "漢字~": only 1 justification patch exists (between 漢 and 字), so
11167+
// the second ideograph 字 must reach the start of the rendered hyphen at (cjkWidth - hyphen)
11168+
SkScalar hyphenWidth = layoutCJK("-", 10000, TextDirection::kLtr)->getMaxIntrinsicWidth();
11169+
SkScalar cjkWidth = layoutCJK("漢字-", 10000, TextDirection::kLtr)->getMaxIntrinsicWidth() + 6;
11170+
auto paragraph = layoutCJK("漢字~語文", cjkWidth, TextDirection::kLtr);
11171+
auto* impl = static_cast<ParagraphImpl*>(paragraph.get());
11172+
REPORTER_ASSERT(reporter, impl->lines().size() == 2);
11173+
REPORTER_ASSERT(reporter, impl->lines()[0].hyphen() != nullptr);
11174+
auto secondChar = lineRectExtent(paragraph.get(), impl->lines()[0], 1, 2);
11175+
REPORTER_ASSERT(reporter,
11176+
SkScalarNearlyEqual(secondChar.second, cjkWidth - hyphenWidth, EPSILON100),
11177+
"%g vs %g",
11178+
secondChar.second,
11179+
cjkWidth - hyphenWidth);
11180+
auto extent = lineRectExtent(paragraph.get(), impl->lines()[0], 0, 3);
11181+
REPORTER_ASSERT(
11182+
reporter, SkScalarNearlyEqual(extent.first, 0, EPSILON100), "%g", extent.first);
11183+
REPORTER_ASSERT(reporter,
11184+
SkScalarNearlyEqual(extent.second, cjkWidth, EPSILON100),
11185+
"%g vs %g",
11186+
extent.second,
11187+
cjkWidth);
11188+
11189+
// A single ideograph before a soft hyphen break ("漢~字") has 0 inter-character patches to
11190+
// stretch, so its width must remain its unstretched advance plus the hyphen
11191+
SkScalar oneCharPlusHyphen =
11192+
layoutCJK("漢-", 10000, TextDirection::kLtr)->getMaxIntrinsicWidth();
11193+
SkScalar oneWidth = oneCharPlusHyphen + 6;
11194+
auto single = layoutCJK("漢~字", oneWidth, TextDirection::kLtr);
11195+
auto* singleImpl = static_cast<ParagraphImpl*>(single.get());
11196+
REPORTER_ASSERT(reporter, singleImpl->lines().size() == 2);
11197+
REPORTER_ASSERT(reporter, singleImpl->lines()[0].hyphen() != nullptr);
11198+
REPORTER_ASSERT(reporter,
11199+
SkScalarNearlyEqual(
11200+
singleImpl->lines()[0].width(), oneCharPlusHyphen, EPSILON100),
11201+
"%g vs %g",
11202+
singleImpl->lines()[0].width(),
11203+
oneCharPlusHyphen);
11204+
11205+
// An unbroken soft hyphen between two ideographs ("漢~字語~文言") must not insert a second
11206+
// justification patch between 漢 and 字: each character's justified box must match the same
11207+
// line without the unbroken soft hyphen ("漢字語~文言")
11208+
SkScalar threeWidth =
11209+
layoutCJK("漢字語-", 10000, TextDirection::kLtr)->getMaxIntrinsicWidth() + 6;
11210+
auto unbroken = layoutCJK("漢~字語~文言", threeWidth, TextDirection::kLtr);
11211+
auto* unbrokenImpl = static_cast<ParagraphImpl*>(unbroken.get());
11212+
auto reference = layoutCJK("漢字語~文言", threeWidth, TextDirection::kLtr);
11213+
auto* referenceImpl = static_cast<ParagraphImpl*>(reference.get());
11214+
REPORTER_ASSERT(reporter, unbrokenImpl->lines().size() == 2);
11215+
REPORTER_ASSERT(reporter, unbrokenImpl->lines()[0].hyphen() != nullptr);
11216+
auto char1 = lineRectExtent(unbroken.get(), unbrokenImpl->lines()[0], 2, 3);
11217+
auto refChar1 = lineRectExtent(reference.get(), referenceImpl->lines()[0], 1, 2);
11218+
auto char2 = lineRectExtent(unbroken.get(), unbrokenImpl->lines()[0], 3, 4);
11219+
REPORTER_ASSERT(reporter,
11220+
SkScalarNearlyEqual(char1.first, refChar1.first, EPSILON100),
11221+
"%g vs %g",
11222+
char1.first,
11223+
refChar1.first);
11224+
REPORTER_ASSERT(reporter,
11225+
SkScalarNearlyEqual(char1.second, refChar1.second, EPSILON100),
11226+
"%g vs %g",
11227+
char1.second,
11228+
refChar1.second);
11229+
REPORTER_ASSERT(reporter,
11230+
SkScalarNearlyEqual(char2.second, threeWidth - hyphenWidth, EPSILON100),
11231+
"%g vs %g",
11232+
char2.second,
11233+
threeWidth - hyphenWidth);
11234+
}
11235+
1108711236
// Laid out at its min intrinsic width, text with soft hyphens doesn't overflow: as in CSS
1108811237
// min-content, the min intrinsic width includes the hyphen drawn at a soft hyphen break.
1108911238
UNIX_ONLY_TEST(SkParagraph_SoftHyphenMinIntrinsicWidthIncludesHyphen, reporter) {

0 commit comments

Comments
 (0)