character to justify text

Fred moyer fmoyer at aol.com
Wed Jun 27 21:08:53 EDT 2007


I was just working on this justification problem 2 weeks ago and came  
up with the following low-tech solution that manipulates the textsize  
of spaces. This script:
    - doesn't require any special stack preparation (adding images, etc.)
    - preserves textstyles (except textsize.)
    - allows the last straggling line in a paragraph to remain unjustified.
    - and it allows for hyphenation, which means that you don't end up  
having huge spaces between words.

This solution doesn't justify on the fly like Brian's script (very  
nice, Brian!) There are also a few other bells and whistles I need to  
add. But if this script is helpful, please use it. Also there may be  
bugs in it -- I wrote it just for my own use. To use it just use  
"justify " and the short id of the field.

Note:  it does add "-  " (dash and two spaces) inside hyphenated  
words. So, in order to get back to the original text, I suggest this  
script:

on GetRidOfHyphens fid
   put the htmlText of fld id fid into hText
   replace "-  " with "" in hText
   set the htmlText of fld id fid to hText
end GetRidOfHyphens


on justify fid
   set the fixedLineHeight of fld id fid to true
   set the cursor to watch
   lock screen
   set the backColor of char 1 to -1 of fld id fid to empty
   put " " into strangeCharThatLooksLikeASpace
   put the htmlText of fld id fid into hText
   replace strangeCharThatLooksLikeASpace with " " in hText
   replace "-  " with "" in hText
   set the htmlText of fld id fid to hText
   set the fixedLineHeight of fld id fid to true
   set the backColor of char 1 to -1 of fld id fid to empty
   put fld id fid into FieldPlainText
   ---set the textsize of char 1 to -1 of fld id fid to empty
   put the number of chars in FieldPlainText into numberOfCharsInFld
   put the textheight of fld id fid into FieldTextHeight
   put the effective textsize of fld id fid into EffectiveTextSize
   put empty into listofSpacesinThisLine
   put 1 into CurrChar
   put 1 into CharNoForFirstCharOfLine
   put empty into CharNoForLastSpaceInLine
   put trunc(EffectiveTextSize * .6) into SmallSpaceSize
   if SmallSpaceSize < 8 then put 8 into SmallSpaceSize
   ---put "yes" into isThereAMax
   --- = NO WHEN HYPHENATION IS NOT POSSIBLE AND YOU HAVE TO JUST GO  
FOR IT
   put 0 into charsToSkip
   --- NEXT LOOP MAKES ALL SPACES IN THE FIELD A BIT SMALLER THAN NORMAL
   repeat
     put offset(" ",FieldPlainText,charsToSkip) into o
     if o = 0 then exit repeat
     set the textsize of char charsToSkip + o of fld id fid to  
SmallSpaceSize
     put charstoSkip + o into charstoSkip
   end repeat
   repeat
     put "normal" into status
     --- try to find the current "line" meaning: which words take up  
one line
     ---put "yes" into isThereAMax
     put char CurrChar of FieldPlainText into c
     if c is in (" " & return) then
       put the formattedHeight of char CharNoForFirstCharOfLine to  
currChar of fld id fid into fh
       if fh ² FieldTextHeight then
         if c = return then
           add 1 to currChar
           put empty into listofSpacesinThisLine
           put currChar into CharNoForFirstCharOfLine
         else
           put currChar & "," after listofSpacesinThisLine
           put currChar into CharNoForLastSpaceInLine
           --- WE NEED TO STORE THIS VALUE BECAUSE WHEN WE GO OVER  
INTO TWO LINES, WE'LL NEED
           --- TO REMEMBER THE LAST SPACE CHAR  WHERE IT WAS JUST ONE  
LINE
           add 1 to currChar
         end if
       else
         --- ALL OF THE FOLLOWING HAPPENS IF THESE WORDS TAKE UP TWO  
LINES:
         put last item of listofSpacesinThisLine into saveForHyphenation
         delete last item of listofSpacesinThisLine
         if listofSpacesinThisLine is empty then
           --- THERE ARE NO SPACES IN THIS LINE!
           --- WILL HAPPEN IF that line consists of only a return
           --- OR MAYBE A REALLY REALLY LONG WORD!
           --- SO START WITH THE NEXT LINE
           put empty into listofSpacesinThisLine
           put currChar + 1 into CharNoForFirstCharOfLine
           put CharNoForFirstCharOfLine into currChar
         else
           --- STEP 1: MAKE SPACES BIGGER AND BIGGER UNTIL WE'RE INTO  
2 LINES
           put SmallSpaceSize into ts
           repeat
             add 1 to ts
             if ts > EffectiveTextSize * 2 and status = "normal" then
               --- WEIRD THINGS HAPPEN IF THE TEXTSIZE OF A SPACE  
GETS TOO BIG
               --- SO AT THIS POINT WE NEED TO HYPHENATE

               --- FIND WORDTOBEHYPHENATED
               put char CharNoForLastSpaceInLine + 1 to currChar - 1  
of fieldPlainText into WordToBeHyphenated
               repeat for each item i in listofSpacesinThisLine
                 set the textsize of char i of fld id fid to  
SmallSpaceSize
               end repeat
               put 0 into hyphenTries
               put the htmltext of char CharNoForFirstCharOfLine to  
currChar - 1 of fld id fid into HTMLLinePlusExtraWord
               repeat
                 set the backcolor of char CharNoForLastSpaceInLine +  
1 to currChar - 1 of fld id fid to "red"
                 select char CharNoForLastSpaceInLine + 1 of fld id  
fid -- SCROLL TO THE SPOT
                 select empty
                 if hyphenTries = 0 then
                   ask "Please hyphenate this word: (Just put '-'  
inside it.) If no hyphenation is possible then don't change it." with  
WordToBeHyphenated
                 else
                   Ask "Your last hyphenation did not work. Try  
something else. If no hyphenation is possible then then don't change  
it" with WordToBeHyphenated
                 end if
                 if it is WordToBeHyphenated then
                   put SmallSpaceSize into ts
                   put "NoMaxSpaceSize" into status
                   set the backcolor of char 1 to -1 of fld id fid to  
empty
                   add 1 to ts
                   exit repeat
                 else if it is empty or the result is not empty then
                   beep
                   set the backcolor of char 1 to -1 of fld id fid to  
empty
                   exit justify
                 else
                   put it into desiredHyphenation
                   set the backcolor of char 1 to -1 of fld id fid to  
empty
                   put offset("-",desiredHyphenation) into o
                   put "-  " before char (CharNoForLastSpaceInLine +  
o) of fld id fid
                   --- put WordToBeHyphenated & "," & it & return  
after gPastHyphens
                   put the formattedheight of char  
CharNoForFirstCharOfLine to (CharNoForLastSpaceInLine + o + 2) of fld  
id fid into postHyphenFH
                   if PostHyphenFH ² FieldTextHeight  then
                     -- yay! hyphenation worked!
                     put (CharNoForLastSpaceInLine + o + 2) into  
CharNoForLastSpaceInLine
                     put listofSpacesinThisLine & "," after  
listOfSpacesInThisLine
                     put CharNoForLastSpaceInLine + 1 into currChar
                     put SmallSpaceSize into ts
                     put fld id fid into fieldPLainText
                     put the number of chars in fieldPlainText into  
numberOfCharsinFld
                     exit repeat
                   else
                     set the htmltext of char  
CharNoForFirstCharOfLine to currChar + 2 of fld id fid to  
HTMLLinePlusExtraWord
                     add 1 to hyphenTries
                   end if
                 end if
               end repeat
             end if
             repeat for each item i in listofSpacesinThisLine
               set the textsize of char i of fld id fid to ts
               if the formattedheight of char  
CharNoForFirstCharOfLine to CharNoForLastSpaceInLine of fld id fid >  
FieldTextHeight then
                 subtract 1 from ts
                 set the textsize of char i of fld id fid to ts --  
ALL YOU NEED TO DO IS BRING DIMINISH ONE SPACE
                 put "done" into status
                 put empty into listofSpacesinThisLine
                 put CharNoForLastSpaceInLine + 1 into  
CharNoForFirstCharOfLine
                 put CharNoForFirstCharOfLine into currChar
                 exit repeat
               end if
             end repeat
             if status = "done" then exit repeat
           end repeat
         end if
       end if
     else
       add 1 to currChar
       if currChar > numberOfCharsInFld then exit repeat
     end if
   end repeat
end justify



More information about the use-livecode mailing list