diff --git a/lib/Epub/Epub/ParsedText.cpp b/lib/Epub/Epub/ParsedText.cpp index 82d0db61..1a0d2c56 100644 --- a/lib/Epub/Epub/ParsedText.cpp +++ b/lib/Epub/Epub/ParsedText.cpp @@ -29,15 +29,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; @@ -47,7 +49,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 ac105caa..1ace7283 100644 --- a/lib/Epub/Epub/blocks/TextBlock.cpp +++ b/lib/Epub/Epub/blocks/TextBlock.cpp @@ -33,7 +33,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 21234483..1c7fd986 100644 --- a/lib/GfxRenderer/GfxRenderer.cpp +++ b/lib/GfxRenderer/GfxRenderer.cpp @@ -722,17 +722,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); @@ -743,7 +743,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 4054e15a..68364499 100644 --- a/lib/GfxRenderer/GfxRenderer.h +++ b/lib/GfxRenderer/GfxRenderer.h @@ -110,8 +110,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,