character to justify text -- spacer graphic implementation
Brian Yennie
briany at qldlearning.com
Wed Jun 27 01:16:06 EDT 2007
This uses a naive algorithm and has limitations, but it's a fun
experiment.
Create a scrolling text field and a 1x1 empty image object.
Paste this into the field script and type away.
on keyDown c
lock screen
do "put c into"&& (the selectedChunk)
fullJustify (the id of me), (the id of image "spacer")
unlock screen
end keyDown
on fullJustify fldID, imageID
local i, targetWidth
put fullFieldLineWidth(fldID) into targetWidth
put 1 into i
lock screen
replace numToChar(0) with empty in fld id fldID
put the formattedText of fld id fldID into fld id fldID
repeat for each line l in fld id fldID
put (targetWidth - the formattedWidth of line i of fld id fldID)
into tLineGaps[i]
add 1 to i
end repeat
put 1 into i
repeat for each line l in fld id fldID
put padLine(l, tLineGaps[i], numToChar(0)) into line i of fld id
fldID
add 1 to i
end repeat
put 1 into i
repeat for each char c in fld id fldID
if (char i of fld 1 is numToChar(0)) then
set the imageSource of char i of fld id fldID to imageID
end if
add 1 to i
end repeat
unlock screen
end fullJustify
function fullFieldLineWidth fldID
put (the width of fld id fldID) into targetWidth
subtract (2* the borderWidth of fld id fldID) from targetWidth
subtract (item 1 of the margins of fld id fldID) from targetWidth
get (item 3 of the margins of fld id fldID)
if (it is empty) then subtract (item 1 of the margins of fld id
fldID) from targetWidth
else subtract (item 3 of the margins of fld id fldID) from
targetWidth
if (the vScrollbar of fld id fldID) then subtract the
scrollbarWidth of fld id fldID from targetWidth
return targetWidth
end fullFieldLineWidth
function padLine l, pixels
put empty into tmp1
put 0 into spaceCount
repeat for each char c in l
if (c is space) then add 1 to spaceCount
end repeat
if (spaceCount = 0) then return l
put (pixels div spaceCount) into spacePadding
put (pixels mod spaceCount) into leftoverPads
repeat for each char c in l
if (c is space) then
repeat spacePadding
put numToChar(0) after tmp1
end repeat
end if
put c after tmp1
end repeat
repeat for each char c in tmp1
if (c is space) AND (leftoverPads > 0) then
put numToChar(0) after tmp2
subtract 1 from leftoverPads
end if
put c after tmp2
end repeat
return tmp2
end padLine
More information about the use-livecode
mailing list