Bug 1733876 Part 2 - Run line wrapping algorithm only if content of current line is not empty. r=m_kato,mbrodesser

With this edge case explicitly considered, it will be easier for the
following patches to simplify logic in `FindWrapIndexForContent()`.

Differential Revision: https://phabricator.services.mozilla.com/D127382
This commit is contained in:
Ting-Yu Lin 2021-10-07 23:01:40 +00:00
parent 11076c9ddf
commit afe1ba25ee

View File

@ -118,6 +118,7 @@ void nsPlainTextSerializer::CurrentLine::ResetContentAndIndentationHeader() {
int32_t nsPlainTextSerializer::CurrentLine::FindWrapIndexForContent(
const uint32_t aWrapColumn, const uint32_t aContentWidth,
mozilla::intl::LineBreaker* aLineBreaker) const {
MOZ_ASSERT(!mContent.IsEmpty());
MOZ_ASSERT(aContentWidth < std::numeric_limits<int32_t>::max());
MOZ_ASSERT(static_cast<int32_t>(aContentWidth) ==
GetUnicharStringWidth(mContent));
@ -149,7 +150,7 @@ int32_t nsPlainTextSerializer::CurrentLine::FindWrapIndexForContent(
// https://bugzilla.mozilla.org/show_bug.cgi?id=333064 for more
// information.
if (mContent.IsEmpty() || aWrapColumn < prefixwidth) {
if (aWrapColumn < prefixwidth) {
goodSpace = NS_LINEBREAKER_NEED_MORE_TEXT;
} else {
goodSpace = std::min(aWrapColumn - prefixwidth, mContent.Length() - 1);
@ -1236,10 +1237,6 @@ void nsPlainTextSerializer::MaybeWrapAndOutputCompleteLines() {
const uint32_t prefixwidth = mCurrentLine.DeterminePrefixWidth();
// The width of the line as it will appear on the screen (approx.).
uint32_t currentLineContentWidth =
GetUnicharStringWidth(mCurrentLine.mContent);
// Yes, wrap!
// The "+4" is to avoid wrap lines that only would be a couple
// of letters too long. We give this bonus only if the
@ -1247,7 +1244,14 @@ void nsPlainTextSerializer::MaybeWrapAndOutputCompleteLines() {
const uint32_t wrapColumn = mSettings.GetWrapColumn();
uint32_t bonuswidth = (wrapColumn > 20) ? 4 : 0;
while (currentLineContentWidth + prefixwidth > wrapColumn + bonuswidth) {
while (!mCurrentLine.mContent.IsEmpty()) {
// The width of the line as it will appear on the screen (approx.).
const uint32_t currentLineContentWidth =
GetUnicharStringWidth(mCurrentLine.mContent);
if (currentLineContentWidth + prefixwidth <= wrapColumn + bonuswidth) {
break;
}
const int32_t goodSpace = mCurrentLine.FindWrapIndexForContent(
wrapColumn, currentLineContentWidth, mLineBreaker);
@ -1281,7 +1285,6 @@ void nsPlainTextSerializer::MaybeWrapAndOutputCompleteLines() {
}
}
mCurrentLine.mContent.Append(restOfContent);
currentLineContentWidth = GetUnicharStringWidth(mCurrentLine.mContent);
mEmptyLines = -1;
} else {
// Nothing to do. Hopefully we get more data later