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

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

Closed
cbm755 opened this issue Sep 9, 2022 · 5 comments
Assignees
Labels
bug resolved fixed / implemented / answered

Comments

@cbm755
Copy link
Contributor

cbm755 commented Sep 9, 2022

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
Copy link
Contributor Author

cbm755 commented Sep 9, 2022

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
Copy link
Contributor Author

cbm755 commented Sep 9, 2022

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?

@JorjMcKie
Copy link
Collaborator

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
Copy link
Collaborator

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

julian-smith-artifex-com pushed a commit to ArtifexSoftware/PyMuPDF-julian that referenced this issue Oct 13, 2022
subset_fonts(): added `verbose` arg.
julian-smith-artifex-com pushed a commit to ArtifexSoftware/PyMuPDF-julian that referenced this issue Oct 13, 2022
…1913, pymupdf#1909

Changes to: jm_checkquad(), trace_lineto(), trace_close(), jm_trace_text_span().
julian-smith-artifex-com pushed a commit to ArtifexSoftware/PyMuPDF-julian that referenced this issue Oct 13, 2022
subset_fonts(): added `verbose` arg.
julian-smith-artifex-com pushed a commit to ArtifexSoftware/PyMuPDF-julian that referenced this issue Oct 13, 2022
…1913, pymupdf#1909

Changes to: jm_checkquad(), trace_lineto(), trace_close(), jm_trace_text_span().
julian-smith-artifex-com pushed a commit to ArtifexSoftware/PyMuPDF-julian that referenced this issue Oct 18, 2022
Changing the default value of "color" parameter from None to (0,) causes color
"black" if letting default it.

New verbose parameter in Document,subset_fonts suppresses messages by default.
julian-smith-artifex-com pushed a commit to ArtifexSoftware/PyMuPDF-julian that referenced this issue Oct 18, 2022
…1913, pymupdf#1909

Pertain to 2 issues:

    False detection of rectangles, leading to too many triples of consecutive
    "line" commands being converted to "rect" items in the returned
    dictionary. We now only accept exactly 3 consecutive "line" commannds
    followed by "close path" to be a "rect candidate. This can no longer ever
    happen within function "jm_checkquad()".

    Calculating the font size in function "jm_trace_text_span" was incorrect
    and leading to wrong results if properties "a" and "d" of the text matrix
    (scaling factors in matrix "trm") are different.
    Changed to sqrt(a * d) - the geometric mean of the two.
julian-smith-artifex-com pushed a commit to ArtifexSoftware/PyMuPDF-julian that referenced this issue Oct 19, 2022
Changing the default value of "color" parameter from None to (0,) causes color
"black" if letting default it.

New verbose parameter in Document.subset_fonts() suppresses messages by default.
julian-smith-artifex-com pushed a commit to ArtifexSoftware/PyMuPDF-julian that referenced this issue Oct 19, 2022
Changing the default value of "color" parameter from None to (0,) causes color
"black" if letting default it.

New verbose parameter in Document.subset_fonts() suppresses messages by default.
julian-smith-artifex-com pushed a commit that referenced this issue Oct 19, 2022
Changing the default value of "color" parameter from None to (0,) causes color
"black" if letting default it.

New verbose parameter in Document.subset_fonts() suppresses messages by default.
@julian-smith-artifex-com julian-smith-artifex-com added resolved fixed / implemented / answered and removed Fixed in next release labels Nov 8, 2022
@julian-smith-artifex-com
Copy link
Collaborator

Fixed in 1.21.0

cbm755 added a commit to plomgrading/plom that referenced this issue Dec 9, 2022
Width alone does not change width [1], although from looking at our
other code it looks like we were already aware of this.

[1] pymupdf/PyMuPDF#1913
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug resolved fixed / implemented / answered
Projects
None yet
Development

No branches or pull requests

3 participants