Skip to content

Commit 6678311

Browse files
committed
0.4.1: Fixed Measure in CSharpMath.Rendering
1 parent 258a99d commit 6678311

File tree

11 files changed

+89
-18
lines changed

11 files changed

+89
-18
lines changed

CSharpMath.Editor/MathKeyboard.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ public MathListIndex InsertionIndex {
8585
public TFont Font { get; set; }
8686
public LineStyle LineStyle { get; set; }
8787
public Structures.Color SelectColor { get; set; }
88-
public RectangleF? Measure => Display?.DisplayBounds();
88+
public virtual RectangleF Measure => Display?.DisplayBounds() ?? RectangleF.Empty;
8989
public bool HasText => MathList?.Atoms?.Count > 0;
9090
public void RecreateDisplayFromMathList() {
9191
var position = Display?.Position ?? default;

CSharpMath.Forms.Example/CSharpMath.Forms.Example/LayoutPage.xaml.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,14 +34,14 @@ private void Canvas_PaintSurface(object sender, SKPaintSurfaceEventArgs e) {
3434
if (reset) { e.Surface.Canvas.Clear(); reset = false; }
3535
e.Surface.Canvas.Clear();
3636
if (drawOneLine) {
37-
var measure = painter.Measure(float.PositiveInfinity) ?? throw new Structures.InvalidCodePathException("Invalid LaTeX");
37+
var measure = painter.Measure(float.PositiveInfinity);
3838
e.Surface.Canvas.DrawRect(x, y,
3939
measure.Width, measure.Height, new global::SkiaSharp.SKPaint {
4040
Color = global::SkiaSharp.SKColors.Orange
4141
});
4242
painter.DrawOneLine(e.Surface.Canvas, x, y);
4343
} else {
44-
var measure = painter.Measure(w) ?? throw new Structures.InvalidCodePathException("Invalid LaTeX");
44+
var measure = painter.Measure(w);
4545
e.Surface.Canvas.DrawRect(x, y,
4646
measure.Width, measure.Height, new global::SkiaSharp.SKPaint {
4747
Color = global::SkiaSharp.SKColors.Orange

CSharpMath.Forms.Example/CSharpMath.Forms.Example/SlidePage.xaml.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ public partial class SlidePage : ContentPage {
1414

1515
private void Canvas_PaintSurface(object sender, SKPaintSurfaceEventArgs e) {
1616
if (reset) { e.Surface.Canvas.Clear(); reset = false; }
17-
var measure = painter.Measure() ?? throw new Structures.InvalidCodePathException("Invalid LaTeX");
17+
var measure = painter.Measure();
1818
e.Surface.Canvas.DrawRect
1919
((float)x, (float)y, measure.Width, -measure.Height,
2020
new global::SkiaSharp.SKPaint { Color = global::SkiaSharp.SKColors.Orange });

CSharpMath.Rendering.Tests/DrawIcon.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ public static void Draw(SKCanvas c) {
1818
painter.TextColor = SKColors.White;
1919
for (int i = 0; i < count; i++) {
2020
painter.LaTeX = i.ToString();
21-
var m = painter.Measure() ?? throw new Structures.InvalidCodePathException("Invalid LaTeX");
21+
var m = painter.Measure();
2222
painter.Draw(c, cx - m.Width / 2, cy + m.Height / 2 - r);
2323
c.RotateDegrees(θ, cx, cy);
2424
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
namespace CSharpMath.Rendering.Tests {
2+
using System;
3+
using System.Drawing;
4+
using BackEnd;
5+
using Xunit;
6+
using CSharpMath.Display.FrontEnd;
7+
8+
public class TestMeasure {
9+
class D : Display.IDisplay<Fonts, Glyph> {
10+
public float Ascent => 12;
11+
public float Descent => 3;
12+
public float Width => 10;
13+
14+
public PointF Position { get => PointF.Empty; set => throw new NotImplementedException(); }
15+
public Atom.Range Range => throw new NotImplementedException();
16+
public Structures.Color? TextColor { get => throw new NotImplementedException(); set => throw new NotImplementedException(); }
17+
public Structures.Color? BackColor { get => throw new NotImplementedException(); set => throw new NotImplementedException(); }
18+
public bool HasScript { get => throw new NotImplementedException(); set => throw new NotImplementedException(); }
19+
public void Draw(IGraphicsContext<Fonts, Glyph> context) => throw new NotImplementedException();
20+
public void SetTextColorRecursive(Structures.Color? textColor) => throw new NotImplementedException();
21+
}
22+
class DEditorKeyboard : Editor.MathKeyboard<Fonts, Glyph> {
23+
public DEditorKeyboard() : base(TypesettingContext.Instance, new Fonts()) =>
24+
Display = new Display.Displays.ListDisplay<Fonts, Glyph>(new[] { new D() });
25+
}
26+
class DRenderingMath : SkiaSharp.MathPainter {
27+
public DRenderingMath() =>
28+
Display = new Display.Displays.ListDisplay<Fonts, Glyph>(new[] { new D() });
29+
protected override void UpdateDisplayCore(float unused) { }
30+
}
31+
class DRenderingText : SkiaSharp.TextPainter {
32+
public DRenderingText() =>
33+
Display = new Display.Displays.ListDisplay<Fonts, Glyph>(new[] { new D() });
34+
protected override void UpdateDisplayCore(float canvasWidth) { }
35+
}
36+
class DRenderingKeyboard : FrontEnd.MathKeyboard {
37+
public DRenderingKeyboard() =>
38+
Display = new Display.Displays.ListDisplay<Fonts, Glyph>(new[] { new D() });
39+
}
40+
/// <summary>
41+
/// CSharpMath and CSharpMath.Editor use the mathematical coordinate system,
42+
/// i.e. the rectangle position is at the bottom-left.
43+
/// </summary>
44+
[Fact]
45+
public void CoreMeasure_YIsNegDescent() {
46+
Assert.Equal(new RectangleF(0, -3, 10, 15), new D().DisplayBounds());
47+
Assert.Equal(new RectangleF(0, -3, 10, 15), new DEditorKeyboard().Measure);
48+
}
49+
/// <summary>
50+
/// CSharpMath.Rendering and descendants use the graphical coordinate system,
51+
/// i.e. the rectangle position is at the top-left.
52+
/// </summary>
53+
[Fact]
54+
public void RenderingMeasure_YIsNegAscent() {
55+
Assert.Equal(new RectangleF(0, -12, 10, 15), new DRenderingMath().Measure());
56+
Assert.Equal(new RectangleF(0, -12, 10, 15), new DRenderingText().Measure(float.NaN));
57+
Assert.Equal(new RectangleF(0, -12, 10, 15), new DRenderingKeyboard().Measure);
58+
}
59+
}
60+
}

CSharpMath.Rendering/FrontEnd/MathKeyboard.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@ public class MathKeyboard : MathKeyboard<Fonts, Glyph> {
1010
public MathKeyboard(float fontSize = PainterConstants.DefaultFontSize, double blinkMilliseconds = DefaultBlinkMilliseconds)
1111
: base(TypesettingContext.Instance,
1212
new Fonts(Array.Empty<Typography.OpenFont.Typeface>(), fontSize), blinkMilliseconds) { }
13-
13+
// Rendering: Convert to
14+
public override RectangleF Measure =>
15+
Display != null ? new RectangleF(0, -Display.Ascent, Display.Width, Display.Ascent + Display.Descent) : RectangleF.Empty;
1416
public void DrawCaret(ICanvas canvas, Structures.Color color, CaretShape shape) {
1517
if (CaretState != MathKeyboardCaretState.Shown || Display is null)
1618
return;

CSharpMath.Rendering/FrontEnd/MathPainter.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ public abstract class MathPainter<TCanvas, TColor> : Painter<TCanvas, Atom.MathL
1111
Atom.LaTeXParser.MathListFromLaTeX(latex);
1212
protected override string ContentToLaTeX(Atom.MathList mathList) =>
1313
Atom.LaTeXParser.MathListToLaTeX(mathList).ToString();
14-
public override RectangleF? Measure(float unused = float.NaN) => base.Measure(unused);
14+
public override RectangleF Measure(float unused = float.NaN) => base.Measure(unused);
1515
protected override void SetRedisplay() => _displayChanged = true;
1616
protected override void UpdateDisplayCore(float unused) {
1717
if (_displayChanged)

CSharpMath.Rendering/FrontEnd/Painter.cs

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -63,9 +63,11 @@ public Painter() {
6363
public abstract Color WrapColor(TColor color);
6464
public abstract TColor UnwrapColor(Color color);
6565
public abstract ICanvas WrapCanvas(TCanvas canvas);
66-
public virtual RectangleF? Measure(float textPainterCanvasWidth) {
66+
public virtual RectangleF Measure(float textPainterCanvasWidth) {
6767
UpdateDisplay(textPainterCanvasWidth);
68-
return Display?.DisplayBounds();
68+
if (Display != null)
69+
return new RectangleF(0, -Display.Ascent, Display.Width, Display.Ascent + Display.Descent);
70+
else return RectangleF.Empty;
6971
}
7072
protected abstract void UpdateDisplayCore(float textPainterCanvasWidth);
7173
protected void UpdateDisplay(float textPainterCanvasWidth) {
@@ -119,11 +121,7 @@ protected void DrawCore(ICanvas canvas, IDisplay<Fonts, Glyph>? display, PointF?
119121
canvas.DefaultColor = WrapColor(TextColor);
120122
canvas.CurrentColor = WrapColor(HighlightColor);
121123
canvas.CurrentStyle = PaintStyle;
122-
var measure = Measure(canvas.Width) ??
123-
throw new InvalidCodePathException(
124-
nameof(Measure) + " returned null." +
125-
$"Any conditions leading to this should have already been checked via " +
126-
nameof(Content) + " != null.");
124+
var measure = Measure(canvas.Width);
127125
canvas.FillRect(display.Position.X + measure.X, display.Position.Y - display.Descent,
128126
measure.Width, measure.Height);
129127
canvas.CurrentColor = null;

CSharpMath/Display/IDisplay.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,10 @@ namespace CSharpMath {
2626
using Display;
2727
using Display.FrontEnd;
2828
partial class Extensions {
29-
/// <summary>The display's bounds, in its own coordinate system.</summary>
29+
/// <summary>
30+
/// The display's bounds, in its own coordinate system.<br/>
31+
/// **Internal only! The Rectangle's position is at the bottom left: not what the outer world expects**
32+
/// </summary>
3033
public static RectangleF DisplayBounds<TFont, TGlyph>
3134
(this IDisplay<TFont, TGlyph> display) where TFont : IFont<TGlyph> =>
3235
new RectangleF(0, -display.Descent, display.Width, display.Ascent + display.Descent);

Directory.Build.props

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
<!--Ordered according to https://docs.microsoft.com/en-us/nuget/reference/msbuild-targets#pack-target-->
1313
<PackageId>$(MSBuildProjectName)</PackageId>
14-
<PackageVersion>0.4.0</PackageVersion>
14+
<PackageVersion>0.4.1</PackageVersion>
1515
<Authors>CSharpMath Contributors (verybadcat, Happypig375, charlesroddie, FoggyFinder)</Authors>
1616
<Title>$(PackageId)</Title>
1717
<!--Description property is defined in individual projects-->
@@ -25,7 +25,8 @@
2525
expression tex latex render rendering display beautiful
2626
</PackageTags>
2727
<PackageReleaseNotes>
28-
The 0.4.0 Avalonia Update brings the Avalonia front end!
28+
The 0.4 Avalonia Update brings the Avalonia front end!
29+
0.4.1: Fixed Measure in CSharpMath.Rendering
2930
</PackageReleaseNotes>
3031
<RepositoryUrl>https://github.com/verybadcat/CSharpMath.git</RepositoryUrl>
3132
<RepositoryType>git</RepositoryType>

0 commit comments

Comments
 (0)