From a19c2008705bfda4891d58e95b874e8ec5e1aa3e Mon Sep 17 00:00:00 2001 From: Nunuhara Cabbage Date: Fri, 14 Aug 2026 11:16:35 -0700 Subject: [PATCH] parts: clean up text layout logic --- include/gfx/font.h | 10 ++-------- src/hll/CharSpriteManager.c | 2 +- src/hll/StoatSpriteEngine.c | 2 +- src/parts/parts_internal.h | 2 +- src/parts/render.c | 2 +- src/parts/text.c | 7 +++---- src/text.c | 12 +++++++----- 7 files changed, 16 insertions(+), 21 deletions(-) diff --git a/include/gfx/font.h b/include/gfx/font.h index 28d2051..5764385 100644 --- a/include/gfx/font.h +++ b/include/gfx/font.h @@ -163,21 +163,15 @@ float gfx_render_textf(Texture *dst, float x, int y, char *msg, struct text_styl void gfx_render_dash_text(Texture *dst, struct text_style *ts); void gfx_draw_text_to_amap(Texture *dst, int x, int y, char *text); void gfx_draw_text_to_pmap(Texture *dst, int x, int y, char *text); +float gfx_size_char_unstyled(struct text_style *ts, const char *ch); float gfx_size_char(struct text_style *ts, const char *ch); -float gfx_size_char_kerning(struct text_style *ts, uint32_t code, uint32_t code_next); float gfx_size_text(struct text_style *ts, const char *text); float gfx_get_actual_font_size(unsigned face, float size); float gfx_get_actual_font_size_round_down(unsigned face, float size); -static inline float text_style_width(struct text_style *ts, const char *ch) -{ - return (gfx_size_char(ts, ch) + (ts->bold_width * 2) + ts->edge_left + ts->edge_right) - * ts->scale_x; -} - static inline float text_style_height(struct text_style *ts) { - return ts->size + (ts->bold_width * 2) + ts->edge_up + ts->edge_down; + return ts->size + ceilf(ts->bold_width)*2 + ts->edge_up + ts->edge_down; } #endif /* SYSTEM4_FONT_H */ diff --git a/src/hll/CharSpriteManager.c b/src/hll/CharSpriteManager.c index ca34986..4ddd60f 100644 --- a/src/hll/CharSpriteManager.c +++ b/src/hll/CharSpriteManager.c @@ -176,7 +176,7 @@ static void charsprite_render(struct charsprite *cs) ch[2] = 0; } - int w = ceilf(text_style_width(&cs->ts, ch)); + int w = ceilf(gfx_size_char(&cs->ts, ch)); int h = cs->ts.size + cs->ts.size/2; sprite_init_color(&cs->sp, w, h, 0, 0, 0, 0); sprite_get_texture(&cs->sp); // XXX: force initialization of texture diff --git a/src/hll/StoatSpriteEngine.c b/src/hll/StoatSpriteEngine.c index 5380861..c00979d 100644 --- a/src/hll/StoatSpriteEngine.c +++ b/src/hll/StoatSpriteEngine.c @@ -113,7 +113,7 @@ bool StoatSpriteEngine_SP_SetTextSprite(int sp_no, struct string *text) char s[3]; extract_sjis_char(text->text, s); - int w = ceilf(text_style_width(&text_sprite_ts, s)); + int w = ceilf(gfx_size_char(&text_sprite_ts, s)); int h = text_sprite_ts.size; // XXX: System40.exe lies about the height of half-width characters. // E.g. a size 64 letter "H" is reported as 32 pixels tall. diff --git a/src/parts/parts_internal.h b/src/parts/parts_internal.h index 522519b..db4a21e 100644 --- a/src/parts/parts_internal.h +++ b/src/parts/parts_internal.h @@ -82,7 +82,7 @@ struct sound_motion { struct parts_text_char { Texture t; char ch[4]; - float advance; + int advance; Point off; }; diff --git a/src/parts/render.c b/src/parts/render.c index f700e8a..49480b6 100644 --- a/src/parts/render.c +++ b/src/parts/render.c @@ -135,7 +135,7 @@ static void parts_render_text(struct parts *parts, struct parts_text *t) Rectangle r = { 0, 0, ch->t.w, ch->t.h }; parts_render_texture(&ch->t, mw_transform, &r, blend_rate, add_color, multiply_color, 0, parts->alpha_clipper_parts_no); - x += ceilf(ch->advance); + x += ch->advance; } x = parts->global.pos.x + t->common.origin_offset.x; y += line->height + t->line_space; diff --git a/src/parts/text.c b/src/parts/text.c index 57bff27..423cc10 100644 --- a/src/parts/text.c +++ b/src/parts/text.c @@ -84,13 +84,12 @@ static const char *parts_text_append_char(struct parts_text *t, const char *str) ch->off = text_style_offset(&t->ts); int len = extract_sjis_char(str, ch->ch); - int width = ceilf(text_style_width(&t->ts, ch->ch)); + int width = gfx_size_char(&t->ts, ch->ch); int height = text_style_height(&t->ts); gfx_init_texture_rgba(&ch->t, width, height, (SDL_Color){0,0,0,0}); - ch->advance = gfx_render_textf(&ch->t, 0, 0, ch->ch, &t->ts, false); - + ch->advance = ceilf(gfx_render_textf(&ch->t, 0, 0, ch->ch, &t->ts, false)); line->width += ch->advance; - line->height = max(line->height, height + ceilf(t->ts.bold_width)*2); + line->height = max(line->height, height); return str + len; } diff --git a/src/text.c b/src/text.c index 6ea9141..0a72cd0 100644 --- a/src/text.c +++ b/src/text.c @@ -158,16 +158,18 @@ static uint32_t char_to_code(const char *ch, enum charmap charmap) return c; } -float gfx_size_char(struct text_style *ts, const char *ch) +float gfx_size_char_unstyled(struct text_style *ts, const char *ch) { struct font_size *size = text_style_font_size(ts); return font_size_char(size, char_to_code(ch, size->font->charmap)); } -float gfx_size_char_kerning(struct text_style *ts, uint32_t code, uint32_t code_next) +float gfx_size_char(struct text_style *ts, const char *ch) { - struct font_size *size = text_style_font_size(ts); - return size->font->size_char_kerning(size, code, code_next); + float w = gfx_size_char_unstyled(ts, ch); + if (gfx_text_advance_edges) + w += ts->edge_left + ts->edge_right + ceilf(ts->bold_width)*2; + return w * ts->scale_x; } float gfx_size_text(struct text_style *ts, const char *text) @@ -182,7 +184,7 @@ float gfx_size_text(struct text_style *ts, const char *text) x += edge_advance; text = sjis_skip_char(text); } - return x; + return x * ts->scale_x;; } float gfx_get_actual_font_size(unsigned face, float size)