Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
QFindMaxPos
#1
Code: (Select All)
Screen _NewImage(640, 480, 32)
Randomize Timer
f = _LoadFont("times.ttf", 20)
_Font f

Do
Cls
text$ = ""
For i = 1 To 75
text$ = text$ + Chr$(Rnd * 60 + 64) 'build a random string for various lengths when testing
Next

temp$ = Left$(text$, QFindMaxPos(text$, _Width))
_UPrintString (0, 0), temp$
'The next line shows how long our text would be, if we printed the next character.
Locate 2, 1: Print _UPrintWidth(temp$), _UPrintWidth(Left$(text$, Len(temp$) + 1))
Do
k = _KeyHit
If k = 27 Then System
_Limit 10
Loop Until k > 0
Loop




Function QFindMaxPos (text$, w)
'Quick Find Max Position
'This routine quickly finds which position fits within a given width of a string
'This works on a binary search method to determine max length and character position,
'So for long strings or large screens, it can find the proper position much quicker than just searching
'and comparing lengths from left to right, or right to left.

min = 0
max = Len(text$)
If _FontWidth Then 'monospaced font
max = Int(w / _FontWidth) + 1 'the most possible characters that can fit on a line
Else
If max > w Then max = w 'most possible would be 1 character per pixel!
End If
If _UPrintWidth(Left$(text$, max)) < w Then QFindMaxPos = max: Exit Function
Do
test = Int((max - min) / 2) + min
p = _UPrintWidth(Left$(text$, test))
If p = oldp Then Exit Do
Select Case p
Case Is < w
min = test
Case Is > w: max = test
Case Is = w: Exit Do
End Select
oldp = p
Loop
QFindMaxPos = test
End Function


A routine to quickly find the max sized string that one can _UPRINTSTRING to a given area.
Reply




Users browsing this thread: 1 Guest(s)