From d86b3fe1341007dc2ea3e8105e1481dd106ced1e Mon Sep 17 00:00:00 2001 From: Jonas Diemer Date: Thu, 18 Dec 2025 22:45:20 +0100 Subject: [PATCH] Bugfix/word spacing indented (#59) Simplified the indentation to fix having too large gaps between words (original calculation was inaccurate). --- lib/Epub/Epub/ParsedText.cpp | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/lib/Epub/Epub/ParsedText.cpp b/lib/Epub/Epub/ParsedText.cpp index b666192..3747246 100644 --- a/lib/Epub/Epub/ParsedText.cpp +++ b/lib/Epub/Epub/ParsedText.cpp @@ -27,12 +27,16 @@ void ParsedText::layoutAndExtractLines(const GfxRenderer& renderer, const int fo const size_t totalWordCount = words.size(); const int pageWidth = renderer.getScreenWidth() - horizontalMargin; 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 wordWidths; 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 wordStylesIt = wordStyles.begin(); @@ -53,7 +57,7 @@ void ParsedText::layoutAndExtractLines(const GfxRenderer& renderer, const int fo ans[totalWordCount - 1] = totalWordCount - 1; for (int i = totalWordCount - 2; i >= 0; --i) { - int currlen = -spaceWidth + indentWidth; + int currlen = -spaceWidth; dp[i] = MAX_COST; for (size_t j = i; j < totalWordCount; ++j) { @@ -125,9 +129,6 @@ void ParsedText::layoutAndExtractLines(const GfxRenderer& renderer, const int fo // Calculate spacing int spareSpace = pageWidth - lineWordWidthSum; - if (wordWidthIndex == 0) { - spareSpace -= indentWidth; - } int spacing = spaceWidth; const bool isLastLine = lineBreak == totalWordCount; @@ -137,8 +138,7 @@ void ParsedText::layoutAndExtractLines(const GfxRenderer& renderer, const int fo } // Calculate initial x position - uint16_t xpos = (wordWidthIndex == 0) ? indentWidth : 0; - + uint16_t xpos = 0; if (style == TextBlock::RIGHT_ALIGN) { xpos = spareSpace - (lineWordCount - 1) * spaceWidth; } else if (style == TextBlock::CENTER_ALIGN) {