Skip to content

Commit

Permalink
Inside MainViewModel.Export(), exception handling was made using a …
Browse files Browse the repository at this point in the history
…more specific exception type
  • Loading branch information
Lehonti Ramos committed Sep 27, 2023
1 parent 837e6da commit aaba30f
Showing 1 changed file with 39 additions and 41 deletions.
80 changes: 39 additions & 41 deletions src/WpfMath.Example/MainViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
using WpfMath.Parsers;
using WpfMath.Rendering;
using XamlMath;
using XamlMath.Exceptions;

namespace WpfMath.Example;

Expand Down Expand Up @@ -90,7 +91,7 @@ public string Formula
}

public ObservableCollection<Preset> Presets { get; }

public double Scale
{
get => _scale;
Expand All @@ -115,53 +116,50 @@ private void Export()
if (result is false)
return;

// Create formula object from input text.
TexFormula? formula = null;
try
{
formula = WpfTeXFormulaParser.Instance.Parse(Formula);
// Create formula object from input text.
TexFormula formula = WpfTeXFormulaParser.Instance.Parse(Formula);
var scale = Scale;
var environment = WpfTeXEnvironment.Create(scale: scale);

// Open stream
var filename = saveFileDialog.FileName;
using var stream = new FileStream(filename, FileMode.Create);
switch (saveFileDialog.FilterIndex)
{
case 1:
var geometry = formula.RenderToGeometry(environment, scale: scale);
var converter = new SVGConverter();
var svgPathText = converter.ConvertGeometry(geometry);
var svgBuilder = new StringBuilder();
svgBuilder.AppendLine("<?xml version=\"1.0\" encoding=\"UTF-8\" ?>")
.AppendLine("<svg xmlns=\"http://www.w3.org/2000/svg\" version=\"1.1\" >")
.AppendLine(svgPathText)
.AppendLine("</svg>");
var svgText = svgBuilder.ToString();
using (var writer = new StreamWriter(stream))
writer.WriteLine(svgText);
break;

case 2:
var bitmap = formula.RenderToBitmap(environment, scale, dpi: 300);
var encoder = new PngBitmapEncoder
{
Frames = { BitmapFrame.Create(bitmap) }
};
encoder.Save(stream);
break;

default:
return;
}
}
catch (Exception ex)
catch (TexParseException ex)
{
MessageBox.Show("An error occurred while parsing the given input:" + Environment.NewLine +
Environment.NewLine + ex.Message, "WPF-Math Example",
MessageBoxButton.OK, MessageBoxImage.Error);
return;
}

var scale = Scale;
var environment = WpfTeXEnvironment.Create(scale: scale);

// Open stream
var filename = saveFileDialog.FileName;
using var stream = new FileStream(filename, FileMode.Create);
switch (saveFileDialog.FilterIndex)
{
case 1:
var geometry = formula.RenderToGeometry(environment, scale: scale);
var converter = new SVGConverter();
var svgPathText = converter.ConvertGeometry(geometry);
var svgBuilder = new StringBuilder();
svgBuilder.AppendLine("<?xml version=\"1.0\" encoding=\"UTF-8\" ?>")
.AppendLine("<svg xmlns=\"http://www.w3.org/2000/svg\" version=\"1.1\" >")
.AppendLine(svgPathText)
.AppendLine("</svg>");
var svgText = svgBuilder.ToString();
using (var writer = new StreamWriter(stream))
writer.WriteLine(svgText);
break;

case 2:
var bitmap = formula.RenderToBitmap(environment, scale, dpi: 300);
var encoder = new PngBitmapEncoder
{
Frames = { BitmapFrame.Create(bitmap) }
};
encoder.Save(stream);
break;

default:
return;
}
}

Expand Down

0 comments on commit aaba30f

Please sign in to comment.