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
www.rdsisemore.com/fonttest.txt
If anyone can figure out a more appropirate equation, I’l be in debted