typemytype / drawbot

http://www.drawbot.com
Other
401 stars 62 forks source link

pick a named instance of a variation font without installed it first #507

Open typemytype opened 1 year ago

typemytype commented 1 year ago

installFonts(...) is deprecated in favour of providing a font path... but for a variation font a script must be able to select a named instance, now the fontName is a path and not a PS font name.

I would like to propose something similar to fontNumber

font(path, namedInstance="myCool-Regular")

https://github.com/typemytype/drawbot/blob/master/drawBot/drawBotDrawingTools.py#L1312

this should raise an error when the current font is not a variation font and warn when the name instance is not available

justvanrossum commented 1 year ago

It could also be a variant of fontVariations(). In a way, named instances are just shortcuts for designspace locations.

justvanrossum commented 1 year ago
    def fontVariations(self, namedInstance=None, **axes):
        if namedInstance is not None:
            assert not axes
            ...select named instance...
        else:
            ...select location from axes...
typemytype commented 1 year ago

True, only those named instances have already their own listFontVariations and named instances are not listed there.

So to make it similar it could also be fontNamedInstance(name)

justvanrossum commented 1 year ago

fontNamedInstance(name) is fine with me, too.

We'd need to ensure this also works with installed fonts.

typemytype commented 1 year ago

we store the fontNameOrPath in self._font, see

if this is not a path then fontNamedInstance(Name) would just redirect to font(name)

justvanrossum commented 1 year ago

if this is not a path then fontNamedInstance(Name) would just redirect to font(name)

Hm, so this would "work"?:

font("Helvetica")
fontNamedInstance("A-Named-Instance-From-An-Unrelated-VF-That-Is-Installed")

Either we provide something that works transparently (and intuititely) for both installed and path-based fonts, or we need to make clear this funtionality is specific to path-based fonts. The latter is not something we currently have I think.

typemytype commented 1 year ago

It should test if the font has an instance with that name, otherwise raise or warn (redirect wasnt the smartest plan :) )

typemytype commented 1 year ago

oke, proposal:

from drawBot.misc import DrawBotError

def fontNamedInstance(name):
    instances = listNamedInstances()
    if name in instances:
        fontVariations(**instances[name])    
    else:
        raise DrawBotError(f"Can not find instance with name: '{name}'.")

font("Skia", 200)        
fontNamedInstance("Skia-Regular_Black-Extended")
fontNamedInstance("Skia-Regular_Light-Extended")

text("abc", (50, 50))