tinygo-org / tinyfont

Text library for TinyGo displays
https://tinygo.org
BSD 3-Clause "New" or "Revised" License
49 stars 12 forks source link

Put the font data in the const area #14

Closed sago35 closed 1 year ago

sago35 commented 4 years ago

The current dev branch code has font data in RAM. This makes it very RAM intensive.

This issue shows how to put the font data in const, which will be done in the future. It will also allow you to put it in spi flash afterwards.

I plan to put out a PR once I get my code together. The PR will replace all existing fonts.

before:

var NotoEmojiRegular12pt = tinyfont.Font{
    Glyphs: []tinyfont.Glyph{
        /*  */ tinyfont.Glyph{Rune: 0x0, Width: 0x0, Height: 0x0, XAdvance: 0x10, XOffset: 0, YOffset: 0, Bitmaps: []uint8{}},
        /*  */ tinyfont.Glyph{Rune: 0xd, Width: 0x0, Height: 0x0, XAdvance: 0x10, XOffset: 0, YOffset: 0, Bitmaps: []uint8{}},
        /*   */ tinyfont.Glyph{Rune: 0x20, Width: 0x0, Height: 0x0, XAdvance: 0x10, XOffset: 0, YOffset: 0, Bitmaps: []uint8{}},
        /* # */ tinyfont.Glyph{Rune: 0x23, Width: 0xa, Height: 0xc, XAdvance: 0x10, XOffset: 3, YOffset: -11, Bitmaps: []uint8{0x19, 0x86, 0x41, 0x13, 0xff, 0x13, 0xc, 0xc3, 0x23, 0xff, 0x22, 0x9, 0x82, 0x61, 0x90}},
        /* 0 */ tinyfont.Glyph{Rune: 0x30, Width: 0x8, Height: 0xc, XAdvance: 0x10, XOffset: 4, YOffset: -10, Bitmaps: []uint8{0x3c, 0x66, 0xc2, 0xc3, 0xc3, 0xc3, 0xc3, 0xc3, 0xc3, 0xc3, 0x66, 0x3c}},
        /* 1 */ tinyfont.Glyph{Rune: 0x31, Width: 0x4, Height: 0xc, XAdvance: 0x10, XOffset: 6, YOffset: -11, Bitmaps: []uint8{0x37, 0x91, 0x11, 0x11, 0x11, 0x11}},
        /* 2 */ tinyfont.Glyph{Rune: 0x32, Width: 0x8, Height: 0xc, XAdvance: 0x10, XOffset: 4, YOffset: -11, Bitmaps: []uint8{0x7c, 0xc6, 0x6, 0x6, 0x6, 0x4, 0xc, 0x18, 0x30, 0x60, 0xc0, 0xff}},
        /* 3 */ tinyfont.Glyph{Rune: 0x33, Width: 0x8, Height: 0xc, XAdvance: 0x10, XOffset: 4, YOffset: -10, Bitmaps: []uint8{0x7c, 0xc6, 0x3, 0x2, 0x6, 0x38, 0x6, 0x3, 0x3, 0x3, 0x86, 0xfc}},
        // ...

after:

const cMplusConst10pt = "" +
    /*  */ "\x00\x11" + "\x00\x00\x00\x00" + "\x05\x0B" + "\x05\x00" + "\xF7" + "\x00" + "\x03\x9C\xE7\x39\xCE\x73\x80" +
    /*  */ "\x00\x11" + "\x00\x00\x00\x01" + "\x05\x0B" + "\x05\x00" + "\xF7" + "\x00" + "\x00\x00\x06\x79\x80\x00\x00" +
    /*  */ "\x00\x11" + "\x00\x00\x00\x02" + "\x05\x0B" + "\x05\x00" + "\xF7" + "\x00" + "\x02\xAA\xAA\xAA\xAA\xAA\xAA" +
    /*  */ "\x00\x11" + "\x00\x00\x00\x03" + "\x05\x0B" + "\x05\x00" + "\xF7" + "\x00" + "\x04\xBD\x29\x03\xE4\x21\x00" +
    /*  */ "\x00\x11" + "\x00\x00\x00\x04" + "\x05\x0B" + "\x05\x00" + "\xF7" + "\x00" + "\x07\xA1\xE8\x03\xD0\xF4\x00" +
    // ...
timboldt commented 4 years ago

The egregious use of RAM for fonts was the primary reason I abandoned TinyGo for my e-paper clock. I had plenty of flash, but almost no RAM. The Arduino libraries solved this problem (although I was forced to switch to C++).

sago35 commented 4 years ago

Hi @timboldt

Sometimes LLVM will store data in ROM without using a const font, But even in that case, the build time is very long, as described in #22.

The method described in this issue has the following advantages:

  1. The build time will be shorter.
  2. RAM is no longer used.

I'll try to make a PR somewhere this week.

timboldt commented 4 years ago

This looks like the perfect answer to the main issues I had with fonts: long build times, and excess RAM usage.

sago35 commented 1 year ago

For example, fonts created with tinyfontgen-ttf are created in const. So I believe that we have completed the support.