PDFsharp & MigraDoc Foundation

PDFsharp - A .NET library for processing PDF & MigraDoc Foundation - Creating documents on the fly
It is currently Thu Jan 16, 2025 8:44 pm

All times are UTC


Forum rules


Please read this before posting on this forum: Forum Rules



Post new topic Reply to topic  [ 2 posts ] 
Author Message
PostPosted: Wed Jan 08, 2025 6:55 pm 
Offline

Joined: Tue Dec 17, 2024 11:56 pm
Posts: 8
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.


Top
 Profile  
Reply with quote  
PostPosted: Sun Jan 12, 2025 4:47 pm 
Offline
PDFsharp Guru
User avatar

Joined: Sat Mar 14, 2015 10:15 am
Posts: 1026
Location: CCAA
cshearercooper wrote:
Why is my 20-point font sometimes 24-points high and sometimes 14 points?
I will not attempt to answer your question. Too much in your post does not make sense to me.
You do not give details how you performed that "100%" print-out.

cshearercooper wrote:
[...] 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'?
The PDFsharp XFont class mimics the behaviour of Microsoft’s GDI+ Font class and parameters have the same name and the same function.
And yes, "emSize" refers to the size of "M", in this case the height of "M".
But depending on the liberties taken by the font designers, the actual height of "M" may vary between different fonts.

cshearercooper wrote:
Font height not making sense
Sometimes things make sense, but not at first glance. To me, "I do not understand" is more inviting than "makes no sense".

Another rant about the emSize parameter can be found here:
https://stackoverflow.com/q/75998265/162529

_________________
Best regards
Thomas
(Freelance Software Developer with several years of MigraDoc/PDFsharp experience)


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 2 posts ] 

All times are UTC


Who is online

Users browsing this forum: No registered users and 47 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Privacy Policy, Data Protection Declaration, Impressum
Powered by phpBB® Forum Software © phpBB Group