-
Notifications
You must be signed in to change notification settings - Fork 105
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Add some symbols it contains - Add an example in WpfMath.Example - Add TTFMetrics tool - Improve WpfMath.Example editor
- Loading branch information
Showing
15 changed files
with
656 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" /> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
namespace TTFMetrics; | ||
|
||
public partial class App | ||
{ | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
)] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.