Skip to content

Commit

Permalink
Implemented autodetecting font height from filename.
Browse files Browse the repository at this point in the history
  • Loading branch information
indigoparadox committed Jun 1, 2024
1 parent d0ef4f2 commit e6d395e
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 4 deletions.
45 changes: 45 additions & 0 deletions src/retrofnt.h
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,40 @@ void retrofont_dump_glyph( uint8_t* glyph, uint8_t w, uint8_t h ) {
}
}

static size_t retrofont_sz_from_filename( const char* font_name ) {
const char* p_c = NULL;
size_t glyph_h = 0;
size_t i = 0;
char glyph_h_buf[10];

maug_mzero( glyph_h_buf, 10 );

p_c = strrchr( font_name, '.' );
while( p_c - 1 > font_name ) {
/* Start at the char before the '.' and work backwords until a '-'. */
p_c--;
if( '-' == *p_c ) {
break;
}

/* TODO: Break if not a digit! */

/* Shift existing numbers up by one. */
for( i = 9 ; 0 < i ; i-- ) {
glyph_h_buf[i] = glyph_h_buf[i - 1];
}

/* Add the most recent number to the beginning. */
glyph_h_buf[0] = *p_c;
}

glyph_h = atoi( glyph_h_buf );

debug_printf( 1, "detected glyph height: " SIZE_T_FMT, glyph_h );

return glyph_h;
}

MERROR_RETVAL retrofont_load(
const char* font_name, MAUG_MHANDLE* p_font_h,
uint8_t glyph_h, uint16_t first_glyph, uint16_t glyphs_count
Expand All @@ -85,10 +119,21 @@ MERROR_RETVAL retrofont_load(
uint8_t glyph_w_bytes = 0;
uint8_t glyph_w = 0;

maug_mzero( &font_file, sizeof( mfile_t ) );

/* TODO: Font loading seems to be very slow on a 486. This needs
* investigation.
*/

if( 0 == glyph_h ) {
glyph_h = retrofont_sz_from_filename( font_name );
}
if( 0 == glyph_h ) {
error_printf( "unable to determine font height!" );
retval = MERROR_GUI;
goto cleanup;
}

/* Try to separate the string into index:glyph bytes. */
#define retrofont_split_glyph_line( line, line_bytes ) \
line_bytes = strchr( line, ':' ); \
Expand Down
4 changes: 2 additions & 2 deletions src/retrohtr.h
Original file line number Diff line number Diff line change
Expand Up @@ -546,10 +546,10 @@ static MERROR_RETVAL retrohtr_load_font(
/* Load the font into the cache. */
#ifdef RETROGXC_PRESENT
*font_idx_p =
retrogxc_load_font( &(str_table[effect_style->FONT_FAMILY]), 8, 33, 93 );
retrogxc_load_font( &(str_table[effect_style->FONT_FAMILY]), 0, 33, 93 );
#else
retval = retrofont_load(
&(str_table[effect_style->FONT_FAMILY]), font_h_p, 8, 33, 93 );
&(str_table[effect_style->FONT_FAMILY]), font_h_p, 0, 33, 93 );
#endif /* RETROGXC_PRESENT */

cleanup:
Expand Down
4 changes: 2 additions & 2 deletions src/retrow3d.h
Original file line number Diff line number Diff line change
Expand Up @@ -226,8 +226,8 @@ MERROR_RETVAL retro3dw_push_win(

win->flags |= RETROWIN3D_FLAG_INIT_GUI;

/* TODO: Parse font properties from filename. */
retval = retrofont_load( font_filename, &(win->gui.font_h), 8, 33, 93 );
/* Parse font height from filename and only load printable glyphs. */
retval = retrofont_load( font_filename, &(win->gui.font_h), 0, 33, 93 );
maug_cleanup_if_not_ok();

win->w = w;
Expand Down

0 comments on commit e6d395e

Please sign in to comment.