typemytype / drawbot

http://www.drawbot.com
Other
408 stars 63 forks source link

clickable-area for inline links not correct #426

Open stenson opened 3 years ago

stenson commented 3 years ago

Was playing around with adding some inline links in running text (using drawbot in module mode and the url property on FormattedString sub-strings) & ran into an issue with the bounding boxes not advancing along the x-axis (as in the first image below — the black boxes are just debug code added in pdfContext.py, to show where the clickable area is).

I repurposed some code from the textBoxCharacterBounds source and got it working correctly (or at least it appears correct to me) on a fork: https://github.com/typemytype/drawbot/compare/master...stenson:master

Current drawbot/master image

With change in url-linking code: https://github.com/typemytype/drawbot/compare/master...stenson:master image

Happy to open a pull request if the change seems good, or discuss further if the original behavior is intentional and I've misunderstood!

typemytype commented 3 years ago

Oh cool, please make a PR

Just one question: why using the typographic bounds over the image bounds? As far as I understands it the image bounds wrap the letters: so if the word does not contain ascenders or descenders the bounds are not unnecessary big

Thanks!

typemytype commented 3 years ago

Im trying to reproduce your issue in the current DrawBot... This seems to work fine where "world" is always the clickable area:

t = FormattedString()
t.fontSize(20)

for i in range(20):
    t += "hello "
    t.url("http://www.drawbot.com")
    t.fill(0, 0, 1)
    t += "world "
    t.fill(0)
    t.url(None)
    t += "hello "

textBox(t, (50, 50, 400, 400))

could you make a narrowed example?

thanks

stenson commented 3 years ago

Interesting! That example you provided does work for me in the app itself, both in the in-app view, and in a pdf saved from the app itself. But when using drawbot as a module (in a python3.7 virtualenv with the latest drawbot master), I get the same result as reported initially, where all the clickable areas aren't being advanced on the x-axis. Here's the pdf generated by this slightly modified version of the code:

test_original.pdf

from drawBot import *

t = FormattedString()
t.fontSize(20)

for i in range(20):
    t += "hello "
    t.url("http://www.drawbot.com")
    t.fill(0, 0, 1)
    t += "world "
    t.fill(0)
    t.url(None)
    t += "hello "

textBox(t, (50, 50, 400, 400))
saveImage("~/Desktop/test_original.pdf")

(To answer the previous question, I was just using the typographic bounds b/c that's what the source code for textBoxCharacterBounds uses)