Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 52 additions & 11 deletions CSharpMath.Core.Tests/Display/TypesetterTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,15 @@

namespace CSharpMath.Core.DisplayTests {
public class TypesetterTests {
private sealed class NonScalarAlignedGlyphFinder : IGlyphFinder<TFont, TGlyph> {
public TGlyph FindGlyphForCharacterAtIndex(TFont font, int index, string str) =>
TestGlyphFinder.Instance.FindGlyphForCharacterAtIndex(font, index, str);
public System.Collections.Generic.IEnumerable<TGlyph> FindGlyphs(TFont font, string str) =>
TestGlyphFinder.Instance.FindGlyphs(font, str).Take(1);
public TGlyph EmptyGlyph => TestGlyphFinder.Instance.EmptyGlyph;
public bool GlyphIsEmpty(TGlyph glyph) => TestGlyphFinder.Instance.GlyphIsEmpty(glyph);
}

private sealed class MalformedAssemblyTable : JsonMathTable {
private readonly float _advance, _connector;
public MalformedAssemblyTable(float advance, float connector = 0) : base(
Expand Down Expand Up @@ -62,6 +71,38 @@ public void NonFiniteAssemblyMetricsFailDeterministically(bool horizontal) {
AtomTests.LaTeXParserTest.ParseLaTeX(latex), _font, context, LineStyle.Display));
}

[Fact]
public void FusedMathAtomsRequireOneGlyphPerUnicodeScalar() {
var context = new TypesettingContext<TFont, TGlyph>(
(font, size) => new TFont(size), TestGlyphBoundsProvider.Instance,
new NonScalarAlignedGlyphFinder(), _context.MathTable);

var error = Assert.Throws<System.InvalidOperationException>(() => Typesetter.CreateLine(
AtomTests.LaTeXParserTest.ParseLaTeX("P2"), _font, context, LineStyle.Display));

Assert.Contains("one glyph per Unicode scalar", error.Message);
}

[Fact]
public void PublicInnerDisplayReflectsMutableInnerInkExtent() {
var child = new GlyphDisplay<TFont, TGlyph>(
new TGlyph('P'), Range.NotFound, _font, 1, 1, 10);
var inner = new ListDisplay<TFont, TGlyph>(new IDisplay<TFont, TGlyph>[] { child });
var right = new GlyphDisplay<TFont, TGlyph>(
new TGlyph(')'), Range.NotFound, _font, 1, 1, 2);
var display = new InnerDisplay<TFont, TGlyph>(inner, null, right, Range.NotFound);
display.Position = new PointF(5, 0);

Assert.Equal(12, display.Width);
Assert.Equal(15, right.Position.X);

child.Position = new PointF(20, 0);
display.Position = new PointF(5, 0);

Assert.Equal(32, display.Width);
Assert.Equal(35, right.Position.X);
}

static System.Action<IDisplay<TFont, TGlyph>?> TestList((int, int) range, double ascent, double descent, double width, double x, double y,
LinePosition linePos, int indexInParent, params System.Action<IDisplay<TFont, TGlyph>>[] inspectors) => d => {
var list = Assert.IsType<ListDisplay<TFont, TGlyph>>(d);
Expand Down Expand Up @@ -106,7 +147,7 @@ public void TestSingleCharacter(string latex) =>

[Theory, InlineData("xyzw"), InlineData("xy2w"), InlineData("12.3"), InlineData("|`@/"), InlineData("1`y.")]
public void TestVariablesNumbersAndOrdinaries(string latex) =>
TestOuter(latex, 4, 14, 4, 40,
TestOuter(latex, 4, 14, 4, latex is "xy2w" or "1`y." ? 40.16 : 40,
d => {
var line = Assert.IsType<TextLineDisplay<TFont, TGlyph>>(d);
Assert.Equal(4, line.Atoms.Count);
Expand All @@ -117,7 +158,7 @@ public void TestVariablesNumbersAndOrdinaries(string latex) =>

Assert.Equal(14, line.Ascent);
Assert.Equal(4, line.Descent);
Assert.Equal(40, line.Width);
Approximately.Equal(latex is "xy2w" or "1`y." ? 40.16 : 40, line.Width);
});
[Theory]
[InlineData("%\n1234", "1234", 1, 4)]
Expand Down Expand Up @@ -309,7 +350,7 @@ public void TestFraction(string latex, double lineThickness) =>
});
[Theory, InlineData("2x+3=y"), InlineData("y=3+2x"), InlineData("y-3=2x"), InlineData("3=y-2x")]
public void TestEquationWithOperatorsAndRelations(string latex) =>
TestOuter(latex, 6, 14, 4, 80, d => {
TestOuter(latex, 6, 14, 4, latex is "2x+3=y" ? 80.32 : 80.16, d => {
var line = Assert.IsType<TextLineDisplay<TFont, TGlyph>>(d);

Assert.Equal(6, line.Atoms.Count);
Expand All @@ -320,28 +361,28 @@ public void TestEquationWithOperatorsAndRelations(string latex) =>

Assert.Equal(14, line.Ascent);
Assert.Equal(4, line.Descent);
Assert.Equal(80, line.Width);
Approximately.Equal(latex is "2x+3=y" ? 80.32 : 80.16, line.Width);
});

[Theory, InlineData("[", "]"), InlineData("(", @"\}"), InlineData(@"\{", "]")] // Using ) confuses the test explorer...
public void TestInner(string left, string right) =>
TestOuter($@"a\left{left}x\right{right}", 2, 14, 4, 43.333,
TestOuter($@"a\left{left}x\right{right}", 2, 14, 4, 43.553,
d => Assert.IsType<TextLineDisplay<TFont, TGlyph>>(d),
d => {
var inner = Assert.IsType<InnerDisplay<TFont, TGlyph>>(d);
Approximately.At(13.333, 0, inner.Position);
Approximately.At(13.553, 0, inner.Position);
Assert.Equal(new Range(1, 1), inner.Range);
Assert.Equal(14, inner.Ascent);
Assert.Equal(4, inner.Descent);
Assert.Equal(30, inner.Width);

var glyph = Assert.IsType<GlyphDisplay<TFont, TGlyph>>(inner.Left);
Approximately.At(13.333, 0, glyph.Position);
Approximately.At(13.553, 0, glyph.Position);
Assert.Equal(Range.NotFound, glyph.Range);
Assert.False(glyph.HasScript);
Assert.Equal(left.EnumerateRunes().Last(), glyph.Glyph);

TestList(1, 14, 4, 10, 23.333, 0, LinePosition.Regular, Range.UndefinedInt,
TestList(1, 14, 4, 10, 23.553, 0, LinePosition.Regular, Range.UndefinedInt,
d => {
var line = Assert.IsType<TextLineDisplay<TFont, TGlyph>>(d);
Assert.Single(line.Atoms);
Expand All @@ -352,7 +393,7 @@ public void TestInner(string left, string right) =>
})(inner.Inner);

var glyph2 = Assert.IsType<GlyphDisplay<TFont, TGlyph>>(inner.Right);
Approximately.At(33.333, 0, glyph2.Position);
Approximately.At(33.553, 0, glyph2.Position);
Assert.Equal(Range.NotFound, glyph2.Range);
Assert.False(glyph2.HasScript);
Assert.Equal(right.EnumerateRunes().Last(), glyph2.Glyph);
Expand Down Expand Up @@ -479,7 +520,7 @@ public void TestLimit() =>
Assert.Equal("lim", string.Concat(largeOpText.Text));
Approximately.Equal(new PointF(31.111f, 0), largeOpText.Position);
Assert.False(largeOpText.HasScript);
TestList(3, 11.046, 2.8, 26, 38.111, -18.386, LinePosition.Regular, Range.UndefinedInt,
TestList(3, 11.046, 2.8, 26.32, 37.951, -18.386, LinePosition.Regular, Range.UndefinedInt,
d => {
var subscript = Assert.IsType<TextLineDisplay<TFont, TGlyph>>(d);
Assert.Equal("𝑥→0", string.Concat(subscript.Text));
Expand All @@ -488,7 +529,7 @@ public void TestLimit() =>
Assert.True(subscript.HasScript);
Assert.Equal(new Range(0, 3), subscript.Range);
},
TestList(1, 7, 2, 5, 21, 4.046, LinePosition.Superscript, 2,
TestList(1, 7, 2, 5, 21.32, 4.046, LinePosition.Superscript, 2,
d => {
var superscript = Assert.IsType<TextLineDisplay<TFont, TGlyph>>(d);
Assert.Equal("+", string.Concat(superscript.Text));
Expand Down
52 changes: 26 additions & 26 deletions CSharpMath.Core.Tests/Editor/PointForIndexTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -142,8 +142,8 @@ public static TestData ExponentsData {

public static TestData Issue46Data =>
new TestData {
{ (57.777, 0), 4 },
{ (75.097, 0), 5 },
{ (58.097, 0), 4 },
{ (75.418, 0), 5 },
};
[Theory, MemberData(nameof(Issue46Data))] // https://github.com/verybadcat/CSharpMath/issues/46
public void Issue46(PointF point, MathListIndex expected) => Test("2+x+x^y", point, expected);
Expand Down Expand Up @@ -209,10 +209,10 @@ public void Complex(PointF point, MathListIndex expected) =>
{ (10, 9.68), 0, (SubIndex.Superscript, 0) },
{ (17, 9.68), 0, (SubIndex.Superscript, 1) },
{ (21.453, 0), 1 },
{ (31.453, 0), 2 },
{ (41.453, 0), 3 },
{ (51.453, 0), 4 },
{ (61.453, 0), 5 },
{ (31.773, 0), 2 },
{ (41.773, 0), 3 },
{ (51.773, 0), 4 },
{ (61.773, 0), 5 },
};
[Theory, MemberData(nameof(IntegralData))]
public void Integral(PointF point, MathListIndex expected) => Test(@"\int_a^b x\ dx", point, expected);
Expand All @@ -225,10 +225,10 @@ public void Complex(PointF point, MathListIndex expected) =>
{ (1.5, 20.8), 0, (SubIndex.Superscript, 0) },
{ (8.5, 20.8), 0, (SubIndex.Superscript, 1) },
{ (13.333, 0), 1 },
{ (23.333, 0), 2 },
{ (33.333, 0), 3 },
{ (43.333, 0), 4 },
{ (53.333, 0), 5 },
{ (23.653, 0), 2 },
{ (33.653, 0), 3 },
{ (43.653, 0), 4 },
{ (53.653, 0), 5 },
};
[Theory, MemberData(nameof(IntegralLimitsData))]
public void IntegralLimits(PointF point, MathListIndex expected) => Test(@"\int\limits_a^b x\ dx", point, expected);
Expand All @@ -237,9 +237,9 @@ public void Complex(PointF point, MathListIndex expected) =>
{ (0, 0), 0 },
{ (13.333, 0), 1 },
{ (53.333, 0), 1, (SubIndex.BetweenBaseAndScripts, 1) },
{ (22.833, -17.14), 1, (SubIndex.Subscript, 0) },
{ (29.833, -17.14), 1, (SubIndex.Subscript, 1) },
{ (36.833, -17.14), 1, (SubIndex.Subscript, 2) },
{ (22.673, -17.14), 1, (SubIndex.Subscript, 0) },
{ (29.993, -17.14), 1, (SubIndex.Subscript, 1) },
{ (36.993, -17.14), 1, (SubIndex.Subscript, 2) },
{ (56.666, 0), 2 },
{ (66.666, 0), 2, (SubIndex.BetweenBaseAndScripts, 1) },
{ (66.666, -6.12), 2, (SubIndex.Subscript, 0) },
Expand Down Expand Up @@ -274,19 +274,19 @@ public void Complex(PointF point, MathListIndex expected) =>
new TestData {
{ (0, 0), 0 },
{ (13.333, 0), 1 },
{ (26.667, 0), 2 },
{ (36.667, 0), 2, (SubIndex.Inner, 0) },
{ (46.667, 0), 2, (SubIndex.Inner, 1) },
{ (60, 0), 2, (SubIndex.Inner, 2) },
{ (70, 0), 2, (SubIndex.Inner, 2), (SubIndex.Inner, 0) },
{ (80, 0), 2, (SubIndex.Inner, 2), (SubIndex.Inner, 1) },
{ (90, 0), 2, (SubIndex.Inner, 2), (SubIndex.Inner, 2) },
{ (103.333, 0), 2, (SubIndex.Inner, 3) },
{ (113.333, 0), 2, (SubIndex.Inner, 4) },
{ (123.333, 0), 2, (SubIndex.Inner, 5) },
{ (136.666, 0), 3 },
{ (150, 0), 4 },
{ (160, 0), 5 },
{ (26.887, 0), 2 },
{ (36.887, 0), 2, (SubIndex.Inner, 0) },
{ (46.887, 0), 2, (SubIndex.Inner, 1) },
{ (60.22, 0), 2, (SubIndex.Inner, 2) },
{ (70.22, 0), 2, (SubIndex.Inner, 2), (SubIndex.Inner, 0) },
{ (80.22, 0), 2, (SubIndex.Inner, 2), (SubIndex.Inner, 1) },
{ (90.22, 0), 2, (SubIndex.Inner, 2), (SubIndex.Inner, 2) },
{ (103.553, 0), 2, (SubIndex.Inner, 3) },
{ (113.553, 0), 2, (SubIndex.Inner, 4) },
{ (123.553, 0), 2, (SubIndex.Inner, 5) },
{ (136.887, 0), 3 },
{ (150.22, 0), 4 },
{ (160.22, 0), 5 },
};
[Theory, MemberData(nameof(InnerData))]
public void Inner(PointF point, MathListIndex expected) => Test(@"\int a\left(bb\left[cc\right]dd\right)e\sum ", point, expected);
Expand Down
Binary file modified CSharpMath.Rendering.Tests/MathDisplay/Cases.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified CSharpMath.Rendering.Tests/MathDisplay/FontStyles.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified CSharpMath.Rendering.Tests/MathDisplay/FunctionDomainCodomain.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified CSharpMath.Rendering.Tests/MathDisplay/LnEquation.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified CSharpMath.Rendering.Tests/MathDisplay/Logic.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified CSharpMath.Rendering.Tests/MathDisplay/Matrix.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified CSharpMath.Rendering.Tests/MathDisplay/Matrix2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified CSharpMath.Rendering.Tests/MathDisplay/Matrix3.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified CSharpMath.Rendering.Tests/MathDisplay/Matrix4.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified CSharpMath.Rendering.Tests/MathDisplay/Matrix5.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified CSharpMath.Rendering.Tests/MathDisplay/Matrix6.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified CSharpMath.Rendering.Tests/MathDisplay/Matrix7.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified CSharpMath.Rendering.Tests/MathDisplay/Matrixception.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified CSharpMath.Rendering.Tests/MathDisplay/QuadraticFormula.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified CSharpMath.Rendering.Tests/MathDisplay/QuarticSolutions.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified CSharpMath.Rendering.Tests/MathDisplay/SolveEquations.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified CSharpMath.Rendering.Tests/MathDisplay/SummationWithCup.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified CSharpMath.Rendering.Tests/MathDisplay/TangentPeriodShift.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified CSharpMath.Rendering.Tests/MathDisplay/VectorProjection.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified CSharpMath.Rendering.Tests/MathInline/Cases.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified CSharpMath.Rendering.Tests/MathInline/FontStyles.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified CSharpMath.Rendering.Tests/MathInline/FunctionDomainCodomain.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified CSharpMath.Rendering.Tests/MathInline/LnEquation.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified CSharpMath.Rendering.Tests/MathInline/Logic.png
Binary file modified CSharpMath.Rendering.Tests/MathInline/Matrix.png
Binary file modified CSharpMath.Rendering.Tests/MathInline/Matrix2.png
Binary file modified CSharpMath.Rendering.Tests/MathInline/Matrix3.png
Binary file modified CSharpMath.Rendering.Tests/MathInline/Matrix4.png
Binary file modified CSharpMath.Rendering.Tests/MathInline/Matrix5.png
Binary file modified CSharpMath.Rendering.Tests/MathInline/Matrix6.png
Binary file modified CSharpMath.Rendering.Tests/MathInline/Matrix7.png
Binary file modified CSharpMath.Rendering.Tests/MathInline/Matrixception.png
Binary file modified CSharpMath.Rendering.Tests/MathInline/QuadraticFormula.png
Binary file modified CSharpMath.Rendering.Tests/MathInline/SolveEquations.png
Binary file modified CSharpMath.Rendering.Tests/MathInline/SummationWithCup.png
Binary file modified CSharpMath.Rendering.Tests/MathInline/TangentPeriodShift.png
Binary file modified CSharpMath.Rendering.Tests/MathInline/VectorProjection.png
Loading
Loading