0

I'm trying to compute the smallest rectangle that can enclose a glyph, using SharpDX. The method GetGdiCompatibleGlyphMetrics doesn't seem to return the right values as each of my characters seem to have a width and height of 1000+, whereas I specified a font size of 16.

fontFace = new FontFace(factory, FontFaceType.Truetype, fontFileArray, 0, FontSimulations.None);    
var glyphMetrics = fontFace.GetGdiCompatibleGlyphMetrics(16, 1, null, false, glyphIndices, false);

If someone has an idea, I'd be really thankful.

Slel
  • 63
  • 6

3 Answers3

1

I found the solution. I'll post it here for future questions.

The FontFace class has a children class called FontFace1 which itself has a method GetKerningPairAdjustments. This kerning parameter solves everything.

Slel
  • 63
  • 6
0

It is probably because the font size (16) is set using a different measurement unit than the screen resolution.

Charles Petzold is pretty much the expert on these calculations, and covers them on his blog and in his books (which are always recommended reading for writing Windows Code). The only way to know for sure what the answer should be is to calculate it yourself for your current screen resolution and DPI settings.

Dr Herbie
  • 3,930
  • 1
  • 25
  • 28
  • I understand the font size is probably wrong. I have to give it a size in dip. I just can't figure out how to do it. By any chance, could you provide me with an example? All I know is that I want a font size of 16 (don't know which unit usually expresses it). My screen is a 120dpi monitor. – Slel Apr 02 '15 at 15:36
  • For font size in WPF, each point is 1/96 inch. So a font size of 16 points would be 0.1667 inches high. – Dr Herbie Apr 06 '15 at 09:18
  • Hello again. I managed to compute the result with the right font size and stuff. However, I'm having a problem computing the glyph bounding rectangles on the following string: "b A". The capital 'A' seem to overlap the space between the two letter, causing the computed box to be slighty shifted from the actual position of 'A'. Any thought? Thx – Slel Apr 09 '15 at 16:15
  • Sorry, but you've exceeded my 'circle of competence'! Hopefully someone else here will be able to help. – Dr Herbie Apr 12 '15 at 08:44
0

GetGdiCompatibleGlyphMetrics returns metrics in design units, you have to scale them yourself, using your font size, pixel per dip ratio (1 in your case), and design Em box size your font has. This will give you scaled design metrics. GetKerningPairAdjustments has nothing to do with this, you can use it only if font has kerning pairs data, and if you're actually enabled kerning in layout. Note that it's different from default kerning your layout might get from shaping, you can't assume it's the same because GPOS data does not have to match pair kerning data. Anyway, if you want to get actual ink box, you have to render a glyph, there's no other way to get accurate results.

bunglehead
  • 1,104
  • 1
  • 14
  • 22