ESP8266 ILI9341 display support code with printf sources, wire-frame viewer and custom fonts  1.0
ESP8266ILI9341DisplayProject
bdffont2c.c
Go to the documentation of this file.
1 
2 
37 #include <stdio.h>
38 #include <stdlib.h>
39 #include <unistd.h>
40 #include <ctype.h>
41 #include <string.h>
42 #include <limits.h>
43 #include <errno.h>
44 #include <sys/types.h>
45 #include <sys/stat.h>
46 #include <unistd.h>
47 #include <stdint.h>
48 
49 #include "font.h"
50 #include "bdffontutil.h"
51 
57 void usage(char *prog)
58 {
59  printf("%s: [-o font_output_file ] [ -l lower_bound ] [ -u upper_bound ] [ -preview ] [ -Preview ] bdf files...\n",
60  prog);
61  printf(" -o: output file containg C STructures of converted font data\n");
62  printf(" -u: limit Glyph processing above limit\n");
63  printf(" -l: limit Glyph processing below limit\n");
64  printf(" -p 1 Preview font bitmap data - only parts with 1 bits set\n");
65  printf(" -p 2 Preview Full font bitmap data - everything inside font bounding box\n");
66  printf(" -p 3 Preview Proportional font bitmap data and fixed - everything inside font bounding box\n");
67  printf(" -f convert any font to fixed size bitmap - everything inside font bounding box\n");
68  printf(" -s Compact font to smallest bounding box\n");
69  printf(" Notes: modifies or creates font specs\n");
70  printf(" bdf files...: One of more BDF format font files\n");
71 
72 }
73 
91 int main(int argc, char *const argv[])
92 {
93  int i;
94  struct stat buf;
95  _font fx;
96  char *name, *cfonts, *ptr;;
97  // Dump font ascii preview
98  int font_preview = 0;
99  int font_adjust_full = 0;
100  int font_adjust_small = 0;
101  // Glyph code of first font to process - we ignore fonts less then this
102  int lower_bound = 32;
103  // Glyph code of last font to process - we ignore fonts greater then this
104  int upper_bound = 127;
105 
106  // We always emit fonts specs and info parts
107  // These flags just determin if we emit them in the converted font file
108  int fontinfo_f = 0;
109  int fontspecs_f = 0;
110 
111  char str[MAXLINE];
112 
113  int bdfind = 0;
114  int find = 0;
115  FILE *FI;
116 
117  FILE *FO = NULL;
118  cfonts = NULL;
119 
120  for(i=1;i<argc;++i)
121  {
122  ptr = argv[i];
123  if(*ptr == '-')
124  {
125  ptr++;
126  if(*ptr == 'o')
127  {
128  ++ptr;
129  if(*ptr)
130  {
131  cfonts = ptr;
132  }
133  else
134  {
135  cfonts = argv[++i];
136 
137  }
138  continue;
139  }
140  if(*ptr == 'l')
141  {
142  ++ptr;
143  if(*ptr)
144  {
145  lower_bound = atoi(ptr);
146  }
147  else
148  {
149  lower_bound = atoi(argv[++i]);
150  }
151  continue;
152  }
153  if(*ptr == 'u')
154  {
155  ++ptr;
156  if(*ptr)
157  {
158  upper_bound = atoi(ptr);
159  }
160  else
161  {
162  upper_bound = atoi(argv[++i]);
163  }
164  continue;
165  }
166  if(*ptr == 'p')
167  {
168  ++ptr;
169  if(*ptr)
170  {
171  font_preview = atoi(ptr);
172  }
173  else
174  {
175  font_preview = atoi(argv[++i]);
176  }
177  continue;
178  }
179  if(*ptr == 'f')
180  {
181  ++ptr;
182  font_adjust_full = 1;
183  continue;
184  }
185  if(*ptr == 's')
186  {
187  ++ptr;
188  font_adjust_small = 1;
189  continue;
190  }
191  }
192  else
193  {
194  if(find < MAXFONTS)
195  {
196  if(stat(argv[i], (struct stat *) &buf) != 0)
197  {
198  fprintf(stderr,"File:[%s] missing, skipping\n", argv[i]);
199  continue;
200  }
201  fnames[find++] = stralloc(argv[i]);
202  }
203  }
204  }
205 
206  if(font_adjust_full && font_adjust_small)
207  {
208  fprintf(stderr,"Can not have -f and -s set at the same time\n");
209  exit(1);
210  }
211 
212  if(!find)
213  {
214  usage(basename(argv[0]));
215  fprintf(stderr,"No font files specified\n");
216  exit(1);
217  }
218 
219  if(cfonts == NULL)
220  {
221  FO = stdout;
222  cfonts = "stdout";
223  }
224  else
225  {
226  if(stat(cfonts, (struct stat *) &buf) == 0)
227  {
228  fprintf(stderr,"Font file: %s - exists - not overwritting\n", cfonts);
229  exit(1);
230 
231  }
232  FO = fopen(cfonts,"w");
233  if(!FO)
234  {
235  fprintf(stderr,"Can't open: %s\n", cfonts);
236  exit(1);
237  }
238  }
239 
240 
241  bdfind = 0;
242  for(i=0;i<find;++i)
243  {
244  int ret;
245  name = fnames[i];
246 
247  if(!ReadBdf(name, &fx, lower_bound, upper_bound))
248  {
249  FreeFont(&fx);
250  fprintf(stderr,"Can't open:[%s]\n", name);
251  continue;
252  }
253  ret = FindFontName(fx.info->STRUCT_NAME);
254  if(ret != -1)
255  {
256  fprintf(stderr,"Duplicate font:[%s] in file:[%s], skipping\n", fx.info->STRUCT_NAME, name);
257  fprintf(stderr,"Exists in font:[%s] in file:[%s]\n\n",
258  BDFnames[ret].structname,
259  BDFnames[ret].filename
260  );
261  FreeFont(&fx);
262  continue;
263  }
264 
265 
266  if(font_adjust_full)
267  {
268  //fprintf(stderr,"Bytes:%d\n", fx.Bytes);
269  FontAdjustFull(&fx);
270  fontspecs_f = 0;
271  //fprintf(stderr,"New Bytes:%d\n", fx.Bytes);
272  }
273 
274  if(font_adjust_small)
275  {
276  //fprintf(stderr,"Bytes:%d\n", fx.Bytes);
277  FontAdjustSmall(&fx);
278  fontspecs_f = 1;
279  //fprintf(stderr,"New Bytes:%d\n", fx.Bytes);
280  }
281 
282  BDFnames[bdfind].filename = name;
283  BDFnames[bdfind].structname = stralloc(fx.info->STRUCT_NAME);
284 
285  ComputeGapSize(&fx);
286 
287  FontHeaderInfo(FO, &fx, argv[0], cfonts);
288 
289  if(fontinfo_f = 0)
290  {
291  fprintf(FO,"#ifndef FONTINFO\n");
292  fprintf(FO," #define FONTINFO\n");
293  fprintf(FO,"#endif\n");
294  }
295  if(fontspecs_f)
296  {
297  fprintf(FO,"#ifndef FONTSPECS\n");
298  fprintf(FO," #define FONTSPECS\n");
299  fprintf(FO,"#endif\n");
300  }
301 
302  Convert_Font2c(FO, &fx);
303  WriteFontBitsPreview ( FO, &fx, font_preview);
304  FreeFont(&fx);
305  ++bdfind;
306  }
307 
308  fprintf(FO, "\n\n\n");
309  fprintf(FO, "/* All cfonts */\n");
310  fprintf(FO, "_font *allfonts[%d] = {\n", bdfind+1);
311  for(i=0;i<bdfind;++i)
312  {
313  fprintf(FO, "\t&%s,\n", BDFnames[i].structname);
314  }
315  fprintf(FO, "\tNULL\n");
316  fprintf(FO, "};\n");
317 
318  if(FO != stdout)
319  fclose(FO);
320  return(0);
321 }
int ReadBdf(char *name, _font *font, int lower, int upper)
Read and parse a BDF file for specified font and character set range Fills fonts structures.
Definition: bdffontutil.c:790
char * fnames[MAXFONTS]
Definition: bdffontutil.c:53
void FreeFont(_font *font)
Free font data.
Definition: bdffontutil.c:554
par BDF font utils for BDF to C code converter Displays fonts generated by bdffont2c BDF to C code co...
MEMSPACE FILE * fopen(const char *path, const char *mode)
POSIX Open a file with path name and ascii file mode string.
Definition: posix.c:782
void FontHeaderInfo(FILE *out, _font *font, char *prog, char *target)
Write Font Header Information Copyright, font family, etc.
Definition: bdffontutil.c:323
#define MAXLINE
Definition: bdffontutil.h:40
FILE type structure.
Definition: posix.h:156
MEMSPACE int fprintf(FILE *fp, const char *format,...)
fprintf function Example user defined printf function using fputc for I/O This method allows I/O to d...
Definition: posix.c:2484
#define NULL
Definition: cpu.h:55
void WriteFontBitsPreview(FILE *out, _font *font, int preview)
Write all Font characters in a font as ASCII character comments.
Definition: bdffontutil.c:2040
int FindFontName(char *str)
Search for a font name.
Definition: bdffontutil.c:611
int main(int argc, char *const argv[])
convert BDF fonts to C structures
Definition: bdffont2c.c:91
void ComputeGapSize(_font *font)
Find a good gap size (inter-character spacing) for any font Currently we searching for the smallest w...
Definition: bdffontutil.c:1524
MEMSPACE int fclose(FILE *stream)
POSIX close a file stream.
Definition: posix.c:1239
POSIX stat structure.
Definition: posix.h:105
#define stdout
Definition: posix.h:270
void FontAdjustSmall(_font *font)
Ajust font to use smallest font bounding box for each font Can be used to converting large fixed font...
Definition: bdffontutil.c:1325
#define MAXFONTS
Definition: font.h:38
MEMSPACE int printf(const char *format,...)
#define stderr
Definition: posix.h:271
MEMSPACE char * basename(char *str)
POSIX Basename of filename.
Definition: posix.c:1446
void usage(char *prog)
Display Usage.
Definition: bdffont2c.c:57
MEMSPACE char * stralloc(char *str)
Allocate space for string.
Definition: stringsup.c:774
void Convert_Font2c(FILE *out, _font *font)
Convert font to C structure Writes header information, font specification, font bitmap.
Definition: bdffontutil.c:365
MEMSPACE int stat(char *name, struct stat *buf)
POSIX stat - get file status of named file.
Definition: posix.c:1344
_bdffile BDFnames[MAXFONTS]
Definition: bdffontutil.c:51
char * filename
Definition: font.h:45
void FontAdjustFull(_font *font)
Adjust font to full size with no offset.
Definition: bdffontutil.c:1240
MEMSPACE int atoi(const char *str)
Convert ASCII string to number in base 10.
Definition: mathio.c:281
Definition: font.h:70
char * structname
Definition: font.h:46