Skip to content

Commit

Permalink
Add jlm_msam10.ttf support
Browse files Browse the repository at this point in the history
 - Add some symbols it contains
 - Add an example in WpfMath.Example
 - Add TTFMetrics tool
 - Improve WpfMath.Example editor
  • Loading branch information
Orace committed May 10, 2023
1 parent 112bb2e commit adda323
Show file tree
Hide file tree
Showing 15 changed files with 656 additions and 3 deletions.
9 changes: 9 additions & 0 deletions XamlMath.All.sln
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,10 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "api", "api", "{3F2FED19-93A
api\XamlMath.Shared.net6.0.verified.cs = api\XamlMath.Shared.net6.0.verified.cs
EndProjectSection
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Tools", "Tools", "{78661BBB-288F-4AB5-8574-C8AA1B5BAC52}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TTFMetrics", "src\Tools\TTFMetrics\TTFMetrics.csproj", "{A29B12C8-9AE6-4016-9AB8-702B67409C95}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -124,12 +128,17 @@ Global
{54F77EF7-F918-46F8-A615-56BC0BFE8AAB}.Debug|Any CPU.Build.0 = Debug|Any CPU
{54F77EF7-F918-46F8-A615-56BC0BFE8AAB}.Release|Any CPU.ActiveCfg = Release|Any CPU
{54F77EF7-F918-46F8-A615-56BC0BFE8AAB}.Release|Any CPU.Build.0 = Release|Any CPU
{A29B12C8-9AE6-4016-9AB8-702B67409C95}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{A29B12C8-9AE6-4016-9AB8-702B67409C95}.Debug|Any CPU.Build.0 = Debug|Any CPU
{A29B12C8-9AE6-4016-9AB8-702B67409C95}.Release|Any CPU.ActiveCfg = Release|Any CPU
{A29B12C8-9AE6-4016-9AB8-702B67409C95}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(NestedProjects) = preSolution
{B296A539-C985-43AB-B740-7FBE8F85CCFF} = {422A1C06-160C-4422-B019-F9B63C6C6877}
{A29B12C8-9AE6-4016-9AB8-702B67409C95} = {78661BBB-288F-4AB5-8574-C8AA1B5BAC52}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {6563FDA0-9C49-4091-AAE3-611169C996D8}
Expand Down
Binary file added fonts/jlm_msam10.ttf
Binary file not shown.
Binary file added src/AvaloniaMath/Fonts/jlm_msam10.ttf
Binary file not shown.
4 changes: 4 additions & 0 deletions src/Tools/TTFMetrics/App.xaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<Application x:Class="TTFMetrics.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
StartupUri="MainWindow.xaml" />
5 changes: 5 additions & 0 deletions src/Tools/TTFMetrics/App.xaml.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
namespace TTFMetrics;

public partial class App
{
}
10 changes: 10 additions & 0 deletions src/Tools/TTFMetrics/AssemblyInfo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using System.Windows;

[assembly: ThemeInfo(
ResourceDictionaryLocation.None, //where theme specific resource dictionaries are located
//(used if a resource is not found in the page,
// or application resource dictionaries)
ResourceDictionaryLocation.SourceAssembly //where the generic resource dictionary is located
//(used if a resource is not found in the page,
// app, or any theme specific resource dictionaries)
)]
108 changes: 108 additions & 0 deletions src/Tools/TTFMetrics/MainViewModel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using System.Runtime.CompilerServices;
using System.Text;
using System.Windows.Input;
using System.Windows.Media;
using Microsoft.Win32;
using Microsoft.Xaml.Behaviors.Core;

namespace TTFMetrics;

