I have code that uses a loop to get the font size of a text string that would fill an area (example shown at the bottom of this post, for a graphic control).
But what I'd like to do is come up with an approximate equation (exact would be ok too) and avoid the loop.
For example, this single line of code will generate a font size that is bounded by an area of WxH. Yes, it does a really poor job, but it demonstrates what I'd like to come up with.
The forum posts I've found mostly talk about looping solutions, mostly using GetTextExtent32. I didn't seen anything here, or on the web in general, that discusses a single equation-based font size calculation. An approximate answer would be fine - although not as approximate as the simple line of code I give above.
I'm experimenting with some code that's similar to creating gradient colors on a screen, but haven't come up with anything exciting so far.
Anyone seen anything along those lines?
Here's the looping code that gives an accurate answer (for a graphic control area in this case).
But what I'd like to do is come up with an approximate equation (exact would be ok too) and avoid the loop.
For example, this single line of code will generate a font size that is bounded by an area of WxH. Yes, it does a really poor job, but it demonstrates what I'd like to come up with.
Code:
fSize& = IIF( w<h, 0.1*w, 0.1*h)
I'm experimenting with some code that's similar to creating gradient colors on a screen, but haven't come up with anything exciting so far.
Anyone seen anything along those lines?
Here's the looping code that gives an accurate answer (for a graphic control area in this case).
Code:
'Compilable Example: #Compile Exe #Dim All #Include "win32api.inc" Global hDlg As Dword Function PBMain () As Long Local w As Long, h As Long Desktop Get Client To w, h Dialog New Pixels, 0, "Control Resize",100,100,200,200, %WS_OverlappedWindow To hDlg Control Add Graphic, hDlg, 300,"", 0,0,w,h, %WS_Visible Or %SS_Sunken Graphic Attach hDlg, 300, ReDraw Dialog Show Modal hDlg Call DlgProc End Function CallBack Function DlgProc() As Long Select Case Cb.Msg Case %WM_Size Dim w As Long, h As Long, x As Long, y As Long, txt$, fSize& Dialog Get Client Cb.Hndl To w,h Control Set Size Cb.Hndl, 300, w-20, h-20 txt$ = "Sample Text" 'center and print fSize& = GetFontSize_Graphic(w, h, txt$, 0.9) Graphic Clear Graphic Font "Comic MS Sans", fSize&, 1 Graphic Text Size txt$ To x,y Graphic Set Pos ((w-x)/2,(h-y)/2) Graphic Print txt$ Graphic ReDraw End Select End Function Function GetFontSize_Graphic(w As Long, h As Long, txt$, factor As Single) As Long Local x As Long, y As Long, fS& Do Until x > factor * w Or y > factor * h Incr fS& Graphic Font "Comic MS Sans", fS&, 1 Graphic Text Size txt$ To x,y Loop Function = fS& End Function 'gbs_00360
Comment