fonts – 在WinRT中计算字体基线

我想在WinRT中找到
baseline of a font.

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

最佳答案 不幸的是,你正在寻找的是FormattedText [MSDN:
1
2],它存在于WPF中并且在WinRT中不存在(我甚至认为它甚至还没有在Silverlight中存在).

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

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

如果您想尝试,这里有几个跳跃点:

> these guys seem to actually be using DirectWrite in a WinRT app
> this is a wrapper for C++ making DX available, DirectWrite would be
much of the same

希望这会有所帮助,祝你好运

更新

我想到了这一点,并记得TextBlocks有经常被遗忘的属性BaselineOffset,它为你提供了从所选类型面的框顶部开始的基线下降!所以你可以使用相同的hack来替换MeasureString来替换丢失的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;
    }

我用它来做到这一点:

完善!对?希望这有助于确定

点赞