fix: keep y0/y1 in sync when clipping objects in clip_obj#1382
Open
otiscuilei wants to merge 1 commit into
Open
fix: keep y0/y1 in sync when clipping objects in clip_obj#1382otiscuilei wants to merge 1 commit into
otiscuilei wants to merge 1 commit into
Conversation
clip_obj (behind Page.crop / within_bbox / outside_bbox) rewrote an object's top-based coordinates (top, bottom, doctop, height) but left the PDF-space y0/y1 untouched. For an object clipped along the vertical axis this broke the top/bottom <-> y0/y1 invariant, so y1 - y0 no longer matched height and consumers reading y0/y1 off a cropped object got stale values. Update y0/y1 alongside top/bottom, mirroring move_object/resize_object: y1 tracks top, y0 tracks bottom. Adds a regression test.
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.
Who hits this
Anyone reading
y0/y1off an object that was clipped by.crop(...),.within_bbox(...), or.outside_bbox(...). Those fields are part of the public per-object schema (exported viato_dict,.objects, and the CSV/JSON output, documented as "distance from bottom of page"), so consumers that ready0/y1— or compute geometry/height from them — on a cropped page get silently wrong numbers. Text extraction is unaffected (it usestop/doctop/x0), so this is silent corruption of the coordinate fields rather than a crash.Root cause
clip_objinpdfplumber/utils/geometry.pyrewrites an object's top-based coordinates (x0,x1,top,bottom,doctop,width,height) but never updates the PDF-spacey0/y1. pdfplumber objects carry the same box in two coordinate systems with the invarianttop == page_bottom_ref - y1andbottom == page_bottom_ref - y0. After a vertical clip,top/bottomshift buty0/y1keep their pre-crop values, so the two representations describe different boxes andy1 - y0no longer equalsheight. The sibling helpersmove_objectandresize_objectboth already updatey0/y1when present; onlyclip_objwas missing it.The fix
After computing the clipped
top/bottom, also updatey0/y1when present, mirroringresize_object:y1trackstop,y0tracksbottom.Before / after
Cropping a page so
chars[0](top=117.18, bottom=135.18, y0=656.82, y1=674.82, height=18) is cut in half vertically:top=126.18,bottom=135.18,height=9.0, buty0=656.82,y1=674.82->y1-y0 = 18.0(stale);page.height - y1 = 117.18!=topy1=665.82,y0=656.82->y1-y0 = 9.0 = height, andpage.height - y1 = 126.18 = top,page.height - y0 = 135.18 = bottomNotes
test_crop_preserves_y0_y1intests/test_basics.pythat crops through a char vertically and assertsy1-y0 == heightand thetop/bottomtoy0/y1invariant. It fails on the current code and passes with this change.CHANGELOG.md(Unreleased / Fixed) and adds a contributor entry toREADME.md.make lintpasses (flake8, mypy, black, isort).