ipython / xkcd-font

The xkcd font
https://cdn.rawgit.com/ipython/xkcd-font/master/preview.html
Other
1.06k stars 48 forks source link

How to programatically install the fonts? #42

Closed matthewfeickert closed 5 months ago

matthewfeickert commented 5 months ago

:wave: Hi. Despite using Linux for over a decade, I know very little about fonts. Whenever I have to install something a font that I can't get through my package manager I download the files and install them by opening up the font manger GUI.

Now say that I'm working in an environment, like building a Linux container image, where I want to install the XKCD fonts in this repo. What is the recommended programatic way to install them? Is there a way that I can do so easily with a single command for all the fonts? Or would I need to manually copy them into directories like

$ mkdir -p /usr/share/fonts/opentype/xkcd
$ curl -sL https://github.com/ipython/xkcd-font/raw/master/xkcd/build/xkcd.otf -o /usr/share/fonts/opentype/xkcd/xkcd.otf
$ mkdir -p /usr/share/fonts/truetype/xkcd-script
$ curl -sL https://github.com/ipython/xkcd-font/raw/master/xkcd-script/font/xkcd-script.ttf -o /usr/share/fonts/truetype/xkcd-script/xkcd-script.ttf

?

matthewfeickert commented 5 months ago

Ah, @story645 pointed out to me (thanks!) that the way matplotlib does it in their CI is the downloading method (which is how these fonts work) of basically

$ mkdir -p ~/.local/share/fonts
$ curl -sL https://github.com/ipython/xkcd-font/raw/master/xkcd-script/font/xkcd-script.ttf -o ~/.local/share/fonts/xkcd-script.ttf
$ fc-cache -f -v

and to avoid issues I've had in the past I might also suggest throwing in a

$ rm ~/.cache/matplotlib/*

to force a cache rebuild.

matthewfeickert commented 5 months ago

As @jni was curious about macOS, a similar procedure is used, but macOS keeps your local fonts under ~/Library/Fonts/ and there's no ~/.cache/ directory, just a ~/.matplotlib directory. So the following works

$ curl -sL https://github.com/ipython/xkcd-font/raw/master/xkcd/build/xkcd.otf ~/Library/Fonts/xkcd.otf
$ curl -sL https://github.com/ipython/xkcd-font/raw/master/xkcd-script/font/xkcd-script.ttf ~/Library/Fonts/xkcd-script.ttf
$ rm -rf ~/.matplotlib

Example after doing the above:

(I use micromamba on macOS for the time being, though I use pyenv for my Linux machines)

For

# example.py
import matplotlib.pyplot as plt
import numpy as np

x = np.linspace(0, 10, 1000)
y = np.sin(x)

with plt.xkcd():
    fig, ax = plt.subplots()
    ax.plot(x, y)

    fig.savefig("example.png")
$ micromamba env create --name xkcd-debug python=3.12 matplotlib
$ micromamba activate xkcd-debug
(xkcd-debug) $ python example.py  # No missing font warnings

example