Anyone done Knuth & Plass text wrapping routine?

General FreeBASIC programming questions.
lassar
Posts: 300
Joined: Jan 17, 2006 1:35
Contact:

Anyone done Knuth & Plass text wrapping routine?

Postby lassar » Aug 15, 2018 2:17

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.
srvaldez
Posts: 2007
Joined: Sep 25, 2005 21:54

Re: Anyone done Knuth & Plass text wrapping routine?

Postby srvaldez » Aug 15, 2018 10:04

hello lassar
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
jj2007
Posts: 1180
Joined: Oct 23, 2016 15:28
Location: Roma, Italia
Contact:

Re: Anyone done Knuth & Plass text wrapping routine?

Postby jj2007 » Aug 15, 2018 10:57

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
;-)
dodicat
Posts: 5828
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: Anyone done Knuth & Plass text wrapping routine?

Postby dodicat » Aug 15, 2018 13:02

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
   
lassar
Posts: 300
Joined: Jan 17, 2006 1:35
Contact:

Re: Anyone done Knuth & Plass text wrapping routine?

Postby lassar » Aug 15, 2018 19:58

Found a fairly good youtube video on text wrapping.

Youtube video: Text Justification Dynamic Programming

Youtube video: Principle of Optimality - Dynamic Programming

Return to “General”

Who is online

Users browsing this forum: No registered users and 2 guests