FreeType generator & MicroPython FontDrawer
Project contains several items:
- FreeType Generator : a Python 3 FreeType Font generator creating font files that are compatible with MicroPython ressources.
- FontDrawer : a MicroPython library loading font file and draw font-based text onto graphical display (via FrameBuffer manipulation).
FreeType Generator
The generator project will load .ttf files and generates font files under (.py and .bin) format.
The .bin files could be copied to the MicroPython board and then be used with MicroPython code to draw font-based text.
The .py file are intermediate file that can be used for inspection and better understanding of the the font internals. This file can be loaded into a Python 3 as well as a MicroPython session. This file format imply a big parsing overhead under MicroPython, the raison why we now use binary format!
See the Files section here under for more details about the font file organization, their compilation to python and binary format.
FontDrawer
The font drawer is a MicroPython code that uses a .bin font file to draw characters inside the MicroPython FrameBuffer of a screen.
The binary .bin format is quicly loaded inside the microcontroler memory with minimal impact on the micropython parser!
The fdrawer.py
library and desired font (.bin files, EG: vera.m15.bin
) must be copied to the MicroPython board.
The following test_basic.py
example shows how to draw a character and a string.
from machine import I2C
from SSD1306 import SSD1306_I2C
from fdrawer import FontDrawer
i2c = I2C( 1, freq=2000000 )
lcd = SSD1306_I2C( width=128, height=64, i2c=i2c, addr=0x3c, external_vcc=True )
# Normal FrameBuffer operation
lcd.rect( 0, 0, 128, 64, 1 )
lcd.show()
# Use a font drawer to draw font to FrameBuffer
fd = FontDrawer( frame_buffer=lcd, font_name = 'vera_m15' )
fd.print_str( "Font Demo", 2, 2 )
fd.print_char( "#", 100, 2 )
fd.print_str( fd.font_name, 2, 18 )
# Send the FrameBuffer content to the LCD
lcd.show()
Which produce the following results:
Note:
- the "m" in front of "m15" means that the .bin file only contains a part of the character set.
- An uncoded character is replaced by a squared rectangle (like the space in this example)
Other examples and dedicated README are availables in the micropython (Font-Drawer) subfolder
Files
- ttf-fonts/ : This folder contains source font that will be transformed
- upy-fonts/ : This folder contains the microPython font file generated (*.py & *.bin files). Binary file format is described in the tech_info.md .
- generate-all-font.sh : Use the
ft-generate.py
to generates all the ILI9341 insideili-fonts/
from FreeType font available inttf-font/
- ft-generator.py : script with arguments generating ILI-driver fonts (see the .sh file for sample, please read the ili-fonts.md for more information on applicable naming conventions).
- ft-view.py : debugging tool. Allow to view a character bitmap encoding (or the whole font set). calculate the max size (height,width) of the font. The file
ft-view-for-vera.txt
shows the generated content... which is very interesting to learn. - ft-view-encoded.py : same as ft-view.py but also showing the encoded data (useful for bug tracking)
Somes of the fonts availables in the upy-fonts folder:
arrow_15
arrow_23
dejav_m8
dejav_m10
dejav_m12
dejav_m15
dejav_m20
etypo_13
etypo_23
fserif_m10
fserif_m12
fserif_m15
fserif_m20
heyd_23
pitch_15
pitch_23
robotl_m8
robotl_m10
robotl_m12
robotl_m15
robotl_m20
vera_10
vera_23
veram_15 (VeraMono)
veram_23 (VeraMono)
Install instructions
The font generator "ft-generator.py" is a Python 3 script with some dependencies. You will need to install the dependencies prior to use them.
pip3 install freetype-py
pip3 install docopt
FreeType and library installation on non Linux machines
FreeType library should be available aout-of-the box on most of the Linux systems (As Linux Mint, Ubuntu and Debian).
For windows, you will certainly run into a '''dll nightmare'''. Finding the good DLL version properly compiled for win32/win64 was a lonnnnnnggggg path to go.
Here some path to find the good Windows DLL. Better use Linux!
*** DLL Nightmare on Windows ***
http://www.freetype.org/
Win32 installation can be found here
http://gnuwin32.sourceforge.net/packages/freetype.htm
Resources
Technical details
- tech_info.md - describing the binary format for font.
FreeType
DocOpt - argument parser
La plus belle manière de parser les arguments de script en python
- docopt.org
- La plus belle maniere de parser les arguments de script en python sur Sam-et-Max (attention, gros écarts de langage).
About font
- A Crash Course in Typography: The Basics of Type noupe.com
- Font Kerning Wikipedia
- Glyph Metrics Freetype.org
Roman Podgaiski ILI9341 driver
This project was initially written to support the ILI9341 driver from Roman Podgaiski. Later the project were improved to be used with any FrameBuffer based screen display.
The ILI9341 driver is developed by Roman Podgaiski (ropod7) and is available here at https://github.com/ropod7/pyboard_drive
Some French technical information and wiring are available at MCHobby http://wiki.mchobby.be/index.php?title=MicroPython-ILI9341