I want to compute the exact text width in metric units of a given string. My pseudocode looks like this:
Bitmap.Canvas.Assign(Font);
PixelWidth := Bitmap.Canvas.TextWidth(Font)
MetricWidth := PtToMM * (PixelWidth * 72.0 / GetScreenDPI);
PtToMM
is a constant that is defined as 0.352777778
. This is pretty accurate for some fonts and font sizes but for others it is either too small or too large. I experimented a lot witht other possiblities such as GetCharWidth32
and GetCharABCWidths
, also with mapping mode MM_LOMETRIC
but I just can't get it to work. This problem is haunting me, so please can anyone help and show me where I'm wrong. Thank you very much!
EDIT I checked for one string: metric width is computed as 4.17 cm, the width on the actual printout (measured on paper) is 4.4 cm (font Times New Roman, size 12).