blob: 6f7881f4b9b717ef4919b0604201677cb76f44b7 [file] [log] [blame]
Jack Jansen5073b371998-04-27 15:06:50 +00001import Printing
2import Qd
3import Fm
4import Res
5
6# some constants
7PostScriptBegin = 190 # Set driver state to PostScript
8PostScriptEnd = 191 # Restore QuickDraw state
9PostScriptHandle = 192 # PostScript data referenced in handle
10
11CHUNK_SIZE = 0x8000 # max size of PicComment
12
13def PostScript(text):
14 """embed text as plain PostScript in print job."""
15 handle = Res.Resource('')
16 Qd.PicComment(PostScriptBegin, 0, handle)
17 while text:
18 chunk = text[:CHUNK_SIZE]
19 text = text[CHUNK_SIZE:]
20 handle.data = chunk
21 Qd.PicComment(PostScriptHandle, len(chunk), handle)
22 handle.data = ''
23 Qd.PicComment(PostScriptEnd, 0, handle)
24
25# create a new print record
26printrecord = Printing.NewTPrintRecord()
27
28# open the printer
29Printing.PrOpen()
30try:
31 # initialize print record with default values
32 Printing.PrintDefault(printrecord)
33
34 # page setup, ok is 0 when user cancelled
35 ok = Printing.PrStlDialog(printrecord)
36 if not ok:
37 raise KeyboardInterrupt
38 # at this stage, you should save the print record in your document for later
39 # reference.
40
41 # print job dialog, ok is 0 when user cancelled
42 ok = Printing.PrJobDialog(printrecord)
43 if not ok:
44 raise KeyboardInterrupt
45
46 # once per document
47 port = Printing.PrOpenDoc(printrecord)
48 # port is the Printer's GrafPort, it is also the current port, so no need to Qd.SetPort(port)
49 try:
50 # start printing a page
51 # XXX should really look up what pages to print by
52 # inspecting the print record.
53 Printing.PrOpenPage(port, None)
54 try:
55 # use QuickDraw like in any other GrafPort
56 Qd.FrameRect((10, 250, 100, 500))
57 Qd.FrameRect((10, 510, 100, 600))
58 Qd.MoveTo(10, 100)
59 Qd.TextSize(50)
60 Qd.TextFont(Fm.GetFNum("Helvetica"))
61 Qd.DrawString("It rreally works!")
62 Qd.MoveTo(10, 150)
63 Qd.TextSize(20)
64 Qd.DrawString("(and now for a little PostScript...)")
65
66 # example PostScript code
67 ps = """
68 % the coordinate system is the quickdraw one, which is flipped
69 % compared to the default PS one. That means text will appear
70 % flipped when used directly from PostScript.
71 % As an example we start by defining a custom scalefont operator
72 % that corrects this.
73 /myscalefont{[exch 0 0 2 index neg 0 0]makefont}def
74 0.75 setgray
75 0 0 moveto
76 0 30 lineto 10000 30 lineto
77 10000 0 lineto closepath fill
78 0 setgray
79 5 25 moveto /Courier findfont 20 myscalefont setfont
80 (Printed with PostScript!) show
81 2 setlinewidth [10 10 5 10] 0 setdash 5 5 moveto 400 0 rlineto stroke
82 """
83 # embed the PostScript code in the print job
84 PostScript(ps)
85 finally:
86 # when done with the page
87 Printing.PrClosePage(port)
88 finally:
89 # when done with the document
90 Printing.PrCloseDoc(port)
91finally:
92 # when done printing
93 Printing.PrClose()