A fork of https://github.com/crosspoint-reader/crosspoint-reader
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

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 |
| -- | -- |
| <img
src="https://github.com/user-attachments/assets/87bf0e4b-341f-4ba9-b3ea-38c13bd26363"
width="400" /> | <img
src="https://github.com/user-attachments/assets/bf11ba20-c297-4ce1-aa07-43477ef86fc2"
width="400" /> |

---

### 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**_

authored by

Zach Nelson and committed by
GitHub
f1740dbe 6be4413c

+13 -11
+6 -4
lib/Epub/Epub/ParsedText.cpp
··· 29 29 } 30 30 } 31 31 32 - // Returns the rendered width for a word while ignoring soft hyphen glyphs and optionally appending a visible hyphen. 32 + // Returns the advance width for a word while ignoring soft hyphen glyphs and optionally appending a visible hyphen. 33 + // Uses advance width (sum of glyph advances) rather than bounding box width so that italic glyph overhangs 34 + // don't inflate inter-word spacing. 33 35 uint16_t measureWordWidth(const GfxRenderer& renderer, const int fontId, const std::string& word, 34 36 const EpdFontFamily::Style style, const bool appendHyphen = false) { 35 37 if (word.size() == 1 && word[0] == ' ' && !appendHyphen) { 36 - return renderer.getSpaceWidth(fontId); 38 + return renderer.getSpaceWidth(fontId, style); 37 39 } 38 40 const bool hasSoftHyphen = containsSoftHyphen(word); 39 41 if (!hasSoftHyphen && !appendHyphen) { 40 - return renderer.getTextWidth(fontId, word.c_str(), style); 42 + return renderer.getTextAdvanceX(fontId, word.c_str(), style); 41 43 } 42 44 43 45 std::string sanitized = word; ··· 47 49 if (appendHyphen) { 48 50 sanitized.push_back('-'); 49 51 } 50 - return renderer.getTextWidth(fontId, sanitized.c_str(), style); 52 + return renderer.getTextAdvanceX(fontId, sanitized.c_str(), style); 51 53 } 52 54 53 55 } // namespace
+1 -1
lib/Epub/Epub/blocks/TextBlock.cpp
··· 33 33 if (w.size() >= 3 && static_cast<uint8_t>(w[0]) == 0xE2 && static_cast<uint8_t>(w[1]) == 0x80 && 34 34 static_cast<uint8_t>(w[2]) == 0x83) { 35 35 const char* visiblePtr = w.c_str() + 3; 36 - const int prefixWidth = renderer.getTextAdvanceX(fontId, "\xe2\x80\x83"); 36 + const int prefixWidth = renderer.getTextAdvanceX(fontId, "\xe2\x80\x83", currentStyle); 37 37 const int visibleWidth = renderer.getTextWidth(fontId, visiblePtr, currentStyle); 38 38 startX = wordX + prefixWidth; 39 39 underlineWidth = visibleWidth;
+4 -4
lib/GfxRenderer/GfxRenderer.cpp
··· 722 722 return HalDisplay::DISPLAY_WIDTH; 723 723 } 724 724 725 - int GfxRenderer::getSpaceWidth(const int fontId) const { 725 + int GfxRenderer::getSpaceWidth(const int fontId, const EpdFontFamily::Style style) const { 726 726 const auto fontIt = fontMap.find(fontId); 727 727 if (fontIt == fontMap.end()) { 728 728 LOG_ERR("GFX", "Font %d not found", fontId); 729 729 return 0; 730 730 } 731 731 732 - return fontIt->second.getGlyph(' ', EpdFontFamily::REGULAR)->advanceX; 732 + return fontIt->second.getGlyph(' ', style)->advanceX; 733 733 } 734 734 735 - int GfxRenderer::getTextAdvanceX(const int fontId, const char* text) const { 735 + int GfxRenderer::getTextAdvanceX(const int fontId, const char* text, const EpdFontFamily::Style style) const { 736 736 const auto fontIt = fontMap.find(fontId); 737 737 if (fontIt == fontMap.end()) { 738 738 LOG_ERR("GFX", "Font %d not found", fontId); ··· 743 743 int width = 0; 744 744 const auto& font = fontIt->second; 745 745 while ((cp = utf8NextCodepoint(reinterpret_cast<const uint8_t**>(&text)))) { 746 - width += font.getGlyph(cp, EpdFontFamily::REGULAR)->advanceX; 746 + width += font.getGlyph(cp, style)->advanceX; 747 747 } 748 748 return width; 749 749 }
+2 -2
lib/GfxRenderer/GfxRenderer.h
··· 110 110 EpdFontFamily::Style style = EpdFontFamily::REGULAR) const; 111 111 void drawText(int fontId, int x, int y, const char* text, bool black = true, 112 112 EpdFontFamily::Style style = EpdFontFamily::REGULAR) const; 113 - int getSpaceWidth(int fontId) const; 114 - int getTextAdvanceX(int fontId, const char* text) const; 113 + int getSpaceWidth(int fontId, EpdFontFamily::Style style = EpdFontFamily::REGULAR) const; 114 + int getTextAdvanceX(int fontId, const char* text, EpdFontFamily::Style style) const; 115 115 int getFontAscenderSize(int fontId) const; 116 116 int getLineHeight(int fontId) const; 117 117 std::string truncatedText(int fontId, const char* text, int maxWidth,