summaryrefslogtreecommitdiff
path: root/font/tools/bdf2c.awk
diff options
context:
space:
mode:
Diffstat (limited to 'font/tools/bdf2c.awk')
-rwxr-xr-xfont/tools/bdf2c.awk86
1 files changed, 86 insertions, 0 deletions
diff --git a/font/tools/bdf2c.awk b/font/tools/bdf2c.awk
new file mode 100755
index 0000000..0f58167
--- /dev/null
+++ b/font/tools/bdf2c.awk
@@ -0,0 +1,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 "};"
+}