M5Paper text



  • When I use the code

    canvas1.createRender(100);
    canvas1.setTextSize(100);
    
    uur=random(23);
    minuut=random(59);
    seconde=random(59);
    sprintf(charBuffer,"%02d:%02d:%02d",uur,minuut,seconde);
    int nWidth = canvas1.textWidth(charBuffer);
    

    nWidth is zero. How can I get the width of a string?
    When I test is wihth :
    nWidth = canvas1.textWidth("test");
    the answer is sill zero.
    Any sugestion?



  • Hello @Powersoft

    would you mind posting your complete code? I cannot compile the snippet you posted w/o errors.

    Thanks
    Felix



  • Are you using a freetype font?

    Looking at the source for textWidth, it does not support freetype fonts:

    int16_t TFT_eSPI::textWidth(const char *string, uint8_t font)
    {
      int32_t str_width = 0;
      uint16_t uniCode  = 0;
    
    #ifdef FREETYPE_FONT
      if((gfxFont == NULL) && _is_freetype_loaded && _use_freetype_font)
      {
        // TODO: Measure freetype width
        return 0;
      }
    #endif
    


  • You can workaround this by using drawString instead, to an offscreen location.
    drawString returns the total width of the string drawn, and works for freetype fonts.

    int nWidth = canvas1.drawString(charBuffer,0,1000);
    


  • It looks like you are trying to get information from the canvas before anything is written to the canvas which is why you are getting zero.
    As @murraypaul you need to query the data in the memory before it is drawn on the screen.



  • @murraypaul

    Thanks, have also take a look into the API documentation.
    Found the function drawString, but what is the meaning of "uint8_t font"

    Function: Draw string
    int16_t drawString(const char *string, int32_t poX, int32_t poY, uint8_t font)

    Thanks for any help.



  • @murraypaul
    Thanks for this.Was helpful



  • @powersoft said in M5Paper text:

    @murraypaul

    Thanks, have also take a look into the API documentation.
    Found the function drawString, but what is the meaning of "uint8_t font"

    Function: Draw string
    int16_t drawString(const char *string, int32_t poX, int32_t poY, uint8_t font)

    The font parameter is not used if you are using FreeType fonts, there is a drawString version that does not require it.

    I'm still working through this as well, but it seems that there are a number of different ways of using fonts on the device.

    • TTF fonts.
      • Use loadFont to load a TTF font file,
      • Use createRender to convert the vector TTF data into bitmap data at as many sizes as you want
      • Use setTextSize to select one of the pre-rendered sizes
      • Use drawString with no font argument to draw the text
      • Allows use of multiple font sizes easily, but is expensive to switch fonts or bold/italic
    • Pre-rendered vector fonts
      • Convert a TTF font file into a pre-rendered source file at a specified size, and compile it in to your application
      • A number of fonts are already converted and available, see libraries\M5EPD\src\Fonts\GFXFF in your Arduino directory
      • See https://learn.adafruit.com/adafruit-gfx-graphics-library/using-fonts to convert more fonts
      • To use one of these fonts
        • call useFreetypeFont(false)
        • call setFreeFont(&FreeSans12pt7b) or whatever the font is defined as in the header file created by the conversion app
        • call drawString with GFXFF as the font argument
        • setTextSize does not do anything
      • Quickly switch between fonts and bold/italic
      • Sizes are pre-calculated, and you can only use the ones you have compiled in
    • (There are also build-in bitmap fonts at a few sizes, these are selected with a font parameter != GFXFF)


  • I think this all means that you have to choose between being able to quickly display the same font at any arbitrary size, or being able to quickly switch between different fonts, or enable/disable bold and italic.

    To be able to efficiently do both would I think require changes to the In_eSPI class to store multiple rendered fonts.