Skip to content

Commit

Permalink
Add project files.
Browse files Browse the repository at this point in the history
  • Loading branch information
vertexi committed Mar 14, 2024
1 parent f636877 commit fcc24ec
Show file tree
Hide file tree
Showing 17 changed files with 814 additions and 0 deletions.
454 changes: 454 additions & 0 deletions .gitignore

Large diffs are not rendered by default.

6 changes: 6 additions & 0 deletions Directory.Build.props
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<Project>
<PropertyGroup>
<Nullable>enable</Nullable>
<AvaloniaVersion>11.0.2</AvaloniaVersion>
</PropertyGroup>
</Project>
24 changes: 24 additions & 0 deletions tex2mathml.Desktop/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using System;

using Avalonia;
using Avalonia.ReactiveUI;

namespace tex2mathml.Desktop;

class Program
{
// Initialization code. Don't use any Avalonia, third-party APIs or any
// SynchronizationContext-reliant code before AppMain is called: things aren't initialized
// yet and stuff might break.
[STAThread]
public static void Main(string[] args) => BuildAvaloniaApp()
.StartWithClassicDesktopLifetime(args);

// Avalonia configuration, don't remove; also used by visual designer.
public static AppBuilder BuildAvaloniaApp()
=> AppBuilder.Configure<App>()
.UsePlatformDetect()
.WithInterFont()
.LogToTrace()
.UseReactiveUI();
}
18 changes: 18 additions & 0 deletions tex2mathml.Desktop/app.manifest
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?xml version="1.0" encoding="utf-8"?>
<assembly manifestVersion="1.0" xmlns="urn:schemas-microsoft-com:asm.v1">
<!-- This manifest is used on Windows only.
Don't remove it as it might cause problems with window transparency and embeded controls.
For more details visit https://learn.microsoft.com/en-us/windows/win32/sbscs/application-manifests -->
<assemblyIdentity version="1.0.0.0" name="AvaloniaTest.Desktop"/>

<compatibility xmlns="urn:schemas-microsoft-com:compatibility.v1">
<application>
<!-- A list of the Windows versions that this application has been tested on
and is designed to work with. Uncomment the appropriate elements
and Windows will automatically select the most compatible environment. -->

<!-- Windows 10 -->
<supportedOS Id="{8e0f7a12-bfb3-4fe8-b9a5-48fd50a15a9a}" />
</application>
</compatibility>
</assembly>
Binary file added tex2mathml.Desktop/assets/texmath.exe
Binary file not shown.
25 changes: 25 additions & 0 deletions tex2mathml.Desktop/tex2mathml.Desktop.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>WinExe</OutputType>
<!--If you are willing to use Windows/MacOS native APIs you will need to create 3 projects.
One for Windows with net7.0-windows TFM, one for MacOS with net7.0-macos and one with net7.0 TFM for Linux.-->
<TargetFramework>net7.0</TargetFramework>
<Nullable>enable</Nullable>
<BuiltInComInteropSupport>true</BuiltInComInteropSupport>
<ApplicationManifest>app.manifest</ApplicationManifest>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Avalonia.Desktop" Version="$(AvaloniaVersion)" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\tex2mathml\tex2mathml.csproj" />
</ItemGroup>

<ItemGroup>
<None Update="assets\texmath.exe">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
</ItemGroup>
</Project>
31 changes: 31 additions & 0 deletions tex2mathml.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.8.34330.188
MinimumVisualStudioVersion = 10.0.40219.1
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "tex2mathml", "tex2mathml\tex2mathml.csproj", "{01E4DFBE-2C43-45B3-B52B-1378C517CE97}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "tex2mathml.Desktop", "tex2mathml.Desktop\tex2mathml.Desktop.csproj", "{85DB25CA-79D3-44D9-AB25-A85053A05DFE}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{01E4DFBE-2C43-45B3-B52B-1378C517CE97}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{01E4DFBE-2C43-45B3-B52B-1378C517CE97}.Debug|Any CPU.Build.0 = Debug|Any CPU
{01E4DFBE-2C43-45B3-B52B-1378C517CE97}.Release|Any CPU.ActiveCfg = Release|Any CPU
{01E4DFBE-2C43-45B3-B52B-1378C517CE97}.Release|Any CPU.Build.0 = Release|Any CPU
{85DB25CA-79D3-44D9-AB25-A85053A05DFE}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{85DB25CA-79D3-44D9-AB25-A85053A05DFE}.Debug|Any CPU.Build.0 = Debug|Any CPU
{85DB25CA-79D3-44D9-AB25-A85053A05DFE}.Release|Any CPU.ActiveCfg = Release|Any CPU
{85DB25CA-79D3-44D9-AB25-A85053A05DFE}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {C355459D-834D-4C80-AD84-C5C181FE4CF7}
EndGlobalSection
EndGlobal
10 changes: 10 additions & 0 deletions tex2mathml/App.axaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<Application xmlns="https://github.com/avaloniaui"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
x:Class="tex2mathml.App"
RequestedThemeVariant="Default">
<!-- "Default" ThemeVariant follows system theme variant. "Dark" or "Light" are other available options. -->

