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:
    https://github.com/m5stack/M5Cloud#lcd


  • M5Stack

    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
    lcd.font(lcd.FONT_DejaVu24)

    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])

    https://github.com/m5stack/M5Cloud#lcdcompilefontfile_name-debug


  • M5Stack

    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
    https://littlevgl.com/ttf-font-to-c-array

    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
    False
    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


  • M5Stack

    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 https://ibb.co/bZBCoV 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).


  • M5Stack

    @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 ...



  • The online font creator is now ready for testing.
    More instructions and examples will be added later, but it should work.
    Please, test and report the issues.

    LoBo Font creator


  • M5Stack

    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.


登录后回复
 

与 M5Stack Community 的连接断开,我们正在尝试重连,请耐心等待