Enhance hyphenation logic to allow fallback breaks for better line breaking in ParsedText

This commit is contained in:
Arthur Tazhitdinov 2026-01-07 03:15:38 +05:00
parent f0ea0b4d5b
commit 07f1786ff2
2 changed files with 7 additions and 4 deletions

View File

@ -177,7 +177,9 @@ std::vector<size_t> ParsedText::computeHyphenatedLineBreaks(const GfxRenderer& r
} }
const int availableWidth = pageWidth - lineWidth - spacing; 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 // Widths updated for the split word; retry with current index
continue; continue;
} }
@ -199,7 +201,8 @@ std::vector<size_t> ParsedText::computeHyphenatedLineBreaks(const GfxRenderer& r
// Splits words[wordIndex] into prefix+hyphen and remainder when a legal breakpoint fits the available width. // 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, bool ParsedText::hyphenateWordAtIndex(const size_t wordIndex, const int availableWidth, const GfxRenderer& renderer,
const int fontId, std::vector<uint16_t>& wordWidths) { const int fontId, std::vector<uint16_t>& wordWidths,
const bool allowFallbackBreaks) {
if (availableWidth <= 0 || wordIndex >= words.size()) { if (availableWidth <= 0 || wordIndex >= words.size()) {
return false; return false;
} }
@ -209,7 +212,7 @@ bool ParsedText::hyphenateWordAtIndex(const size_t wordIndex, const int availabl
std::advance(wordIt, wordIndex); std::advance(wordIt, wordIndex);
std::advance(styleIt, wordIndex); std::advance(styleIt, wordIndex);
const auto breakOffsets = Hyphenator::breakOffsets(*wordIt, true); const auto breakOffsets = Hyphenator::breakOffsets(*wordIt, allowFallbackBreaks);
if (breakOffsets.empty()) { if (breakOffsets.empty()) {
return false; return false;
} }

View File

@ -24,7 +24,7 @@ class ParsedText {
std::vector<size_t> computeHyphenatedLineBreaks(const GfxRenderer& renderer, int fontId, int pageWidth, std::vector<size_t> computeHyphenatedLineBreaks(const GfxRenderer& renderer, int fontId, int pageWidth,
int spaceWidth, std::vector<uint16_t>& wordWidths); int spaceWidth, std::vector<uint16_t>& wordWidths);
bool hyphenateWordAtIndex(size_t wordIndex, int availableWidth, const GfxRenderer& renderer, int fontId, bool hyphenateWordAtIndex(size_t wordIndex, int availableWidth, const GfxRenderer& renderer, int fontId,
std::vector<uint16_t>& wordWidths); std::vector<uint16_t>& wordWidths, bool allowFallbackBreaks);
void extractLine(size_t breakIndex, int pageWidth, int spaceWidth, const std::vector<uint16_t>& wordWidths, void extractLine(size_t breakIndex, int pageWidth, int spaceWidth, const std::vector<uint16_t>& wordWidths,
const std::vector<size_t>& lineBreakIndices, const std::vector<size_t>& lineBreakIndices,
const std::function<void(std::shared_ptr<TextBlock>)>& processLine); const std::function<void(std::shared_ptr<TextBlock>)>& processLine);