XmacsLabs / mogan

Mogan STEM Suite (based on GNU TeXmacs): Enjoy exploring science and technology!
https://mogan.app
GNU General Public License v3.0
356 stars 29 forks source link

Project: OpenType math font support #523

Open mgubi opened 1 year ago

mgubi commented 1 year ago

At the moment TeXmacs (and Mogan) are able to use only very few math fonts (like Stix, TeX gyre) and this support is hardwired in the C++ files and does not take into account all the informations available in the font files.

Some time ago I've started to implement some OpenType font support for TeXmacs, here: https://github.com/mgubi/texmacs/tree/wip-unicode-math

If some other developer is interested we could try to make it working in Mogan. TeXmacs still depends very much on old font formats while there are now available many free fonts in OpenType format which make it easier to support professional math typesetting. In particular there are Latin Modern fonts which can replace our old TeX fonts, and possibly improve speed since there will be less font searching.

One side goal could be to get rid of all TeX fonts and distribute only the OpenType variants. This is for example what ConTeXt does.:

https://wiki.contextgarden.net/ConTeXt_distribution%27s_Fonts

da-liii commented 1 year ago

Last time, I tried to upgrade the old TeX fonts, but failed. And discussed with @YiqiXu , he agree that I can set the Stix font as the default math font.

@mgubi Do you think setting Stix font as the default Math font is a good idea for Mogan? Or is there a better choice?

mgubi commented 1 year ago

Latin modern is an OTF replacement for the standard TeX fonts. The problem is that TeXmacs does not typeset well yet any OTF math font but only some of them which are hardcoded (Stix, Pagella, ...). I personally do not like Stix. Ideally We need to support uniformly OTF fonts, as I propose here.

YiqiXu commented 1 year ago

To clarify, I recommend setting STIX2 as the default fallback font for Mogan, because it provides the broadest support for mathematical symbols compared to other popular unicode-math fonts.

The current default font, TeXmacs Computer Modern, has limited support for math symbols, requiring the use of a macro to specify a font or changing the document font altogether to type unsupported symbols. In most cases, STIX is sufficient for these purposes. As a result of my experience, I have observed that this situation often causes confusion or frustration among new users. Therefore, I think that implementing a fallback math font would be beneficial. This would also provide an opportunity to extend Mogan's support for STIX2, since STIX2 has almost completely replaced STIX in LaTeX.

While a fallback font is a temporary solution, @vdhoeven's machinery of semantic math fonts (outlined in his paper Mathematical Font Art) offers a more elegant solution. However, I have unfortunately not yet fully understood how it works, and in practice, only a limited number of symbols and fonts support this feature. To provide a better solution, I suggest expanding @vdhoeven's machinery to support a larger number of OTF fonts in the future.

mgubi commented 1 year ago

The problem is that right now we do not have a robust way to locate glyphs in a math font. The current machinery is hard-coded by Joris in the font handling code (in smart_fonts but also other places). My unicode_math branch is an attempt to parse the MATH table in the OTF fonts and use it: it contains the placements within the font of the math glyphs (e.g. the various sizes of parentheses, the spacings, the extensible elements). If this become robust we can use any OTF font for math without hardcoded stuff. We would maybe still like to fine tune some spacings but it would be more robust to add other fonts. I would be happy to discuss with people interested what I learned about this. It would be great if we can improve my branch. In the meanwhile you can give a look at its changes to see the points in the TeXmacs codebase where specific code for maths fonts is handled/needed.

mgubi commented 1 year ago

Let me expand my comment: currently TeXmacs is unable to use a font like STIX2 without writing some specialised code. What code: one can look at the source to see how Joris handles fonts like "Pagella" or the "TeX Gyre" family. Many parameters and glyph indexes are hardcoded and they vary from font to font. To support STIX2 as a fallback font one would need to hardcode similar information. But all this informations are already present in the font in its MATH table which currently TeXmacs ignore. My branch contains code to read the table and take the info into account. It was working fine the last time I was working on it, but requires still some more work to be good enough for production. Also maybe some restructuring of the font handling code which is not easy to do in TeXmacs but I hope is easier in Mogan where we can experiment a bit more.

da-liii commented 1 year ago

currently TeXmacs is unable to use a font like STIX2 without writing some specialised code.

Those specialized code should be maintained in scheme code as a font plugin. That's my plan.

mgubi commented 1 year ago

currently TeXmacs is unable to use a font like STIX2 without writing some specialised code.

Those specialized code should be maintained in scheme code as a font plugin. That's my plan.

It is not clear this is possible. Ideally I agree with you but I think most of these code paths are performance critical and would slow down typesetting to put them in scheme, unless one design a reliable caching mechanism.

Also: most of the tweaking Joris was doing should be not necessary once we can access MATH font informations and metrics from the font files. This is the goal of my branch: it implements code which reads the MATH font tables and set up the font with the right metrics for proper placement of sub/superscripts, square roots, parentheses... Then if one wants, on top of that, one could still implement some micro-management of tweaks to improve math typesetting even more, but the main goal is to have a broad set of good fonts one can use in math.

da-liii commented 6 months ago

but I think most of these code paths are performance critical

Yes, just improved the performance a lot in 11_17 for Mogan v1.2.1.