One aspect is monospaced vs proportional fonts. The former have fixed width, but even that is not only the character width, there's also letter spacing. In proportional fonts this can even become negative (aka kerning) in letter combinations like AV, for example. Therefore character, average character width or maximum character width are not helpful. In monospaced fonts that's easier, as all letters have same width, such fonts are designed to align perfectly in successive lines.
Fontmetric, for example, gives you a large width for an Arial A and V, but AV together are not simply the sum of both width because of kerning. The best thing you have for measuring width is an autofit Label. The problem with that is it only supports captions up to 256 character length.
Another detail: Height is also not just chracter Height x linesof text, there are fontmetrics for different partial aspects of the font, one of which also is leading, the space between lines, just like there is letter spacing. Those are the major metrics to know and take into account when it comes to calculating the editbox size. Ideally you have a function that takes in text and gives you the dimensions of how large it renders.
But even in simple situation like monospaced fonts you don't get the same results from an autosize label width and height and Fontmetric, though the overall text size in such fonts should simply be character width x length of the longest line and character height x lines count.
Let's look at what a label autosizes to when giving it a single A, a double AA, and two or three lines of AA:
Code:
_screen.Fontname = "Arial"
_screen.FontSize = 12
_screen.AddObject("label1","label")
With _screen.label1
.fontname="Courier New"
.fontsize=12
.autosize=.t.
.wordwrap = .T.
.visible = .T.
.caption="A"
Clear
?
?
?
?
? "A", .width, .height
? "---"
.caption="AA"
? "AA", .width, .height
? "---"
.caption="AA"+Chr(13)+Chr(10)+"AA"
? "AA"+Chr(13)+Chr(10)+"AA", .width, .height
? "---"
.caption="AA"+Chr(13)+Chr(10)+"AA"+Chr(13)+Chr(10)+"AA"
? "AA"+Chr(13)+Chr(10)+"AA"+Chr(13)+Chr(10)+"AA", .width, .height
? "---"
? "fontmetric: width ", Fontmetric(6,"Courier New",12,"N")," height ",Fontmetric(1,"Courier New",12,"N")
? "fontmetric leading (line spacing)", Fontmetric(4,"Courier New",12,"N")
EndWith
A single A label captions makes it a 10x20 rectangle area, AA simply has double width, as you can expect from a monospaced font.
A double line AA does only rise by 18 in height, not 20, but has width 22, why? A mishap aof autosizing of the label, it seems.
But then the Fontmetric height is 18 and Fontmetirc width is 10, leading is 2 pixels, which does not explain why a single line already has leading, I would only expect lines-1 times leading, as there are one less than the lie count of line gaps.
It's somewhat consistent, but when it comes to the editbox on a form, it seems to change a bit. I'll get back to this later, just wanted to share these findings now already.