在WinRT中计算字体基线

时间:2013-04-07 12:54:22

标签: fonts windows-runtime baseline

我想在WinRT中找到baseline of a font

我还通过creating a dummy TextBlock计算了特定字体的文字大小,但我不确定如何计算基线。这在WinRT中甚至可能吗?

1 个答案:

答案 0 :(得分:2)

不幸的是,你要找的是FormattedText [MSDN:1 2],它存在于WPF中并且在WinRT中不存在(我甚至不认为它是偶数在银光中)。

它可能会包含在未来的版本中,因为它似乎是一个非常受欢迎的功能,非常错过,团队意识到它的遗漏。见这里:http://social.msdn.microsoft.com

如果您感兴趣或真的,真的需要一种方法来衡量类型面的细节,您可以尝试编写 DirectWrite 的包装器,据我所知,这是WinRT可用的技术内部堆栈,但它只能通过C ++访问

如果你想尝试的话,这里有几个跳跃点:

希望这有帮助,祝你好运 -

<强> 更新

我想到了这一点,并记住TextBlock有遗忘的属性BaselineOffset,它为您提供了从所选类型面的框顶部开始的基线下降!因此,您可以使用每个人用来替换MeasureString的相同hack来替换FormattedText的丢失。这里酱:

    private double GetBaselineOffset(double size, FontFamily family = null, FontWeight? weight = null, FontStyle? style = null, FontStretch? stretch = null)
    {
        var temp = new TextBlock();
        temp.FontSize = size;
        temp.FontFamily = family ?? temp.FontFamily;
        temp.FontStretch = stretch ?? temp.FontStretch;
        temp.FontStyle = style ?? temp.FontStyle;
        temp.FontWeight = weight ?? temp.FontWeight;

        var _size = new Size(10000, 10000);
        var location = new Point(0, 0);

        temp.Measure(_size);
        temp.Arrange(new Rect(location, _size));

        return temp.BaselineOffset;
    }

我用它来做到这一点: screenshot

完美!对?希望这有助于确认