Bugfix/word spacing indented (#59)
Some checks are pending
CI / build (push) Waiting to run

Simplified the indentation to fix having too large gaps between words
(original calculation was inaccurate).
This commit is contained in:
Jonas Diemer 2025-12-18 22:45:20 +01:00 committed by GitHub
parent 1a3d6b125d
commit d86b3fe134
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -27,12 +27,16 @@ void ParsedText::layoutAndExtractLines(const GfxRenderer& renderer, const int fo
const size_t totalWordCount = words.size(); const size_t totalWordCount = words.size();
const int pageWidth = renderer.getScreenWidth() - horizontalMargin; const int pageWidth = renderer.getScreenWidth() - horizontalMargin;
const int spaceWidth = renderer.getSpaceWidth(fontId); const int spaceWidth = renderer.getSpaceWidth(fontId);
// width of 1em to indent first line of paragraph if Extra Spacing is enabled
const int indentWidth = (!extraParagraphSpacing) ? 1 * renderer.getTextWidth(fontId, "m", REGULAR) : 0;
std::vector<uint16_t> wordWidths; std::vector<uint16_t> wordWidths;
wordWidths.reserve(totalWordCount); wordWidths.reserve(totalWordCount);
// add em-space at the beginning of first word in paragraph to indent
if (!extraParagraphSpacing) {
std::string& first_word = words.front();
first_word.insert(0, "\xe2\x80\x83");
}
auto wordsIt = words.begin(); auto wordsIt = words.begin();
auto wordStylesIt = wordStyles.begin(); auto wordStylesIt = wordStyles.begin();
@ -53,7 +57,7 @@ void ParsedText::layoutAndExtractLines(const GfxRenderer& renderer, const int fo
ans[totalWordCount - 1] = totalWordCount - 1; ans[totalWordCount - 1] = totalWordCount - 1;
for (int i = totalWordCount - 2; i >= 0; --i) { for (int i = totalWordCount - 2; i >= 0; --i) {
int currlen = -spaceWidth + indentWidth; 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) {
@ -125,9 +129,6 @@ void ParsedText::layoutAndExtractLines(const GfxRenderer& renderer, const int fo
// Calculate spacing // Calculate spacing
int spareSpace = pageWidth - lineWordWidthSum; int spareSpace = pageWidth - lineWordWidthSum;
if (wordWidthIndex == 0) {
spareSpace -= indentWidth;
}
int spacing = spaceWidth; int spacing = spaceWidth;
const bool isLastLine = lineBreak == totalWordCount; const bool isLastLine = lineBreak == totalWordCount;
@ -137,8 +138,7 @@ void ParsedText::layoutAndExtractLines(const GfxRenderer& renderer, const int fo
} }
// Calculate initial x position // Calculate initial x position
uint16_t xpos = (wordWidthIndex == 0) ? indentWidth : 0; uint16_t xpos = 0;
if (style == TextBlock::RIGHT_ALIGN) { if (style == TextBlock::RIGHT_ALIGN) {
xpos = spareSpace - (lineWordCount - 1) * spaceWidth; xpos = spareSpace - (lineWordCount - 1) * spaceWidth;
} else if (style == TextBlock::CENTER_ALIGN) { } else if (style == TextBlock::CENTER_ALIGN) {