Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Inside MainViewModel.Export(), exception handling was made using a more specific exception type #466

Merged
merged 4 commits into from
Sep 28, 2023
Merged
Changes from 2 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
92 changes: 45 additions & 47 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 @@ -109,59 +110,56 @@ public Preset? SelectedPreset

private void Export()
{
// Choose file
var saveFileDialog = new SaveFileDialog { Filter = "SVG Files (*.svg)|*.svg|PNG Files (*.png)|*.png" };
var result = saveFileDialog.ShowDialog();
if (result is false)
return;

// Create formula object from input text.
TexFormula? formula = null;
try
{
formula = WpfTeXFormulaParser.Instance.Parse(Formula);
// Choose file
var saveFileDialog = new SaveFileDialog { Filter = "SVG Files (*.svg)|*.svg|PNG Files (*.png)|*.png" };
var result = saveFileDialog.ShowDialog();
if (result is false)
return;

// 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
Loading