Repository navigation
BUG: Count Tc and Tw in the advance used for space detection - #4227
Merged
Merged
Conversation
Character and word spacing were ignored, so text shown with them got a synthetic space, e.g. "WO RD" instead of "WORD".
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #4227 +/- ##
=======================================
Coverage 99.25% 99.26%
=======================================
Files 59 59
Lines 12425 12438 +13
Branches 2368 2371 +3
=======================================
+ Hits 12333 12346 +13
Misses 38 38
Partials 54 54 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
stefan6419846
requested changes
Oct 20, 2026
Horizontal scaling now applies to the whole advance, as defined in the PDF specification. Character and word spacing are counted per character code instead of per decoded character, so two-byte codes forming a surrogate pair are no longer merged, and word spacing applies to the single-byte code 32 in multi-byte encodings like Shift-JIS. Also use meaningful test IDs.
stefan6419846
approved these changes
Nov 20, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
extract_text()inserts spaces inside words when a PDF uses word or character spacing.With the PDF from the issue,
WORD NAMEis extracted asWO RD NAME.Cause
A space is inserted when the next text starts more than half a space after the
calculated end of the previous text. That calculation ignored
Tc(no handlerexisted) and
Tw(stored inspace_scale, but never used).In the issue's PDF, a space is shown with
2.664 Tw. Those 2.664 pt were notcounted, so the gap before the following
Tdlooked like 2.68 pt, which is abovethe 1.5 pt threshold.
Changes
Tcand store the actualTwvalue. Both are saved and restored withq/Q.get_text_operandsalso returns the number of glyphs and of single-byte code 32 characters.Tc * glyphs + Tw * spacesto the width used for the space check.Twonly applies to the single-byte code 32,so composite fonts do not get word spacing.
This fixes the cause instead of raising the threshold (the
1.05factor suggestedin #4067), so the existing threshold stays unchanged.
Tests
test_character_and_word_spacing_count_towards_the_advancecoveringTw,Tc,and a real gap that has to keep its space. The first two cases fail without the fix.
get_text_operands.Closes #3219