From 439f880abee39bd33dd2be27a8c973ff1e5bec9e Mon Sep 17 00:00:00 2001 From: Virgil Dupras Date: Tue, 7 Jan 2020 18:26:40 -0500 Subject: [PATCH] Rewrite font_compile.pl to C --- fonts/5x7.txt | 14 ++++----- tools/.gitignore | 1 + tools/Makefile | 16 +++------- tools/font_compile.pl | 35 --------------------- tools/fontcompile.c | 71 +++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 83 insertions(+), 54 deletions(-) delete mode 100755 tools/font_compile.pl create mode 100644 tools/fontcompile.c diff --git a/fonts/5x7.txt b/fonts/5x7.txt index 91ed358..dad9605 100644 --- a/fonts/5x7.txt +++ b/fonts/5x7.txt @@ -1,4 +1,4 @@ - . + . . . . @@ -13,11 +13,11 @@ - . . -..... - . . -..... - . . + . . +..... + . . +..... + . . . .... @@ -64,7 +64,7 @@ . . . ... -..... +..... ... . . . diff --git a/tools/.gitignore b/tools/.gitignore index 2535376..b64728e 100644 --- a/tools/.gitignore +++ b/tools/.gitignore @@ -1,3 +1,4 @@ /memdump /blkdump /upload +/fontcompile diff --git a/tools/Makefile b/tools/Makefile index 7bbe9f6..e069d33 100644 --- a/tools/Makefile +++ b/tools/Makefile @@ -1,10 +1,8 @@ MEMDUMP_TGT = memdump -MEMDUMP_SRC = memdump.c BLKDUMP_TGT = blkdump -BLKDUMP_SRC = blkdump.c UPLOAD_TGT = upload -UPLOAD_SRC = upload.c -TARGETS = $(MEMDUMP_TGT) $(BLKDUMP_TGT) $(UPLOAD_TGT) +FONTCOMPILE_TGT = fontcompile +TARGETS = $(MEMDUMP_TGT) $(BLKDUMP_TGT) $(UPLOAD_TGT) $(FONTCOMPILE_TGT) OBJS = common.o all: $(TARGETS) @@ -13,14 +11,8 @@ all: $(TARGETS) .c.o: $(CC) $(CFLAGS) -c $< -o $@ -$(MEMDUMP_TGT): $(MEMDUMP_SRC) $(OBJS) - $(CC) $(CFLAGS) $(MEMDUMP_SRC) $(OBJS) -o $@ - -$(BLKDUMP_TGT): $(BLKDUMP_SRC) $(OBJS) - $(CC) $(CFLAGS) $(BLKDUMP_SRC) $(OBJS) -o $@ - -$(UPLOAD_TGT): $(UPLOAD_SRC) $(OBJS) - $(CC) $(CFLAGS) $(UPLOAD_SRC) $(OBJS) -o $@ +$(TARGETS): $@.c $(OBJS) + $(CC) $(CFLAGS) $@.c $(OBJS) -o $@ .PHONY: clean clean: diff --git a/tools/font_compile.pl b/tools/font_compile.pl deleted file mode 100755 index 62cea89..0000000 --- a/tools/font_compile.pl +++ /dev/null @@ -1,35 +0,0 @@ -#!/usr/bin/perl -use strict; - -# This script converts "space-dot" fonts to binary "glyph rows". One byte for -# each row. In a 5x7 font, each glyph thus use 7 bytes. -# Resulting bytes are aligned to the **left** of the byte. Therefore, for -# a 5-bit wide char, ". . ." translates to 0b10101000 -# Left-aligned bytes are easier to work with when compositing glyphs. - -my $fn = @ARGV[0]; -unless ($fn =~ /.*(\d)x(\d)\.txt/) { die "$fn isn't a font filename" }; -my ($width, $height) = ($1, $2); - -if ($width > 8) { die "Can't have a width > 8"; } - -print STDERR "Reading a $width x $height font.\n"; - -my $handle; -unless (open($handle, '<', $fn)) { die "Can't open $fn"; } - -# We start the binary data with our first char, space, which is not in our input -# but needs to be in our output. -print pack('C*', (0) x $height); - -while (<$handle>) { - unless (/( |\.){0,${width}}\n/) { die "Invalid line format '$_'"; } - my @line = split //, $_; - my $num = 0; - for (my $i=0; $i<$width; $i++) { - if (@line[$i] eq '.') { - $num += (1 << (7-$i)); - } - } - print pack('C', $num); -} diff --git a/tools/fontcompile.c b/tools/fontcompile.c new file mode 100644 index 0000000..65430ba --- /dev/null +++ b/tools/fontcompile.c @@ -0,0 +1,71 @@ +#include +#include +#include +#include + +/* This script converts "space-dot" fonts to binary "glyph rows". One byte for + * each row. In a 5x7 font, each glyph thus use 7 bytes. + * Resulting bytes are aligned to the **left** of the byte. Therefore, for + * a 5-bit wide char, ". . ." translates to 0b10101000 + * Left-aligned bytes are easier to work with when compositing glyphs. + */ + +int main(int argc, char **argv) +{ + if (argc != 2) { + fprintf(stderr, "Usage: ./fontcompile fpath\n"); + return 1; + } + char *fn = basename(argv[1]); + if (!fn) { + return 1; + } + int w = 0; + if ((fn[0] >= '3') && (fn[0] <= '8')) { + w = fn[0] - '0'; + } + int h = 0; + if ((fn[2] >= '3') && (fn[2] <= '8')) { + h = fn[2] - '0'; + } + if (!w || !h || fn[1] != 'x') { + fprintf(stderr, "Not a font filename: (3-8)x(3-8).txt.\n"); + return 1; + } + fprintf(stderr, "Reading a %d x %d font\n", w, h); + FILE *fp = fopen(argv[1], "r"); + if (!fp) { + fprintf(stderr, "Can't open %s.\n", argv[1]); + return 1; + } + // We start the binary data with our first char, space, which is not in our + // input but needs to be in our output. + for (int i=0; i w+1) { // +1 because of the newline char. + fprintf(stderr, "Line %d too long.\n", lineno); + fclose(fp); + return 1; + } + // line can be narrower than width. It's padded with spaces. + while (l < w+1) { + buf[l] = ' '; + l++; + } + unsigned char c = 0; + for (int i=0; i