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
5551char 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- };
6453static 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
13962void 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
222111u8 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
232131u8 LCD_GetFont ()
233132{
234- return cur_str .font . idx ;
133+ return cur_str .font_idx ;
235134}
236135
237136void 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
270169void 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
275174void 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 ;
0 commit comments