如何在WPF中找到任意字形的确切大小? [英] How to find exact size for an arbitrary glyph in WPF?

查看:122
本文介绍了如何在WPF中找到任意字形的确切大小?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要找到任意字体中每个字符的确切大小.

I need to find exact size for each character in an arbitrary font.

使用 GlyphTypeface 加载字体课.

问题是,我只能使用AdvanceWidths属性直接访问字形的宽度,但是每个字符的高度都相同,并且将其设置为

The problem is that I can access directly only width of a glyph using the AdvanceWidths property, but the height is the same for each characters and it is set to Height property.

似乎可以使用Baseline,Height,XHeight,BottomSideBearings和TopSideBearings属性的组合来计算单个字符的高度,但是没有有关所有这些值的真实含义的文档.对于其他两个字体,BottomSideBearings和TopSideBearings,整个字体的Baseline,Height和XHeight值是恒定的.我找不到它们的含义.

It seems that individual character height can be computed using a combination of Baseline, Height, XHeight, BottomSideBearings and TopSideBearings properties, but there is no documentation about real meaning of all these values. Baseline, Height and XHeight values are constant for the entire font, regarding other two - BottomSideBearings and TopSideBearings - I can't find their meaning.

通常来说,是否有任何信息可以从任意字体计算出单个字形的大小? (仅包含"TTF"文件,也可以从其他文件中加载GlyphTypeface).

Generally speaking, is there any information how can be computed the size of an individual glyph from an arbitrary font? (having just the 'TTF' file, or anything else GlyphTypeface can be loaded from).

推荐答案

经过研究,找到了计算字符大小的正确方法:

After some research, found the right way to compute character size:

var typeface = new GlyphTypeface(new Uri(fontpath));
var character = 'W';
var charIndex = typeface.CharacterToGlyphMap[character];

var width = typeface.AdvanceWidths[charIndex];
var height = typeface.Height - typeface.TopSideBearings[charIndex]
                             - typeface.BottomSideBearings[charIndex];

这将给出em中的字符大小-要找到实际大小,需要将其乘以字体大小.

This will give the character size in em - to find real size it needs to be multiplied by the font size.

这篇关于如何在WPF中找到任意字形的确切大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