Justify text in a field
MisterX
b.xavier at internet.lu
Thu Jul 28 22:36:33 EDT 2005
Al
> I agree, but the ideal could be to use
> unicode fonts, that have many spaces of different width. :-D
>
> <http://www.cs.tut.fi/~jkorpela/chars/spaces.html>
> <http://www.microsoft.com/typography/developers/fdsspec/spaces.htm>
>
I didn't know that! Just learned something!
Here's my old monospace script, im sure you can adjust it for modern times
;)
Please share your improvements to it ;)
Im working on a special editor and this could make it in after all...
cheers
Xavier
on mouseUp
put cd fld "linewidth" into lineWidth
put cd fld "Lmargin" into Lmargin
put cd fld "Rmargin" into Rmargin
put cd fld "Indent" into Indent
put hilite of cd btn "full justification" into fulljustification
if indent<0 then
put Stringfill("L", Lmargin+indent ) into L
else put Stringfill("L", Lmargin ) into L
put Stringfill("R", Rmargin ) into R
put Stringfill("I", abs(indent) ) into I
put Stringfill("-", linewidth-(Lmargin+Rmargin)-indent ) into P
-- LIPR for left right indent and paragraph start
get L & I & P & R
put L & I & P & R into cd fld "Ruler"
put setmonospacestyle(cd fld "input",selectedtext of cd btn
"alignment",lineWidth,\
Lmargin, Rmargin, Indent,fulljustification) into cd fld "output"
-- text you want to limitlength
end mouseUp
function setmonospacestyle
thetext,alignment,Paraglinewidth,LMargin,RMargin,Indent,fulljustification
put Paraglinewidth - (LMargin + RMargin) into linewidth
if LMargin<>0
then put StringFill(" ",LMargin) into LMargintxt
else put "" into LMargintxt
if LMargin<>0
then put StringFill(" ",RMargin) into RMargintxt
else put "" into RMargintxt
if indent>0
then put StringFill(" ",indent) into indenttxt
else put "" into indenttxt
put 0 into thislinelen
put "" into thisline
put "" into output
repeat with y = 1 to the number of lines in thetext
put line y of thetext into theline
put theline <> "" into isindented
if isindented then put indenttxt before thisline
put the number of words in theline into thelinewordcount
repeat with x = 1 to thelinewordcount
set cursor to busy
put word x of theline into thisword
put the length of thisword into thiswordlen
if (isindented and (thislinelen+thiswordlen+1 - indent) > linewidth)
or\
(thislinelen+thiswordlen+1) > linewidth then
if char 1 of thisline = " " then delete char 1 of thisline
if isindented and indent<0 and LMargin>abs(indent)
then put StringFill(" ",LMargin+indent) before thisline
else if LMargin>0 then put LMarginTxt before thisline
if RMargin>0 then put RMarginTxt after thisline
if the number of words in thisline > 0 then
if alignment = "Center" then
put AlignCenterTxtString(thisline,lineWidth) into thisline
else if alignment = "Right" then
put AlignRightTxtString(thisline,lineWidth) into thisline
else if alignment = "Justify" then
if x < thelinewordcount
then put AlignJustifyTxtString(thisline,lineWidth) into thisline
end if
end if
put return & thisline after output
put thisword into thisline
put thiswordlen into thislinelen
put false into isindented
else
if thisline="" then
put thisword into thisline
else put " " & thisword after thisline
put length(thisline) into thislinelen
--add thiswordlen+1 to thislinelen
end if
end repeat
if char 1 of thisline = " " then delete char 1 of thisline
if isindented and indent<0 and LMargin>abs(indent)
then put StringFill(" ",LMargin+indent) before thisline
else if LMargin>0 then put LMarginTxt before thisline
-- if indent<0 and LMargin>indent
-- then put StringFill(" ",LMargin+indent) before thisline
-- else if LMargin>0 then put LMarginTxt before thisline
if RMargin>0 then put RMarginTxt after thisline
if the number of words in thisline > 0 then
if alignment = "Center" then
put AlignCenterTxtString(thisline,lineWidth) into thisline
else if alignment = "Right" then
put AlignRightTxtString(thisline,lineWidth) into thisline
-- you don't justify last line of paragraph unless fullJustified
else if alignment = "Justify" and fulljustification then
put AlignJustifyTxtString(thisline,lineWidth) into thisline
end if
end if
put return & thisline after output
put "" into thisline
put 0 into thislinelen
end repeat
delete char 1 of output
return output
end setmonospacestyle
function StringFill what,anum
if anum=0 then return ""
put "" into out
repeat with x = 1 to anum
put what after out
end repeat
return out
end StringFill
function textmonospaceCenter txt,alen,margin
put "" into thisline
put length(txt) into textlen
-- generate white chars
get (alen-textlen)
repeat it
put " " after thisline
end repeat
put it div 2 into textoffset
put txt after char textoffset of thisline
return thisline
end textmonospaceCenter
--
function AlignCenterTxtString thisline,lineWidth
put linewidth-length(thisline) into diff
put diff div 2 into offspace
put stringfill(" ",offspace) into offsetspacetxt
put offsetspacetxt&thisline&offsetspacetxt into thisline
return thisline
end AlignCenterTxtString
function AlignRightTxtString thisline,lineWidth
repeat while last char of thisline = " "
delete last char of thisline
end repeat
put linewidth-length(thisline) into diff
put stringfill(" ",diff) into offsetspacetxt
put offsetspacetxt&thisline into thisline
return thisline
end AlignRightTxtString
function AlignJustifyTxtString thisline,lineWidth
put length(thisline) into lenline
put linewidth-lenline into diff
if diff>0 then
put the number of words in thisline into linewords
put 0 into wordsep
put 0 into spacesteps
if diff>linewords and linewords>2 then
put linewords into wordsep
put diff div (linewords-1) into spacesteps
end if
else if diff>1 then
put linewords-1 into wordsep
put (linewords div diff-1) into spacesteps
end if
put stringfill(" ",spacesteps) into spacerun
repeat with z = 2 to wordsep --step bystep
if the length of thisline >= linewidth then exit repeat
put spacerun before word z of thisline
end repeat
--end if
return thisline
end AlignJustifyTxtString
More information about the use-livecode
mailing list