Skip to content

How to correctly calculate the width of text in pixels? #4

Open
@colesnicov

Description

Advise on how to calculate the length of a string in pixels? I use this

uint16_t getStringWidth(window* _window, char* buf){
  uint16_t text_width = 0;
  _fontc f;
  
  for (uint8_t i = 0; i < strlen(buf); i++){
    if (0 > font_attr(_window, buf[i], &f)) {
      ESP_LOGW("Text", "Char '%c' not found in the font specs!", buf[i]);
      return 0;
    } else {
      text_width += f.skip;
    }
  }
  return text_width;
}

but sometimes it gives poor results.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions