刘恋 發表於 2022-3-9 10:52:00

Avalonia 在银河麒麟系统系统字体图标加载异常问题处理

<div class="cnblogs_Highlighter">
<pre class="brush:csharp;gutter:false;">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#微软雅黑");

      <span style="background-color: rgba(255, 0, 0, 1)">private readonly Typeface _emojiTypeface = new Typeface("resm:AvaloniaDemo.Assets.Fonts.FluentAvalonia#Symbols");</span>

      public CustomFontManagerImpl()
      {
            _customTypefaces = new[] { _defaultTypeface, _emojiTypeface };
            _defaultFamilyName = _defaultTypeface.FontFamily.FamilyNames.PrimaryFamilyName;
      }

      public string GetDefaultFontFamilyName()
      {
            return _defaultFamilyName;
      }

      public IEnumerable&lt;string&gt; GetInstalledFontFamilyNames(bool checkForUpdates = false)
      {
            return _customTypefaces.Select(x =&gt; 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;
<span style="background-color: rgba(255, 0, 0, 1)">                case "Symbols": //解决Linux系统加载不了字体图标的异常
                  skTypeface = SKTypeface.FromFamilyName(_emojiTypeface.FontFamily.Name); break;
</span>                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);
      }
    }
}
</pre>
</div>
<p>&nbsp;PS:参考标红部分代码</p><br><br>
来源:https://www.cnblogs.com/-wxh/p/15984071.html
頁: [1]
查看完整版本: Avalonia 在银河麒麟系统系统字体图标加载异常问题处理