Skip to content

Commit

Permalink
Simplify the matrix box rendering code (ForNeVeR#100)
Browse files Browse the repository at this point in the history
Now there're no special atoms for matrices, because we can reuse the
original FencedAtom.
  • Loading branch information
ForNeVeR committed Aug 11, 2019
1 parent c2f6dc7 commit 54b1851
Show file tree
Hide file tree
Showing 11 changed files with 2,082 additions and 2,774 deletions.
682 changes: 234 additions & 448 deletions src/WpfMath.Tests/TestResults/BoxTests.casesBox.approved.txt

Large diffs are not rendered by default.

1,318 changes: 560 additions & 758 deletions src/WpfMath.Tests/TestResults/BoxTests.nestedMatrixBox.approved.txt

Large diffs are not rendered by default.

713 changes: 323 additions & 390 deletions src/WpfMath.Tests/TestResults/BoxTests.simpleMatrixBox.approved.txt

Large diffs are not rendered by default.

1,931 changes: 865 additions & 1,066 deletions src/WpfMath.Tests/TestResults/BoxTests.wideItemInMatrixBox.approved.txt

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -372,6 +372,20 @@
"Source": "\\Gamma_{\\mu \\rho} ^{\\sigma}= \\pmatrix{\\pmatrix{0 & 0 & 0 \\\\ 0 & -r & 0 \\\\ 0 & 0 & -r sin^2(\\theta)} \\\\ \\pmatrix{0 & \\frac{1}{r} & 0 \\\\ \\frac{1}{r} & 0 & 0 \\\\ 0 & 0 & -\\sin(\\theta) \\cos(\\theta)} \\\\ \\pmatrix{0 & 0 & \\frac{1}{r} \\\\ 0 & 0 & \\frac{1}{\\tan(\\theta)} \\\\ \\frac{1}{r} & \\frac{1}{\\tan(\\theta)} & 0 }}"
}
},
"LeftDelimeter": {
"IsDelimeter": true,
"Name": "lbrack",
"IsTextSymbol": false,
"Type": "Opening",
"Source": null
},
"RightDelimeter": {
"IsDelimeter": true,
"Name": "rbrack",
"IsTextSymbol": false,
"Type": "Closing",
"Source": null
},
"Type": "Ordinary",
"Source": {
"Start": 47,
Expand Down Expand Up @@ -773,6 +787,20 @@
"Source": "\\Gamma_{\\mu \\rho} ^{\\sigma}= \\pmatrix{\\pmatrix{0 & 0 & 0 \\\\ 0 & -r & 0 \\\\ 0 & 0 & -r sin^2(\\theta)} \\\\ \\pmatrix{0 & \\frac{1}{r} & 0 \\\\ \\frac{1}{r} & 0 & 0 \\\\ 0 & 0 & -\\sin(\\theta) \\cos(\\theta)} \\\\ \\pmatrix{0 & 0 & \\frac{1}{r} \\\\ 0 & 0 & \\frac{1}{\\tan(\\theta)} \\\\ \\frac{1}{r} & \\frac{1}{\\tan(\\theta)} & 0 }}"
}
},
"LeftDelimeter": {
"IsDelimeter": true,
"Name": "lbrack",
"IsTextSymbol": false,
"Type": "Opening",
"Source": null
},
"RightDelimeter": {
"IsDelimeter": true,
"Name": "rbrack",
"IsTextSymbol": false,
"Type": "Closing",
"Source": null
},
"Type": "Ordinary",
"Source": {
"Start": 112,
Expand Down Expand Up @@ -1205,6 +1233,20 @@
"Source": "\\Gamma_{\\mu \\rho} ^{\\sigma}= \\pmatrix{\\pmatrix{0 & 0 & 0 \\\\ 0 & -r & 0 \\\\ 0 & 0 & -r sin^2(\\theta)} \\\\ \\pmatrix{0 & \\frac{1}{r} & 0 \\\\ \\frac{1}{r} & 0 & 0 \\\\ 0 & 0 & -\\sin(\\theta) \\cos(\\theta)} \\\\ \\pmatrix{0 & 0 & \\frac{1}{r} \\\\ 0 & 0 & \\frac{1}{\\tan(\\theta)} \\\\ \\frac{1}{r} & \\frac{1}{\\tan(\\theta)} & 0 }}"
}
},
"LeftDelimeter": {
"IsDelimeter": true,
"Name": "lbrack",
"IsTextSymbol": false,
"Type": "Opening",
"Source": null
},
"RightDelimeter": {
"IsDelimeter": true,
"Name": "rbrack",
"IsTextSymbol": false,
"Type": "Closing",
"Source": null
},
"Type": "Ordinary",
"Source": {
"Start": 206,
Expand All @@ -1226,6 +1268,20 @@
"Source": "\\Gamma_{\\mu \\rho} ^{\\sigma}= \\pmatrix{\\pmatrix{0 & 0 & 0 \\\\ 0 & -r & 0 \\\\ 0 & 0 & -r sin^2(\\theta)} \\\\ \\pmatrix{0 & \\frac{1}{r} & 0 \\\\ \\frac{1}{r} & 0 & 0 \\\\ 0 & 0 & -\\sin(\\theta) \\cos(\\theta)} \\\\ \\pmatrix{0 & 0 & \\frac{1}{r} \\\\ 0 & 0 & \\frac{1}{\\tan(\\theta)} \\\\ \\frac{1}{r} & \\frac{1}{\\tan(\\theta)} & 0 }}"
}
},
"LeftDelimeter": {
"IsDelimeter": true,
"Name": "lbrack",
"IsTextSymbol": false,
"Type": "Opening",
"Source": null
},
"RightDelimeter": {
"IsDelimeter": true,
"Name": "rbrack",
"IsTextSymbol": false,
"Type": "Closing",
"Source": null
},
"Type": "Ordinary",
"Source": {
"Start": 38,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -574,6 +574,14 @@
"Source": "f(x) = \\cases{1/3 & if \\thinspace 0\\le x\\le 1;\\cr 2/3 & if \\thinspace 3\\le x \\le 4; \\cr 0 & elsewhere.\\cr}"
}
},
"LeftDelimeter": {
"IsDelimeter": true,
"Name": "lbrace",
"IsTextSymbol": false,
"Type": "Opening",
"Source": null
},
"RightDelimeter": null,
"Type": "Ordinary",
"Source": {
"Start": 14,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -334,6 +334,14 @@
"Source": "\\cases{x,&if x > 0;\\cr -x,& otherwise.}"
}
},
"LeftDelimeter": {
"IsDelimeter": true,
"Name": "lbrace",
"IsTextSymbol": false,
"Type": "Opening",
"Source": null
},
"RightDelimeter": null,
"Type": "Ordinary",
"Source": {
"Start": 7,
Expand Down
30 changes: 0 additions & 30 deletions src/WpfMath/Atoms/CasesAtom.cs

This file was deleted.

58 changes: 16 additions & 42 deletions src/WpfMath/Atoms/MatrixAtom.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Collections.ObjectModel;
using System.Linq;
using System.Text.RegularExpressions;
using System.Windows.Media;
using WpfMath.Boxes;

namespace WpfMath.Atoms
Expand Down Expand Up @@ -34,10 +35,6 @@ public MatrixAtom(

protected override Box CreateBoxCore(TexEnvironment environment)
{
var texFont = environment.MathFont;
var style = environment.Style;
var axis = texFont.GetAxisHeight(style);

var rowCount = MatrixCells.Count;
var maxColumnCount = MatrixCells.Max(row => row.Count);

Expand All @@ -55,44 +52,21 @@ protected override Box CreateBoxCore(TexEnvironment environment)
var rowsContainer = new VerticalBox();
foreach (var row in rowBoxes)
{
var verticalPadding = new StrutBox(0.0, VerticalPadding / 2, 0, 0);

rowsContainer.Add(verticalPadding);
rowsContainer.Add(row);
rowsContainer.Add(verticalPadding);
}

rowsContainer.Depth = rowsContainer.Height / 2;
ApplyCellSizes(rowBoxes, matrixCellGaps);

double sigmaTotalHeight = 0;
double sigmaDepth = 0;
double sigmaHeight = 0;
double adjwidth = 0;
foreach (var item in rowsContainer.Children)
{
sigmaTotalHeight += item.TotalHeight;
sigmaHeight += item.Height;
sigmaDepth += item.Depth;
if (item.TotalWidth>adjwidth)
{
adjwidth = item.TotalWidth;
}
}

double adjustedTotalHeight = rowHeights.Sum()+ (rowCount * VerticalPadding);

rowsContainer.Depth = 0;
rowsContainer.Height = adjustedTotalHeight>sigmaTotalHeight?adjustedTotalHeight:sigmaTotalHeight;
rowsContainer.Width = adjwidth>rowsContainer.TotalWidth?adjwidth:rowsContainer.TotalWidth;
var enviroYDiff = axis>= rowsContainer.TotalHeight ? - (axis- rowsContainer.TotalHeight)/2: ( rowsContainer.TotalHeight-axis) / 2;
rowsContainer.Shift = enviroYDiff;
var adjustedWidth = rowBoxes.Max(r => r.Width);
var adjustedHeight = rowHeights.Sum();

var finalbox = new HorizontalBox() ;
finalbox.Add(new StrutBox(HorizontalPadding/8, 0, 0, 0));
finalbox.Add(rowsContainer);
finalbox.Add(new StrutBox(HorizontalPadding/8, 0, 0, 0));
rowsContainer.Depth = adjustedHeight / 2;
rowsContainer.Height = adjustedHeight / 2;
rowsContainer.Width = adjustedWidth;
rowsContainer.Shift = 0;

return finalbox;
return rowsContainer;
}

private HorizontalBox CreateRowBox(
Expand Down Expand Up @@ -129,9 +103,9 @@ private HorizontalBox CreateRowBox(
//cell box holder
var rowcolbox = new VerticalBox() {Tag = $"Cell{rowIndex}:{j}"};

var celltoppad = new StrutBox(rowcellbox.TotalWidth, VerticalPadding / 2, 0, 0) {Tag = $"CellTopPad{rowIndex}:{j}",};
var celltoppad = new StrutBox(rowcellbox.TotalWidth, VerticalPadding / 2, 0, 0) {Tag = $"CellTopPad{rowIndex}:{j}"};
var cellbottompad = new StrutBox(rowcellbox.TotalWidth, VerticalPadding / 2, 0, 0)
{Tag = $"CellBottomPad{rowIndex}:{j}",};
{Tag = $"CellBottomPad{rowIndex}:{j}"};
rowcolbox.Add(celltoppad);
rowcolbox.Add(rowcellbox);
rowcolbox.Add(cellbottompad);
Expand Down Expand Up @@ -199,11 +173,11 @@ private void ApplyCellSizes(List<HorizontalBox> rowBoxes, List<List<CellGaps>> m
double rowwidth = 0;
for (int j = 0; j < row.Children.Count; j++)
{
var currowcolitem = ((HorizontalBox) row).Children[j];
var prevrowcolitem = j > 0 ? ((HorizontalBox) row).Children[j - 1] : ((HorizontalBox) row).Children[j];
var nextrowcolitem = j < ((HorizontalBox) row).Children.Count - 1
? ((HorizontalBox) row).Children[j + 1]
: ((HorizontalBox) row).Children[j];
var currowcolitem = row.Children[j];
var prevrowcolitem = j > 0 ? row.Children[j - 1] : row.Children[j];
var nextrowcolitem = j < row.Children.Count - 1
? row.Children[j + 1]
: row.Children[j];

if (currowcolitem is VerticalBox && Regex.IsMatch(currowcolitem.Tag.ToString(), @"Cell[0-9]+:[0-9]+"))
{
Expand Down
38 changes: 0 additions & 38 deletions src/WpfMath/Atoms/PmatrixAtom.cs

This file was deleted.

14 changes: 12 additions & 2 deletions src/WpfMath/TexFormulaParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -389,7 +389,12 @@ private Atom ProcessCommand(
var matrixsource = ReadElement(value, ref position);

var cells = GetMatrixData(formula, matrixsource);
return new CasesAtom(matrixsource, new MatrixAtom(matrixsource, cells, matrixCellAlignment: MatrixCellAlignment.Left));
var matrix = new MatrixAtom(matrixsource, cells, MatrixCellAlignment.Left);
return new FencedAtom(
matrixsource,
matrix,
GetDelimiterSymbol("lbrace", null),
null);
}

case "frac":
Expand Down Expand Up @@ -450,7 +455,12 @@ private Atom ProcessCommand(
var matrixsource = ReadElement(value, ref position);

var cells = GetMatrixData(formula, matrixsource);
return new PmatrixAtom(matrixsource, new MatrixAtom(matrixsource, cells));
var matrix = new MatrixAtom(matrixsource, cells);
return new FencedAtom(
matrixsource,
matrix,
GetDelimiterSymbol("lbrack", null),
GetDelimiterSymbol("rbrack", null));
}

case "right":
Expand Down

0 comments on commit 54b1851

Please sign in to comment.