问题
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