From a6f8bf6f530f80b00a7cc64605667dd1d75a971c Mon Sep 17 00:00:00 2001 From: Raph Levien Date: Wed, 24 Jun 2015 12:59:18 -0700 Subject: [PATCH] Separate additional penalty for last line with hyphen A recent change added a penalty for a hyphen at the last line break, which is visually undesirable. However, the penalty was assessed to "widthScore", which broke the assumption (used for another optimization) that widthScore increases monotonically. This patch separates the penalty into a different parameter, restoring the validity of the monotonicity assumption. Bug: 22066119 Change-Id: I6a47a350ef3ceee2f00ee430d6954d0c307227f0 --- libs/minikin/LineBreaker.cpp | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/libs/minikin/LineBreaker.cpp b/libs/minikin/LineBreaker.cpp index 8275a6cc10e..162d14b8af7 100644 --- a/libs/minikin/LineBreaker.cpp +++ b/libs/minikin/LineBreaker.cpp @@ -353,12 +353,17 @@ void LineBreaker::computeBreaksOptimal(bool isRectangle) { float delta = mCandidates[j].preBreak - leftEdge; // compute width score for line + + // Note: the "bestHope" optimization makes the assumption that, when delta is + // non-negative, widthScore will increase monotonically as successive candidate + // breaks are considered. float widthScore = 0.0f; + float additionalPenalty = 0.0f; if (delta < 0) { widthScore = SCORE_OVERFULL; } else if (atEnd && mStrategy != kBreakStrategy_Balanced) { // increase penalty for hyphen on last line - widthScore = LAST_LINE_PENALTY_MULTIPLIER * mCandidates[j].penalty; + additionalPenalty = LAST_LINE_PENALTY_MULTIPLIER * mCandidates[j].penalty; } else { widthScore = delta * delta; } @@ -369,7 +374,7 @@ void LineBreaker::computeBreaksOptimal(bool isRectangle) { bestHope = widthScore; } - float score = jScore + widthScore; + float score = jScore + widthScore + additionalPenalty; if (score <= best) { best = score; bestPrev = j; @@ -378,6 +383,9 @@ void LineBreaker::computeBreaksOptimal(bool isRectangle) { mCandidates[i].score = best + mCandidates[i].penalty + mLinePenalty; mCandidates[i].prev = bestPrev; mCandidates[i].lineNumber = mCandidates[bestPrev].lineNumber + 1; +#ifdef VERBOSE_DEBUG + ALOGD("break %d: score=%g, prev=%d", i, mCandidates[i].score, mCandidates[i].prev); +#endif } finishBreaksOptimal(); }