Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing the current platform font into SKTypeface?

When attempting to render Chinese (or other symbolic) text. SkiSharp will render boxes instead of the correct Chinese characters. Obviously the font that Skia is using by default doesn't support those characters. So we have to assign our own SKTypeface using a font that does support those characters.

My initial strategy was to simply include the necessary fonts to render those characters, which worked fine. However when supporting multiple different symbolic languages with their own fonts, the application size grows dramatically (about 15 mb per font).

So thinking about this a bit more... The default platform fonts seems to support any of these symbolic characters just fine. What I mean by this, is that the font that is used by default renders Buttons, labels and titles perfectly.

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

The problem is that I don't know how to get ahold of whatever that fall-back or default font is in order to create a new SKTypeface with it.

My Question

How can I create an SKTypeface from the same font that is rendering these buttons, labels and titles just fine?


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

like image 525
Dan Beaulieu Avatar asked Jan 01 '18 22:01

Dan Beaulieu


1 Answers

You should be able to use SKFontManager.MatchCharacter or one of its overloads in order to:

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

Below is an example based on 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);
        }
    }
}

And below is the output

Example output

like image 62
Yurii Avatar answered Oct 07 '22 09:10

Yurii