www.gibmonks.com




  Previous section   Next section

Practical Programming in Tcl & Tk, Third Edition
By Brent B. Welch

Table of Contents
Chapter 34.  The Canvas Widget


Generating Postscript

The postscript operation generates postscript based on the contents of a canvas. One limitation to note is that embedded windows are not captured in the postscript output. Table 34-12 summarizes all the options for generating postscript:

Table 34-12. Canvas postscript options.
-colormap varNameThe 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 modemode is one of color, grey, or mono.
-file nameThe file in which to write the postscript. If not specified, the postscript is returned as the result of the command.
-fontmap varNameThe 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 sizeHeight of the area to print.
-pageanchor anchorAnchor: c, n, ne, e, se, s, sw, w, or nw.
-pageheight sizeHeight of image on the output. A floating point number followed by c (centimeters), i (inches), m (millimeters), or p (printer points).
-pagewidth sizeWidth of image on the output.
-pagex positionThe output X coordinate of the anchor point.
-pagey positionThe output Y coordinate of the anchor point.
-rotate booleanIf true, rotates so that X axis is the long direction of the page (landscape orientation).
-width sizeWidth of the area to print.
-x positionCanvas X coordinate of left edge of the image.
-y positionCanvas 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, 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.

The next example 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 34-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
}

      Previous section   Next section
    Top