-
Notifications
You must be signed in to change notification settings - Fork 7
/
font.c
395 lines (339 loc) · 13.6 KB
/
font.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
/******************************************************************************
* Font Generator
* designed for GLCD with memory organized in vertical bytes
*
* Own character sets or symbols can easily be added:
* The structure of font data is compatible with Hagen Reddmanns FontEditor
* http://www.mikrocontroller.net/topic/99701#865331
* when using the attached template_simplefont.c and template_simplefont.h
* files, saving in uncompressed format and using font heights of multiples
* of 8.
*
* Fixed width fonts are treated as proportional fonts, but do not have a
* table for the width of each character (D'OH!)
*
* When using with other GLCDs, make sure the byte orientation of the LCDs
* memory matches the design of the ea-dogm series or link the LCD access
* functions (see header file) to functions converting the data.
*
* My thanks go to Hagen Reddmann for his Font Editor software
* (http://www.mikrocontroller.net/topic/99701#865331)
* and Benedikt K. for his various LCD fonts
* (http://www.mikrocontroller.net/topic/54860)
* as well as Oliver Schwaneberg for adding several new functions
*
* Author: Jan Michel (jan at mueschelsoft dot de)
* License: GNU General Public License, version 3
* Version: v0.94 October 2011
* ****************************************************************************
* New features in v0.94
* - Characters may now be bigger than 128 Byte and occupy more than 1024 Pixels each
* - Added 32px high digits (4 digits plus colon plus large spacing = 128px)
* New features in v0.93
* - output functions for long / int / float depend on global font setting
* (removing the two additional parameters made usage much easier I think)
* - fixed bug in font_proportional_16px regarding space
* - fixed symbol_16px
* New features in v0.92
* - font and style setting can be stored globally (lcd_set_font)
* - simple put functions: lcd_putc, lcd_putstr, lcd_putstr_P
* - added 24px high, 16px wide digit font
* - added underline to font styles
* New features in v0.91
* - text can be displayed in inverted mode
* - text can automatically wrap to the next line (if selected)
* - direct output of long / int / float
* - put_string and put_char functions now return the total width of the
* text in pixel
*****************************************************************************/
#include "font.h"
/******************************************************************************
* Global storage for easy-to-use putc functions
*****************************************************************************/
FONT_P global_font_select;
uint8_t global_font_style;
/******************************************************************************
* Stores the default font type and style in a global variable
* font - Font identifier
* style - Style Modifier
*/
inline void lcd_set_font(FONT_P font, uint8_t style){
global_font_select = font;
global_font_style = style;
}
/******************************************************************************
* Helper Functions to find, retrieve and display characters
*****************************************************************************/
/******************************************************************************
* Loads the pointer to the selected fonts data
*/
inline PGM_P font_data(FONT_P font) {
PGM_P tmp;
if (sizeof(tmp) == 2)
tmp = (PGM_P)pgm_read_word(&(font->data));
else
memcpy_P((char*)&tmp,&(font->data),sizeof(tmp));
return tmp;
}
/******************************************************************************
* Loads the pointer to the width table for the selected font
*/
inline PGM_P font_widthtable(FONT_P font) {
PGM_P tmp;
if (sizeof(tmp) == 2)
tmp = (PGM_P)pgm_read_word(&(font->widthtable));
else
memcpy_P((char*)&tmp,&(font->widthtable),sizeof(tmp));
return tmp;
}
/******************************************************************************
* Loads the height (in bytes) of the given font
*/
inline uint8_t font_get_height_bytes(FONT_P font) {
FONT_P tmp = font;
uint8_t t = pgm_read_byte(&tmp->height);
return (((uint8_t)(t-1)>>3)+1);
}
/******************************************************************************
* Get the number of the character in the given font
*/
inline int16_t font_get_char_number(FONT_P font, char character) {
FONT_P tmp = font;
if (character > pgm_read_byte(&tmp->lastchar))
return -1;
uint8_t first = pgm_read_byte(&tmp->firstchar);
if (character < first)
return -1;
return character - first;
}
/******************************************************************************
* Read the width of the selected character from the font width table
*/
inline uint8_t font_get_char_width(FONT_P font, char character) {
PGM_P table = font_widthtable(font);
if (table)
return pgm_read_byte(table+font_get_char_number(font,character));
else
return pgm_read_byte(&font->width);
}
/******************************************************************************
* Calculate the pointer to the requested character inside the Flash ROM
*/
PGM_P font_get_char_position(FONT_P font, char character) {
uint16_t ret = 0;
int16_t charnum_ret = font_get_char_number(font, character);
uint8_t charnum = charnum_ret;
PGM_P base = font_widthtable(font);
if (charnum_ret < 0) //char not found
return 0;
if(base == 0) //fixed width
return font_data(font) + (uint16_t)charnum * (uint8_t)(font_get_height_bytes(font) * font_get_char_width(font,character));
if (charnum) //proportional width
while(charnum--)
ret += (uint8_t) pgm_read_byte(base++);
return (font_data(font))+ret*font_get_height_bytes(font);
}
/******************************************************************************
* Doubles the bytes of either the upper of lower nibble of the given byte
* part = 0: abcdefgh -> eeffgghh
* part = 1: abcdefgh -> aabbccdd
* Used for double height font
*/
inline unsigned char double_bits(uint8_t part, char c) {
uint8_t t = 0;
if (part) c = c>>4;
if (c & 0x08) t = 0xC0;
if (c & 0x04) t |= 0x30;
if (c & 0x02) t |= 0x0C;
if (c & 0x01) t |= 0x03;
return t;
}
/******************************************************************************
* Output functions for characters and strings
*****************************************************************************/
/******************************************************************************
* Outputs a character on the display, using the given font and style
*/
uint8_t lcd_put_char(FONT_P font, uint8_t style, char character) {
uint16_t i;
uint8_t row = 0; //current row of char
#ifdef LCD_DOUBLE_PIXEL
uint8_t hc = 1; //height forced
#else
uint8_t hc = (style & DOUBLE_HEIGHT)?1:0; //height changed
#endif
uint8_t wc = (style & DOUBLE_WIDTH)?1:0; //width changed
uint8_t ul = (style & UNDERLINE)?0x80:0x00; //underline
uint8_t inv = (style & INVERT)?0xFF:0; //inverted
uint8_t spc = (style & SPACING)?3:1; //spacing
uint8_t tmp;
//load information about character
uint8_t char_width = font_get_char_width(font,character);
uint8_t font_height = font_get_height_bytes(font);
uint8_t free_space = spc;
PGM_P tableposition = font_get_char_position(font,character);
//final size of character
uint8_t char_final_width = (uint8_t)(char_width+free_space) << wc;
uint8_t char_final_height = (uint8_t)font_height << hc;
//check for avail. space on display
if ((style & WRAP) && (LCD_CURRENT_COL() + char_final_width > LCD_WIDTH)) {
LCD_MOVE_TO(LCD_CURRENT_PAGE()+char_final_height,0);
if (character == ' ') return 0;
}
//write character
do {
for(i=(row>>hc); i<char_width*font_height; i+=font_height) {
tmp = pgm_read_byte(tableposition+i);
if(row == char_final_height-1)
tmp |= ul;
if(hc)
tmp = double_bits((row&1),tmp);
if(inv)
tmp = ~tmp;
LCD_WRITE(tmp);
if(wc)
LCD_WRITE(tmp);
}
if (free_space) {
uint8_t c = inv;
if(row == char_final_height-1) {
c ^= ul;
if(hc)
c ^= ul>>1;
}
for(uint8_t x = free_space<<wc; x>0;x--) {
LCD_WRITE(c);
}
}
LCD_MOVE(1,-char_final_width);
} while (++row < char_final_height);
//move cursor to upper right corner of character
LCD_MOVE(-char_final_height,char_final_width);
return char_final_width;
}
/******************************************************************************
* Outputs a string on the display, loading it from the program memory,
* using the given font and style
*/
uint16_t lcd_put_string_P(FONT_P font, uint8_t style, PGM_P str) {
unsigned char t;
uint16_t length = 0;
while((t = pgm_read_byte(str++)))
length += lcd_put_char(font,style,t);
return length;
}
/******************************************************************************
* Outputs a string on the display, using the given font and style
*/
uint16_t lcd_put_string(FONT_P font, uint8_t style, char* str) {
unsigned char t;
uint16_t length = 0;
while((t = *str++))
length += lcd_put_char(font,style,t);
return length;
}
/******************************************************************************
* Outputs a string on the display, using the given font and style, reading
* length characters from the memory
*/
uint16_t lcd_put_string_length(FONT_P font, uint8_t style, char* str, uint8_t length) {
unsigned char t;
uint16_t total_len = 0;
for(t=0;t<length;t++)
total_len += lcd_put_char(font,style,*str++);
return total_len;
}
/******************************************************************************
* Outputs a string on the display, using the given font and style, reading
* the string from program memory. The position of the string on the display
* is selected by page / col.
*/
uint16_t lcd_put_string_xy_P(FONT_P font, uint8_t style, PGM_P str,uint8_t page, uint8_t col) {
LCD_MOVE_TO(page,col);
return lcd_put_string_P(font,style,str);
}
/******************************************************************************
* Outputs a string on the display, using the given font and style, reading
* the string from main memory. The position of the string on the display
* is selected by page / col.
*/
uint8_t lcd_put_char_xy(FONT_P font, uint8_t style, char character, uint8_t page, uint8_t col) {
LCD_MOVE_TO(page,col);
return lcd_put_char(font,style,character);
}
/******************************************************************************
* Outputs a character on the display, using the global font and style
*/
uint8_t lcd_putc(char c) {
return lcd_put_char(global_font_select, global_font_style, c);
}
/******************************************************************************
* Outputs a character on the display, using the global font and style
*/
uint8_t lcd_putc_xy(char c, uint8_t page, uint8_t col) {
return lcd_put_char_xy(global_font_select, global_font_style, c, page, col);
}
/******************************************************************************
* Outputs a string on the display, using the global font and style
*/
uint16_t lcd_putstr(char* str) {
return lcd_put_string(global_font_select, global_font_style, str);
}
/******************************************************************************
* Outputs a string stored in program memory on the display, using the global
* font and style
*/
uint16_t lcd_putstr_P(PGM_P str) {
return lcd_put_string_P(global_font_select, global_font_style, str);
}
/******************************************************************************
* Outputs a string on the display, using the global font and style at the
* given position
*/
uint16_t lcd_putstr_xy_P(PGM_P str, uint8_t page, uint8_t col) {
return lcd_put_string_xy_P(global_font_select, global_font_style, str, page, col);
}
#if INCLUDE_INTEGER_OUTPUT == 1
/******************************************************************************
* Outputs a 32bit signed integer on the display // Added by Olli S.
*/
uint16_t lcd_put_long (int32_t integer) {
char buffer[10];
ltoa(integer, buffer, 10);
return lcd_put_string(global_font_select, global_font_style, buffer);
}
/******************************************************************************
* Outputs a 16bit signed integer on the display // Added by Olli S.
*/
uint16_t lcd_put_int (int16_t integer) {
char buffer[10];
itoa(integer, buffer, 10);
return lcd_put_string(global_font_select, global_font_style, buffer);
}
/******************************************************************************
* Outputs a 16bit unsigned integer on the display // Added by Olli S.
*/
uint16_t lcd_put_uint (uint16_t integer) {
char buffer[10];
utoa(integer, buffer, 10);
return lcd_put_string(global_font_select, global_font_style, buffer);
}
/******************************************************************************
* Outputs a 8bit signed integer on the display
*/
uint16_t lcd_put_short (int8_t integer) {
char buffer[10];
itoa(integer, buffer, 10);
return lcd_put_string(global_font_select, global_font_style, buffer);
}
#endif
#if INCLUDE_FLOAT_OUTPUT == 1
/******************************************************************************
* Outputs a float on the display // Added by Olli S.
*/
uint16_t lcd_put_float (float fvalue) {
char buffer[10];
dtostrf(fvalue, 2, 1, buffer);
return lcd_put_string(global_font_select, global_font_style, buffer);
}
#endif