draw_rect: does not respect width if color is not specified, and possible doc issue
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")

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.
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:
- I didn't see
widthunder thosecommonparms - I didn't find a link to those commonparms except from
finishandinsert_text, which are unlikely to be found by newb's like me trying to understanddraw_rect.
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?
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.
Next version will correct this:
Setting color=None will suppress any border being drawn, default is color=0.
Fixed in 1.21.0