latex bot discord
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
using Godot;
using System.IO;
using CSharpMath.SkiaSharp;
using CSharpMath.Rendering;
using SkiaSharp;

[Tool]
public class LaTeXture : ImageTexture {
	public string LatexExpression;
	public float FontSize = 40f;
	public Color MathColor = new Color(0,0,0,1);
	public bool AntiAliasing = true;
	public bool Fill = true;
	public bool ShowError = true;
	
	private MathPainter Painter = new MathPainter{};
	
	public float Width;
	public float Height;
	public float OffsetX;
	public float OffsetY;
	
	public void Render() {
		var r = (byte)(255*this.MathColor.r);
		var g = (byte)(255*this.MathColor.g);
		var b = (byte)(255*this.MathColor.b);
		var a = (byte)(255*this.MathColor.a);
		
		var paintStyle = CSharpMath.Rendering.FrontEnd.PaintStyle.Stroke;
		if (this.Fill) {
			paintStyle = CSharpMath.Rendering.FrontEnd.PaintStyle.Fill;
		}
		
		this.Painter = new MathPainter {AntiAlias = this.AntiAliasing, TextColor = new SKColor(r, g, b, a), FontSize = this.FontSize, LaTeX = @"\raisebox{40mu}{}\raisebox{-40mu}{}" + this.LatexExpression + @"\:\raisebox{1mu}", DisplayErrorInline = this.ShowError, PaintStyle = paintStyle};
		
		var measure = this.Painter.Measure();
		this.Width = measure.Width;
		this.Height = measure.Height;
		this.OffsetX = measure.X;
		this.OffsetY = measure.Y;
		
		using (var png = this.Painter.DrawAsStream()) {
			var image = new Godot.Image();
			
			using (var memoryStream = new MemoryStream()) {
			  png.CopyTo(memoryStream);
			  image.LoadPngFromBuffer(memoryStream.ToArray());
			}
			
			this.CreateFromImage(image);
		};
	}
}