Cahnge Font Size in Python

  • Is there a way to change the text size via microPython? or all the embedded fonts are fixed size without the option to make them smaller or bigger?

    I could not find any reference in the M5Cloud API:

  • To my understanding the fonts supported by micropython are bitmap fonts and are thus unscalable. However there are bigger and smaller sizes of some of the fonts on the M5 for instance the dejavu font has 12pt, 18pt and 24pt.

    You can set the font by calling the command

    micropython only supports .fon files an otherwise outdated font format. I managed to find a handful of other .fon fonts and put them in the fonts folder in the m5's flash. However thats not enough to use those fonts, they need to be setup in whichever script relates to the font, and I have not found it yet, no mention in the lcd module of the fonts.

    I will keep trying to add more fonts and if I have any progress I will report my findings here

  • Have you tried:
    lcd.compileFont(file_name [,debug])

  • I've tried the lcd.compileFont(file_name [,debug])
    with a .c extension font I created with this handy online ttf font to .c converter

    I haven't had any luck so far, just results in errors or no response
    will keep trying

  • @lukasmaximus

    You can use ttf2c_vc2003.exe to convert the ttf fonts to c files and compile it in MicroPython. It works.

  • hello also tried to compile font
    prepared font with ttf2c_vc2003.exe and uploaded to M5stack
    but python returns
    lcd.compileFont('/flash/impact.c', debug=True)
    memory allocation error
    any idea whats wrong?

    impact.c is 160kb size (i need bigger letters)

  • @devros
    lcd.compileFont() function reads the whole .c file into the memory buffer allocated on the esp32 heap and in your case there is obviously not enough free heap space.
    I'll try to change the function to read the source file in smaller increments so that large files can be compiled.

    Until then, if you can post your .c file here or somewhere else, I can try to compile it for you and send you the compiled .fon file.

    You can post your questions and issues related to MicroPython port used on M5Stack also on its Forum.

  • @loboris
    thanks, dont want to waste your time...
    looks like about 100kb is maximum
    hmm or can i run micropython virtualy on computer and compile font myself?

    tried about 80kb size font (80px size) on m5stack and it works (only font looks little croped form left side on some letters) :) thanks

  • I saw about the ttf2c_vc2003.exe on your github but as I am predominately a mac user and my windows machine is in for repair at the moment, I thought it would be good to offer an alternative to others in a similar situation. I tried wine but get errors and so am unable to use the tool. I compiled a bunch of fonts but always tend to get error messages like this Does that mean the file size is too big? I succesfully compiled the vera font you included on your github so I know the main issue i'm facing is with the conversion from ttf to c.

    I was wondering what is the big difference between the online tool I found and the windows command line tool. I hope to write a clear guide to make it easy for anyone to use their own custom fonts no matter what platform they are on.

    Thanks for your awesome work on this micropython port, I'll have a read on the forums and see if I can find more answers to my question

  • @lukasmaximus
    hi, im also on mac and ttf2c_vc2003.exe works great with wine. I used brew installation method for Wine and wineconsole ttf2c_vc2003.exe command...
    File size is limited to 100kb for now

  • I'm working on online font converter which will be available on my server.
    It will convert any ttf font to c source file and/or compiled fon file compatible with this MicroPython.
    It should be ready in a couple of weeks (I don't have much free time).

  • @loboris very cool, look forward to trying it out, meanwhile I will find a way to use the tool you suggested, for some reason I have never been able to successfully install homebrew on my mac. BTW I was wondering does it work with non roman fonts? japanese, thai, arabic etc..

  • @loboris While you are working on this, could you also make it an output option to create the .h files needed to use the font with Adafruit's gfx library? (See here for details.) Is there any way for me to help?

  • @ropCahnge Font Size in Python 中说:

    @loboris While you are working on this, could you also make it an output option to create the .h files needed to use the font with Adafruit's gfx library? (See here for details.) Is there any way for me to help?

    Maybe, if you can provide the exact and detailed description of the format.

  • @loboris Not sure what server you would be running this on. If linux or other unix it should be relatively easy. As per the link I sent earlier:

    Adding New Fonts
    If you want to create new font sizes not included with the library, or adapt entirely new fonts, we have a command-line tool (in the fontconvert folder) for this. It should work on many Linux- or UNIX-like systems (Raspberry Pi, Mac OS X, maybe Cygwin for Windows, among others).
    Building this tool requires the gcc compiler and FreeType library. Most Linux distributions include both by default. For others, you may need to install developer tools and download and build FreeType from the source. Then edit the Makefile to match your setup before invoking make.
    fontconvert expects at least two arguments: a font filename (such as a scalable TrueType vector font) and a size, in points (72 points = 1 inch; the code presumes a screen resolution similar to the Adafruit 2.8" TFT displays). The output should be redirected to a .h file…you can call this whatever you like but I try to be somewhat descriptive:

    ./fontconvert myfont.ttf 12 > myfont12pt7b.h

  • I have finished the font converter (written in Python) for the font format used in this MicroPython port and it works wery well.
    I'm working on the web interface now, the online converter will be available soon on my server, after some more testing.
    It will also be available as a standalone Python program (should work on any OS).

    Some features:

    • Converting any ttf font to C source file which can be compiled with tft.font_compile() or included into the build to be compiled into the firmware is supported

    • Converting directly to binary fon file which can be used with tft.font() is also supported

    • Convert full ascii character set (32 - 127) or only the selected characters

    • Adding characters from cp1252 and cp1250 code pages is supported (more code pages will be probably added later).
      Including characters like Ä, , ß, Č, Đ, ž, ø, µ, ° is now supported
      I'll try to add support for non-Latin character sets like Cyrillic, Hebrew, Arabic, Chinese, later.

    • Font sizes from 8 to 110 pixels are supported

    At the moment I don't have enough free time to support conversion to any other format.

  • @loboris
    thanks :) can test it when its ready ...

  • Awesome can we share this or do you want to keep the testing to a small number of people?

  • @lukasmaximusCahnge Font Size in Python 中说:

    Awesome can we share this or do you want to keep the testing to a small number of people?

    It should be ready for use, by "testing" I've meant I would like to get some information if any issue is detected or any improvement is wanted.
    I've already found a small "bug", if hebrew characters are included the wrong C file is generated (caused by right to left writting), the bynary fon file is OK.

  • Super old thread, but I'm curious if @loboris's tool still works to convert fonts (ttf or otf) to be used with the m5stack, since the firmware moved to use the mainline MicroPython code base.

    I used the tool and created a font (name is Flighter20) and copied it to /flash. When I try to load it, the m5stack crashes.

    MicroPython c24d12861-dirty on 2020-08-06; M5Stack with ESP32
    Type "help()" for more information.
    >>> os.listdir()
    ['Flighter20.c', 'Flighter20.fon', 'apps', 'blocks', '', 'emojiImg', 'img', '', 'res', '', '']
    >>> lcd.font("Flighter20")
    Guru Meditation Error: Core  1 panic'ed (LoadProhibited). Exception was unhandled.
    Core 1 register dump:
    PC      : 0x4010a2f5  PS      : 0x00060f30  A0      : 0x80116f3d  A1      : 0x3ffcea40  
    A2      : 0x00000001  A3      : 0x3f81b150  A4      : 0x3ffceaf0  A5      : 0x00000008  
    A6      : 0x3ffceaa0  A7      : 0x00000000  A8      : 0x3ffbdbec  A9      : 0x3ffbdbec  
    A10     : 0x00000000  A11     : 0x00000000  A12     : 0x80096333  A13     : 0x3ffce950  
    A14     : 0x00000003  A15     : 0x00060023  SAR     : 0x00000020  EXCCAUSE: 0x0000001c  
    EXCVADDR: 0x00000000  LBEG    : 0x40095234  LEND    : 0x4009523f  LCOUNT  : 0x00000000  
    ELF file SHA256: 0000000000000000000000000000000000000000000000000000000000000000
    Backtrace: 0x4010a2f5:0x3ffcea40 0x40116f3a:0x3ffcea60 0x400e3be1:0x3ffceaf0 0x400df959:0x3ffceb20 0x400dfa8d:0x3ffceb40 0x400ed9b3:0x3ffceb60 0x400e3cb0:0x3ffcec00 0x400df959:0x3ffcec70 0x400df982:0x3ffcec90 0x4011c6a7:0x3ffcecb0 0x4011c8c1:0x3ffced40 0x400f6f1c:0x3ffced80 0x4009613a:0x3ffcedb0
    ets Jun  8 2016 00:22:57
    rst:0xc (SW_CPU_RESET),boot:0x13 (SPI_FAST_FLASH_BOOT)
    configsip: 0, SPIWP:0xee
    mode:DIO, clock div:1
    entry 0x400806c0

    Is there any way to load custom fonts to the m5stack now?