Skip to content

Instantly share code, notes, and snippets.

@mwgamera
Last active August 29, 2015 14:07
Show Gist options
  • Select an option

  • Save mwgamera/42db83037974616ca715 to your computer and use it in GitHub Desktop.

Select an option

Save mwgamera/42db83037974616ca715 to your computer and use it in GitHub Desktop.
14-px Braille bitmap font compatible with Terminus.
#!/usr/bin/env perl
# 14-px Braille bitmap font compatible with Terminus.
# klg, Oct 2014
use strict;
use List::MoreUtils 'pairwise';
use constant XLFD => '-klg-TBraille-Medium-R-Normal--14-140-72-72-C-80-ISO10646-1';
use constant BBX => (8, 14, 0, -2);
my %properties = do {
my @names = (0, qw(FOUNDRY FAMILY_NAME WEIGHT_NAME SLANT SETWIDTH_NAME
ADD_STYLE_NAME PIXEL_SIZE POINT_SIZE RESOLUTION_X RESOLUTION_Y SPACING
AVERAGE_WIDTH CHARSET_REGISTRY CHARSET_ENCODING));
my @xlfd = split '-', XLFD;
pairwise { $a ? ($a => $b) : () } @names, @xlfd;
};
$properties{FONT_ASCENT} = (BBX)[1] + (BBX)[3];
$properties{FONT_DESCENT} = -(BBX)[3];
$properties{MIN_SPACE} = (BBX)[0];
my %glyphs;
=for comment
........
........
........ 0x00
.11..44. 0x66
.11..44.
........
.22..55.
.22..55.
........
.33..66.
.33..66.
________
.77..88.
.77..88.
=cut
# Braille Patterns (0x2800—0x28FF)
for my $c (0 .. 255) {
my @x = map { $c >> $_ & 1 } 0 .. 7, -1;
my @y = qw(0 0 0 0 0 0 1 4 1 4 0 0 2 5 2 5 0 0 3 6 3 6 0 0 7 8 7 8);
$glyphs{0x2800+$c} = [map {96 * $x[$y[2*$_]-1] + 6 * $x[$y[2*$_+1]-1]} 0 .. (BBX)[1]-1];
}
# Braille ASCII
%glyphs = (%glyphs, do {
my @a = split'', q{ A1B'K2L@CIF/MSP"E3H9O6R^DJG>NTQ,*5<-U8V.%[$+X!&;:4\0Z7(_?W]#Y)=};
my @b = (0 .. $#a);
pairwise { ord($a) => $glyphs{0x2800+$b} } @a, @b;
});
# NUL
$glyphs{0} = [(0x55, 0xaa) x (BBX)[1]];
# NOTE: Some software won't work correctly unless there are some known characters defined.
print "STARTFONT 2.1\n";
printf "FONT %s\n", XLFD;
printf "SIZE %u %u %u\n", map $properties{$_}, qw(PIXEL_SIZE RESOLUTION_X RESOLUTION_Y);
printf "FONTBOUNDINGBOX %u %u %d %d\n", BBX;
printf "STARTPROPERTIES %u\n", scalar(keys %properties);
for (sort keys %properties) {
my $val = $properties{$_};
if ($_ eq 'CHARSET_ENCODING' || !($val =~ /^[0-9]+$/)) {
$val = "\"$val\"";
}
printf "%s %s\n", $_, $val;
}
print "ENDPROPERTIES\n";
printf "CHARS %u\n", scalar(keys %glyphs);
for my $c (sort {$a <=> $b} keys %glyphs) {
printf "STARTCHAR uni%04x\n", $c;
printf "ENCODING %u\n", $c;
printf "SWIDTH %u 0\n", $properties{RESOLUTION_X} * (BBX)[0];
printf "DWIDTH %u 0\n", BBX;
printf "BBX %u %u %d %d\n", BBX;
print "BITMAP\n";
printf "%02x\n", $glyphs{$c}[$_] for 0 .. (BBX)[1]-1;
print "ENDCHAR\n";
}
print "ENDFONT\n";
1;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment