Skip to content

Commit d13c86b

Browse files
dbebawyskcq-be@skia-corp.google.com.iam.gserviceaccount.com
authored andcommitted
[skparagraph] Count the hyphen in min intrinsic width at soft hyphens
A word that ends at a soft hyphen is drawn with a hyphen when the line breaks there, but the min intrinsic width left the hyphen out. Laid out at its min intrinsic width, "xy abcd\u00ADef" broke as "xy" / "abcd-" / "ef" and the second line overflowed by the hyphen's width. TextWrapper::lookAhead now adds the hyphen string's width to such a word, both where words end normally and for a word too long for the line, so the result doesn't depend on the layout width. This matches CSS min-content in Chrome, where "abcd\u00ADefgh" is as wide as "abcd-". A soft hyphen that ends the text adds nothing, since no hyphen is drawn there. Lines that may get an ellipsis still reserve no room for the hyphen while breaking, but their words now count it in the min intrinsic width too. The hyphen is measured in the soft hyphen's font when that font has glyphs for the hyphen string, and otherwise shaped with font fallback the same way it is drawn (TextLine::ShapeString, factored out of shapeEllipsis). The too-long-word measurement includes the soft break cluster's own width, which letter spacing makes non-zero, and records the hyphenated word's width even when the word fits on a line. The scan of the text after maxLines now ends words at soft breaks as well as at spaces, so truncated text without spaces, such as CJK, also gets the min intrinsic width of its longest word rather than of the whole remainder. [email protected], [email protected] Bug: flutter/flutter#194582 Change-Id: I2aff91fdd7db647d5c81b9296fe8521449497724 Reviewed-on: https://skia-review.googlesource.com/c/skia/+/1398738 Reviewed-by: Julia Lavrova <[email protected]> Commit-Queue: Julia Lavrova <[email protected]> Reviewed-by: Alexis Cruz-Ayala <[email protected]> Auto-Submit: David H Bebawy <[email protected]>
1 parent 88eb0aa commit d13c86b

5 files changed

Lines changed: 331 additions & 35 deletions

File tree

‎modules/skparagraph/src/TextLine.cpp‎

Lines changed: 27 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -709,6 +709,13 @@ void TextLine::createSoftHyphen() {
709709
}
710710

