Center text within a field?

Trevor DeVore lists at mangomultimedia.com
Wed Jun 6 15:31:07 EDT 2007


On Jun 6, 2007, at 3:43 AM, Trevor DeVore wrote:

> I'm trying to center text in a field within a graphic (circle). The  
> text can be any font and any size.

Update - I haven't been able to find a reliable means of calculating  
the center of text using any field properties so I resorted to  
exporting a snapshot of the field (opaque = false) to an image,  
grabbing the alphadata of the image and determining extreme left,  
top, right, bottom of the text by looping through it.

I've included the function for anyone who is interested in taking a  
look. It seems to be working but I'm severely sleep deprived right  
now so who knows :-) This function is part of a group script that has  
one field and one invisible image.

function FindCenterOfText
     local theAlphaData,theByte,theColumnCount,theColumnNo
     local theOpaque,theRect,theRowNo
     local theValue

     lock screen
     put the opaque of field 1 of me into theOpaque
     set the opaque of field 1 of me to false

     ## CREATE IMAGE WITH ALPHADATA
     export snapshot from field 1 of me to image 1 of me as PNG
     put the alphadata of image 1 of me into theAlphaData
     set the text of image 1 of me to empty

     ## DEFINE OUR GRID
     put the width of image 1 of me into theColumnCount

     ## LOOP THROUGH ALPHA DATA LOOKING FOR PIXELS THAT MEET THE  
VISIBILITY THRESHOLD
     put 1 into theRowNo
     put 0 into theColumnNo
     put 0,0,0,0 into theRect

     repeat for each char theByte in theAlphaData
         add 1 to theColumnNo

         put chartonum(theByte) into theValue
         if theValue > 100 then
             if item 1 of theRect is 0 then
                 put theColumnNo into item 1 of theRect
                 put theRowNo into item 2 of theRect
                 put theColumnNo into item 3 of theRect
                 put theRowNo into item 4 of theRect
             end IF

             put min(theColumnNo, item 1 of theRect) into item 1 of  
theRect
             put min(theRowNo, item 2 of theRect) into item 2 of theRect
             put max(theColumnNo, item 3 of theRect) into item 3 of  
theRect
             put max(theRowNo, item 4 of theRect) into item 4 of theRect
         end if

         if theColumnNo is theColumnCount then
             add 1 to theRowNo
             put 0 into theColumnNo
         end if
     end repeat

     set the opaque of field 1 of me to theOpaque
     unlock screen

     put item 1 of theRect + (item 3 of theRect - item 1 of  
theRect) / 2 into item 1 of theLoc
     put item 2 of theRect + (item 4 of theRect - item 2 of  
theRect) / 2 into item 2 of theLoc

     ## YEAH, THIS IS FOR GETTING THE FLOOR
     replace comma with cr in theLoc
     set the itemdelimiter to "."
     delete item 2 of line 1 of theLoc
     delete item 2 of line 2 of theLoc
     replace cr with comma in theLoc
     set the itemdelimiter to comma

     add the left of field 1 of me to item 1 of theLoc
     add the top of field 1 of me to item 2 of theLoc
     return theLoc
end FindCenterOfText


-- 
Trevor DeVore
Blue Mango Learning Systems
www.bluemangolearning.com    -    www.screensteps.com
trevor at bluemangolearning.com





More information about the use-livecode mailing list