How to find exact size for an arbitrary glyph in WPF?

旧城冷巷雨未停 提交于 2019-12-11 01:27:15

问题


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

The font is loaded using GlyphTypeface class.

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.

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.

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).


回答1:


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];

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




回答2:


You could perhaps get the Bounds of the geometry returned by the GlyphTypeface.GetGlyphOutline method:

GlyphTypeface typeface = new GlyphTypeface(new Uri(@"C:\Windows\Fonts\SegoeUI.ttf"));
ushort glyphIndex = typeface.CharacterToGlyphMap['W'];
double emSize = 14d;
Geometry outline = typeface.GetGlyphOutline(glyphIndex, emSize, 0d);
Size size = outline.Bounds.Size;


来源:https://stackoverflow.com/questions/11971547/how-to-find-exact-size-for-an-arbitrary-glyph-in-wpf

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!