• Stars
    star
    370
  • Rank 115,405 (Top 3 %)
  • Language
    Python
  • License
    MIT License
  • Created about 8 years ago
  • Updated 6 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A Python 3 utility to convert fonts to Python source capable of being frozen as bytecode

MicroPython font handling

This repository defines a method of creating and deploying fonts for use with MicroPython display drivers. A PC utility renders industry standard font files as a bitmap in the form of Python sourcecode. A MicroPython module enables such files to be displayed on devices with suitable device drivers. These include OLED displays using the SSD1306 chip and the official device driver. Compatible drivers for a variety of display technologies are available as part of the nano-gui repository.

1. Introduction

MicroPython platforms generally have limited RAM, but more abundant storage in the form of flash memory. Font files tend to be relatively large. The conventional technique of rendering strings to a device involves loading the entire font into RAM. This is fast but RAM intensive. The alternative of storing the font as a random access file and loading individual glyphs into RAM on demand is too slow for reasonable performance on most display devices.

This alternative implements a font as a Python source file, with the data being declared as bytes objects. Such a file may be frozen as bytecode: this involves building the firmware from source with the Python file in a specific directory. On import very little RAM is used, yet the data may be accessed fast. Note that the use of frozen bytecode is entirely optional: font files may be imported in the normal way if RAM usage is not an issue.

The resultant file is usable with two varieties of display device drivers:

  1. Drivers where the display class is subclassed from the official framebuffer class.
  2. Drivers for displays where the frame buffer is implemented in the display device hardware.

2. Solution

This comprises four components, links to docs below:

  1. font_to_py.py This utility runs on a PC and converts an industry standard font file to Python source. See below.
  2. Writer and CWriter classes These facilitate rendering text to a monochrome or colour display having a suitable device driver.
  3. Creating icon fonts Ways to incorporate icons in a Python font file.
  4. Device driver notes. Notes for authors of display device drivers. Provides details of the font file format and information on ensuring comptibility with the Writer classes.

3. font_to_py.py

This command line utility is written in Python 3 and runs on a PC. To convert a scalable font to Python the utility takes input a font file in ttf or otf form together with a height in pixels and outputs a Python source file containing the font as a bitmap. Fixed and variable pitch rendering are supported. The design has the following aims:

  • Independence of specific display hardware.
  • The path from font file to Python code to be fully open source.

The first is achieved by supplying hardware specific arguments to the utility. These define horizontal or vertical mapping and the bit order for font data.

The second is achieved by using Freetype and the Freetype Python bindings. Its use is documented here. This also details measurements of RAM usage when importing fonts stored as frozen bytecode.

3.1 Small fonts

Converting scalable ttf or otf files programmatically works best for larger fonts. For small fonts it is best to use hand-designed bitmapped font files. These are now supported: bdf or pcf font files may be converted to Python source in the same format as files originating from scalable fonts.

3.2 Limitations

Kerning is not supported. Fonts are one bit per pixel. Colour displays are supported by the CWriter class which adds colour information at the rendering stage. This assumes that all pixels of a character are coloured identically.

By default the font_to_py.py utility produces the ASCII character set from chr(32) to chr(126) inclusive. Command line options enable the character set to be modified to include arbitrary Unicode characters. Alternative sets may be specified such as for non-English languages. Efficient support is now provided for sparse character sets.

4. Font file interface

A font file is imported in the usual way e.g. import font14. Python font files contain the following functions. These return values defined by the arguments which were provided to font_to_py.py:

height Returns height in pixels.
max_width Returns maximum width of a glyph in pixels.
baseline Offset from top of glyph to the baseline.
hmap Returns True if font is horizontally mapped.
reverse Returns True if bit reversal was specified.
monospaced Returns True if monospaced rendering was specified.
min_ch Returns the ordinal value of the lowest character in the file.
max_ch Returns the ordinal value of the highest character in the file.

Glyphs are returned with the get_ch function. Its argument is a Unicode character and it returns the following values:

  • A memoryview object containing the glyph bytes.
  • The height in pixels.
  • The character width in pixels.

The font_to_py.py utility allows a default glyph to be specified (typically ?). If called with an undefined character, this glyph will be returned.

The min_ch and max_ch functions are mainly relevant to contiguous character sets.

5. Licence

All code is released under the MIT licence.

More Repositories

1

micropython-async

Application of uasyncio to hardware interfaces. Tutorial and code.
Python
714
star
2

micropython-mqtt

A 'resilient' asynchronous MQTT driver. Recovers from WiFi and broker outages.
Python
555
star
3

micropython-nano-gui

A lightweight MicroPython GUI library for display drivers based on framebuf class
Python
459
star
4

micropython-samples

Assorted code ideas, unofficial MP FAQ, plus index to my other repositories.
Python
443
star
5

micropython_ir

Nonblocking device drivers to receive from IR remotes and for IR "blaster" apps.
Python
239
star
6

micropython-micro-gui

A lightweight MicroPython GUI library for display drivers based on framebuf, allows input via pushbuttons. See also micropython-touch.
Python
237
star
7

micropython-iot

An approach to designing IOT applications using ESP8266, ESP32 or Pyboard D endpoints
Python
86
star
8

micropython-tft-gui

Simple GUI for Pyboard and TFT touch panel displays
Python
85
star
9

micropython-fourier

Fast Fourier transform in MicroPython's inline ARM assembler.
Python
76
star
10

micropython_eeprom

MicroPython device drivers for memory chips (EEPROM, FRAM, Flash, SPIRAM)
Python
70
star
11

micropython-filters

Digital filters impemented in MicroPython's inline ARM Thumb assembler (e.g. Pyboard, RP2).
Python
64
star
12

micropython_remote

Capture and replay 433MHz remote control codes. Control remote switched power adaptors.
Python
64
star
13

micropython-epaper

Driver for 2.7 inch Adafruit and Embedded Artists e-paper displays
Python
60
star
14

micropython-radio

Simple way to use an nRF24L01 radio to exchange arbitrary Python objects between two Pyboards
Python
52
star
15

micropython-micropower

Support for building ultra low power systems based on the Pyboard (1.x and D series).
Python
44
star
16

micropython-lcd160cr-gui

Touch GUI for the official MicroPython LCD display
Python
28
star
17

micropython-monitor

Display the behaviour of a realtime program with a scope or logic analyser.
Python
28
star
18

micropython-msgpack

MessagePack serialisation library optimised for MicroPython
Python
26
star
19

micropython_data_to_py

A Python3 utility to convert an arbitrary binary file to Python source for storage in Flash
Python
26
star
20

micropython-vs1053

Synchronous and asynchronous drivers for VS1053b MP3/FLAC player
Python
25
star
21

micropython-touch

A GUI for touch panel displays.
Python
18
star
22

micropython-amg88xx

Driver for Grid-EYE thermal infra red array sensor (Adafruit 3538)
Python
18
star
23

micropython_ra8875

MicroPython device driver and nano-GUI for RA8875 based displays
Python
9
star