ESP8266 ILI9341 display support code with printf sources, wire-frame viewer and custom fonts  1.0
ESP8266ILI9341DisplayProject
font.c
Go to the documentation of this file.
1 
30 #include <stdint.h>
31 #include <stdarg.h>
32 #include <string.h>
33 #include <math.h>
34 
35 
36 #ifdef USER_CONFIG
37 #include "user_config.h"
38 #endif
39 
41 #ifndef MEMSPACE
42 #define MEMSPACE_FONT MEMSPACE
43 #endif
44 
45 #include "display/ili9341.h"
46 #include "display/font.h"
47 
54 #include "fonts.h"
55 
56 // All the fonts - defined in fonts.h
57 extern _font *allfonts[];
58 
59 int font_max()
60 {
61  int i;
62  for(i=0;allfonts[i];++i)
63  ;
64  return(i);
65 }
66 
70 int font_H(int font)
71 {
72 // check font
73  _font *z = allfonts[font];
74  return(z->Height);
75 }
76 
80 int font_W(int font)
81 {
82 // check font
83  _font *z = allfonts[font];
84  return(z->Width);
85 }
86 
87 
88 
94 int font_attr(window *win, int c, _fontc *f)
95 {
96  int offset;
97  int num;
98  unsigned char *ptr;
99 
100 // check font
101  _font *z = allfonts[win->font];
102  _fontspecs s;
103 
104  num = c - z->First;
105  if(num < 0 || num >= z->Glyphs)
106  return(-1);
107 
108  f->ptr = z->bitmap;
109 
110 // If we have font specifications defined and included we can use them.
111 // Notes: Normally for small fixed fonts we do not want to included them.
112 // However; if the font is large we can define just the active part of
113 // the character to reduce the overall size.
114 
115  if(z->specs)
116  {
117  f->Width = z->Width;
118  f->Height = z->Height;
119 
120 // Copy the full font specification into ram for easy access
121 // This does not use much memory as it does not include the bitmap itself
122 // This method avoids memory alignment access errors on the ESP8266.
123 
124  cpy_flash((uint8_t *)&(z->specs[num]), (uint8_t *)&s,sizeof(_fontspecs));
125 
126  // Fonts Width,Height,X,Y
127  f->w = s.Width;
128  f->h = s.Height;
129  f->x= s.X;
130  f->y= s.Y;
131 
132  // Bitmap offset
133  offset = s.Offset;
134  f->ptr += offset;
135 
136 
137  // The user may override the fixed variable font flag
138  // This reduces the width of fixed fonts to just the active pixels.
139  f->flags = win->flags;
140 
141  // Skip is the combined width, and an optional character spacing gap
142  // Some characters have no active size (like the space character )
143  // so we just use the master font width and gap
144  if( !(f->flags & FONT_VAR) || !f->w )
145  f->skip = z->Width + z->gap;
146  else
147  f->skip = f->x + f->w + z->gap; // Include the X offset, Width and Gap
148  }
149 
150  else
151  {
152  // No Specifications, therefore the font must be fixed.
153  // We create one using the master font size spec
154  // There are no proportional options here.
155  f->Width = z->Width;
156  f->Height = z->Height;
157 
158  f->w = z->Width;
159  f->h = z->Height;
160  f->x = 0;
161  f->y = 0;
162 
163  // FIXME - fixed fonts without specs have no proportional modes to use
164  f->flags = win->flags;
165 
166  f->skip = z->Width + z->gap;
167 
168  // Each bitmap is a bit array w by h in size without any padding
169  // except at the end of the array which is rounded to the next byte boundary.
170  offset = ((z->Width * z->Height)+7)/8; /* round to byte boundary */
171  f->ptr += (offset * num);
172 
173  }
174 
175  // FIXME
176  // This zero size skip test should never be needed - for now we increase to 1..
177  if(!f->skip)
178  f->skip++;
179 // =====================================
180 
181 #if ILI9341_DEBUG & 2
182  printf("c: %02x font:%d w:%d h:%d x:%d y:%d skip:%d, W:%d, H:%d\r\n",
183  0xff & c, 0xff & win->font, f->w, f->h, f->x, f->y, f->skip, f->Width, f->Height);
184 #endif
185  return(win->font);
186 }
187 
188 
201 {
202  _fontc f;
203  int ret;
204  int yskip;
205 
206  ret = font_attr(win, c, &f);
207  if(ret < 0)
208  return;
209 
210 // process wrapping - will the character fit ?
211  if((win->xpos + f.skip - 1) >= win->w)
212  {
213  if(win->flags & WRAP_H)
214  {
215  win->ypos += f.Height;
216  win->xpos = 0;
217  }
218  else
219  {
220  // TODO H scroll
221  return;
222  }
223  }
224 
225  // Will the character fit ?
226  if((win->ypos+f.Height-1) >= win->h)
227  {
228  if(win->flags & WRAP_V)
229  {
230  win->ypos = 0;
231  }
232  else
233  {
234  win->ypos -= f.Height;
235  tft_Vscroll(win,f.Height);
236  }
237  }
238 
239 // Conditionally clear the character area - not needed for full size fixed fonts..
240 // If the character is not full size then pre-clear the full font bit array
241 // (saves the more complex tests of clearing additional areas around the active font)
242 // Note: We use skip instead of Width because of the additional gap that must also
243 // be cleared..
244 
245 // FIXME we should do this in two parts - font area and then the gap area.
246 // Since we always want to clear the gap - but not always the font - when fixed.
247 
248 // Optionally clear the font area, then the gap
249  if(f.h != f.Height || f.w != f.skip || f.x != 0 || f.y != 0)
250  tft_fillRectWH(win, win->xpos, win->ypos, f.skip, f.Height, win->bg);
251 
252 // This can happen for characters with no active pixels like the space character.
253  if(!f.h || !f.w)
254  {
255  win->xpos += f.skip;
256  return;
257  }
258 
259 // Top of bit bounding box ( first row with a 1 bit in it)
260  yskip = f.Height - (f.y+f.h);
261 
262 // Write the font to the screen
263  tft_bit_blit(win, f.ptr, win->xpos+f.x, win->ypos+yskip, f.w, f.h);
264 
265  // skip is the offset to the next character
266  win->xpos += f.skip;
267 }
int16_t w
Definition: ili9341.h:40
#define FONT_VAR
Definition: ili9341.h:78
int16_t h
Definition: ili9341.h:41
void tft_bit_blit(window *win, uint8_t *ptr, int16_t x, int16_t y, int16_t w, int16_t h)
BLIT functions
Definition: ili9341.c:234
uint16_t flags
Definition: ili9341.h:43
Master include file for project Includes all project includes and defines here.
uint16_t bg
Definition: ili9341.h:45
void tft_fillRectWH(window *win, int16_t x, int16_t y, int16_t w, int16_t h, uint16_t color)
Partial window Fill with color We clip the window to the current view.
Definition: ili9341.c:627
int8_t w
Definition: font.h:93
int16_t xpos
Definition: ili9341.h:36
void cpy_flash(uint8_t *src, uint8_t *dest, int size)
Copy data from Flash to Ram Uses flash_read8() to avoid alighnment problems.
Definition: flash.c:89
int8_t Width
Definition: font.h:97
uint8_t * bitmap
Definition: font.h:84
int8_t skip
Definition: font.h:99
int8_t h
Definition: font.h:94
int8_t flags
Definition: font.h:92
uint8_t Width
Definition: font.h:52
int8_t y
Definition: font.h:96
#define WRAP_H
Definition: ili9341.h:79
uint8_t Height
Definition: font.h:76
void tft_drawChar(window *win, uint8_t c)
Display a character and optionally wrap the graphic cursor.
Definition: font.c:200
int8_t x
Definition: font.h:95
int Offset
Definition: font.h:51
int8_t Y
Definition: font.h:55
_fontspecs * specs
Definition: font.h:85
uint8_t * ptr
Definition: font.h:100
int font_max()
Definition: font.c:59
#define WRAP_V
Definition: ili9341.h:80
uint16_t font
Definition: ili9341.h:42
int8_t X
Definition: font.h:54
int16_t ypos
Definition: ili9341.h:37
int font_W(int font)
Get font Width used for character to character spacing.
Definition: font.c:80
_font * allfonts[]
Include the Generated Font table The generated tables always include Font specifications: width...
Definition: fonts.h:14699
_font font
uint8_t Height
Definition: font.h:53
void tft_Vscroll(window *win, int dir)
Scroll window up by dir lines We start at the top of the window and move down.
Definition: ili9341.c:903
int16_t First
Definition: font.h:74
MEMSPACE int printf(const char *format,...)
uint8_t Width
Definition: font.h:75
Definition: ili9341.h:34
int8_t Height
Definition: font.h:98
ili9341 driver inspired by Adafruit ili9341 code All code in this file has been rewritten by Mike Gor...
int font_attr(window *win, int c, _fontc *f)
Get font attributes for a font.
Definition: font.c:94
int16_t Glyphs
Definition: font.h:72
int8_t gap
Definition: font.h:81
unsigned char uint8_t
Definition: send.c:17
Definition: font.h:89
int font_H(int font)
Get font height used for line to line spacing.
Definition: font.c:70
Definition: font.h:70