dotfiles/bin/groff_ttf.sh

100 lines
3.0 KiB
Bash
Raw Normal View History

2020-11-23 16:54:04 +00:00
#!/bin/sh
2020-11-25 07:46:56 +00:00
# shellcheck disable=2035
2020-11-23 16:54:04 +00:00
set -e
2020-11-25 07:46:56 +00:00
# print help if no arugment supplied
2020-11-24 10:30:38 +00:00
if [ "$#" -lt 2 ]; then
CMD=$(basename "$0")
echo "Usage: $CMD <*.ttf/ttc/otf> <style>"
echo " regular : $CMD sarasa-ui-sc-regular.ttf R"
echo " italic : $CMD sarasa-ui-sc-italic.ttf I"
echo " bold : $CMD sarasa-ui-sc-bold.ttf B"
echo " bold italic : $CMD sarasa-ui-sc-bolditalic.ttf BI"
echo
echo "note: the suffix of groff_name (R/I/B/BI) is essential!"
fi
FONTPATH=$(readlink -f "$1")
2020-11-25 07:46:56 +00:00
# argument checking
2020-11-24 10:30:38 +00:00
STYLE=$2
2020-11-24 18:18:32 +00:00
case "$STYLE" in
R|B|I|BI)
;;
*)
echo "warning: unknown style $STYLE, are u sure? [y/N]"
read -r CONFIRM_STYLE
if [ "$CONFIRM_STYLE" != 'y' ]; then
exit
fi
;;
esac
2020-11-24 10:30:38 +00:00
GROFF_CURRENT=$(readlink -f /usr/share/groff/current)
GROFF_SITEFONT=/usr/share/groff/site-font
2020-11-23 16:54:04 +00:00
2020-11-25 07:46:56 +00:00
# conversion
2020-11-23 16:54:04 +00:00
TMPDIR="$(mktemp -d)"
2020-11-24 18:18:32 +00:00
trap 'rm -rf '"$TMPDIR"';cd - >/dev/null' EXIT
2020-11-23 16:54:04 +00:00
cd "$TMPDIR"
2020-11-24 10:30:38 +00:00
echo "Change working directory to: $(pwd)"
echo "Using fontforge to generate PFA"
2020-11-25 07:46:56 +00:00
# fontforge do not process -c argument the same way as -script, have to create a temporary script
cat <<'EOT' > generate-pfa.pe
Open($1);
Generate($fontname + ".pfa");
EOT
fontforge -script generate-pfa.pe "$FONTPATH" >/dev/null 2>&1
AFM=$(ls *.afm)
PFA=$(ls *.pfa)
2020-11-24 10:30:38 +00:00
FONTNAME=$(awk '$1 == "FamilyName" {print substr($0, 12)}' "$AFM" | sed -r 's/\s+//g')
GROFFNAME=$FONTNAME$STYLE
echo "Converting to groff font"
afmtodit "$AFM" "$GROFF_CURRENT/font/devps/generate/textmap" "$GROFFNAME" >/dev/null 2>&1
INA=$(awk '$1 == "internalname" { print $2; exit; }' "$GROFFNAME")
echo "groff name : $GROFFNAME"
echo "internalname : $INA"
2020-11-25 07:46:56 +00:00
# installation
2020-11-24 10:30:38 +00:00
echo "Copying to $GROFF_SITEFONT"
sudo mkdir -p $GROFF_SITEFONT/devps
2020-11-23 16:54:04 +00:00
sudo cp "$PFA" "$GROFFNAME" /usr/share/groff/site-font/devps
2020-11-25 07:46:56 +00:00
sudo ln -sf "/usr/share/groff/site-font/devps/$PFA" "/usr/share/groff/site-font/devpdf/$PFA"
sudo ln -sf "/usr/share/groff/site-font/devps/$GROFFNAME" "/usr/share/groff/site-font/devpdf/$GROFFNAME"
2020-11-23 16:54:04 +00:00
2020-11-24 10:30:38 +00:00
echo "Adding font to downloadable list"
2020-11-23 16:54:04 +00:00
if ! grep "$PFA" "$GROFF_CURRENT/font/devps/download" ; then
2020-11-24 10:30:38 +00:00
printf '%s\t%s\n' "$INA" "$PFA" | sudo tee -a "$GROFF_CURRENT/font/devps/download" >/dev/null
2020-11-23 16:54:04 +00:00
fi
2020-11-25 07:46:56 +00:00
if ! grep "$PFA" "$GROFF_CURRENT/font/devpdf/download" ; then
printf '\t%s\t%s\n' "$INA" "$PFA" | sudo tee -a "$GROFF_CURRENT/font/devpdf/download" >/dev/null
fi
2020-11-24 10:30:38 +00:00
2020-11-25 07:46:56 +00:00
# print result and tips
2020-11-24 18:18:32 +00:00
echo
2020-11-25 07:46:56 +00:00
echo "Done, now you may use this font in your .mom file:"
2020-11-24 18:18:32 +00:00
printf "\033[32m"
2020-11-25 07:46:56 +00:00
echo " .class [cjk] \\[u4E00]-\\[u9FFF]"
echo " .class [fpunc] \\[u3000]-\\[u303F]"
echo " .cflags 66 \\C'[cjk]'"
echo " .cflags 68 \\C'[fpunc]'"
echo " .FAMILY $FONTNAME"
2020-11-24 18:18:32 +00:00
if [ "$STYLE" != "R" ] ; then
2020-11-25 07:46:56 +00:00
echo " .FONT $STYLE"
2020-11-24 18:18:32 +00:00
fi
printf "\033[0m"
2020-11-24 10:30:38 +00:00
echo
echo "Then, use following command to generate pdf file:"
2020-11-24 18:18:32 +00:00
printf "\033[32m"
2020-11-25 07:46:56 +00:00
echo " groff -mom -U -Kutf8 input.ms > /tmp/tmp.ps"
echo " ps2pdf /tmp/tmp.ps output.pdf"
2020-11-24 18:18:32 +00:00
printf "\033[0m"
2020-11-24 10:30:38 +00:00
echo
2020-11-25 07:46:56 +00:00
echo "PS: to insert image"
echo " imagemagick covert image: convert input.jpg output.eps"
echo " in .mom file use : .PSPIC output.eps"
echo
2020-11-24 10:30:38 +00:00