summaryrefslogtreecommitdiff
path: root/font/tools/bdf2c.awk
blob: 0f581678f713c3eacf5110a476a32a83a3e90451 (plain)
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
#!/bin/gawk -f
# Converts bdf to nice C header
# Glyphs should all have the same height, but width can vary
#
# Put this in "font.h":
#
# #pragma once
#
# #include <stdint.h>
# #include <stddef.h>
#
# struct glyph {
#    size_t offset;
#    uint8_t width;
# };
#
# struct font {
#    const struct glyph *ucs2glyph;
#    const uint8_t *data;
#    size_t max_ucs;
#    uint8_t height;
# };

BEGIN {
   bbx = 0
   offset = 0
   max_ucs = 0
   encoding = 0
   font = "unnamed"
   print "#pragma once"
   print "#include \"font.h\""
}

/^FONT / {
   gsub(/-/, "_", $2)
   font = $2
}

/^ENCODING / {
   idx2ucs[encoding= $2
   idx2ucs_offset[encoding++= offset
   if ($2 > max_ucs)
      max_ucs = $2
}

/^BBX / {
   widths[bbx++= $2
   height = $3
}

/^ENDCHAR/ {
   bitmap = 0
}

bitmap {
   for (i = 1; i <= NF; ++i) {
      for (s = 0; s < length($1); s += 2) {
         data = data "0x" substr($1, s, 2","
         ++offset
      }
   }
}

/^BITMAP/ {
   bitmap = 1
}

END {
   print "static const uint8_t " font "_data[] = {"
   filter = "awk -F',' -v OFS=',' -v n=24 '{for (i=n+1; i<=NF; i+=n) $i = \"\\n\" $i; print}'"
   print data | filter
   close(filter)
   print "};"

   print "static const struct glyph " font "_glyph[] = {"
   for (i in idx2ucs)
   print "[" idx2ucs[i"] = { .offset = " idx2ucs_offset[i", .width = " widths[i" },"
   print "};"

   print "static const struct font " font " = {"
   print ".ucs2glyph = " font "_glyph,"
   print ".data = " font "_data,"
   print ".max_ucs = " max_ucs ","
   print ".height = " height
   print "};"
}