Skip to content

Commit

Permalink
Handle more flags in print and len functions
Browse files Browse the repository at this point in the history
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).
  • Loading branch information
Daaaav committed Jan 30, 2023
1 parent 49bc70f commit f347f0c
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 10 deletions.
37 changes: 28 additions & 9 deletions desktop_version/src/Font.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -573,12 +573,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)
{
Expand All @@ -604,16 +611,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(
Expand All @@ -628,11 +634,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)
{
Expand All @@ -647,7 +651,6 @@ void print(
x -= textlen;
}
}
// TODO cjk_low/cjk_high

if (pf.border && !graphics.notextoutline)
{
Expand All @@ -665,13 +668,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,
Expand Down
2 changes: 1 addition & 1 deletion desktop_version/src/Font.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit f347f0c

Please sign in to comment.