From d3848779f99b37026336ee67b6e6a895752b521c Mon Sep 17 00:00:00 2001 From: Eunchurn Park Date: Sat, 27 Dec 2025 15:41:04 +0900 Subject: [PATCH] fix(parser): handle oversized words in line break algorithm When a word exceeds the page width, the DP algorithm would leave dp[i] = MAX_COST, causing a cascade failure where all preceding words also got MAX_COST. This resulted in each word being placed on its own line. Fix: When dp[i] remains MAX_COST after the inner loop, force the oversized word onto its own line (ans[i] = i) and inherit the cost from the next word (dp[i] = dp[i+1]) to allow preceding words to find valid configurations. --- lib/Epub/Epub/ParsedText.cpp | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/lib/Epub/Epub/ParsedText.cpp b/lib/Epub/Epub/ParsedText.cpp index d73f80a..687f9df 100644 --- a/lib/Epub/Epub/ParsedText.cpp +++ b/lib/Epub/Epub/ParsedText.cpp @@ -106,6 +106,18 @@ std::vector ParsedText::computeLineBreaks(const int pageWidth, const int ans[i] = j; // j is the index of the last word in this optimal line } } + + // Handle oversized word: if no valid configuration found, force single-word line + // This prevents cascade failure where one oversized word breaks all preceding words + if (dp[i] == MAX_COST) { + ans[i] = i; // Just this word on its own line + // Inherit cost from next word to allow subsequent words to find valid configurations + if (i + 1 < static_cast(totalWordCount)) { + dp[i] = dp[i + 1]; + } else { + dp[i] = 0; + } + } } // Stores the index of the word that starts the next line (last_word_index + 1)