From de981f5072061f86109906de1b9752c714caf541 Mon Sep 17 00:00:00 2001 From: Zach Nelson Date: Thu, 19 Feb 2026 03:44:07 -0600 Subject: [PATCH] fix: Correct word width and space calculations (#963) ## Summary **What is the goal of this PR?** This change fixes an issue I noticed while reading where occasionally, especially in italics, some words would have too much space between them. The problem was that word width calculations were including any negative X overhang, and combined with a space before the word, that can lead to an inconsistently large space. ## Additional Context Screenshots of some problematic text: | In CrossPoint 1.0 | With this change | | -- | -- | | | | --- ### AI Usage While CrossPoint doesn't have restrictions on AI tools in contributing, please be transparent about their usage as it helps set the right context for reviewers. Did you use AI tools to help write this code? _**NO**_ --- lib/Epub/Epub/ParsedText.cpp | 10 ++++++---- lib/Epub/Epub/blocks/TextBlock.cpp | 2 +- lib/GfxRenderer/GfxRenderer.cpp | 8 ++++---- lib/GfxRenderer/GfxRenderer.h | 4 ++-- 4 files changed, 13 insertions(+), 11 deletions(-) diff --git a/lib/Epub/Epub/ParsedText.cpp b/lib/Epub/Epub/ParsedText.cpp index f4a0b51c..edc01a84 100644 --- a/lib/Epub/Epub/ParsedText.cpp +++ b/lib/Epub/Epub/ParsedText.cpp @@ -28,15 +28,17 @@ void stripSoftHyphensInPlace(std::string& word) { } } -// Returns the rendered width for a word while ignoring soft hyphen glyphs and optionally appending a visible hyphen. +// Returns the advance width for a word while ignoring soft hyphen glyphs and optionally appending a visible hyphen. +// Uses advance width (sum of glyph advances) rather than bounding box width so that italic glyph overhangs +// don't inflate inter-word spacing. uint16_t measureWordWidth(const GfxRenderer& renderer, const int fontId, const std::string& word, const EpdFontFamily::Style style, const bool appendHyphen = false) { if (word.size() == 1 && word[0] == ' ' && !appendHyphen) { - return renderer.getSpaceWidth(fontId); + return renderer.getSpaceWidth(fontId, style); } const bool hasSoftHyphen = containsSoftHyphen(word); if (!hasSoftHyphen && !appendHyphen) { - return renderer.getTextWidth(fontId, word.c_str(), style); + return renderer.getTextAdvanceX(fontId, word.c_str(), style); } std::string sanitized = word; @@ -46,7 +48,7 @@ uint16_t measureWordWidth(const GfxRenderer& renderer, const int fontId, const s if (appendHyphen) { sanitized.push_back('-'); } - return renderer.getTextWidth(fontId, sanitized.c_str(), style); + return renderer.getTextAdvanceX(fontId, sanitized.c_str(), style); } } // namespace diff --git a/lib/Epub/Epub/blocks/TextBlock.cpp b/lib/Epub/Epub/blocks/TextBlock.cpp index 471c3af8..1671e59a 100644 --- a/lib/Epub/Epub/blocks/TextBlock.cpp +++ b/lib/Epub/Epub/blocks/TextBlock.cpp @@ -30,7 +30,7 @@ void TextBlock::render(const GfxRenderer& renderer, const int fontId, const int if (w.size() >= 3 && static_cast(w[0]) == 0xE2 && static_cast(w[1]) == 0x80 && static_cast(w[2]) == 0x83) { const char* visiblePtr = w.c_str() + 3; - const int prefixWidth = renderer.getTextAdvanceX(fontId, "\xe2\x80\x83"); + const int prefixWidth = renderer.getTextAdvanceX(fontId, "\xe2\x80\x83", currentStyle); const int visibleWidth = renderer.getTextWidth(fontId, visiblePtr, currentStyle); startX = wordX + prefixWidth; underlineWidth = visibleWidth; diff --git a/lib/GfxRenderer/GfxRenderer.cpp b/lib/GfxRenderer/GfxRenderer.cpp index f615647d..85a9d1a4 100644 --- a/lib/GfxRenderer/GfxRenderer.cpp +++ b/lib/GfxRenderer/GfxRenderer.cpp @@ -808,17 +808,17 @@ int GfxRenderer::getScreenHeight() const { return HalDisplay::DISPLAY_WIDTH; } -int GfxRenderer::getSpaceWidth(const int fontId) const { +int GfxRenderer::getSpaceWidth(const int fontId, const EpdFontFamily::Style style) const { const auto fontIt = fontMap.find(fontId); if (fontIt == fontMap.end()) { LOG_ERR("GFX", "Font %d not found", fontId); return 0; } - return fontIt->second.getGlyph(' ', EpdFontFamily::REGULAR)->advanceX; + return fontIt->second.getGlyph(' ', style)->advanceX; } -int GfxRenderer::getTextAdvanceX(const int fontId, const char* text) const { +int GfxRenderer::getTextAdvanceX(const int fontId, const char* text, const EpdFontFamily::Style style) const { const auto fontIt = fontMap.find(fontId); if (fontIt == fontMap.end()) { LOG_ERR("GFX", "Font %d not found", fontId); @@ -829,7 +829,7 @@ int GfxRenderer::getTextAdvanceX(const int fontId, const char* text) const { int width = 0; const auto& font = fontIt->second; while ((cp = utf8NextCodepoint(reinterpret_cast(&text)))) { - width += font.getGlyph(cp, EpdFontFamily::REGULAR)->advanceX; + width += font.getGlyph(cp, style)->advanceX; } return width; } diff --git a/lib/GfxRenderer/GfxRenderer.h b/lib/GfxRenderer/GfxRenderer.h index 9758ce61..ecc33464 100644 --- a/lib/GfxRenderer/GfxRenderer.h +++ b/lib/GfxRenderer/GfxRenderer.h @@ -105,8 +105,8 @@ class GfxRenderer { EpdFontFamily::Style style = EpdFontFamily::REGULAR) const; void drawText(int fontId, int x, int y, const char* text, bool black = true, EpdFontFamily::Style style = EpdFontFamily::REGULAR) const; - int getSpaceWidth(int fontId) const; - int getTextAdvanceX(int fontId, const char* text) const; + int getSpaceWidth(int fontId, EpdFontFamily::Style style = EpdFontFamily::REGULAR) const; + int getTextAdvanceX(int fontId, const char* text, EpdFontFamily::Style style) const; int getFontAscenderSize(int fontId) const; int getLineHeight(int fontId) const; std::string truncatedText(int fontId, const char* text, int maxWidth,