Makie font variants?

I’m trying to find out how to specify font variants. The following attempt fails:

using CairoMakie

fig = Figure(; fonts = (
    regular = "Baskerville",
    italic  = "Baskerville Italic",
))

ax = Axis(fig[1, 1])
text!(ax, (0.5,0.5); text = rich("italic?", font=:italic))
fig

This does change the font, presumably to Baskerville, but it’s not italic. Mac’s “FontBook” app shows that Baskerville does have an italic variant.

The file /System/Library/Fonts/Supplemental/Baskerville.ttc exists. That’s the only Baskerville* font file in the directory.

ttc fonts contain multiple faces in one file. However, the font finding machinery is currently not set up to handle this, the assumption is one face per file and for collections, I think the first face’s info is being used. So it simply does not see the other variants. There are websites or tools which allow you to convert ttc files to multiple ttf or otf ones, though.

The capability to use multiple variants could in principle be added to FreeType/FreeTypeAbstraction, too.

1 Like

you could try

using CairoMakie
using FreeTypeAbstraction

font_name = "Baskerville"
font_dir = last(FreeTypeAbstraction.valid_fontpaths)
italic_path = joinpath(font_dir, "$(replace(font_name, " " => ""))-Italic.ttf")

fig = Figure(fonts=(
    regular=font_name,
    italic=italic_path
))

text!(Axis(fig[1, 1]), (0.5, 0.5); text=rich("this is italic?", font=:italic))
fig

Thanks, but as I said, there is only one file: /System/Library/Fonts/Supplemental/Baskerville.ttc. There is no Baskerville-Italic.ttf. There is no Baskerville... file in the directory except for the single ttc file.

I know almost nothing about how fonts are arranged, but I notice that it’s not a ttf file. It’s a ttc file. That must mean that this single file contains all the variants.

What we don’t know, therefore, is how to select one variant within the file.

I explained above that it’s currently not possible

Sorry I missed your message!

So, inspired by this discussion, I explored how to extract ttfs from a ttc file and found a solution: First install “fonttools” (brew install fonttools if you use Homebrew) and then

using CairoMakie
using FreeTypeAbstraction

font_name = "Baskerville"
font_dir = last(FreeTypeAbstraction.valid_fontpaths)
ttc = joinpath(font_dir, font_name * ".ttc")
@show ttc
run(`ttx -y 2 -o tmp.ttx $(ttc)`) # extract font 2
run(`ttx tmp.ttx`) # convert ttx to ttf

fig = Figure(fonts=(
    regular=font_name,
    italic="./tmp.ttf"
))

text!(Axis(fig[1, 1]), (0.5, 0.5); text=rich("this is italic?", font=:italic))
fig

The italic variant turned out to be number 2 in the collection (which is what a ttc is) so that’s what -y 2 means.

I found this by trial and error, but there seem to be systematic methods to find that out, but those methods seem to be very complicated. There must be a simple way to query a ttc file, but the above is fine as a workaround.

As far as I know FreeType already contains machinery for this, we just don’t use it as I think ttcs are mostly a thing on Mac and the main maintainer of that font stack is on Linux.

I found there are more tcc files than ttf files under /System/Library/Fonts/ on macOS. In particular, the staple fonts like Courier, Helvetica, Palatino, and Times seem to have only tcc.

That means that only the 0th variant is available for these fonts from Makie.

If I seriously need the variants, I’ll find a set of ttfs and install them under my home directory or generate them from the ttcs. Currently I’m fine with the above workaround.

If you need ttf, there’s a version LibreBaskerville fromGoogle Fonts or you can get open type from LibreBaskerville. Keep the downloaded fonts, whether or not you install them with FontBook and specify those.

Or perhaps, the font library on Linux is already designed to hide the distinction between a collection of ttfs and a ttc? or Linux distributions always generate ttfs from a ttc on installation? See this external Q&A thread, which means that ttcs are already a standard component on Linux.

Also, a comment in another external thread indicates that Cambria shipped with Windows is in the ttc format.

This all makes sense. To present a “font family” as a single file (ttc) is more convenient than to have a set of files named like Times-Regular.ttf, Times-Italic.ttf, Times-Bold.ttf, . . .

1 Like