Skip to content
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

Page.get_drawings() return a Rect object with swapped x0, x1 #1234

Closed
rednam-ntn opened this issue Aug 27, 2021 · 3 comments
Closed

Page.get_drawings() return a Rect object with swapped x0, x1 #1234

rednam-ntn opened this issue Aug 27, 2021 · 3 comments
Assignees
Labels

Comments

@rednam-ntn
Copy link

rednam-ntn commented Aug 27, 2021

Describe

I have a PDF file.
When open with fitz, get page 0 and using Page.get_drawings() will return a result that have an item
('re', Rect(289.44879150390625, 149.60101318359375, 285.1968078613281, 153.85302734375))
with x0 and x1 had been swapped.

So, when I draw_rect() that item in another fitz.Document, blank page, new Rect will be drawn in wrong position at 289.44879150390625, 149.60101318359375, 289.44879150390625 + width, 149.60101318359375 + height

To Reproduce

I have written a short snippet with attached PDF file in this Google Colab Notebook

Please feel free to re-run it.

Expected behavior

the Page.get_drawings() method will return a Rect item in correct order of x0, x1
('re', Rect(285.1968078613281, 149.60101318359375, 289.44879150390625, 153.85302734375))

Screenshots

Screen Shot 2021-08-27 at 18 19 29

Your configuration (mandatory)

Test on:

  • My machine
    • macOS 11.5.2 (20G95)
    • Python 3.6.9
    • pip install PyMuPDF==1.18.17
  • Google Colab
    • Ubuntu 18.04.5
    • Python 3.7.11
    • pip install PyMuPDF==1.18.17
@JorjMcKie
Copy link
Collaborator

Thanks for submitting this. I will have a look.
In the meantime, you can fix this via executing rect.normalize() I think (will confirm this in testing). This makes sure the rectangle is finite.

@JorjMcKie
Copy link
Collaborator

JorjMcKie commented Aug 27, 2021

Confirmed. If you do this, you should be ok.

    for item in path["items"]:  # these are the draw commands
        if item[0] == "l":  # line
            shape.draw_line(item[1], item[2])
        elif item[0] == "re":  # rectangle
            shape.draw_rect(item[1].normalize())  # <==
        elif item[0] == "qu":  # <== quad
            shape.draw_quad(item[1])
        elif item[0] == "c":  # curve
            shape.draw_bezier(item[1], item[2], item[3], item[4])
        else:
            raise ValueError("unhandled drawing", item)

Also please note to incorporate a few changes in shape.finish() - necessary since v1.18.17:

    shape.finish(
        fill=path["fill"],  # fill color
        color=path["color"],  # line color
        dashes=path["dashes"],  # line dashing
        even_odd=path.get("even_odd", True),  # control color of overlaps
        closePath=path["closePath"],  # whether to connect last and first point
        lineJoin=path["lineJoin"],  # how line joins should look like
        lineCap=max(path["lineCap"]),  # how line ends should look like
        width=path["width"],  # line width
        stroke_opacity=path.get("stroke_opacity", 1),  # key may not always be present
        fill_opacity=path.get("fill_opacity", 1),  # key may not be present
    )

@JorjMcKie
Copy link
Collaborator

new version 1.18.18 now on PyPI

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants