fix: Correct word width and space calculations#963
fix: Correct word width and space calculations#963daveallie merged 2 commits intocrosspoint-reader:masterfrom
Conversation
📝 WalkthroughWalkthroughThis PR updates the graphics renderer's text measurement methods to respect font styles by adding a style parameter to Changes
Estimated code review effort🎯 2 (Simple) | ⏱️ ~10 minutes 🚥 Pre-merge checks | ✅ 2✅ Passed checks (2 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. Comment |
There was a problem hiding this comment.
Caution
Some comments are outside the diff and can’t be posted inline due to platform limitations.
⚠️ Outside diff range comments (1)
lib/GfxRenderer/GfxRenderer.cpp (1)
711-730:⚠️ Potential issue | 🔴 CriticalAdd null checks before dereferencing glyph pointers.
Lines 717 and 729 dereference
getGlyph()without null checks. WhileEpdFontFamily::getFont()correctly masks the UNDERLINE bit and falls back to a valid font variant, the underlyingEpdFont::getGlyph()can still returnnullptrfor codepoints not in the font data (see EpdFont.cpp line 83). This creates a crash path when a codepoint is missing from all available font variants.Add null checks or fallback to a replacement glyph (e.g., space width or a default value) before dereferencing, similar to the pattern in
EpdFont::getTextBounds()which handles missing glyphs gracefully.🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@lib/GfxRenderer/GfxRenderer.cpp` around lines 711 - 730, GfxRenderer::getSpaceWidth and GfxRenderer::getTextAdvanceX currently dereference the result of fontMap.at(fontId).getGlyph(...) without checking for nullptr; change both to check the returned glyph pointer and, if null, use a safe fallback (e.g., use the space glyph advanceX if available or a default 0 width) before accessing ->advanceX; ensure getTextAdvanceX also handles missing glyphs when iterating codepoints (using utf8NextCodepoint) and sums the fallback width to avoid crashes for missing codepoints, mirroring the graceful handling used in EpdFont::getTextBounds().
📜 Review details
Configuration used: Organization UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (4)
lib/Epub/Epub/ParsedText.cpplib/Epub/Epub/blocks/TextBlock.cpplib/GfxRenderer/GfxRenderer.cpplib/GfxRenderer/GfxRenderer.h
🧰 Additional context used
🧬 Code graph analysis (1)
lib/GfxRenderer/GfxRenderer.h (2)
lib/GfxRenderer/GfxRenderer.cpp (4)
getSpaceWidth(711-718)getSpaceWidth(711-711)getTextAdvanceX(720-732)getTextAdvanceX(720-720)lib/EpdFont/EpdFontFamily.h (1)
EpdFontFamily(4-24)
🔇 Additional comments (3)
lib/GfxRenderer/GfxRenderer.h (1)
106-107: Style-aware spacing API looks good.Exposing style on the width/advance APIs aligns with the rest of the text methods and avoids hardcoded REGULAR lookups.
lib/Epub/Epub/blocks/TextBlock.cpp (1)
35-37: Advance-width prefix measurement is a good fix.Using
getTextAdvanceXfor the em-space prefix should keep underline alignment consistent across styles.lib/Epub/Epub/ParsedText.cpp (1)
32-53: Advance-width measurement for word layout looks correct.This should prevent italic overhangs from inflating inter-word spacing, and the soft-hyphen stripping keeps measurements aligned with rendering.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Outside diff comments:
In `@lib/GfxRenderer/GfxRenderer.cpp`:
- Around line 711-730: GfxRenderer::getSpaceWidth and
GfxRenderer::getTextAdvanceX currently dereference the result of
fontMap.at(fontId).getGlyph(...) without checking for nullptr; change both to
check the returned glyph pointer and, if null, use a safe fallback (e.g., use
the space glyph advanceX if available or a default 0 width) before accessing
->advanceX; ensure getTextAdvanceX also handles missing glyphs when iterating
codepoints (using utf8NextCodepoint) and sums the fallback width to avoid
crashes for missing codepoints, mirroring the graceful handling used in
EpdFont::getTextBounds().
# Conflicts: # lib/GfxRenderer/GfxRenderer.cpp
|
@znelson I think that's a valid fix, what was the reason behind closing it? |
|
@osteotek I think it is correct, too. I had another bug in my working branch and for a moment there I thought it was caused by this change. |
## 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**_
## 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**_
## 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**_
## 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**_
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:
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