From cc993e05efef9fb1a797d686a18161fb906676d4 Mon Sep 17 00:00:00 2001 From: b5f0d6c3 Date: Fri, 22 Oct 2021 14:45:22 +0800 Subject: add mkvtool-gui --- mkvtool-gui/App.axaml | 11 + mkvtool-gui/App.axaml.cs | 35 +++ mkvtool-gui/CustomFontManagerImpl.cs | 73 ++++++ mkvtool-gui/MainWindow.axaml | 218 ++++++++++++++++++ mkvtool-gui/MainWindow.axaml.cs | 428 +++++++++++++++++++++++++++++++++++ mkvtool-gui/Program.cs | 23 ++ mkvtool-gui/mkvtool.csproj | 28 +++ mkvtool-gui/sdk.cs | 135 +++++++++++ 8 files changed, 951 insertions(+) create mode 100644 mkvtool-gui/App.axaml create mode 100644 mkvtool-gui/App.axaml.cs create mode 100644 mkvtool-gui/CustomFontManagerImpl.cs create mode 100644 mkvtool-gui/MainWindow.axaml create mode 100644 mkvtool-gui/MainWindow.axaml.cs create mode 100644 mkvtool-gui/Program.cs create mode 100644 mkvtool-gui/mkvtool.csproj create mode 100644 mkvtool-gui/sdk.cs diff --git a/mkvtool-gui/App.axaml b/mkvtool-gui/App.axaml new file mode 100644 index 0000000..9871dd1 --- /dev/null +++ b/mkvtool-gui/App.axaml @@ -0,0 +1,11 @@ + + + + + + + + + \ No newline at end of file diff --git a/mkvtool-gui/App.axaml.cs b/mkvtool-gui/App.axaml.cs new file mode 100644 index 0000000..44e12cf --- /dev/null +++ b/mkvtool-gui/App.axaml.cs @@ -0,0 +1,35 @@ +using Avalonia; +using Avalonia.Controls.ApplicationLifetimes; +using Avalonia.Markup.Xaml; +using Avalonia.Platform; + +namespace mkvtool +{ + public class App : Application + { + public override void Initialize() + { + AvaloniaXamlLoader.Load(this); + } + + public override void OnFrameworkInitializationCompleted() + { + if (ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktop) + { + desktop.MainWindow = new MainWindow(); + } + + base.OnFrameworkInitializationCompleted(); + } + + /// + /// override RegisterServices register custom service + /// + public override void RegisterServices() + { + AvaloniaLocator.CurrentMutable.Bind().ToConstant(new CustomFontManagerImpl()); + base.RegisterServices(); + } + + } +} \ No newline at end of file diff --git a/mkvtool-gui/CustomFontManagerImpl.cs b/mkvtool-gui/CustomFontManagerImpl.cs new file mode 100644 index 0000000..c47fedb --- /dev/null +++ b/mkvtool-gui/CustomFontManagerImpl.cs @@ -0,0 +1,73 @@ +using System.Collections.Generic; +using System.Globalization; +using System.Linq; +using Avalonia.Media; +using Avalonia.Platform; +using Avalonia.Skia; +using SkiaSharp; + +namespace mkvtool +{ + public class CustomFontManagerImpl : IFontManagerImpl + { + private readonly Typeface[] _customTypefaces; + private readonly string _defaultFamilyName; + + private readonly Typeface _defaultTypeface = + new Typeface("Microsoft Yahei"); + + public CustomFontManagerImpl() + { + _customTypefaces = new[] {_defaultTypeface}; + _defaultFamilyName = _defaultTypeface.FontFamily.FamilyNames.PrimaryFamilyName; + } + + public string GetDefaultFontFamilyName() + { + return _defaultFamilyName; + } + + public IEnumerable GetInstalledFontFamilyNames(bool checkForUpdates = false) + { + return _customTypefaces.Select(x => x.FontFamily.Name); + } + + private readonly string[] _bcp47 = + { + CultureInfo.CurrentCulture.ThreeLetterISOLanguageName, CultureInfo.CurrentCulture.TwoLetterISOLanguageName + }; + + public bool TryMatchCharacter(int codepoint, FontStyle fontStyle, FontWeight fontWeight, FontFamily fontFamily, + CultureInfo culture, out Typeface typeface) + { + foreach (var customTypeface in _customTypefaces) + { + if (customTypeface.GlyphTypeface.GetGlyph((uint) codepoint) == 0) + { + continue; + } + + typeface = new Typeface(customTypeface.FontFamily.Name, fontStyle, fontWeight); + + return true; + } + + var fallback = SKFontManager.Default.MatchCharacter(fontFamily?.Name, (SKFontStyleWeight) fontWeight, + SKFontStyleWidth.Normal, (SKFontStyleSlant) fontStyle, _bcp47, codepoint); + + typeface = new Typeface(fallback?.FamilyName ?? _defaultFamilyName, fontStyle, fontWeight); + + return true; + } + + public IGlyphTypefaceImpl CreateGlyphTypeface(Typeface typeface) + { + SKTypeface skTypeface; + skTypeface = SKTypeface.FromFamilyName(_defaultTypeface.FontFamily.Name, + (SKFontStyleWeight) typeface.Weight, SKFontStyleWidth.Normal, (SKFontStyleSlant) typeface.Style); + + + return new GlyphTypefaceImpl(skTypeface); + } + } +} \ No newline at end of file diff --git a/mkvtool-gui/MainWindow.axaml b/mkvtool-gui/MainWindow.axaml new file mode 100644 index 0000000..014010e --- /dev/null +++ b/mkvtool-gui/MainWindow.axaml @@ -0,0 +1,218 @@ + + + + + + + +