Skip to content

Commit 36da044

Browse files
New frontend CSharpMath.VectSharp. (verybadcat#215)
* Added new frontend CSharpMath.VectSharp. * Fix CI Nightly/Release --------- Co-authored-by: Hadrian Tang <hadrianwttang@outlook.com>
1 parent a3df60c commit 36da044

14 files changed

Lines changed: 311 additions & 37 deletions

File tree

.github/workflows/Nightly.yml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,6 @@ jobs:
7777
run: |
7878
# "dotnet nuget push" with "dotnet nuget add source" to GitHub Packages is unstable for project names with a dot: https://github.com/NuGet/Home/issues/9775#issuecomment-714509211
7979
# So we must specify api-key directly in "dotnet nuget push" instead of following the GitHub Packages documentation
80-
# We use quotes to avoid shell globbing: https://github.com/NuGet/Home/issues/4393#issuecomment-667618120
8180
# --no-symbols true to not let GitHub Releases interpret .snupkg as .nupkg
82-
dotnet nuget push '.nupkgs/*.nupkg' --source 'https://nuget.pkg.github.com/verybadcat/index.json' --api-key ${{ github.token }} --skip-duplicate --no-symbols true
81+
dotnet nuget push .nupkgs\*.nupkg --source 'https://nuget.pkg.github.com/verybadcat/index.json' --api-key ${{ github.token }} --skip-duplicate --no-symbols true
8382
shell: pwsh

.github/workflows/Release.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ jobs:
3838
file_glob: true
3939
overwrite: true
4040
- name: Upload to NuGet
41-
run: | # We use double quotes to avoid shell globbing: https://github.com/NuGet/Home/issues/4393#issuecomment-667618120
41+
run: |
4242
# Use --skip-duplicate since we want re-runs of this workflow to succeed in case of network issues
43-
dotnet nuget push ".nupkgs/*.nupkg" -k ${{ secrets.NUGET_API_KEY }} -s https://api.nuget.org/v3/index.json --skip-duplicate
43+
dotnet nuget push .nupkgs\*.nupkg -k ${{ secrets.NUGET_API_KEY }} -s https://api.nuget.org/v3/index.json --skip-duplicate
4444
shell: pwsh

CSharpMath.Core.Tests/Editor/CaretTests.cs

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,18 @@ namespace CSharpMath.Core.Tests {
99
using BackEnd;
1010
// Tests in different classes run in parallel, unlike tests in the same class
1111
public class CaretBlinks {
12-
public const int MillisecondBuffer = 150;
12+
public const int MillisecondBuffer = 200;
1313
[Fact]
1414
public async Task Test() {
1515
var keyboard = new MathKeyboard<TestFont, TGlyph>(TestTypesettingContext.Instance, new TestFont()) {
1616
InsertionPositionHighlighted = true
1717
};
1818
Assert.True(keyboard.InsertionPositionHighlighted);
1919
Assert.True(keyboard.ShouldDrawCaret);
20-
await Task.Delay((int)MathKeyboard<TestFont, TGlyph>.DefaultBlinkMilliseconds + MillisecondBuffer);
20+
await Task.Delay((int)MathKeyboard<TestFont, TGlyph>.DefaultBlinkMilliseconds + MillisecondBuffer, TestContext.Current.CancellationToken);
2121
Assert.False(keyboard.InsertionPositionHighlighted);
2222
Assert.False(keyboard.ShouldDrawCaret);
23-
await Task.Delay((int)MathKeyboard<TestFont, TGlyph>.DefaultBlinkMilliseconds + MillisecondBuffer);
23+
await Task.Delay((int)MathKeyboard<TestFont, TGlyph>.DefaultBlinkMilliseconds + MillisecondBuffer, TestContext.Current.CancellationToken);
2424
Assert.True(keyboard.InsertionPositionHighlighted);
2525
Assert.True(keyboard.ShouldDrawCaret);
2626
}
@@ -33,9 +33,9 @@ public async Task Test() {
3333
};
3434
keyboard.StopBlinking();
3535
Assert.False(keyboard.ShouldDrawCaret);
36-
await Task.Delay((int)MathKeyboard<TestFont, TGlyph>.DefaultBlinkMilliseconds + CaretBlinks.MillisecondBuffer);
36+
await Task.Delay((int)MathKeyboard<TestFont, TGlyph>.DefaultBlinkMilliseconds + CaretBlinks.MillisecondBuffer, TestContext.Current.CancellationToken);
3737
Assert.False(keyboard.ShouldDrawCaret);
38-
await Task.Delay((int)MathKeyboard<TestFont, TGlyph>.DefaultBlinkMilliseconds + CaretBlinks.MillisecondBuffer);
38+
await Task.Delay((int)MathKeyboard<TestFont, TGlyph>.DefaultBlinkMilliseconds + CaretBlinks.MillisecondBuffer, TestContext.Current.CancellationToken);
3939
Assert.False(keyboard.ShouldDrawCaret);
4040
}
4141
}
@@ -74,7 +74,7 @@ public async Task Test(MathKeyboardInput input) {
7474
Assert.True(keyboard.ShouldDrawCaret);
7575
keyboard.KeyPress(input);
7676
Assert.False(keyboard.ShouldDrawCaret);
77-
await Task.Delay((int)MathKeyboard<TestFont, TGlyph>.DefaultBlinkMilliseconds + CaretBlinks.MillisecondBuffer);
77+
await Task.Delay((int)MathKeyboard<TestFont, TGlyph>.DefaultBlinkMilliseconds + CaretBlinks.MillisecondBuffer, TestContext.Current.CancellationToken);
7878
Assert.False(keyboard.ShouldDrawCaret);
7979
}
8080
}
@@ -86,18 +86,18 @@ public async Task Test() {
8686
};
8787
Assert.True(keyboard.ShouldDrawCaret);
8888

