summaryrefslogtreecommitdiff
path: root/mkvtool-gui/CustomFontManagerImpl.cs
diff options
context:
space:
mode:
authorb5f0d6c3 <[email protected]>2021-10-22 14:45:22 +0800
committerb5f0d6c3 <[email protected]>2021-10-22 14:45:22 +0800
commitcc993e05efef9fb1a797d686a18161fb906676d4 (patch)
tree6592579d71980d58fdc466fefcb3b9a7699c1783 /mkvtool-gui/CustomFontManagerImpl.cs
parent7fde5d4d448f64c2250ab003e2ecb75a7d268733 (diff)
add mkvtool-gui
Diffstat (limited to 'mkvtool-gui/CustomFontManagerImpl.cs')
-rw-r--r--mkvtool-gui/CustomFontManagerImpl.cs73
1 files changed, 73 insertions, 0 deletions
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<string> 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