Implement hyphenation support in text layout by enhancing word splitting and line breaking logic

This commit is contained in:
Arthur Tazhitdinov 2025-12-26 00:39:09 +05:00
parent 54d7a9437e
commit e7edcb6467
4 changed files with 255 additions and 131 deletions

View File

@ -1,15 +1,72 @@
#include "ParsedText.h" #include "ParsedText.h"
#include <GfxRenderer.h> #include <GfxRenderer.h>
#include "hyphenation/Hyphenator.h"
#include <algorithm> #include <algorithm>
#include <cmath> #include <cmath>
#include <functional> #include <functional>
#include <iterator>
#include <limits> #include <limits>
#include <vector> #include <vector>
constexpr int MAX_COST = std::numeric_limits<int>::max(); constexpr int MAX_COST = std::numeric_limits<int>::max();
namespace {
struct HyphenSplitDecision {
size_t byteOffset;
uint16_t prefixWidth;
};
struct HyphenationGuard {
size_t prefixIndex;
size_t tailIndex;
};
bool chooseSplitForWidth(const GfxRenderer& renderer, const int fontId, const std::string& word,
const EpdFontStyle style, const int availableWidth, const bool includeFallback,
HyphenSplitDecision* decision) {
if (!decision || availableWidth <= 0) {
return false;
}
const int hyphenWidth = renderer.getTextWidth(fontId, "-", style);
const int adjustedWidth = availableWidth - hyphenWidth;
if (adjustedWidth <= 0) {
return false;
}
auto offsets = Hyphenator::breakOffsets(word, includeFallback);
if (offsets.empty()) {
return false;
}
size_t chosenOffset = std::numeric_limits<size_t>::max();
uint16_t chosenWidth = 0;
for (const size_t offset : offsets) {
const std::string prefix = word.substr(0, offset);
const int prefixWidth = renderer.getTextWidth(fontId, prefix.c_str(), style);
if (prefixWidth <= adjustedWidth) {
chosenOffset = offset;
chosenWidth = static_cast<uint16_t>(prefixWidth + hyphenWidth);
} else {
break;
}
}
if (chosenOffset == std::numeric_limits<size_t>::max()) {
return false;
}
decision->byteOffset = chosenOffset;
decision->prefixWidth = chosenWidth;
return true;
}
} // namespace
void ParsedText::addWord(std::string word, const EpdFontStyle fontStyle) { void ParsedText::addWord(std::string word, const EpdFontStyle fontStyle) {
if (word.empty()) return; if (word.empty()) return;
@ -27,8 +84,9 @@ void ParsedText::layoutAndExtractLines(const GfxRenderer& renderer, const int fo
const int pageWidth = renderer.getScreenWidth() - horizontalMargin; const int pageWidth = renderer.getScreenWidth() - horizontalMargin;
const int spaceWidth = renderer.getSpaceWidth(fontId); const int spaceWidth = renderer.getSpaceWidth(fontId);
const auto wordWidths = calculateWordWidths(renderer, fontId); // Pre-split oversized tokens so the DP step always has feasible line candidates.
const auto lineBreakIndices = computeLineBreaks(pageWidth, spaceWidth, wordWidths); auto wordWidths = calculateWordWidths(renderer, fontId, pageWidth);
auto lineBreakIndices = computeLineBreaks(renderer, fontId, pageWidth, spaceWidth, wordWidths);
const size_t lineCount = includeLastLine ? lineBreakIndices.size() : lineBreakIndices.size() - 1; const size_t lineCount = includeLastLine ? lineBreakIndices.size() : lineBreakIndices.size() - 1;
for (size_t i = 0; i < lineCount; ++i) { for (size_t i = 0; i < lineCount; ++i) {
@ -36,7 +94,8 @@ void ParsedText::layoutAndExtractLines(const GfxRenderer& renderer, const int fo
} }
} }
std::vector<uint16_t> ParsedText::calculateWordWidths(const GfxRenderer& renderer, const int fontId) { std::vector<uint16_t> ParsedText::calculateWordWidths(const GfxRenderer& renderer, const int fontId,
const int pageWidth) {
const size_t totalWordCount = words.size(); const size_t totalWordCount = words.size();
std::vector<uint16_t> wordWidths; std::vector<uint16_t> wordWidths;
@ -52,7 +111,32 @@ std::vector<uint16_t> ParsedText::calculateWordWidths(const GfxRenderer& rendere
auto wordStylesIt = wordStyles.begin(); auto wordStylesIt = wordStyles.begin();
while (wordsIt != words.end()) { while (wordsIt != words.end()) {
wordWidths.push_back(renderer.getTextWidth(fontId, wordsIt->c_str(), *wordStylesIt)); uint16_t width = renderer.getTextWidth(fontId, wordsIt->c_str(), *wordStylesIt);
if (width > pageWidth) {
HyphenSplitDecision decision;
if (chooseSplitForWidth(renderer, fontId, *wordsIt, *wordStylesIt, pageWidth, true, &decision)) {
const std::string originalWord = *wordsIt;
const std::string tail = originalWord.substr(decision.byteOffset);
if (tail.empty()) {
continue;
}
const std::string prefix = originalWord.substr(0, decision.byteOffset) + "-";
*wordsIt = prefix;
auto nextWordIt = words.insert(std::next(wordsIt), tail);
auto nextStyleIt = wordStyles.insert(std::next(wordStylesIt), *wordStylesIt);
// Continue processing the freshly inserted tail so cascading splits still respect the limit.
wordWidths.push_back(decision.prefixWidth);
wordsIt = nextWordIt;
wordStylesIt = nextStyleIt;
continue;
}
}
wordWidths.push_back(width);
std::advance(wordsIt, 1); std::advance(wordsIt, 1);
std::advance(wordStylesIt, 1); std::advance(wordStylesIt, 1);
@ -61,71 +145,160 @@ std::vector<uint16_t> ParsedText::calculateWordWidths(const GfxRenderer& rendere
return wordWidths; return wordWidths;
} }
std::vector<size_t> ParsedText::computeLineBreaks(const int pageWidth, const int spaceWidth, std::vector<size_t> ParsedText::computeLineBreaks(const GfxRenderer& renderer, const int fontId, const int pageWidth,
const std::vector<uint16_t>& wordWidths) const { const int spaceWidth, std::vector<uint16_t>& wordWidths) {
const size_t totalWordCount = words.size(); if (words.empty()) {
return {};
}
std::vector<HyphenationGuard> guards;
auto shiftGuardIndices = [&](size_t insertPos) {
for (auto& guard : guards) {
if (guard.prefixIndex >= insertPos) {
guard.prefixIndex++;
}
if (guard.tailIndex >= insertPos) {
guard.tailIndex++;
}
}
};
auto runDp = [&](std::vector<size_t>& lineBreaks) {
const size_t totalWordCount = wordWidths.size();
// DP table to store the minimum badness (cost) of lines starting at index i
std::vector<int> dp(totalWordCount); std::vector<int> dp(totalWordCount);
// 'ans[i]' stores the index 'j' of the *last word* in the optimal line starting at 'i'
std::vector<size_t> ans(totalWordCount); std::vector<size_t> ans(totalWordCount);
// Base Case
dp[totalWordCount - 1] = 0; dp[totalWordCount - 1] = 0;
ans[totalWordCount - 1] = totalWordCount - 1; ans[totalWordCount - 1] = totalWordCount - 1;
for (int i = totalWordCount - 2; i >= 0; --i) { for (int i = static_cast<int>(totalWordCount) - 2; i >= 0; --i) {
int currlen = -spaceWidth; int currlen = -spaceWidth;
dp[i] = MAX_COST; dp[i] = MAX_COST;
for (size_t j = i; j < totalWordCount; ++j) { for (size_t j = i; j < totalWordCount; ++j) {
// Current line length: previous width + space + current word width
currlen += wordWidths[j] + spaceWidth; currlen += wordWidths[j] + spaceWidth;
if (currlen > pageWidth) { if (currlen > pageWidth) {
break; break;
} }
bool violatesGuard = false;
for (const auto& guard : guards) {
if (i <= guard.prefixIndex && j >= guard.tailIndex) {
violatesGuard = true;
break;
}
}
if (violatesGuard) {
continue;
}
int cost; int cost;
if (j == totalWordCount - 1) { if (j == totalWordCount - 1) {
cost = 0; // Last line cost = 0;
} else { } else {
const int remainingSpace = pageWidth - currlen; const int remainingSpace = pageWidth - currlen;
// Use long long for the square to prevent overflow
const long long cost_ll = static_cast<long long>(remainingSpace) * remainingSpace + dp[j + 1]; const long long cost_ll = static_cast<long long>(remainingSpace) * remainingSpace + dp[j + 1];
cost = cost_ll > MAX_COST ? MAX_COST : static_cast<int>(cost_ll);
if (cost_ll > MAX_COST) {
cost = MAX_COST;
} else {
cost = static_cast<int>(cost_ll);
}
} }
if (cost < dp[i]) { if (cost < dp[i]) {
dp[i] = cost; dp[i] = cost;
ans[i] = j; // j is the index of the last word in this optimal line ans[i] = j;
} }
} }
} }
// Stores the index of the word that starts the next line (last_word_index + 1) lineBreaks.clear();
std::vector<size_t> lineBreakIndices;
size_t currentWordIndex = 0; size_t currentWordIndex = 0;
constexpr size_t MAX_LINES = 1000; constexpr size_t MAX_LINES = 1000;
while (currentWordIndex < totalWordCount) { while (currentWordIndex < totalWordCount && lineBreaks.size() < MAX_LINES) {
if (lineBreakIndices.size() >= MAX_LINES) { const size_t nextBreakIndex = ans[currentWordIndex] + 1;
lineBreaks.push_back(nextBreakIndex);
currentWordIndex = nextBreakIndex;
}
};
std::vector<size_t> lineBreakIndices;
while (true) {
runDp(lineBreakIndices);
if (!hyphenationEnabled) {
return lineBreakIndices;
}
bool insertedSplit = false;
size_t lastBreakAt = 0;
for (size_t lineIdx = 0; lineIdx < lineBreakIndices.size(); ++lineIdx) {
const size_t lineBreak = lineBreakIndices[lineIdx];
const bool isLastLine = lineIdx == lineBreakIndices.size() - 1;
const size_t lineWordCount = lineBreak - lastBreakAt;
int lineWordWidthSum = 0;
for (size_t idx = lastBreakAt; idx < lineBreak; ++idx) {
lineWordWidthSum += wordWidths[idx];
}
lastBreakAt = lineBreak;
if (isLastLine || lineBreak >= wordWidths.size()) {
continue;
}
const size_t spacingCount = lineWordCount > 0 ? lineWordCount - 1 : 0;
const int usedSpace = lineWordWidthSum + static_cast<int>(spacingCount) * spaceWidth;
const int unusedWidth = pageWidth - usedSpace;
const int spaceNeeded = lineWordCount == 0 ? 0 : spaceWidth;
const int budgetForPrefix = unusedWidth - spaceNeeded;
if (budgetForPrefix <= 0) {
continue;
}
auto nextWordIt = words.begin();
auto nextStyleIt = wordStyles.begin();
std::advance(nextWordIt, lineBreak);
std::advance(nextStyleIt, lineBreak);
if (nextWordIt == words.end()) {
break; break;
} }
size_t nextBreakIndex = ans[currentWordIndex] + 1; HyphenSplitDecision decision;
lineBreakIndices.push_back(nextBreakIndex); if (!chooseSplitForWidth(renderer, fontId, *nextWordIt, *nextStyleIt, budgetForPrefix, false, &decision)) {
continue;
currentWordIndex = nextBreakIndex;
} }
const EpdFontStyle styleForSplit = *nextStyleIt;
const std::string originalWord = *nextWordIt;
const std::string prefix = originalWord.substr(0, decision.byteOffset) + "-";
const std::string tail = originalWord.substr(decision.byteOffset);
if (tail.empty()) {
continue;
}
*nextWordIt = tail;
words.insert(nextWordIt, prefix);
wordStyles.insert(nextStyleIt, styleForSplit);
const uint16_t tailWidth = renderer.getTextWidth(fontId, tail.c_str(), styleForSplit);
wordWidths.insert(wordWidths.begin() + lineBreak, decision.prefixWidth);
wordWidths[lineBreak + 1] = tailWidth;
shiftGuardIndices(lineBreak);
guards.push_back({lineBreak, lineBreak + 1});
insertedSplit = true;
break;
}
if (!insertedSplit) {
return lineBreakIndices; return lineBreakIndices;
} }
}
}
void ParsedText::extractLine(const size_t breakIndex, const int pageWidth, const int spaceWidth, void ParsedText::extractLine(const size_t breakIndex, const int pageWidth, const int spaceWidth,
const std::vector<uint16_t>& wordWidths, const std::vector<size_t>& lineBreakIndices, const std::vector<uint16_t>& wordWidths, const std::vector<size_t>& lineBreakIndices,
@ -136,8 +309,8 @@ void ParsedText::extractLine(const size_t breakIndex, const int pageWidth, const
// Calculate total word width for this line // Calculate total word width for this line
int lineWordWidthSum = 0; int lineWordWidthSum = 0;
for (size_t i = lastBreakAt; i < lineBreak; i++) { for (size_t idx = lastBreakAt; idx < lineBreak; ++idx) {
lineWordWidthSum += wordWidths[i]; lineWordWidthSum += wordWidths[idx];
} }
// Calculate spacing // Calculate spacing

View File

@ -19,11 +19,12 @@ class ParsedText {
bool extraParagraphSpacing; bool extraParagraphSpacing;
bool hyphenationEnabled; bool hyphenationEnabled;
std::vector<size_t> computeLineBreaks(int pageWidth, int spaceWidth, const std::vector<uint16_t>& wordWidths) const; std::vector<size_t> computeLineBreaks(const GfxRenderer& renderer, int fontId, int pageWidth, int spaceWidth,
std::vector<uint16_t>& wordWidths);
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);
std::vector<uint16_t> calculateWordWidths(const GfxRenderer& renderer, int fontId); std::vector<uint16_t> calculateWordWidths(const GfxRenderer& renderer, int fontId, int pageWidth);
public: public:
explicit ParsedText(const TextBlock::BLOCK_STYLE style, const bool extraParagraphSpacing, explicit ParsedText(const TextBlock::BLOCK_STYLE style, const bool extraParagraphSpacing,

View File

@ -1,11 +1,9 @@
#include "Hyphenator.h" #include "Hyphenator.h"
#include <GfxRenderer.h>
#include <Utf8.h> #include <Utf8.h>
#include <algorithm> #include <algorithm>
#include <array> #include <array>
#include <limits>
#include <vector> #include <vector>
#include "EnglishHyphenator.h" #include "EnglishHyphenator.h"
@ -87,84 +85,44 @@ size_t byteOffsetForIndex(const std::vector<CodepointInfo>& cps, const size_t in
return cps[index].byteOffset; return cps[index].byteOffset;
} }
// Safely slices a UTF-8 string without splitting multibyte sequences.
std::string slice(const std::string& word, const size_t startByte, const size_t endByte) {
if (startByte >= endByte || startByte >= word.size()) {
return std::string();
}
const size_t boundedEnd = std::min(endByte, word.size());
return word.substr(startByte, boundedEnd - startByte);
}
} // namespace } // namespace
bool Hyphenator::splitWord(const GfxRenderer& renderer, const int fontId, const std::string& word, std::vector<size_t> Hyphenator::breakOffsets(const std::string& word, const bool includeFallback) {
const EpdFontStyle style, const int availableWidth, HyphenationResult* result, std::vector<size_t> byteOffsets;
const bool force) { if (word.empty()) {
if (!result || word.empty()) { return byteOffsets;
return false;
} }
auto cps = collectCodepoints(word); auto cps = collectCodepoints(word);
if (cps.size() < MIN_PREFIX_CP + MIN_SUFFIX_CP) { if (cps.size() < MIN_PREFIX_CP + MIN_SUFFIX_CP) {
return false; return byteOffsets;
} }
// Skip mixed tokens (e.g., "v2.0") unless the caller forces a split due to overflow. std::vector<size_t> indexes;
if (!force && !hasOnlyAlphabetic(cps)) { indexes.reserve(cps.size());
return false;
if (hasOnlyAlphabetic(cps)) {
auto dictBreaks = collectBreakIndexes(cps);
indexes.insert(indexes.end(), dictBreaks.begin(), dictBreaks.end());
} }
const auto breakIndexes = collectBreakIndexes(cps); if (includeFallback) {
// Budget for a trailing hyphen so rendered width matches the layout test.
const int hyphenWidth = renderer.getTextWidth(fontId, "-", style);
const int adjustedWidth = availableWidth - hyphenWidth;
size_t chosenIndex = std::numeric_limits<size_t>::max();
// Prefer dictionary-style break points emitted by language hyphenators.
if (adjustedWidth > 0) {
for (const size_t idx : breakIndexes) {
const size_t byteOffset = byteOffsetForIndex(cps, idx);
const std::string prefix = word.substr(0, byteOffset);
const int prefixWidth = renderer.getTextWidth(fontId, prefix.c_str(), style);
if (prefixWidth <= adjustedWidth) {
chosenIndex = idx;
} else {
break;
}
}
}
if (chosenIndex == std::numeric_limits<size_t>::max() && force) {
// Emergency fallback: brute-force through codepoints to avoid overflow when no legal breaks fit.
for (size_t idx = MIN_PREFIX_CP; idx + MIN_SUFFIX_CP <= cps.size(); ++idx) { for (size_t idx = MIN_PREFIX_CP; idx + MIN_SUFFIX_CP <= cps.size(); ++idx) {
const size_t byteOffset = byteOffsetForIndex(cps, idx); indexes.push_back(idx);
const std::string prefix = word.substr(0, byteOffset);
const int prefixWidth = renderer.getTextWidth(fontId, prefix.c_str(), style);
if (adjustedWidth <= 0 || prefixWidth <= adjustedWidth) {
chosenIndex = idx;
if (adjustedWidth > 0 && prefixWidth > adjustedWidth) {
break;
}
}
} }
} }
if (chosenIndex == std::numeric_limits<size_t>::max()) { if (indexes.empty()) {
return false; return byteOffsets;
} }
const size_t splitByte = byteOffsetForIndex(cps, chosenIndex); std::sort(indexes.begin(), indexes.end());
const std::string head = word.substr(0, splitByte); indexes.erase(std::unique(indexes.begin(), indexes.end()), indexes.end());
const std::string tail = slice(word, splitByte, word.size());
if (head.empty() || tail.empty()) { byteOffsets.reserve(indexes.size());
return false; for (const size_t idx : indexes) {
byteOffsets.push_back(byteOffsetForIndex(cps, idx));
} }
// Append the printed hyphen to the prefix while leaving the tail untouched. return byteOffsets;
result->head = head + "-";
result->tail = tail;
return true;
} }

View File

@ -1,20 +1,12 @@
#pragma once #pragma once
#include <EpdFontFamily.h> #include <cstddef>
#include <string> #include <string>
#include <vector>
class GfxRenderer;
// Holds the split portions of a hyphenated word.
struct HyphenationResult {
std::string head;
std::string tail;
};
class Hyphenator { class Hyphenator {
public: public:
// Splits a word so it fits within availableWidth, appending a hyphen to the head when needed. // Returns byte offsets where the word may be hyphenated. When includeFallback is true, all positions obeying the
static bool splitWord(const GfxRenderer& renderer, int fontId, const std::string& word, EpdFontStyle style, // minimum prefix/suffix constraints are returned even if no language-specific rule matches.
int availableWidth, HyphenationResult* result, bool force); static std::vector<size_t> breakOffsets(const std::string& word, bool includeFallback);
}; };