Welcome!
This is the community forum for my apps Pythonista and Editorial.
For individual support questions, you can also send an email. If you have a very short question or just want to say hello — I'm @olemoritz on Twitter.
Load a custom symbol font from within running script?
-
Is there a way to load a custom font for use in a custom view inside of a script?
To be clear, I don't want to load a font for the user to use in general, in this use case the font has symbols I want to be able to use, and it's a convenient way to store vector icon images. I don't see an obvious way to load a font from my local script area...wondering if this is a sandbox security thing?Slightly off-topic: as of iOS 13 Apple is supplying a special system standard symbols "font" which UIImages will be able to load individual icons from, which is basically the same idea as what I want to do here.
-
-
I saw that post in my search...that's almost literally the opposite of what I want.
What I absolutely don't want is to load this font from an external source, or to have any user interaction involved.
I'd like to simply have the font .ttf file located in my script's directory and then load it for use at run time.I'm sensing that's not possible, due to some security implications?
-
PIL includes ImageFont.truetype which let's you load a file. You then have to use ImageDraw to create an PIL image, which can then be dumped to BytesIO -> ui.Image.from_data.
As an example, see:
https://github.com/tdamdouni/Pythonista/blob/master/raspberrypi/infodisplay.py
I'm not 100% sure I pythonista's ImageFont let's you load ttf from file -- I think it does -- but if not, you could convert your font to the PIL native font format.
-
ImageFont.truetype(file, size)
Note In Pythonista, the truetype() function can be used to load built-in system fonts, e.g. font = ImageFont.truetype('Helvetica', 20).
-
-
Example
Downloaded tiny.tff from herefrom PIL import Image, ImageFont, ImageDraw font = ImageFont.truetype('tiny.ttf',20) img = Image.new('RGBA', (100,100), "white") draw = ImageDraw.Draw(img) draw.text((2,2), 'test', font=font, fill='blue') img.show()
-
If you install the font via AnyFont it becomes system wide available.
Then you can just load it directly, without the need for a .ttf to be loaded. -
The original question was about not installing system fonts. Obviously you cant use an ImageFont in a textfield.