NanoMichael / MicroTeX

A dynamic, cross-platform, and embeddable LaTeX rendering library
MIT License
399 stars 66 forks source link

What are CLM files and where to get them? #174

Open afalkenhahn opened 1 month ago

afalkenhahn commented 1 month ago

When initializing MicroTeX I always need to pass a CLM file as well as an OTF file. I'm currently using the latinmodern-math.clm1 that is part of the MicroTeX distribution and that works just fine but what if I want to use some arbitrary font that is installed on the user's system? There are no CLM files but just OTF/TTF/TTC files in the system's font directory.

So what if I want to use some arbitrary system font? Do I first have to generate a CLM file for that before it can be used with MicroTeX or how does that work? I've never heard of the CLM format before and Google doesn't really help here either. Is CLM a format proprietary to MicroTeX or is it some sort of standard?

Thanks for some clarification!

sp1ritCS commented 1 month ago

clm files contain precalculated metadata needed by µTeX to render glyphs, which we don't want to do at runtime. The name comes from cLaTeXMath, which was the name of the original library (thats whats still in the master branch) altho ironically cLaTeXMath doesn't use those clm files.

Specifically, .clm1 files contain only metadata and .clm2 files contain both meta- and glyph data for usecases where there isn't a library available capably of laying out glyphs from font files.

They can be generated using the otf2clm.py script.

afalkenhahn commented 1 month ago

Thanks! Have you got some more information about the "use cases" you mentioned, that is which use cases require .clm1 and which require .clm2 files? Is that related to the different backends (cairo, GDI+, Flutter...) supported by MicroTeX or what exacxtly do you mean by "use case"?

sp1ritCS commented 1 month ago

Yes. The backends provide a set of (more or less) primitive vector operations.

MicroTeX doesn't actually read the otf font files, they just get passed to the backend on initialization. This way, characters can be drawn using drawGlyph(codepoint) directly. But not every backend supports that, so characters can also be drawn using the vector operations of the backend.

afalkenhahn commented 1 month ago

Ok, is there an overview which backends require .clm2 and which are fine with .clm1? For example, I'm using the cairo backend. Does that require .clm1 or .clm2? I suppose .clm1 because looking at the backend source it passes the otf font file to fontconfig and then converts it into a cairo font face so I suppose the cairo backend doesn't require .clm2? But what about the other backends? Which ones need .clm1 and which need .clm2?

sp1ritCS commented 1 month ago

Im not sure, the whole backend situation gets even more complicated, as some backend builds on top of others. AFAIK, there is gtk which uses cairo; qt, skia and gdi are independent and flutter and wasm build ontop of wrapper, which serializes draw calls first.

While all backends seem to have implemented drawGlyph, I suspect some backends can't load arbitrary fonts (at least wasm can't)