Skip to content

Commit 11055ac

Browse files
authored
Remove font member from font_def to save 80bytes RAM (#594)
the buffer for each character is allocated from the stack.
1 parent 0c2943b commit 11055ac

1 file changed

Lines changed: 28 additions & 27 deletions

File tree

src/screen/320x240x16/lcd_string.c

Lines changed: 28 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -46,20 +46,20 @@ static FATFS FontFAT;
4646
*/
4747
//#define LINE_SPACING 2 // move to _gui.h as devo10's line spacing is different from devo8's
4848
#define CHAR_SPACING 1
49+
#define CHAR_BUF_SIZE 80
4950
#define RANGE_TABLE_SIZE 20
5051
#define NUM_FONTS 10
5152

5253
#define HEIGHT(x) x.height
5354
#define FONT_NAME_LEN 9
5455
char FontNames[NUM_FONTS][FONT_NAME_LEN];
5556

56-
struct font_def
57+
struct font_def
5758
{
58-
u8 idx;
59-
FILE *fh;
60-
u8 font[80];
59+
u8 idx;
60+
FILE *fh;
6161
u8 height; /* Character height for storage */
62-
u16 range[2 * (RANGE_TABLE_SIZE + 1)]; /* Array containing the ranges of supported characters */
62+
u16 range[2 * (RANGE_TABLE_SIZE + 1)]; /* Array containing the ranges of supported characters */
6363
};
6464
static struct {
6565
struct font_def font;
@@ -89,6 +89,7 @@ u8 get_char_range(u32 c, u32 *begin, u32 *end)
8989
{
9090
u32 offset = 0;
9191
u32 pos = 5;
92+
u8 buf[6];
9293
u16 *range = cur_str.font.range;
9394
while(1) {
9495
if (range[0] == 0 && range[1] == 0)
@@ -102,30 +103,27 @@ u8 get_char_range(u32 c, u32 *begin, u32 *end)
102103
pos += 4;
103104
}
104105
fseek(cur_str.font.fh, pos, SEEK_SET);
105-
u8 *font = cur_str.font.font;
106-
fread(font, 6, 1, cur_str.font.fh);
107-
*begin = font[0] | (font[1] << 8) | (font[2] << 16);
108-
*end = font[3] | (font[4] << 8) | (font[5] << 16);
106+
fread(buf, 6, 1, cur_str.font.fh);
107+
*begin = buf[0] | (buf[1] << 8) | (buf[2] << 16);
108+
*end = buf[3] | (buf[4] << 8) | (buf[5] << 16);
109109
return 1;
110110
}
111111

112-
const u8 *char_offset(u32 c, u8 *width)
112+
void char_read(u8 *font, u32 c, u8 *width)
113113
{
114114
u32 begin;
115115
u32 end;
116-
u8 *font = cur_str.font.font;
117116

118117
u8 row_bytes = ((cur_str.font.height - 1) / 8) + 1;
119118
get_char_range(c, &begin, &end);
120119
*width = (end - begin) / row_bytes;
121120
fseek(cur_str.font.fh, begin, SEEK_SET);
122-
if (end - begin > sizeof(cur_str.font.font)) {
121+
if (end - begin > CHAR_BUF_SIZE) {
123122
printf("Character '%04d' is larger than allowed size\n", (int)c);
124-
end = begin + (sizeof(cur_str.font.font) / row_bytes) * row_bytes;
123+
end = begin + (CHAR_BUF_SIZE / row_bytes) * row_bytes;
125124
*width = (end - begin) / row_bytes;
126125
}
127126
fread(font, end - begin, 1, cur_str.font.fh);
128-
return font;
129127
}
130128

131129
u8 get_width(u32 c)
@@ -136,12 +134,15 @@ u8 get_width(u32 c)
136134
u8 row_bytes = ((cur_str.font.height - 1) / 8) + 1;
137135
get_char_range(c, &begin, &end);
138136
return (end - begin) / row_bytes;
139-
}
137+
}
140138

141139
void LCD_PrintCharXY(unsigned int x, unsigned int y, u32 c)
142140
{
143141
u8 row, col, width;
144-
const u8 *offset = char_offset(c, &width);
142+
u8 font[CHAR_BUF_SIZE];
143+
const u8 *offset;
144+
offset = font;
145+
char_read(font, c, &width);
145146
if (! offset || ! width) {
146147
printf("Could not locate character U-%04x\n", (int)c);
147148
return;
@@ -178,17 +179,17 @@ void close_font()
178179

179180
u8 open_font(unsigned int idx)
180181
{
181-
char font[20];
182+
char fontname[20];
182183
close_font();
183184
if (! idx) {
184185
cur_str.font.idx = 0;
185186
return 1;
186187
}
187-
sprintf(font, "media/%s.fon", FontNames[idx-1]);
188+
sprintf(fontname, "media/%s.fon", FontNames[idx-1]);
188189
finit(&FontFAT, "media");
189-
cur_str.font.fh = fopen2(&FontFAT, font, "rb");
190+
cur_str.font.fh = fopen2(&FontFAT, fontname, "rb");
190191
if (! cur_str.font.fh) {
191-
printf("Couldn't open font file: %s\n", font);
192+
printf("Couldn't open font file: %s\n", fontname);
192193
return 0;
193194
}
194195
setbuf(cur_str.font.fh, 0);
@@ -199,19 +200,19 @@ u8 open_font(unsigned int idx)
199200
return 0;
200201
}
201202
cur_str.font.idx = idx;
202-
idx = 0;
203-
u8 *f = (u8 *)font;
203+
int range_idx = 0;
204+
u8 buf[4];
204205
while(1) {
205-
if (fread(f, 4, 1, cur_str.font.fh) != 1) {
206+
if (fread(buf, 4, 1, cur_str.font.fh) != 1) {
206207
printf("Failed to parse font range table\n");
207208
fclose(cur_str.font.fh);
208209
cur_str.font.fh = NULL;
209210
return 0;
210211
}
211-
u16 start_c = f[0] | (f[1] << 8);
212-
u16 end_c = f[2] | (f[3] << 8);
213-
cur_str.font.range[idx++] = start_c;
214-
cur_str.font.range[idx++] = end_c;
212+
u16 start_c = buf[0] | (buf[1] << 8);
213+
u16 end_c = buf[2] | (buf[3] << 8);
214+
cur_str.font.range[range_idx++] = start_c;
215+
cur_str.font.range[range_idx++] = end_c;
215216
if (start_c == 0 && end_c == 0)
216217
break;
217218
}

0 commit comments

Comments
 (0)