| [ Team LiB ] |
|
Generating PostscriptThe 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.
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 ] |
|