Update to font_to_py for tiny fonts

Discuss development of drivers for external hardware and components, such as LCD screens, sensors, motor drivers, etc.
Target audience: Users and developers of drivers.
Post Reply
User avatar
pythoncoder
Posts: 5956
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Update to font_to_py for tiny fonts

Post by pythoncoder » Sat Feb 06, 2021 2:47 pm

Thanks to ideas from @enigmaniac the font_to_py utility now supports bdf and pcf files.

The original concept of this utility was a means of converting scalable ttf and otf files to bitmaps in a format providing fast access with minimal RAM consumption. This works well at relatively large sizes. As the output size reduces programmatic scaling becomes visually poorer. The best tiny fonts are hand-crafted for a single, fixed size and distributed as bitmaps; these may be fixed or variable pitch.

Various bitmapped file formats exist, but bdf and pcf files are widely available. The utility now accepts these formats. Data is converted to Python source, preserving the font bitmap as the designer intended. The Python font format is unchanged, enabling these fonts to be used interchangeably with ones created from scalable formats.
Peter Hinch
Index to my micropython libraries.

User avatar
bouffski
Posts: 6
Joined: Tue Feb 23, 2021 7:54 pm

Re: Update to font_to_py for tiny fonts

Post by bouffski » Thu Apr 08, 2021 5:06 pm

@pythoncoder
Hi Peter!
I have been trying to get new fonts by your 'font_to_py' program for the ili9341 driver, in MicroPython for this nifty pico controller.
(python3 font_to_py.py /home/pi/.fonts/LiquidCrystal-Normal.otf 28 --fixed mefont.py -c 1234567890:) This for the build-in RTC.
No success at all with this for me cryptic error message in Thonny:
Traceback (most recent call last):
File "<stdin>", line 66, in <module>
File "/lib/ili934xnew.py", line 318, in print
AttributeError: 'module' object has no attribute 'get_width'
There is no inspiration left what to do about this...
Can you help me out? Do you need more information?
Me is just an old but enthusiast newbie, especially to (Micro)Python ...
Cheers,
Hubert, aka Bouffski
Nostalgia isn't what it used to be...

User avatar
pythoncoder
Posts: 5956
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: Update to font_to_py for tiny fonts

Post by pythoncoder » Fri Apr 09, 2021 1:12 pm

I have no idea what /lib/ili934xnew.py is. My ILI9341 driver is this one. Python fonts produced by font_to_py.py are compatible with my nano_gui, Writer and CWriter classes, using my drivers.

The specification for the files is documented, so other authors may choose to support them. Or they may use other formats. In the absence of knowledge of the driver you're using, it's impossible for me to comment.
Peter Hinch
Index to my micropython libraries.

User avatar
bouffski
Posts: 6
Joined: Tue Feb 23, 2021 7:54 pm

Re: Update to font_to_py for tiny fonts

Post by bouffski » Fri Apr 09, 2021 3:29 pm

@pythoncoder
Thank you for your prompt reply. I wasn't aware of two different libraries. My bad. The library I used was: https://github.com/jeffmer/micropython- ... 934xnew.py. I also saw/found your 'font_to_py' there for the first time...
I will have a look into your library and accompanying progs.
Cheers
Nostalgia isn't what it used to be...

User avatar
bouffski
Posts: 6
Joined: Tue Feb 23, 2021 7:54 pm

Re: Update to font_to_py for tiny fonts

Post by bouffski » Sat Apr 10, 2021 2:29 pm

@pythoncoder
Thanks again: by your answer I got it all working now!
Nostalgia isn't what it used to be...

User avatar
pythoncoder
Posts: 5956
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Incompatible font files

Post by pythoncoder » Sun Apr 11, 2021 9:27 am

I've had a look at that driver. It works in a different way from mine. In some ways it is more efficient: my drivers inherit from framebuf which has a cost in RAM. One upside of my approach is compatibility with nano-gui. The second is that the display objects inherit the framebuf graphics primitives.

He claims compatibility with my fonts. This is incorrect. The one font he provides (glcdfont.py) has strong similarities to mine, but as you discovered his code assumes that the font has a get_width() method which returns the length of a string. A quick look on GitHub indicates my fonts never had this. The library contains a modified font_to_py.py which produces font files containing this method. It is based on ancient code: font_to_py.py has had numerous improvements in the last three years. I can see no merit in putting get_width() in the font file. In my view it belongs in the display driver.

[EDIT]I've just spotted that he's documented this:
additional fonts can be generated by a very slightly modified version of Peter Hinch's font-to-py program
I have raised this issue.
Peter Hinch
Index to my micropython libraries.

Post Reply