1
0
mirror of https://github.com/hsoft/collapseos.git synced 2024-12-24 14:28:06 +11:00

Rewrite font_compile.pl to C

This commit is contained in:
Virgil Dupras 2020-01-07 18:26:40 -05:00
parent 9a7617115f
commit 439f880abe
5 changed files with 83 additions and 54 deletions

View File

@ -1,4 +1,4 @@
.
.
.
.
.
@ -13,11 +13,11 @@
. .
.....
. .
.....
. .
. .
.....
. .
.....
. .
.
....
@ -64,7 +64,7 @@
. . .
...
.....
.....
...
. . .

1
tools/.gitignore vendored
View File

@ -1,3 +1,4 @@
/memdump
/blkdump
/upload
/fontcompile

View File

@ -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:

View File

@ -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);
}

71
tools/fontcompile.c Normal file
View File

@ -0,0 +1,71 @@
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <libgen.h>
/* 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<h; i++) {
putchar(0);
}
int lineno = 1;
char buf[0x10];
while (fgets(buf, 0x10, fp)) {
size_t l = strlen(buf);
if (l > 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<w; i++) {
if (buf[i] == '.') {
c |= (1 << (7-i));
}
}
putchar(c);
lineno++;
}
fclose(fp);
return 0;
}