From 0417907d039cced53ea5de0b600d1f5c59cc6bdb Mon Sep 17 00:00:00 2001 From: Reed Nightingale Date: Mon, 27 Jan 2020 23:08:32 -0800 Subject: [PATCH] Add justification option for text rendering --- nano_gui.cpp | 13 +++++++++++-- nano_gui.h | 9 ++++++++- 2 files changed, 19 insertions(+), 3 deletions(-) diff --git a/nano_gui.cpp b/nano_gui.cpp index fdfdfa3..1bc9d55 100644 --- a/nano_gui.cpp +++ b/nano_gui.cpp @@ -228,7 +228,8 @@ void displayRawText(char *text, int x1, int y1, int w, int color, int background tft.print(text); } -void displayText(char *text, int x1, int y1, int w, int h, int color, int background, int border) { +void displayText(char *text, int x1, int y1, int w, int h, int color, int background, int border, TextJustification_e justification) +{ displayFillrect(x1, y1, w ,h, background); displayRect(x1, y1, w ,h, border); @@ -237,7 +238,15 @@ void displayText(char *text, int x1, int y1, int w, int h, int color, int backgr uint16_t width_out; uint16_t height_out; tft.getTextBounds(text,x1,y1,&x1_out,&y1_out,&width_out,&height_out,w); - x1 += (w - ( (int32_t)width_out + (x1_out-x1)))/2; + if(TextJustification_e::Center == justification){ + x1 += (w - ( (int32_t)width_out + (x1_out-x1)))/2; + } + else if(TextJustification_e::Right == justification){ + x1 += w - ((int32_t)width_out + (x1_out-x1)); + } + else{ + x1 += 2;//Give a little bit of padding from the border + } y1 += (ubitx_font->yAdvance + h - ( (int32_t)height_out))/2; displayRawText(text,x1,y1,w,color,background); } diff --git a/nano_gui.h b/nano_gui.h index b6d24ac..7c1fd21 100644 --- a/nano_gui.h +++ b/nano_gui.h @@ -7,6 +7,13 @@ struct Point { }; extern struct Point ts_point; +enum TextJustification_e : uint8_t +{ + Left, + Right, + Center +}; + void displayInit(); void displayClear(unsigned int color); void displayPixel(unsigned int x, unsigned int y, unsigned int c); @@ -16,7 +23,7 @@ void displayRect(unsigned int x,unsigned int y,unsigned int w,unsigned int h,uns void displayFillrect(unsigned int x,unsigned int y,unsigned int w,unsigned int h,unsigned int c); void displayChar(int16_t x, int16_t y, unsigned char c, uint16_t color, uint16_t bg); void displayRawText(char *text, int x1, int y1, int color, int background); -void displayText(char *text, int x1, int y1, int w, int h, int color, int background, int border); +void displayText(char *text, int x1, int y1, int w, int h, int color, int background, int border, TextJustification_e justification = TextJustification_e::Center); void formatFreq(uint32_t freq, char* buff, uint16_t buff_size);