Has anyone in freebasic done Knuth & Plass text wrapping routine?
Or know of any library or dll that will do it?
The best I can do, is use a greedy algorithm to do text wrapping.
Link to wikipedia on line wrapping .
Another website on this https://xxyxyz.org/line-breaking/
Rosettacode has c source code, that does this, but c is not my cup of tea.
Anyone done Knuth & Plass text wrapping routine?
Re: Anyone done Knuth & Plass text wrapping routine?
hello lassar
that's a rather complex algorithm, how about a very simple word-wrap
that's a rather complex algorithm, how about a very simple word-wrap
Code: Select all
dim as string tale = "In olden times when wishing still helped one, there lived a king "_
"whose daughters were all beautiful, but the youngest was so beautiful "_
"that the sun itself, which has seen so much, was astonished whenever "_
"it shone in her face. Close by the king's castle lay a great dark "_
"forest, and under an old lime tree in the forest was a well, and when "_
"the day was very warm, the king's child went out into the forest and "_
"sat down by the side of the cool fountain, and when she was bored she "_
"took a golden ball, and threw it up on high and caught it, and this "_
"ball was her favorite plaything."
dim as long i, m = 80
while len(tale)>m
i=m
while mid(tale, i+1,1)<>" "
i-=1
wend
print left(tale,i)
tale=mid(tale,i+2)
wend
if tale<>"" then print tale
Re: Anyone done Knuth & Plass text wrapping routine?
Here is a drammatically different approach - not portable, I'm afraid:;-)
Code: Select all
#include "Windows.bi"
dim as string tale = "In olden times when wishing still helped one, there lived a king "_
"whose daughters were all beautiful, but the youngest was so beautiful "_
"that the sun itself, which has seen so much, was astonished whenever "_
"it shone in her face. Close by the king's castle lay a great dark "_
"forest, and under an old lime tree in the forest was a well, and when "_
"the day was very warm, the king's child went out into the forest and "_
"sat down by the side of the cool fountain, and when she was bored she "_
"took a golden ball, and threw it up on high and caught it, and this "_
"ball was her favorite plaything."
dim hEdit as HWND=CreateWindowEx(0, "edit", tale, ES_MULTILINE, 0, 0, 200, 800, GetDesktopWindow(), 0, GetModuleHandle(0), 0)
dim as integer ct, chars
dim as const string buffer="[...........................................................................................................................................................]"
for ct=0 to 9
print left(buffer, SendMessage(hEdit, EM_GETLINE, ct, StrPtr(buffer)))
next
Sleep
Re: Anyone done Knuth & Plass text wrapping routine?
Need a happier ending.
Code: Select all
function wrap(s as string,z as long) as string
dim as string res
dim as long ct
for n as long=0 to len(s)-1
ct+=1
res+=chr(s[n])
if ct>z and s[n]=32 or s[n]=asc(".") then
ct=0
res+=chr(13,10)
end if
next
return res
end function
dim as string tale = " In olden times when wishing still helped one, there lived a king "_
"whose daughters were all beautiful, but the youngest was so beautiful "_
"that the sun itself, which has seen so much, was astonished whenever "_
"it shone in her face. Close by the king's castle lay a great dark "_
"forest, and under an old lime tree in the forest was a well, and when "_
"the day was very warm, the king's child went out into the forest and "_
"sat down by the side of the cool fountain, and when she was bored she "_
"took a golden ball, and threw it up on high and caught it, and this "_
"ball was her favorite plaything." _
" Then late one evening Lancelot passed by." _
" Tall and dark and straight. Like some young cypress, which in a Queen's " _
"secluded garden throws it's long slight shadow on the moonlit turf by midnight, " _
"to a bubbling fountain's sound. The end."
print wrap(tale,80)
sleep
Re: Anyone done Knuth & Plass text wrapping routine?
Found a fairly good youtube video on text wrapping.
Youtube video: Text Justification Dynamic Programming
Youtube video: Principle of Optimality - Dynamic Programming
Youtube video: Text Justification Dynamic Programming
Youtube video: Principle of Optimality - Dynamic Programming