Wrote a CLI tool to help create Fonts for daisy

Hi,

inspired by this thread by @JMC64 (and feeling a need to have this myself) I wrote a small CLI tool that converts PixelForge font files to fitting C-code that can be inclided in libDaisy’s oled_fonts.c.

The tool is written in python and has no external dependencies. I released it under the MIT license and would be happy if it was included with the helper-scripts for the project. You can see more details here: daisyfontconverter · PyPI

In short, you run:

daisyfontconverter --input pat/to/my_font.pxf --width 4 --height g

and it outputs something like this (in this case a self designed 4x6 font):

static const uint16_t Font4x6[] = {
    0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, // Space
    0x0000, 0x4000, 0x4000, 0x4000, 0x0000, 0x4000, // !
    0x0000, 0xA000, 0xA000, 0x0000, 0x0000, 0x0000, // "
    0x0000, 0x4000, 0xE000, 0x4000, 0xE000, 0x4000, // #
    0x0000, 0x6000, 0xC000, 0x4000, 0x6000, 0xC000, // $
    0x0000, 0xA000, 0x2000, 0x4000, 0x8000, 0xA000, // %
    0x0000, 0x4000, 0xA000, 0x4000, 0xA000, 0x6000, // &
    0x0000, 0x4000, 0x4000, 0x0000, 0x0000, 0x0000, // '
    0x0000, 0x4000, 0x8000, 0x8000, 0x8000, 0x4000, // (
    0x0000, 0x4000, 0x2000, 0x2000, 0x2000, 0x4000, // )
    0x0000, 0x4000, 0xE000, 0xA000, 0x0000, 0x0000, // *
    0x0000, 0x0000, 0x4000, 0xE000, 0x4000, 0x0000, // +
    0x0000, 0x0000, 0x0000, 0x0000, 0x4000, 0xC000, // ,
    0x0000, 0x0000, 0x0000, 0xE000, 0x0000, 0x0000, // -
    0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x4000, // .
    0x0000, 0x2000, 0x2000, 0x4000, 0x8000, 0x8000, // /
    0x0000, 0xE000, 0xA000, 0xA000, 0xA000, 0xE000, // 0
    0x0000, 0x4000, 0xC000, 0x4000, 0x4000, 0xE000, // 1
    0x0000, 0xE000, 0x2000, 0xE000, 0x8000, 0xE000, // 2
    0x0000, 0xE000, 0x2000, 0xC000, 0x2000, 0xE000, // 3
    0x0000, 0xA000, 0xA000, 0xE000, 0x2000, 0x2000, // 4
    0x0000, 0xE000, 0x8000, 0xC000, 0x2000, 0xC000, // 5
    0x0000, 0xC000, 0x8000, 0xE000, 0xA000, 0xE000, // 6
    0x0000, 0xE000, 0x2000, 0x4000, 0x8000, 0x8000, // 7
    0x0000, 0x4000, 0xA000, 0x4000, 0xA000, 0xE000, // 8
    0x0000, 0xE000, 0xA000, 0xE000, 0x2000, 0xE000, // 9
    0x0000, 0x0000, 0x4000, 0x0000, 0x4000, 0x0000, // :
    0x0000, 0x0000, 0x4000, 0x0000, 0x4000, 0x8000, // ;
    0x0000, 0x2000, 0x4000, 0x8000, 0x4000, 0x2000, // <
    0x0000, 0x0000, 0xE000, 0x0000, 0xE000, 0x0000, // =
    0x0000, 0x8000, 0x4000, 0x2000, 0x4000, 0x8000, // >
    0x0000, 0xC000, 0x2000, 0x4000, 0x0000, 0x4000, // ?
    0x0000, 0xC000, 0x2000, 0x6000, 0xA000, 0xC000, // @
    0x0000, 0x4000, 0xA000, 0xA000, 0xE000, 0xA000, // A
    0x0000, 0xC000, 0xA000, 0xC000, 0xA000, 0xE000, // B
    0x0000, 0x6000, 0x8000, 0x8000, 0x8000, 0xE000, // C
    0x0000, 0xC000, 0xA000, 0xA000, 0xA000, 0xC000, // D
    0x0000, 0xE000, 0x8000, 0xC000, 0x8000, 0xE000, // E
    0x0000, 0xE000, 0x8000, 0xE000, 0x8000, 0x8000, // F
    0x0000, 0x6000, 0x8000, 0xA000, 0xA000, 0x6000, // G
    0x0000, 0xA000, 0xA000, 0xE000, 0xA000, 0xA000, // H
    0x0000, 0xE000, 0x4000, 0x4000, 0x4000, 0xE000, // I
    0x0000, 0xC000, 0x4000, 0x4000, 0x4000, 0x8000, // J
    0x0000, 0xA000, 0xA000, 0xC000, 0xA000, 0xA000, // K
    0x0000, 0x8000, 0x8000, 0x8000, 0x8000, 0xE000, // L
    0x0000, 0xA000, 0xE000, 0xE000, 0xA000, 0xA000, // M
    0x0000, 0xE000, 0xA000, 0xA000, 0xA000, 0xA000, // N
    0x0000, 0x4000, 0xA000, 0xA000, 0xA000, 0x4000, // O
    0x0000, 0xC000, 0xA000, 0xA000, 0xC000, 0x8000, // P
    0x0000, 0x4000, 0xA000, 0xA000, 0xE000, 0x6000, // Q
    0x0000, 0xC000, 0xA000, 0xC000, 0xA000, 0xA000, // R
    0x0000, 0x6000, 0x8000, 0xE000, 0x2000, 0xC000, // S
    0x0000, 0xE000, 0x4000, 0x4000, 0x4000, 0x4000, // T
    0x0000, 0xA000, 0xA000, 0xA000, 0xA000, 0x6000, // U
    0x0000, 0xA000, 0xA000, 0xA000, 0xA000, 0x4000, // V
    0x0000, 0xA000, 0xA000, 0xA000, 0xE000, 0xE000, // W
    0x0000, 0xA000, 0xA000, 0x4000, 0xA000, 0xA000, // X
    0x0000, 0xA000, 0xA000, 0xA000, 0x4000, 0x4000, // Y
    0x0000, 0xE000, 0x2000, 0x4000, 0x8000, 0xE000, // Z
    0x0000, 0xC000, 0x8000, 0x8000, 0x8000, 0xC000, // [
    0x0000, 0x8000, 0x8000, 0x4000, 0x2000, 0x2000, // backslash
    0x0000, 0x6000, 0x2000, 0x2000, 0x2000, 0x6000, // ]
    0x0000, 0x4000, 0xA000, 0x0000, 0x0000, 0x0000, // ^
    0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0xE000, // _ underscore
    0x0000, 0x4000, 0x2000, 0x0000, 0x0000, 0x0000, // ` backtick
    0x0000, 0xE000, 0x2000, 0x6000, 0xA000, 0xE000, // a
    0x0000, 0x8000, 0x8000, 0xC000, 0xA000, 0xC000, // b
    0x0000, 0x0000, 0x6000, 0x8000, 0x8000, 0x6000, // c
    0x0000, 0x2000, 0x2000, 0x6000, 0xA000, 0x6000, // d
    0x0000, 0x4000, 0xA000, 0xE000, 0x8000, 0x6000, // e
    0x4000, 0x8000, 0xC000, 0x8000, 0x8000, 0x8000, // f
    0x0000, 0x0000, 0x6000, 0xA000, 0x6000, 0xE000, // g
    0x0000, 0x8000, 0x8000, 0xC000, 0xA000, 0xA000, // h
    0x0000, 0x4000, 0x0000, 0x4000, 0x4000, 0x4000, // i
    0x0000, 0x4000, 0x0000, 0x4000, 0x4000, 0x8000, // j
    0x0000, 0x8000, 0x8000, 0xA000, 0xC000, 0xA000, // k
    0x0000, 0xC000, 0x4000, 0x4000, 0x4000, 0x4000, // l
    0x0000, 0x0000, 0x0000, 0xC000, 0xE000, 0xE000, // m
    0x0000, 0x0000, 0x0000, 0xC000, 0xA000, 0xA000, // n
    0x0000, 0x0000, 0x4000, 0xA000, 0xA000, 0x4000, // o
    0x0000, 0x0000, 0xC000, 0xA000, 0xC000, 0x8000, // p
    0x0000, 0x0000, 0x6000, 0xA000, 0x6000, 0x2000, // q
    0x0000, 0x0000, 0x6000, 0x8000, 0x8000, 0x8000, // r
    0x0000, 0x0000, 0x6000, 0x8000, 0x2000, 0xC000, // s
    0x0000, 0x4000, 0xE000, 0x4000, 0x4000, 0x2000, // t
    0x0000, 0x0000, 0x0000, 0xA000, 0xA000, 0x6000, // u
    0x0000, 0x0000, 0x0000, 0xA000, 0xA000, 0x4000, // v
    0x0000, 0x0000, 0x0000, 0xE000, 0xE000, 0xC000, // w
    0x0000, 0x0000, 0x0000, 0xA000, 0x4000, 0xA000, // x
    0x0000, 0x0000, 0xA000, 0xE000, 0x2000, 0x6000, // y
    0x0000, 0x0000, 0xE000, 0x2000, 0x4000, 0xE000, // z
    0x0000, 0x6000, 0x4000, 0xC000, 0x4000, 0x6000, // {
    0x0000, 0x4000, 0x4000, 0x4000, 0x4000, 0x4000, // |
    0x0000, 0xC000, 0x4000, 0x6000, 0x4000, 0xC000, // }
    0x0000, 0x0000, 0x2000, 0xE000, 0x8000, 0x0000, // ~ Tilde
};

FontDef Font_4x6 = {4, 6, Font4x6};

This goes into oled_fonts.c.

Additionally you should put this into oled_fonts.h:

extern FontDef Font_4x6;

May it help someone.

4 Likes

Awesome work! and nice that it’s an easy pip install away :slight_smile:

1 Like

Four new fonts are in Pull Request #619, this may also solve the issue @JMC64 had.

Fonts added:

4x6
4x8
5x8
6x7

(I hate lowercase m and w characters)

This is awesome.

How hard would be to write something that takes a 128x64 B/W jpeg or gif and converts it to an array?

1 Like

Not to hard at all, but I guess such code exists somewhere already (at least in the adafruit world somewhere, these displays are quite popular). The “hardest” part is probably reading the jpg (there are libraries for that) and aesthetically deciding on how to convert the image to 1 bit colors, which dither to use etc. There is this awesome resource on dithering for those who don’t know it yet.

1 Like

Thank you so much for putting this tool together and opening a PR!!
We’ll review as soon as we get a chance :slight_smile:

1 Like