Get typeface from Control properties?

36 views Asked by At

I am doing some custom text rendering using the DrawText method of DrawingContext, which takes a FormattedText object.

Instead of passing in a new typeface e.g new Typeface("Verdana"), how can I compute the current typeface used by the controls FontFamily?

public class Example : Control
{
    protected override void OnRender(DrawingContext drawingContext)
    {
        base.OnRender(drawingContext);

        double fontSize = FontSize;
        Brush brush = Foreground;
        CultureInfo cultureInfo = Thread.CurrentThread.CurrentCulture;
        Typeface typeface = ???;

        // I believe its based on these 3 properties?
        this.FontFamily;
        this.FontWeight;
        this.FontStyle;

        FormattedText formattedText = new FormattedText("Some text", cultureInfo,
            FlowDirection.LeftToRight, new Typeface("Verdana"), fontSize, brush,
            VisualTreeHelper.GetDpi(this).PixelsPerDip);

        drawingContext.DrawText(formattedText, new Point(0, 0));
    }
}

EDIT :

Looks like I can use the following.

 var typeface = FontFamily.GetTypefaces().Where(e => e.Weight == FontWeight && e.Stretch == FontStretch)
                .FirstOrDefault();
0

There are 0 answers