Skip to content
Merged
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
3 changes: 2 additions & 1 deletion samples/SharpEquations.Uno.Demo/App.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,8 @@ protected override void OnLaunched(LaunchActivatedEventArgs args)
panel.Children.Add(view);
if (System.Environment.GetEnvironmentVariable("SHARPEQUATIONS_DEMO_EQUATION") is { Length: > 0 } initial)
editor.Equation = Equation.Parse(initial, EquationFormat.UnicodeMath, new EquationOptions { Display = true });
window = new Window { Content = new Grid { Background = new SolidColorBrush(Microsoft.UI.Colors.White), Children = { panel } } };
// a white page in the light theme: with the dark theme of the system, WinUI would write the text white on it
window = new Window { Content = new Grid { Background = new SolidColorBrush(Microsoft.UI.Colors.White), RequestedTheme = ElementTheme.Light, Children = { panel } } };
window.Activate();
editor.Loaded += (_, _) => editor.FocusEditor();
}
Expand Down
28 changes: 28 additions & 0 deletions src/SharpEquations.Editing/BuildUp.cs
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,8 @@ public static bool IsBalanced(string text, EquationFormat format)
return null;

var elements = equation.ToMathML().Elements().ToList();
if (!line && format == EquationFormat.UnicodeMath)
elements = NaryWithItsLimits(elements, source, display);
if (line)
{
if (elements.Count != 1 || elements[0].Elements().ToList() is not { Count: 1 } rows || rows[0].Elements().ToList() is not { Count: 1 } cells)
Expand All @@ -251,6 +253,32 @@ public static bool IsBalanced(string text, EquationFormat format)
return used.All(u => u) ? nodes : null;
}

/// <summary>
/// An n-ary operator built up with its limits before its operand is typed (<c>∑_(i=1)^n</c>): UnicodeMath reads it
/// as an operator with scripts, while the same operator with an operand takes the limits of its kind (under and
/// over a sum in display, at the right of an integral). The text is read again with an empty operand, and the
/// operator takes the limits it has there, as when the whole equation is built up.
/// </summary>
private static List<XElement> NaryWithItsLimits(List<XElement> elements, string source, bool display)
{
if (elements.Count == 0 || elements[elements.Count - 1] is not { } last
|| last.Name.LocalName is not ("msub" or "msup" or "msubsup") || last.Elements().FirstOrDefault()?.Name.LocalName != "mo")
return elements;
if (!Equation.TryParse(source + "▒〖〗", EquationFormat.UnicodeMath, new EquationOptions { Display = display }, out var withOperand) || withOperand == null)
return elements;

// the operator and its empty operand, alone or in a row of their own
var others = withOperand.ToMathML().Elements().ToList();
if (others.Count > 0 && others[others.Count - 1] is { } row && row.Name.LocalName == "mrow" && row.Elements().Count() == 2)
others = others.Take(others.Count - 1).Concat(row.Elements()).ToList();
if (others.Count != elements.Count + 1 || others[others.Count - 2] is not { } nary
|| nary.Name.LocalName is not ("munder" or "mover" or "munderover"))
return elements;
var result = elements.Take(elements.Count - 1).ToList();
result.Add(nary);
return result;
}

/// <summary>
/// Where the nodes a run at an index of a row builds up with start: after the opening fence when the row is a pair
/// of fences and the run is between them, since the opening fence alone does not always parse (<c>|x</c>).
Expand Down
7 changes: 6 additions & 1 deletion src/SharpEquations.Editing/MathEditor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -980,7 +980,12 @@ private void ReopenScriptBase(Rune rune)
"msub" or "munder" => rune.Value == '^',
_ => false,
};
if (!lacksScript || BuildUp.ToEquation(new[] { row.Children[j - 1] }, IsDisplay) is not { } equation)
// the limit over or under an n-ary operator goes back to the text it was typed as (∑^2, not ∑┴2)
var structure = row.Children[j - 1];
if (structure.Name is "mover" or "munder" && structure.Children[0] is { } nary
&& (nary.Name == "mo" || nary.IsRow && nary.Children.Count == 1 && nary.Children[0].Name == "mo"))
structure = MathNode.Element(structure.Name == "mover" ? "msup" : "msub", structure.Children, structure.Attributes);
if (!lacksScript || BuildUp.ToEquation(new[] { structure }, IsDisplay) is not { } equation)
return;

