From 22dcc29d453ee929ee42dd1a9c1763407b2ca9e9 Mon Sep 17 00:00:00 2001 From: Dav999-v Date: Sun, 8 Jan 2023 01:22:15 +0100 Subject: [PATCH] Handle more flags in print and len functions The font::len function now handles the printing scale, so it can immediately return the scaled length instead of having the caller calculate it. The print function now handles CJK low/high flags and vertically centers CJK text by default (instead of letting it stick out on the bottom). --- desktop_version/src/Font.cpp | 37 +++++++++++++++++++++++++++--------- desktop_version/src/Font.h | 2 +- 2 files changed, 29 insertions(+), 10 deletions(-) diff --git a/desktop_version/src/Font.cpp b/desktop_version/src/Font.cpp index 6db08dab..3822e701 100644 --- a/desktop_version/src/Font.cpp +++ b/desktop_version/src/Font.cpp @@ -577,12 +577,19 @@ static int print_char( return glyph->advance * scale; } +static Font* fontsel_to_font(int sel) +{ + /* Take font selection integer (0-31) and turn it into the correct Font */ + // TODO handle all these cases here like 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 etc + return &font::temp_bfont; +} + #define FLAG_PART(start, count) ((flags >> start) % (1 << count)) static PrintFlags decode_print_flags(uint32_t flags) { PrintFlags pf; pf.scale = FLAG_PART(0, 3) + 1; - pf.font_sel = FLAG_PART(3, 5); + pf.font_sel = fontsel_to_font(FLAG_PART(3, 5)); if (flags & PR_AB_IS_BRI) { @@ -608,16 +615,15 @@ static PrintFlags decode_print_flags(uint32_t flags) int len(const uint32_t flags, const std::string& t) { PrintFlags pf = decode_print_flags(flags); - // TODO flags! int text_len = 0; std::string::const_iterator iter = t.begin(); while (iter != t.end()) { int cur = utf8::unchecked::next(iter); - text_len += get_advance(&font::temp_bfont, cur); + text_len += get_advance(pf.font_sel, cur); } - return text_len; + return text_len * pf.scale; } void print( @@ -632,11 +638,9 @@ void print( { PrintFlags pf = decode_print_flags(flags); - // TODO pf.font_sel - if (pf.align_cen || pf.align_right) { - const int textlen = graphics.len(text) * pf.scale; + const int textlen = len(flags, text); if (pf.align_cen) { @@ -651,7 +655,6 @@ void print( x -= textlen; } } - // TODO cjk_low/cjk_high if (pf.border && !graphics.notextoutline) { @@ -669,13 +672,29 @@ void print( } } + int h_diff_8 = pf.font_sel->glyph_h-8; + if (h_diff_8 < 0) + { + /* If the font is less high than 8, + * just center it (lower on screen) */ + y -= h_diff_8/2; + } + else if (pf.cjk_high) + { + y -= h_diff_8; + } + else if (!pf.cjk_low) + { + y -= h_diff_8/2; + } + int position = 0; std::string::const_iterator iter = text.begin(); while (iter != text.end()) { const uint32_t character = utf8::unchecked::next(iter); position += font::print_char( - &font::temp_bfont, + pf.font_sel, character, x + position, y, diff --git a/desktop_version/src/Font.h b/desktop_version/src/Font.h index 47d66d5a..ee84c2bb 100644 --- a/desktop_version/src/Font.h +++ b/desktop_version/src/Font.h @@ -63,7 +63,7 @@ struct Font struct PrintFlags { uint8_t scale; - uint8_t font_sel; + Font* font_sel; uint8_t alpha; uint8_t colorglyph_bri; bool border;