[ Team LiB ] Previous Section Next Section

Generating Postscript

The postscript operation generates Postscript based on the contents of a canvas. One limitation in earlier versions of Tk is that images and embedded windows are not captured in the Postscript output. As of Tk 8.3, images are included in the generated Postscript. Also, as of Tk 8.3 for Unix and Tk 8.4.1 for Windows, embedded windows are included in the generated Postscript if they are currently displayed on the screen (that is, displayed within the canvas's viewport and not obscured by other windows).

Table 37-13 summarizes all the options for generating Postscript.

Table 37-13. Canvas postscript options

-channel fid

The channel identifier of a channel already opened for writing. The Postscript is written to that channel, and the channel is left open for further writing at the end of the operation. If -channel or -file are not specified, the Postscript is returned as the result of the command.

-colormap varName

The index of varName is a named color, and the contents of each element is the Postscript code to generate the RGB values for that color.

-colormode mode

mode is one of color, grey, or mono.

-file name

The file in which to write the Postscript. If -file or -channel are not specified, the Postscript is returned as the result of the command.

-fontmap varName

The index of varName is an X font name. Each element contains a list of two items: a Postscript font name and a point size.

-height size

Height of the area to print.

-pageanchor anchor

Anchor: c, n, ne, e, se, s, sw, w, or nw.

-pageheight size

Height of image on the output. A floating point number followed by c (centimeters), i (inches), m (millimeters), or p (printer points).

-pagewidth size

Width of image on the output.

-pagex position

The output X coordinate of the anchor point.

-pagey position

The output Y coordinate of the anchor point.

-rotate boolean

If true, rotates so that X axis is the long direction of the page (landscape orientation).

-width size

Width of the area to print.

-x position

Canvas X coordinate of left edge of the image.

-y position

Canvas Y coordinate of top edge of the image.

You control what region of the canvas is printed with the -width, -height, -x, and -y options. You control the size and location of this in the output with the -pageanchor, -pagex, -pagey, -pagewidth, and -pageheight options. The Postscript is written to the file named by the -file option, to a channel already opened for writing whose channel identifier is provided by the -channel option, or it is returned as the value of the postscript canvas operation.

You control fonts with a mapping from X screen fonts to Postscript fonts. Define an array where the index is the name of the X font and the contents are the name and pointsize of a Postscript font.

Example 37-14 positions a number of text objects with different fonts onto a canvas. For each different X font used, it records a mapping to a Postscript font. The example has a fairly simple font mapping, and in fact the canvas would probably have guessed the same font mapping itself. If you use more exotic screen fonts, you may need to help the canvas widget with an explicit font map.

The example positions the output at the upper-left corner of the printed page by using the -pagex, -pagey, and -pageanchor options. Recall that Postscript has its origin at the lower-left corner of the page.

Example 37-14 Generating Postscript from a canvas
proc Setup {} {
   global fontMap
   canvas .c
   pack .c -fill both -expand true
   set x 10
   set y 10
   set last [.c create text $x $y -text "Font sampler" \
      -font fixed -anchor nw]

   # Create several strings in different fonts and sizes

   foreach family {times courier helvetica} {
      set weight bold
      switch -- $family {
         times { set fill blue; set psfont Times}
         courier { set fill green; set psfont Courier }
         helvetica { set fill red; set psfont Helvetica }
      }
      foreach size {10 14 24} {
         set y [expr 4+[lindex [.c bbox $last] 3]]

         # Guard against missing fonts
         if {[catch {.c create text $x $y \
                -text $family-$weight-$size \
                -anchor nw -fill $fill \
                -font -*-$family-$weight-*-*-*-$size-*} \
         it] == 0} {
            set fontMap(-*-$family-$weight-*-*-*-$size-*)\
               [list $psfont $size]
            set last $it
         }
      }
   }
   set fontMap(fixed) [list Courier 12]
}
proc Postscript { c file } {
   global fontMap
   # Tweak the output color
   set colorMap(blue) {0.1 0.1 0.9 setrgbcolor}
   set colorMap(green) {0.0 0.9 0.1 setrgbcolor}
   # Position the text at the upper-left corner of
   # an 8.5 by 11 inch sheet of paper
   $c postscript -fontmap fontMap -colormap colorMap \
      -file $file \
      -pagex 0.i -pagey 11.i -pageanchor nw
}
    [ Team LiB ] Previous Section Next Section