Large font on micropython_ra8875

The official pyboard running MicroPython.
This is the reference design and main target board for MicroPython.
You can buy one at the store.
Target audience: Users with a pyboard.
Post Reply
Volvodriver
Posts: 5
Joined: Sat Jun 13, 2020 10:02 am

Large font on micropython_ra8875

Post by Volvodriver » Sun Apr 04, 2021 12:36 am

I'm working on a project where i'd like to use a large font on a 5 inch screen, i'm using micropython_ra8875 library and everything is working fine until i try and use a font larger than 35.

I'm using micropython-font-to-py to convert the font with commands similar to

Code: Select all

micropython-font-to-py-master/font_to_py.py -x OpenSans-Regular.ttf 36 font36.py
which has worked fine for sizes up to and including 35.

I'm looking at something like font 96 at least but i can't seem to find a way.

An alternative would be to essentially blow up the whole screen to essentially zoom it to a size that can be read from approximately 4m away.

Any help would be much appreciated.

User avatar
pythoncoder
Posts: 5148
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: Large font on micropython_ra8875

Post by pythoncoder » Sun Apr 04, 2021 12:25 pm

There is nothing in font_to_py.py to limit the maximum size. However I've never tried fonts of that size - as far as I know you're breaking new ground here.

The file size of the bitmapped Python font will get large unless you limit the character set. If you import the file without freezing it, RAM will be consumed. Freezing will fix this, but for very large font files perhaps flash size limits will come into play.

Is there any scope for limiting the character set? For example a digital clock might use "0123456789:".

Please explain what goes wrong when you try to use a large font.
Peter Hinch

Volvodriver
Posts: 5
Joined: Sat Jun 13, 2020 10:02 am

Re: Large font on micropython_ra8875

Post by Volvodriver » Mon Apr 05, 2021 9:41 am

Thanks Peter,

Sorry i hadn't tried doing it through Screen so all i knew is it wasn't loading. Having tried it through Screen and you're right, it's a memory issue when i did the import on something like 80pt.

I tried a lower number that wasn't working with 40pt and it imported ok but i got

Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "micropython_ra8875/py/ugui.py", line 75, in change
File "<stdin>", line 5, in __init__
File "<stdin>", line 2, in dynamiclabel
File "micropython_ra8875/widgets/label.py", line 23, in __init__
File "micropython_ra8875/py/ugui.py", line 246, in __init__
File "micropython_ra8875/driver/tft.py", line 45, in text_style
AttributeError: 'module' object has no attribute 'hmap'

I only need numbers, k and n so i tried reducing it down using fontTools and got the same error. With that same subset font i made 20pt version and everything loaded fine so it seems to just be the larger fonts. In all instances i've used the -x command while creating the .py file for the font.

User avatar
pythoncoder
Posts: 5148
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: Large font on micropython_ra8875

Post by pythoncoder » Mon Apr 05, 2021 9:51 am

That error either implies you are using an old version of font_to_py.py or is a symptom of out of memory. I have run

Code: Select all

font_to_py.py FreeSans.ttf 96 -x -c 1234567890 numbers.py
It produced a file of 42K in size (which includes the hmap() function). This is very large, and I think you'll need to use frozen bytecode to avoid out-of-memory errors.

The start of the font file is as follows:

Code: Select all

# Code generated by font_to_py.py.
# Font: FreeSans.ttf Char set: 0123456789
# Cmd: ./font_to_py.py FreeSans.ttf 96 -x -c 1234567890 numbers.py
version = '0.33'

def height():
    return 96

def baseline():
    return 93

def max_width():
    return 70

def hmap():
    return True

def reverse():
    return False

def monospaced():
    return False

def min_ch():
    return 48

def max_ch():
    return 63

_font =\
b'\x45\x00\x00\x00\x00\x00\xff\xf8\x00\x00\x00\x00\x00\x00\x1f\xff'\
Peter Hinch

User avatar
pythoncoder
Posts: 5148
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Large font on micropython_ra8875 WORKS!

Post by pythoncoder » Mon Apr 05, 2021 12:36 pm

Curiosity got the better of me and I had to try this. tl;dr A 96 pixel high font renders quite quickly.

My test rig for RA8875 uses a Pyboard D. I used the font described above, with the following test script. Each time you press "Go" a new random number is displayed in the huge font. Rendering is not instant but it is (in my opinion) acceptably quick. I did not need to use frozen bytecode, but your mileage may vary on a Pyboard 1.1.

Code: Select all

# bignum.py Test/demo of large font for Pyboard RA8875 GUI

# Released under the MIT License (MIT). See LICENSE.
# Copyright (c) 2021 Peter Hinch

from micropython_ra8875.py.ugui import Screen
from micropython_ra8875.py.colors import *

from micropython_ra8875.widgets.label import Label
from micropython_ra8875.widgets.buttons import Button

from micropython_ra8875.fonts import font14
from micropython_ra8875.fonts import numbers  # Experimental big font
from micropython_ra8875.driver.tft_local import setup
import urandom

class NumberScreen(Screen):
    def __init__(self):
        super().__init__()
        labels = { 'width' : 300,
                'fontcolor' : WHITE,
                'border' : 2,
                'fgcolor' : RED,
                'bgcolor' : (0, 40, 0),
                'font' : numbers,
                }

        self.lbl = Label((5, 5), **labels))

        self.btn_quit = Button((390, 240), font = font14, height = 30, width = 80,
                               fgcolor = RED, shape = RECTANGLE, text = 'Quit',
                               callback = self.quit)

        self.btn_go = Button((5, 240), font = font14, height = 30, width = 80,
                               fgcolor = RED, shape = RECTANGLE, text = 'Go',
                               callback = self.go)

    def quit(self, _):
        Screen.shutdown()

    def go(self, _):
        lbl.value(str(urandom.getrandbits(16)))


print('Testing TFT...')
setup()
Screen.change(NumberScreen)
Peter Hinch

User avatar
pythoncoder
Posts: 5148
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: Large font on micropython_ra8875

Post by pythoncoder » Mon Apr 05, 2021 1:01 pm

Here it is on a 5" display (Pyboard for size reference):

Image
Peter Hinch

wangshujun@tom.com
Posts: 25
Joined: Fri Feb 15, 2019 9:22 am

Re: Large font on micropython_ra8875

Post by wangshujun@tom.com » Sun Apr 18, 2021 2:12 pm

If you are using framebuf to implement the display driver, you can consider trying my framebuf enhancement

https://github.com/wangshujun-tj/mpy-Fr ... boost-code

Support 8 × 8, 6 × 12, 8 × 16, 12 × 24, 16 × 32 font size, and can also enlarge up to 4 times, fully meet the needs of large font, character display work in C code, speed compared with MPY implementation speed is greatly improved.

Volvodriver
Posts: 5
Joined: Sat Jun 13, 2020 10:02 am

Re: Large font on micropython_ra8875

Post by Volvodriver » Tue May 11, 2021 11:10 am

Thanks Peter, I've been run off my feet with other projects for a while so it took a while to test but that worked perfect.

I had used another tool to do the subset and then converted it with font_to_py.py which was probably the cause.

Thanks for your help
pythoncoder wrote:
Mon Apr 05, 2021 1:01 pm
Here it is on a 5" display (Pyboard for size reference):

Image

Jackli
Posts: 3
Joined: Thu Apr 29, 2021 9:11 am

Re: Large font on micropython_ra8875

Post by Jackli » Fri May 14, 2021 2:30 am

It's a coincidence that this post also helped me solve my problem!

Post Reply