Skip to content

Commit e4d7b83

Browse files
dbebawyskcq-be@skia-corp.google.com.iam.gserviceaccount.com
authored andcommitted
[skparagraph] Reserve room for the hyphen at soft hyphen line breaks
When a line breaks at a soft hyphen, the rendered hyphen is added after the break is chosen (TextLine::createSoftHyphen), so its width is never reserved and the line can overflow the paragraph width by the hyphen's advance. TextWrapper::lookAhead now checks whether a soft hyphen break still fits once the hyphen string is added, measured in the soft hyphen's font. If the line has to end at a soft hyphen break that doesn't fit, it ends at the last break opportunity on the line that does instead: a space or an earlier soft hyphen. As in browsers, the hyphen still overflows when the soft hyphen is the line's first break opportunity. Lines that may get an ellipsis reserve no room, since they draw no hyphen. This matches Chrome with hyphens: manual. For example, "xy abcd\u00ADefgh" at width("xy abcd") plus half a hyphen now breaks as "xy" / "abcd-" / "efgh" instead of "xy abcd-" / "efgh". [email protected], [email protected] Bug: flutter/flutter#194582 Change-Id: Id04da01f33a15db0ee2181373d7656bcabded39f Reviewed-on: https://skia-review.googlesource.com/c/skia/+/1397276 Reviewed-by: Julia Lavrova <[email protected]> Reviewed-by: Alexis Cruz-Ayala <[email protected]> Commit-Queue: Julia Lavrova <[email protected]>
1 parent 123ec19 commit e4d7b83

3 files changed

Lines changed: 265 additions & 4 deletions

File tree

‎modules/skparagraph/src/TextWrapper.cpp‎

Lines changed: 45 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
#include "modules/skparagraph/src/ParagraphImpl.h"
33
#include "modules/skparagraph/src/TextWrapper.h"
44

