#!/bin/bash
# Get a list of the unicode points in a psfu font.
# Copyright © 2012 Ken Moffat
# requires psftools

usage() {
	echo "list the qlyphs within a psfu file" >&2
	echo "usage $0 /path/to/a/psfufile (can be .gz) [outfile]" >&2
	echo "output is to stdout if no outfile is specified" >&2
	exit 1
}
if [ $# -eq 0 ] || [ $# -gt 2 ]; then
	usage
fi

file $1 | grep -q gzip && GZIP=true

if [ "$GZIP" = "true" ]; then
	GET="zcat $1 | psfgettable -"
else
	GET="psfgettable $1"
fi

if [ $# -eq 2 ]; then
	SORT="sort >$2"
else
	SORT="sort"
fi

# for the moment, I have not found a font with more than 19 codepoints
# mapped to a single graphic, so allow for 20 : most fonts will have
# far fewer
eval $GET | grep U+ | \
 awk '{ print $2 "\n" $3 "\n" $4 "\n" $5 "\n" $6 "\n" $7 "\n" $8 "\n" $9 "\n" $10 "\n" $11 "\n" $12 "\n" $13 "\n" $14 "\n" $15 "\n" $16 "\n" $17 "\n" $18 "\n" $19 "\n" $20 "\n" $21 }' \
 | grep -v '^$' | tr [:lower:] [:upper:] | eval $SORT
