From 07f1786ff2f76661f5bef24d5c75eb549ab0fd10 Mon Sep 17 00:00:00 2001 From: Arthur Tazhitdinov Date: Wed, 7 Jan 2026 03:15:38 +0500 Subject: [PATCH] Enhance hyphenation logic to allow fallback breaks for better line breaking in ParsedText --- lib/Epub/Epub/ParsedText.cpp | 9 ++++++--- lib/Epub/Epub/ParsedText.h | 2 +- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/lib/Epub/Epub/ParsedText.cpp b/lib/Epub/Epub/ParsedText.cpp index 7dac22ad..305ba1a2 100644 --- a/lib/Epub/Epub/ParsedText.cpp +++ b/lib/Epub/Epub/ParsedText.cpp @@ -177,7 +177,9 @@ std::vector ParsedText::computeHyphenatedLineBreaks(const GfxRenderer& r } const int availableWidth = pageWidth - lineWidth - spacing; - if (availableWidth > 0 && hyphenateWordAtIndex(currentIndex, availableWidth, renderer, fontId, wordWidths)) { + const bool allowFallbackBreaks = isFirstWord; // Only permit fallback splits when even the first word overflows + if (availableWidth > 0 && + hyphenateWordAtIndex(currentIndex, availableWidth, renderer, fontId, wordWidths, allowFallbackBreaks)) { // Widths updated for the split word; retry with current index continue; } @@ -199,7 +201,8 @@ std::vector ParsedText::computeHyphenatedLineBreaks(const GfxRenderer& r // Splits words[wordIndex] into prefix+hyphen and remainder when a legal breakpoint fits the available width. bool ParsedText::hyphenateWordAtIndex(const size_t wordIndex, const int availableWidth, const GfxRenderer& renderer, - const int fontId, std::vector& wordWidths) { + const int fontId, std::vector& wordWidths, + const bool allowFallbackBreaks) { if (availableWidth <= 0 || wordIndex >= words.size()) { return false; } @@ -209,7 +212,7 @@ bool ParsedText::hyphenateWordAtIndex(const size_t wordIndex, const int availabl std::advance(wordIt, wordIndex); std::advance(styleIt, wordIndex); - const auto breakOffsets = Hyphenator::breakOffsets(*wordIt, true); + const auto breakOffsets = Hyphenator::breakOffsets(*wordIt, allowFallbackBreaks); if (breakOffsets.empty()) { return false; } diff --git a/lib/Epub/Epub/ParsedText.h b/lib/Epub/Epub/ParsedText.h index e7f53f9c..1089054e 100644 --- a/lib/Epub/Epub/ParsedText.h +++ b/lib/Epub/Epub/ParsedText.h @@ -24,7 +24,7 @@ class ParsedText { std::vector computeHyphenatedLineBreaks(const GfxRenderer& renderer, int fontId, int pageWidth, int spaceWidth, std::vector& wordWidths); bool hyphenateWordAtIndex(size_t wordIndex, int availableWidth, const GfxRenderer& renderer, int fontId, - std::vector& wordWidths); + std::vector& wordWidths, bool allowFallbackBreaks); void extractLine(size_t breakIndex, int pageWidth, int spaceWidth, const std::vector& wordWidths, const std::vector& lineBreakIndices, const std::function)>& processLine);