Skip to content

Commit

Permalink
Improve button text position and clip
Browse files Browse the repository at this point in the history
  • Loading branch information
rhaleblian committed Jul 7, 2024
1 parent 8d1a03a commit a04fc81
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 18 deletions.
1 change: 1 addition & 0 deletions include/text.h
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,7 @@ class Text {
u8 GetAdvance(u32 ucs);
u8 GetAdvance(u32 ucs, u8 style);
u8 GetCharCode(const char* txt, u32* code);
u8 GetCharCountInsideWidth(const char *txt, u8 style, u8 pixels);
FT_Face GetFace() { return face; }
FT_Face GetFace(u8 style) { return faces[style]; }
string GetFontFile(u8 style);
Expand Down
6 changes: 3 additions & 3 deletions source/app_browser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -164,15 +164,15 @@ void App::browser_init(void)
buttons[i]->SetLabel2(*(book->GetAuthor()));
}
buttonprev.Init(ts);
buttonprev.Move(2,240);
buttonprev.Move(2,238);
buttonprev.Resize(60,16);
buttonprev.Label("prev");
buttonnext.Init(ts);
buttonnext.Move(130,240);
buttonnext.Move(130,238);
buttonnext.Resize(60,16);
buttonnext.Label("next");
buttonprefs.Init(ts);
buttonprefs.Move(66,240);
buttonprefs.Move(66,238);
buttonprefs.Resize(60,16);
buttonprefs.Label("prefs");

Expand Down
28 changes: 13 additions & 15 deletions source/button.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,15 +79,17 @@ void Button::Draw(u16 *fb, bool highlight) {
}
}

// u16 bordercolor = RGB15(22,22,22) | BIT(15);
// for (x=ul.x;x<lr.x;x++) {
// fb[ul.y*SCREEN_WIDTH + x] = bordercolor;
// fb[lr.y*SCREEN_WIDTH + x] = bordercolor;
// }
// for (y=ul.y;y<lr.y;y++) {
// fb[y*SCREEN_WIDTH + ul.x] = bordercolor;
// fb[y*SCREEN_WIDTH + lr.x-1] = bordercolor;
// }
#if 0
u16 bordercolor = RGB15(22,22,22) | BIT(15);
for (x=ul.x;x<lr.x;x++) {
fb[ul.y*SCREEN_WIDTH + x] = bordercolor;
fb[lr.y*SCREEN_WIDTH + x] = bordercolor;
}
for (y=ul.y;y<lr.y;y++) {
fb[y*SCREEN_WIDTH + ul.x] = bordercolor;
fb[y*SCREEN_WIDTH + lr.x-1] = bordercolor;
}
#endif

bool invert = ts->GetInvert();
ts->SetScreen(fb);
Expand All @@ -97,13 +99,9 @@ void Button::Draw(u16 *fb, bool highlight) {
ts->SetPen(ul.x+6, ul.y + ts->GetHeight());
if(highlight) ts->usebgcolor = true;

// FIXME request a string fitting into the bounding box instead.
ts->SetPixelSize(ts->GetPixelSize()+1);
if(text.length() > 30)
ts->PrintString((const char*)text.substr(0,30).append("...").c_str(),
TEXT_STYLE_BROWSER);
else
ts->PrintString((const char*)text.c_str(), TEXT_STYLE_BROWSER);
uint8_t len = ts->GetCharCountInsideWidth(text.c_str(), TEXT_STYLE_BROWSER, SCREEN_HEIGHT);
ts->PrintString((const char*)text.substr(0, len).c_str(), TEXT_STYLE_BROWSER);
ts->SetPixelSize(ts->GetPixelSize()-1);

if (text2.length()) {
Expand Down
15 changes: 15 additions & 0 deletions source/text.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -408,6 +408,21 @@ u8 Text::GetStringWidth(const char *txt, FT_Face face)
return width;
}

u8 Text::GetCharCountInsideWidth(const char *txt, u8 style, u8 pixels) {
u8 n = 0;
u8 width = 0;
const char *c;
for(c = txt; c != NULL; c++)
{
u32 ucs = 0;
GetCharCode(c, &ucs);
width += GetAdvance(ucs, GetFace(style));
if (width > pixels) return n;
n++;
}
return n;
}

u8 Text::GetCharCode(const char *utf8, u32 *ucs) {
//! Given a UTF-8 encoding, fill in the Unicode/UCS code point.
//! Return the bytelength of the encoding, for advancing
Expand Down

0 comments on commit a04fc81

Please sign in to comment.