문제

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

도움이 되었습니까?

해결책

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.

다른 팁

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;
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top