Problem centering lines in a canvas

meanwhile I read about this issue and tested a lot of suggestions given in the forum. But I could not come to a solution.

Font and fontsize do not change during execution of the code (paint event of a canvas). But assigning different font-name and size, difference in height comes quicky to the eye.

g.DrawingColor = pQRTxtColor
g.FontName = pFontName
g.FontUnit = FontUnits.Point
g.FontSize = pFontSize
g.Bold = isBold
g.Italic = isItalic

dim mTop as Integer = g.TextHeight//double

dim r as String = “I” + endofline + “go” + endofline + “to” + endofline + “London”

dim mSplitter() as String = split(r, EndOfLine)
dim incr as double = mtop// + (g.TextHeight - g.TextAscent)// *2//integer
for j as Integer = 0 to mSplitter.Ubound
dim theLine as string = mSplitter(j)
g.DrawText(theLine, (g.Width - g.TextWidth(theLine)) / 2, incr)
incr = incr + mTop //+ (g.TextHeight - g.TextAscent)// / 3 -2
next

As you can see from the various alternatives after each commented line (//), I tried a lot of solutions, but I have not come to a stable one.
That is, the overall heigh of the text when drawn center.aligned is never of the same height of the text when drawn as left.aligned.

Suggestions welcome. Thanks.

[removed a few lines because they would not show properly here].

Hi Carlos,

I assembled a quick program to see what the code is doing.

CanvasPic

Changing colour, name, unit, size, bold, and italic seem to work on my computer. Does this work on your computer?

ForumCanvas.zip (9.4 KB)

I am not sure what the computer is doing with the left-alignment or center-alignment. Could I ask you to change the attached code and show me what you are seeing?

Thanks :slight_smile:

Hi, Eugene,
yes your code works on my computer exactly as it used to work.

I’m afraid I cannot undestand what to change; in fact, after clicking the OK button I see what I said in the original post: i.e. comparing to the vertical spacing before clicking OK, now the line vertical spacing gets bigger.
Or am I not understanding what you intended to say? You know, i’m rather dense.
Meanwhile, thank you for giving your time.

Hi Carlo,

Its all good, I am just trying to understand the issues and help you :slight_smile:
What is the code that you were using to draw centre.align and left.align, and do you see the issue with the example program?

Warm regards.

Sorry, it is clear I did not understand well what you said.
So, what I get from your code is correct, yet when I run the same code in my project then the vertical distance between the four lines is smaller (as I tried to explain in my post.

This is yours, and the line spacing is correc:

This is my project, and you can see that the line spacing is smaller:
Screenshot 2024-10-02 at 06.06.03

Difficult to understand, since we are running the same code…
The only difference is that I run your code on Xojo 24, and my code on Xojo 2021. Trying now to make my project work with Xojo 24 since it stumbles when launching the debugger.
I’ll let you now.
Meanwhile, thanks again

note that “go” appears lower because the writing line is below “o” and “g” is a subline character

@Eugene_Dakin Actually the problem is still the one outlined in my post. In fact, when running your project I did not notice that the font name defaults to Times New Roman when it is not Arial. So, changing the alternate font to Times one can clearly see the difference in line-spacing between Left-align and Center-align (of course, each fonts behaves its own way, only that I was accostumed testing with Times because it was the one showing more clearly the line-spacing difference).
Attaching your file revised: I added a checkbox: “Align Center”; ticking it one can see the difference in line-spacing I’m talking about and it confirms that the issue is the code inside the canvas.

ForumCanvasCarlo 2.xojo_binary_project.zip (10.8 KB)

Thank you again for giving your time.

Hi, the file attached in my reply to Eugene shows the issue.
As I said, every font behaves its own way according to its height and ascent etc.; with Times the difference comes at once to the eye; with other fonts it is less noticeble.
All this points to the inadeguacy of my code.
Thank you.

Hi Carlo,

Thanks for changing the program to help me understand the issue. I ran the program and made screen-grabs, and compared the height of the letters, and they seem to be the same. Is there another test that I can perform to view this issue?

Thank you.

Hi Eugene,
I added a fontMenu and another canvas, so that you can test more quickly the fonts and see the difference if any…
Although I see that that majority of the fonts behave in a positive way, nevertheless there are fonts showing different rendering: apart from Times, there is Palatino, Hoefler, Helvetica and so on that draw correctly in text editors.
Thank you,
EugeneTest 2021 2.xojo_binary_project.zip (11.4 KB)

[added zip file]

Guys, keep in mind that the issue is probably in the fonts themselves, in which case passing these sample projects back and forth may not yield the same results no matter what you do.

1 Like

By passing the files I make myself sure that the issue is in the fonts themselves; i.e. that the code in the paint event of the canvas needs adjusting; yet I don’t know how.

Can you attach some screen shots showing the fonts which appear incorrectly?

They are already there, on my second or third post.

Visually line spacings can be different.
For example between this 2 lines :
aeiou
aeiou
The space is bigger than between
pgj
fhlt
because for this 4 lines the TextHeight is the same but not the hight of the characters.

Just in case you aren’t aware, the y position when you draw text is the baseline and not the top left.

I think your code should be something like this (I haven’t tested this though):

g.DrawingColor = pQRTxtColor
g.FontName = pFontName
g.FontUnit = FontUnits.Point
g.FontSize = pFontSize
g.Bold = isBold
g.Italic = isItalic

dim mTop as Integer = g.TextAscent

dim r as String = “I” + endofline + “go” + endofline + “to” + endofline + “London”

dim mSplitter() as String = split(r, EndOfLine)

for j as Integer = 0 to mSplitter.Ubound
     dim theLine as string = mSplitter(j)

     g.DrawText(theLine, (g.Width - g.TextWidth(theLine)) / 2, mTop)

     mTop = mTop + g.TextHeight
next

Tried it: still not working. Welcome to the challenge.

I’ve never used the FontUnit property, what happens if you remove the line that assigns it?

No improvements…

I’ve just read through the topic again and i’ve misunderstood the problem.

so… When you draw the text centre aligned, you are trying to match the line spacing that occurs when drawing the text left aligned.

Am I correct?