-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Fix precision rounding issues in LineWrapper #1595
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
cb8603c
1c1d275
9ff7a7d
9b6e9ba
6158dae
df8c494
802014d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,8 +1,27 @@ | ||
| export function PDFNumber(n) { | ||
| const fArray = new Float32Array(1); | ||
| const uArray = new Uint32Array(fArray.buffer); | ||
|
|
||
| export function PDFNumber(n, roundUp = false) { | ||
|
||
| // PDF numbers are strictly 32bit | ||
| // so convert this number to the nearest 32bit number | ||
| // so convert this number to a 32bit number | ||
| // @see ISO 32000-1 Annex C.2 (real numbers) | ||
| return Math.fround(n); | ||
| const rounded = Math.fround(n); | ||
| if (roundUp) { | ||
| if (rounded >= n) return rounded; | ||
| } else if (rounded <= n) return rounded; | ||
|
|
||
| // Will have to perform 32bit float truncation | ||
| fArray[0] = n; | ||
|
|
||
| // Get the 32-bit representation as integer and shift bits | ||
| if (!(roundUp ^ (n > 0))) { | ||
| uArray[0] += 1; | ||
| } else { | ||
| uArray[0] -= 1; | ||
| } | ||
|
|
||
| // Return the float value | ||
| return fArray[0]; | ||
| } | ||
|
|
||
| /** | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should not be rounded only when storing the number to the pdf file?
wordWidth is called in many intermediate steps.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we need to ensure that during the line_wrapper computations we are keeping a float32 number so we could either wrap all instances of wordWidth with the rounder, or just do it here
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
or alternatively we wrap every comparison operation with
PDFNumberto ensure the rounding never affects itThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Lets take the current approach, adding tests to ensure correctness. Later we can try to minimize number of roundings