From f54e7e8c2aeb4fafebf0d5bd5570b060462c9ecf Mon Sep 17 00:00:00 2001 From: Jari Vetoniemi Date: Thu, 1 Feb 2018 08:31:01 +0200 Subject: Initial commit --- font/tools/bdf2c.awk | 86 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 86 insertions(+) create mode 100755 font/tools/bdf2c.awk (limited to 'font/tools/bdf2c.awk') 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 +# #include +# +# 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 "};" +} -- cgit v1.2.3