PDFGraphics FontAscent not accurate?

Hopefully someone else has seen this and can advise.
I believe that fontAscent is not accurate until something has been drawn to the graphics context. I’ve ended up having to do something like DrawText “”,0,0 and then text shows up right.

The evidence seems to be drawing one line of text using the FontAscent as part of the calculation for the Y position, and then a second string. They stagger.

Hope this makes sense and if so, that there is a more logical workaround than what I’m doing.

A picture of what “they stagger” means would be helpful.

I’ve only tested this on Mac, so I don’t know if there are platform differences.
This is produced from the code below:

Var p As New PDFDocument
Var g As graphics = p.Graphics

g.FontName = “Helvetica”
g.FontSize = 24

g.drawText “One”, 20, g.Height/2-g.FontAscent/2
Var w As Double = g.TextWidth(“One”)+40
g.DrawText “Two”,w, g.Height/2-g.FontAscent/2
w =w +g.TextWidth(“Two”)+20
g.DrawText “Three”,w, g.Height/2-g.FontAscent/2
g.PenSize = 1
g.drawline 20, g.Height/2, g.Width-20, g.Height/2

Var f As FolderItem = SpecialFolder.Desktop.child(“FontAscentExample.pdf”)
If f.Exists Then f.delete
p.Save(f)
f.open

and insert g.drawText “”,0,0 after setting the font size etc, and “One” prints at the same Y location as the rest.

Hi @Chris_Halford,

change your code into this and it works fine :wink:

Var p As New PDFDocument
Var g As Graphics = p.Graphics

g.FontName = "Helvetica"
g.FontSize = 24

Var x As Double = 20
Var y As Double = g.Height / 2 - g.FontAscent / 2

g.DrawText("One", x, y)
x = x + g.TextWidth("One") + 20

g.DrawText("Two", x, y)
x = x + g.TextWidth("Two") + 20

g.DrawText("Three", x, y)

g.DrawLine(20, g.Height / 2, g.Width - 20, g.Height / 2)

Var f As FolderItem = SpecialFolder.Desktop.child("FontAscentExample.pdf")
If f.Exists Then f.Delete
p.Save(f)
f.Open

Yes, I can store a value in a variable to use. My concern is that this should be consistent and now I’m not positive which case is giving me the right result. I tend to think it’s the subsequent calls since “two” and “three” position the same. I don’t want to cache the wrong value to reuse.

Yeah, it doesn’t seem right that FontAscent changes from 10 to 26 after printing something. Even if you cache the correct value (whichever that is), something is weird.

Please, would you mind to file a case on Feedback so I can keep this in the radar? :wink:

Thank you!

I meant to reply before. Yes, I did submit it.