PyMuPDF icon indicating copy to clipboard operation
PyMuPDF copied to clipboard

draw_rect: does not respect width if color is not specified, and possible doc issue

Open cbm755 opened this issue 3 years ago • 4 comments

from fitz import *
doc = fitz.open()
pg = doc.new_page()

pg.draw_rect(Rect(100, 100, 190, 200), color=(1,0,0), width=5)
pg.draw_rect(Rect(200, 100, 290, 200), color=(0,0,0), width=5)
pg.draw_rect(Rect(300, 100, 390, 200), width=5)

doc.save("foo.pdf")

image

The last box has the default width, same as if the width kwarg was not specified.


But perhaps it is supposed to be this way: not wanting to embarrass myself I went to RTFM:

draw_rect(rect, color=None, fill=None, width=1, dashes=None, lineCap=0, lineJoin=0, overlay=True, morph=None, stroke_opacity=1, fill_opacity=1, oc=0)

PDF only: Draw a rectangle. See Shape.draw_rect().

So I clicked on the Shape.draw_rect link but the arguments were not documented there either. I guess I didn't try that hard but within 2 or 3 mins I was not able to find documentation about how width and color are supposed to interact.

cbm755 avatar Sep 09 '22 22:09 cbm755

I found this:

https://pymupdf.readthedocs.io/en/latest/shape.html#commonparms

Reading between the lines, I can guess what is happening from:

If letting default a color parameter to None, then no resp. color selection command will be generated. If fill and color are both None, then the drawing will contain no color specification. But it will still be “stroked”, which causes PDF’s default color “black” be used by Adobe Acrobat and all other viewers.

Notes:

  1. I didn't see width under those commonparms
  2. I didn't find a link to those commonparms except from finish and insert_text, which are unlikely to be found by newb's like me trying to understand draw_rect.

cbm755 avatar Sep 09 '22 23:09 cbm755

Hmmm, if I don't specify color then each PDF viewer gets to make a possibly different decision about what width to use. Is that so?

cbm755 avatar Sep 09 '22 23:09 cbm755

Hmmm, if I don't specify color then each PDF viewer gets to make a possibly different decision about what width to use. Is that so?

It might be so, but it would nonetheless be a bug: the default color by the books is black.

JorjMcKie avatar Sep 10 '22 04:09 JorjMcKie

Next version will correct this: Setting color=None will suppress any border being drawn, default is color=0.

JorjMcKie avatar Sep 10 '22 05:09 JorjMcKie

Fixed in 1.21.0