All Projects → peterhinch → Micropython Font To Py

peterhinch / Micropython Font To Py

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

Programming Languages

python
139335 projects - #7 most used programming language
micropython
64 projects

Labels

Projects that are alternatives of or similar to Micropython Font To Py

Linearicons
Linearicons is the highest quality set of line icons, matching with minimalist UI designs in iOS.
Stars: ✭ 64 (-54.61%)
Mutual labels:  fonts
Google Fonts Module
Google Fonts module for NuxtJS
Stars: ✭ 98 (-30.5%)
Mutual labels:  fonts
Fontmatrix
Free font collections manager for Linux, Mac and Windows
Stars: ✭ 133 (-5.67%)
Mutual labels:  fonts
Maker.js
📐⚙ 2D vector line drawing and shape modeling for CNC and laser cutters.
Stars: ✭ 1,185 (+740.43%)
Mutual labels:  fonts
Gatsby Plugin Google Fonts
Bring Google Fonts to Gatsby https://www.gatsbyjs.org/
Stars: ✭ 84 (-40.43%)
Mutual labels:  fonts
Free Fonts
Free fonts available under the Apache license
Stars: ✭ 110 (-21.99%)
Mutual labels:  fonts
Allsorts Tools
Example tools for the Allsorts font parser, shaping engine, and subsetter
Stars: ✭ 55 (-60.99%)
Mutual labels:  fonts
Linux Dotfiles
I configure lots of things, sorting them out here
Stars: ✭ 137 (-2.84%)
Mutual labels:  fonts
Work Sans
A grotesque sans.
Stars: ✭ 1,319 (+835.46%)
Mutual labels:  fonts
Gftools
Misc tools for working with the Google Fonts library
Stars: ✭ 132 (-6.38%)
Mutual labels:  fonts
Piazzolla
Piazzolla type family
Stars: ✭ 77 (-45.39%)
Mutual labels:  fonts
Gfonts
🔤 Offline Google Fonts for rmarkdown and shiny
Stars: ✭ 85 (-39.72%)
Mutual labels:  fonts
Notocjk
NotoSansCJK & NotoSerifCJK full weight patch for Android devices.
Stars: ✭ 116 (-17.73%)
Mutual labels:  fonts
Codefont
60余种常用的等宽字体 愉快的code吧 欢迎issues我 收录更多字体 Happy code bar with more than 60 common fonts of equal width. Welcome issues. I include more fonts.
Stars: ✭ 68 (-51.77%)
Mutual labels:  fonts
Google Sans Fonts
Stars: ✭ 136 (-3.55%)
Mutual labels:  fonts
Swifticonfont
Icons fonts for iOS (Font Awesome 5, Iconic, Ionicon, Octicon, Themify, MapIcon, MaterialIcon, Foundation 3, Elegant Icon, Captain Icon)
Stars: ✭ 1,094 (+675.89%)
Mutual labels:  fonts
Ttfunk
Font Metrics Parser for Prawn
Stars: ✭ 108 (-23.4%)
Mutual labels:  fonts
San Francisco Family
All the fonts in San Francisco family font, developed by Apple. Including new SF Camera font from iOS 13!
Stars: ✭ 141 (+0%)
Mutual labels:  fonts
Figma Linux Font Helper
Font Helper for Figma for Linux x64 platform
Stars: ✭ 136 (-3.55%)
Mutual labels:  fonts
Opentype Svg Font Editor
A user-friendly tool for adding SVG to OpenType fonts
Stars: ✭ 126 (-10.64%)
Mutual labels:  fonts

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 three components, links to docs below:

  1. font_to_py.py This utility runs on a PC and converts a 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. 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.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].