build: move font generation to the main repo (#2837)

* build: move font generation to the main repo

* Update fonts

* chore: remove submodules

* Update paths

* Update fonts.yml

Co-authored-by: Kevin Barabash <kevinb@khanacademy.org>
This commit is contained in:
ylemkimon
2021-03-30 03:21:59 +09:00
committed by GitHub
parent 1643b5deb5
commit 266fcb046c
100 changed files with 6872 additions and 37 deletions

40
dockers/fonts/Dockerfile Normal file
View File

@@ -0,0 +1,40 @@
FROM ubuntu:14.04
MAINTAINER xymostech <xymostech@gmail.com>
# Install things
RUN apt-get update \
&& DEBIAN_FRONTEND=noninteractive apt-get -y upgrade \
&& DEBIAN_FRONTEND=noninteractive apt-get -y install \
--no-install-recommends --auto-remove \
software-properties-common \
texlive \
wget \
fontforge \
mftrace \
man-db \
build-essential \
python-fontforge \
python-dev \
python-pip \
pkg-config \
libharfbuzz-dev \
libfreetype6-dev \
libjson-perl \
&& add-apt-repository ppa:git-core/ppa \
&& apt-get update \
&& DEBIAN_FRONTEND=noninteractive apt-get -y install \
--no-install-recommends --auto-remove \
git \
&& apt-get clean \
&& rm -rf /var/lib/apt/lists/* \
&& pip install fonttools==3.28.0 brotli zopfli
# Download and compile ttfautohint
RUN wget "http://download.savannah.gnu.org/releases/freetype/ttfautohint-1.3.tar.gz" \
&& tar -xzf ttfautohint-*.tar.gz \
&& cd ttfautohint-*/ \
&& ./configure --without-qt \
&& make \
&& mv frontend/ttfautohint /usr/bin \
&& cd .. \
&& rm -r ttfautohint-*

31
dockers/fonts/README.md Normal file
View File

@@ -0,0 +1,31 @@
### How to generate KaTeX fonts and metrics
Originally based on MathJax font generation
#### Fonts
The `buildFonts.sh` script should do everything automatically,
as long as Docker is installed.
If you want to try out a change
to [the katex-fonts repository](https://github.com/KaTeX/katex-fonts),
create a local clone (or download and unpack the ZIP file)
and specify the path to this directory as an argument to `buildFonts.sh`.
You can also specify a local or remote tarball,
e.g. a GitHub download of your own personal feature branch.
The script `buildFonts.sh` automatically creates Docker images
from the supplied `Dockerfile`.
It uses the hash of the file to tag the image, so a change to the file
will result in the creation of a new image.
If you want to see all created images, run `docker images katex/fonts`.
To remove all generated images, you can run
`docker rmi $(docker images --format '{{.Repository}}:{{.Tag}}' katex/fonts)`.
#### Metrics
The script `buildMetrics.sh` generates [metrics](../../src/fontMetricsData.js)
(dimensions of each character) for the generated fonts.
See [detailed requirements for running this script](../../src/metrics/).
If there is a problem, file a bug report.

101
dockers/fonts/buildFonts.sh Executable file
View File

@@ -0,0 +1,101 @@
#!/usr/bin/env bash
shopt -s extglob
usage() {
while [[ $# -gt 1 ]]; do
echo "$1" >&2
shift
done
echo "Usage: ${0##*/} [OPTIONS]"
echo ""
echo "OPTIONS:"
echo " -h|--help display this help"
echo " --image NAME:TAG use the named docker image [$IMAGE]"
exit $1
}
used_fonts=(
KaTeX_AMS-Regular
KaTeX_Caligraphic-Bold
KaTeX_Caligraphic-Regular
KaTeX_Fraktur-Bold
KaTeX_Fraktur-Regular
KaTeX_Main-Regular
KaTeX_Main-Bold
KaTeX_Main-Italic
KaTeX_Main-BoldItalic
KaTeX_Math-Italic
KaTeX_Math-BoldItalic
KaTeX_SansSerif-Bold
KaTeX_SansSerif-Italic
KaTeX_SansSerif-Regular
KaTeX_Script-Regular
KaTeX_Size1-Regular
KaTeX_Size2-Regular
KaTeX_Size3-Regular
KaTeX_Size4-Regular
KaTeX_Typewriter-Regular
)
filetypes=( ttf woff woff2 )
set -e
cd "$(dirname "$0")/../.."
cleanup() {
[[ "${CONTAINER}" ]] \
&& docker stop "${CONTAINER}" >/dev/null \
&& docker rm "${CONTAINER}" >/dev/null
CONTAINER=
[[ -f "${TMPFILE}" ]] && rm "${TMPFILE}"
TMPFILE=
}
CONTAINER=
trap cleanup EXIT
LAST_COMMIT_DATE="$(git log -1 --format=%ct -- src/fonts)"
IMAGE="katex/fonts:DF-$(openssl sha1 $(dirname "$0")/Dockerfile | tail -c 9)"
TMPFILE="$(mktemp "${TMPDIR:-/tmp}/mjf.XXXXXXXX")"
FILE="$TMPFILE"
pushd "src"
if [[ ! -f fonts/Makefile ]]; then
echo "src does not look like katex-fonts" >&2
exit 1
fi
tar cfP "$FILE" ../package.json fonts
popd
# build image if missing
if [[ $(docker images "$IMAGE" | wc -l) -lt 2 ]]; then
echo "Need to build docker image $IMAGE"
docker build --tag "$IMAGE" "$(dirname "$0")"
fi
CMDS="set -ex
export SOURCE_DATE_EPOCH=${LAST_COMMIT_DATE}
tar xfP katex-fonts.tar.gz
make -C fonts all
tar cf /fonts.tar ${filetypes[*]/#/fonts/}"
echo "Creating and starting docker container from image $IMAGE"
CONTAINER=$(docker create "$IMAGE" /bin/sh -c "${CMDS}")
if [[ ${FILE} ]]; then
docker cp "${FILE}" $CONTAINER:/katex-fonts.tar.gz
fi
docker start --attach $CONTAINER
docker cp $CONTAINER:/fonts.tar .
cleanup
echo "Docker executed successfully, will now unpack the fonts"
tar xf fonts.tar
mv fonts temp
mkdir fonts
for filetype in "${filetypes[@]}"; do
for font in "${used_fonts[@]}"; do
echo "$filetype/$font"
mv "temp/$filetype/$font".* ./fonts/
done
done
rm -rf temp fonts.tar
echo $LAST_COMMIT_DATE > fonts/VERSION

7
dockers/fonts/buildMetrics.sh Executable file
View File

@@ -0,0 +1,7 @@
#!/bin/sh
# Generates fontMetricsData.js
PERL="perl"
PYTHON=`python2 --version >/dev/null 2>&1 && echo python2 || echo python`
cd src/metrics
$PERL ./mapping.pl | $PYTHON ./extract_tfms.py | $PYTHON ./extract_ttfs.py | $PYTHON ./format_json.py --width > ../fontMetricsData.js