Skip to content

Commit 773fe03

Browse files
jason-simmonsgspencergoog
authored andcommitted
Add an adjustment to currentLineWidth comparisons when pushing greedy line breaks (flutter#21356)
This is similar to the workaround used for flutter/flutter#30347 The Minikin line breaker inserts greedy breaks based on a comparison of postBreak width and currentLineWidth. currentLineWidth is provided by the framework based on previous calls to Layout::measureText. That calculation may not exactly match the calculation of postBreak. This change ensures that breaks are only added if the difference between postBreak and currentLineWidth is significant. Fixes flutter/flutter#65419
1 parent 49e765e commit 773fe03

File tree

1 file changed

+10
-3
lines changed

1 file changed

+10
-3
lines changed

third_party/txt/src/minikin/LineBreaker.cpp

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,12 @@ const size_t MAX_TEXT_BUF_RETAIN = 32678;
6262
// Maximum amount that spaces can shrink, in justified text.
6363
const float SHRINKABILITY = 1.0 / 3.0;
6464

65+
// libtxt: Add a fudge factor to comparisons between currentLineWidth and
66+
// postBreak width. The currentLineWidth passed by the Flutter framework
67+
// is based on maxIntrinsicWidth/Layout::measureText calculations that may
68+
// not precisely match the postBreak width.
69+
const float LIBTXT_WIDTH_ADJUST = 0.00001;
70+
6571
void LineBreaker::setLocale(const icu::Locale& locale, Hyphenator* hyphenator) {
6672
mWordBreaker.setLocale(locale);
6773
mLocale = locale;
@@ -240,7 +246,7 @@ void LineBreaker::addWordBreak(size_t offset,
240246
// libtxt: add a fudge factor to this comparison. The currentLineWidth passed
241247
// by the framework is based on maxIntrinsicWidth/Layout::measureText
242248
// calculations that may not precisely match the postBreak width.
243-
if (postBreak - width > currentLineWidth() + 0.00001) {
249+
if (postBreak - width > currentLineWidth() + LIBTXT_WIDTH_ADJUST) {
244250
// Add desperate breaks.
245251
// Note: these breaks are based on the shaping of the (non-broken) original
246252
// text; they are imprecise especially in the presence of kerning,
@@ -305,7 +311,7 @@ void LineBreaker::addCandidate(Candidate cand) {
305311
// mCandidates, and mPreBreak is its preBreak value. mBestBreak is the index
306312
// of the best line breaking candidate we have found since then, and
307313
// mBestScore is its penalty.
308-
if (cand.postBreak - mPreBreak > currentLineWidth()) {
314+
if (cand.postBreak - mPreBreak > currentLineWidth() + LIBTXT_WIDTH_ADJUST) {
309315
// This break would create an overfull line, pick the best break and break
310316
// there (greedy)
311317
if (mBestBreak == mLastBreak) {
@@ -316,7 +322,8 @@ void LineBreaker::addCandidate(Candidate cand) {
316322
}
317323

318324
while (mLastBreak != candIndex &&
319-
cand.postBreak - mPreBreak > currentLineWidth()) {
325+
cand.postBreak - mPreBreak >
326+
currentLineWidth() + LIBTXT_WIDTH_ADJUST) {
320327
// We should rarely come here. But if we are here, we have broken the line,
321328
// but the remaining part still doesn't fit. We now need to break at the
322329
// second best place after the last break, but we have not kept that

0 commit comments

Comments
 (0)