Skip to content

Instantly share code, notes, and snippets.

@torbiak
Last active November 21, 2023 21:26
Show Gist options
  • Save torbiak/3352fe1f559dbfbf99d6f5704adf442e to your computer and use it in GitHub Desktop.
Save torbiak/3352fe1f559dbfbf99d6f5704adf442e to your computer and use it in GitHub Desktop.
Install ttf and otf fonts for use with groff
#!/usr/bin/env bash
# groff-install-ttf converts a TrueType (ttf) or OpenType (otf) font to a
# Printer Font ASCII (pfa) font and a groff font (ditroff) and installs them to
# groff's site-font directory.
#
# Requires fontforge.
#
# You're the best, Peter Schaffter, but contrary to the verbose and
# difficult-to-follow http://www.schaffter.ca/mom/momdoc/appendices.html#fonts,
# the t42 file doesn't seem to be necessary, at least with recent versions of
# groff. Just copy the groff font and pfa generated by fontforge to both
# site-font device directories (devps, devpdf) and update the "download" files
# with the internalname -> <pfa> mapping.
#
# Note that "groffname" needs to match groff's conventions for font styles.
# Regular should end with "R", bold with "B", etc.
set -eu
font=${1:?No font file given}; shift
groffname=${1:?No groff font name given, eg TimesR, TimesI, TimesB, etc}; shift
name=$(basename "$font" .${font##*.})
prefix=/usr/share/groff
fontdir=$prefix/current/font
textmap=$fontdir/devps/generate/textmap
sitefont=$prefix/site-font
trap cleanup EXIT
cleanup() {
rm -f "$groffname" "$name.pfa"
}
fontforge -lang=ff -c "Open(\"$font\");Generate(\"$name.pfa\");"
afmtodit "$name.afm" "$textmap" "$groffname"
mkdir -p "$sitefont"
for dir in "$sitefont"/dev{ps,pdf}; do
mkdir -p "$dir"
cp "$name.pfa" "$groffname" "$dir"
done
internalname=$(awk '/^internalname/ {print $2}' "$groffname")
echo -e "$internalname\t$name.pfa" >>"$fontdir/devps/download"
echo -e "\t$internalname\t$name.pfa" >>"$fontdir/devpdf/download"
@dj-bauer
Copy link

Thanks! Now it works like a charm :)

@xenu
Copy link

xenu commented Dec 10, 2020

You're the best, Peter Schaffter, but contrary to the verbose and
difficult-to-follow http://www.schaffter.ca/mom/momdoc/appendices.html#fonts,
the t42 file doesn't seem to be necessary, at least with recent versions of
groff. Just copy the groff font and pfa generated by fontforge to both
site-font device directories (devps, devpdf) and update the "download" files
with the internalname -> mapping.

TrueType -> Type 42 conversion is lossless, TrueType -> Type 1 isn't. That's the reason why Peter's guide is using .t42 files.

@torbiak
Copy link
Author

torbiak commented Dec 10, 2020

@xenu ah, thanks. The PostScript fonts Wikipedia page describes various font types. Type 42 is a PostScript wrapper around TrueType.

It's probably better to use install-font.sh, referenced from the groff manual, instead of this script. install-font.sh is baroque but it deals with a bunch of issues that I'm ignorant of.

@RobWilliams1948
Copy link

RobWilliams1948 commented Jun 6, 2023

I think the shebang "/usr/bin/env bash" leads to a problem with the echo flag -e.
Reset it to /usr/bin/bash or /bin/bash (for my Ubuntu Linux).
Also I unpacked the for iteration to make it work:
for dir in "$sitefont"/devps "$sitefont"/devpdf; do
Otherwise the bash script worked well, thank you for sharing it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment