Another Puzzle

I have two controls where I need to line up the text vertically. One is a CANVAS the other is a TextArea (actually the Swift counterparts, but they should be the same for this exercise).

Given any font size, and the metrics for it, I need to calculate an offset… This value is where the text needs to be DRAWN in the canvas to line up with the textview.
I derived an equation, that works in almost ALL font sizes (Menlo font)… but it fails on at least one case (I obviously haven’t tried EVERY combination)

For Example … Menlo 20pt gives this information

PointSize  : 20.0
Ascender   : 18.564453125
Descender  : -4.716796875
Xheight    : 10.9375
capheight  : 14.580078125
charH      : 30.0
LineSpace  : 24.0
*Offset    : -5.5  <--------- THIS IS THE VALUE TO BE CALCULATED

The current equation is
EQUATION : (charH - ceil(ascender)-ceil(abs(descender+0.5)))+0.5
but DOESN’T provide the correct answer for font size 30

PointSize  : 30.0
Ascender   : 27.8466796875
Descender  : -7.0751953125
Xheight    : 16.40625
capheight  : 21.8701171875
charH      : 43.0
LineSpace  : 35.0
***Offset    : -7.5 [6.5] <-----6.5 is the right answer

I have test fontsizes of 10,20,24,26,28,30,31,35 and 40 and of those 30 is the only one the is wrong. That is not to say that others may be…

Here is a file with all the metrics for those font sizes

If anyone can figure out a more appropirate equation, I’l be in debted :slight_smile:

Are these correct ? Some I’m not sure what they represent

Ascender - the maximum height (vertical from baseline) of the ascender in the given font
Descender - the maximum height of the descender in the given font (assuming this is measure as baseline =0 ?)
Xheight - NOT SURE ???
capheight - NOT SURE ???
charH :- NOT SURE ???
LineSpace spacing between lines ???

Most of direct attributes of the Font (NSFont)
CharH is the character height (would be TextHeight in Xojo)
Linespacing is a value I calculated, and verified to be the accurate spacing for the TextView based on the supplied font

for some reason, Leading is ZERO in all cases so I didn’t put it here

curious as you think it would just be

(line # * line height) - (leading + descender)

This is a differnet “puzzle” that the question I posed to find the coordinates of a line, because that soultion to that is

The solution to this puzzle, you can forget that a font is even involved…

Given the supplied values, derive an equation that results in the specified value

The equation I posted above, does in fact give precisely the correct result for most cases, but I can’t figure out why its wrong for font size 30. Most likely a rounding error, but so far I can’t see it.