refactor: Streamline hyphenation logic by consolidating fallback break handling

This commit is contained in:
Arthur Tazhitdinov 2026-01-14 23:32:56 +05:00
parent 25d251abe5
commit baa2aff2b2
2 changed files with 3 additions and 13 deletions

View File

@ -112,10 +112,6 @@ std::vector<size_t> ParsedText::computeLineBreaks(const GfxRenderer& renderer, c
// Ensure any word that would overflow even as the first entry on a line is split using fallback hyphenation.
for (size_t i = 0; i < wordWidths.size(); ++i) {
while (wordWidths[i] > pageWidth) {
// Try language-aware hyphenation first; only fall back to heuristics when no dictionary break fits.
if (hyphenateWordAtIndex(i, pageWidth, renderer, fontId, wordWidths, /*allowFallbackBreaks=*/false)) {
continue;
}
if (!hyphenateWordAtIndex(i, pageWidth, renderer, fontId, wordWidths, /*allowFallbackBreaks=*/true)) {
break;
}
@ -279,10 +275,7 @@ bool ParsedText::hyphenateWordAtIndex(const size_t wordIndex, const int availabl
const auto style = *styleIt;
// Collect candidate breakpoints (byte offsets and hyphen requirements).
auto breakInfos = Hyphenator::breakOffsets(word, /*allowFallback=*/false);
if (breakInfos.empty() && allowFallbackBreaks) {
breakInfos = Hyphenator::breakOffsets(word, /*allowFallback=*/true);
}
auto breakInfos = Hyphenator::breakOffsets(word, allowFallbackBreaks);
if (breakInfos.empty()) {
return false;
}

View File

@ -78,14 +78,11 @@ std::vector<Hyphenator::BreakInfo> Hyphenator::breakOffsets(const std::string& w
indexes = hyphenator->breakIndexes(cps);
}
// Only add fallback breaks if needed and deduplicate if both language and fallback breaks exist.
if (includeFallback) {
// Only add fallback breaks if needed
if (includeFallback && indexes.empty()) {
for (size_t idx = minPrefix; idx + minSuffix <= cps.size(); ++idx) {
indexes.push_back(idx);
}
// Only deduplicate if we have both language-specific and fallback breaks.
std::sort(indexes.begin(), indexes.end());
indexes.erase(std::unique(indexes.begin(), indexes.end()), indexes.end());
}
if (indexes.empty()) {