Skip to content

TTF Fonts in Webfont umwandeln

Als erstes installieren wir das Paket fontforge aud den Paketquellen.

sudo apt install fontforge

Dann erstellen wir und ein Skript und nennen es z.B. ttf2webfonts.sh.

#!/bin/bash

# Get the path to the fontforge binary
fontforge=$(which fontforge)

# Get the first argument as the directory, where the ttf files are located
dir=$1

# Check, if a directory was defined
if [ ! -d "${dir}" ]; then
    echo "No directory selected." && exit 1
fi

# Check, if path to fontforge binary exists
if [ ! -x "${fontforge}" ]; then
    echo "No fontforge installed on your system." && exit 1
fi

trap "rm ${dir}/*.pe" EXIT

# Loop through all ttf files in the directory
for ttf_file in ${dir}/*.ttf; do
    # Get the filename without .ttf
    newname="${ttf_file%.ttf}"

    # Create a fontforge script for each ttf file
    cat - <<SCRIPT | tee ${dir}/convert.pe >/dev/null
#!${fontforge}
Open("${ttf_file}")
Generate("${ttf_file:r}.otf")
Generate("${ttf_file:r}.svg")
Generate("${ttf_file:r}.woff")
Generate("${ttf_file:r}.woff2")
SCRIPT

    # Make the script executable and run it
    chmod 755 ${dir}/convert.pe && \
        ${dir}/convert.pe

    # Rename .otf file
    if [ "${ttf_file}.otf}" ]; then
        mv ${ttf_file}.otf ${newname}.otf
    fi

    # Rename .svg file
    if [ "${ttf_file}.svg}" ]; then
        mv ${ttf_file}.svg ${newname}.svg
    fi

    # Rename .woff file
    if [ "${ttf_file}.woff}" ]; then
        mv ${ttf_file}.woff ${newname}.woff
    fi

    # Rename .woff2 file
    if [ "${ttf_file}.woff2}" ]; then
        mv ${ttf_file}.woff2 ${newname}.woff2
    fi

done

Das Skript machen wir nun ausführbar.

chmod 755 ttf2webfonts.sh

Dann rufen wir das Sript mit dem Verzeichnis als Argument auf, in dem sich die TTF Fonts befinden.

./ttf2webfonts.sh /home/tux/Downloads/Roboto