Skip to content

Commit aa32d71

Browse files
authored
Move font code to a separate file under screen/ (#612)
* Move font code to a separate file under screen/.
1 parent 3aba833 commit aa32d71

3 files changed

Lines changed: 188 additions & 133 deletions

File tree

src/screen/320x240x16/lcd_string.c

Lines changed: 32 additions & 133 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,7 @@
1414
*/
1515
#include "common.h"
1616
#include "gui/gui.h"
17-
18-
static FATFS FontFAT;
17+
#include "screen/font.h"
1918

2019
/*
2120
* The font 'font_table' begins with a list of u24 values which represent
@@ -46,95 +45,19 @@ static FATFS FontFAT;
4645
*/
4746
//#define LINE_SPACING 2 // move to _gui.h as devo10's line spacing is different from devo8's
4847
#define CHAR_SPACING 1
49-
#define CHAR_BUF_SIZE 80
50-
#define RANGE_TABLE_SIZE 20
5148
#define NUM_FONTS 10
5249

53-
#define HEIGHT(x) x.height
5450
#define FONT_NAME_LEN 9
5551
char FontNames[NUM_FONTS][FONT_NAME_LEN];
5652

57-
struct font_def
58-
{
59-
u8 idx;
60-
FILE *fh;
61-
u8 height; /* Character height for storage */
62-
u16 range[2 * (RANGE_TABLE_SIZE + 1)]; /* Array containing the ranges of supported characters */
63-
};
6453
static struct {
65-
struct font_def font;
54+
u8 font_idx;
6655
unsigned int x_start;
6756
unsigned int x;
6857
unsigned int y;
6958
u16 color;
7059
} cur_str;
7160

72-
u8 FONT_GetFromString(const char *value)
73-
{
74-
int i;
75-
for (i = 0; i < NUM_FONTS; i++) {
76-
if (FontNames[i][0] == 0) {
77-
strlcpy(FontNames[i], value, FONT_NAME_LEN);
78-
return i + 1;
79-
}
80-
if(strcasecmp(FontNames[i], value) == 0) {
81-
return i + 1;
82-
}
83-
}
84-
printf("Unknown font: %s\n", value);
85-
return 0;
86-
}
87-
88-
u8 get_char_range(u32 c, u32 *begin, u32 *end)
89-
{
90-
u32 offset = 0;
91-
u32 pos = 5;
92-
u8 buf[6];
93-
u16 *range = cur_str.font.range;
94-
while(1) {
95-
if (range[0] == 0 && range[1] == 0)
96-
break;
97-
if (c >= range[0] && c <= range[1]) {
98-
pos += 3 * (offset + c - range[0]);
99-
} else {
100-
offset += range[1] + 1 - range[0];
101-
}
102-
range += 2;
103-
pos += 4;
104-
}
105-
fseek(cur_str.font.fh, pos, SEEK_SET);
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);
109-
return 1;
110-
}
111-
112-
void char_read(u8 *font, u32 c, u8 *width)
113-
{
114-
u32 begin;
115-
u32 end;
116-
117-
u8 row_bytes = ((cur_str.font.height - 1) / 8) + 1;
118-
get_char_range(c, &begin, &end);
119-
*width = (end - begin) / row_bytes;
120-
fseek(cur_str.font.fh, begin, SEEK_SET);
121-
if (end - begin > CHAR_BUF_SIZE) {
122-
printf("Character '%04d' is larger than allowed size\n", (int)c);
123-
end = begin + (CHAR_BUF_SIZE / row_bytes) * row_bytes;
124-
*width = (end - begin) / row_bytes;
125-
}
126-
fread(font, end - begin, 1, cur_str.font.fh);
127-
}
128-
129-
u8 get_width(u32 c)
130-
{
131-
u32 begin;
132-
u32 end;
133-
134-
u8 row_bytes = ((cur_str.font.height - 1) / 8) + 1;
135-
get_char_range(c, &begin, &end);
136-
return (end - begin) / row_bytes;
137-
}
13861