5+
#include "include/core/SkFont.h"
6+
57
namespace skia {
68
namespace textlayout {
79

@@ -49,7 +51,10 @@ struct LineBreakerWithLittleRounding {
4951

5052
// Since we allow cluster clipping when they don't fit
5153
// we have to work with stretches - parts of clusters
52-
void TextWrapper::lookAhead(SkScalar maxWidth, Cluster* endOfClusters, bool applyRoundingHack) {
54+
void TextWrapper::lookAhead(SkScalar maxWidth,
55+
Cluster* endOfClusters,
56+
bool applyRoundingHack,
57+
const SkString* softHyphen) {
5358

5459
reset();
5560
fEndLine.metrics().clean();
@@ -59,6 +64,13 @@ void TextWrapper::lookAhead(SkScalar maxWidth, Cluster* endOfClusters, bool appl
5964

6065
LineBreakerWithLittleRounding breaker(maxWidth, applyRoundingHack);
6166
Cluster* nextNonBreakingSpace = nullptr;
67+
// A line that breaks at a soft hyphen also draws the hyphen, so that break only fits if the
68+
// hyphen does too. When the last break opportunity is a soft hyphen that doesn't fit with its
69+
// hyphen, remember the words before it: if the line has to end there, end it before instead.
70+
// (Like browsers, keep the soft hyphen break if there's no earlier opportunity on the line.)
71+
bool softHyphenBreakTooWide = false;
72+
TextStretch wordsBeforeSoftHyphen;
73+
bool brokeForWidth = false;
6274
for (auto cluster = fEndLine.endCluster(); cluster < endOfClusters; ++cluster) {
6375
if (cluster->isHardBreak()) {
6476
} else if (
@@ -70,12 +82,14 @@ void TextWrapper::lookAhead(SkScalar maxWidth, Cluster* endOfClusters, bool appl
7082
fClusters.extend(cluster);
7183
fMinIntrinsicWidth = std::max(fMinIntrinsicWidth, this->getClustersTrimmedWidth());
7284
fWords.extend(fClusters);
85+
softHyphenBreakTooWide = false;
7386
continue;
7487
} else if (cluster->run().isPlaceholder()) {
7588
if (!fClusters.empty()) {
7689
// Placeholder ends the previous word
7790
fMinIntrinsicWidth = std::max(fMinIntrinsicWidth, this->getClustersTrimmedWidth());
7891
fWords.extend(fClusters);
92+
softHyphenBreakTooWide = false;
7993
}
8094

8195
if (cluster->width() > maxWidth && fWords.empty()) {
@@ -87,6 +101,7 @@ void TextWrapper::lookAhead(SkScalar maxWidth, Cluster* endOfClusters, bool appl
87101
} else {
88102
// Placeholder does not fit the line; it will be considered again on the next line
89103
}
104+
brokeForWidth = true;
90105
break;
91106
}
92107

@@ -125,11 +140,13 @@ void TextWrapper::lookAhead(SkScalar maxWidth, Cluster* endOfClusters, bool appl
125140
fClusters = TextStretch(fClusters.startCluster(), nextNonBreakingSpace, fClusters.metrics().getForceStrut());
126141
fMinIntrinsicWidth = std::max(fMinIntrinsicWidth, nextShortWordLength);
127142
fWords.extend(fClusters);
143+
softHyphenBreakTooWide = false;
128144
} else {
129145
// We can place the short word on the next line
130146
fClusters.clean();
131147
}
132148
// Either way we are not in "word is too long" situation anymore
149+
brokeForWidth = true;
133150
break;
134151
}
135152
// If the word is too long we can break it right now and hope it's enough
@@ -148,6 +165,7 @@ void TextWrapper::lookAhead(SkScalar maxWidth, Cluster* endOfClusters, bool appl
148165
fTooLongCluster = true;
149166
fTooLongWord = true;
150167
}
168+
brokeForWidth = true;
151169
break;
152170
}
153171

@@ -161,12 +179,24 @@ void TextWrapper::lookAhead(SkScalar maxWidth, Cluster* endOfClusters, bool appl
161179
// Placeholder is separate word and its width now is counted in minIntrinsicWidth
162180
fMinIntrinsicWidth = std::max(fMinIntrinsicWidth, cluster->width());
163181
fWords.extend(cluster);
182+
softHyphenBreakTooWide = false;
164183
} else {
165184
fClusters.extend(cluster);
166185

167186
// Keep adding clusters/words
168187
if (fClusters.endOfWord()) {
169188
fMinIntrinsicWidth = std::max(fMinIntrinsicWidth, getClustersTrimmedWidth());
189+
bool tooWide = false;
190+
if (softHyphen != nullptr && cluster->isSoftHyphen() && !fWords.empty()) {
191+
SkScalar hyphenWidth = cluster->run().font().measureText(
192+
softHyphen->c_str(), softHyphen->size(), SkTextEncoding::kUTF8);
193+
tooWide = breaker.breakLine(fWords.width() + fClusters.width() + hyphenWidth);
194+
}
195+
if (tooWide && !softHyphenBreakTooWide) {
196+
// Keep the last break that fits, even past several soft hyphens that don't.
197+
wordsBeforeSoftHyphen = fWords;
198+
}
199+
softHyphenBreakTooWide = tooWide;
170200
fWords.extend(fClusters);
171201
}
172202
}
@@ -176,6 +206,11 @@ void TextWrapper::lookAhead(SkScalar maxWidth, Cluster* endOfClusters, bool appl
176206
break;
177207
}
178208
}
209+
210+
if (brokeForWidth && softHyphenBreakTooWide && !fTooLongWord) {
211+
fWords = wordsBeforeSoftHyphen;
212+
fClusters.clean();
213+
}
179214
}
180215

181216
void TextWrapper::moveForward(bool hasEllipsis) {
@@ -290,6 +325,11 @@ void TextWrapper::breakTextIntoLines(ParagraphImpl* parent,
290325
auto unlimitedLines = maxLines == std::numeric_limits<size_t>::max();
291326
auto endlessLine = !SkIsFinite(maxWidth);
292327
auto hasEllipsis = parent->paragraphStyle().ellipsized();
328+
const SkString* softHyphen = nullptr;
329+
if (parent->paragraphStyle().getRenderSoftHyphens() &&
330+
!parent->paragraphStyle().getSoftHyphenString().isEmpty()) {
331+
softHyphen = &parent->paragraphStyle().getSoftHyphenString();
332+
}
293333

294334
auto disableFirstAscent = parent->paragraphStyle().getTextHeightBehavior() & TextHeightBehavior::kDisableFirstAscent;
295335
auto disableLastDescent = parent->paragraphStyle().getTextHeightBehavior() & TextHeightBehavior::kDisableLastDescent;
@@ -303,11 +343,13 @@ void TextWrapper::breakTextIntoLines(ParagraphImpl* parent,
303343
bool needEllipsis = false;
304344
while (fEndLine.endCluster() != end) {
305345

306-
this->lookAhead(maxWidth, end, parent->getApplyRoundingHack());
307-
308346
auto lastLine = (hasEllipsis && unlimitedLines) || fLineNumber >= maxLines;
309347
needEllipsis = hasEllipsis && !endlessLine && lastLine;
310348

349+
// A line that gets an ellipsis draws no hyphen, so it needs no room for one.
350+
this->lookAhead(
351+
maxWidth, end, parent->getApplyRoundingHack(), needEllipsis ? nullptr : softHyphen);
352+
311353
this->moveForward(needEllipsis);
312354
needEllipsis &= fEndLine.endCluster() < end - 1; // Only if we have some text to ellipsize
313355

‎modules/skparagraph/src/TextWrapper.h‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,11 @@ class TextWrapper {
221221
fHardLineBreak = false;
222222
}
223223

224-
void lookAhead(SkScalar maxWidth, Cluster* endOfClusters, bool applyRoundingHack);
224+
// softHyphen is the string drawn at soft hyphen line breaks, or nullptr if none is drawn.
225+
void lookAhead(SkScalar maxWidth,
226+
Cluster* endOfClusters,
227+
bool applyRoundingHack,
228+
const SkString* softHyphen);
225229
void moveForward(bool hasEllipsis);
226230
void trimEndSpaces(TextAlign align);
227231
std::tuple<Cluster*, size_t, SkScalar> trimStartSpaces(Cluster* endOfClusters);

‎modules/skparagraph/tests/SkParagraphTest.cpp‎

Lines changed: 215 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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)
1043610651
UNIX_ONLY_TEST(SkParagraph_ICU_EmojiRuns, reporter) {
1043710652
SkUnicode_Emoji(SkUnicodes::ICU::Make(), reporter);

0 commit comments

Comments
 (0)