<Application.Styles>
<FluentTheme />
</Application.Styles>
</Application>
36 changes: 36 additions & 0 deletions tex2mathml/App.axaml.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
using Avalonia;
using Avalonia.Controls.ApplicationLifetimes;
using Avalonia.Markup.Xaml;

using tex2mathml.ViewModels;
using tex2mathml.Views;

namespace tex2mathml;

public partial class App : Application
{
public override void Initialize()
{
AvaloniaXamlLoader.Load(this);
}

public override void OnFrameworkInitializationCompleted()
{
if (ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktop)
{
desktop.MainWindow = new MainWindow
{
DataContext = new MainViewModel()
};
}
else if (ApplicationLifetime is ISingleViewApplicationLifetime singleViewPlatform)
{
singleViewPlatform.MainView = new MainView
{
DataContext = new MainViewModel()
};
}

base.OnFrameworkInitializationCompleted();
}
}
Binary file added tex2mathml/Assets/avalonia-logo.ico
Binary file not shown.
6 changes: 6 additions & 0 deletions tex2mathml/ViewModels/MainViewModel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace tex2mathml.ViewModels;

public class MainViewModel : ViewModelBase
{
public string Greeting => "Welcome to Avalonia!";
}
7 changes: 7 additions & 0 deletions tex2mathml/ViewModels/ViewModelBase.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
using ReactiveUI;

namespace tex2mathml.ViewModels;

public class ViewModelBase : ReactiveObject
{
}
16 changes: 16 additions & 0 deletions tex2mathml/Views/MainView.axaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<UserControl xmlns="https://github.com/avaloniaui"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:vm="clr-namespace:tex2mathml.ViewModels"
mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450"
x:Class="tex2mathml.Views.MainView"
x:DataType="vm:MainViewModel">
<Design.DataContext>
<!-- This only sets the DataContext for the previewer in an IDE,
to set the actual DataContext for runtime, set the DataContext property in code (look at App.axaml.cs) -->
<vm:MainViewModel />
</Design.DataContext>

<TextBlock Text="{Binding Greeting}" HorizontalAlignment="Center" VerticalAlignment="Center"/>
</UserControl>
11 changes: 11 additions & 0 deletions tex2mathml/Views/MainView.axaml.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using Avalonia.Controls;

namespace tex2mathml.Views;

public partial class MainView : UserControl
{
public MainView()
{
InitializeComponent();
}
}
35 changes: 35 additions & 0 deletions tex2mathml/Views/MainWindow.axaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<Window xmlns="https://github.com/avaloniaui"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:math="clr-namespace:AvaloniaMath.Controls;assembly=AvaloniaMath"
xmlns:vm="using:tex2mathml.ViewModels"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:views="clr-namespace:tex2mathml.Views"
mc:Ignorable="d" d:DesignWidth="500" d:DesignHeight="230"
x:Class="tex2mathml.Views.MainWindow"
Icon="/Assets/avalonia-logo.ico"
Title="tex2mathml">
<Grid RowDefinitions="auto,auto,auto,auto,auto">

<TextBox Name="formula" Text="L = \int_a^b \sqrt[4]{ \left| \sum_{i,j=1}^ng_{ij}\left(\gamma(t)\right) \left[\frac{d}{dt}x^i\circ\gamma(t) \right] \left\{ \frac{d}{dt}x^j\circ\gamma(t) \right\} \right|}dt"/>
<Slider Grid.Row="1" Minimum="1" Maximum="100" Value="20" Name="zoom"/>

<ScrollViewer Grid.Row="2" HorizontalScrollBarVisibility="Auto">
<math:FormulaBlock Scale="{Binding #zoom.Value}" Formula="{Binding #formula.Text}" />
</ScrollViewer>

<Button x:Name="ConvertButton"
Content="Convert"
Padding="10,5"
Grid.Row="3"/>