711711
std::unique_ptr<Run> TextLine::shapeEllipsis(const SkString& ellipsis, const Cluster* cluster) {
712+
return ShapeString(fOwner, fBlockRange, ellipsis, cluster);
713+
}
714+
715+
std::unique_ptr<Run> TextLine::ShapeString(ParagraphImpl* owner,
716+
BlockRange blockRange,
717+
const SkString& ellipsis,
718+
const Cluster* cluster) {
712719

713720
class ShapeHandler final : public SkShaper::RunHandler {
714721
public:
@@ -753,9 +760,9 @@ std::unique_ptr<Run> TextLine::shapeEllipsis(const SkString& ellipsis, const Clu
753760
};
754761

755762
const Run& run = cluster->run();
756-
TextStyle textStyle = fOwner->paragraphStyle().getTextStyle();
757-
for (auto i = fBlockRange.start; i < fBlockRange.end; ++i) {
758-
auto& block = fOwner->block(i);
763+
TextStyle textStyle = owner->paragraphStyle().getTextStyle();
764+
for (auto i = blockRange.start; i < blockRange.end; ++i) {
765+
auto& block = owner->block(i);
759766
if (run.leftToRight() && cluster->textRange().end <= block.fRange.end) {
760767
textStyle = block.fStyle;
761768
break;
@@ -771,7 +778,7 @@ std::unique_ptr<Run> TextLine::shapeEllipsis(const SkString& ellipsis, const Clu
771778
// single run, so if the affix would split in that direction (like
772779
// "Read more…" in RTL text), shape it in the opposite direction instead.
773780
SkBidiIterator::Level bidiLevel = run.bidiLevel();
774-
if (sk_sp<SkUnicode> unicode = fOwner->getUnicode()) {
781+
if (sk_sp<SkUnicode> unicode = owner->getUnicode()) {
775782
const bool ltr = run.leftToRight();
776783
const auto runDir = ltr ? SkUnicode::TextDirection::kLTR : SkUnicode::TextDirection::kRTL;
777784
const auto otherDir = ltr ? SkUnicode::TextDirection::kRTL : SkUnicode::TextDirection::kLTR;
@@ -790,13 +797,13 @@ std::unique_ptr<Run> TextLine::shapeEllipsis(const SkString& ellipsis, const Clu
790797
font.setSubpixel(textStyle.getSubpixel());
791798

792799
std::unique_ptr<SkShaper> shaper = SkShapers::HB::ShapeDontWrapOrReorder(
793-
fOwner->getUnicode(), fallback ? fallback : SkFontMgr::RefEmpty());
800+
owner->getUnicode(), fallback ? fallback : SkFontMgr::RefEmpty());
794801

795802
const char* utf8 = ellipsis.c_str();
796803
size_t utf8Bytes = ellipsis.size();
797804

798805
std::unique_ptr<SkShaper::BiDiRunIterator> bidi = SkShapers::unicode::BidiRunIterator(
799-
fOwner->getUnicode(), utf8, utf8Bytes, bidiLevel);
806+
owner->getUnicode(), utf8, utf8Bytes, bidiLevel);
800807
SkASSERT(bidi);
801808

802809
std::unique_ptr<SkShaper::LanguageRunIterator> language =
@@ -821,7 +828,7 @@ std::unique_ptr<Run> TextLine::shapeEllipsis(const SkString& ellipsis, const Clu
821828
&handler);
822829
auto ellipsisRun = handler.run();
823830
ellipsisRun->fTextRange = TextRange(0, ellipsis.size());
824-
ellipsisRun->fOwner = fOwner;
831+
ellipsisRun->fOwner = owner;
825832
return ellipsisRun;
826833
};
827834

@@ -832,7 +839,7 @@ std::unique_ptr<Run> TextLine::shapeEllipsis(const SkString& ellipsis, const Clu
832839
}
833840

834841
// Check all allowed fonts
835-
std::vector<sk_sp<SkTypeface>> typefaces = fOwner->fontCollection()->findTypefaces(
842+
std::vector<sk_sp<SkTypeface>> typefaces = owner->fontCollection()->findTypefaces(
836843
textStyle.getFontFamilies(), textStyle.getFontStyle(), textStyle.getFontArguments());
837844
for (const auto& typeface : typefaces) {
838845
ellipsisRun = shaped(typeface, nullptr);
@@ -842,25 +849,24 @@ std::unique_ptr<Run> TextLine::shapeEllipsis(const SkString& ellipsis, const Clu
842849
}
843850

844851
// Try the fallback
845-
if (fOwner->fontCollection()->fontFallbackEnabled()) {
852+
if (owner->fontCollection()->fontFallbackEnabled()) {
846853
const char* ch = ellipsis.c_str();
847854
SkUnichar unicode = SkUTF::NextUTF8WithReplacement(&ch,
848855
ellipsis.c_str()
849856
+ ellipsis.size());
850857
// We do not expect emojis in ellipsis so if they appeat there
851858
// they will not be resolved with the pretiest color emoji font
852-
auto typeface = fOwner->fontCollection()->defaultFallback(
853-
unicode,
854-
textStyle.getFontFamilies(),
855-
textStyle.getFontStyle(),
856-
textStyle.getLocale(),
857-
textStyle.getFontArguments());
858-
if (typeface) {
859-
ellipsisRun = shaped(typeface, fOwner->fontCollection()->getFallbackManager());
860-
if (ellipsisRun->isResolved()) {
861-
return ellipsisRun;
862-
}
863-
}
859+
auto typeface = owner->fontCollection()->defaultFallback(unicode,
860+
textStyle.getFontFamilies(),
861+
textStyle.getFontStyle(),
862+
textStyle.getLocale(),
863+
textStyle.getFontArguments());
864+
if (typeface) {
865+
ellipsisRun = shaped(typeface, owner->fontCollection()->getFallbackManager());
866+
if (ellipsisRun->isResolved()) {
867+
return ellipsisRun;
868+
}
869+
}
864870
}
865871
return ellipsisRun;
866872
}

‎modules/skparagraph/src/TextLine.h‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,13 @@ class TextLine {
149149

150150
bool endsWithHardLineBreak() const;
151151

152+
// Shapes an affix string (ellipsis or soft hyphen) at cluster using its run direction and
153+
// font fallback so layout width reservation and line rendering measure the same glyphs
154+
static std::unique_ptr<Run> ShapeString(ParagraphImpl* owner,
155+
BlockRange blockRange,
156+
const SkString& ellipsis,
157+
const Cluster* cluster);
158+
152159
private:
153160
std::unique_ptr<Run> shapeEllipsis(const SkString& ellipsis, const Cluster* cluster);
154161
void justify(SkScalar maxWidth);

‎modules/skparagraph/src/TextWrapper.cpp‎

Lines changed: 61 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,41 @@
33
#include "modules/skparagraph/src/TextWrapper.h"
44

55
#include "include/core/SkFont.h"
6+
#include "include/private/SkTemplates.h"
67

78
namespace skia {
89
namespace textlayout {
910

1011
namespace {
12+
// The width of the hyphen drawn when the line breaks after cluster, or 0 if none is drawn.
13+
// A soft hyphen that ends the text never breaks a line
14+
SkScalar softHyphenWidth(const SkString* softHyphen, const Cluster* cluster) {
15+
if (softHyphen == nullptr || !cluster->isSoftHyphen() ||
16+
cluster->textRange().end >= cluster->owner()->text().size()) {
17+
return 0;
18+
}
19+
const SkFont& font = cluster->run().font();
20+
skia_private::AutoSTArray<8, SkGlyphID> glyphs(softHyphen->size());
21+
size_t glyphCount = font.textToGlyphs(
22+
softHyphen->c_str(), softHyphen->size(), SkTextEncoding::kUTF8, glyphs);
23+
bool resolved = glyphCount > 0;
24+
for (size_t i = 0; i < glyphCount; ++i) {
25+
if (glyphs[i] == 0) {
26+
resolved = false;
27+
break;
28+
}
29+
}
30+
if (resolved) {
31+
return font.measureText(softHyphen->c_str(), softHyphen->size(), SkTextEncoding::kUTF8);
32+
}
33+
// When the cluster's font lacks a glyph for the hyphen string, shape with font fallback
34+
// matching TextLine::createSoftHyphen so reserved and intrinsic widths match the drawn hyphen
35+
ParagraphImpl* owner = cluster->owner();
36+
auto hyphenRun = TextLine::ShapeString(
37+
owner, owner->findAllBlocks(cluster->textRange()), *softHyphen, cluster);
38+
return hyphenRun ? hyphenRun->advance().fX : 0;
39+
}
40+
1141
struct LineBreakerWithLittleRounding {
1242
LineBreakerWithLittleRounding(SkScalar maxWidth, bool applyRoundingHack)
1343
: fLower(maxWidth - 0.25f)
@@ -54,7 +84,8 @@ struct LineBreakerWithLittleRounding {
5484
void TextWrapper::lookAhead(SkScalar maxWidth,
5585
Cluster* endOfClusters,
5686
bool applyRoundingHack,
57-
const SkString* softHyphen) {
87+
const SkString* softHyphen,
88+
bool lineMayEllipsize) {
5889

5990
reset();
6091
fEndLine.metrics().clean();
@@ -108,8 +139,9 @@ void TextWrapper::lookAhead(SkScalar maxWidth,
108139
// Walk further to see if there is a too long word, cluster or glyph
109140
SkScalar nextWordLength = fClusters.width();
110141
SkScalar nextShortWordLength = nextWordLength;
142+
SkScalar nextWordHyphenWidth = 0;
111143
for (auto further = cluster; further != endOfClusters; ++further) {
112-
if (further->isSoftBreak() || further->isHardBreak() || further->isWhitespaceBreak()) {
144+
if (further->isHardBreak() || further->isWhitespaceBreak()) {
113145
break;
114146
}
115147
if (further->run().isPlaceholder()) {
@@ -129,6 +161,11 @@ void TextWrapper::lookAhead(SkScalar maxWidth,
129161
} else {
130162
nextWordLength += further->width();
131163
}
164+
if (maxWidth > 0 && further->isSoftBreak()) {
165+
// Include further->width() before stopping so letter-spaced soft hyphens count
166+
nextWordHyphenWidth = softHyphenWidth(softHyphen, further);
167+
break;
168+
}
132169
}
133170
if (nextWordLength > maxWidth) {
134171
if (nextNonBreakingSpace != nullptr) {
@@ -150,14 +187,20 @@ void TextWrapper::lookAhead(SkScalar maxWidth,
150187
break;
151188
}
152189
// If the word is too long we can break it right now and hope it's enough
153-
fMinIntrinsicWidth = std::max(fMinIntrinsicWidth, nextWordLength);
190+
fMinIntrinsicWidth =
191+
std::max(fMinIntrinsicWidth, nextWordLength + nextWordHyphenWidth);
154192
if (fClusters.endPos() - fClusters.startPos() > 1 ||
155193
fWords.empty()) {
156194
fTooLongWord = true;
157195
} else {
158196
// Even if the word is too long there is a very little space on this line.
159197
// let's deal with it on the next line.
160198
}
199+
} else {
200+
// Record the hyphenated word width even when nextWordLength <= maxWidth so a word
201+
// pushed to the next line or cut off by maxLines still counts in fMinIntrinsicWidth
202+
fMinIntrinsicWidth =
203+
std::max(fMinIntrinsicWidth, nextWordLength + nextWordHyphenWidth);
161204
}
162205

163206
if (cluster->width() > maxWidth) {
@@ -185,13 +228,13 @@ void TextWrapper::lookAhead(SkScalar maxWidth,
185228

186229
// Keep adding clusters/words
187230
if (fClusters.endOfWord()) {
188-
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-
}
231+
// A word that ends at a soft hyphen is drawn with a hyphen when the line breaks
232+
// there, so the hyphen counts in its width (as in CSS min-content)
233+
SkScalar hyphenWidth = softHyphenWidth(softHyphen, cluster);
234+
fMinIntrinsicWidth =
235+
std::max(fMinIntrinsicWidth, getClustersTrimmedWidth() + hyphenWidth);
236+
bool tooWide = hyphenWidth > 0 && !lineMayEllipsize && !fWords.empty() &&
237+
breaker.breakLine(fWords.width() + fClusters.width() + hyphenWidth);
195238
if (tooWide && !softHyphenBreakTooWide) {
196239
// Keep the last break that fits, even past several soft hyphens that don't.
197240
wordsBeforeSoftHyphen = fWords;
@@ -346,9 +389,7 @@ void TextWrapper::breakTextIntoLines(ParagraphImpl* parent,
346389
auto lastLine = (hasEllipsis && unlimitedLines) || fLineNumber >= maxLines;
347390
needEllipsis = hasEllipsis && !endlessLine && lastLine;
348391

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);
392+
this->lookAhead(maxWidth, end, parent->getApplyRoundingHack(), softHyphen, needEllipsis);
352393

353394
this->moveForward(needEllipsis);
354395
needEllipsis &= fEndLine.endCluster() < end - 1; // Only if we have some text to ellipsize
@@ -497,6 +538,13 @@ void TextWrapper::breakTextIntoLines(ParagraphImpl* parent,
497538
// Nothing out of ordinary - just add this cluster to the word and to the line
498539
softLineMaxIntrinsicWidth += cluster->width();
499540
lastWordLength += cluster->width();
541+
if (cluster->isSoftBreak()) {
542+
// Words after maxLines also split at soft breaks and include any drawn hyphen
543+
fMinIntrinsicWidth =
544+
std::max(fMinIntrinsicWidth,
545+
lastWordLength + softHyphenWidth(softHyphen, cluster));
546+
lastWordLength = 0;
547+
}
500548
}
501549
++cluster;
502550
}

‎modules/skparagraph/src/TextWrapper.h‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -222,10 +222,12 @@ class TextWrapper {
222222
}
223223

224224
// softHyphen is the string drawn at soft hyphen line breaks, or nullptr if none is drawn.
225+
// A line that may get an ellipsis draws no hyphen, so it needs no room for one
225226
void lookAhead(SkScalar maxWidth,
226227
Cluster* endOfClusters,
227228
bool applyRoundingHack,
228-
const SkString* softHyphen);
229+
const SkString* softHyphen,
230+
bool lineMayEllipsize);
229231
void moveForward(bool hasEllipsis);
230232
void trimEndSpaces(TextAlign align);
231233
std::tuple<Cluster*, size_t, SkScalar> trimStartSpaces(Cluster* endOfClusters);

0 commit comments

Comments
 (0)