MathTeXEngine.jl icon indicating copy to clipboard operation
MathTeXEngine.jl copied to clipboard

[FEATURE] Provide Function to Install CMU Fonts on a User's Machine

Open TheCedarPrince opened this issue 2 years ago • 6 comments

Hey @Kolaru ,

This is an amazing package! Thanks to @davibarreira, we have it working with enabling LaTeX to work within Luxor.jl to make really professional looking diagrams:

image

Would it be possible to create a function that allows one to install the CMU fonts onto a user's machine? I see you distribute the package with the fonts already and it would be nice if this package installed the fonts for you as well.

Thanks!

~ tcp

TheCedarPrince avatar Dec 28 '21 18:12 TheCedarPrince

Wow, that looks absolutely amazing!

Installing the font seems to be easy for linux, but some kind of black magic for windows. So it will require further investigation.

What is your use case for this? In MathTeXEngine I currently only include the subset of the CM family we are using.

Kolaru avatar Dec 31 '21 11:12 Kolaru

Hey @Kolaru !

Yea, with Linux, using a tool like fontconfig would probably work well. With Windows, not too sure.

In my case, I have two main use cases currently:

  1. Right now we want to merge this as potentially an optional dependency to Luxor.jl. By merging, we were hoping to have this functionality to make it easier for Luxor users to use MathTeXEngine.
  2. I am the co-creator of Javis.jl in the JuliaAnimators organization and would love to have this function for our users in the ecosystem to simply/easily install the tools to utilize MathTeXEngine with Javis.

As Javis is built on top of Luxor, solving the first use case would help solve the second.

Totally understand if you don't want to work on this but figured I would ask to see if it was possible. Thanks @Kolaru !

TheCedarPrince avatar Dec 31 '21 20:12 TheCedarPrince

P.S. An additional use case that I forgot about was the fact that the above image was created for use within the AlgebraicJulia ecosystem. I had some tremendous issues at first as I didn't know the fonts were required and kept getting outputs like this:

image

Having a function or alert that could assist a user in installing these fonts would be great as I spent a couple hours tracking down this strange issue.

TheCedarPrince avatar Dec 31 '21 22:12 TheCedarPrince

I see. I build the package on the assumption that any dependency would load the fonts from the files included in the package using for example FreeTypeAbstraction.jl (it is what Makie.jl and MathTeXEngine.jl itself are doing). This way there is no need to rely on the font being installed. I guess that this difference in design is the source of your problems there.

Would it be feasible for Luxor to load the font directly without needing a system installation?

Kolaru avatar Jan 03 '22 23:01 Kolaru

@Kolaru - unfortunately, I do not know. In my experiments with my Linux machine, I was unable to get Luxor to automatically recognize a loaded font via FreeTypeAbstraction.jl. I am unsure as to why it did not work and still unsure how Makie.jl does this for rendering.

The best person who would probably know is @cormullion . I know we were chatting about the evils of font installations earlier but do you think there is anything that can be done Luxor side to better compatibility with MathTeXEngine? Or not possible @cormullion? Based on what I was reading is that Cairo uses its own quirky version of fontconfig to find system fonts, and I did not see an easy way to interface with these fonts through Cairo. But you'd know better than I would here so I defer to you - thanks!

TheCedarPrince avatar Jan 05 '22 03:01 TheCedarPrince

TL;DR: Cairo.jl does all the font-finding and font-rendering work for Luxor.jl, and I don't know enough about how it works or whether adding new features would be possible. I wish I could be more helpful. :(

I think there are two tasks here: finding the desired font files on disk, and extracting the glyph outlines to draw the glyphs on some surface. I suspect Cairo.jl and Makie.jl use different code paths for both tasks.

I don't know what Makie.jl does. ¯\_(ツ)_/¯

Finding fonts

I think Cairo.jl uses fontconfig to load a list of active fonts from the operating system. For example, to render LaTeX text correctly, I first copied the fonts from MathTeXEngine/assets/fonts directory to my font manager's folder and activated them. Each platform uses different ways of installing and activating fonts. Last year we investigated some Julia-based ways of installing the JuliaMono fonts but made no progress.

Drawing glyphs

Presumably Cairo is using FreeType on Linux, Quartz on MacOS, and Win-something on Windows? (See https://cairographics.org/manual/cairo-fonts.html)

When drawing text to PNGs and GIFs, I think Cairo.jl passes the job of actually rasterizing the glyph outlines to the operating system's text rendering routines. This might explain why you get different output on different platforms see this section.

When drawing text to SVGs, Cairo.jl extracts the outlines and converts them to Bezier-based paths; this is also how you can draw text outlines in Luxor.

When drawing text to EPS and PDF, Cairo.jl just makes a copy of a subset of the original font data and embeds it in the file.

That's about all I know... :)

cormullion avatar Jan 05 '22 12:01 cormullion

This will be fixed on the Cairo side, all font information is included by MathTeXEngine through FreeTypeAbstraction.

Kolaru avatar Oct 15 '22 19:10 Kolaru