89-
await Task.Delay((int)MathKeyboard<TestFont, TGlyph>.DefaultBlinkMilliseconds - CaretBlinks.MillisecondBuffer);
89+
await Task.Delay((int)MathKeyboard<TestFont, TGlyph>.DefaultBlinkMilliseconds - CaretBlinks.MillisecondBuffer, TestContext.Current.CancellationToken);
9090
Assert.True(keyboard.ShouldDrawCaret);
9191

9292
keyboard.KeyPress(MathKeyboardInput.A);
93-
await Task.Delay((int)MathKeyboard<TestFont, TGlyph>.DefaultBlinkMilliseconds - CaretBlinks.MillisecondBuffer);
93+
await Task.Delay((int)MathKeyboard<TestFont, TGlyph>.DefaultBlinkMilliseconds - CaretBlinks.MillisecondBuffer, TestContext.Current.CancellationToken);
9494
Assert.True(keyboard.ShouldDrawCaret);
9595

9696
keyboard.KeyPress(MathKeyboardInput.Left);
97-
await Task.Delay((int)MathKeyboard<TestFont, TGlyph>.DefaultBlinkMilliseconds - CaretBlinks.MillisecondBuffer);
97+
await Task.Delay((int)MathKeyboard<TestFont, TGlyph>.DefaultBlinkMilliseconds - CaretBlinks.MillisecondBuffer, TestContext.Current.CancellationToken);
9898
Assert.True(keyboard.ShouldDrawCaret);
9999

