maug
Quick and dirty C mini-augmentation library.
Loading...
Searching...
No Matches
retrofnt.h
Go to the documentation of this file.
1
2#ifndef RETROFNT_H
3#define RETROFNT_H
4
15
17#define RETROFONT_PRESENT 1
18
19#ifndef RETROFONT_LINE_SZ
20# define RETROFONT_LINE_SZ 80
21#endif /* !RETROFONT_LINE_SZ */
22
23#ifndef RETROFONT_TRACE_LVL
24# define RETROFONT_TRACE_LVL 0
25#endif /* !RETROFONT_TRACE_LVL */
26
30struct RETROFONT;
31
41 const char* font_name, MAUG_MHANDLE* p_font_h,
42 uint8_t glyph_h, uint16_t first_glyph, uint16_t glyphs_count );
43
54 retroflat_blit_t* target, RETROFLAT_COLOR color,
55 const char* str, size_t str_sz,
56 MAUG_MHANDLE font_h, size_t x, size_t y,
57 size_t max_w, size_t max_h, uint8_t flags );
58
59MERROR_RETVAL retrofont_string_sz(
60 retroflat_blit_t* target, const char* str, size_t str_sz,
61 MAUG_MHANDLE font_h, retroflat_pxxy_t max_w, retroflat_pxxy_t max_h,
62 retroflat_pxxy_t* p_out_w, retroflat_pxxy_t* p_out_h, uint8_t flags );
63
64void retrofont_free( MAUG_MHANDLE* p_font_h );
65
71 struct RETROFONT* font, const char* sub_name, void* data );
72
73#ifdef RETROFNT_C
74
75/* Provide this utility to all font API internal mechanisms. */
76
77MERROR_RETVAL retrofont_read_line(
78 mfile_t* font_file, char* glyph_idx_str, char** p_glyph_bytes
79) {
80 MERROR_RETVAL retval = MERROR_OK;
81 size_t last_char_idx = 0;
82
83 retval = font_file->read_line(
84 font_file, glyph_idx_str, RETROFONT_LINE_SZ, 0 );
85 maug_cleanup_if_not_ok();
86
87 *p_glyph_bytes = maug_strchr( glyph_idx_str, ':' );
88 if( NULL == *p_glyph_bytes ) { \
89 error_printf( "invalid line: %s", glyph_idx_str );
90 /* The line couldn't parse, so skip it, but don't give up entirely and
91 * keep going to the next line.
92 */
93 retval = MERROR_WAIT;
94 goto cleanup;
95 }
96
97 /* Replace : with NULL to split the string and move pointer to actual bytes.
98 */
99 (*p_glyph_bytes)[0] = '\0'; \
100 (*p_glyph_bytes)++;
101
102 /* Hunt for sub statements. */
103 if( 0 == strncmp( "SUB", glyph_idx_str, 3 ) ) {
104 last_char_idx = maug_strlen( *p_glyph_bytes ) - 1;
105 if(
106 '\n' == (*p_glyph_bytes)[last_char_idx] ||
107 '\r' == (*p_glyph_bytes)[last_char_idx] ||
108 '\t' == (*p_glyph_bytes)[last_char_idx] ||
109 ' ' == (*p_glyph_bytes)[last_char_idx]
110 ) {
111 (*p_glyph_bytes)[last_char_idx] = '\0';
112 }
113 debug_printf( RETROFONT_TRACE_LVL, "found sub: \"%s\"", *p_glyph_bytes );
114 retval = MERROR_PARSE;
115 goto cleanup;
116 }
117
118cleanup:
119 return retval;
120}
121
122/* === */
123
124MERROR_RETVAL retrofont_load_stub(
125 const char* font_name, struct RETROFONT* font,
126 retrofont_try_platform_t try_platform,
127 void* try_platform_data
128) {
129 MERROR_RETVAL retval = MERROR_OK;
130 mfile_t font_file;
131 retroflat_asset_path font_stub_name;
132 char line[RETROFONT_LINE_SZ];
133 char* line_bytes = NULL;
134
135 /* Load font stub and find substitute. */
136 maug_mzero( font_stub_name, sizeof( retroflat_asset_path ) );
137 mfile_assign_path( font_stub_name, font_name, 0 );
138 font_stub_name[strlen( font_stub_name ) - 5] = 'x';
139 debug_printf( RETROFONT_TRACE_LVL, "stub font_name: %s", font_stub_name );
140 maug_mzero( &font_file, sizeof( mfile_t ) );
141
142 retval = mfile_open_read( font_stub_name, &font_file );
143 maug_cleanup_if_not_ok();
144
145 /* Figure out font width from file and alloc just enough. */
146 do {
147 retval = retrofont_read_line( &font_file, line, &line_bytes );
148 if( MERROR_WAIT == retval || MERROR_OK == retval ) {
149 /* Not a sub line. */
150 retval = MERROR_PARSE;
151 continue;
152
153 } else if( MERROR_PARSE != retval ) {
154 goto cleanup;
155 }
156 debug_printf( RETROFONT_TRACE_LVL, "attempting substitute: %s",
157 line_bytes );
158 retval = try_platform( font, line_bytes, try_platform_data );
159 } while( MERROR_PARSE == retval );
160
161cleanup:
162
163 mfile_close( &font_file );
164
165 return retval;
166}
167
168/* === */
169
170size_t retrofont_sz_from_filename( const char* font_name ) {
171 const char* p_c = NULL;
172 size_t glyph_h = 0;
173 size_t i = 0;
174 char glyph_h_buf[10];
175
176 maug_mzero( glyph_h_buf, 10 );
177
178 assert( NULL != font_name );
179 assert( ' ' <= font_name[0] );
180
181 p_c = maug_strrchr( font_name, '.' );
182 while( p_c - 1 > font_name ) {
183 /* Start at the char before the '.' and work backwords until a '-'. */
184 p_c--;
185 if( '-' == *p_c || '_' == *p_c ) {
186 break;
187 }
188
189 /* TODO: Break if not a digit! */
190
191 /* Shift existing numbers up by one. */
192 for( i = 9 ; 0 < i ; i-- ) {
193 glyph_h_buf[i] = glyph_h_buf[i - 1];
194 }
195
196 /* Add the most recent number to the beginning. */
197 glyph_h_buf[0] = *p_c;
198 }
199
200 glyph_h = atoi( glyph_h_buf );
201
202 debug_printf(
203 RETROFONT_TRACE_LVL, "detected glyph height: " SIZE_T_FMT, glyph_h );
204
205 return glyph_h;
206}
207
208#endif /* RETROFNT_C */
209
210#include <mrapifon.h>
211
212/* \} */ /* retrofnt */
213
214#endif /* !RETROFNT_H */
215
int MERROR_RETVAL
Return type indicating function returns a value from this list.
Definition merror.h:19
MERROR_RETVAL mfile_open_read(const char *filename, mfile_t *p_file)
Open a file and read it into memory or memory-map it.
void mfile_close(mfile_t *p_file)
Close a file opened with mfile_open_read().
char retroflat_asset_path[MAUG_PATH_SZ_MAX+1]
Path/name used to load an asset from disk.
Definition mfile.h:103
int8_t RETROFLAT_COLOR
Defines an index in the platform-specific color-table.
Definition retroflt.h:325
size_t retroflat_pxxy_t
Type used for surface pixel coordinates.
Definition retroflt.h:870
MERROR_RETVAL(* retrofont_try_platform_t)(struct RETROFONT *font, const char *sub_name, void *data)
Callback for platform-specific font substitute loader to attempt to use font substitute.
Definition retrofnt.h:70
void retrofont_string(retroflat_blit_t *target, RETROFLAT_COLOR color, const char *str, size_t str_sz, MAUG_MHANDLE font_h, size_t x, size_t y, size_t max_w, size_t max_h, uint8_t flags)
Draw a string with the given font.
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)
Load a font for drawing.