Code

Initial commit; imported font2tex.sh helper script.
authorSebastian Harl <sh@tokkee.org>
Thu, 31 May 2012 10:47:57 +0000 (12:47 +0200)
committerSebastian Harl <sh@tokkee.org>
Thu, 31 May 2012 10:47:57 +0000 (12:47 +0200)
The script may be used to convert arbitrary fonts (any font supported by
fontforge) to LaTeX fonts plus appropriate packages and install them into the
texmf directories.

tex/font2tex.sh [new file with mode: 0755]

diff --git a/tex/font2tex.sh b/tex/font2tex.sh
new file mode 100755 (executable)
index 0000000..8395990
--- /dev/null
@@ -0,0 +1,399 @@
+#! /bin/bash
+#
+# Copyright (C) 2012 Sebastian 'tokkee' Harl <sh@tokkee.org>
+# All rights reserved.
+#
+# Redistribution and use in source and binary forms, with or without
+# modification, are permitted provided that the following conditions
+# are met:
+# 1. Redistributions of source code must retain the above copyright
+#    notice, this list of conditions and the following disclaimer.
+# 2. Redistributions in binary form must reproduce the above copyright
+#    notice, this list of conditions and the following disclaimer in the
+#    documentation and/or other materials provided with the distribution.
+#
+# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+# ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
+# TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR
+# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
+# EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
+# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
+# OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
+# WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
+# OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
+# ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+
+#
+# This script may be used to convert various font formats
+# to fonts usable by LaTeX.
+#
+
+set -e
+set -u
+
+orig_wd="$( pwd )"
+
+KPSEWHICH="$( which kpsewhich )"
+FONTFORGE="$( which fontforge )"
+MKTEXLSR="$( which mktexlsr )"
+TEX="$( which tex )"
+
+supplier_map="$( $KPSEWHICH supplier.map )"
+typeface_map="$( $KPSEWHICH typeface.map )"
+
+target="$( $KPSEWHICH -expand-var \$TEXMFHOME )"
+target_type="home"
+
+keeptmp=0
+domaps=1
+doinstall=1
+
+fntfont=0
+fntfont_name=""
+
+supplier_name=""
+typeface_name=""
+latexfamily=rm
+
+function exit_usage() {
+cat <<EOF
+Usage: font2tex [-M] [-I] [-g | -T dir] [-k] [-h] [-T]
+                [-s supplier] [-t face] [-F rm|sf|tt]
+                family-code font-file [font-file [...]]
+
+Options:
+  -M        Do not register map file
+  -I        Do not install the fonts (implies -k and -M)
+  -g        Install fonts globally (\$TEXMFLOCAL = $( $KPSEWHICH -expand-var \$TEXMFLOCAL ))
+            (Default: \$TEXMFHOME = $( $KPSEWHICH -expand-var \$TEXMFHOME ))
+  -T DIR    Install fonts to the specified target directory rather than any of
+            the texmf directories.
+  -k        Keep temporary directory used to store intermediate files
+  -h        Display this help and exit
+
+  -x        Create a font test document (fntproof.pdf in your cwd)
+            (By default, the regular series of the font will be used unless
+            another font name is specified using the -X option)
+  -X FONT   Specify the font name to be used to be used for the test document
+            (implies -x; see that option for more details)
+
+  -s SUPP   Specify supplier name
+            (Default is to determine that from the family name)
+  -t FACE   Specify typeface name
+            (Default is to determine that from the family name)
+
+  -F FAM    Specify LaTeX font family name
+            (Default: rm)
+
+The font files may be specified in an arbitrary order. font2tex will try to
+determine which of the files contains regular, bold, and italic fonts. If not
+italic fonts have been specified, font2tex will try to generate them for you.
+
+Copyright (C) 2012 Sebastian 'tokkee' Harl <sh@tokkee.org>
+This is free software under the terms of the BSD license, see the source for
+copying conditions. There is NO WARRANTY; not even for MERCHANTABILITY or
+FITNESS FOR A PARTICULAR PURPOSE.
+EOF
+       exit $1
+}
+
+while getopts MIgT:khxX:s:t:F: arg; do
+       case "$arg" in
+               M)
+                       domaps=0
+                       ;;
+               I)
+                       doinstall=0
+                       ;;
+               g)
+                       target="$( $KPSEWHICH -expand-var \$TEXMFLOCAL )"
+                       target_type="local"
+                       ;;
+               T)
+                       target="$OPTARG"
+                       target_type="custom"
+                       ;;
+               k)
+                       keeptmp=1
+                       ;;
+
+               h)
+                       exit_usage 0
+                       ;;
+
+               x)
+                       fntfont=1
+                       ;;
+               X)
+                       fntfont=1
+                       fntfont_name="$OPTARG"
+                       ;;
+
+               s)
+                       supplier_name="$OPTARG"
+                       ;;
+               t)
+                       typeface_name="$OPTARG"
+                       ;;
+               F)
+                       latexfamily="$OPTARG"
+                       ;;
+
+               *)
+                       exit_usage 1
+                       ;;
+       esac
+done
+shift $(( $OPTIND - 1 ))
+
+if test $doinstall -ne 1; then
+       domaps=0
+       keeptmp=1
+fi
+
+if test $# -lt 1; then
+       exit_usage 1
+fi
+
+family_code="$1"
+shift
+
+echo "Using family code '$family_code'"
+
+if [ -z "$supplier_name" ]; then
+       supplier_name=$( grep "^$( echo $family_code | cut -c1 )" \
+               "$supplier_map" | cut -d' ' -f2 )
+fi
+if test -n "$supplier_name"; then
+       echo "Using supplier name '$supplier_name'"
+fi
+
+if [ -z "$typeface_name" ]; then
+       typeface_name=$( grep "^$( echo $family_code | cut -c2-3 )" \
+               "$typeface_map" | cut -d' ' -f2 )
+fi
+if test -n "$typeface_name"; then
+       echo "Using type-face name '$typeface_name'"
+fi
+
+if [ -z "$supplier_name" -o -z "$typeface_name" ]; then
+       echo "Supplier or typeface codes not known." >&2
+       echo "Aborting!" >&2
+       exit 1
+fi
+
+tmpdir=$( mktemp -d -t font2tex.XXXXXXXXXX )
+if test $keeptmp -eq 0; then
+       trap "rm -rf $tmpdir" EXIT
+else
+       trap "echo \"Not removing temporary directory '$tmpdir' as requested.\"" EXIT
+fi
+echo "Using temporary directory '$tmpdir'."
+
+regular_font=""
+bold_font=""
+italic_font=""
+bold_italic_font=""
+
+while test $# -gt 0; do
+       font_file="$1"
+       font_type="${font_file##*.}"
+       shift
+
+       if test "$font_file" = "$font_type"; then
+               echo "Failed to determine file extension for '$font_file'." >&2
+               exit 1
+       fi
+
+       font_weight="$( $FONTFORGE -lang=ff \
+               -c "Open('$font_file', 1); Print(\$weight)" 2> /dev/null )"
+       font_italicangle="$( $FONTFORGE -lang=ff \
+               -c "Open('$font_file', 1); Print(\$italicangle)" 2> /dev/null )"
+
+       case "$font_weight-$font_italicangle" in
+               Regular-0)
+                       if test -n "$regular_font"; then
+                               echo "Multiple candidates for regular font:" \
+                                       "$regular_font, $font_file" >&2
+                               exit 1
+                       fi
+                       echo "Using regular font '$font_file'"
+                       cp "$font_file" "$tmpdir/${family_code}r8a.$font_type"
+                       regular_font="${family_code}r8a.$font_type"
+                       ;;
+               Regular-?[0-9]*)
+                       if test -n "$italic_font"; then
+                               echo "Multiple candidates for italic font:" \
+                                       "$italic_font, $font_file" >&2
+                               exit 1
+                       fi
+                       echo "Using italic font '$font_file'"
+                       cp "$font_file" "$tmpdir/${family_code}ri8a.$font_type"
+                       italic_font="${family_code}ri8a.$font_type"
+                       ;;
+               Medium-0|Bold-0)
+                       if test -n "$bold_font"; then
+                               echo "Multiple candidates for bold font:" \
+                                       "$bold_font, $font_file" >&2
+                               exit 1
+                       fi
+                       echo "Using bold font '$font_file'"
+                       cp "$font_file" "$tmpdir/${family_code}b8a.$font_type"
+                       bold_font="${family_code}b8a.$font_type"
+                       ;;
+               Medium*-?[0-9]*|Bold*-?[0-9]*)
+                       if test -n "$bold_italic_font"; then
+                               echo "Multiple candidates for bold italic font:" \
+                                       "$bold_italic_font, $font_file" >&2
+                               exit 1
+                       fi
+                       echo "Using bold italic font '$font_file'"
+                       cp "$font_file" "$tmpdir/${family_code}bi8a.$font_type"
+                       bold_italic_font="${family_code}bi8a.$font_type"
+                       ;;
+               *)
+                       echo "Failed to parse font weight ($font_weight) /" \
+                               "italic angle ($font_italicange)" >&2
+                       exit 1
+                       ;;
+       esac
+done
+
+cd $tmpdir
+
+if test -z "$regular_font" -o -z "$bold_font"; then
+       echo "Failed to determine regular and/or bold font." >&2
+       echo "Aborting!" >&2
+       exit 1
+fi
+
+if test -z "$italic_font"; then
+       font_type="${regular_font##*.}"
+       echo "Generating italic font for you."
+       $FONTFORGE -lang=ff \
+               -c "Open('$regular_font', 1);
+                       SelectAll();
+                       Skew(13);
+                       SetItalicAngle(-13);
+                       SetFontNames(\$fontname + '-Italic', \$familyname + '-Italic', \$fullname + ' Italic');
+                       Generate('${family_code}ri8a.$font_type');"
+fi
+
+if test -z "$bold_italic_font"; then
+       font_type="${bold_font##*.}"
+       echo "Generating bold italic font for you."
+       $FONTFORGE -lang=ff \
+               -c "Open('$bold_font', 1);
+                       SelectAll();
+                       Skew(13);
+                       SetItalicAngle(-13);
+                       SetFontNames(\$fontname + '-Italic', \$familyname + '-Italic', \$fullname + ' Italic');
+                       Generate('${family_code}bi8a.$font_type');"
+fi
+
+for T in r b ri bi; do
+       in="$( ls ${family_code}${T}8a.* )"
+       out="${family_code}${T}8a.pfb"
+       if test -z "$in"; then
+               echo "Internal error: Failed to find input file (T=$T)!" >&2
+               exit 5
+       fi
+       $FONTFORGE -lang=ff -c "Open('$in', 1); Generate('$out')"
+done
+
+cat <<EOF > "$family_code-drv.tex"
+\\input fontinst.sty
+\\recordtransforms{$family_code-rec.tex}
+\\latinfamily{$family_code}{}
+\\endrecordtransforms
+\\bye
+EOF
+$TEX "$family_code-drv.tex"
+
+for f in *.pl; do
+       pltotf $f
+done
+
+for f in *.vpl; do
+       vptovf $f
+done
+
+date=$( date +%Y/%m/%d )
+cat <<EOF > "$typeface_name.sty"
+\\NeedsTeXFormat{LaTeX2e}
+\\ProvidesPackage{$typeface_name}[$date v0.1 $supplier_name $typeface_name for LaTeX]
+\\renewcommand*{\\${latexfamily}default}{$family_code}
+\\endinput
+EOF
+
+cat <<EOF > "$family_code-map.tex"
+\\input finstmsc.sty
+\\resetstr{PSfontsuffix}{.pfb}
+\\adddriver{dvips}{$family_code.map}
+\\input $family_code-rec.tex
+\\donedrivers
+\\bye
+EOF
+$TEX "$family_code-map.tex"
+
+if test $doinstall -ne 1; then
+       echo "Successfully created LaTeX font for $family_code."
+       echo "Skipping installation of the font as requested."
+       exit 0
+fi
+
+mkdir -p "$target"/fonts/{afm,tfm,type1,vf}/"$supplier_name/$typeface_name"
+for s in afm tfm vf; do
+       cp *.$s "$target/fonts/$s/$supplier_name/$typeface_name"
+done
+cp *.pfb "$target/fonts/type1/$supplier_name/$typeface_name"
+mkdir -p "$target/tex/latex/$supplier_name-$typeface_name"
+
+cp *.fd *.sty "$target/tex/latex/$supplier_name-$typeface_name"
+
+mkdir -p "$target"/fonts/map/{dvips,pdftex}/"$supplier_name/$typeface_name"
+cp "$family_code.map" "$target/fonts/map/dvips/$supplier_name/$typeface_name"
+cp "$family_code.map" "$target/fonts/map/pdftex/$supplier_name/$typeface_name"
+
+$MKTEXLSR "$target"
+
+if test $domaps -eq 1; then
+       case $target_type in
+               system)
+                       updmap-sys --force --enable Map="$family_code.map"
+                       ;;
+               home)
+                       updmap --force --enable Map="$family_code.map"
+                       ;;
+               *)
+                       TEXMFHOME="$target" updmap \
+                               --dvipsoutputdir "$target"/var/fonts/map/dvips/updmap \
+                               --pdftexoutputdir "$target"/var/fonts/map/pdftex/updmap \
+                               --force --enable Map="$family_code.map"
+                       ;;
+       esac
+fi
+
+if test $fntfont -eq 1; then
+       if test -z "$fntfont_name"; then
+               fntfont_name="${family_code}r8r"
+       fi
+       if test "$target_type" = "custom"; then
+               export TEXMFVAR="$target/var"
+               export TEXMFHOME="$target"
+       fi
+       pdftex fntproof "$fntfont_name" '\fonttable\bye'
+       cp fntproof.pdf "$orig_wd"
+fi
+
+echo ""
+echo "Installed LaTeX font for $family_code to '$target'."
+if test "$target_type" = "custom"; then
+       echo "Set TEXMFVAR to '$target/var'"
+       echo "and TEXMFHOME to '$target'"
+       echo "when using the font."
+fi
+
+echo "Successfully created and installed LaTeX font for $family_code."
+