typemytype / drawbot

http://www.drawbot.com
Other
398 stars 61 forks source link

`fontSize(0)` renders default font size #483

Open roberto-arista opened 2 years ago

roberto-arista commented 2 years ago

hey all!

I am not sure if this is a bug or not, but I just discovered that this snippet:

fontSize(0)
text("hello", (100, 100))

does not leave the canvas blank. DrawBot draws "hello" with the default font size. Does this happen with intention or because zero is falsy?

Of course I can always wrap the code in an if statement and skip the text() function, but fontSize = 0 seems a logic – but maybe unorthodox – way to not draw some text on the canvas.

justvanrossum commented 2 years ago

It's the OS doing it:

from AppKit import NSFont

f = NSFont.fontWithName_size_("Helvetica", 0)
print(f)

Prints:

"Helvetica 12.00 pt. P [] (0x10faa7300) fobj=0x1002696f0, spc=3.33"
typemytype commented 2 years ago

see https://developer.apple.com/documentation/appkit/nsfont/1525977-fontwithname

If you use a fontSize of 0.0, this method uses the default User Font size.

justvanrossum commented 2 years ago

The fact that NSFont.fontWithName_Size_ behaves like this should no necessarily mean DrawBot should also behave like this. I don't like that this AppKit oddity trickles back into the DrawBot API, and I will for example not duplicate this behavior in drawbot-skia.

I tried for fun: NSFont.fontWithName_matrix_() can be passed a (0, 0, 0, 0, 0, 0) matrix, and it won't magically use 12 pt. So we could fix it. Although it's such an edge case that I'm inclined to just leave it as it is.

roberto-arista commented 2 years ago

I agree that it is an oddity, and definitely an edge case.

typemytype commented 2 years ago

this is funny:

import AppKit

s = 0  # change to a other number and it returns the same cached font object

f1= AppKit.NSFont.fontWithName_matrix_("Helvetica", (s, 0, 0, s, 0, 0))
print(f1)
print(f1.pointSize())

f2 = AppKit.NSFont.fontWithName_size_("Helvetica", s)
print(f2)
print(f2.pointSize())

print(f1 == f2)
justvanrossum commented 2 years ago

Yeah, there's clearly a special case for (0, 0, 0, 0, 0, 0).

typemytype commented 2 years ago

I guess there must be a postscript limitation or assumption

import AppKit

t = FormattedString()

s = 0

f1= AppKit.NSFont.fontWithName_matrix_("Helvetica", (s, 0, 0, s, 0, 0))
attr1 = AppKit.NSAttributedString.alloc().initWithString_attributes_("foo", {AppKit.NSFontAttributeName: f1, AppKit.NSForegroundColorAttributeName: AppKit.NSColor.blackColor()})
t.getNSObject().appendAttributedString_(attr1)

f2 = AppKit.NSFont.fontWithName_size_("Helvetica", s)
attr2 = AppKit.NSAttributedString.alloc().initWithString_attributes_("foo", {AppKit.NSFontAttributeName: f2, AppKit.NSForegroundColorAttributeName: AppKit.NSColor.blackColor()})
t.getNSObject().appendAttributedString_(attr2)

print(t)
text(t, (100, 100))

saveImage('zeroPointSize.pdf')

this renders wrongly in Acrobat, Illustrator and Preview...

image