13962
void LCD_PrintCharXY(unsigned int x, unsigned int y, u32 c)
14063
{
@@ -148,13 +71,13 @@ void LCD_PrintCharXY(unsigned int x, unsigned int y, u32 c)
14871
return;
14972
}
15073
// Check if the requested character is available
151-
LCD_DrawStart(x, y, x + width - 1, y + HEIGHT(cur_str.font) - 1, DRAW_NWSE);
74+
LCD_DrawStart(x, y, x + width - 1, y + get_height() - 1, DRAW_NWSE);
15275
for (col = 0; col < width; col++)
15376
{
15477
const u8 *data = offset++;
15578
u8 bit = 0;
15679
// Data is right aligned,adrawn top to bottom
157-
for (row = 0; row < HEIGHT(cur_str.font); ++row)
80+
for (row = 0; row < get_height(); ++row)
15881
{
15982
if (bit == 8) {
16083
data = offset++;
@@ -169,69 +92,45 @@ void LCD_PrintCharXY(unsigned int x, unsigned int y, u32 c)
16992
LCD_DrawStop();
17093
}
17194

172-
void close_font()
173-
{
174-
if(cur_str.font.fh) {
175-
fclose(cur_str.font.fh);
176-
cur_str.font.fh = NULL;
177-
}
178-
}
179-
180-
u8 open_font(unsigned int idx)
95+
u8 FONT_GetFromString(const char *value)
18196
{
182-
char fontname[20];
183-
close_font();
184-
if (! idx) {
185-
cur_str.font.idx = 0;
186-
return 1;
187-
}
188-
sprintf(fontname, "media/%s.fon", FontNames[idx-1]);
189-
finit(&FontFAT, "media");
190-
cur_str.font.fh = fopen2(&FontFAT, fontname, "rb");
191-
if (! cur_str.font.fh) {
192-
printf("Couldn't open font file: %s\n", fontname);
193-
return 0;
194-
}
195-
setbuf(cur_str.font.fh, 0);
196-
if(fread(&cur_str.font.height, 1, 1, cur_str.font.fh) != 1) {
197-
printf("Failed to read height from font\n");
198-
fclose(cur_str.font.fh);
199-
cur_str.font.fh = NULL;
200-
return 0;
201-
}
202-
cur_str.font.idx = idx;
203-
int range_idx = 0;
204-
u8 buf[4];
205-
while(1) {
206-
if (fread(buf, 4, 1, cur_str.font.fh) != 1) {
207-
printf("Failed to parse font range table\n");
208-
fclose(cur_str.font.fh);
209-
cur_str.font.fh = NULL;
210-
return 0;
97+
int i;
98+
for (i = 0; i < NUM_FONTS; i++) {
99+
if (FontNames[i][0] == 0) {
100+
strlcpy(FontNames[i], value, FONT_NAME_LEN);
101+
return i + 1;
102+
}
103+
if(strcasecmp(FontNames[i], value) == 0) {
104+
return i + 1;
211105
}
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;
216-
if (start_c == 0 && end_c == 0)
217-
break;
218106
}
219-
return 1;
107+
printf("Unknown font: %s\n", value);
108+
return 0;
220109
}
221110

222111
u8 LCD_SetFont(unsigned int idx)
223112
{
224113
u8 old = LCD_GetFont();
225114
if (old == idx)
226115
return old;
227-
if (! open_font(idx))
228-
open_font(old);
116+
if (idx == 0) {
117+
close_font();
118+
cur_str.font_idx = 0;
119+
return 0;
120+
} else if (! open_font(FontNames[idx - 1])){
121+
if (old == 0)
122+
close_font();
123+
else
124+
open_font(FontNames[old - 1]);
125+
}
126+
else
127+
cur_str.font_idx = idx;
229128
return old;
230129
}
231130

232131
u8 LCD_GetFont()
233132
{
234-
return cur_str.font.idx;
133+
return cur_str.font_idx;
235134
}
236135

237136
void LCD_SetXY(unsigned int x, unsigned int y)
@@ -260,28 +159,28 @@ void LCD_PrintChar(u32 c)
260159
{
261160
if(c == '\n') {
262161
cur_str.x = cur_str.x_start;
263-
cur_str.y += HEIGHT(cur_str.font) + LINE_SPACING;
162+
cur_str.y += get_height() + LINE_SPACING;
264163
} else {
265164
LCD_PrintCharXY(cur_str.x, cur_str.y, c);
266165
cur_str.x += get_width(c) + CHAR_SPACING;
267166
}
268167
}
269168

270169
void LCD_GetCharDimensions(u32 c, u16 *width, u16 *height) {
271-
*height = HEIGHT(cur_str.font);
170+
*height = get_height();
272171
*width = get_width(c);
273172
}
274173

275174
void LCD_GetStringDimensions(const u8 *str, u16 *width, u16 *height) {
276175
int line_width = 0;
277-
*height = HEIGHT(cur_str.font);
176+
*height = get_height();
278177
*width = 0;
279178
//printf("String: %s\n", str);
280179
while(*str) {
281180
u32 ch;
282181
str = (const u8 *)utf8_to_u32((const char *)str, &ch);
283182
if(ch == '\n') {
284-
*height += HEIGHT(cur_str.font) + LINE_SPACING;
183+
*height += get_height() + LINE_SPACING;
285184
if(line_width > *width)
286185
*width = line_width;
287186
line_width = 0;

src/screen/font.c

Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
/*
2+
This project is free software: you can redistribute it and/or modify
3+
it under the terms of the GNU General Public License as published by
4+
the Free Software Foundation, either version 3 of the License, or
5+
(at your option) any later version.
6+
7+
Deviation is distributed in the hope that it will be useful,
8+
but WITHOUT ANY WARRANTY; without even the implied warranty of
9+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10+
GNU General Public License for more details.
11+
12+
You should have received a copy of the GNU General Public License
13+
along with Deviation. If not, see <http://www.gnu.org/licenses/>.
14+
*/
15+
#include "common.h"
16+
#include "gui/gui.h"
17+
#include "font.h"
18+
19+
static FATFS FontFAT;
20+
21+
#define RANGE_TABLE_SIZE 20
22+
23+
static struct font_def
24+
{
25+
FILE *fh;
26+
u8 height; /* Character height for storage */
27+
u16 range[2 * (RANGE_TABLE_SIZE + 1)]; /* Array containing the ranges of supported characters */
28+
}font;
29+
30+
static u8 get_char_range(u32 c, u32 *begin, u32 *end)
31+
{
32+
u32 offset = 0;
33+
u32 pos = 5;
34+
u8 buf[6];
35+
u16 *range = font.range;
36+
while(1) {
37+
if (range[0] == 0 && range[1] == 0)
38+
break;
39+
if (c >= range[0] && c <= range[1]) {
40+
pos += 3 * (offset + c - range[0]);
41+
} else {
42+
offset += range[1] + 1 - range[0];
43+
}
44+
range += 2;
45+
pos += 4;
46+
}
47+
fseek(font.fh, pos, SEEK_SET);
48+
fread(buf, 6, 1, font.fh);
49+
*begin = buf[0] | (buf[1] << 8) | (buf[2] << 16);
50+
*end = buf[3] | (buf[4] << 8) | (buf[5] << 16);
51+
return 1;
52+
}
53+
54+
void char_read(u8 *fontbuf, u32 c, u8 *width)
55+
{
56+
u32 begin;
57+
u32 end;
58+
59+
u8 row_bytes = ((font.height - 1) / 8) + 1;
60+
get_char_range(c, &begin, &end);
61+
*width = (end - begin) / row_bytes;
62+
fseek(font.fh, begin, SEEK_SET);
63+
if (end - begin > CHAR_BUF_SIZE) {
64+
printf("Character '%04d' is larger than allowed size\n", (int)c);
65+
end = begin + (CHAR_BUF_SIZE / row_bytes) * row_bytes;
66+
*width = (end - begin) / row_bytes;
67+
}
68+
fread(fontbuf, end - begin, 1, font.fh);
69+
}
70+
71+
u8 get_width(u32 c)
72+
{
73+
u32 begin;
74+
u32 end;
75+
76+
u8 row_bytes = ((font.height - 1) / 8) + 1;
77+
get_char_range(c, &begin, &end);
78+
return (end - begin) / row_bytes;
79+
}
80+
81+
u8 get_height()
82+
{
83+
return font.height;
84+
}
85+
86+
void close_font()
87+
{
88+
if(font.fh) {
89+
fclose(font.fh);
90+
font.fh = NULL;
91+
}
92+
}
93+
94+
u8 open_font(const char* fontname)
95+
{
96+
char filename[20];
97+
close_font();
98+
99+
sprintf(filename, "media/%s.fon", fontname);
100+
finit(&FontFAT, "media");
101+
font.fh = fopen2(&FontFAT, filename, "rb");
102+
if (! font.fh) {
103+
printf("Couldn't open font file: %s\n", filename);
104+
return 0;
105+
}
106+
setbuf(font.fh, 0);
107+
if(fread(&font.height, 1, 1, font.fh) != 1) {
108+
printf("Failed to read height from font\n");
109+
fclose(font.fh);
110+
font.fh = NULL;
111+
return 0;
112+
}
113+
114+
int range_idx = 0;
115+
u8 buf[4];
116+
while(1) {
117+
if (fread(buf, 4, 1, font.fh) != 1) {
118+
printf("Failed to parse font range table\n");
119+
fclose(font.fh);
120+
font.fh = NULL;
121+
return 0;
122+
}
123+
u16 start_c = buf[0] | (buf[1] << 8);
124+
u16 end_c = buf[2] | (buf[3] << 8);
125+
font.range[range_idx++] = start_c;
126+
font.range[range_idx++] = end_c;
127+
if (start_c == 0 && end_c == 0)
128+
break;
129+
}
130+
return 1;
131+
}

0 commit comments

Comments
 (0)