forked from verybadcat/CSharpMath
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMathPainter.cs
More file actions
59 lines (57 loc) · 2.79 KB
/
MathPainter.cs
File metadata and controls
59 lines (57 loc) · 2.79 KB
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
54
55
56
57
58
59
using System.Drawing;
namespace CSharpMath.Rendering.FrontEnd {
using System.Runtime;
using BackEnd;
using Display;
public abstract class MathPainter<TCanvas, TColor> : Painter<TCanvas, Atom.MathList, TColor> {
protected bool _displayChanged = true;
public override IDisplay<Fonts, Glyph>? Display { get; protected set; }
protected override Atom.Result<Atom.MathList> LaTeXToContent(string latex) =>
Atom.LaTeXParser.MathListFromLaTeX(latex);
protected override string ContentToLaTeX(Atom.MathList mathList) =>
Atom.LaTeXParser.MathListToLaTeX(mathList).ToString();
public override RectangleF Measure(float unused = float.NaN) => base.Measure(unused);
protected override void SetRedisplay() => _displayChanged = true;
protected override void UpdateDisplayCore(float unused) {
if (_displayChanged) {
Display = Content == null
? null
: Typesetter.CreateLine(Content, Fonts, TypesettingContext.Instance, LineStyle);
_displayChanged = false;
}
}
public override void Draw(TCanvas canvas, TextAlignment alignment = TextAlignment.Center, Thickness padding = default, float offsetX = 0, float offsetY = 0) {
var c = WrapCanvas(canvas);
UpdateDisplay(float.NaN);
DrawCore(c, Display, Display == null ? new PointF?() : IPainterExtensions.GetDisplayPosition(Display.Width, Display.Ascent, Display.Descent, FontSize, c.Width, c.Height, alignment, padding, offsetX, offsetY));
}
public void Draw(TCanvas canvas, float x, float y) {
var c = WrapCanvas(canvas);
UpdateDisplay(float.NaN);
DrawCore(c, Display, new PointF(x, -y)); // Invert the canvas
}
/// <summary>
/// Directly draw the given <paramref name="display"/>. Repositions the <paramref name="display"/>.
/// </summary>
public void DrawDisplay(IDisplay<Fonts, Glyph>? display, TCanvas canvas, float x, float y) {
if (display is null) return;
var original = (Display, _displayChanged);
(Display, _displayChanged) = (display, false);
Draw(canvas, x, y);
(Display, _displayChanged) = original;
}
/// <summary>
/// Directly draw the given <paramref name="display"/>. Repositions the <paramref name="display"/>.
/// </summary>
public void DrawDisplay(IDisplay<Fonts, Glyph>? display, TCanvas canvas,
TextAlignment textAlignment = TextAlignment.Center,
Thickness padding = default, float offsetX = 0, float offsetY = 0) {
if (display is null) return;
var original = (Display, _displayChanged);
(Display, _displayChanged) = (display, false);
Draw(canvas, textAlignment, padding, offsetX, offsetY);
(Display, _displayChanged) = original;
}
public new MathPainter<TCanvas, TColor> ShallowClone() => (MathPainter<TCanvas, TColor>)MemberwiseClone();
}
}