Passing the current platform font to SKTypeface?

When trying to display Chinese (or other symbolic) text. SkiSharp will display boxes instead of the correct Chinese characters. Obviously, the font that Skia uses by default does not support these characters. Therefore, we must assign our own SKTypeface using a font that supports these characters.

My initial strategy was to simply include the necessary fonts to render these characters that worked perfectly. However, with the support of several different symbolic languages ​​with their own fonts, the size of the application increases dramatically (about 15 mb per font).

So, think about this a little more ... The default platform fonts seem to support any of these symbolic characters very well . I mean, the default font displays buttons, shortcuts, and headers nicely.

So, my current thought is: why can't I just pass whatever font into SKTypeface for my control?

The problem is that I do not know how to get what this back or standard font is to create a new SKTypeface with it.

My question

How can I create a SKTypeface from the same font that perfectly displays these buttons, shortcuts and headers?


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

+4
1

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);
        }
    }
}

Example output

+5

Source: https://habr.com/ru/post/1691597/


All Articles