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

Text alignment is broken for redact annotations (>=1.18.17) #1286

Closed
caerulescens opened this issue Sep 24, 2021 · 11 comments
Closed

Text alignment is broken for redact annotations (>=1.18.17) #1286

caerulescens opened this issue Sep 24, 2021 · 11 comments
Assignees
Labels
bug postpone postpone to a future version

Comments

@caerulescens
Copy link

caerulescens commented Sep 24, 2021

Describe the bug (mandatory)

The align argument does not work as it should. Text within the textbox is always aligned left.

To Reproduce (mandatory)

  • Install PyMuPDF>=1.18.17
  • Run below
import fitz

doc = fitz.Document()
doc.new_page()
page = doc[0]
x0, y0 = (100, 100)
x1, y1 = (250, 150)
rect = fitz.Rect(x0, y0, x1, y1)
page.add_redact_annot(rect,
                      text='text',
                      fontname="helv",
                      fontsize=12,
                      align=1,
                      text_color=(1, 1, 1),
                      fill=(0, 0, 0))
page.apply_redactions()
doc.save('./out.pdf')

Expected behavior (optional):

See here. In the code above, since align=1, the text should be centered in the redact annotation.

Screenshots (optional)

Actual:
incorrect_align

Expected:
correct_align

Your configuration (mandatory)

  • Operating system: debian 10
  • Python version: 3.9.1
  • PyMuPDF version: 1.18.16 is the most recent release without this issue; I confirmed the issue for 1.18.17, 1.18.18, and 1.18.19.
@JorjMcKie
Copy link
Collaborator

Accepted - typo.
Will be corrected in next version.

@caerulescens
Copy link
Author

caerulescens commented Sep 24, 2021

Thanks

@JorjMcKie JorjMcKie added the postpone postpone to a future version label Oct 1, 2021
@caerulescens
Copy link
Author

caerulescens commented Oct 14, 2021

@JorjMcKie This is fixed for align=0, 1, 2 in 1.19.0. Using the same script, see screenshots below.

align = 0, expected left ✔️

Screenshot from 2021-10-14 10-42-30

align = 1, expected center ✔️

Screenshot from 2021-10-14 10-43-14

align = 2, expected right ✔️

Screenshot from 2021-10-14 10-43-54

@caerulescens
Copy link
Author

caerulescens commented Oct 14, 2021

align=3 is working correctly too!

align = 0, expected left ✔️

Screenshot from 2021-10-14 10-53-32

align = 3, expected justified ✔️

Screenshot from 2021-10-14 10-54-21

@JorjMcKie
Copy link
Collaborator

Thank you very much for testing and confirming all this so thoroughly!

@caerulescens
Copy link
Author

caerulescens commented Oct 14, 2021

Interesting, it seems the periods at the end of "words" weren't considered during the justification. Is that expected? If you use a textbox instead of a redact annotation and draw a rectangle with outline, you can see it. See screenshot below.

align=3

Screenshot from 2021-10-14 11-11-02

Also, should the text be vertically centered, similar to the redact annotation? I'm guessing no.

@JorjMcKie
Copy link
Collaborator

Also, should the text be vertically centered, similar to the redact annotation? I'm guessing no.

No. I am actually using a an unpleasent code to achieve something close to vertical centering in redact annots. It always is just an approximation there.

Strictly speaking, align=3 is not supported by PDF, only values 0, 1, 2 are:
grafik
So my internal spec /Q 3 is illegal anyway and may be ignored or otherwise handled in an unexpected way by other software.
I think I will remove this value and restrict to the official range.

Interesting though what happened to the dots - maybe internally changed to the Python Ellipsis. I will look into it.

@JorjMcKie
Copy link
Collaborator

Justified alignment has an error in insert_textbox, I can reproduce it independent from redactions.
So that must be fixed no matter what.

@JorjMcKie
Copy link
Collaborator

Justified alignment has an error in insert_textbox, I can reproduce it independent from redactions. So that must be fixed no matter what.

Found and fixed that error. As I said, this was caused by an error in insert_textbox() directly:
To support justification, I compute the word lengths within a line and then adjust the length of the spaces in that same line such that the sum of all lengths equal line length.
This PDF parameter, called Tw ("set word spacing") must be explicitely set to 0 for the last line (which must not be justified ever). This did not happen. No idea why this was undetected until today.
So instead of this:
grafik
one should now see this:
grafik

@JorjMcKie
Copy link
Collaborator

Resolved in new version 1.19.0 which is currently being uploaded.

@cajmorgan
Copy link

Hello! This bug is re-introduced in the newer version

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

No branches or pull requests

3 participants