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

Passing the current platform font into SKTypeface?

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

我最初的策略是简单地包含必要的字体来呈现这些字符,效果很好。但是,当使用自己的字体支持多种不同的符号语言时,应用程序的大小会急剧增加(每种字体大约 15 mb)。

所以再考虑一下...默认平台字体似乎支持 any 这些符号字符就好了。我的意思是,default 使用的字体完美呈现按钮、标签和标题。

所以我目前的想法是,为什么我不能直接通过,无论是什么字体都进入 SKTypeface 供我控制?

问题是我不知道如何获取 fall-back 或默认字体,以便用它创建新的 SKTypeface。

我的问题

我如何使用可以很好地呈现这些按钮、标签和标题的相同字体创建 SKTypeface?


note: if you need anything from me to help you understand the problem or solve the problem just let me know.

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

Use the system fallback to find a typeface for the given character.

下面是一个基于SkiaSharp WindowsSample's 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);
        }
    }
}

下面是输出