fix(engines): correct annotation positions for PDFs with non-zero MediaBox#627
Open
marco-streng wants to merge 1 commit into
Open
Conversation
|
@marco-streng is attempting to deploy a commit to the OpenBook Team on Vercel. A member of the Team first needs to authorize it. |
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.
Summary
Closes: #625
Fix incorrect annotation positions for PDF files with a non-zero MediaBox origin (e.g., CAD/technical drawing exports from AutoCAD or Vectorworks). In such PDFs the MediaBox lower-left corner is not at
(0, 0)— a typical example is[-1685, -1192, 1685, 1192]. Because the previous coordinate conversion assumed the origin was always(0, 0), annotation icons (e.g., text/comment stamps) were rendered at the raw PDF coordinate in CSS space, placing them far outside the visible page area. Native PDF viewers display these annotations at the correct position.The fix reads each page's actual bounding-box origin at document open time via
FPDF_GetPageBoundingBox, stores it inPdfPageObject.origin, and applies it in both the PDF→CSS and CSS→PDF coordinate conversions.Changes
pdf.ts
origin?: Positionfield toPdfPageObjectto carry the MediaBox/CropBox lower-left corner in PDF user space.engine.ts
openDocumentBuffer: For each page, callsFPDF_LoadPage+FPDF_GetPageBoundingBoxto read the bounding-box origin (left,bottom) and stores it inpage.origin. Falls back to(0, 0)when the call fails or the PDF has no explicit origin.convertPagePointToDevicePoint: Subtractspage.originbefore converting PDF user-space coordinates to CSS device-space coordinates (device_x = PDF.x − ox,device_y = DH − (PDF.y − oy)).convertDevicePointToPagePoint: Addspage.originback when converting CSS device-space coordinates to PDF user space (inverse of the above), ensuring newly placed or moved annotations are saved at the correct position in the file.convertPageRectToDeviceRectbenefits automatically through delegation toconvertPagePointToDevicePoint— no direct change required.