-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbdf2uc.cpp
784 lines (672 loc) · 20.9 KB
/
bdf2uc.cpp
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
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
/**
* @file bdf2uc.cpp
* @version 1.3
*
* @section License
* Copyright (C) 2014-2015, jeditekunum
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
*/
#define VERSION "1.3"
#define AUTHOR "jeditekunum"
#define SOURCE "https://github.com/jeditekunum/bdf2uc"
#include <unistd.h>
#include <stdlib.h>
#include <errno.h>
#include <iostream>
#include <iomanip>
#include <fstream>
#include "Bdf.hh"
Bdf bdf;
bool verbose = false;
bool compress = false;
unsigned int total_converted = 0;
unsigned int total_compressed = 0;
#define ATMEGA2560_ESCAPE 0x21 // 3 in a row
int
percent(int l, int r)
{
int result;
if (l < r)
result = -((int)(((r - l) / (float)r) * 100.0));
else
result = (int)(((l - r) / (float)r) * 100.0);
// std::cerr << "percent(" << l << "," << r << ")=" << result << std::endl;
return (result);
}
void
describe(std::ostream& out, Glyph::encoding_t i)
{
if (isprint(i))
out << "'" << (char)i << "' ";
else
{
switch (i)
{
case '\a':
out << "'\\a' ";
break;
case '\b':
out << "'\\b' ";
break;
case '\f':
out << "'\\f' ";
break;
case '\n':
out << "'\\n' ";
break;
case '\r':
out << "'\\r' ";
break;
case '\t':
out << "'\\t' ";
break;
case '\v':
out << "'\\v' ";
break;
default:
out << "'\\";
if (i < 8)
out << "0";
if (i < 64)
out << "0";
out << std::oct << (int)i << "' " << std::dec;
}
}
out << "(";
if (i < 10)
out << " ";
if (i < 100)
out << " ";
out << i;
out << ", 0x";
if (i < 16)
out << "0";
out << std::hex << i << std::dec;
out << ")";
}
void
output_hdr(std::ofstream& out, Bdf& bdf)
{
out << "/* BDF " << std::endl;
#if 0
out << bdf.comments();
#endif
out << "Font:\t\t" << bdf.font() << std::endl;
out << "Foundry:\t" << bdf.foundry() << std::endl;
out << "FamilyName:\t" << bdf.family_name() << std::endl;
out << "WeightName:\t" << bdf.weight_name() << std::endl;
out << "Slant:\t\t";
switch (bdf.slant())
{
case Bdf::ROMAN:
out << "R";
break;
case Bdf::ITALIC:
out << "I";
break;
case Bdf::OBLIQUE:
out << "O";
break;
}
out << std::endl;
out << "WidthName:\t" << bdf.setwidth_name() << std::endl;
out << "Spacing:\t";
switch (bdf.spacing())
{
case Bdf::CELL:
out << "C";
break;
case Bdf::MONOSPACE:
out << "M";
break;
case Bdf::PROPORTIONAL:
out << "P";
break;
}
out << std::endl;
out << "Copyright:\t" << bdf.copyright() << std::endl;
out << "*/" << std::endl << std::endl;
}
void
convert_glyph(Bdf& bdf, Glyph::encoding_t i)
{
if (bdf.glyph(i).converted_count())
return; // must be invalid glyph
if(bdf.glyph(i).valid())
{
// Cosa Font
for (unsigned int yb = 0; yb < BITS_TO_BYTES(bdf.bb_height()); yb++)
{
// out << "yb=" << yb << std::endl;
for (unsigned int x = 0; x < bdf.bb_width(); x++)
{
// out << " x=" << x;
unsigned int bits = bdf.bb_height() - (yb*8);
if (bits > 8)
bits = 8;
// out << ", bits=" << bits << std::endl;
Bitmap::byte_t byte = 0;
for (unsigned int bit = 0; bit < bits; bit++)
{
// out << " bit=" << bit;
unsigned short in_offset =
yb * (BITS_TO_BYTES(bdf.bb_width()) * 8) +
BITS_TO_BYTES(x+1) - 1 +
bit * BITS_TO_BYTES(bdf.bb_width());
// out << " in_offset=" << in_offset;
unsigned short right_shift = 7 - x % 8;
// out << " rs=" << right_shift;
Bitmap::byte_t data = bdf.glyph(i).input_byte(in_offset);
// out << " data=0x" << std::hex << (uint16_t)data << std::dec
// << " (0b" << (std::bitset<8>)data << ")";
byte |= ((data >> right_shift) & 0x1) << bit;
// out << " byte=0b" << (std::bitset<8>)byte << std::endl;
}
bdf.glyph(i).append_converted_byte(byte);
}
}
}
else
{
if (verbose)
std::cerr << "WARNING: Missing glyph for " << "0x" << std::hex << (i>>4) << (i&0xF) << std::dec << std::endl;
unsigned int amount = bdf.bb_width() * BITS_TO_BYTES(bdf.bb_height());
for (unsigned int j=0; j<amount; j++)
bdf.glyph(i).append_converted_byte(0);
}
total_converted += bdf.glyph(i).converted_count();
}
void
compress_glyph(Bdf& bdf, Glyph::encoding_t i)
{
// Atmega2560 interprets 0x21 0x21 0x21 sequence as an escape
unsigned int hex21 = 0;
if (!compress)
return;
if (bdf.glyph(i).compressed_count())
return; // must be invalid glyph
#if 0
// Pre-compression
std::cerr << "pre ";
for (unsigned int j=0; j<bdf.glyph(i).converted_count(); j++)
std::cerr << "0x"
<< std::hex << (bdf.glyph(i).converted_byte(j)>>4)
<< (bdf.glyph(i).converted_byte(j) & 0xF)
<< std::dec << ",";
std::cerr << std::endl;
#endif
unsigned char presentbits[BITS_TO_BYTES(bdf.glyph(i).converted_count())];
memset(presentbits, 0, BITS_TO_BYTES(bdf.glyph(i).converted_count()));
unsigned int present = 0;
for (unsigned int j = 0; j < bdf.glyph(i).converted_count(); j++)
{
if (bdf.glyph(i).converted_byte(j) != 0)
{
present++;
presentbits[j/8] |= (1 << (7-(j%8)));
}
}
for (unsigned int j = 0; j < BITS_TO_BYTES(bdf.glyph(i).converted_count()); j++)
if (presentbits[j] == ATMEGA2560_ESCAPE)
hex21++;
else
hex21 = 0;
if (hex21 > 0 && hex21 < 3)
{
// check previous character
if (i != bdf.first())
{
// other than first character
if (bdf.glyph(i).converted_byte(0) == ATMEGA2560_ESCAPE)
{
// lookback at previous if initial byte is ATMEGA2560_ESCAPE
if (bdf.glyph(i).compressed_count() > 1 &&
bdf.glyph(i).compressed_byte(1) == ATMEGA2560_ESCAPE)
{
// our first two are ATMEGA2560_ESCAPE so only have to look back 1
if (bdf.glyph(i-1).compressed_byte(bdf.glyph(i-1).compressed_count()-1) == ATMEGA2560_ESCAPE)
hex21 = 3; // force escaping
}
else
{
// look back 2
if (bdf.glyph(i-1).compressed_byte(bdf.glyph(i-1).compressed_count()-1) == ATMEGA2560_ESCAPE &&
bdf.glyph(i-1).compressed_byte(bdf.glyph(i-1).compressed_count()-2) == ATMEGA2560_ESCAPE)
hex21 = 3; // force escaping
}
}
}
}
if (hex21 >= 3)
bdf.glyph(i).escaped_bitset(true);
for (unsigned int j = 0; j < BITS_TO_BYTES(bdf.glyph(i).converted_count()); j++)
{
if (hex21 >= 3)
bdf.glyph(i).append_compressed_byte(0);
bdf.glyph(i).append_compressed_byte(presentbits[j]);
}
for (unsigned int j = 0; j < bdf.glyph(i).converted_count(); j++)
{
if (bdf.glyph(i).converted_byte(j) != 0)
bdf.glyph(i).append_compressed_byte(bdf.glyph(i).converted_byte(j));
}
total_compressed += bdf.glyph(i).compressed_count();
}
// Atmega2560 interprets 0x21 0x21 0x21 sequence as an escape
static unsigned int global_hex21 = 0;
void
output_glyph(std::ofstream& out, Bdf& bdf, char* class_name, Glyph::encoding_t i)
{
static bool seen_blank = false;
bool duplicate = false;
out << "/* ";
describe(out, i);
if (compress)
{
if (!seen_blank)
seen_blank = bdf.glyph(i).blank();
else
if (bdf.glyph(i).blank())
duplicate = true;
}
if (!compress || !duplicate)
{
out << " offset=" << bdf.glyph(i).output_offset();
out << " length=";
if (compress)
out << bdf.glyph(i).compressed_count();
else
out << bdf.glyph(i).converted_count();
if (compress)
out << " compressed="
<< -percent(bdf.glyph(i).compressed_count(), bdf.glyph(i).converted_count())
<< "%";
}
else
out << " duplicate";
if (!bdf.glyph(i).valid())
out << " !BDF";
out << " */" << std::endl;
if (duplicate)
return; // don't output duplicate blank
if(bdf.glyph(i).valid())
bdf.glyph(i).print(out, "/* ", " */");
unsigned int number;
if (compress)
{
number = bdf.glyph(i).compressed_count();
if (number == 0)
number = bdf.glyph(i).converted_count();
}
else
number = bdf.glyph(i).converted_count();
global_hex21 = 0;
for (unsigned int j=0; j<number; j++)
{
unsigned int byte;
if (compress)
{
if (bdf.glyph(i).compressed_count() > 0)
byte = bdf.glyph(i).compressed_byte(j);
else
byte = bdf.glyph(i).converted_byte(j);
}
else
byte = bdf.glyph(i).converted_byte(j);
if (byte == ATMEGA2560_ESCAPE) // !
{
if (++global_hex21 == 3)
{
if (class_name)
std::cerr << class_name << " ";
std::cerr << "WARNING: 3rd 0x21 replaced with 0x41 for BOARD_ATMEGA2560" << std::endl;
out << std::endl;
out << "#ifndef BOARD_ATMEGA2560" << std::endl;
}
}
else
global_hex21 = 0;
out << "0x" << std::hex;
if (byte < 16)
out << "0";
out << byte;
out << std::dec << ",";
if (global_hex21 == 3)
{
out << std::endl;
out << "#else" << std::endl;
out << "0x41," << std::endl;
out << "#endif" << std::endl;
global_hex21 = 0;
}
if (compress && bdf.glyph(i).compressed_count() > 0)
if ((bdf.glyph(i).escaped_bitset() && j == 2*BITS_TO_BYTES(bdf.glyph(i).converted_count())-1) ||
(!bdf.glyph(i).escaped_bitset() && j == BITS_TO_BYTES(bdf.glyph(i).converted_count())-1))
out << std::endl;
}
if (compress && bdf.glyph(i).compressed_count() == BITS_TO_BYTES(bdf.glyph(i).converted_count()))
out << "/*blank*/";
out << std::endl;
}
void
generate_offset(std::ofstream& out, Bdf& bdf, Glyph::encoding_t en)
{
unsigned short b = bdf.glyph(en).output_offset();
unsigned int high = b >> 8;
unsigned int low = b & 0xFF;
out << " ";
if (b < 10)
out << " ";
if (b < 100)
out << " ";
if (b < 1000)
out << " ";
if (b < 10000)
out << " ";
out << "+";
out << b;
out << " */";
if (bdf.glyph(en).escaped_bitset())
high |= 0x80;
out << " 0x";
if (high<16)
out << "0";
out << std::hex << high;
out << ",0x";
if (low<16)
out << "0";
out << std::hex << low;
out << std::dec;
}
void
generate(std::ofstream& out, Bdf& bdf, char* class_name, Glyph::encoding_t first, Glyph::encoding_t last)
{
out << "/* Generated by bdf2uc " << VERSION
<< " " << AUTHOR
<< " " << SOURCE
<< " */" << std::endl;
output_hdr(out, bdf);
out << "/* encoding format is 8 rows at a time (byte) sweeping across columns */" << std::endl;
out << std::endl;
if (!class_name)
{
out << "/*"
<< " width=" << bdf.bb_width()
<< " height=" << bdf.bb_height()
<< " first=0x" << std::hex << first << std::dec
<< " last=0x" << std::hex << last << std::dec
<< " glyph_size=" << (bdf.bb_width() * BITS_TO_BYTES(bdf.bb_height()))
<< " bitmap_size="<< (bdf.bb_width() * BITS_TO_BYTES(bdf.bb_height())) * (last-first+1)
<< " */"
<< std::endl;
out << bdf.bb_width() << ",";
out << bdf.bb_height() << ",";
out << first << ",";
out << last << ",";
out << std::endl;
}
else
{
out << "const uint8_t " << class_name << "::width = " << bdf.bb_width() << ";" << std::endl;
out << "const uint8_t " << class_name << "::height = " << bdf.bb_height() << ";" << std::endl;
out << "const uint8_t " << class_name << "::first = 0x" << std::hex << first << std:: dec << ";" << std::endl;
out << "const uint8_t " << class_name << "::last = 0x" << std::hex << last << std::dec << ";" << std::endl;
out << "const uint8_t " << class_name << "::compression_type = "
<< compress << ";" << std::endl;
out << std::endl;
out << "/* glyph_size=" << (bdf.bb_width() * BITS_TO_BYTES(bdf.bb_height())) << " */" << std::endl;
if (compress)
out << "/* uncompressed_size=" << (bdf.bb_width() * BITS_TO_BYTES(bdf.bb_height())) * (last-first+1) << " */" << std::endl;
out << "/* bitmap_size=";
if (compress)
out << total_compressed+(2*(last-first+1));
else
out << total_converted;
out << " */" << std::endl;
if (compress)
out << "/* compression saved " << -percent(total_compressed+(2*(last-first+1)), (bdf.bb_width() * BITS_TO_BYTES(bdf.bb_height())) * (last-first+1)) << "% */" << std::endl;
out << std::endl;
if (class_name)
out << "const uint8_t " << class_name << "::bitmap[] __PROGMEM = {" << std::endl;
if (compress)
{
// Offset of first "blank" glyph
bool seen_blank = false;
Glyph::encoding_t blank_en;
// Output offsets table
unsigned int offset = 2*(last-first+1);
for (Glyph::encoding_t i = first; i <= last; i++)
{
out << "/* ";
describe(out, i);
if (bdf.glyph(i).blank())
{
if (seen_blank)
{
// duplicate
generate_offset(out, bdf, blank_en);
out << ",";
out << " /*blank*/";
}
else
{
// first
seen_blank = true;
blank_en = i;
bdf.glyph(i).output_offset(offset);
generate_offset(out, bdf, i);
if (bdf.glyph(i).compressed_count() > 0)
offset += bdf.glyph(i).compressed_count();
else
offset += bdf.glyph(i).converted_count();
out << ",";
out << " /*first blank*/";
}
}
else
{
bdf.glyph(i).output_offset(offset);
generate_offset(out, bdf, i);
if (bdf.glyph(i).compressed_count() > 0)
offset += bdf.glyph(i).compressed_count();
else
offset += bdf.glyph(i).converted_count();
out << ",";
}
out << std::endl;
}
out << std::endl;
}
}
for (Glyph::encoding_t i = first; i <= last; i++)
{
output_glyph(out, bdf, class_name, i);
out << std::endl;
}
if (class_name)
out << "};" << std::endl;
}
int
main(int argc, char *const argv[])
{
Glyph::encoding_t first = 0;
Glyph::encoding_t last = 255;
char * bdf_name;
char *out_name;
char *class_name = NULL;
std::ofstream out;
for (;;) {
switch (getopt(argc, argv, "f:l:n:vch?-")) {
case 'f':
if (optarg[0] == '0' &&
(optarg[1] == 'x' || optarg[1] == 'X'))
first = strtoul(&optarg[2], NULL, 16);
else
{
if (strlen(optarg) > 1)
{
std::cerr << "Argument to -f more than 1 character" << std::endl;
exit(-1);
}
first = optarg[0];
}
continue;
case 'l':
if (optarg[0] == '0' &&
(optarg[1] == 'x' || optarg[1] == 'X'))
last = strtoul(&optarg[2], NULL, 16);
else
{
if (strlen(optarg) > 1)
{
std::cerr << "Argument to -l more than 1 character" << std::endl;
exit(-1);
}
last = optarg[0];
}
continue;
case 'n':
if (optind == argc)
{
std::cerr << "No class name provided" << std::endl;
exit(-1);
}
class_name = optarg;
continue;
case 'v':
verbose = true;
continue;
case 'c':
compress = true;
continue;
case EOF:
break;
case '?':
case 'h':
std::cout << "bdf2uc [option]... {bdf-input-file} {output-path-name}" << std::endl << std::endl;
std::cout << "-f {first} - {first} is character or hex (0xXX)" << std::endl;
std::cout << "-l {last} - {last} is character or hex (0xXX)" << std::endl;
std::cout << "-n {name} - will package for Cosa using class {name}" << std::endl;
std::cout << "-c - compress" << std::endl;
std::cout << "-v - verbose" << std::endl;
exit(0);
case '-':
std::cerr << "Long options are invalid" << std::endl;
exit(-1);
case ':':
std::cerr << "Missing argument for option '" << optopt << "'" << std::endl;
exit(-1);
default:
std::cerr << "Unkown option '" << optopt << "'" << std::endl;
exit(-1);
}
break;
}
// Check arguments
if (optind == argc)
{
std::cerr << "No bdf file provided" << std::endl;
exit(-1);
}
bdf_name = argv[optind++];
if (!class_name && compress)
{
std::cerr << "WARNING: without name (-n) compression is disabled" << std::endl;
compress = false;
}
if (optind == argc)
out.open("/dev/stdout");
else
{
out_name = argv[optind++];
out.open(out_name);
if (!out.is_open())
{
std::cerr << "Can't open file '" << out_name << "' "
<< strerror(errno) << std::endl;
exit(-1);
}
}
while (optind < argc) {
std::cerr << "Ignored argument '" << argv[optind++] << "'" << std::endl;
}
if (first > last)
{
std::cerr << "-f greater than -l, swapped" << std::endl;
unsigned int t = first;
first = last;
last = t;
}
// Read in BDF
std::ifstream input;
input.open(bdf_name);
if (!input.is_open())
{
std::cerr << "Can't open input file '" << bdf_name << "' "
<< strerror(errno) << std::endl;
exit(-1);
}
bdf.read(input);
input.close();
// Convert
for (Glyph::encoding_t i = first; i <= last; i++)
convert_glyph(bdf, i);
// Compress
if (compress)
for (Glyph::encoding_t i = first; i <= last; i++)
compress_glyph(bdf, i);
// Verbose reporting
if (verbose)
for (Glyph::encoding_t i = first; i <= last; i++)
{
describe(std::cerr, i);
if (compress)
{
if (bdf.glyph(i).converted_count() < bdf.glyph(i).compressed_count())
std::cerr << " *** GREW ***"
<< percent(bdf.glyph(i).compressed_count(), bdf.glyph(i).converted_count()) << "%";
if (bdf.glyph(i).converted_count() > bdf.glyph(i).compressed_count())
std::cerr << percent(bdf.glyph(i).converted_count(), bdf.glyph(i).compressed_count()) << "%";
}
std::cerr << std::endl;
}
// Decide if we are keeping compression
if (compress)
{
// Compressing means we need a offsets table too
unsigned int offsets_size = 2*(last-first+1);
if (class_name)
std::cerr << class_name << " ";
std::cerr << "overall compression: ";
if (total_converted < (total_compressed + offsets_size))
{
std::cerr << " *** GREW *** "
<< percent(total_compressed + offsets_size, total_converted)
<< "%, cancelling compression";
compress = false;
}
if (total_converted > (total_compressed + offsets_size))
std::cerr << -percent(total_compressed + offsets_size, total_converted)
<< "%";
if (total_converted == (total_compressed + offsets_size))
{
std::cerr << "0%, cancelling compression";
compress = false;
}
std::cerr << std::endl;
}
// Generate output
generate(out, bdf, class_name, first, last);
out.close();
return (0);
}