<Grid ColumnDefinitions="*,auto" Grid.Row="6">
<TextBox Name="mathml" Text="hello!"/>
<Button x:Name="copyButton"
Content="copy"
Padding="10,5"
Grid.Row="0"
Grid.Column="1"/>
</Grid>
</Grid>
</Window>
106 changes: 106 additions & 0 deletions tex2mathml/Views/MainWindow.axaml.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
using Avalonia.Controls;
using Avalonia.Interactivity;
using System.Diagnostics;
using System.IO;
using System.Text;
using System.Xml.Xsl;
using System.Xml;
using System;

namespace tex2mathml.Views;

public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();

var ConvertButton = this.FindControl<Button>("ConvertButton");
ConvertButton.Click += ConvertButton_Click;

var copyButton = this.FindControl<Button>("copyButton");
copyButton.Click += CopyButton_Click;
}

private void CopyButton_Click(object? sender, RoutedEventArgs e)
{
Clipboard.SetTextAsync(mathml.Text);
}

public string GetWordDocumentAsMathML(string mathParagraphXml, string officeVersion = "14")
{
string officeML = string.Empty;

string testMathXml = @"<m:oMathPara xmlns:m=""http://schemas.openxmlformats.org/officeDocument/2006/math"">
</m:oMathPara>";
Console.WriteLine(mathParagraphXml);
XslCompiledTransform xslTransform = new XslCompiledTransform();

// The OMML2MML.xsl file is located under
// %ProgramFiles%\Microsoft Office\Office15\
xslTransform.Load(@"c:\Program Files\Microsoft Office\root\Office16" + @"\OMML2MML.XSL");

using (TextReader tr = new StringReader(mathParagraphXml))
{
// Load the xml of your main document part.
using (XmlReader reader = XmlReader.Create(tr))
{
using (MemoryStream ms = new MemoryStream())
{
XmlWriterSettings settings = xslTransform.OutputSettings.Clone();

// Configure xml writer to omit xml declaration.
settings.ConformanceLevel = ConformanceLevel.Fragment;
settings.OmitXmlDeclaration = true;

XmlWriter xw = XmlWriter.Create(ms, settings);

// Transform our OfficeMathML to MathML.
xslTransform.Transform(reader, xw);
ms.Seek(0, SeekOrigin.Begin);

using (StreamReader sr = new StreamReader(ms, Encoding.UTF8))
{
officeML = sr.ReadToEnd();
// Console.Out.WriteLine(officeML);
}
}
}
}
return officeML;
}

private string ReplaceFirstOccurrence(string Source, string Find, string Replace)
{
int Place = Source.IndexOf(Find);
string result = Source.Remove(Place, Find.Length).Insert(Place, Replace);
return result;
}

private void ConvertButton_Click(object? sender, RoutedEventArgs e)
{
Process p = new Process();
p.StartInfo.FileName = "./assets/texmath.exe";
p.StartInfo.Arguments = "-f tex -t omml";
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardInput = true;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.CreateNoWindow = true;
p.Start();
p.StandardInput.WriteLine($"{formula.Text}\n");
p.StandardInput.Flush();
p.StandardInput.Close();
string output = p.StandardOutput.ReadToEnd();
p.WaitForExit(); // This waits until the program called is closed

Debug.WriteLine("Output:");
Debug.WriteLine(output);

var result = ReplaceFirstOccurrence(output, @"<m:oMathPara>", @"<m:oMathPara xmlns:m=""http://schemas.openxmlformats.org/officeDocument/2006/math"">");

Debug.WriteLine("Convert Output:");
string final_result = (GetWordDocumentAsMathML(result));

mathml.Text = final_result;
}
}
29 changes: 29 additions & 0 deletions tex2mathml/tex2mathml.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<Nullable>enable</Nullable>
<LangVersion>latest</LangVersion>
</PropertyGroup>


<ItemGroup>
<AvaloniaResource Include="Assets\**" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="Avalonia" Version="$(AvaloniaVersion)" />
<PackageReference Include="Avalonia.Themes.Fluent" Version="$(AvaloniaVersion)" />
<PackageReference Include="Avalonia.Fonts.Inter" Version="$(AvaloniaVersion)" />
<PackageReference Include="Avalonia.ReactiveUI" Version="$(AvaloniaVersion)" />
<!--Condition below is needed to remove Avalonia.Diagnostics package from build output in Release configuration.-->
<PackageReference Condition="'$(Configuration)' == 'Debug'" Include="Avalonia.Diagnostics" Version="$(AvaloniaVersion)" />
<PackageReference Include="AvaloniaMath" Version="2.1.0" />
<PackageReference Include="DocumentFormat.OpenXml" Version="3.0.1" />
</ItemGroup>

<ItemGroup>
<AvaloniaResource Update="Assets\texmath.exe">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</AvaloniaResource>
</ItemGroup>
</Project>

0 comments on commit fcc24ec

Please sign in to comment.