100-
await Task.Delay(4 * CaretBlinks.MillisecondBuffer);
100+
await Task.Delay(4 * CaretBlinks.MillisecondBuffer, TestContext.Current.CancellationToken);
101101
Assert.False(keyboard.ShouldDrawCaret);
102102
}
103103
}
@@ -111,19 +111,19 @@ public async Task Test() {
111111
Assert.True(keyboard.ShouldDrawCaret);
112112

113113
keyboard.StopBlinking();
114-
await Task.Delay((int)MathKeyboard<TestFont, TGlyph>.DefaultBlinkMilliseconds + CaretBlinks.MillisecondBuffer);
114+
await Task.Delay((int)MathKeyboard<TestFont, TGlyph>.DefaultBlinkMilliseconds + CaretBlinks.MillisecondBuffer, TestContext.Current.CancellationToken);
115115
Assert.True(keyboard.ShouldDrawCaret);
116116

117117
keyboard.StartBlinking();
118-
await Task.Delay((int)MathKeyboard<TestFont, TGlyph>.DefaultBlinkMilliseconds + CaretBlinks.MillisecondBuffer);
118+
await Task.Delay((int)MathKeyboard<TestFont, TGlyph>.DefaultBlinkMilliseconds + CaretBlinks.MillisecondBuffer, TestContext.Current.CancellationToken);
119119
Assert.False(keyboard.ShouldDrawCaret);
120120

121121
keyboard.StopBlinking();
122-
await Task.Delay((int)MathKeyboard<TestFont, TGlyph>.DefaultBlinkMilliseconds + CaretBlinks.MillisecondBuffer);
122+
await Task.Delay((int)MathKeyboard<TestFont, TGlyph>.DefaultBlinkMilliseconds + CaretBlinks.MillisecondBuffer, TestContext.Current.CancellationToken);
123123
Assert.False(keyboard.ShouldDrawCaret);
124124

125125
keyboard.StartBlinking();
126-
await Task.Delay((int)MathKeyboard<TestFont, TGlyph>.DefaultBlinkMilliseconds + CaretBlinks.MillisecondBuffer);
126+
await Task.Delay((int)MathKeyboard<TestFont, TGlyph>.DefaultBlinkMilliseconds + CaretBlinks.MillisecondBuffer, TestContext.Current.CancellationToken);
127127
Assert.True(keyboard.ShouldDrawCaret);
128128
}
129129
}
@@ -188,15 +188,15 @@ public async Task CustomizedPlaceholderBlinks() {
188188
Assert.Equal("😀", inner.Nucleus);
189189
Assert.Equal(System.Drawing.Color.Green, inner.Color);
190190

191-
await Task.Delay((int)MathKeyboard<TestFont, TGlyph>.DefaultBlinkMilliseconds + CaretBlinks.MillisecondBuffer);
191+
await Task.Delay((int)MathKeyboard<TestFont, TGlyph>.DefaultBlinkMilliseconds + CaretBlinks.MillisecondBuffer, TestContext.Current.CancellationToken);
192192
Assert.False(keyboard.InsertionPositionHighlighted);
193193
Assert.False(keyboard.ShouldDrawCaret);
194194
Assert.Equal("😐", outer.Nucleus);
195195
Assert.Equal(System.Drawing.Color.Blue, outer.Color);
196196
Assert.Equal("😐", inner.Nucleus);
197197
Assert.Equal(System.Drawing.Color.Blue, inner.Color);
198198

199-
await Task.Delay((int)MathKeyboard<TestFont, TGlyph>.DefaultBlinkMilliseconds + CaretBlinks.MillisecondBuffer);
199+
await Task.Delay((int)MathKeyboard<TestFont, TGlyph>.DefaultBlinkMilliseconds + CaretBlinks.MillisecondBuffer, TestContext.Current.CancellationToken);
200200
Assert.True(keyboard.InsertionPositionHighlighted);
201201
Assert.False(keyboard.ShouldDrawCaret);
202202
Assert.Equal("😐", outer.Nucleus);
@@ -251,9 +251,9 @@ void Check() {
251251
ExpectedAppearance(DefaultPlaceholderSettings.RestingNucleus, DefaultPlaceholderSettings.ActiveNucleus);
252252
}
253253
Check();
254-
await Task.Delay((int)MathKeyboard<TestFont, TGlyph>.DefaultBlinkMilliseconds + CaretBlinks.MillisecondBuffer);
254+
await Task.Delay((int)MathKeyboard<TestFont, TGlyph>.DefaultBlinkMilliseconds + CaretBlinks.MillisecondBuffer, TestContext.Current.CancellationToken);
255255
Check();
256-
await Task.Delay((int)MathKeyboard<TestFont, TGlyph>.DefaultBlinkMilliseconds + CaretBlinks.MillisecondBuffer);
256+
await Task.Delay((int)MathKeyboard<TestFont, TGlyph>.DefaultBlinkMilliseconds + CaretBlinks.MillisecondBuffer, TestContext.Current.CancellationToken);
257257
Check();
258258
}
259259
[Fact]
@@ -285,19 +285,19 @@ public void NonBlinkingActivePlaceholderMoves() {
285285
public async Task IfNonBlinkingPlaceholderIsNotHighLightedThenCaretBlinks() {
286286
Assert.True(keyboard.ShouldDrawCaret);
287287

288-
await Task.Delay((int)MathKeyboard<TestFont, TGlyph>.DefaultBlinkMilliseconds + CaretBlinks.MillisecondBuffer);
288+
await Task.Delay((int)MathKeyboard<TestFont, TGlyph>.DefaultBlinkMilliseconds + CaretBlinks.MillisecondBuffer, TestContext.Current.CancellationToken);
289289
Assert.False(keyboard.ShouldDrawCaret);
290290

291291
keyboard.KeyPress(MathKeyboardInput.Subscript);
292292
keyboard.KeyPress(MathKeyboardInput.Right);
293293
ExpectedAppearance(DefaultPlaceholderSettings.RestingNucleus, DefaultPlaceholderSettings.RestingNucleus);
294294
Assert.True(keyboard.ShouldDrawCaret);
295295

296-
await Task.Delay((int)MathKeyboard<TestFont, TGlyph>.DefaultBlinkMilliseconds + CaretBlinks.MillisecondBuffer);
296+
await Task.Delay((int)MathKeyboard<TestFont, TGlyph>.DefaultBlinkMilliseconds + CaretBlinks.MillisecondBuffer, TestContext.Current.CancellationToken);
297297
ExpectedAppearance(DefaultPlaceholderSettings.RestingNucleus, DefaultPlaceholderSettings.RestingNucleus);
298298
Assert.False(keyboard.ShouldDrawCaret);
299299

300-
await Task.Delay((int)MathKeyboard<TestFont, TGlyph>.DefaultBlinkMilliseconds + CaretBlinks.MillisecondBuffer);
300+
await Task.Delay((int)MathKeyboard<TestFont, TGlyph>.DefaultBlinkMilliseconds + CaretBlinks.MillisecondBuffer, TestContext.Current.CancellationToken);
301301
ExpectedAppearance(DefaultPlaceholderSettings.RestingNucleus, DefaultPlaceholderSettings.RestingNucleus);
302302
Assert.True(keyboard.ShouldDrawCaret);
303303
}

CSharpMath.CrossPlatform.slnf

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
{
2+
"solution": {
3+
"path": "CSharpMath.sln",
4+
"projects": [
5+
"CSharpMath.Wiki/CSharpMath.Wiki.shproj",
6+
"CSharpMath/CSharpMath.csproj",
7+
"CSharpMath.CoreTests/CSharpMath.CoreTests.csproj",
8+
"CSharpMath.Editor/CSharpMath.Editor.csproj",
9+
"CSharpMath.Editor.Tests/CSharpMath.Editor.Tests.csproj",
10+
"CSharpMath.Editor.Tests.Visualizer/CSharpMath.Editor.Tests.Visualizer.csproj",
11+
"CSharpMath.Evaluation/CSharpMath.Evaluation.csproj",
12+
"CSharpMath.Evaluation.Tests/CSharpMath.Evaluation.Tests.csproj",
13+
"CSharpMath.Rendering.Benchmarks/CSharpMath.Rendering.Benchmarks.csproj",
14+
"CSharpMath.Rendering.Tests/CSharpMath.Rendering.Tests.csproj",
15+
"CSharpMath.Rendering.Tests.FSharp/CSharpMath.Rendering.Tests.FSharp.fsproj",
16+
"CSharpMath.Rendering.Text.Tests/CSharpMath.Rendering.Text.Tests.csproj",
17+
"CSharpMath.Rendering/CSharpMath.Rendering.csproj",
18+
"CSharpMath.Xaml/CSharpMath.Xaml.shproj",
19+
"CSharpMath.Xaml.Tests/CSharpMath.Xaml.Tests.csproj",
20+
"CSharpMath.Xaml.Tests.NuGet/CSharpMath.Xaml.Tests.NuGet.csproj",
21+
"PixelFarm.Typography (Excerpt)/PixelFarm.Typography (Excerpt).shproj",
22+
"Typography/Typography.OpenFont/Typography.OpenFont.shproj",
23+
"Typography/Typography.GlyphLayout/Typography.GlyphLayout.shproj",
24+
"Typography/Typography.TextBreak/Typography.TextBreak/Typography.TextBreak.shproj",
25+
"Typography/Typography.TextBreak/Typography.TextBreak.UnitTests/TextBreakTests.csproj",
26+
"CSharpMath.Playground/CSharpMath.Playground.csproj",
27+
"CSharpMath.Avalonia/CSharpMath.Avalonia.csproj",
28+
"CSharpMath.Avalonia.Example/CSharpMath.Avalonia.Example.csproj",
29+
"CSharpMath.SkiaSharp/CSharpMath.SkiaSharp.csproj",
30+
"CSharpMath.VectSharp/CSharpMath.VectSharp.csproj",
31+
"CSharpMath.Forms/CSharpMath.Forms.csproj",
32+
"CSharpMath.Forms.Example/CSharpMath.Forms.Example/CSharpMath.Forms.Example.csproj",
33+
"CSharpMath.Forms.Tests/CSharpMath.Forms.Tests.csproj"
34+
]
35+
}
36+
}
Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,12 @@
11
<Project Sdk="Microsoft.NET.Sdk">
2+
<PropertyGroup>
3+
<OutputType>Exe</OutputType>
4+
<TargetFramework>net10.0</TargetFramework>
5+
<ImplicitUsings>enable</ImplicitUsings>
6+
</PropertyGroup>
27

3-
<PropertyGroup>
4-
<OutputType>Exe</OutputType>
5-
<TargetFramework>net10.0</TargetFramework>
6-
<ImplicitUsings>enable</ImplicitUsings>
7-
<Nullable>enable</Nullable>
8-
</PropertyGroup>
9-
10-
<ItemGroup>
11-
<ProjectReference Include="..\CSharpMath.SkiaSharp\CSharpMath.SkiaSharp.csproj" />
12-
<PackageReference Condition="$([System.OperatingSystem]::IsLinux())" Include="SkiaSharp.NativeAssets.Linux" Version="3.119.1" />
13-
</ItemGroup>
14-
8+
<ItemGroup>
9+
<ProjectReference Include="..\CSharpMath.SkiaSharp\CSharpMath.SkiaSharp.csproj" />
10+
<PackageReference Condition="$([System.OperatingSystem]::IsLinux())" Include="SkiaSharp.NativeAssets.Linux" Version="3.119.1" />
11+
</ItemGroup>
1512
</Project>
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
<PropertyGroup>
3+
<OutputType>Exe</OutputType>
4+
<TargetFramework>net10.0</TargetFramework>
5+
</PropertyGroup>
6+
<ItemGroup>
7+
<ProjectReference Include="..\CSharpMath.VectSharp\CSharpMath.VectSharp.csproj" />
8+
<PackageReference Include="VectSharp.PDF" Version="3.2.0" />
9+
<PackageReference Include="VectSharp.SVG" Version="1.10.2" />
10+
</ItemGroup>
11+
</Project>
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
using System.IO;
2+
using VectSharp;
3+
using VectSharp.PDF;
4+
using VectSharp.SVG;
5+
using CSharpMath.VectSharp;
6+
7+
var painter = new TextPainter {
8+
LaTeX = @"Let's render some math to a PDF and an SVG file!$$x = {-b \pm \color{red}\sqrt{b^2-4ac} \over 2a}$$"
9+
};
10+
var page = painter.DrawToPage(400f); // adjust width here
11+
var doc = new Document { Pages = { page } };
12+
13+
// PDF
14+
var tempFileName = Path.Combine(Path.GetTempPath(), "CSharpMath.pdf");
15+
doc.SaveAsPDF(tempFileName);
16+
System.Diagnostics.Process.Start(new System.Diagnostics.ProcessStartInfo {
17+
FileName = tempFileName,
18+
UseShellExecute = true
19+
});
20+
21+
// SVG
22+
tempFileName = Path.Combine(Path.GetTempPath(), "CSharpMath.svg");
23+
page.SaveAsSVG(tempFileName);
24+
System.Diagnostics.Process.Start(new System.Diagnostics.ProcessStartInfo {
25+
FileName = tempFileName,
26+
UseShellExecute = true
27+
});
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFramework>netstandard2.0</TargetFramework>
5+
<Description>The VectSharp front end for CSharpMath.</Description>
6+
<PackageTags>$(PackageTags) vectsharp</PackageTags>
7+
</PropertyGroup>
8+
9+
<ItemGroup>
10+
<PackageReference Include="VectSharp" Version="2.6.1" />
11+
<ProjectReference Include="..\CSharpMath.Rendering\CSharpMath.Rendering.csproj" />
12+
</ItemGroup>
13+
14+
</Project>

CSharpMath.VectSharp/Extensions.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
using System;
2+
using CSharpMath.Rendering.FrontEnd;
3+
4+
namespace CSharpMath.VectSharp;
5+
public static class Extensions {
6+
internal static global::VectSharp.Colour ToNative(this System.Drawing.Color color) =>
7+
global::VectSharp.Colour.FromRgba(color.R, color.G, color.B, color.A);
8+
internal static System.Drawing.Color FromNative(this global::VectSharp.Colour colour) =>
9+
System.Drawing.Color.FromArgb((int)Math.Round(colour.A * 255), (int)Math.Round(colour.R * 255), (int)Math.Round(colour.G * 255), (int)Math.Round(colour.B * 255));
10+
public static global::VectSharp.Page DrawToPage<TContent>(this Painter<global::VectSharp.Page, TContent, global::VectSharp.Colour> painter, float textPainterCanvasWidth = TextPainter.DefaultCanvasWidth, TextAlignment alignment = TextAlignment.TopLeft) where TContent : class {
11+
var size = painter.Measure(textPainterCanvasWidth).Size;
12+
var page = new global::VectSharp.Page(size.Width, size.Height);
13+
painter.Draw(page, alignment);
14+
return page;
15+
}
16+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
using System.Drawing;
2+
using CSharpMath.Rendering.FrontEnd;
3+
using VectSharp;
4+
5+
namespace CSharpMath.VectSharp;
6+
7+
public class MathPainter : MathPainter<Page, Colour> {
8+
public void Draw(Page canvas, global::VectSharp.Point point) => Draw(canvas, (float)point.X, (float)point.Y);
9+
public override Colour UnwrapColor(Color color) => color.ToNative();
10+
public override Color WrapColor(Colour color) => color.FromNative();
11+
public override ICanvas WrapCanvas(Page canvas) => new VectSharpCanvas(canvas);
12+
}

0 commit comments

Comments
 (0)