Avalonia 在银河麒麟系统系统字体图标加载异常问题处理
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Runtime.InteropServices;
using Avalonia.Media;
using Avalonia.Media.Fonts;
using Avalonia.Platform;
using Avalonia.Skia;
using SkiaSharp;
namespace LinuxShelfUI
{
public class CustomFontManagerImpl : IFontManagerImpl
{
private readonly Typeface[] _customTypefaces;
private readonly string _defaultFamilyName;
//Load font resources in the project, you can load multiple font resources
private readonly Typeface _defaultTypeface =new Typeface("resm:AvaloniaDemo.Assets.Fonts.msyh#微软雅黑");
private readonly Typeface _emojiTypeface = new Typeface("resm:AvaloniaDemo.Assets.Fonts.FluentAvalonia#Symbols");
public CustomFontManagerImpl()
{
_customTypefaces = new[] { _defaultTypeface, _emojiTypeface };
_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;
switch (typeface.FontFamily.Name)
{
case FontFamily.DefaultFontFamilyName:
case "微软雅黑": //font family name
skTypeface = SKTypeface.FromFamilyName(_defaultTypeface.FontFamily.Name); break;
case "Symbols": //解决Linux系统加载不了字体图标的异常
skTypeface = SKTypeface.FromFamilyName(_emojiTypeface.FontFamily.Name); break;
default:
skTypeface = SKTypeface.FromFamilyName(typeface.FontFamily.Name,
(SKFontStyleWeight)typeface.Weight, SKFontStyleWidth.Normal, (SKFontStyleSlant)typeface.Style);
break;
}
//解决linux系统下skTypeface是null
if (skTypeface == null)
{
skTypeface = SKTypeface.FromFamilyName(_defaultTypeface.FontFamily.Name);
}
//如果是centos7之类的使用linux里面的字体
if (skTypeface == null)
{
skTypeface = SKTypeface.FromFamilyName("WenQuanYi Micro Hei");
}
return new GlyphTypefaceImpl(skTypeface);
}
}
}
PS:参考标红部分代码

浙公网安备 33010602011771号