CSharpMath icon indicating copy to clipboard operation
CSharpMath copied to clipboard

Incorrect spacing and bounding box

Open sadqiang opened this issue 6 years ago • 30 comments

Describe the bug There are incorrect spacing and bounding boxes as indicated by green ellipses. The top is created by LaTeX and the bottom is created by CSharpMath.

  • [ ] comparer-05
  • [ ] comparer-00
  • [ ] comparer-01
  • [ ] comparer-02
  • [ ] comparer-04
  • [ ] comparer-07
  • [ ] comparer-08
  • [ ] comparer-09

To Reproduce Make sure you have TeX distribution installed. Download the VS solution from my rep: https://github.com/sadqiang/GitHubReport/tree/master/GitHubReport.

Questions

  • [ ] 1. What causes the size of LaTeX output to be smaller than that of Skia's ? I already set 12pt for both actually.
  • [ ] 2. Why can't we use the following?
\Biggl(\biggl(\Bigl(\bigl((x)\bigr)\Bigr)\biggr)\Biggr)
y=\cfrac{x}{1+\cfrac{x}{1+\cfrac{x}{1}}}
\mathscr{L}\left\{f(t)\right\} = F(s) 
  • [ ] 3. Can we change the math font from the default to, for example, Palatino, Bera, etc?

sadqiang avatar Jan 26 '19 12:01 sadqiang

Some of these are deficiencies from iosMath that this library was ported from. I'll keep this in mind in the upcoming rewrite.

Happypig375 avatar Jan 26 '19 13:01 Happypig375

@Happypig375 : Is there a roadmap (a schedule of updates, versioning, etc) for CSharpMath ?

sadqiang avatar Feb 01 '19 13:02 sadqiang

See the bottom part of #29, basically CSharpMath is in a process of being rewritten.

Happypig375 avatar Feb 01 '19 14:02 Happypig375

comparer-00 The 𝑃 case is interesting. It has a smaller glyph advance than the whole character width which makes sense for italic characters following it, but causes problems for non-italic characters such as ). image wpf-math, iosMath, CSharpMath and Typography follow the glyph advance resulting in the same output, while MathJax and KaTeX follow the character width, ignoring the glyph advance.

@prepare would ignoring the glyph advance be more suitable?

Happypig375 avatar Jun 09 '20 07:06 Happypig375