var linear = BuildUp.ToLinear(equation, InputFormat);
Expand Down
6 changes: 6 additions & 0 deletions src/SharpEquations.Wpf/MathEditBox.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ namespace SharpEquations.Wpf
using System.Windows.Automation.Peers;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Threading;
Expand Down Expand Up @@ -131,6 +132,11 @@ public MathEditBox()
},
};

// the bar is the user interface, not the equation: the font of the system, not the size of the editor's, which
// the popup would inherit from it
quickBar.SetValue(TextElement.FontSizeProperty, SystemFonts.MessageFontSize);
quickBar.SetValue(TextElement.FontFamilyProperty, SystemFonts.MessageFontFamily);

blink = new DispatcherTimer(DispatcherPriority.Normal, Dispatcher) { Interval = MathEditorView.CaretBlinkInterval };
blink.Tick += (_, _) =>
{
Expand Down
14 changes: 9 additions & 5 deletions tests/SharpEquations.Avalonia.Tests/Controls/MathEditBoxTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,14 @@ public async Task TypingBuildsUp()
await Assert.That(events).IsGreaterThan(0);
}

/// <summary>The limits of a sum and of an integral, each ended by a space, one key at a time as a keyboard types them.</summary>
/// <summary>
/// The limits of a sum and of an integral, each ended by a space, one key at a time as a keyboard types them: under
/// and over the sum in display, at the right of the integral.
/// </summary>
[Test]
[Arguments("∑^2 _2 ", "∑_2^2")]
[Arguments("\\int^1 _0 ", "∫_0^1")]
[Arguments("x^2+b+\\int^1 _1 ", "x^2+b+∫_1^1")]
[Arguments("∑^2 _2 ", "\\sum_2^2")]
[Arguments("\\int^1 _0 ", "\\int_0^1")]
[Arguments("x^2+b+\\int^1 _1 ", "x^2+b+\\int_1^1")]
public async Task LimitsOfAnNaryOperator(string typed, string expected)
{
var equation = await Headless.Run(() => WithEditor((window, editor) =>
Expand All @@ -58,7 +61,8 @@ public async Task LimitsOfAnNaryOperator(string typed, string expected)
window.KeyTextInput(rune.ToString());
return editor.Equation;
}));
await Assert.That(equation).IsEqualTo(Display(expected));
// the expected equation in LaTeX, which reads the limits of a sum alone under and over it, as with an operand
await Assert.That(equation).IsEqualTo(Equation.Parse(expected, EquationFormat.LaTeX, new EquationOptions { Display = true }));
}

[Test]
Expand Down
25 changes: 25 additions & 0 deletions tests/SharpEquations.Avalonia.Tests/Editing/BuildUpTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,31 @@ public async Task OtherScriptAfterABuiltScriptInLaTeX(string typed, string expec
await Assert.That(editor.ToEquation()).IsEqualTo(Equation.Parse(expected, EquationFormat.LaTeX));
}

/// <summary>
/// An n-ary operator built up with its limits before its operand is typed takes the limits the operator has with
/// an operand: under and over a sum in display, at the right of an integral, as the whole text built up again.
/// </summary>
[Test]
[Arguments("\\sum_(i=1)^n i^2 ")]
[Arguments("∑_(i=1)^n i^2 ")]
[Arguments("∏_k a_k ")]
[Arguments("∑^2 x ")]
[Arguments("a+∑_k k ")]
[Arguments("∫_0^1 x dx ")]
[Arguments("∯_S f ")]
public async Task NaryLimitsAsWhenBuiltUpWhole(string typed)
{
static string Limits(MathEditor editor) => editor.ToMathML().Descendants()
.First(e => e.Name.LocalName is "msub" or "msup" or "msubsup" or "munder" or "mover" or "munderover").Name.LocalName;

var editor = new MathEditor { IsDisplay = true };
editor.Insert(typed);
var built = Limits(editor);
editor.BuildDown();
editor.BuildUpAll();
await Assert.That(built).IsEqualTo(Limits(editor));
}

[Test]
public async Task ArgumentStillToComeDoesNotBuildUp()
{
Expand Down
Loading