Drawing Text

The create composition element command which is described in the Drawing shapes documentation section can also be used to draw text. The text drawing functionality of create composition element is fairly basic but still quite powerful.

Available functionality:

The first example draws some text using the bottom right corner of the text as the reference point. Panther users can open the following script in a new Script Editor window by clicking here.

on run
  
tell application "iMagine Photo"
    
set thisDocument to make new window document with properties {dimensions:{800, 600}, name:"Drawing Window"}
    
tell thisDocument to create composition element with properties {class:standard text, font:"Monaco", font size:36, drawing text:"This is the text to draw", start from:bottom right, start point:{800, 600}}
  
end tell
end run

The second example draws some text using the centre of the text as the reference point, and positions the text at the bottom horizontally centred. The text is drawn over a image selected by the user, and is drawn semi-transparent. To draw the text partially transparent the blend graphics mode is used, and with an opcolor of {32767, 32767, 32767} which is halfway between {0, 0, 0} and {65535, 65535, 65535} blends half the text with half of the image. To calculate the vertical position of the centre of the text the get text dimensions command is used. Panther users can open the following script in a new Script Editor window by clicking here.

on run
  
set blueColor to {0, 0, 32767}
  
set redColor to {32767, 0, 0}
  
set thisFile to choose file with prompt "Select an image to draw text on: "
  
tell application "iMagine Photo"
    
set thisImporter to import graphic thisFile
    
if the component error of thisImporter is not equal to 0 then
      
close thisImporter
      
return
    
end if
    
set theWidth to 800
    
set theHeight to 600
    
set thisDocument to make new window document with properties {dimensions:{theWidth, theHeight}, name:"Drawing Window"}
    
set the drawing destination of thisImporter to thisDocument
    
set the destination rectangle of thisImporter to {0, 0, theWidth, theHeight}
    
draw thisImporter
    
close thisImporter
    
set textRecord to {class:standard text, font:"Monaco", font size:36, drawing text:"This is the text to draw", start from:centre, start point:{theWidth div 2, theHeight div 2}, color:blueColor, background color:redColor, graphics mode:blend, opcolor:{32767, 32767, 32767}}
    
set {textWidth, textHeight} to get text dimensions with properties textRecord
    
set start point of textRecord to {theWidth div 2, theHeight - (textHeight div 2)}
    
tell thisDocument to create composition element with properties textRecord
  
end tell
end run

The third example draws text on the right edge of the image centred vertically and rotated 90 degrees anti-clockwise. The white text is drawn with no background and this is achieved by using the add pin graphics mode. The add pin graphics mode adds the source and destination pixels together up to a maxumim value specified by opcolor. In this example opcolor is set to every color being fully saturated (white) and the text is to be drawn fully saturated while the background is black or fully unsaturated. Panther users can open the following script in a new Script Editor window by clicking here.

on run
  
set whiteColor to {65535, 65535, 65535}
  
set blackColor to {0, 0, 0}
  
set thisFile to choose file with prompt "Select an image to draw text on: "
  
tell application "iMagine Photo"
    
set thisImporter to import graphic thisFile
    
if the component error of thisImporter is not equal to 0 then
      
close thisImporter
      
return
    
end if
    
set theWidth to 800
    
set theHeight to 600
    
set thisDocument to make new window document with properties {dimensions:{theWidth, theHeight}, name:"Drawing Window"}
    
set the drawing destination of thisImporter to thisDocument
    
set the destination rectangle of thisImporter to {0, 0, theWidth, theHeight}
    
draw thisImporter
    
close thisImporter
    
set textRecord to {class:standard text, font:"Palatino", font size:36, drawing text:"This is the text to draw", start from:centre, start point:{theWidth div 2, theHeight div 2}, color:whiteColor, background color:blackColor, graphics mode:add pin, opcolor:{65535, 65535, 65535}, rotation:-90}
    
set {textWidth, textHeight} to get text dimensions with properties textRecord
    
set start point of textRecord to {theWidth - (textWidth div 2), theHeight div 2}
    
tell thisDocument to create composition element with properties textRecord
  
end tell
end run

To draw black text then you set the graphics mode to sub pin and opcolor to {0, 0, 0} in the above script and the composition element command will subtract the white text from the destination pixels limiting the minimum value to that specified by opcolor.

The fourth example draws three lines of text using the same font and at the same font size. The second and third line have the font style set to bold, and the third line has the color set to red and the background color to green. Panther users can open the following script in a new Script Editor window by clicking here.

on run
  
set redColor to {65535, 0, 0}
  
set greenColor to {0, 65535, 0}
  
tell application "iMagine Photo"
    
set thisDocument to make new window document with properties {dimensions:{800, 600}, name:"Drawing Window"}
    
tell thisDocument to create composition element with properties {class:standard text, font:"Verdana", font size:36, drawing text:"This is the text to draw", start from:top left, start point:{0, 0}}
    
tell thisDocument to create composition element with properties {class:standard text, font:"Verdana", font size:36, drawing text:"This is the text to draw", start from:top left, start point:{0, 38}, font style:bold}
    
tell thisDocument to create composition element with properties {class:standard text, font:"Verdana", font size:36, drawing text:"This is the text to draw", start from:top left, start point:{0, 76}, font style:bold, color:redColor, background color:greenColor}
  
end tell
end run

The ability to open scripts in a new Script Editor window is provided by an application called "Convert Script to Markup Code" and can be obtained from http://homepage.mac.com/jonn8/as/

keywords: AppleScript, Apple Script, text, Macintosh, Quartz, Quickdraw