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
33#define RETROFONT_FLAG_OUTLINE 0x04
34
39#define RETROFONT_FLAG_SZ_MIN 0x08
40
44struct RETROFONT;
45
55 const char* font_name, MAUG_MHANDLE* p_font_h,
56 uint8_t glyph_h, uint16_t first_glyph, uint16_t glyphs_count );
57
68 retroflat_blit_t* target, RETROFLAT_COLOR color,
69 const char* str, size_t str_sz,
70 MAUG_MHANDLE font_h, size_t x, size_t y,
71 size_t max_w, size_t max_h, uint8_t flags );
72
73void retrofont_string_indent(
74 retroflat_blit_t* target, RETROFLAT_COLOR color,
75 const char* str, size_t str_sz,
76 MAUG_MHANDLE font_h, size_t x, size_t y,
77 size_t max_w, size_t max_h, size_t x_iter, uint8_t flags );
78
79MERROR_RETVAL retrofont_string_sz(
80 retroflat_blit_t* target, const char* str, size_t str_sz,
81 MAUG_MHANDLE font_h, retroflat_pxxy_t max_w, retroflat_pxxy_t max_h,
82 retroflat_pxxy_t* p_out_w, retroflat_pxxy_t* p_out_h, uint8_t flags );
83
84void retrofont_free( MAUG_MHANDLE* p_font_h );
85
91 struct RETROFONT* font, const char* sub_name, void* data );
92
93#ifdef RETROFNT_C
94
95/* Provide this utility to all font API internal mechanisms. */
96
97MERROR_RETVAL retrofont_read_line(
98 mfile_t* font_file, char* glyph_idx_str, char** p_glyph_bytes
99) {
100 MERROR_RETVAL retval = MERROR_OK;
101 size_t last_char_idx = 0;
102
103 retval = font_file->read_line(
104 font_file, glyph_idx_str, RETROFONT_LINE_SZ, 0 );
105 maug_cleanup_if_not_ok();
106
107 *p_glyph_bytes = maug_strchr( glyph_idx_str, ':' );
108 if( NULL == *p_glyph_bytes ) { \
109 error_printf( "invalid line: %s", glyph_idx_str );
110 /* The line couldn't parse, so skip it, but don't give up entirely and
111 * keep going to the next line.
112 */
113 retval = MERROR_WAIT;
114 goto cleanup;
115 }
116
117 /* Replace : with NULL to split the string and move pointer to actual bytes.
118 */
119 (*p_glyph_bytes)[0] = '\0'; \
120 (*p_glyph_bytes)++;
121
122 /* Hunt for sub statements. */
123 if( 0 == strncmp( "SUB", glyph_idx_str, 3 ) ) {
124 last_char_idx = maug_strlen( *p_glyph_bytes ) - 1;
125 if(
126 '\n' == (*p_glyph_bytes)[last_char_idx] ||
127 '\r' == (*p_glyph_bytes)[last_char_idx] ||
128 '\t' == (*p_glyph_bytes)[last_char_idx] ||
129 ' ' == (*p_glyph_bytes)[last_char_idx]
130 ) {
131 (*p_glyph_bytes)[last_char_idx] = '\0';
132 }
133 debug_printf( RETROFONT_TRACE_LVL, "found sub: \"%s\"", *p_glyph_bytes );
134 retval = MERROR_PARSE;
135 goto cleanup;
136 }
137
138cleanup:
139 return retval;
140}
141
142/* === */
143
144MERROR_RETVAL retrofont_load_stub(
145 const char* font_name, struct RETROFONT* font,
146 retrofont_try_platform_t try_platform,
147 void* try_platform_data
148) {
149 MERROR_RETVAL retval = MERROR_OK;
150 mfile_t font_file;
151 retroflat_asset_path font_stub_name;
152 char line[RETROFONT_LINE_SZ];
153 char* line_bytes = NULL;
154
155 /* Load font stub and find substitute. */
156 maug_mzero( font_stub_name, sizeof( retroflat_asset_path ) );
157 mfile_assign_path( font_stub_name, font_name, 0 );
158 font_stub_name[strlen( font_stub_name ) - 5] = 'x';
159 debug_printf( RETROFONT_TRACE_LVL, "stub font_name: %s", font_stub_name );
160 maug_mzero( &font_file, sizeof( mfile_t ) );
161
162 retval = mfile_open_read( font_stub_name, &font_file );
163 maug_cleanup_if_not_ok();
164
165 /* Figure out font width from file and alloc just enough. */
166 do {
167 retval = retrofont_read_line( &font_file, line, &line_bytes );
168 if( MERROR_WAIT == retval || MERROR_OK == retval ) {
169 /* Not a sub line. */
170 retval = MERROR_PARSE;
171 continue;
172
173 } else if( MERROR_PARSE != retval ) {
174 goto cleanup;
175 }
176 debug_printf( RETROFONT_TRACE_LVL, "attempting substitute: %s",
177 line_bytes );
178 retval = try_platform( font, line_bytes, try_platform_data );
179 } while( MERROR_PARSE == retval );
180
181cleanup:
182
183 mfile_close( &font_file );
184
185 return retval;
186}
187
188/* === */
189
190size_t retrofont_sz_from_filename( const char* font_name ) {
191 const char* p_c = NULL;
192 size_t glyph_h = 0;
193 size_t i = 0;
194 char glyph_h_buf[10];
195
196 maug_mzero( glyph_h_buf, 10 );
197
198 assert( NULL != font_name );
199 assert( ' ' <= font_name[0] );
200
201 p_c = maug_strrchr( font_name, '.' );
202 while( p_c - 1 > font_name ) {
203 /* Start at the char before the '.' and work backwords until a '-'. */
204 p_c--;
205 if( '-' == *p_c || '_' == *p_c ) {
206 break;
207 }
208
209 /* TODO: Break if not a digit! */
210
211 /* Shift existing numbers up by one. */
212 for( i = 9 ; 0 < i ; i-- ) {
213 glyph_h_buf[i] = glyph_h_buf[i - 1];
214 }
215
216 /* Add the most recent number to the beginning. */
217 glyph_h_buf[0] = *p_c;
218 }
219
220 glyph_h = atoi( glyph_h_buf );
221
222 debug_printf(
223 RETROFONT_TRACE_LVL, "detected glyph height: " SIZE_T_FMT, glyph_h );
224
225 return glyph_h;
226}
227
228/* === */
229
231 retroflat_blit_t* target, RETROFLAT_COLOR color,
232 const char* str, size_t str_sz,
233 MAUG_MHANDLE font_h, size_t x, size_t y,
234 size_t max_w, size_t max_h, uint8_t flags
235) {
236 retrofont_string_indent(
237 target, color, str, str_sz, font_h, x, y, max_w, max_h, 0, flags );
238}
239
240#endif /* RETROFNT_C */
241
242#include <mrapifon.h>
243
244/* \} */ /* retrofnt */
245
246#endif /* !RETROFNT_H */
247
uint16_t 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:129
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:90
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.