Skip to content

Commit

Permalink
Killing four bugs with one PR? (#144)
Browse files Browse the repository at this point in the history
* Killing four bugs with one fix?

* Revert unneeded changes

* Add comments

* Update CSharpMath.Forms.Example.UWP.csproj
  • Loading branch information
Happypig375 authored Jul 18, 2020
1 parent a97d586 commit f75643d
Show file tree
Hide file tree
Showing 96 changed files with 229 additions and 81 deletions.

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -171,9 +171,7 @@
</ItemGroup>
<ItemGroup>
<PackageReference Include="Xamarin.Forms" Version="4.3.0.908675" />
<PackageReference Include="Microsoft.NETCore.UniversalWindowsPlatform">
<Version>6.2.8</Version>
</PackageReference>
<PackageReference Include="Microsoft.NETCore.UniversalWindowsPlatform" Version="6.2.10" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\CSharpMath.Editor\CSharpMath.Editor.csproj">
Expand Down Expand Up @@ -212,4 +210,4 @@
<Target Name="AfterBuild">
</Target>
-->
</Project>
</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ public SelectPage() {
InitializeComponent();
App.AllMathViews.Add(View);
Size.ItemsSource = TryPage.FontSizes;
Size.SelectedItem = View.FontSize;
Size.SelectedIndexChanged += (sender, e) =>
View.FontSize = (float)Size.SelectedItem;
Size.SelectedItem = 96f;
Picker.ItemsSource = Rendering.Tests.TestRenderingMathData.AllConstants.Keys.ToList();
Picker.SelectedIndexChanged += (sender, e) =>
View.LaTeX = Label.Text = Rendering.Tests.TestRenderingMathData.AllConstants[(string)Picker.SelectedItem];
Expand Down
3 changes: 2 additions & 1 deletion CSharpMath.Forms/Buttons.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ public BaseButton() {
var latex = c.Painter.LaTeX;
// Appropriate positioning for non-full characters, e.g. prime, degree
// Also acts as spacing between MathButtons next to each other
c.Painter.LaTeX = @"{\color{#0000}|}" + latex + @"{\color{#0000}|}";
// TODO: Implement and use \phantom
c.Painter.LaTeX = @"{\color{#00000000}|}" + latex + @"{\color{#00000000}|}";
var stream = c.Painter.DrawAsStream();
c.Painter.LaTeX = latex;
return stream;
Expand Down
2 changes: 1 addition & 1 deletion CSharpMath.Forms/MathInputButton.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ public class MathInputButton : MathButton {
public MathInputButton() => Command = new Command(() => Keyboard?.KeyPress(Input));
public static readonly BindableProperty KeyboardProperty =
BindableProperty.Create(nameof(Keyboard), typeof(MathKeyboard), typeof(MathInputButton));
public MathKeyboard Keyboard { get => (MathKeyboard)GetValue(KeyboardProperty); set => SetValue(KeyboardProperty, value); }
public MathKeyboard? Keyboard { get => (MathKeyboard?)GetValue(KeyboardProperty); set => SetValue(KeyboardProperty, value); }
static string InputToLaTeX(MathKeyboardInput input) {
switch (input) {
case MathKeyboardInput.Left: return "\u25C0";
Expand Down
29 changes: 28 additions & 1 deletion CSharpMath.Rendering.Tests/TestRendering.cs
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,33 @@ public void TextCenterInfiniteWidth(string file, string latex) =>
[SkippableTheory, ClassData(typeof(TestRenderingTextData))]
public void TextRightInfiniteWidth(string file, string latex) =>
Run(file, latex, new TTextPainter(), TextAlignment.TopRight, textPainterCanvasWidth: float.PositiveInfinity);
public static TheoryData<float, TextAlignment> TextFontSizesData() {
var data = new TheoryData<float, TextAlignment>();
// TODO: Fix font sizes at 100 and 300
foreach (var fontSize in stackalloc[] { 20, 40, 60 })
foreach (var alignment in typeof(TextAlignment).GetEnumValues().Cast<TextAlignment>())
data.Add(fontSize, alignment);
return data;
}
[SkippableTheory, MemberData(nameof(TextFontSizesData))]
public void TextFontSizes(float fontSize, TextAlignment alignment) =>
Run((fontSize, alignment).ToString(), @"Here are some text.
This text is made to be long enough to have the TextPainter of CSharpMath add a line break to this text automatically.
To demonstrate the capabilities of the TextPainter,
here are some math content:
First, a fraction in inline mode: $\frac34$
Next, a summation in inline mode: $\sum_{i=0}^3i^i$
Then, a summation in display mode: $$\sum_{i=0}^3i^i$$
After that, an integral in display mode: $$\int^6_{-56}x\ dx$$
Finally, an escaped dollar sign \$ that represents the start/end of math mode when it is unescaped.
Colors can be achieved via \backslash color\textit{\{color\}\{content\}}, or \backslash \textit{color\{content\}},
where \textit{color} stands for one of the LaTeX standard colors.
\red{Colored text in text mode are able to automatically break up when spaces are inside the colored text, which the equivalent in math mode cannot do.}
\textbf{Styled} \texttt{text} can be achieved via the LaTeX styling commands.
The SkiaSharp version of this is located at CSharpMath.SkiaSharp.TextPainter;
and the Xamarin.Forms version of this is located at CSharpMath.Forms.TextView.
Was added in 0.1.0-pre4; working in 0.1.0-pre5; fully tested in 0.1.0-pre6. \[\frac{Display}{maths} \sqrt\text\mathtt{\ at\ the\ end}^\mathbf{are\ now\ incuded\ in\ Measure!} \]",
new TTextPainter { FontSize = fontSize }, alignment);
protected void Run<TContent>(
string inFile, string latex, Painter<TCanvas, TContent, TColor> painter, TextAlignment alignment = TextAlignment.TopLeft,
float textPainterCanvasWidth = TextPainter<TCanvas, TColor>.DefaultCanvasWidth, [CallerMemberName] string folder = "") where TContent : class {
Expand All @@ -120,7 +147,7 @@ protected void Run<TContent>(

var expectedFile = new FileInfo(System.IO.Path.Combine(folder, inFile + ".png"));
if (!expectedFile.Exists) {
Skip.If(FileSizeTolerance != 0, "Baseline images may only be created by SkiaSharp.");
Skip.IfNot(FileSizeTolerance is 0, "Baseline images may only be created by SkiaSharp.");
actualFile.CopyTo(expectedFile.FullName);
expectedFile.Refresh();
}
Expand Down
Binary file modified CSharpMath.Rendering.Tests/TextCenter/Accent.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/TextCenter/Alphabets.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/TextCenter/CapitalGreeks.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/TextCenter/Capitals.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/TextCenter/Color.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/TextCenter/Cyrillic.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/TextCenter/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/TextCenter/Greeks.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/TextCenter/InlineMath.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/TextCenter/IntegrationByParts.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/TextCenter/KindergartenQuestion.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/TextCenter/Numbers.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/TextCenter/QuadraticPolynomial.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/TextCenter/WideDisplayMaths.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/TextCenterInfiniteWidth/Accent.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/TextCenterInfiniteWidth/Alphabets.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/TextCenterInfiniteWidth/Capitals.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/TextCenterInfiniteWidth/Color.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/TextCenterInfiniteWidth/Cyrillic.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/TextCenterInfiniteWidth/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/TextCenterInfiniteWidth/Greeks.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.
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/TextCenterInfiniteWidth/Numbers.png
Binary file modified CSharpMath.Rendering.Tests/TextRight/Accent.png
Binary file modified CSharpMath.Rendering.Tests/TextRight/Alphabets.png
Binary file modified CSharpMath.Rendering.Tests/TextRight/CapitalGreeks.png
Binary file modified CSharpMath.Rendering.Tests/TextRight/Capitals.png
Binary file modified CSharpMath.Rendering.Tests/TextRight/Color.png
Binary file modified CSharpMath.Rendering.Tests/TextRight/Cyrillic.png
Binary file modified CSharpMath.Rendering.Tests/TextRight/FontStyles.png
Binary file modified CSharpMath.Rendering.Tests/TextRight/Greeks.png
Binary file modified CSharpMath.Rendering.Tests/TextRight/InlineMath.png
Binary file modified CSharpMath.Rendering.Tests/TextRight/IntegrationByParts.png
Binary file modified CSharpMath.Rendering.Tests/TextRight/KindergartenQuestion.png
Binary file modified CSharpMath.Rendering.Tests/TextRight/Numbers.png
Binary file modified CSharpMath.Rendering.Tests/TextRight/QuadraticPolynomial.png
Binary file modified CSharpMath.Rendering.Tests/TextRight/WideDisplayMaths.png
Binary file modified CSharpMath.Rendering.Tests/TextRightInfiniteWidth/Accent.png
Binary file modified CSharpMath.Rendering.Tests/TextRightInfiniteWidth/Alphabets.png
Binary file modified CSharpMath.Rendering.Tests/TextRightInfiniteWidth/Capitals.png
Binary file modified CSharpMath.Rendering.Tests/TextRightInfiniteWidth/Color.png
Binary file modified CSharpMath.Rendering.Tests/TextRightInfiniteWidth/Cyrillic.png
Binary file modified CSharpMath.Rendering.Tests/TextRightInfiniteWidth/FontStyles.png
Binary file modified CSharpMath.Rendering.Tests/TextRightInfiniteWidth/Greeks.png
Binary file modified CSharpMath.Rendering.Tests/TextRightInfiniteWidth/InlineMath.png
Binary file modified CSharpMath.Rendering.Tests/TextRightInfiniteWidth/Numbers.png
3 changes: 3 additions & 0 deletions CSharpMath.Rendering/FrontEnd/TextPainter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,9 @@ private void DrawCore(TCanvas canvas, float? width, TextAlignment alignment,
? System.Math.Max(_relativeXCoordDisplay.Displays.CollectionWidth(),
_absoluteXCoordDisplay.Displays.IsNonEmpty() ? _absoluteXCoordDisplay.Displays.Max(d => d.Width) : 0)
: c.Width;
// https://github.com/verybadcat/CSharpMath/issues/123
// Take into account padding, offset etc. on both sides
adjustedCanvasWidth -= _relativeXCoordDisplay.Position.X * 2;
float Δx = 0;
var y = float.NegativeInfinity;
var leftRightFlags = alignment & (TextAlignment.Left | TextAlignment.Right);
Expand Down
6 changes: 5 additions & 1 deletion CSharpMath.Xaml/Views.cs
Original file line number Diff line number Diff line change
Expand Up @@ -156,8 +156,12 @@ protected override void OnTouch(global::SkiaSharp.Views.Forms.SKTouchEventArgs e
}
protected sealed override void OnPaintSurface(global::SkiaSharp.Views.Forms.SKPaintSurfaceEventArgs e) {
base.OnPaintSurface(e);
e.Surface.Canvas.Clear();
var canvas = e.Surface.Canvas;
canvas.Clear();
// https://github.com/verybadcat/CSharpMath/issues/136 and https://github.com/verybadcat/CSharpMath/issues/137
// SkiaSharp deals with raw pixels as opposed to Xamarin.Forms's device-independent units.
// We should scale to occupy the full view size.
canvas.Scale(e.Info.Width / (float)Width);
#endif
Painter.Draw(canvas, TextAlignment, Padding, DisplacementX, DisplacementY);
}
Expand Down
141 changes: 69 additions & 72 deletions CSharpMath/Display/Typesetter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ private void CreateDisplayAtoms(List<MathAtom> preprocessedAtoms) {
case Variable _:
case UnaryOperator _:
throw new InvalidCodePathException
($"Type {atom.GetType()} should have been removed by preprocessing");
($"Type {atom.TypeName} should have been removed by preprocessing");
case Space space:
AddDisplayLine(false);
_currentPosition.X += space.ActualLength(_mathTable, _font);
Expand Down Expand Up @@ -232,7 +232,7 @@ private void CreateDisplayAtoms(List<MathAtom> preprocessedAtoms) {
AddInterElementSpace(prevAtom, inner);
IDisplay<TFont, TGlyph> innerDisplay;
if (inner.LeftBoundary != Boundary.Empty || inner.RightBoundary != Boundary.Empty) {
innerDisplay = _MakeInner(inner, atom.IndexRange);
innerDisplay = MakeInner(inner, atom.IndexRange);
} else {
innerDisplay = CreateLine(inner.InnerList, _font, _context, _style, _cramped);
}
Expand Down Expand Up @@ -522,6 +522,20 @@ private void AddInterElementSpace(MathAtom? prev, MathAtom current) =>
return null;
}
private RadicalDisplay<TFont, TGlyph> MakeRadical(MathList radicand, Range range) {
IGlyphDisplay<TFont, TGlyph> _GetRadicalGlyph(float radicalHeight) {
// TODO: something related to GlyphFinder.FindGlyph
var radicalGlyph = _context.GlyphFinder.FindGlyphForCharacterAtIndex(_font, 0, "\u221A");
var glyph = FindGlyph(radicalGlyph, radicalHeight,
out float glyphAscent, out float glyphDescent, out float glyphWidth);

return
glyphAscent + glyphDescent < radicalHeight
// the glyphs are not big enough, so we construct one using extenders
&& ConstructGlyph(radicalGlyph, radicalHeight) is IGlyphDisplay<TFont, TGlyph> constructed
? constructed
: new GlyphDisplay<TFont, TGlyph>
(glyph, Range.NotFound, _styleFont, glyphAscent, glyphDescent, glyphWidth);
}
var innerDisplay = CreateLine(radicand, _font, _context, _style, true);
var radicalVerticalGap =
_style == LineStyle.Display
Expand Down Expand Up @@ -562,46 +576,43 @@ private RadicalDisplay<TFont, TGlyph> MakeRadical(MathList radicand, Range range
};
}

private float _NumeratorShiftUp(bool hasRule) =>
(hasRule, _style) switch
{
(true, LineStyle.Display) => _mathTable.FractionNumeratorDisplayStyleShiftUp(_styleFont),
(true, _) => _mathTable.FractionNumeratorShiftUp(_styleFont),
(false, LineStyle.Display) => _mathTable.StackTopDisplayStyleShiftUp(_styleFont),
(false, _) => _mathTable.StackTopShiftUp(_styleFont)
};
private float _NumeratorGapMin =>
_style == LineStyle.Display
? _mathTable.FractionNumDisplayStyleGapMin(_styleFont)
: _mathTable.FractionNumeratorGapMin(_styleFont);

private float _DenominatorShiftDown(bool hasRule) =>
(hasRule, _style) switch
{
(true, LineStyle.Display) => _mathTable.FractionDenominatorDisplayStyleShiftDown(_styleFont),
(true, _) => _mathTable.FractionDenominatorShiftDown(_styleFont),
(false, LineStyle.Display) => _mathTable.StackBottomDisplayStyleShiftDown(_styleFont),
(false, _) => _mathTable.StackBottomShiftDown(_styleFont)
};

private float _DenominatorGapMin =>
_style == LineStyle.Display
? _mathTable.FractionDenomDisplayStyleGapMin(_styleFont)
: _mathTable.FractionDenominatorGapMin(_styleFont);

private float _StackGapMin =>
_style == LineStyle.Display
? _mathTable.StackDisplayStyleGapMin(_styleFont)
: _mathTable.StackGapMin(_styleFont);

private float _FractionDelimiterHeight =>
_style == LineStyle.Display
? _mathTable.FractionDelimiterDisplayStyleSize(_styleFont)
: _mathTable.FractionDelimiterSize(_styleFont);

private IDisplay<TFont, TGlyph> MakeFraction(Fraction fraction) {
float _NumeratorShiftUp(bool hasRule) =>
(hasRule, _style) switch
{
(true, LineStyle.Display) => _mathTable.FractionNumeratorDisplayStyleShiftUp(_styleFont),
(true, _) => _mathTable.FractionNumeratorShiftUp(_styleFont),
(false, LineStyle.Display) => _mathTable.StackTopDisplayStyleShiftUp(_styleFont),
(false, _) => _mathTable.StackTopShiftUp(_styleFont)
};
float _NumeratorGapMin() =>
_style == LineStyle.Display
? _mathTable.FractionNumDisplayStyleGapMin(_styleFont)
: _mathTable.FractionNumeratorGapMin(_styleFont);

float _DenominatorShiftDown(bool hasRule) =>
(hasRule, _style) switch
{
(true, LineStyle.Display) => _mathTable.FractionDenominatorDisplayStyleShiftDown(_styleFont),
(true, _) => _mathTable.FractionDenominatorShiftDown(_styleFont),
(false, LineStyle.Display) => _mathTable.StackBottomDisplayStyleShiftDown(_styleFont),
(false, _) => _mathTable.StackBottomShiftDown(_styleFont)
};
float _DenominatorGapMin() =>
_style == LineStyle.Display
? _mathTable.FractionDenomDisplayStyleGapMin(_styleFont)
: _mathTable.FractionDenominatorGapMin(_styleFont);
float _StackGapMin() =>
_style == LineStyle.Display
? _mathTable.StackDisplayStyleGapMin(_styleFont)
: _mathTable.StackGapMin(_styleFont);
float _FractionDelimiterHeight() =>
_style == LineStyle.Display
? _mathTable.FractionDelimiterDisplayStyleSize(_styleFont)
: _mathTable.FractionDelimiterSize(_styleFont);

var numeratorDisplay =
CreateLine(fraction.Numerator, _font, _context, _fractionStyle, false);
CreateLine(fraction.Numerator, _font, _context, _fractionStyle, false);
var denominatorDisplay =
CreateLine(fraction.Denominator, _font, _context, _fractionStyle, true);

Expand All @@ -616,20 +627,20 @@ private IDisplay<TFont, TGlyph> MakeFraction(Fraction fraction) {
var distanceFromNumeratorToBar =
numeratorShiftUp - numeratorDisplay.Descent - (barLocation + barThickness / 2);
// The distance should be at least displayGap
if (distanceFromNumeratorToBar < _NumeratorGapMin) {
numeratorShiftUp += (_NumeratorGapMin - distanceFromNumeratorToBar);
if (distanceFromNumeratorToBar < _NumeratorGapMin()) {
numeratorShiftUp += (_NumeratorGapMin() - distanceFromNumeratorToBar);
}
// now, do the same for the denominator
var distanceFromDenominatorToBar =
barLocation - barThickness / 2 - (denominatorDisplay.Ascent - denominatorShiftDown);
if (distanceFromDenominatorToBar < _DenominatorGapMin) {
denominatorShiftDown += _DenominatorGapMin - distanceFromDenominatorToBar;
if (distanceFromDenominatorToBar < _DenominatorGapMin()) {
denominatorShiftDown += _DenominatorGapMin() - distanceFromDenominatorToBar;
}
} else {
float clearance =
numeratorShiftUp - numeratorDisplay.Descent
- (denominatorDisplay.Ascent - denominatorShiftDown);
float minClearance = _StackGapMin;
float minClearance = _StackGapMin();
if (clearance < minClearance) {
numeratorShiftUp += (minClearance - clearance / 2);
denominatorShiftDown += (minClearance - clearance) / 2;
Expand All @@ -649,11 +660,11 @@ private IDisplay<TFont, TGlyph> MakeFraction(Fraction fraction) {

if (fraction.LeftDelimiter is null && fraction.RightDelimiter is null)
return display;
var glyphHeight = _FractionDelimiterHeight;
var glyphHeight = _FractionDelimiterHeight();
var position = new PointF();
var innerGlyphs = new List<IDisplay<TFont, TGlyph>>();
if (fraction.LeftDelimiter?.Length > 0) {
var leftGlyph = _FindGlyphForBoundary(fraction.LeftDelimiter, glyphHeight);
var leftGlyph = FindGlyphForBoundary(fraction.LeftDelimiter, glyphHeight);
leftGlyph.Position = position;
innerGlyphs.Add(leftGlyph);
position.X += leftGlyph.Width;
Expand All @@ -662,7 +673,7 @@ private IDisplay<TFont, TGlyph> MakeFraction(Fraction fraction) {
position.X += display.Width;
innerGlyphs.Add(display);
if (fraction.RightDelimiter?.Length > 0) {
var rightGlyph = _FindGlyphForBoundary(fraction.RightDelimiter, glyphHeight);
var rightGlyph = FindGlyphForBoundary(fraction.RightDelimiter, glyphHeight);
rightGlyph.Position = position;
innerGlyphs.Add(rightGlyph);
position.X += rightGlyph.Width;
Expand All @@ -672,7 +683,7 @@ private IDisplay<TFont, TGlyph> MakeFraction(Fraction fraction) {
};
}

private InnerDisplay<TFont, TGlyph> _MakeInner(Inner inner, Range range) {
private InnerDisplay<TFont, TGlyph> MakeInner(Inner inner, Range range) {
if (inner.LeftBoundary == Boundary.Empty && inner.RightBoundary == Boundary.Empty) {
throw new InvalidCodePathException("Inner should have a boundary to call this function.");
}
Expand All @@ -689,24 +700,24 @@ private InnerDisplay<TFont, TGlyph> _MakeInner(Inner inner, Range range) {

var leftGlyph =
inner.LeftBoundary is Boundary { Nucleus: var left } && left.Length > 0
? _FindGlyphForBoundary(left, glyphHeight)
? FindGlyphForBoundary(left, glyphHeight)
: null;

var rightGlyph =
inner.RightBoundary is Boundary { Nucleus: var right } && right.Length > 0
? _FindGlyphForBoundary(right, glyphHeight)
? FindGlyphForBoundary(right, glyphHeight)
: null;
return new InnerDisplay<TFont, TGlyph>(innerListDisplay, leftGlyph, rightGlyph, range);
}

private IGlyphDisplay<TFont, TGlyph> _FindGlyphForBoundary(
private IGlyphDisplay<TFont, TGlyph> FindGlyphForBoundary(
string delimiter, float glyphHeight) {
var leftGlyph = _context.GlyphFinder.FindGlyphForCharacterAtIndex(_font, 0, delimiter);
var glyph = _FindGlyph(leftGlyph, glyphHeight,
var glyph = FindGlyph(leftGlyph, glyphHeight,
out float glyphAscent, out float glyphDescent, out float glyphWidth);
var glyphDisplay =
glyphAscent + glyphDescent < glyphHeight
&& _ConstructGlyph(leftGlyph, glyphHeight) is IGlyphDisplay<TFont, TGlyph> constructed
&& ConstructGlyph(leftGlyph, glyphHeight) is IGlyphDisplay<TFont, TGlyph> constructed
? constructed
: new GlyphDisplay<TFont, TGlyph>
(glyph, Range.NotFound, _styleFont, glyphAscent, glyphDescent, glyphWidth);
Expand All @@ -718,35 +729,21 @@ private IGlyphDisplay<TFont, TGlyph> _FindGlyphForBoundary(
return glyphDisplay;
}

private IGlyphDisplay<TFont, TGlyph> _GetRadicalGlyph(float radicalHeight) {
// TODO: something related to GlyphFinder.FindGlyph
var radicalGlyph = _context.GlyphFinder.FindGlyphForCharacterAtIndex(_font, 0, "\u221A");
var glyph = _FindGlyph(radicalGlyph, radicalHeight,
out float glyphAscent, out float glyphDescent, out float glyphWidth);

return
glyphAscent + glyphDescent < radicalHeight
// the glyphs are not big enough, so we construct one using extenders
&& _ConstructGlyph(radicalGlyph, radicalHeight) is IGlyphDisplay<TFont, TGlyph> constructed
? constructed
: new GlyphDisplay<TFont, TGlyph>
(glyph, Range.NotFound, _styleFont, glyphAscent, glyphDescent, glyphWidth);
}

private GlyphConstructionDisplay<TFont, TGlyph>? _ConstructGlyph(TGlyph glyph, float glyphHeight) {
private GlyphConstructionDisplay<TFont, TGlyph>? ConstructGlyph(TGlyph glyph, float glyphHeight) {
var parts = _mathTable.GetVerticalGlyphAssembly(glyph, _styleFont);
if (parts is null) return null;
var glyphs = new List<TGlyph>();
var offsets = new List<float>();
float height = _ConstructGlyphWithParts(parts, glyphHeight, glyphs, offsets);
float height = ConstructGlyphWithParts(parts, glyphHeight, glyphs, offsets);
using var singleGlyph = new Structures.RentedArray<TGlyph>(glyphs[0]);
// descent:0 because it's up to the rendering to adjust the display glyph up or down by setting ShiftDown
return new GlyphConstructionDisplay<TFont, TGlyph>
(glyphs, offsets, _styleFont, height, 0, _context.GlyphBoundsProvider
.GetAdvancesForGlyphs(_styleFont, singleGlyph.Result, 1).Total);
}

private float _ConstructGlyphWithParts(IEnumerable<GlyphPart<TGlyph>> parts,
private float ConstructGlyphWithParts(IEnumerable<GlyphPart<TGlyph>> parts,
float glyphHeight, List<TGlyph> glyphs, List<float> offsets) {
for (int nExtenders = 0; ; nExtenders++) {
glyphs.Clear();
Expand Down Expand Up @@ -800,7 +797,7 @@ private float _ConstructGlyphWithParts(IEnumerable<GlyphPart<TGlyph>> parts,
}
}

private TGlyph _FindGlyph(TGlyph rawGlyph, float height,
private TGlyph FindGlyph(TGlyph rawGlyph, float height,
out float glyphAscent, out float glyphDescent, out float glyphWidth) {
// in iosMath.
glyphAscent = glyphDescent = glyphWidth = float.NaN;
Expand Down

0 comments on commit f75643d

Please sign in to comment.