-
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
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
cb8603c
Fix further LineWrapper precision issues
hollandjake 1c1d275
add test of bounded text precision issue
hollandjake 9ff7a7d
add rowSpanning table example
hollandjake 9b6e9ba
add failure threshold
hollandjake 6158dae
implement toContainText jest matcher
blikblum df8c494
create a unit test for bounded text precision
blikblum 802014d
remove round up rounding code path
blikblum File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,7 @@ | ||
import matcher from './toContainChunk'; | ||
import toContainChunk from './toContainChunk'; | ||
import toContainText from './toContainText'; | ||
import { toMatchImageSnapshot } from 'jest-image-snapshot'; | ||
|
||
expect.extend(matcher); | ||
expect.extend(toContainChunk); | ||
expect.extend(toContainText); | ||
expect.extend({ toMatchImageSnapshot }); |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
import { getObjects, parseTextStream } from '../helpers.js'; | ||
|
||
/** | ||
* @import { TextStream, PDFDataObject } from '../helpers.js'; | ||
* @import JestMatchedUtils from 'jest-matcher-utils'; | ||
*/ | ||
|
||
/** | ||
* @param {JestMatchedUtils} utils | ||
* @param {TextStream} argument | ||
* @return {string} | ||
*/ | ||
const passMessage = (utils, argument) => () => { | ||
return ( | ||
utils.matcherHint('.not.toContainText', 'data', 'textStream') + | ||
'\n\n' + | ||
`Expected data not to contain text:\n\n${utils.printExpected(argument)}` | ||
); | ||
}; | ||
|
||
/** | ||
* @param {JestMatchedUtils} utils | ||
* @param {TextStream[]} received | ||
* @param {TextStream} argument | ||
* @return {string} | ||
*/ | ||
const failMessage = (utils, received, argument) => () => { | ||
return ( | ||
utils.matcherHint('.toContainText', 'data', 'textStream') + | ||
'\n\n' + | ||
`Expected data to contain text:\n\n${utils.printExpected(argument)}\n\nFound:\n\n${utils.printReceived(received)}` | ||
); | ||
}; | ||
|
||
function textStreamMatches(expected, actual) { | ||
if (expected.text !== actual.text) { | ||
return false; | ||
} | ||
|
||
if (expected.font && expected.font !== actual.font) { | ||
return false; | ||
} | ||
|
||
if (expected.fontSize && expected.fontSize !== actual.fontSize) { | ||
return false; | ||
} | ||
|
||
return true; | ||
} | ||
|
||
/** | ||
* @param {PDFDataObject} object | ||
* @return {TextStream | undefined} | ||
*/ | ||
function getTextStream(object) { | ||
// text stream objects have 4 items | ||
// first item is a string containing the Length of the stream | ||
// second item 'stream' | ||
// third item is the stream content Buffer | ||
// fourth item is 'endstream' | ||
|
||
if (object.items.length !== 4) { | ||
return; | ||
} | ||
if (typeof object.items[0] !== 'string') { | ||
return; | ||
} | ||
if (object.items[1] !== 'stream') { | ||
return; | ||
} | ||
if (!(object.items[2] instanceof Buffer)) { | ||
return; | ||
} | ||
if (!/endstream/.test(object.items[3])) { | ||
return; | ||
} | ||
|
||
return parseTextStream(object.items[2]); | ||
} | ||
|
||
export default { | ||
/** | ||
* | ||
* @param {(string | Buffer)[]} data | ||
* @param {Partial<TextStream>} textStream | ||
* @returns | ||
*/ | ||
toContainText(data, textStream) { | ||
const objects = getObjects(data); | ||
const foundTextStreams = []; | ||
let pass = false; | ||
|
||
for (const object of objects) { | ||
const objectTextStream = getTextStream(object, textStream); | ||
if (!objectTextStream) { | ||
continue; | ||
} | ||
foundTextStreams.push(objectTextStream); | ||
if (textStreamMatches(textStream, objectTextStream)) { | ||
pass = true; | ||
break; | ||
} | ||
} | ||
|
||
if (pass) { | ||
return { | ||
pass: true, | ||
message: passMessage(this.utils, textStream), | ||
}; | ||
} | ||
|
||
return { | ||
pass: false, | ||
message: failMessage(this.utils, foundTextStreams, textStream), | ||
}; | ||
}, | ||
}; |
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
Binary file added
BIN
+67.1 KB
tests/visual/__image_snapshots__/table-spec-js-table-multi-page-table-1-snap.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+16.8 KB
tests/visual/__image_snapshots__/table-spec-js-table-multi-page-table-2-snap.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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
Oops, something went wrong.
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.
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
PDFNumber
to 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