I think in this case for math expression + math-font, Exact glyph bounds should be used. (ignore glyph's advance) (since each glyph position is controled by Math layout engine)

But if you want to layout 'human text' with general font. It should use glyph's advance (since the result glyph advances are processed by GSUB, GPOS)


math-asana GSUB, GPOS with AsanaMath, LatinModernMath, XITSMath


(𝑃𝑃𝑃) latin_modern_notepad Notepad, LatinModernMath


@brezza92 (Typography-based MathML implementation) What do you think about this?,

Did you use glyph's advance or exact glyph bounds?

prepare avatar Jun 09 '20 16:06 prepare

My MathML implement use glyph's advance this it my result. It has same problem. Capture13

brezza92 avatar Jun 10 '20 15:06 brezza92

I have an idea that, a cluster of letter may be treated as a human text (eg. some variable name may need GSUB/GPOS). and then find exact bounds of a cluster.

other else (eg math signs + - * / etc) should use exact bounds.

so (𝑃𝑃𝑃) should be split to 3 parts as image below (edited by hand). (central part 𝑃𝑃𝑃 => a cluster of letter, can use GSUB/GPOS)

idea01 a cluster of 𝑃𝑃𝑃 with GSUB/GPOS

prepare avatar Jun 10 '20 17:06 prepare

What about when italic characters are mixed with upright characters?

Happypig375 avatar Jun 11 '20 03:06 Happypig375

What about when italic characters are mixed with upright characters?

please give me some example and your expected result

prepare avatar Jun 11 '20 04:06 prepare

image

Happypig375 avatar Jun 11 '20 08:06 Happypig375

Sure, That side effect will happen in the same GSUB/GPOS layout block

ppp_01

Then, It should be considered that => the block of consecutive letter need gsub/gpos layout process or not.

In latin script=> may not,

but in other script => may be, some script need GSUB/GPOS to get correct final output. (eg. if the math expression contains some variable in non-latin letter).

prepare avatar Jun 11 '20 09:06 prepare

So what should be considered as "a block of consecutive letters"? Same Unicode block? Same Unicode category?

Happypig375 avatar Jun 11 '20 09:06 Happypig375

not sure if https://docs.microsoft.com/en-us/dotnet/api/system.char.isletter?view=netcore-3.1 is OK?

If all letters are latin or math char=> it is OK to use only exact bounds, no need for GSUB,GPOS.

In math layout, When to use GSUB/GPOS?,

I think when we found a char from other script. (some unicode ranges that related to script)

Not sure that If it possible to put a char from other script (eg arabic, chinese, emoji), into math-expression or not,

If it possible, and no glyph inside a math-font, How to present it ?

and it means that block of text may need GSUB/GPOS.

prepare avatar Jun 11 '20 11:06 prepare

David Carlisle told me that we cannot win. See section 7.5.5-7.5.6.

mauler2025 avatar Jun 12 '20 13:06 mauler2025

@pstricks-fans What is "we cannot win"?

Happypig375 avatar Jun 12 '20 13:06 Happypig375

@prepare

System.Char.IsLetter("𝑃P", 0) // true => 𝑃 is a letter
System.Char.IsLetter("𝑃P", 2) // true => P is a letter

Happypig375 avatar Jun 12 '20 13:06 Happypig375

@Happypig375 : I am not sure the meaning. It might mean it is impossible to get it right. Here is his statement.

mauler2025 avatar Jun 12 '20 14:06 mauler2025

@pstricks-fans I'd say that at least getting the spacing specified in the original post counts as fixing this case.

Happypig375 avatar Jun 12 '20 14:06 Happypig375

@prepare

System.Char.IsLetter("𝑃P", 0) // true => 𝑃 is a letter
System.Char.IsLetter("𝑃P", 2) // true => P is a letter

Yes ?

ppp_01

Sure, That side effect will happen in the same GSUB/GPOS layout block


This is limitation of 𝑃P , if you put it in the same GSUB/GPOS block,

in the real world => It should be rarely used?

prepare avatar Jun 12 '20 14:06 prepare

After reading ... from http://texdoc.net/texmf-dist/doc/context/documents/general/manuals/luatex.pdf#page=116

7.5.6 Script and kerning ... ... ... In 2018 fonts like Latin Modern and Pagella rely on cheatswith the boundingbox, Cambria uses staircase kerns and Lucida a mixture. Depending on howfonts evolve we might add some more control over what one can turn on and off.

EDIT: I'm not sure that GSUB/GPOS technique is good for layout some Math fonst or not? not sure that some Math fonts are designed with the GSUB,GPOS mechanism in mind or not?

Do you know who create/maintenance/update Latin-modern math font?

SORRY about what I've said, =>it come from my little knowledge :(

see => https://github.com/verybadcat/CSharpMath/issues/32#issuecomment-649200044

prepare avatar Jun 12 '20 14:06 prepare

Latin Modern Math's last update is 6 years old and is pretty much unmaintained. However, I love its glyphs and would like to retain it as the default font.

Happypig375 avatar Jun 12 '20 15:06 Happypig375

Useful information about Math symbols advance width

https://docs.microsoft.com/en-us/typography/develop/character-design-standards/math

prepare avatar Jun 13 '20 02:06 prepare

@prepare What about storing an italic measure (glyph.MathGlyphInfo?.ItalicCorrection) when positioning each glyph? So if the current character has less italic correction than the previous character, the difference becomes additional spacing?

Happypig375 avatar Jun 17 '20 04:06 Happypig375

I've overlooked that info!

The Math Font file (eg Latin Modern Math, Asana Math)

contain that info.

It may be used in this situation.

When a run of slanted characters is followed by a straight character (such as an operator or a delimiter), the italics correction of the last glyph is added to its advance width.

see detail more detail =>

https://docs.microsoft.com/en-us/typography/opentype/spec/math#mathitalicscorrectioninfo-table

prepare avatar Jun 17 '20 10:06 prepare

@brezza92, please check this info (https://github.com/verybadcat/CSharpMath/issues/32#issuecomment-645291053) too.

prepare avatar Jun 17 '20 10:06 prepare

Math Layout in "In-span" italic correction

from https://github.com/verybadcat/CSharpMath/issues/32 and should be solved by (https://github.com/verybadcat/CSharpMath/issues/32#issuecomment-645134531 and https://github.com/verybadcat/CSharpMath/issues/32#issuecomment-645291053)

math_italic_not_set_scriptLang pic 1: No italic correction if not set ScriptLang property to "math"


Users can layout math manually entire process by their own MathLayout engine.

But the GlyphLayout class also have a help for layouting "in-span" italic glyph.

  1. EnableBuiltinMathItalicCorrection (https://github.com/LayoutFarm/Typography/commit/ccad43bb2130137b71958d8b0aa8b046e3a82a15) is true by default

BUT

  1. GlyphLayout ScriptLang must be set to new ScriptLang("math") too.

see detail impl => https://github.com/LayoutFarm/Typography/commit/ccad43bb2130137b71958d8b0aa8b046e3a82a15#diff-b705c14388c05643560a455e65eaf449R468


math_italic_set_scriptLang pic 2: 'in-span' math italic correction

f_italic pic 3: latin modern math, 20pt, math italic correction

[A] (𝐹𝐹𝐹), [B] (FFF), [C] (𝐹F𝐹)

prepare avatar Jun 25 '20 03:06 prepare

Please test it :)

prepare avatar Jun 25 '20 03:06 prepare

Will do, thanks

Happypig375 avatar Jun 25 '20 04:06 Happypig375

Blocked on https://github.com/verybadcat/CSharpMath/issues/107#issuecomment-657902124

Happypig375 avatar Jul 14 '20 09:07 Happypig375

@Happypig375 We're having this issue too for Matrices, seems like it's the same issue? image

kevcrooks avatar Jul 23 '21 11:07 kevcrooks