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