diff --git a/libraries/badger2040/badger2040.cpp b/libraries/badger2040/badger2040.cpp index 18d764ff..24e5ecd9 100644 --- a/libraries/badger2040/badger2040.cpp +++ b/libraries/badger2040/badger2040.cpp @@ -205,7 +205,6 @@ namespace pimoroni { } int32_t Badger2040::glyph(unsigned char c, int32_t x, int32_t y, float s, float a) { - // if space character then return a width to move the caret by const hershey_font_glyph_t *gd = glyph_data(c); // if glyph data not found (id too great) then skip @@ -266,6 +265,26 @@ namespace pimoroni { } } + int32_t Badger2040::measure_text(std::string message, float s) { + int32_t width = 0; + for(auto &c : message) { + width += measure_glyph(c, s); + } + return width; + } + + int32_t Badger2040::measure_glyph(unsigned char c, float s) { + const hershey_font_glyph_t *gd = glyph_data(c); + + // if glyph data not found (id too great) then skip + if(!gd) { + return 0; + } + + return gd->width * s; + } + + void Badger2040::font(std::string name) { // check that font exists and assign it if(fonts.find(name) != fonts.end()) { diff --git a/libraries/badger2040/badger2040.hpp b/libraries/badger2040/badger2040.hpp index a2650d3c..08073d8d 100644 --- a/libraries/badger2040/badger2040.hpp +++ b/libraries/badger2040/badger2040.hpp @@ -56,9 +56,13 @@ namespace pimoroni { void image(const uint8_t *data); // text (fonts: sans, sans_bold, gothic, cursive_bold, cursive, serif_italic, serif, serif_bold) - void text(std::string message, int32_t x, int32_t y, float s = 1.0f, float a = 0.0f); const hershey_font_glyph_t* glyph_data(unsigned char c); - int32_t glyph(unsigned char c, int32_t x, int32_t y, float s, float a = 0.0f); + + void text(std::string message, int32_t x, int32_t y, float s = 1.0f, float a = 0.0f); + int32_t glyph(unsigned char c, int32_t x, int32_t y, float s = 1.0f, float a = 0.0f); + + int32_t measure_text(std::string message, float s = 1.0f); + int32_t measure_glyph(unsigned char c, float s = 1.0f); void debug_command(uint8_t command, size_t len, const uint8_t *data); void dump_otp(uint8_t *otp_data);