Re: Fill Out Algorythm
Re: Fill Out Algorythm
- Subject: Re: Fill Out Algorythm
- From: Arthur J Knapp <email@hidden>
- Date: Fri, 21 Dec 2001 13:17:52 -0500
>
Date: Fri, 21 Dec 2001 01:26:55 +0100
>
From: Alexander Schrieken <email@hidden>
>
Subject: Fill Out Algorythm
"Algorythm", I see that you and I suffer from the same spelling
affliction... ;-)
>
I like my text nice and clean. But unfortunately
>
not all applications offer the possibility of
>
spreading the text over the entire length of the
>
lines (adding extra spaces inbetween the words
>
of the sentences).
Publishing software does this using special "kerning" information,
rather than with "spaces".
>
... So I'm trying to develop a
>
script that will take care of this for me. But what
>
is the most effective algorythm? What logic may
>
best be applied so that my text looks like the
>
example below, rather than this.
>
Example:
>
>
| I like my text nice and clean. But unfortunately
>
| not all applications offer the possibility of
Ooooo, a CHALLENGE!!!
on AlignJustified(str, line_len)
(* str == A "paragraph" to space-format. I am assuming a mono-spaced
* font, and that there are no tabs.
*
* line_len == An amount per each line of the "paragraph". This
* amount should include the final carriage return.
*)
set o to text item delimiters
try
-- Remove all runs of spaces, (perhaps added from
-- a previous call to an AlignType() handler).
--
repeat while (str contains (space & space))
set text item delimiters to {space & space}
set str to str's text items
set text item delimiters to {space}
set str to "" & str
end repeat
-- Break at whitespace. This destroys all "paragraphs",
-- so when using this handler, one would want to pass
-- individual paragraphs for proccessing.
--
-- We could go about this in a different way, but it
-- seems easier to send each paragraph to this handler
-- than to try to preserve existing paragraphs.
--
set text item delimiters to {space}
--
-- Note: The line below is more complicated than it looks:
--
set str to ("" & (every paragraph of str))'s text items
set theText to {} -- collect the text
set oneLine to {} -- collect lines
repeat with wrd in str
-- Note: tids are still {space}
--
-- Note: "< line_len" as opposed to
-- "<= line_len" to save room for return.
--
if (("" & oneLine & space & wrd)'s length < line_len) then
set oneLine's end to "" & wrd
else
set theText's end to oneLine
set oneLine to {"" & wrd}
end if
end repeat
-- Save the last line.
--
-- Note: In my testing, I was getting last lines that
-- looked like this:
--
-- be applied so that my text looks like the example
-- below, rather than this.
--
-- I assume this to be undesirable.
--
set text item delimiters to {space}
set lastLine to "" & oneLine
-- Process each line.
--
repeat with lin in theText
-- Initially, a fast way to "space" words:
--
set text item delimiters to {space}
repeat while (("" & lin)'s length < line_len)
set text item delimiters to ,
{space & text item delimiters}
end repeat
set text item delimiters to ,
{("" & text item delimiters)'s text 1 thru -2}
-- Then, we have to go word by word:
--
repeat with wrd in lin
if ((space & lin)'s length >= line_len) then
exit repeat
else
set wrd's contents to "" & wrd & space
end if
end repeat
set lin's contents to "" & lin
end repeat
-- Lineify, adding last line:
--
set text item delimiters to {return}
set str to "" & theText & return & lastLine
on error m number n
set text item delimiters to o
error ("AlignJustified()" & return & return & m) number n
end try
set text item delimiters to o
return str
end AlignJustified
"I like my text nice and clean. But unfortunately
not all applications offer the possibility of
spreading the text over the entire length of the
lines (adding extra spaces inbetween the words
of the sentences). So I'm trying to develop a
script that will take care of this for me. But what
is the most effective algorythm? What logic may
best be applied so that my text looks like the
example below, rather than this."
set theString to result
AlignJustified(theString, 50)
-->
I like my text nice and clean. But unfortunately
not all applications offer the possibility of
spreading the text over the entire length of the
lines (adding extra spaces inbetween the words of
the sentences). So I'm trying to develop a script
that will take care of this for me. But what is
the most effective algorythm? What logic may best
be applied so that my text looks like the example
below, rather than this.
OK, at this point, there are a couple of problems. First
I have not taken into consideration a paragraph indent. Also,
there would be diffaculties involved if the string contains
tabs. But for now, assuming that you process one "paragraph"
at a time, and that you are using a mono-spaced font, I'd
say this is a good start. :)
{ Arthur J. Knapp, of <
http://www.STELLARViSIONs.com>
<
mailto:email@hidden>
try
<
http://www.esglabs.com/>
on error number -128
end try
}