drawbot icon indicating copy to clipboard operation
drawbot copied to clipboard

`fontSize(0)` renders default font size

Open roberto-arista opened this issue 3 years ago • 7 comments

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.

roberto-arista avatar Sep 07 '22 09:09 roberto-arista

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"

justvanrossum avatar Sep 07 '22 09:09 justvanrossum

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.

typemytype avatar Sep 07 '22 10:09 typemytype

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.

justvanrossum avatar Sep 07 '22 10:09 justvanrossum

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

roberto-arista avatar Sep 07 '22 10:09 roberto-arista

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)

typemytype avatar Sep 07 '22 11:09 typemytype

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

justvanrossum avatar Sep 07 '22 11:09 justvanrossum

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

typemytype avatar Sep 07 '22 12:09 typemytype