g.getFont does not return font set by g.setFont

Posted on
  • can anybody explain the following behaviour (entered into the left-hand side of the Web IDE):

    =Graphics: {
      flip: function () { [native code] },
      drawRoundedRect: function (x1,y1,x2,y2,r) { ... },
      fillRoundedRect: function (x1,y1,x2,y2,r) { ... }

    I have a similar problem deep inside a program which crashes with

    Uncaught Error: Font bitmap must be a String
     at line 1 col 1140

    In Details.font I've stored a previously configured font, obtained using g.getFont() (which returned Custom, although the font was set using g.setFont12x20() before)

  • The issue is the 12x20 font is stored inside Espruino as if it was a custom font. When a custom font is loaded, all that's stored in Graphics is the data for the font - so it's pretty much impossible to figure out which function was called to get that data :(

    Potentially we could also store the font name in Graphics, but that's not trivial due to restrictions on how many arguments built-in functions can have :(

  • Well, that's bad but not not tragical.

    Are there more fonts like 12x20 that will be reported as Custom?

  • I just wrote a little "program" to find out which fonts on a Bangle.js 2 will be reported properly and which will not:

    print('which fonts will not properly be reported?');
      let FontList = g.getFonts();
      for (let i = 0, l = FontList.length; i < l; i++) {
        let Font = FontList[i];
        if (g.getFont() === Font) {
          print('==  set: "' + Font + '", got "' + Font + '"');
        } else {
          print('<>  set: "' + Font + '", got "' + g.getFont() + '"');


    >which fonts will not properly be reported?
    >==  set: "4x6", got "4x6"
    ==  set: "6x8", got "6x8"
    <>  set: "12x20", got "Custom"
    <>  set: "6x15", got "Custom"
    ==  set: "Vector", got "Vector"
  • Yes, that sounds right - any actual custom fonts added will have the same issue too

  • Unfortunately no meta data is kept about the custom font (except the fact that it is recognized as a custom font). Therefore, I wrapped in my http://www.espruino.com/ui (doc) / ...UI framework... (forum) the font handling. At the same time I used a palette approach to simplify / application-localize font referencing.

  • good to know - thank you very much!

  • Post a reply
    • Bold
    • Italics
    • Link
    • Image
    • List
    • Quote
    • code
    • Preview

g.getFont does not return font set by g.setFont

Posted by Avatar for Andreas_Rozek @Andreas_Rozek