将当前平台字体传递到SKTypeface?

Dan*_*ieu 7 c# fonts xamarin.forms skiasharp

尝试渲染中文(或其他符号)文本时.SkiSharp将渲染框而不是正确的中文字符.显然,Skia默认使用的字体不支持这些字符.所以我们必须使用支持这些字符的字体来分配我们自己的SKTypeface.

我最初的策略是简单地包含必要的字体来渲染这些字符,这些字符工作正常.但是,当使用自己的字体支持多种不同的符号语言时,应用程序的大小会急剧增加(每个字体大约15 MB).

所以考虑一下这个...默认平台字体似乎支持这些符号字符中的任何一个就好了.我的意思是,默认情况下使用的字体完美呈现按钮,标签和标题.

所以我目前的想法是,为什么我不能只是通过,任何字体进入我的控制SKTypeface?

问题是,我不知道如何获得任何后备或默认字体,以便用它创建一个新的SKTypeface.

我的问题

如何使用渲染这些按钮,标签和标题的相同字体创建SKTypeface?


注意:如果你需要任何东西,从我来帮助你理解问题或解决问题只是让我知道.

Yur*_*rii 8

您应该能够使用SKFontManager.MatchCharacter或其中一个重载,以便:

使用系统回退查找给定字符的字体.

以下是基于SkiaSharp WindowsSample的TextSample的示例:

public class TextSample : SampleBase
{
    // ...
    protected override void OnDrawSample(SKCanvas canvas, int width, int height)
    {
        canvas.DrawColor(SKColors.White);
        const string text = "???";
        var x = width / 2f;
        // in my case the line below returns typeface with FamilyName `Yu Gothic UI`
        var typeface = SKFontManager.Default.MatchCharacter(text[0]);
        using (var paint = new SKPaint())
        {
            paint.TextSize = 64.0f;
            paint.IsAntialias = true;
            paint.Color = (SKColor)0xFF4281A4;
            paint.IsStroke = false;
            paint.Typeface = typeface; // use typeface for the first one
            paint.TextAlign = SKTextAlign.Center;

            canvas.DrawText(text, x, 64.0f, paint);
        }

        using (var paint = new SKPaint())
        {
            // no typeface here
            paint.TextSize = 64.0f;
            paint.IsAntialias = true;
            paint.Color = (SKColor)0xFF9CAFB7;
            paint.IsStroke = true;
            paint.StrokeWidth = 3;
            paint.TextAlign = SKTextAlign.Center;

            canvas.DrawText(text, x, 144.0f, paint);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

以下是输出

示例输出

  • 你不知道这节省了多少工作.谢谢! (3认同)