Edit | 16.08.2022 : You can now download via the official asset library here.
Edit | 12.08.2022 : You can visit the project's repository here.

Project Diary: GodoTeX

Hey folks. I'm struggling a little with the new forums, so I hope you can read this ;^o

So I want to develop a plug-in that adds LaTeX to Godot and am researching the best way to do that.

Currently, I'm thinking of using GDNative (or I guess GDExtention in the future) and dynamically linking to a modified version of an open source LaTeX library like MicroTeX.

I'm a little unsure what that would even look like exactly, maybe generating an SVG and displaying that is an option.

So you see - I'm still in the early stages and would really love to hear how you would do this. Any ideas?

Yeah GDNative would work. I guess SVG would be the correct format.

Megalomaniak Thank you! It's going to be difficult but I'm sure it'll work in the end.

@cybereality Alright, here's the deal:

Serendipidously, I already have something resembling working software! I found something called CSharpMath, which is a C#-library for rendering LaTeX that - after some fiddling - I could import into Godot Mono. It has has a bunch of different frontends for directly displaying the expressions, but of course none of them has anything to do with Godot. SVG also isn't supported.

So my first working version renders to a PNG, jams that into a ByteArray, turns that into the engine's implementation of an image and then pushes this into a texture. Behold the (maybe?) first LaTeX rendered inside Godot:

Well, it's not the prettiest, but it does work.

I'm conflicted where to go from here. I wonder if I can somehow improve the results of this approach. As it says in the library's Readme though, it is known to have artifacts which I already could see without trying to break it - not to speak of using it (old software development joke). If I was good at C#, I would try to implement the library's specifications for a drawing frontend and execute it inside Godot's CanvasItem's _draw() method, right? If anyone sees a better way, please tell me.

(My attempts with C++ were all all about as successful as Team Rocket in the Pokémon anime. If I see one more compiler error, I'm going to cry.) (Edit: Grammar)

In theory, you could do this with the Godot drawing methods, but it would be a LOT of work. You'd basically be creating your own font renderer. I mean, it's possible, but it may be a lot of effort. Importing as PNG is probably fine. If it is high resolution, that is probably okay, and good enough for reading. Or maybe see if there is a better library. I don't know.

    23 days later

    cybereality Thank you for the call to reason. After some tinkering, it does work quite well in image format!

    My last roadblock is exporting. It runs great in the editor, but on an exported project the following exception is thrown:
    System.TypeInitializationException: The type initializer for 'SkiaSharp.SKImageInfo' threw an exception. ---> System.DllNotFoundException: Unable to load library 'libSkiaSharp', but strangely only at the part that outputs the image, not before.

    I've tried

    • using Godot 3.2.x, 3.4.4 and 3.5 beta 4
    • switching to dotnet CLI instead of MSBuild
    • trying different export settings, include PKG and such
    • putting DLLs next to the produced .exe-file
    • checking the log file (it's empty)

    , but to no avail.

    For Godot 3.4.4 and 3.5 beta 4, it also outputs ERROR: Mono: Core API hash mismatch., but that doesn't seem to be the root cause here, because this error disappears on 3.2.3 while the above doesn't and well, the program doesn't run.

    Does anyone have an idea of how to fix this or approaches for a workaround?

    cybereality No, sadly - it doesn't cover exporting at all. It was a good introduction though, when I started the project.

    Praise the heavens, LaTeX in an exported project works now. I'm so happy, I just had to use bold typeface.

    For posterity: The library that was not being found in the exported project is what's apparently called a native library, namely .dll files compiled from c++-code for the specific platform you're building for. I don't know for sure, but the build configuration in the editor seemingly has access to them or knows where they are, but the exported game does not. Luckily, there are nuget-packages that explicitely include this native library as a fixed dependency. I used SkiaSharp.NativeAssets.Win32. If it's not referenced, the build system will still download and use them in the editor, but they - for example - won't show up in the dependency tree in Visual Studio.

    There are still some issues to be ironed out with exporting, but at least it works in principle now.

    That literally took two weeks. C# is a hell of a drug folks. Anyways, here's a gif to celebrate:

    11 days later

    OK, next update: Thanks to the generous help from one of the developers/contributers to the SkiaSharp library, Mattew Leibowitz, it also works on Linux now. Exporting is functional, too - yay!

    I'll continue testing and tinkering, but this is in a pretty usable state already. If you want to try it out and give feedback, you can have a look at the repository.

    And another update: The add-on is now live on the asset library. If you've made an asset before, you'll know - seeing your little thing in the engine's browser is really fun! It's this one. :^)

    You can include DLLs by setting up the dependencies in GDNative.

    https://docs.godotengine.org/en/stable/tutorials/scripting/gdnative/gdnative_c_example.html

    [general]
    
    singleton=false
    load_once=true
    symbol_prefix="godot_"
    reloadable=true
    
    [entry]
    
    OSX.64="res://bin/libsimple.dylib"
    OSX.32="res://bin/libsimple.dylib"
    Windows.64="res://bin/libsimple.dll"
    X11.64="res://bin/libsimple.so"
    
    [dependencies]
    
    OSX.64=[  ]
    OSX.32=[  ]
    Windows.64=[  ]
    X11.64=[  ]

    I'm not sure what specific issue you're addressing, but I'm guessing the one on Github regarding building on Linux. If so, thanks, that looks helpful! I'll try that!

    5 days later

    Next Update: I've tried GDNative but didn't get it to work. Maybe it's only for working with C++ dlls after all? Oh well, the workaround as written on GitHub does work, too, it's maybe just not as pretty.

    On a happier note, I've added a Button Class for clickable LaTeX Expressions:

    They're really fun to play with! :^)

    21 days later

    OK, I think I'm basically finished with development. I made a demo project showcasing different elements of the plugin with a fun gameplay idea, which is available here!

    Thank you for reading and see you around :^)