What is up with FormattedHeight?

BNig niggemann at uni-wh.de
Fri Feb 10 16:31:35 EST 2012


Hi Scott,

thanks for posting your script which is as you say based on a script by
Trevor DeVore.

Since Howard wanted to use this on iOS in a pinch/zoom script I tried to
speed up your script.

More specifically I scan the alphaData line by line for non-null bytes from
top to bottom and from bottom to top. 
If a non-null byte is found in the alphaData the script gets the row and
bails out. 

Likewise from left to right and right to left the script scans the columns
and as soon as a column contains a charToNum > 5 it gets the column number
and bails out. The advantage is that I don't have to scan every byte of the
alphaData which reduces computation time.


here is the script: (watch out for linebreaks)
-------------------------------------------------------------------
function bnTextRect pField
   -- pField expects a long id of a field whose opaque is false
   -- and returns the bounding rect of the actual letters in the field
   -- which is different from the formattedRect
   
   -- based on a script by Scott Rossi
   --
http://runtime-revolution.278305.n4.nabble.com/What-is-up-with-FormattedHeight-tt4360344.html#a4372671
   
   
   lock screen
   ## CREATE IMAGE WITH ALPHADATA
   reset the templateImage
   set lineSize of the templateImage to 0
   create image
   put long id of it into tempImage
   do "export snapshot from" && pField && "to" && tempImage && "as PNG"
   put alphadata of tempImage into theAlphaData
   ## DEFINE GRID
   put width of tempImage into theColumnCount
   put the height of tempImage into theRowCount
   delete tempImage
   reset the templateImage
   unlock screen
   
   
   ## LOOP THROUGH ALPHA DATA LOOKING FOR
   ## PIXELS THAT MEET VISIBILITY THRESHOLD (> 5) (right and left)
   ## or lines that contain non null = threshold > 0 characters (top and
bottom)
   
   
   ## make a variable (taBlancLine) the size of the width of the image,
filled with binary null to compare it to the alphaData later
   put numToChar(0) into tNull
   put "" into taBlancLIne
   repeat theColumnCount
      put tNull after taBlancLine
   end repeat
   
   -- tRealRect will hold the bounding rect of the letters
   put 0,0,0,0 into tRealRect
   
   -- go by row from top to bottom, if a row is not all null in the
alphaData then get the row number and bail out
   repeat with i = 0 to theRowCount - 1
      if char (theColumnCount * i +1) to (theColumnCount * i +
theColumnCount) of theAlphaData  <> taBlancLIne  then
         put i  into item 2 of tRealRect
         exit repeat
      end if
   end repeat
   
   -- go by row from bottom to top, if a row is not all null in the
alphaData then get the row number and bail out
   repeat with i = theRowCount - 1 down to 0
      if char (theColumnCount * i +1) to (theColumnCount * i +
theColumnCount) of theAlphaData <> taBlancLIne  then
         put i +1 into item 4 of tRealRect
         exit repeat
      end if
   end repeat
   
   -- get the first alphapixel > 5 from  left to the right, get the column
number and get out
   put false into tExit
   repeat with i = 1 to theColumnCount 
      repeat with j = 0 to theRowCount - 1
         if charToNum(char i + (j * theColumnCount) of theAlphaData) > 5
then
            put i -1 into item 1 of tRealRect
            put true into tExit
            exit repeat
         end if
      end repeat
      if tExit then exit repeat
   end repeat
   
   -- get the first alphapixel > 5 from  right to left, get the column
number and get out
   put false into tExit
   repeat with i = theColumnCount  down to 1
      repeat with j = theRowCount - 1 down to 0
         if charToNum(char i + (j * theColumnCount) of theAlphaData) > 5
then
            put i - 1 into item 3 of tRealRect
            put true into tExit
            exit repeat
         end if
      end repeat
      if tExit then exit repeat
   end repeat
   
   put the left of pField into tLeft
   add tLeft to item 1 of tRealRect
   add tLeft to item 3 of tRealRect
   
   put the top of pField into tTop
   add tTop to item 2 of tRealRect
   add tTop to item 4 of tRealRect
   
   return tRealRect
   
end bnTextRect
---------------------------------------------

Kind regards

Bernd

--
View this message in context: http://runtime-revolution.278305.n4.nabble.com/What-is-up-with-FormattedHeight-tp4360344p4377624.html
Sent from the Revolution - User mailing list archive at Nabble.com.




More information about the use-livecode mailing list