I'm writing text onto a PDF document in a straightforward manner:
Code:
PdfPage infoPage = document.AddPage();
infoPage.Width = XUnit.FromPoint(pageWidthPoints);
infoPage.Height = XUnit.FromPoint(11 * 72);
infoPage.CropBox = new PdfRectangle(new XPoint(0, 0), new XSize(pageWidthPoints, 11 * 72));
XGraphics gfx = XGraphics.FromPdfPage(infoPage);
XFont font20 = new ("Verdana", 20, XFontStyleEx.Underline);
Debug.WriteLine($" font20.CellAscent={font20.CellAscent}; CellDescent={font20.CellDescent}");
XSize size20 = gfx.MeasureString(InfoPageTitle, font20);
Debug.WriteLine($"Size of \"{InfoPageTitle}\" is {size20}");
gfx.DrawString(InfoPageTitle, font20, XBrushes.Black, new XPoint(36, 36), XStringFormats.TopLeft);
A couple of confusing things.
First of all, I thought the point height of a font included both ascent and descent, but in this case it's telling me
Code:
font20.CellAscent=2059; CellDescent=430
so it looks PdfSharp assumes the font height is just the ascent part. Maybe that's why the XFont constructor calls the size parameter 'emSize'?
That is consistent with the later debug where it tells me the size of my title is 198.4x24.3.
But then when I print this document, telling it to print at 100%, the height of a capital I is a hair over 3/16" so about 14 points.
Why is my 20-point font sometimes 24-points high and sometimes 14 points?
EDIT
Playing around with fonts in Microsoft Word, it just gets weirder.
I created a document with two lines, one in 72-point Calibri and one in 72-point Verdana. Each line contained the text "Å y" so I could see both ascent and descent.
The line in 72-point Calibri was almost exactly 1" from bottom of 'y' to top of 'Å' which is great.
But the line in 72-point Verdana is about 1 3/16" from bottom of 'y' to top of 'Å'. I thought maybe it was not counting the circle on top of the A in the font height calculation, but from bottom of 'y' to top of 'A' is more like 7/8".
EDIT #2
While waiting for a reply here, I was playing around with more ideas, and discovered something.
Code:
Debug.WriteLine($" font20.Metrics.Ascent={font20.Metrics.Ascent}; Descent={font20.Metrics.Descent}; CapHeight={font20.Metrics.CapHeight}");
// font20.Metrics.Ascent=2059; Descent=430; CapHeight=1489
So the font height specified maps (approximately) to total ascent, but the height of a "flat" capital letter is only 14.89 points which is basically what I was measuring.