public sealed class MainViewModel : INotifyPropertyChanged
{
private string _content = string.Empty;
private string _filePath = string.Empty;

public MainViewModel()
{
SelectFileCommand = new ActionCommand(SelectFile);
}

public string Content
{
get => _content;
private set => SetField(ref _content, value ?? string.Empty);
}

public string FilePath
{
get => _filePath;
private set
{
if (SetField(ref _filePath, value ?? string.Empty))
Content = BuildContent(_filePath);
}
}

public ICommand SelectFileCommand { get; }

public event PropertyChangedEventHandler? PropertyChanged;

private static string BuildContent(string filePath)
{
if (string.IsNullOrWhiteSpace(filePath) || !File.Exists(filePath))
return string.Empty;

try
{
var uri = new Uri(filePath);

var gtf = new GlyphTypeface(uri);

var sb = new StringBuilder();

var xHeight = gtf.XHeight;

sb.AppendLine($@"xHeight=""{xHeight}""");

foreach (var (code, index) in gtf.CharacterToGlyphMap)
{
if (code > 255)
continue;

var geometry = gtf.GetGlyphOutline(index, 1, 0);
var depth = gtf.DistancesFromHorizontalBaselineToBlackBoxBottom[index];
var height = geometry.Bounds.Height - depth;
var width = gtf.AdvanceWidths[index];

if (!IsValid(depth) || !IsValid(height) || !IsValid(width))
continue;

sb.Append(@$"<Char code=""{code}"" width=""{width:F3}"" height=""{height:F3}"" ");
if (depth != 0)
sb.Append(@$"depth=""{depth:F3}"" ");

sb.AppendLine("/>");
}

return sb.ToString();
}
catch (Exception e)
{
return $"Error: {e.Message}";
}
}

private static bool IsValid(double d) => !double.IsInfinity(d) && !double.IsNaN(d);

private void SelectFile()
{
var ofd = new OpenFileDialog { Filter = "TTF file (*.tff)|*.ttf|All files (*.*)|*.*" };

if (ofd.ShowDialog() is true)
FilePath = ofd.FileName;
}

private bool SetField<T>(ref T field, T value, [CallerMemberName] string? propertyName = null)
{
if (EqualityComparer<T>.Default.Equals(field, value))
return false;

field = value;
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
return true;
}
}
50 changes: 50 additions & 0 deletions src/Tools/TTFMetrics/MainWindow.xaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<Window x:Class="TTFMetrics.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
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:local="clr-namespace:TTFMetrics"
mc:Ignorable="d"
Title="TTFMetrics" Height="450" Width="800">

<Window.DataContext>
<local:MainViewModel />
</Window.DataContext>

<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>

<Button Grid.Column="0"
Grid.Row="0"
Command="{Binding SelectFileCommand, Mode=OneTime}"
Content="Select file..."
Margin="3"
Padding="4 2"/>

<TextBox Grid.Column="1"
Grid.Row="0"
IsReadOnly="True"
Margin="3"
Text="{Binding FilePath, Mode=OneWay}"
VerticalAlignment="Stretch"
VerticalContentAlignment="Center" />

<TextBox Grid.Column="0"
Grid.ColumnSpan="2"
Grid.Row="1"
AcceptsReturn="True"
HorizontalAlignment="Stretch"
IsReadOnly="True"
Margin="5"
ScrollViewer.VerticalScrollBarVisibility="Auto"
Text="{Binding Content, Mode=OneWay}"
VerticalAlignment="Stretch" />
</Grid>
</Window>
13 changes: 13 additions & 0 deletions src/Tools/TTFMetrics/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using System.Globalization;

namespace TTFMetrics;

public partial class MainWindow
{
public MainWindow()
{
CultureInfo.CurrentCulture = CultureInfo.InvariantCulture;

InitializeComponent();
}
}
14 changes: 14 additions & 0 deletions src/Tools/TTFMetrics/TTFMetrics.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>WinExe</OutputType>
<TargetFramework>net7.0-windows</TargetFramework>
<Nullable>enable</Nullable>
<UseWPF>true</UseWPF>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Xaml.Behaviors.Wpf" Version="1.1.39" />
</ItemGroup>

