Actually, I put that code in a separate post:
"Getting Physical Length of String"
And since then I made up two ways to do it:
In the first, I calculate the needed physical width, then add replace each tab with a string of spaces.
In the second, I brute forced it - adding a single space, calculating the new width (as compared to a tab stop), then add more spaces until I exceed the tab stop width.
In both cases you have make sure the ending position, once spaces are added, is not less than the tabstop position, so that in the next iteration you've cleared the last tabstop and won't hit one twice.
Code:
Approach one: build print string, substituting tabs with calculated # of spaces to reach tabstops For j = 0 To UBound(D) temp$ = "" For i = 1 To ParseCount(D(j), $Tab) temp$ = temp$ + Parse$(D(j),$Tab, i) XPrint Text Size temp$ To tempWidth!, ncHeight! iTab& = Fix(tempWidth!/0.5)+1 iSpaces& = (TabLoc(iTab&)-tempWidth!) / iSpaceWidth! temp$ = temp$ + Space$(iSpaces&) 'add extra single space as needed to ensure current tab position is crossed XPrint Text Size temp$ To tempWidth!, ncHeight! If tempWidth! < TabLoc(iTab&) Then temp$ = temp$ + " " Next i result$ = result$ + $CrLf + temp$ Next J Approach Two: build replacement string, add spaces until tabstop is passed For j = 0 To UBound(D) temp$ = "" For i = 1 To ParseCount(D(j), $Tab) temp$ = temp$ + Parse$(D(j),$Tab, i) XPrint Text Size temp$ To tempWidth!, ncHeight! iTab& = Fix(tempWidth!/0.5)+1 'next location tab after current endpoint If iTab& < 50 Then Do temp$ = temp$ + " " XPrint Text Size temp$ To tempWidth!, ncHeight! Loop While tempWidth! < TabLoc(iTab&) End If Next i result$ = result$ + $CrLf + temp$ Next j
The point of the other post was to find a way to not use XPrint as part of the solution for getting the right number of spaces. As you saw in that post,
I tried using GetTextExtentPoint32 in lieu of XPrint statements. It worked, but not perfectly - giving consistently short positions. I'd assumed that was because I failed to take into account borders/edges in some way, but I've not followed up on it to run it to ground.
Leave a comment: