Skip to content

Write rectangles with the precision of real numbers - #385

Open
AlexanderV wants to merge 1 commit into
empira:masterfrom
AlexanderV:upstream/rectangle-precision
Open

Write rectangles with the precision of real numbers#385
AlexanderV wants to merge 1 commit into
empira:masterfrom
AlexanderV:upstream/rectangle-precision

Conversation

@AlexanderV

Copy link
Copy Markdown

PdfWriter.Write(PdfRectangle) rounds the coordinates to three decimal places, while Write(double) and Write(PdfReal) use seven:

const string format = Config.SignificantDecimalPlaces3;

Writing a document therefore changes its geometry. The /MediaBox of an ISO A4 page created by another producer becomes 595.276 instead of 595.2756.

For a signed document that is a changed page: adding a second signature to a document that already contains one makes validators report the pages of the first revision as changed.

With seven decimal places /MediaBox, /CropBox, /BleedBox, /TrimBox, /ArtBox, /Rect and /BBox round-trip unchanged.

Why not convert to a single first, as Write(double) does? That would defeat the purpose: the single nearest to 595.2756 is 595.27557, which does not round-trip either.

Alternative you may prefer: writing the value that was parsed, as PRESERVE_PARSED_VALUES already does for numbers. That preserves every number of a file and not only rectangles, but it needs the parsed value to survive until the document is written, which is not the case for rectangles today. Happy to go that way instead if you consider it the better fix.

Test (IO/WriterTests.cs): a page with a /MediaBox of four decimal places is written and read back; the test checks both the bytes in the file and the values after reading.

Verified on top of current master: full PdfSharp.Tests suite green (253 passed, 0 failed).

This PR is independent of my other PRs; each of them applies to master on its own.

Write(PdfRectangle) rounded the coordinates to three decimal places, while
Write(double) and Write(PdfReal) use seven. Writing a document therefore
changed its geometry, e.g. the /MediaBox of an ISO A4 page created by another
producer became 595.276 instead of 595.2756.

That is a change of the page for every digital signature the document already
contains: adding a second signature to a signed document made validators
report the pages of the first revision as changed.

/MediaBox, /CropBox, /BleedBox, /TrimBox, /ArtBox, /Rect and /BBox now
round-trip unchanged.

Note that the coordinates are written as they are, while Write(double)
converts a real number to a single first. Doing that for rectangles as well
would defeat the purpose, because the single nearest to 595.2756 is 595.27557,
which does not round-trip either.

An alternative would be to write the value that was parsed, as
PRESERVE_PARSED_VALUES does for numbers. That preserves every number of a file
and not only rectangles, but it needs the parsed value to survive until the
document is written, which is not the case for rectangles today.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants