-
Notifications
You must be signed in to change notification settings - Fork 5
/
CustomFontManagerImpl.cs
144 lines (115 loc) · 4.5 KB
/
CustomFontManagerImpl.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
using Avalonia.Media.Fonts;
using Avalonia.Media;
using Avalonia.Platform;
using SkiaSharp;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using OpenHarmony.Sdk.Native;
using System.Runtime.Loader;
using System.Reflection;
using System.Runtime.Serialization;
using System.Runtime.CompilerServices;
namespace Avalonia.OpenHarmony;
public class CustomFontManagerImpl : IFontManagerImpl
{
private readonly string _defaultFamilyName;
private readonly IFontCollection _customFonts;
private bool _isInitialized;
public CustomFontManagerImpl()
{
_defaultFamilyName = "HarmonyOS Sans";
var source = new Uri("resm:Avalonia.OpenHarmony.Assets?assembly=Avalonia.OpenHarmony");
_customFonts = new EmbeddedFontCollection(source, source);
}
public string GetDefaultFontFamilyName()
{
return _defaultFamilyName;
}
public string[] GetInstalledFontFamilyNames(bool checkForUpdates = false)
{
if (!_isInitialized)
{
_customFonts.Initialize(this);
_isInitialized = true;
}
return _customFonts.Select(x => x.Name).ToArray();
}
private readonly string[] _bcp47 = { CultureInfo.CurrentCulture.ThreeLetterISOLanguageName, CultureInfo.CurrentCulture.TwoLetterISOLanguageName };
public bool TryMatchCharacter(int codepoint, FontStyle fontStyle, FontWeight fontWeight, FontStretch fontStretch,
CultureInfo culture, out Typeface typeface)
{
if (!_isInitialized)
{
_customFonts.Initialize(this);
}
if (_customFonts.TryMatchCharacter(codepoint, fontStyle, fontWeight, fontStretch, null, culture, out typeface))
{
return true;
}
var fallback = SKFontManager.Default.MatchCharacter(null, (SKFontStyleWeight)fontWeight,
(SKFontStyleWidth)fontStretch, (SKFontStyleSlant)fontStyle, _bcp47, codepoint);
typeface = new Typeface(fallback?.FamilyName ?? _defaultFamilyName, fontStyle, fontWeight);
return true;
}
public bool TryCreateGlyphTypeface(string familyName, FontStyle style, FontWeight weight,
FontStretch stretch, [NotNullWhen(true)] out IGlyphTypeface glyphTypeface)
{
if (!_isInitialized)
{
_customFonts.Initialize(this);
}
if (familyName == "$Default")
familyName = _defaultFamilyName;
if (_customFonts.TryGetGlyphTypeface(familyName, style, weight, stretch, out glyphTypeface))
{
return true;
}
var assetLoader = AvaloniaLocator.Current.GetRequiredService<IAssetLoader>();
var skTypeface = SKTypeface.FromFamilyName(familyName,
(SKFontStyleWeight)weight, SKFontStyleWidth.Normal, (SKFontStyleSlant)style);
glyphTypeface = GetGlyphTypeface(skTypeface, FontSimulations.None);
return true;
}
public bool TryCreateGlyphTypeface(Stream stream, FontSimulations fontSimulations, [NotNullWhen(true)] out IGlyphTypeface glyphTypeface)
{
var skTypeface = SKTypeface.FromStream(stream);
glyphTypeface = GetGlyphTypeface(skTypeface, fontSimulations);
return true;
}
public IGlyphTypeface GetGlyphTypeface(SKTypeface skTypeface, FontSimulations fontSimulations)
{
Type? type = null;
Assembly? currentAssembly = null;
foreach (var alc in AssemblyLoadContext.All)
{
foreach (var assembly in alc.Assemblies)
{
type = assembly.GetType("Avalonia.Skia.GlyphTypefaceImpl");
currentAssembly = assembly;
if (type != null)
break;
}
if (type != null)
break;
}
try
{
var ctor = type.GetConstructor([typeof(SKTypeface), typeof(FontSimulations)]);
object obj = RuntimeHelpers.GetUninitializedObject(type);
ctor.Invoke(obj, [skTypeface, fontSimulations]);
return (IGlyphTypeface)obj;
}
catch (Exception ex)
{
Hilog.OH_LOG_ERROR(LogType.LOG_APP, "CSharp", ex.Message);
Hilog.OH_LOG_ERROR(LogType.LOG_APP, "CSharp", ex.StackTrace);
if (ex.InnerException != null)
{
Hilog.OH_LOG_ERROR(LogType.LOG_APP, "CSharp", ex.InnerException.Message);
Hilog.OH_LOG_ERROR(LogType.LOG_APP, "CSharp", ex.InnerException.StackTrace);
}
throw ex;
}
return null;
}
}