NanoMichael / MicroTeX

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

[Branch: openmath] Italic and bold maths with XITS #132

Closed jokteur closed 1 year ago

jokteur commented 1 year ago

Hi,

I have posted a few issues already here, but this is more like because I am trying to figure out this library by myself. I have more or less implemented MicroTex with ImGui, and now I am trying to work some kinks out.

So, I am using the XITS font family, and I am manually declaring it like so (I don't use the HAVE_AUTO_FONT_FIND flag):

const FontSrcFile math_regular("data/xits/XITSMath-Regular.clm2", "data/xits/XITSMath-Regular.otf");
const FontSrcFile math_bold("data/xits/XITSMath-Bold.clm2", "data/xits/XITSMath-Bold.otf");
const FontSrcFile xits_boldItalic("data/xits/XITS-BoldItalic.clm2", "data/xits/XITS-BoldItalic.otf");
const FontSrcFile xits_regular("data/xits/XITS-Regular.clm2", "data/xits/XITS-Regular.otf");
const FontSrcFile xits_bold("data/xits/XITS-Bold.clm2", "data/xits/XITS-Bold.otf");
const FontSrcFile xits_talic("data/xits/XITS-Italic.clm2", "data/xits/XITS-Italic.otf");
MicroTeX::init(math_regular);
MicroTeX::addFont(math_bold);
MicroTeX::addFont(xits_boldItalic);
MicroTeX::addFont(xits_regular);
MicroTeX::addFont(xits_bold);
MicroTeX::addFont(xits_talic);

So, I am expecting that MicroTeX should be able to figure out when to apply bold, when to apply italic. This already works fine for normal text: \textit{Hello, }\textbf{world} gives me image

However, when using math symbols: dV = \mathbf{g}_{(i)}\cdot (\mathbf{g}_{(j)}\times \mathbf{g}_{(k)})d\xi_id\xi_jd\xi_k gives me image instead of something like: image

What am I doing wrong ? Clearly, the loading of different fonts works in my implementation as illustrated by the hello world example. Also, when I look into the charmap of XITSMath-Regular, the italic symbols are there, for example xi: image

NanoMichael commented 1 year ago

You should not use the font XITSMath-Bold, this font is not full uni-math supported, remove the following lines should work:

const FontSrcFile math_bold("data/xits/XITSMath-Bold.clm2", "data/xits/XITSMath-Bold.otf");
MicroTeX::addFont(math_bold);

But after adding bold math font should have worked too, I'll fix it later.

jokteur commented 1 year ago

I remove math_bold like you said, but I still don't have italic maths.

I saw that the CLMReader in the source code is responsible for assigning the correct math symbols. I took xits/XITSMath-Regular.clm2 from the res folder in MicroTeX. Is it possible that the clm files do not indicate correctly the symbols for italic ?

NanoMichael commented 1 year ago

That's weird, I can't reproduce this issue with the Cairo impl. If you don't mind, could you make a PR for your impl?

jokteur commented 1 year ago

ImGui requires utf8 when drawing text. So I passed the utf8 codes instead of the glyphId, meaning that italic got converted into regular. Example: s regular is 115 utf8 and 116 glyphId, and s italic is 115 utf8 and 2519 glyphId.

I need to look into on how to fetch characters with glyphId in ImGui. I may have to touch ImGui internals.

NanoMichael commented 1 year ago

Oh, I see. If you compiled with option -DGLYPH_RENDER_TYPE=0 (that means draw glyphs use graphical-path and typeface both, defaults to 0), you can switch to path rendering to check if it works, add the following code to achieve it:

MicroTeX::setRenderGlyphUsePath(true);
jokteur commented 1 year ago

In the end, I used MicroTeX::setRenderGlyphUsePath(true); and cairo for the rendering.

Other solutions for the moment were not practical. I may one day write a simple library agnostic renderer for microtex.