</Project>
37 changes: 36 additions & 1 deletion src/WpfMath.Example/MainViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,42 @@ public MainViewModel()
new ("Cases", @"f(x) = \cases{1/3 & \mathrm{if} \thinspace 0\le x\le 1;\cr 2/3 & \mathrm{if} \thinspace 3\le x \le 4; \cr 0 & \mathrm{elsewhere.}\cr}"),
new ("Matrix and new lines", @"v \times w = \left( \matrix{v_2 w_3 - v_3 w_2 \\ v_3 w_1 - v_1 w_3 \\ v_1 w_2 - v_2 w_1} \right) \\ \matrix{\mathrm{where} & v= \left(\matrix{ v_1 \\ v_2 \\ v_3 }\right), \\ & w= \left( \matrix{w_1 \\ w_2 \\ w_3} \right)}"),
new ("Big matrix", @"\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 }}"),
new ("Environment with Matrix", @"\begin{pmatrix} a_1 & a_2 & a_3 \\ b_1 & b_2 & b_3 \\ c_1 & c_2 & c_3 \end{pmatrix}")
new ("Environment with Matrix", @"\begin{pmatrix} a_1 & a_2 & a_3 \\ b_1 & b_2 & b_3 \\ c_1 & c_2 & c_3 \end{pmatrix}"),
new ("Font jlm_msam10", @"\matrix{
\rightleftharpoons & \angle & \sqsubset & \sqsupset
& \Box & \Diamond & \leadsto & \lhd
& \unlhd & \rhd & \unrhd & \boxdot
& \boxplus & \boxtimes & \square & \blacksquare
\\ \centerdot & \lozenge & \blacklozenge & \circlearrowright
& \circlearrowleft & \leftrightharpoons & \boxminus & \Vdash
& \Vvdash & \vDash & \twoheadrightarrow & \twoheadleftarrow
& \leftleftarrows & \rightrightarrows & \upuparrows & \downdownarrows
\\ \upharpoonright & \downharpoonright & \upharpoonleft & \downharpoonleft
& \rightarrowtail & \leftarrowtail & \leftrightarrows & \rightleftarrows
& \Lsh & \Rsh & \rightsquigarrow & \leftrightsquigarrow
& \looparrowleft & \looparrowright & \circeq & \succsim
\\ \gtrsim & \gtrapprox & \multimap & \therefore
& \because & \doteqdot & \triangleq & \precsim
& \lesssim & \lessapprox & \eqslantless & \eqslantgtr
& \curlyeqprec & \curlyeqsucc & \preccurlyeq & \leqq
\\ \leqslant & \lessgtr & \backprime & \risingdotseq
& \fallingdotseq & \succcurlyeq & \geqq & \geqslant
& \gtrless & \vartriangleright & \vartriangleleft & \trianglerighteq
& \trianglelefteq & \bigstar & \between & \blacktriangledown
\\ \blacktriangleright & \blacktriangleleft & \vartriangle & \blacktriangle
& \triangledown & \eqcirc & \lesseqgtr & \gtreqless
& \lesseqqgtr & \gtreqqless & \yen & \Rrightarrow
& \Lleftarrow & \checkmark & \veebar & \barwedge
\\ \doublebarwedge & \measuredangle & \sphericalangle & \varpropto
& \smallsmile & \smallfrown & \Subset & \Supset
& \Cup & \Cap & \curlywedge & \curlyvee
& \leftthreetimes & \rightthreetimes & \subseteqq & \supseteqq
\\ \bumpeq & \Bumpeq & \lll & \ggg
& \ulcorner & \urcorner & \textregistered & \circledS
& \pitchfork & \dotplus & \backsim & \backsimeq
& \llcorner & \lrcorner & \maltese & \complement
\\ \intercal & \circledcirc & \circledast & \circleddash
}")
};

_formula = Presets[0].Formula;
Expand Down
5 changes: 4 additions & 1 deletion src/WpfMath.Example/MainWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,12 @@
<TextBox Name="InputTextBox"
AcceptsReturn="True"
BorderThickness="0"
FontFamily="Consolas"
HorizontalScrollBarVisibility="Auto"
Margin="3"
SelectionChanged="OnSelectionChanged"
Text="{Binding Formula, UpdateSourceTrigger=PropertyChanged}" />
Text="{Binding Formula, UpdateSourceTrigger=PropertyChanged}"
VerticalScrollBarVisibility="Auto" />
</GroupBox>

<Label Grid.Column="1"
Expand Down
Loading

0 comments on commit adda323

Please sign in to comment.