Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[LaTeX Bug] (Not Urgent) Certain glyphs scale wrongly #291

Closed
RobinCoffee opened this issue Apr 14, 2024 · 4 comments
Closed

[LaTeX Bug] (Not Urgent) Certain glyphs scale wrongly #291

RobinCoffee opened this issue Apr 14, 2024 · 4 comments

Comments

@RobinCoffee
Copy link

RobinCoffee commented Apr 14, 2024

I am using fyne.io as the renderer, I can't see how anything in my implementation could be influencing the pathing. (It should be noted that this is far better than not rendering at all! Thanks for fixing that part :D )
Here is my implementation:

package main

import (
	"fyne.io/fyne/v2/app"
	"fyne.io/fyne/v2"
	"github.com/tdewolff/canvas"
	canvasFyne "github.com/tdewolff/canvas/renderers/fyne"
	"fmt"
)

func drawLatex(latex string, ctx *canvas.Context) {
	formula, err := canvas.ParseLaTeX(latex)
	
		fmt.Println(formula)
	if err != nil {
		panic(err)
	}

	ctx.Scale(20, 20)
	ctx.SetFillColor(canvas.Black)
	ctx.DrawPath(200, 400, formula)
}

func main() {
	a := app.New()
	w := a.NewWindow("Some mighty fine math you got there ;)")

	c := canvasFyne.New(800.0, 600.0, canvas.DPI(300.0))
	ctx := canvas.NewContext(c)

	w.Resize(fyne.Size{800, 600})
	drawLatex("\\left(\\frac{\\sqrt{35x^{15-b^{3}}}}{2}\\right)^{2}", ctx)

	w.SetContent(c.Content())
	w.ShowAndRun()
}

This works great aside from a few strange rendering bugs:
This formula '\int_{2}^{5} \frac{\sqrt{35x^{15-b^{3}}}}{2}' should look like this (shown in Obsidian (which uses MathJax iirc))
Screenshot 2024-04-13 at 6 11 27 PM
In my program it looks like this:
Screenshot 2024-04-13 at 6 11 46 PM
Other similar issues occur with square roots and parenthesis: (these formulas: "\sqrt{\frac{\sqrt{35x^{15-b^{3}}}}{2}}" and "\left({\frac{\sqrt{35x^{15-b^{3}}}}{2}}\right)^2")
Obsidian:
Screenshot 2024-04-13 at 10 51 07 PM
Screenshot 2024-04-13 at 10 56 05 PM
My program:
Screenshot 2024-04-13 at 10 52 33 PM
Screenshot 2024-04-13 at 10 56 36 PM

Again, if there are other more pressing bugs tackle those first, just wanted to make you aware about this in case it is an easy fix or you are bored and looking for something to do :P

@RobinCoffee
Copy link
Author

Btw this formula "\frac{\sqrt{35x^{15-b^{{{{3}^{a^{b^{c^{d^{e^{f^{g^{h^{i^{j}}}}}}}}}}}}}}}}{2}" albeit very stupid crashes the program. It is valid LaTeX, I checked it several times. This is what it should look like:
Screenshot 2024-04-14 at 1 29 01 PM
Instead I get this error after running ./main:

This is TeX, Version 3.141592653 (INITEX)
**(output.tex
(TeXinputs:plain.tex Preloading the plain format: codes, registers,
parameters, fonts, more fonts, macros, math definitions, output routines,
hyphenation (TeXinputs:hyphen.tex)) [1] )
Output written on output.dvi (1 page, 564 bytes).
Transcript written on output.log.
<nil>
panic: bad command: stack is empty at position 361

goroutine 1 [running, locked to thread]:
main.drawLatex({0x1030595bc?, 0x10304b268?}, 0x14000529e00)
	/Users/24ayden.zinter/GOSTUDY-Shared/gui-playground/fyne/main.go:24 +0x130
main.main()
	/Users/24ayden.zinter/GOSTUDY-Shared/gui-playground/fyne/main.go:42 +0x200

I just checked what would happen if I removed an exponent ("\frac{\sqrt{35x^{15-b^{{{{3}^{a^{b^{c^{d^{e^{f^{g^{h^{i}}}}}}}}}}}}}}}{2}") and it ran. It looks like this tho:
Screenshot 2024-04-14 at 1 35 03 PM
I'm actually so confused, if you remove another exponent you get a similar error. For some reason rendering this exact amount of exponents doesn't crash. It doesn't start working again until the formula "\frac{\sqrt{35x^{15-b^{3^{a}}}}}{2}" it still fails to render the higher exponents:
Screenshot 2024-04-14 at 1 44 36 PM
This stuff may be related to the scaling issues, idk.

@tdewolff
Copy link
Owner

Yes, I've noticed similar issues as reported by #231. The problem is with the star-tex.org/x/tex library however, and I've actively looked for an improvement but there is no other library it seems. Improving the library is also difficult, it seems to be a non-trivial amount of work and the author(s) and I don't have a lot of time to fix it.

There are two options: implement font parsers for the LaTeX font formats (quite some work), or port the same library for XeTex which has unicode support (quite some work too). Rewriting a formula parser and parsing the GSUB etc tables for fonts would be even more work.

I'm happy if you can find a good alternative, but unfortunately I don't have time for such a large project right now.

@RobinCoffee
Copy link
Author

No worries, appreciate the fast reply tho. Building with -latex is annoying but I'm willing to deal with it for the time being. I figured star-tex would be the problem.
Screenshot 2024-04-14 at 4 16 39 PM
Works fine with -tags latex. Somewhat annoying, but better than the original solution I came up with of running a KaTeX or Mathjax server locally and integrating it into my application. Maybe once I am a better programmer I can work on a parser or something. For now I will just pretend like the problem doesn't exist. Thanks for all of your help anyway :D

@tdewolff
Copy link
Owner

Yes, using the system's LaTeX binary will work if you control the target machine. Great to hear you have that option, and you're always welcome to try and submit PRs if you wish! Thank a lot for all the feedback and I hope this library can help you build something useful ;-)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants