character to justify text#2

Fred moyer fmoyer at aol.com
Wed Jun 27 22:18:35 EDT 2007


Hi:

I saw that I finally had something to contribute to the list and so  
sent a script in -- just 20 minutes ago, and then immediately found a  
bug in it,  plus noticed that some characters might not to paste  
correctly (for example: the character for "is less than or equal  
to".) I've written to the use-revolution-owner to kill the email; but  
if it goes through, please disregard that script! Try this one instead.

It's a low-tech solution to text justification that manipulates the  
textsize of spaces. It doesn't justify on the fly like Brian's script  
(very nice, Brian!) Rather, it justifies a finished field.

Here are some features:
    - doesn't require any special stack preparation (adding images, etc.)
    - preserves textstyles
    - 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. (Note: you might not even see this  
feature unless you are justifying thin text fields.)

I'm not at all a pro at scripting, and I wrote it for my own use, so  
the scripting is pretty tortured, plus I can't guarantee that there  
aren't any more bugs(!) Also, I do encounter issues with text that I  
paste into fields from the Web or even other Mac applications (where  
for example I find what looks like a space is actually numtochar(202).)

But I hope it might be useful to others.

Fred Moyer

---------

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

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 cursor to watch
   lock screen
   set the fixedLineHeight of fld id fid to true
   set the backColor of char 1 to -1 of fld id fid to empty
   put the htmlText of fld id fid into 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 effective 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