(back_comment): Handle 2-char comment starts
[bpt/emacs.git] / lisp / ps-print.el
CommitLineData
535efc38 1;;; ps-print.el --- Print text from the buffer as PostScript
12d89a2e 2
27f20fc1 3;; Copyright (C) 1993, 1994, 1995, 1996, 1997 Free Software Foundation, Inc.
ef2cbb24 4
090be653 5;; Author: Jim Thompson (was <thompson@wg2.waii.com>)
87a16a06 6;; Author: Jacques Duthen <duthen@cegelec-red.fr>
27f20fc1 7;; Maintainer: Vinicius Jose Latorre <vinicius@cpqd.br>
090be653 8;; Keywords: print, PostScript
a18ed129
RS
9;; Time-stamp: <97/08/27 13:00:37 vinicius>
10;; Version: 3.05.1
090be653 11
a18ed129
RS
12(defconst ps-print-version "3.05.1"
13 "ps-print.el, v 3.05.1 <97/08/24 vinicius>
090be653 14
535efc38 15Vinicius's last change version -- this file may have been edited as part of
090be653
RS
16Emacs without changes to the version number. When reporting bugs,
17please also report the version of Emacs, if any, that ps-print was
18distributed with.
19
20Please send all bug fixes and enhancements to
87a16a06 21 Jacques Duthen <duthen@cegelec-red.fr>.
090be653 22")
ef2cbb24 23
86c10ecb 24;; This file is part of GNU Emacs.
ef2cbb24
RS
25
26;; GNU Emacs is free software; you can redistribute it and/or modify
27;; it under the terms of the GNU General Public License as published by
28;; the Free Software Foundation; either version 2, or (at your option)
29;; any later version.
30
31;; GNU Emacs is distributed in the hope that it will be useful,
32;; but WITHOUT ANY WARRANTY; without even the implied warranty of
33;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
34;; GNU General Public License for more details.
35
36;; You should have received a copy of the GNU General Public License
b578f267
EN
37;; along with GNU Emacs; see the file COPYING. If not, write to the
38;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
39;; Boston, MA 02111-1307, USA.
ef2cbb24 40
12d89a2e 41;;; Commentary:
ef2cbb24
RS
42
43;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
44;;
12d89a2e 45;; About ps-print
ef2cbb24 46;; --------------
bcc0d457 47;;
ef2cbb24
RS
48;; This package provides printing of Emacs buffers on PostScript
49;; printers; the buffer's bold and italic text attributes are
50;; preserved in the printer output. Ps-print is intended for use with
00aa16af
RS
51;; Emacs 19 or Lucid Emacs, together with a fontifying package such as
52;; font-lock or hilit.
12d89a2e 53;;
87a16a06
RS
54;; ps-print uses the same face attributes defined through font-lock or hilit
55;; to print a PostScript file, but some faces are better seeing on the screen
56;; than on paper, specially when you have a black/white PostScript printer.
57;;
58;; ps-print allows a remap of face to another one that it is better to print,
59;; for example, the face font-lock-comment-face (if you are using font-lock)
60;; could have bold or italic attribute when printing, besides foreground color.
61;; This remap improves printing look (see How Ps-Print Maps Faces).
62;;
bcc0d457 63;;
12d89a2e 64;; Using ps-print
ef2cbb24 65;; --------------
ef2cbb24 66;;
12d89a2e
RS
67;; The Commands
68;;
69;; Ps-print provides eight commands for generating PostScript images
70;; of Emacs buffers:
71;;
72;; ps-print-buffer
73;; ps-print-buffer-with-faces
74;; ps-print-region
75;; ps-print-region-with-faces
76;; ps-spool-buffer
77;; ps-spool-buffer-with-faces
78;; ps-spool-region
79;; ps-spool-region-with-faces
80;;
81;; These commands all perform essentially the same function: they
82;; generate PostScript images suitable for printing on a PostScript
83;; printer or displaying with GhostScript. These commands are
84;; collectively referred to as "ps-print- commands".
85;;
86;; The word "print" or "spool" in the command name determines when the
87;; PostScript image is sent to the printer:
ef2cbb24 88;;
12d89a2e
RS
89;; print - The PostScript image is immediately sent to the
90;; printer;
ef2cbb24 91;;
12d89a2e
RS
92;; spool - The PostScript image is saved temporarily in an
93;; Emacs buffer. Many images may be spooled locally
94;; before printing them. To send the spooled images
bcc0d457 95;; to the printer, use the command `ps-despool'.
ef2cbb24 96;;
12d89a2e
RS
97;; The spooling mechanism was designed for printing lots of small
98;; files (mail messages or netnews articles) to save paper that would
99;; otherwise be wasted on banner pages, and to make it easier to find
100;; your output at the printer (it's easier to pick up one 50-page
101;; printout than to find 50 single-page printouts).
06fb6aab 102;;
bcc0d457 103;; Ps-print has a hook in the `kill-emacs-hooks' so that you won't
a7acbbe4 104;; accidentally quit from Emacs while you have unprinted PostScript
12d89a2e
RS
105;; waiting in the spool buffer. If you do attempt to exit with
106;; spooled PostScript, you'll be asked if you want to print it, and if
107;; you decline, you'll be asked to confirm the exit; this is modeled
108;; on the confirmation that Emacs uses for modified buffers.
109;;
110;; The word "buffer" or "region" in the command name determines how
111;; much of the buffer is printed:
112;;
113;; buffer - Print the entire buffer.
114;;
115;; region - Print just the current region.
116;;
117;; The -with-faces suffix on the command name means that the command
118;; will include font, color, and underline information in the
119;; PostScript image, so the printed image can look as pretty as the
120;; buffer. The ps-print- commands without the -with-faces suffix
121;; don't include font, color, or underline information; images printed
122;; with these commands aren't as pretty, but are faster to generate.
123;;
124;; Two ps-print- command examples:
125;;
126;; ps-print-buffer - print the entire buffer,
127;; without font, color, or
128;; underline information, and
129;; send it immediately to the
130;; printer.
131;;
132;; ps-spool-region-with-faces - print just the current region;
133;; include font, color, and
134;; underline information, and
135;; spool the image in Emacs to
136;; send to the printer later.
137;;
138;;
139;; Invoking Ps-Print
bcc0d457 140;; -----------------
ef2cbb24 141;;
12d89a2e 142;; To print your buffer, type
ef2cbb24 143;;
12d89a2e 144;; M-x ps-print-buffer
ef2cbb24 145;;
12d89a2e
RS
146;; or substitute one of the other seven ps-print- commands. The
147;; command will generate the PostScript image and print or spool it as
148;; specified. By giving the command a prefix argument
149;;
150;; C-u M-x ps-print-buffer
151;;
152;; it will save the PostScript image to a file instead of sending it
153;; to the printer; you will be prompted for the name of the file to
154;; save the image to. The prefix argument is ignored by the commands
155;; that spool their images, but you may save the spooled images to a
bcc0d457 156;; file by giving a prefix argument to `ps-despool':
12d89a2e
RS
157;;
158;; C-u M-x ps-despool
159;;
bcc0d457 160;; When invoked this way, `ps-despool' will prompt you for the name of
12d89a2e
RS
161;; the file to save to.
162;;
bcc0d457
RS
163;; Any of the `ps-print-' commands can be bound to keys; I recommend
164;; binding `ps-spool-buffer-with-faces', `ps-spool-region-with-faces',
165;; and `ps-despool'. Here are the bindings I use on my Sun 4 keyboard:
12d89a2e
RS
166;;
167;; (global-set-key 'f22 'ps-spool-buffer-with-faces) ;f22 is prsc
ef2cbb24
RS
168;; (global-set-key '(shift f22) 'ps-spool-region-with-faces)
169;; (global-set-key '(control f22) 'ps-despool)
170;;
12d89a2e
RS
171;;
172;; The Printer Interface
bcc0d457 173;; ---------------------
12d89a2e 174;;
bcc0d457 175;; The variables `ps-lpr-command' and `ps-lpr-switches' determine what
12d89a2e 176;; command is used to send the PostScript images to the printer, and
bcc0d457
RS
177;; what arguments to give the command. These are analogous to
178;; `lpr-command' and `lpr-switches'.
87a16a06 179;;
bcc0d457
RS
180;; Make sure that they contain appropriate values for your system;
181;; see the usage notes below and the documentation of these variables.
182;;
183;; NOTE: `ps-lpr-command' and `ps-lpr-switches' take their initial values
184;; from the variables `lpr-command' and `lpr-switches'. If you have
185;; `lpr-command' set to invoke a pretty-printer such as `enscript',
186;; then ps-print won't work properly. `ps-lpr-command' must name
12d89a2e
RS
187;; a program that does not format the files it prints.
188;;
189;;
bcc0d457
RS
190;; The Page Layout
191;; ---------------
12d89a2e 192;;
bcc0d457
RS
193;; All dimensions are floats in PostScript points.
194;; 1 inch == 2.54 cm == 72 points
195;; 1 cm == (/ 1 2.54) inch == (/ 72 2.54) points
12d89a2e 196;;
bcc0d457
RS
197;; The variable `ps-paper-type' determines the size of paper ps-print
198;; formats for; it should contain one of the symbols:
199;; `a4' `a3' `letter' `legal' `letter-small' `tabloid'
200;; `ledger' `statement' `executive' `a4small' `b4' `b5'
12d89a2e 201;;
bcc0d457
RS
202;; The variable `ps-landscape-mode' determines the orientation
203;; of the printing on the page:
204;; nil means `portrait' mode, non-nil means `landscape' mode.
205;; There is no oblique mode yet, though this is easy to do in ps.
87a16a06 206;;
bcc0d457
RS
207;; In landscape mode, the text is NOT scaled: you may print 70 lines
208;; in portrait mode and only 50 lignes in landscape mode.
209;; The margins represent margins in the printed paper:
210;; the top margin is the margin between the top of the page
211;; and the printed header, whatever the orientation is.
043620f4 212;;
bcc0d457
RS
213;; The variable `ps-number-of-columns' determines the number of columns
214;; both in landscape and portrait mode.
215;; You can use:
216;; - (the standard) one column portrait mode
217;; - (my favorite) two columns landscape mode (which spares trees)
218;; but also
219;; - one column landscape mode for files with very long lines.
220;; - multi-column portrait or landscape mode
12d89a2e 221;;
12d89a2e 222;;
bcc0d457
RS
223;; Horizontal layout
224;; -----------------
12d89a2e 225;;
bcc0d457
RS
226;; The horizontal layout is determined by the variables
227;; `ps-left-margin' `ps-inter-column' `ps-right-margin'
228;; as follows:
12d89a2e 229;;
bcc0d457
RS
230;; ------------------------------------------
231;; | | | | | | | |
232;; | lm | text | ic | text | ic | text | rm |
233;; | | | | | | | |
234;; ------------------------------------------
12d89a2e 235;;
bcc0d457
RS
236;; If `ps-number-of-columns' is 1, `ps-inter-column' is not relevant.
237;; Usually, lm = rm > 0 and ic = lm
238;; If (ic < 0), the text of adjacent columns can overlap.
12d89a2e 239;;
12d89a2e 240;;
bcc0d457
RS
241;; Vertical layout
242;; ---------------
243;;
244;; The vertical layout is determined by the variables
245;; `ps-bottom-margin' `ps-top-margin' `ps-header-offset'
246;; as follows:
247;;
248;; |--------| |--------|
249;; | tm | | tm |
250;; |--------| |--------|
251;; | header | | |
252;; |--------| | |
253;; | ho | | |
254;; |--------| or | text |
255;; | | | |
256;; | text | | |
257;; | | | |
258;; |--------| |--------|
259;; | bm | | bm |
260;; |--------| |--------|
261;;
262;; If `ps-print-header' is nil, `ps-header-offset' is not relevant.
263;; The margins represent margins in the printed paper:
264;; the top margin is the margin between the top of the page
265;; and the printed header, whatever the orientation is.
12d89a2e
RS
266;;
267;;
268;; Headers
bcc0d457 269;; -------
12d89a2e 270;;
bcc0d457 271;; Ps-print can print headers at the top of each column; the default
12d89a2e
RS
272;; headers contain the following four items: on the left, the name of
273;; the buffer and, if the buffer is visiting a file, the file's
bcc0d457
RS
274;; directory; on the right, the page number and date of printing.
275;; The default headers look something like this:
12d89a2e
RS
276;;
277;; ps-print.el 1/21
278;; /home/jct/emacs-lisp/ps/new 94/12/31
06fb6aab 279;;
12d89a2e 280;; When printing on duplex printers, left and right are reversed so
bcc0d457 281;; that the page numbers are toward the outside (cf. `ps-spool-duplex').
12d89a2e 282;;
bcc0d457
RS
283;; Headers are configurable:
284;; To turn them off completely, set `ps-print-header' to nil.
285;; To turn off the header's gaudy framing box,
286;; set `ps-print-header-frame' to nil.
287;;
288;; The font family and size of text in the header are determined
06fb6aab 289;; by the variables `ps-header-font-family', `ps-header-font-size' and
bcc0d457
RS
290;; `ps-header-title-font-size' (see below).
291;;
292;; The variable `ps-header-line-pad' determines the portion of a header
293;; title line height to insert between the header frame and the text
294;; it contains, both in the vertical and horizontal directions:
295;; .5 means half a line.
296
297;; Page numbers are printed in `n/m' format, indicating page n of m pages;
298;; to omit the total page count and just print the page number,
299;; set `ps-show-n-of-n' to nil.
12d89a2e
RS
300;;
301;; The amount of information in the header can be changed by changing
bcc0d457 302;; the number of lines. To show less, set `ps-header-lines' to 1, and
12d89a2e 303;; the header will show only the buffer name and page number. To show
bcc0d457 304;; more, set `ps-header-lines' to 3, and the header will show the time of
12d89a2e
RS
305;; printing below the date.
306;;
307;; To change the content of the headers, change the variables
bcc0d457
RS
308;; `ps-left-header' and `ps-right-header'.
309;; These variables are lists, specifying top-to-bottom the text
310;; to display on the left or right side of the header.
311;; Each element of the list should be a string or a symbol.
312;; Strings are inserted directly into the PostScript arrays,
313;; and should contain the PostScript string delimiters '(' and ')'.
12d89a2e
RS
314;;
315;; Symbols in the header format lists can either represent functions
316;; or variables. Functions are called, and should return a string to
317;; show in the header. Variables should contain strings to display in
318;; the header. In either case, function or variable, the PostScript
a7acbbe4 319;; string delimiters are added by ps-print, and should not be part of
12d89a2e
RS
320;; the returned value.
321;;
322;; Here's an example: say we want the left header to display the text
323;;
324;; Moe
325;; Larry
326;; Curly
327;;
328;; where we have a function to return "Moe"
329;;
330;; (defun moe-func ()
331;; "Moe")
332;;
333;; a variable specifying "Larry"
334;;
335;; (setq larry-var "Larry")
336;;
bcc0d457 337;; and a literal for "Curly". Here's how `ps-left-header' should be
12d89a2e
RS
338;; set:
339;;
340;; (setq ps-left-header (list 'moe-func 'larry-var "(Curly)"))
341;;
342;; Note that Curly has the PostScript string delimiters inside his
bcc0d457 343;; quotes -- those aren't misplaced lisp delimiters!
87a16a06 344;;
bcc0d457
RS
345;; Without them, PostScript would attempt to call the undefined
346;; function Curly, which would result in a PostScript error.
87a16a06 347;;
bcc0d457
RS
348;; Since most printers don't report PostScript errors except by
349;; aborting the print job, this kind of error can be hard to track down.
87a16a06 350;;
bcc0d457 351;; Consider yourself warned!
12d89a2e
RS
352;;
353;;
354;; Duplex Printers
bcc0d457 355;; ---------------
12d89a2e
RS
356;;
357;; If you have a duplex-capable printer (one that prints both sides of
bcc0d457
RS
358;; the paper), set `ps-spool-duplex' to t.
359;; Ps-print will insert blank pages to make sure each buffer starts
360;; on the correct side of the paper.
361;; Don't forget to set `ps-lpr-switches' to select duplex printing
362;; for your printer.
363;;
06fb6aab 364;;
87a16a06
RS
365;; Line Number
366;; -----------
367;;
a18ed129
RS
368;; The variable `ps-line-number' specifies whether to number each line;
369;; non-nil means do so. The default is nil (don't number each line).
87a16a06
RS
370;;
371;;
372;; Zebra Stripes
373;; -------------
374;;
a18ed129
RS
375;; Zebra stripes are a kind of background that appear "underneath" the text
376;; and can make the text easier to read. They look like this:
87a16a06
RS
377;;
378;; XXXXXXXXXXXXXXXXXXXXXXXX
379;; XXXXXXXXXXXXXXXXXXXXXXXX
535efc38
RS
380;; XXXXXXXXXXXXXXXXXXXXXXXX
381;;
87a16a06
RS
382;;
383;;
384;; XXXXXXXXXXXXXXXXXXXXXXXX
385;; XXXXXXXXXXXXXXXXXXXXXXXX
535efc38 386;; XXXXXXXXXXXXXXXXXXXXXXXX
87a16a06 387;;
06fb6aab 388;; The blocks of X's represent rectangles filled with a light gray color.
a18ed129
RS
389;; Each rectangle extends all the way across the page.
390;;
391;; The height, in lines, of each rectangle is controlled by
535efc38
RS
392;; the variable `ps-zebra-stripe-height', which is 3 by default.
393;; The distance between stripes equals the height of a stripe.
394;;
01961237 395;; The variable `ps-zebra-stripes' controls whether to print zebra stripes.
a18ed129
RS
396;; Non-nil means yes, nil means no. The default is nil.
397;;
398;; See also section How Ps-Print Has A Text And/Or Image On Background.
87a16a06 399;;
87a16a06 400;;
bcc0d457
RS
401;; Font managing
402;; -------------
403;;
404;; Ps-print now knows rather precisely some fonts:
405;; the variable `ps-font-info-database' contains information
406;; for a list of font families (currently mainly `Courier' `Helvetica'
407;; `Times' `Palatino' `Helvetica-Narrow' `NewCenturySchlbk').
408;; Each font family contains the font names for standard, bold, italic
409;; and bold-italic characters, a reference size (usually 10) and the
410;; corresponding line height, width of a space and average character width.
06fb6aab 411;;
bcc0d457
RS
412;; The variable `ps-font-family' determines which font family
413;; is to be used for ordinary text.
414;; If its value does not correspond to a known font family,
415;; an error message is printed into the `*Messages*' buffer,
416;; which lists the currently available font families.
417;;
418;; The variable `ps-font-size' determines the size (in points)
419;; of the font for ordinary text, when generating Postscript.
420;; Its value is a float.
421;;
422;; Similarly, the variable `ps-header-font-family' determines
423;; which font family is to be used for text in the header.
424;; The variable `ps-header-font-size' determines the font size,
425;; in points, for text in the header.
426;; The variable `ps-header-title-font-size' determines the font size,
427;; in points, for the top line of text in the header.
428;;
429;;
430;; Adding a new font family
431;; ------------------------
432;;
433;; To use a new font family, you MUST first teach ps-print
87a16a06 434;; this font, i.e., add its information to `ps-font-info-database',
bcc0d457
RS
435;; otherwise ps-print cannot correctly place line and page breaks.
436;;
87a16a06 437;; For example, assuming `Helvetica' is unknown,
bcc0d457
RS
438;; you first need to do the following ONLY ONCE:
439;;
440;; - create a new buffer
441;; - generate the PostScript image to a file (C-u M-x ps-print-buffer)
442;; - open this file and find the line:
443;; `% 3 cm 20 cm moveto 10 /Courier ReportFontInfo showpage'
a18ed129 444;; - delete the leading `%' (which is the PostScript comment character)
bcc0d457
RS
445;; - replace in this line `Courier' by the new font (say `Helvetica')
446;; to get the line:
447;; `3 cm 20 cm moveto 10 /Helvetica ReportFontInfo showpage'
448;; - send this file to the printer (or to ghostscript).
449;; You should read the following on the output page:
450;;
451;; For Helvetica 10 point, the line height is 11.56, the space width is 2.78
452;; and a crude estimate of average character width is 5.09243
453;;
454;; - Add these values to the `ps-font-info-database':
455;; (setq ps-font-info-database
456;; (append
457;; '((Helvetica ; the family name
458;; "Helvetica" "Helvetica-Bold" "Helvetica-Oblique" "Helvetica-BoldOblique"
459;; 10.0 11.56 2.78 5.09243))
460;; ps-font-info-database))
461;; - Now you can use this font family with any size:
462;; (setq ps-font-family 'Helvetica)
463;; - if you want to use this family in another emacs session, you must
464;; put into your `~/.emacs':
465;; (require 'ps-print)
466;; (setq ps-font-info-database (append ...)))
467;; if you don't want to load ps-print, you have to copy the whole value:
468;; (setq ps-font-info-database '(<your stuff> <the standard stuff>))
469;; or, if you can wait until the `ps-print-hook' is implemented, do:
470;; (add-hook 'ps-print-hook '(setq ps-font-info-database (append ...)))
471;; This does not work yet, since there is no `ps-print-hook' yet.
472;;
473;; You can create new `mixed' font families like:
474;; (my-mixed-family
475;; "Courier-Bold" "Helvetica"
476;; "Zapf-Chancery-MediumItalic" "NewCenturySchlbk-BoldItalic"
477;; 10.0 10.55 6.0 6.0)
478;; Now you can use your new font family with any size:
479;; (setq ps-font-family 'my-mixed-family)
480;;
481;; You can get information on all the fonts resident in YOUR printer
482;; by uncommenting the line:
483;; % 3 cm 20 cm moveto ReportAllFontInfo showpage
484;;
a18ed129
RS
485;; The PostScript file should be sent to YOUR PostScript printer.
486;; If you send it to ghostscript or to another PostScript printer,
bcc0d457
RS
487;; you may get slightly different results.
488;; Anyway, as ghostscript fonts are autoload, you won't get
489;; much font info.
490;;
491;;
492;; How Ps-Print Deals With Faces
493;; -----------------------------
12d89a2e 494;;
bcc0d457
RS
495;; The ps-print-*-with-faces commands attempt to determine which faces
496;; should be printed in bold or italic, but their guesses aren't
497;; always right. For example, you might want to map colors into faces
498;; so that blue faces print in bold, and red faces in italic.
12d89a2e 499;;
bcc0d457
RS
500;; It is possible to force ps-print to consider specific faces bold or
501;; italic, no matter what font they are displayed in, by setting the
502;; variables `ps-bold-faces' and `ps-italic-faces'. These variables
503;; contain lists of faces that ps-print should consider bold or
504;; italic; to set them, put code like the following into your .emacs
505;; file:
12d89a2e 506;;
bcc0d457
RS
507;; (setq ps-bold-faces '(my-blue-face))
508;; (setq ps-italic-faces '(my-red-face))
509;;
510;; Faces like bold-italic that are both bold and italic should go in
511;; *both* lists.
512;;
513;; Ps-print keeps internal lists of which fonts are bold and which are
514;; italic; these lists are built the first time you invoke ps-print.
515;; For the sake of efficiency, the lists are built only once; the same
516;; lists are referred in later invocations of ps-print.
517;;
518;; Because these lists are built only once, it's possible for them to
519;; get out of sync, if a face changes, or if new faces are added. To
520;; get the lists back in sync, you can set the variable
521;; `ps-build-face-reference' to t, and the lists will be rebuilt the
522;; next time ps-print is invoked.
523;;
524;;
525;; How Ps-Print Deals With Color
526;; -----------------------------
527;;
528;; Ps-print detects faces with foreground and background colors
529;; defined and embeds color information in the PostScript image.
530;; The default foreground and background colors are defined by the
531;; variables `ps-default-fg' and `ps-default-bg'.
532;; On black-and-white printers, colors are displayed in grayscale.
533;; To turn off color output, set `ps-print-color-p' to nil.
534;;
535;;
87a16a06
RS
536;; How Ps-Print Maps Faces
537;; -----------------------
538;;
539;; As ps-print uses PostScript to print buffers, it is possible to have
540;; other attributes associated with faces. So the new attributes used
541;; by ps-print are:
542;;
543;; strikeout - like underline, but the line is in middle of text.
544;; overline - like underline, but the line is over the text.
545;; shadow - text will have a shadow.
546;; box - text will be surrounded by a box.
a18ed129 547;; outline - print characters as hollow outlines.
87a16a06 548;;
06fb6aab 549;; See the documentation for `ps-extend-face'.
87a16a06
RS
550;;
551;; Let's, for example, remap font-lock-keyword-face to another foreground color
552;; and bold attribute:
553;;
a18ed129 554;; (ps-extend-face '(font-lock-keyword-face "RoyalBlue" nil bold) 'MERGE)
87a16a06 555;;
6c8f2753
RS
556;; If you want to use a new face, define it first with `defface',
557;; and then call `ps-extend-face' to specify how to print it.
558;;
87a16a06
RS
559;;
560;; How Ps-Print Has A Text And/Or Image On Background
561;; --------------------------------------------------
562;;
563;; Ps-print can print texts and/or EPS PostScript images on background; it is
564;; possible to define the following text attributes: font name, font size,
565;; initial position, angle, gray scale and pages to print.
566;;
567;; It has the following EPS PostScript images attributes: file name containing
568;; the image, initial position, X and Y scales, angle and pages to print.
569;;
570;; See documentation for `ps-print-background-text' and
571;; `ps-print-background-image'.
572;;
573;; For example, if we wish to print text "preliminary" on all pages and text
574;; "special" on page 5 and from page 11 to page 17, we could specify:
575;;
576;; (setq ps-print-background-text
577;; '(("preliminary")
578;; ("special"
579;; "LeftMargin" "BottomMargin PrintHeight add" ; X and Y position
580;; ; (upper left corner)
581;; nil nil nil
582;; "PrintHeight neg PrintWidth atan" ; angle
583;; 5 (11 . 17)) ; page list
584;; ))
585;;
586;; Similarly, we could print image "~/images/EPS-image1.ps" on all pages and
587;; image "~/images/EPS-image2.ps" on page 5 and from page 11 to page 17, we
588;; specify:
589;;
590;; (setq ps-print-background-image
591;; '(("~/images/EPS-image1.ps"
592;; "LeftMargin" "BottomMargin") ; X and Y position (lower left corner)
593;; ("~/images/EPS-image2.ps"
594;; "LeftMargin" "BottomMargin PrintHeight 2 div add" ; X and Y position
595;; ; (upper left corner)
596;; nil nil nil
597;; 5 (11 . 17)) ; page list
598;; ))
599;;
600;; If it is not possible to read (or does not exist) an image file, that file
601;; is ignored.
602;;
603;; The printing order is:
604;;
605;; 1. Print zebra stripes
606;; 2. Print background texts that it should be on all pages
607;; 3. Print background images that it should be on all pages
608;; 4. Print background texts only for current page (if any)
609;; 5. Print background images only for current page (if any)
610;; 6. Print header
a18ed129 611;; 7. Print buffer text (with faces, if specified) and line number
87a16a06
RS
612;;
613;;
bcc0d457
RS
614;; Utilities
615;; ---------
616;;
617;; Some tools are provided to help you customize your font setup.
618;;
619;; `ps-setup' returns (some part of) the current setup.
620;;
621;; To avoid wrapping too many lines, you may want to adjust the
622;; left and right margins and the font size. On UN*X systems, do:
623;; pr -t file | awk '{printf "%3d %s\n", length($0), $0}' | sort -r | head
624;; to determine the longest lines of your file.
87a16a06 625;; Then, the command `ps-line-lengths' will give you the correspondence
bcc0d457
RS
626;; between a line length (number of characters) and the maximum font
627;; size which doesn't wrap such a line with the current ps-print setup.
628;;
629;; The commands `ps-nb-pages-buffer' and `ps-nb-pages-region' display
87a16a06 630;; the correspondence between a number of pages and the maximum font
bcc0d457
RS
631;; size which allow the number of lines of the current buffer or of
632;; its current region to fit in this number of pages.
a18ed129
RS
633;;
634;; NOTE: line folding is not taken into account in this process and could
635;; change the results.
b87c5d3d 636;;
b87c5d3d 637;;
b87c5d3d
RS
638;; New since version 1.5
639;; ---------------------
b87c5d3d 640;;
bcc0d457 641;; Color output capability.
b87c5d3d 642;; Automatic detection of font attributes (bold, italic).
b87c5d3d 643;; Configurable headers with page numbers.
b87c5d3d 644;; Slightly faster.
b87c5d3d 645;; Support for different paper sizes.
b87c5d3d
RS
646;; Better conformance to PostScript Document Structure Conventions.
647;;
ef2cbb24 648;;
bcc0d457
RS
649;; New since version 2.8
650;; ---------------------
651;;
87a16a06
RS
652;; [vinicius] 970809 Vinicius Jose Latorre <vinicius@cpqd.br>
653;;
654;; Handle control characters.
655;; Face remapping.
656;; New face attributes.
657;; Line number.
658;; Zebra stripes.
659;; Text and/or image on background.
660;;
bcc0d457
RS
661;; [jack] 960517 Jacques Duthen <duthen@cegelec-red.fr>
662;;
a18ed129 663;; Font family and float size for text and header.
bcc0d457
RS
664;; Landscape mode.
665;; Multiple columns.
666;; Tools for page setup.
667;;
668;;
ef2cbb24
RS
669;; Known bugs and limitations of ps-print:
670;; --------------------------------------
bcc0d457 671;;
043620f4
KH
672;; Although color printing will work in XEmacs 19.12, it doesn't work
673;; well; in particular, bold or italic fonts don't print in the right
674;; background color.
675;;
676;; Invisible properties aren't correctly ignored in XEmacs 19.12.
677;;
b87c5d3d 678;; Automatic font-attribute detection doesn't work well, especially
00aa16af 679;; with hilit19 and older versions of get-create-face. Users having
bcc0d457
RS
680;; problems with auto-font detection should use the lists
681;; `ps-italic-faces' and `ps-bold-faces' and/or turn off automatic
682;; detection by setting `ps-auto-font-detect' to nil.
00aa16af 683;;
043620f4 684;; Automatic font-attribute detection doesn't work with XEmacs 19.12
bcc0d457 685;; in tty mode; use the lists `ps-italic-faces' and `ps-bold-faces'
043620f4 686;; instead.
12d89a2e 687;;
00aa16af 688;; Still too slow; could use some hand-optimization.
ef2cbb24 689;;
12d89a2e 690;; Default background color isn't working.
ef2cbb24
RS
691;;
692;; Faces are always treated as opaque.
693;;
12d89a2e 694;; Epoch and Emacs 18 not supported. At all.
ef2cbb24 695;;
06fb6aab 696;; Fixed-pitch fonts work better for line folding, but are not required.
bcc0d457
RS
697;;
698;; `ps-nb-pages-buffer' and `ps-nb-pages-region' don't take care
699;; of folding lines.
ef2cbb24 700;;
12d89a2e 701;;
bcc0d457
RS
702;; Things to change:
703;; ----------------
ef2cbb24 704;;
bcc0d457
RS
705;; Add `ps-print-hook' (I don't know how to do that (yet!)).
706;; Add 4-up capability (really needed?).
bcc0d457
RS
707;; Add `ps-non-bold-faces' and `ps-non-italic-faces' (should be easy).
708;; Put one header per page over the columns (easy but needed?).
709;; Improve the memory management for big files (hard?).
710;; `ps-nb-pages-buffer' and `ps-nb-pages-region' should take care
711;; of folding lines.
ef2cbb24 712;;
ef2cbb24 713;;
12d89a2e
RS
714;; Acknowledgements
715;; ----------------
bcc0d457
RS
716;; Thanks to Jim Thompson <?@?> for the 2.8 version I started from.
717;; [jack]
718;;
12d89a2e
RS
719;; Thanks to Kevin Rodgers <kevinr@ihs.com> for adding support for
720;; color and the invisible property.
ef2cbb24 721;;
12d89a2e
RS
722;; Thanks to Avishai Yacobi, avishaiy@mcil.comm.mot.com, for writing
723;; the initial port to Emacs 19. His code is no longer part of
724;; ps-print, but his work is still appreciated.
ef2cbb24 725;;
12d89a2e
RS
726;; Thanks to Remi Houdaille and Michel Train, michel@metasoft.fdn.org,
727;; for adding underline support. Their code also is no longer part of
728;; ps-print, but their efforts are not forgotten.
729;;
730;; Thanks also to all of you who mailed code to add features to
731;; ps-print; although I didn't use your code, I still appreciate your
732;; sharing it with me.
733;;
734;; Thanks to all who mailed comments, encouragement, and criticism.
735;; Thanks also to all who responded to my survey; I had too many
736;; responses to reply to them all, but I greatly appreciate your
737;; interest.
738;;
739;; Jim
740;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
ef2cbb24
RS
741
742;;; Code:
743
090be653
RS
744(eval-when-compile
745 (require 'cl))
b87c5d3d 746
090be653
RS
747(unless (featurep 'lisp-float-type)
748 (error "`ps-print' requires floating point support"))
ef2cbb24
RS
749
750;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
12d89a2e
RS
751;; User Variables:
752
bcc0d457
RS
753;;; Interface to the command system
754
e0af0d3e
RS
755(defgroup ps-print nil
756 "Postscript generator for Emacs 19"
757 :prefix "ps-"
758 :group 'wp)
759
760(defgroup ps-print-horizontal nil
761 "Horizontal page layout"
762 :prefix "ps-"
763 :tag "Horizontal"
764 :group 'ps-print)
765
766(defgroup ps-print-vertical nil
767 "Vertical page layout"
768 :prefix "ps-"
769 :tag "Vertical"
770 :group 'ps-print)
771
772(defgroup ps-print-header nil
773 "Headers layout"
774 :prefix "ps-"
775 :tag "Header"
776 :group 'ps-print)
777
778(defgroup ps-print-font nil
779 "Fonts customization"
780 :prefix "ps-"
781 :tag "Font"
782 :group 'ps-print)
783
784(defgroup ps-print-color nil
785 "Color customization"
786 :prefix "ps-"
787 :tag "Color"
788 :group 'ps-print)
789
790(defgroup ps-print-face nil
791 "Faces customization"
792 :prefix "ps-"
793 :tag "PS Faces"
794 :group 'ps-print
795 :group 'faces)
796
797
798(defcustom ps-lpr-command lpr-command
799 "*The shell command for printing a PostScript file."
800 :type 'string
801 :group 'ps-print)
802
803(defcustom ps-lpr-switches lpr-switches
804 "*A list of extra switches to pass to `ps-lpr-command'."
805 :type '(repeat string)
806 :group 'ps-print)
12d89a2e 807
bcc0d457 808;;; Page layout
12d89a2e 809
bcc0d457
RS
810;; All page dimensions are in PostScript points.
811;; 1 inch == 2.54 cm == 72 points
812;; 1 cm == (/ 1 2.54) inch == (/ 72 2.54) points
813
814;; Letter 8.5 inch x 11.0 inch
815;; Legal 8.5 inch x 14.0 inch
816;; A4 8.26 inch x 11.69 inch = 21.0 cm x 29.7 cm
817
818;; LetterSmall 7.68 inch x 10.16 inch
819;; Tabloid 11.0 inch x 17.0 inch
820;; Ledger 17.0 inch x 11.0 inch
821;; Statement 5.5 inch x 8.5 inch
822;; Executive 7.5 inch x 10.0 inch
823;; A3 11.69 inch x 16.5 inch = 29.7 cm x 42.0 cm
824;; A4Small 7.47 inch x 10.85 inch
825;; B4 10.125 inch x 14.33 inch
826;; B5 7.16 inch x 10.125 inch
827
e0af0d3e 828(defcustom ps-page-dimensions-database
bcc0d457
RS
829 (list (list 'a4 (/ (* 72 21.0) 2.54) (/ (* 72 29.7) 2.54))
830 (list 'a3 (/ (* 72 29.7) 2.54) (/ (* 72 42.0) 2.54))
831 (list 'letter (* 72 8.5) (* 72 11.0))
832 (list 'legal (* 72 8.5) (* 72 14.0))
833 (list 'letter-small (* 72 7.68) (* 72 10.16))
834 (list 'tabloid (* 72 11.0) (* 72 17.0))
835 (list 'ledger (* 72 17.0) (* 72 11.0))
836 (list 'statement (* 72 5.5) (* 72 8.5))
837 (list 'executive (* 72 7.5) (* 72 10.0))
838 (list 'a4small (* 72 7.47) (* 72 10.85))
839 (list 'b4 (* 72 10.125) (* 72 14.33))
840 (list 'b5 (* 72 7.16) (* 72 10.125)))
841 "*List associating a symbolic paper type to its width and height.
e0af0d3e
RS
842see `ps-paper-type'."
843 :type '(repeat (list :tag "Paper Type"
844 (symbol :tag "Name")
845 (number :tag "Width")
846 (number :tag "Height")))
847 :group 'ps-print)
848
849(defcustom ps-paper-type 'letter
bcc0d457 850 "*Specifies the size of paper to format for.
090be653 851Should be one of the paper types defined in `ps-page-dimensions-database', for
e0af0d3e
RS
852example `letter', `legal' or `a4'."
853 :type '(symbol :validate (lambda (wid)
87a16a06
RS
854 (if (assq (widget-value wid)
855 ps-page-dimensions-database)
e0af0d3e
RS
856 nil
857 (widget-put wid :error "Unknown paper size")
858 wid)))
859 :group 'ps-print)
860
87a16a06 861(defcustom ps-landscape-mode nil
e0af0d3e
RS
862 "*Non-nil means print in landscape mode."
863 :type 'boolean
864 :group 'ps-print)
865
866(defcustom ps-number-of-columns (if ps-landscape-mode 2 1)
867 "*Specifies the number of columns"
87a16a06
RS
868 :type 'number
869 :group 'ps-print)
870
535efc38 871(defcustom ps-zebra-stripes nil
87a16a06 872 "*Non-nil means print zebra stripes.
06fb6aab 873See also documentation for `ps-zebra-stripe-height'."
87a16a06
RS
874 :type 'boolean
875 :group 'ps-print)
876
535efc38 877(defcustom ps-zebra-stripe-height 3
87a16a06 878 "*Number of zebra stripe lines.
06fb6aab 879See also documentation for `ps-zebra-stripes'."
87a16a06
RS
880 :type 'number
881 :group 'ps-print)
882
883(defcustom ps-line-number nil
884 "*Non-nil means print line number."
885 :type 'boolean
886 :group 'ps-print)
887
888(defcustom ps-print-background-image nil
889 "*EPS image list to be printed on background.
890
891The elements are:
892
893 (FILENAME X Y XSCALE YSCALE ROTATION PAGES...)
894
895FILENAME is a file name which contains an EPS image or some PostScript
896programming like EPS.
897FILENAME is ignored, if it doesn't exist or is read protected.
898
899X and Y are relative positions on paper to put the image.
900If X and Y are nil, the image is centralized on paper.
901
902XSCALE and YSCALE are scale factor to be applied to image before printing.
903If XSCALE and YSCALE are nil, the original size is used.
904
905ROTATION is the image rotation angle; if nil, the default is 0.
906
907PAGES designates the page to print background image.
908PAGES may be a number or a cons cell (FROM . TO) designating FROM page
909to TO page.
910If PAGES is nil, print background image on all pages.
911
912X, Y, XSCALE, YSCALE and ROTATION may be a floating point number,
913an integer number or a string. If it is a string, the string should contain
914PostScript programming that returns a float or integer value.
915
916For example, if you wish to print an EPS image on all pages do:
917
918 '((\"~/images/EPS-image.ps\"))"
35378a09
KH
919 :type '(repeat (list file
920 (choice :tag "X" number string (const nil))
921 (choice :tag "Y" number string (const nil))
922 (choice :tag "X Scale" number string (const nil))
923 (choice :tag "Y Scale" number string (const nil))
924 (choice :tag "Rotation" number string (const nil))
925 (repeat :tag "Pages" :inline t
926 (radio integer
927 (cons :tag "Range"
928 (integer :tag "From")
929 (integer :tag "To"))))))
87a16a06
RS
930 :group 'ps-print)
931
932(defcustom ps-print-background-text nil
933 "*Text list to be printed on background.
934
935The elements are:
936
937 (STRING X Y FONT FONTSIZE GRAY ROTATION PAGES...)
938
939STRING is the text to be printed on background.
940
941X and Y are positions on paper to put the text.
942If X and Y are nil, the text is positioned at lower left corner.
943
944FONT is a font name to be used on printing the text.
945If nil, \"Times-Roman\" is used.
946
947FONTSIZE is font size to be used, if nil, 200 is used.
948
949GRAY is the text gray factor (should be very light like 0.8).
950If nil, the default is 0.85.
951
952ROTATION is the text rotation angle; if nil, the angle is given by
953the diagonal from lower left corner to upper right corner.
954
955PAGES designates the page to print background text.
956PAGES may be a number or a cons cell (FROM . TO) designating FROM page
957to TO page.
958If PAGES is nil, print background text on all pages.
959
960X, Y, FONTSIZE, GRAY and ROTATION may be a floating point number,
961an integer number or a string. If it is a string, the string should contain
962PostScript programming that returns a float or integer value.
963
964For example, if you wish to print text \"Preliminary\" on all pages do:
965
966 '((\"Preliminary\"))"
35378a09
KH
967 :type '(repeat (list string
968 (choice :tag "X" number string (const nil))
969 (choice :tag "Y" number string (const nil))
970 (choice :tag "Font" string (const nil))
971 (choice :tag "Fontsize" number string (const nil))
972 (choice :tag "Gray" number string (const nil))
973 (choice :tag "Rotation" number string (const nil))
974 (repeat :tag "Pages" :inline t
975 (radio integer
976 (cons :tag "Range"
977 (integer :tag "From")
978 (integer :tag "To"))))))
e0af0d3e 979 :group 'ps-print)
bcc0d457
RS
980
981;;; Horizontal layout
982
983;; ------------------------------------------
984;; | | | | | | | |
985;; | lm | text | ic | text | ic | text | rm |
986;; | | | | | | | |
987;; ------------------------------------------
988
e0af0d3e
RS
989(defcustom ps-left-margin (/ (* 72 2.0) 2.54) ; 2 cm
990 "*Left margin in points (1/72 inch)."
991 :type 'number
992 :group 'ps-print-horizontal)
bcc0d457 993
e0af0d3e
RS
994(defcustom ps-right-margin (/ (* 72 2.0) 2.54) ; 2 cm
995 "*Right margin in points (1/72 inch)."
996 :type 'number
997 :group 'ps-print-horizontal)
bcc0d457 998
e0af0d3e
RS
999(defcustom ps-inter-column (/ (* 72 2.0) 2.54) ; 2 cm
1000 "*Horizontal space between columns in points (1/72 inch)."
1001 :type 'number
1002 :group 'ps-print-horizontal)
bcc0d457
RS
1003
1004;;; Vertical layout
1005
1006;; |--------|
1007;; | tm |
1008;; |--------|
1009;; | header |
1010;; |--------|
1011;; | ho |
1012;; |--------|
1013;; | text |
1014;; |--------|
1015;; | bm |
1016;; |--------|
1017
e0af0d3e
RS
1018(defcustom ps-bottom-margin (/ (* 72 1.5) 2.54) ; 1.5 cm
1019 "*Bottom margin in points (1/72 inch)."
1020 :type 'number
1021 :group 'ps-print-vertical)
bcc0d457 1022
e0af0d3e
RS
1023(defcustom ps-top-margin (/ (* 72 1.5) 2.54) ; 1.5 cm
1024 "*Top margin in points (1/72 inch)."
1025 :type 'number
1026 :group 'ps-print-vertical)
bcc0d457 1027
e0af0d3e
RS
1028(defcustom ps-header-offset (/ (* 72 1.0) 2.54) ; 1.0 cm
1029 "*Vertical space in points (1/72 inch) between the main text and the header."
1030 :type 'number
1031 :group 'ps-print-vertical)
bcc0d457 1032
e0af0d3e 1033(defcustom ps-header-line-pad 0.15
bcc0d457 1034 "*Portion of a header title line height to insert between the header frame
e0af0d3e
RS
1035and the text it contains, both in the vertical and horizontal directions."
1036 :type 'number
1037 :group 'ps-print-vertical)
bcc0d457
RS
1038
1039;;; Header setup
12d89a2e 1040
e0af0d3e 1041(defcustom ps-print-header t
86c10ecb
RS
1042 "*Non-nil means print a header at the top of each page.
1043By default, the header displays the buffer name, page number, and, if
1044the buffer is visiting a file, the file's directory. Headers are
ae7f6761 1045customizable by changing variables `ps-left-header' and
e0af0d3e
RS
1046`ps-right-header'."
1047 :type 'boolean
1048 :group 'ps-print-header)
1049
1050(defcustom ps-print-header-frame t
1051 "*Non-nil means draw a gaudy frame around the header."
1052 :type 'boolean
1053 :group 'ps-print-header)
1054
1055(defcustom ps-header-lines 2
1056 "*Number of lines to display in page header, when generating Postscript."
1057 :type 'integer
1058 :group 'ps-print-header)
bcc0d457
RS
1059(make-variable-buffer-local 'ps-header-lines)
1060
e0af0d3e 1061(defcustom ps-show-n-of-n t
00aa16af
RS
1062 "*Non-nil means show page numbers as N/M, meaning page N of M.
1063Note: page numbers are displayed as part of headers, see variable
35378a09 1064`ps-print-header'."
e0af0d3e
RS
1065 :type 'boolean
1066 :group 'ps-print-header)
12d89a2e 1067
e0af0d3e 1068(defcustom ps-spool-duplex nil ; Not many people have duplex
bcc0d457
RS
1069 ; printers, so default to nil.
1070 "*Non-nil indicates spooling is for a two-sided printer.
1071For a duplex printer, the `ps-spool-*' commands will insert blank pages
1072as needed between print jobs so that the next buffer printed will
1073start on the right page. Also, if headers are turned on, the headers
1074will be reversed on duplex printers so that the page numbers fall to
e0af0d3e
RS
1075the left on even-numbered pages."
1076 :type 'boolean
1077 :group 'ps-print-header)
bcc0d457
RS
1078
1079;;; Fonts
1080
e0af0d3e 1081(defcustom ps-font-info-database
bcc0d457
RS
1082 '((Courier ; the family key
1083 "Courier" "Courier-Bold" "Courier-Oblique" "Courier-BoldOblique"
1084 10.0 10.55 6.0 6.0)
1085 (Helvetica ; the family key
1086 "Helvetica" "Helvetica-Bold" "Helvetica-Oblique" "Helvetica-BoldOblique"
1087 10.0 11.56 2.78 5.09243)
1088 (Times
1089 "Times-Roman" "Times-Bold" "Times-Italic" "Times-BoldItalic"
1090 10.0 11.0 2.5 4.71432)
1091 (Palatino
1092 "Palatino-Roman" "Palatino-Bold" "Palatino-Italic" "Palatino-BoldItalic"
1093 10.0 12.1 2.5 5.08676)
1094 (Helvetica-Narrow
1095 "Helvetica-Narrow" "Helvetica-Narrow-Bold"
1096 "Helvetica-Narrow-Oblique" "Helvetica-Narrow-BoldOblique"
1097 10.0 11.56 2.2796 4.17579)
1098 (NewCenturySchlbk
1099 "NewCenturySchlbk-Roman" "NewCenturySchlbk-Bold"
1100 "NewCenturySchlbk-Italic" "NewCenturySchlbk-BoldItalic"
1101 10.0 12.15 2.78 5.31162)
1102 ;; got no bold for the next ones
1103 (AvantGarde-Book
1104 "AvantGarde-Book" "AvantGarde-Book"
1105 "AvantGarde-BookOblique" "AvantGarde-BookOblique"
1106 10.0 11.77 2.77 5.45189)
1107 (AvantGarde-Demi
1108 "AvantGarde-Demi" "AvantGarde-Demi"
1109 "AvantGarde-DemiOblique" "AvantGarde-DemiOblique"
1110 10.0 12.72 2.8 5.51351)
1111 (Bookman-Demi
1112 "Bookman-Demi" "Bookman-Demi"
1113 "Bookman-DemiItalic" "Bookman-DemiItalic"
1114 10.0 11.77 3.4 6.05946)
1115 (Bookman-Light
1116 "Bookman-Light" "Bookman-Light"
1117 "Bookman-LightItalic" "Bookman-LightItalic"
1118 10.0 11.79 3.2 5.67027)
1119 ;; got no bold and no italic for the next ones
1120 (Symbol
1121 "Symbol" "Symbol" "Symbol" "Symbol"
1122 10.0 13.03 2.5 3.24324)
1123 (Zapf-Dingbats
1124 "Zapf-Dingbats" "Zapf-Dingbats" "Zapf-Dingbats" "Zapf-Dingbats"
1125 10.0 9.63 2.78 2.78)
1126 (Zapf-Chancery-MediumItalic
1127 "Zapf-Chancery-MediumItalic" "Zapf-Chancery-MediumItalic"
1128 "Zapf-Chancery-MediumItalic" "Zapf-Chancery-MediumItalic"
1129 10.0 11.45 2.2 4.10811)
87a16a06 1130 )
bcc0d457
RS
1131 "*Font info database: font family (the key), name, bold, italic, bold-italic,
1132reference size, line height, space width, average character width.
1133To get the info for another specific font (say Helvetica), do the following:
1134- create a new buffer
1135- generate the PostScript image to a file (C-u M-x ps-print-buffer)
1136- open this file and delete the leading `%' (which is the Postscript
1137 comment character) from the line
87a16a06 1138 `% 3 cm 20 cm moveto 10 /Courier ReportFontInfo showpage'
bcc0d457 1139 to get the line
87a16a06 1140 `3 cm 20 cm moveto 10 /Helvetica ReportFontInfo showpage'
bcc0d457 1141- add the values to `ps-font-info-database'.
e0af0d3e
RS
1142You can get all the fonts of YOUR printer using `ReportAllFontInfo'."
1143 :type '(repeat (list :tag "Font Definition"
1144 (symbol :tag "Font")
1145 (string :tag "Name")
1146 (string :tag "Bold")
1147 (string :tag "Italic")
1148 (string :tag "Bold-Italic")
1149 (number :tag "Reference Size")
1150 (number :tag "Line Height")
1151 (number :tag "Space Width")
1152 (number :tag "Average Character Width")))
1153 :group 'ps-print-font)
1154
1155(defcustom ps-font-family 'Courier
1156 "Font family name for ordinary text, when generating Postscript."
1157 :type 'symbol
1158 :group 'ps-print-font)
1159
1160(defcustom ps-font-size (if ps-landscape-mode 7 8.5)
1161 "Font size, in points, for ordinary text, when generating Postscript."
1162 :type 'number
1163 :group 'ps-print-font)
1164
1165(defcustom ps-header-font-family 'Helvetica
1166 "Font family name for text in the header, when generating Postscript."
1167 :type 'symbol
1168 :group 'ps-print-font)
1169
1170(defcustom ps-header-font-size (if ps-landscape-mode 10 12)
1171 "Font size, in points, for text in the header, when generating Postscript."
1172 :type 'number
1173 :group 'ps-print-font)
1174
1175(defcustom ps-header-title-font-size (if ps-landscape-mode 12 14)
bcc0d457 1176 "Font size, in points, for the top line of text in the header,
e0af0d3e
RS
1177when generating Postscript."
1178 :type 'number
1179 :group 'ps-print-font)
bcc0d457
RS
1180
1181;;; Colors
1182
87a16a06
RS
1183;; Printing color requires x-color-values.
1184(defcustom ps-print-color-p (or (fboundp 'x-color-values) ; Emacs
4f3beabf 1185 (fboundp 'pixel-components)) ; XEmacs
e0af0d3e
RS
1186 "*If non-nil, print the buffer's text in color."
1187 :type 'boolean
1188 :group 'ps-print-color)
12d89a2e 1189
e0af0d3e
RS
1190(defcustom ps-default-fg '(0.0 0.0 0.0)
1191 "*RGB values of the default foreground color. Defaults to black."
1192 :type '(list (number :tag "Red") (number :tag "Green") (number :tag "Blue"))
1193 :group 'ps-print-color)
12d89a2e 1194
e0af0d3e
RS
1195(defcustom ps-default-bg '(1.0 1.0 1.0)
1196 "*RGB values of the default background color. Defaults to white."
1197 :type '(list (number :tag "Red") (number :tag "Green") (number :tag "Blue"))
1198 :group 'ps-print-color)
12d89a2e 1199
e0af0d3e 1200(defcustom ps-auto-font-detect t
12d89a2e 1201 "*Non-nil means automatically detect bold/italic face attributes.
7f72c06f 1202If nil, we rely solely on the lists `ps-bold-faces', `ps-italic-faces',
e0af0d3e
RS
1203and `ps-underlined-faces'."
1204 :type 'boolean
1205 :group 'ps-print-font)
12d89a2e 1206
e0af0d3e 1207(defcustom ps-bold-faces
090be653
RS
1208 (unless ps-print-color-p
1209 '(font-lock-function-name-face
1210 font-lock-builtin-face
1211 font-lock-variable-name-face
1212 font-lock-keyword-face
1213 font-lock-warning-face))
86c10ecb 1214 "*A list of the \(non-bold\) faces that should be printed in bold font.
e0af0d3e
RS
1215This applies to generating Postscript."
1216 :type '(repeat face)
1217 :group 'ps-print-face)
12d89a2e 1218
e0af0d3e 1219(defcustom ps-italic-faces
090be653
RS
1220 (unless ps-print-color-p
1221 '(font-lock-variable-name-face
1222 font-lock-string-face
1223 font-lock-comment-face
1224 font-lock-warning-face))
86c10ecb 1225 "*A list of the \(non-italic\) faces that should be printed in italic font.
e0af0d3e
RS
1226This applies to generating Postscript."
1227 :type '(repeat face)
1228 :group 'ps-print-face)
12d89a2e 1229
e0af0d3e 1230(defcustom ps-underlined-faces
090be653
RS
1231 (unless ps-print-color-p
1232 '(font-lock-function-name-face
1233 font-lock-type-face
1234 font-lock-reference-face
1235 font-lock-warning-face))
86c10ecb 1236 "*A list of the \(non-underlined\) faces that should be printed underlined.
e0af0d3e
RS
1237This applies to generating Postscript."
1238 :type '(repeat face)
1239 :group 'ps-print-face)
12d89a2e 1240
e0af0d3e 1241(defcustom ps-left-header
12d89a2e 1242 (list 'ps-get-buffer-name 'ps-header-dirpart)
bcc0d457 1243 "*The items to display (each on a line) on the left part of the page header.
86c10ecb 1244This applies to generating Postscript.
12d89a2e 1245
86c10ecb 1246The value should be a list of strings and symbols, each representing an
12d89a2e
RS
1247entry in the PostScript array HeaderLinesLeft.
1248
1249Strings are inserted unchanged into the array; those representing
1250PostScript string literals should be delimited with PostScript string
1251delimiters '(' and ')'.
1252
1253For symbols with bound functions, the function is called and should
1254return a string to be inserted into the array. For symbols with bound
1255values, the value should be a string to be inserted into the array.
1256In either case, function or variable, the string value has PostScript
e0af0d3e
RS
1257string delimiters added to it."
1258 :type '(repeat (choice string symbol))
a6c6e755 1259 :group 'ps-print-header)
12d89a2e
RS
1260(make-variable-buffer-local 'ps-left-header)
1261
e0af0d3e 1262(defcustom ps-right-header
090be653 1263 (list "/pagenumberstring load" 'time-stamp-mon-dd-yyyy 'time-stamp-hh:mm:ss)
bcc0d457 1264 "*The items to display (each on a line) on the right part of the page header.
86c10ecb 1265This applies to generating Postscript.
12d89a2e 1266
86c10ecb 1267See the variable `ps-left-header' for a description of the format of
e0af0d3e
RS
1268this variable."
1269 :type '(repeat (choice string symbol))
a6c6e755 1270 :group 'ps-print-header)
12d89a2e 1271(make-variable-buffer-local 'ps-right-header)
ef2cbb24 1272
e0af0d3e
RS
1273(defcustom ps-razzle-dazzle t
1274 "*Non-nil means report progress while formatting buffer."
1275 :type 'boolean
1276 :group 'ps-print)
12d89a2e 1277
a18ed129 1278(defcustom ps-adobe-tag "%!PS-Adobe-3.0\n"
12d89a2e
RS
1279 "*Contains the header line identifying the output as PostScript.
1280By default, `ps-adobe-tag' contains the standard identifier. Some
a18ed129
RS
1281printers require slightly different versions of this line."
1282 :type 'string
1283 :group 'ps-print)
12d89a2e 1284
e0af0d3e 1285(defcustom ps-build-face-reference t
12d89a2e
RS
1286 "*Non-nil means build the reference face lists.
1287
1288Ps-print sets this value to nil after it builds its internal reference
1289lists of bold and italic faces. By settings its value back to t, you
1290can force ps-print to rebuild the lists the next time you invoke one
86c10ecb 1291of the ...-with-faces commands.
12d89a2e
RS
1292
1293You should set this value back to t after you change the attributes of
1294any face, or create new faces. Most users shouldn't have to worry
e0af0d3e
RS
1295about its setting, though."
1296 :type 'boolean
1297 :group 'ps-print-face)
12d89a2e 1298
e0af0d3e 1299(defcustom ps-always-build-face-reference nil
12d89a2e
RS
1300 "*Non-nil means always rebuild the reference face lists.
1301
1302If this variable is non-nil, ps-print will rebuild its internal
1303reference lists of bold and italic faces *every* time one of the
1304-with-faces commands is called. Most users shouldn't need to set this
e0af0d3e
RS
1305variable."
1306 :type 'boolean
1307 :group 'ps-print-face)
ef2cbb24
RS
1308
1309;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
12d89a2e 1310;; User commands
ef2cbb24 1311
00aa16af 1312;;;###autoload
ef2cbb24 1313(defun ps-print-buffer (&optional filename)
12d89a2e 1314 "Generate and print a PostScript image of the buffer.
ef2cbb24 1315
86c10ecb 1316When called with a numeric prefix argument (C-u), prompts the user for
ef2cbb24
RS
1317the name of a file to save the PostScript image in, instead of sending
1318it to the printer.
1319
1320More specifically, the FILENAME argument is treated as follows: if it
1321is nil, send the image to the printer. If FILENAME is a string, save
1322the PostScript image in a file with that name. If FILENAME is a
12d89a2e 1323number, prompt the user for the name of the file to save in."
00aa16af 1324 (interactive (list (ps-print-preprint current-prefix-arg)))
87a16a06 1325 (ps-print-without-faces (point-min) (point-max) filename))
ef2cbb24
RS
1326
1327
00aa16af 1328;;;###autoload
ef2cbb24 1329(defun ps-print-buffer-with-faces (&optional filename)
12d89a2e 1330 "Generate and print a PostScript image of the buffer.
12d89a2e 1331Like `ps-print-buffer', but includes font, color, and underline
107e7c70
KH
1332information in the generated image. This command works only if you
1333are using a window system, so it has a way to determine color values."
00aa16af 1334 (interactive (list (ps-print-preprint current-prefix-arg)))
87a16a06 1335 (ps-print-with-faces (point-min) (point-max) filename))
ef2cbb24 1336
ef2cbb24 1337
00aa16af 1338;;;###autoload
ef2cbb24 1339(defun ps-print-region (from to &optional filename)
12d89a2e 1340 "Generate and print a PostScript image of the region.
12d89a2e 1341Like `ps-print-buffer', but prints just the current region."
00aa16af 1342 (interactive (list (point) (mark) (ps-print-preprint current-prefix-arg)))
a18ed129 1343 (ps-print-without-faces from to filename t))
ef2cbb24 1344
ef2cbb24 1345
00aa16af 1346;;;###autoload
ef2cbb24 1347(defun ps-print-region-with-faces (from to &optional filename)
12d89a2e 1348 "Generate and print a PostScript image of the region.
12d89a2e 1349Like `ps-print-region', but includes font, color, and underline
107e7c70
KH
1350information in the generated image. This command works only if you
1351are using a window system, so it has a way to determine color values."
00aa16af 1352 (interactive (list (point) (mark) (ps-print-preprint current-prefix-arg)))
a18ed129 1353 (ps-print-with-faces from to filename t))
ef2cbb24 1354
ef2cbb24 1355
00aa16af 1356;;;###autoload
ef2cbb24 1357(defun ps-spool-buffer ()
12d89a2e 1358 "Generate and spool a PostScript image of the buffer.
12d89a2e
RS
1359Like `ps-print-buffer' except that the PostScript image is saved in a
1360local buffer to be sent to the printer later.
ef2cbb24 1361
12d89a2e 1362Use the command `ps-despool' to send the spooled images to the printer."
ef2cbb24 1363 (interactive)
87a16a06 1364 (ps-spool-without-faces (point-min) (point-max)))
ef2cbb24 1365
ef2cbb24 1366
00aa16af 1367;;;###autoload
ef2cbb24 1368(defun ps-spool-buffer-with-faces ()
12d89a2e 1369 "Generate and spool a PostScript image of the buffer.
12d89a2e 1370Like `ps-spool-buffer', but includes font, color, and underline
107e7c70 1371information in the generated image. This command works only if you
1cd7962f 1372are using a window system, so it has a way to determine color values.
ef2cbb24 1373
12d89a2e 1374Use the command `ps-despool' to send the spooled images to the printer."
ef2cbb24 1375 (interactive)
87a16a06 1376 (ps-spool-with-faces (point-min) (point-max)))
ef2cbb24 1377
ef2cbb24 1378
00aa16af 1379;;;###autoload
ef2cbb24 1380(defun ps-spool-region (from to)
12d89a2e 1381 "Generate a PostScript image of the region and spool locally.
12d89a2e 1382Like `ps-spool-buffer', but spools just the current region.
ef2cbb24 1383
12d89a2e 1384Use the command `ps-despool' to send the spooled images to the printer."
ef2cbb24 1385 (interactive "r")
a18ed129 1386 (ps-spool-without-faces from to t))
ef2cbb24 1387
ef2cbb24 1388
00aa16af 1389;;;###autoload
ef2cbb24 1390(defun ps-spool-region-with-faces (from to)
12d89a2e 1391 "Generate a PostScript image of the region and spool locally.
12d89a2e 1392Like `ps-spool-region', but includes font, color, and underline
107e7c70 1393information in the generated image. This command works only if you
1cd7962f 1394are using a window system, so it has a way to determine color values.
ef2cbb24 1395
12d89a2e 1396Use the command `ps-despool' to send the spooled images to the printer."
ef2cbb24 1397 (interactive "r")
a18ed129 1398 (ps-spool-with-faces from to t))
ef2cbb24 1399
00aa16af 1400;;;###autoload
ef2cbb24
RS
1401(defun ps-despool (&optional filename)
1402 "Send the spooled PostScript to the printer.
1403
1404When called with a numeric prefix argument (C-u), prompt the user for
1405the name of a file to save the spooled PostScript in, instead of sending
1406it to the printer.
1407
1408More specifically, the FILENAME argument is treated as follows: if it
1409is nil, send the image to the printer. If FILENAME is a string, save
1410the PostScript image in a file with that name. If FILENAME is a
1411number, prompt the user for the name of the file to save in."
00aa16af
RS
1412 (interactive (list (ps-print-preprint current-prefix-arg)))
1413 (ps-do-despool filename))
12d89a2e 1414
bcc0d457
RS
1415;;;###autoload
1416(defun ps-line-lengths ()
06fb6aab 1417 "Display the correspondence between a line length and a font size,
bcc0d457
RS
1418using the current ps-print setup.
1419Try: pr -t file | awk '{printf \"%3d %s\n\", length($0), $0}' | sort -r | head"
1420 (interactive)
1421 (ps-line-lengths-internal))
1422
1423;;;###autoload
1424(defun ps-nb-pages-buffer (nb-lines)
06fb6aab
RS
1425 "Display number of pages to print this buffer, for various font heights.
1426The table depends on the current ps-print setup."
bcc0d457
RS
1427 (interactive (list (count-lines (point-min) (point-max))))
1428 (ps-nb-pages nb-lines))
1429
1430;;;###autoload
1431(defun ps-nb-pages-region (nb-lines)
06fb6aab
RS
1432 "Display number of pages to print the region, for various font heights.
1433The table depends on the current ps-print setup."
bcc0d457
RS
1434 (interactive (list (count-lines (mark) (point))))
1435 (ps-nb-pages nb-lines))
1436
1437;;;###autoload
1438(defun ps-setup ()
06fb6aab 1439 "Return the current setup."
a18ed129
RS
1440 (format
1441 "
1442\(setq ps-print-color-p %s
bcc0d457
RS
1443 ps-lpr-command \"%s\"
1444 ps-lpr-switches %s
1445
01961237
RS
1446 ps-paper-type '%s
1447 ps-landscape-mode %s
1448 ps-number-of-columns %s
bcc0d457 1449
01961237
RS
1450 ps-zebra-stripes %s
1451 ps-zebra-stripe-height %s
1452 ps-line-number %s
a18ed129
RS
1453
1454 ps-print-background-image %s
1455
1456 ps-print-background-text %s
1457
1458 ps-left-margin %s
1459 ps-right-margin %s
1460 ps-inter-column %s
1461 ps-bottom-margin %s
1462 ps-top-margin %s
1463 ps-header-offset %s
bcc0d457
RS
1464 ps-header-line-pad %s
1465 ps-print-header %s
1466 ps-print-header-frame %s
1467 ps-header-lines %s
1468 ps-show-n-of-n %s
1469 ps-spool-duplex %s
1470
a18ed129
RS
1471 ps-font-family '%s
1472 ps-font-size %s
1473 ps-header-font-family '%s
1474 ps-header-font-size %s
1475 ps-header-title-font-size %s)
bcc0d457 1476"
a18ed129
RS
1477 ps-print-color-p
1478 ps-lpr-command
1479 ps-lpr-switches
1480 ps-paper-type
1481 ps-landscape-mode
1482 ps-number-of-columns
01961237
RS
1483 ps-zebra-stripes
1484 ps-zebra-stripe-height
a18ed129
RS
1485 ps-line-number
1486 ps-print-background-image
1487 ps-print-background-text
1488 ps-left-margin
1489 ps-right-margin
1490 ps-inter-column
1491 ps-bottom-margin
1492 ps-top-margin
1493 ps-header-offset
1494 ps-header-line-pad
1495 ps-print-header
1496 ps-print-header-frame
1497 ps-header-lines
1498 ps-show-n-of-n
1499 ps-spool-duplex
1500 ps-font-family
1501 ps-font-size
1502 ps-header-font-family
1503 ps-header-font-size
1504 ps-header-title-font-size))
bcc0d457 1505
12d89a2e
RS
1506;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1507;; Utility functions and variables:
1508
6770a60f
RS
1509(defvar ps-print-emacs-type
1510 (cond ((string-match "XEmacs" emacs-version) 'xemacs)
1511 ((string-match "Lucid" emacs-version) 'lucid)
1512 ((string-match "Epoch" emacs-version) 'epoch)
1513 (t 'emacs)))
1514
1515(if (or (eq ps-print-emacs-type 'lucid)
1516 (eq ps-print-emacs-type 'xemacs))
043620f4
KH
1517 (if (< emacs-minor-version 12)
1518 (setq ps-print-color-p nil))
12d89a2e
RS
1519 (require 'faces)) ; face-font, face-underline-p,
1520 ; x-font-regexp
1521
1522(require 'time-stamp)
1523
bcc0d457
RS
1524(defvar ps-font nil
1525 "Font family name for ordinary text, when generating Postscript.")
1526
1527(defvar ps-font-bold nil
1528 "Font family name for bold text, when generating Postscript.")
1529
1530(defvar ps-font-italic nil
1531 "Font family name for italic text, when generating Postscript.")
1532
1533(defvar ps-font-bold-italic nil
1534 "Font family name for bold italic text, when generating Postscript.")
1535
1536(defvar ps-avg-char-width nil
1537 "The average width, in points, of a character, for generating Postscript.
1538This is the value that ps-print uses to determine the length,
1539x-dimension, of the text it has printed, and thus affects the point at
1540which long lines wrap around.")
1541
1542(defvar ps-space-width nil
1543 "The width of a space character, for generating Postscript.
1544This value is used in expanding tab characters.")
1545
1546(defvar ps-line-height nil
1547 "The height of a line, for generating Postscript.
1548This is the value that ps-print uses to determine the height,
1549y-dimension, of the lines of text it has printed, and thus affects the
1550point at which page-breaks are placed.
1551The line-height is *not* the same as the point size of the font.")
1552
1553(defvar ps-print-prologue-1
1554 "% ISOLatin1Encoding stolen from ps_init.ps in GhostScript 2.6.1.4:
12d89a2e 1555/ISOLatin1Encoding where { pop } {
bcc0d457
RS
1556% -- The ISO Latin-1 encoding vector isn't known, so define it.
1557% -- The first half is the same as the standard encoding,
1558% -- except for minus instead of hyphen at code 055.
12d89a2e
RS
1559/ISOLatin1Encoding
1560StandardEncoding 0 45 getinterval aload pop
1561 /minus
1562StandardEncoding 46 82 getinterval aload pop
1563%*** NOTE: the following are missing in the Adobe documentation,
1564%*** but appear in the displayed table:
1565%*** macron at 0225, dieresis at 0230, cedilla at 0233, space at 0240.
bcc0d457 1566% 0200 (128)
12d89a2e
RS
1567 /.notdef /.notdef /.notdef /.notdef /.notdef /.notdef /.notdef /.notdef
1568 /.notdef /.notdef /.notdef /.notdef /.notdef /.notdef /.notdef /.notdef
1569 /dotlessi /grave /acute /circumflex /tilde /macron /breve /dotaccent
1570 /dieresis /.notdef /ring /cedilla /.notdef /hungarumlaut /ogonek /caron
bcc0d457 1571% 0240 (160)
12d89a2e
RS
1572 /space /exclamdown /cent /sterling
1573 /currency /yen /brokenbar /section
1574 /dieresis /copyright /ordfeminine /guillemotleft
1575 /logicalnot /hyphen /registered /macron
1576 /degree /plusminus /twosuperior /threesuperior
1577 /acute /mu /paragraph /periodcentered
1578 /cedilla /onesuperior /ordmasculine /guillemotright
1579 /onequarter /onehalf /threequarters /questiondown
bcc0d457 1580% 0300 (192)
12d89a2e
RS
1581 /Agrave /Aacute /Acircumflex /Atilde
1582 /Adieresis /Aring /AE /Ccedilla
1583 /Egrave /Eacute /Ecircumflex /Edieresis
1584 /Igrave /Iacute /Icircumflex /Idieresis
1585 /Eth /Ntilde /Ograve /Oacute
1586 /Ocircumflex /Otilde /Odieresis /multiply
1587 /Oslash /Ugrave /Uacute /Ucircumflex
1588 /Udieresis /Yacute /Thorn /germandbls
bcc0d457 1589% 0340 (224)
12d89a2e
RS
1590 /agrave /aacute /acircumflex /atilde
1591 /adieresis /aring /ae /ccedilla
1592 /egrave /eacute /ecircumflex /edieresis
1593 /igrave /iacute /icircumflex /idieresis
1594 /eth /ntilde /ograve /oacute
1595 /ocircumflex /otilde /odieresis /divide
1596 /oslash /ugrave /uacute /ucircumflex
1597 /udieresis /yacute /thorn /ydieresis
1598256 packedarray def
1599} ifelse
1600
1601/reencodeFontISO { %def
1602 dup
87a16a06 1603 length 12 add dict % Make a new font (a new dict the same size
bcc0d457 1604 % as the old one) with room for our new symbols.
12d89a2e 1605
bcc0d457 1606 begin % Make the new font the current dictionary.
12d89a2e
RS
1607
1608
1609 { 1 index /FID ne
1610 { def } { pop pop } ifelse
bcc0d457
RS
1611 } forall % Copy each of the symbols from the old dictionary
1612 % to the new one except for the font ID.
12d89a2e
RS
1613
1614 /Encoding ISOLatin1Encoding def % Override the encoding with
1615 % the ISOLatin1 encoding.
1616
1617 % Use the font's bounding box to determine the ascent, descent,
1618 % and overall height; don't forget that these values have to be
1619 % transformed using the font's matrix.
bcc0d457
RS
1620
1621% ^ (x2 y2)
1622% | |
1623% | v
1624% | +----+ - -
1625% | | | ^
1626% | | | | Ascent (usually > 0)
1627% | | | |
1628% (0 0) -> +--+----+-------->
1629% | | |
1630% | | v Descent (usually < 0)
1631% (x1 y1) --> +----+ - -
1632
1633 FontBBox % -- x1 y1 x2 y2
1634 FontMatrix transform /Ascent exch def pop
12d89a2e 1635 FontMatrix transform /Descent exch def pop
bcc0d457 1636 /FontHeight Ascent Descent sub def % use `sub' because descent < 0
12d89a2e 1637
bcc0d457 1638 % Define these in case they're not in the FontInfo
87a16a06
RS
1639 % (also, here they're easier to get to).
1640 /UnderlinePosition Descent 0.70 mul def
1641 /OverlinePosition Descent UnderlinePosition sub Ascent add def
1642 /StrikeoutPosition Ascent 0.30 mul def
1643 /LineThickness 0 50 FontMatrix transform exch pop def
1644 /Xshadow 0 80 FontMatrix transform exch pop def
1645 /Yshadow 0 -90 FontMatrix transform exch pop def
1646 /SpaceBackground Descent neg UnderlinePosition add def
1647 /XBox Descent neg def
1648 /YBox LineThickness 0.7 mul def
12d89a2e 1649
bcc0d457
RS
1650 currentdict % Leave the new font on the stack
1651 end % Stop using the font as the current dictionary.
1652 definefont % Put the font into the font dictionary
1653 pop % Discard the returned font.
12d89a2e 1654} bind def
ef2cbb24 1655
bcc0d457 1656/DefFont { % Font definition
12d89a2e
RS
1657 findfont exch scalefont reencodeFontISO
1658} def
1659
bcc0d457 1660/F { % Font selection
12d89a2e 1661 findfont
87a16a06
RS
1662 dup /Ascent get /Ascent exch def
1663 dup /Descent get /Descent exch def
1664 dup /FontHeight get /FontHeight exch def
1665 dup /UnderlinePosition get /UnderlinePosition exch def
1666 dup /OverlinePosition get /OverlinePosition exch def
1667 dup /StrikeoutPosition get /StrikeoutPosition exch def
1668 dup /LineThickness get /LineThickness exch def
1669 dup /Xshadow get /Xshadow exch def
1670 dup /Yshadow get /Yshadow exch def
1671 dup /SpaceBackground get /SpaceBackground exch def
1672 dup /XBox get /XBox exch def
1673 dup /YBox get /YBox exch def
12d89a2e
RS
1674 setfont
1675} def
1676
1677/FG /setrgbcolor load def
1678
1679/bg false def
1680/BG {
1681 dup /bg exch def
87a16a06
RS
1682 {mark 4 1 roll ]}
1683 {[ 1.0 1.0 1.0 ]}
1684 ifelse
1685 /bgcolor exch def
12d89a2e
RS
1686} def
1687
bcc0d457
RS
1688% B width C
1689% +-----------+
1690% | Ascent (usually > 0)
1691% A + +
1692% | Descent (usually < 0)
1693% +-----------+
1694% E width D
1695
12d89a2e 1696/dobackground { % width --
bcc0d457 1697 currentpoint % -- width x y
12d89a2e
RS
1698 gsave
1699 newpath
bcc0d457
RS
1700 moveto % A (x y)
1701 0 Ascent rmoveto % B
1702 dup 0 rlineto % C
1703 0 Descent Ascent sub rlineto % D
1704 neg 0 rlineto % E
12d89a2e
RS
1705 closepath
1706 bgcolor aload pop setrgbcolor
1707 fill
1708 grestore
1709} def
1710
bcc0d457
RS
1711/eolbg { % dobackground until right margin
1712 PrintWidth % -- x-eol
1713 currentpoint pop % -- cur-x
1714 sub % -- width until eol
1715 dobackground
12d89a2e
RS
1716} def
1717
87a16a06 1718/PLN {PrintLineNumber {doLineNumber}if} def
12d89a2e
RS
1719
1720/SL { % Soft Linefeed
1721 bg { eolbg } if
bcc0d457 1722 0 currentpoint exch pop LineHeight sub moveto
12d89a2e
RS
1723} def
1724
87a16a06 1725/HL {SL PLN} def % Hard Linefeed
12d89a2e
RS
1726
1727% Some debug
1728/dcp { currentpoint exch 40 string cvs print (, ) print = } def
87a16a06 1729/dp { print 2 copy exch 40 string cvs print (, ) print = } def
12d89a2e
RS
1730
1731/W {
bcc0d457
RS
1732 ( ) stringwidth % Get the width of a space in the current font.
1733 pop % Discard the Y component.
1734 mul % Multiply the width of a space
1735 % by the number of spaces to plot
12d89a2e
RS
1736 bg { dup dobackground } if
1737 0 rmoveto
87a16a06
RS
1738} def
1739
1740/Effect 0 def
1741/EF {/Effect exch def} def
1742
1743% stack: string |- --
1744% effect: 1 - underline 2 - strikeout 4 - overline
1745% 8 - shadow 16 - box 32 - outline
1746/S {
1747 /xx currentpoint dup Descent add /yy exch def
1748 Ascent add /YY exch def def
1749 dup stringwidth pop xx add /XX exch def
1750 Effect 8 and 0 ne {
1751 /yy yy Yshadow add def
1752 /XX XX Xshadow add def
1753 } if
1754 bg {
1755 true
1756 Effect 16 and 0 ne
1757 {SpaceBackground doBox}
1758 {xx yy XX YY doRect}
1759 ifelse
1760 } if % background
1761 Effect 16 and 0 ne {false 0 doBox}if % box
1762 Effect 8 and 0 ne {dup doShadow}if % shadow
1763 Effect 32 and 0 ne
1764 {true doOutline} % outline
1765 {show} % normal text
1766 ifelse
1767 Effect 1 and 0 ne {UnderlinePosition Hline}if % underline
1768 Effect 2 and 0 ne {StrikeoutPosition Hline}if % strikeout
1769 Effect 4 and 0 ne {OverlinePosition Hline}if % overline
1770} bind def
1771
1772% stack: position |- --
1773/Hline {
1774 currentpoint exch pop add dup
1775 gsave
1776 newpath
1777 xx exch moveto
1778 XX exch lineto
1779 closepath
1780 LineThickness setlinewidth stroke
1781 grestore
1782} bind def
1783
1784% stack: fill-or-not delta |- --
1785/doBox {
1786 /dd exch def
1787 xx XBox sub dd sub yy YBox sub dd sub
1788 XX XBox add dd add YY YBox add dd add
1789 doRect
1790} bind def
1791
1792% stack: fill-or-not lower-x lower-y upper-x upper-y |- --
1793/doRect {
1794 /rYY exch def
1795 /rXX exch def
1796 /ryy exch def
1797 /rxx exch def
1798 gsave
1799 newpath
1800 rXX rYY moveto
1801 rxx rYY lineto
1802 rxx ryy lineto
1803 rXX ryy lineto
1804 closepath
1805 % top of stack: fill-or-not
1806 {FillBgColor}
1807 {LineThickness setlinewidth stroke}
1808 ifelse
1809 grestore
1810} bind def
1811
1812% stack: string |- --
1813/doShadow {
1814 gsave
1815 Xshadow Yshadow rmoveto
1816 false doOutline
1817 grestore
1818} bind def
1819
1820/st 1 string def
1821
1822% stack: string fill-or-not |- --
1823/doOutline {
1824 /-fillp- exch def
1825 /-ox- currentpoint /-oy- exch def def
1826 gsave
1827 LineThickness setlinewidth
1828 {
1829 st 0 3 -1 roll put
1830 st dup true charpath
1831 -fillp- {gsave FillBgColor grestore}if
1832 stroke stringwidth
1833 -oy- add /-oy- exch def
1834 -ox- add /-ox- exch def
1835 -ox- -oy- moveto
1836 } forall
1837 grestore
1838 -ox- -oy- moveto
1839} bind def
1840
1841% stack: --
1842/FillBgColor {bgcolor aload pop setrgbcolor fill} bind def
1843
1844/L0 6 /Times-Italic DefFont
1845
1846% stack: --
1847/doLineNumber {
1848 currentfont
1849 gsave
1850 0.0 0.0 0.0 setrgbcolor
1851 /L0 findfont setfont
1852 LineNumber Lines ge
1853 {(end )}
1854 {LineNumber 6 string cvs ( ) strcat}
1855 ifelse
1856 dup stringwidth pop neg 0 rmoveto
1857 show
1858 grestore
1859 setfont
1860 /LineNumber LineNumber 1 add def
1861} def
1862
1863% stack: --
1864/printZebra {
1865 gsave
1866 0.985 setgray
1867 /double-zebra NumberOfZebra NumberOfZebra add def
1868 /yiter double-zebra LineHeight mul neg def
1869 /xiter PrintWidth InterColumn add def
1870 NumberOfColumns {LinesPerColumn doColumnZebra xiter 0 rmoveto}repeat
1871 grestore
1872} def
1873
1874% stack: lines-per-column |- --
1875/doColumnZebra {
1876 gsave
1877 dup double-zebra idiv {NumberOfZebra doZebra 0 yiter rmoveto}repeat
1878 double-zebra mod
1879 dup 0 le {pop}{dup NumberOfZebra gt {pop NumberOfZebra}if doZebra}ifelse
1880 grestore
1881} def
1882
1883% stack: zebra-height (in lines) |- --
1884/doZebra {
1885 /zh exch 0.05 sub LineHeight mul def
1886 gsave
1887 0 LineHeight 0.65 mul rmoveto
1888 PrintWidth 0 rlineto
1889 0 zh neg rlineto
1890 PrintWidth neg 0 rlineto
1891 0 zh rlineto
1892 fill
1893 grestore
1894} def
1895
1896% tx ty rotation xscale yscale xpos ypos BeginBackImage
1897/BeginBackImage {
1898 /-save-image- save def
1899 /showpage {}def
1900 translate
1901 scale
1902 rotate
1903 translate
1904} def
1905
1906/EndBackImage {
1907 -save-image- restore
1908} def
1909
1910% string fontsize fontname rotation gray xpos ypos ShowBackText
1911/ShowBackText {
1912 gsave
1913 translate
1914 setgray
1915 rotate
1916 findfont exch dup /-offset- exch -0.25 mul def scalefont setfont
1917 0 -offset- moveto
1918 /-saveLineThickness- LineThickness def
1919 /LineThickness 1 def
1920 false doOutline
1921 /LineThickness -saveLineThickness- def
1922 grestore
12d89a2e
RS
1923} def
1924
bcc0d457
RS
1925/BeginDoc {
1926 % ---- save the state of the document (useful for ghostscript!)
1927 /docState save def
1928 % ---- [jack] Kludge: my ghostscript window is 21x27.7 instead of 21x29.7
1929 /JackGhostscript where {
1930 pop 1 27.7 29.7 div scale
1931 } if
1932 LandscapeMode {
1933 % ---- translate to bottom-right corner of Portrait page
1934 LandscapePageHeight 0 translate
1935 90 rotate
1936 } if
1937 /ColumnWidth PrintWidth InterColumn add def
1938 % ---- translate to lower left corner of TEXT
1939 LeftMargin BottomMargin translate
1940 % ---- define where printing will start
1941 /f0 F % this installs Ascent
1942 /PrintStartY PrintHeight Ascent sub def
1943 /ColumnIndex 1 def
1944} def
1945
1946/EndDoc {
1947 % ---- on last page but not last column, spit out the page
1948 ColumnIndex 1 eq not { showpage } if
1949 % ---- restore the state of the document (useful for ghostscript!)
1950 docState restore
1951} def
1952
12d89a2e 1953/BeginDSCPage {
bcc0d457 1954 % ---- when 1st column, save the state of the page
a18ed129 1955 ColumnIndex 1 eq { /pageState save def } if
bcc0d457
RS
1956 % ---- save the state of the column
1957 /columnState save def
12d89a2e
RS
1958} def
1959
1960/BeginPage {
a18ed129
RS
1961 % ---- when 1st column, print all background effects
1962 ColumnIndex 1 eq {
1963 0 PrintStartY moveto % move to where printing will start
1964 Zebra {printZebra}if
1965 printGlobalBackground
1966 printLocalBackground
1967 } if
12d89a2e
RS
1968 PrintHeader {
1969 PrintHeaderFrame { HeaderFrame } if
1970 HeaderText
1971 } if
bcc0d457 1972 0 PrintStartY moveto % move to where printing will start
87a16a06 1973 PLN
12d89a2e
RS
1974} def
1975
1976/EndPage {
1977 bg { eolbg } if
12d89a2e
RS
1978} def
1979
1980/EndDSCPage {
bcc0d457
RS
1981 ColumnIndex NumberOfColumns eq {
1982 % ---- on last column, spit out the page
1983 showpage
1984 % ---- restore the state of the page
1985 pageState restore
1986 /ColumnIndex 1 def
1987 } { % else
1988 % ---- restore the state of the current column
1989 columnState restore
1990 % ---- and translate to the next column
1991 ColumnWidth 0 translate
1992 /ColumnIndex ColumnIndex 1 add def
1993 } ifelse
12d89a2e
RS
1994} def
1995
bcc0d457 1996/SetHeaderLines { % nb-lines --
12d89a2e 1997 /HeaderLines exch def
bcc0d457
RS
1998 % ---- bottom up
1999 HeaderPad
2000 HeaderLines 1 sub HeaderLineHeight mul add
2001 HeaderTitleLineHeight add
2002 HeaderPad add
2003 /HeaderHeight exch def
12d89a2e
RS
2004} def
2005
bcc0d457
RS
2006% |---------|
2007% | tm |
2008% |---------|
2009% | header |
2010% |-+-------| <-- (x y)
2011% | ho |
2012% |---------|
2013% | text |
2014% |-+-------| <-- (0 0)
2015% | bm |
2016% |---------|
2017
2018/HeaderFrameStart { % -- x y
2019 0 PrintHeight HeaderOffset add
12d89a2e
RS
2020} def
2021
2022/HeaderFramePath {
bcc0d457
RS
2023 PrintWidth 0 rlineto
2024 0 HeaderHeight rlineto
2025 PrintWidth neg 0 rlineto
2026 0 HeaderHeight neg rlineto
12d89a2e
RS
2027} def
2028
2029/HeaderFrame {
2030 gsave
2031 0.4 setlinewidth
bcc0d457 2032 % ---- fill a black rectangle (the shadow of the next one)
12d89a2e
RS
2033 HeaderFrameStart moveto
2034 1 -1 rmoveto
2035 HeaderFramePath
2036 0 setgray fill
bcc0d457 2037 % ---- do the next rectangle ...
12d89a2e
RS
2038 HeaderFrameStart moveto
2039 HeaderFramePath
bcc0d457
RS
2040 gsave 0.9 setgray fill grestore % filled with grey
2041 gsave 0 setgray stroke grestore % drawn with black
12d89a2e
RS
2042 grestore
2043} def
2044
2045/HeaderStart {
2046 HeaderFrameStart
bcc0d457
RS
2047 exch HeaderPad add exch % horizontal pad
2048 % ---- bottom up
2049 HeaderPad add % vertical pad
2050 HeaderDescent sub
2051 HeaderLineHeight HeaderLines 1 sub mul add
12d89a2e
RS
2052} def
2053
2054/strcat {
2055 dup length 3 -1 roll dup length dup 4 -1 roll add string dup
2056 0 5 -1 roll putinterval
2057 dup 4 2 roll exch putinterval
2058} def
2059
2060/pagenumberstring {
2061 PageNumber 32 string cvs
2062 ShowNofN {
2063 (/) strcat
2064 PageCount 32 string cvs strcat
2065 } if
2066} def
2067
2068/HeaderText {
2069 HeaderStart moveto
2070
bcc0d457
RS
2071 HeaderLinesRight HeaderLinesLeft % -- rightLines leftLines
2072
2073 % ---- hack: `PN 1 and' == `PN 2 modulo'
2074
2075 % ---- if duplex and even page number, then exchange left and right
12d89a2e
RS
2076 Duplex PageNumber 1 and 0 eq and { exch } if
2077
bcc0d457 2078 { % ---- process the left lines
12d89a2e
RS
2079 aload pop
2080 exch F
2081 gsave
2082 dup xcheck { exec } if
2083 show
2084 grestore
2085 0 HeaderLineHeight neg rmoveto
2086 } forall
2087
2088 HeaderStart moveto
2089
bcc0d457 2090 { % ---- process the right lines
12d89a2e
RS
2091 aload pop
2092 exch F
2093 gsave
2094 dup xcheck { exec } if
2095 dup stringwidth pop
2096 PrintWidth exch sub HeaderPad 2 mul sub 0 rmoveto
2097 show
2098 grestore
2099 0 HeaderLineHeight neg rmoveto
2100 } forall
2101} def
2102
2103/ReportFontInfo {
2104 2 copy
bcc0d457 2105 /t0 3 1 roll DefFont
12d89a2e 2106 /t0 F
00aa16af 2107 /lh FontHeight def
12d89a2e
RS
2108 /sw ( ) stringwidth pop def
2109 /aw (01234567890abcdefghijklmnopqrstuvwxyz) dup length exch
2110 stringwidth pop exch div def
bcc0d457 2111 /t1 12 /Helvetica-Oblique DefFont
12d89a2e 2112 /t1 F
12d89a2e
RS
2113 gsave
2114 (For ) show
2115 128 string cvs show
2116 ( ) show
2117 32 string cvs show
2118 ( point, the line height is ) show
2119 lh 32 string cvs show
2120 (, the space width is ) show
2121 sw 32 string cvs show
2122 (,) show
2123 grestore
00aa16af 2124 0 FontHeight neg rmoveto
bcc0d457
RS
2125 gsave
2126 (and a crude estimate of average character width is ) show
2127 aw 32 string cvs show
2128 (.) show
2129 grestore
2130 0 FontHeight neg rmoveto
2131} def
2132
2133/cm { % cm to point
2134 72 mul 2.54 div
2135} def
2136
2137/ReportAllFontInfo {
2138 FontDirectory
2139 { % key = font name value = font dictionary
2140 pop 10 exch ReportFontInfo
2141 } forall
12d89a2e
RS
2142} def
2143
bcc0d457
RS
2144% 3 cm 20 cm moveto 10 /Courier ReportFontInfo showpage
2145% 3 cm 20 cm moveto ReportAllFontInfo showpage
2146
2147")
2148
2149(defvar ps-print-prologue-2
2150 "
2151% ---- These lines must be kept together because...
2152
2153/h0 F
2154/HeaderTitleLineHeight FontHeight def
2155
2156/h1 F
2157/HeaderLineHeight FontHeight def
2158/HeaderDescent Descent def
2159
2160% ---- ...because `F' has a side-effect on `FontHeight' and `Descent'
2161
12d89a2e
RS
2162")
2163
2164;; Start Editing Here:
ef2cbb24 2165
12d89a2e
RS
2166(defvar ps-source-buffer nil)
2167(defvar ps-spool-buffer-name "*PostScript*")
2168(defvar ps-spool-buffer nil)
ef2cbb24 2169
12d89a2e
RS
2170(defvar ps-output-head nil)
2171(defvar ps-output-tail nil)
ef2cbb24 2172
12d89a2e 2173(defvar ps-page-count 0)
87a16a06
RS
2174(defvar ps-showline-count 1)
2175
2176(defvar ps-background-pages nil)
2177(defvar ps-background-all-pages nil)
2178(defvar ps-background-text-count 0)
2179(defvar ps-background-image-count 0)
ef2cbb24 2180
12d89a2e 2181(defvar ps-current-font 0)
12d89a2e
RS
2182(defvar ps-default-color (if ps-print-color-p ps-default-fg)) ; black
2183(defvar ps-current-color ps-default-color)
2184(defvar ps-current-bg nil)
2185
2186(defvar ps-razchunk 0)
2187
bcc0d457
RS
2188(defvar ps-color-format
2189 (if (eq ps-print-emacs-type 'emacs)
12d89a2e 2190
bcc0d457
RS
2191 ;;Emacs understands the %f format; we'll
2192 ;;use it to limit color RGB values to
2193 ;;three decimals to cut down some on the
2194 ;;size of the PostScript output.
2195 "%0.3f %0.3f %0.3f"
12d89a2e 2196
bcc0d457
RS
2197 ;; Lucid emacsen will have to make do with
2198 ;; %s (princ) for floats.
2199 "%s %s %s"))
12d89a2e
RS
2200
2201;; These values determine how much print-height to deduct when headers
2202;; are turned on. This is a pretty clumsy way of handling it, but
2203;; it'll do for now.
12d89a2e 2204
87a16a06
RS
2205(defvar ps-header-font nil)
2206(defvar ps-header-title-font nil)
12d89a2e 2207
87a16a06
RS
2208(defvar ps-header-line-height nil)
2209(defvar ps-header-title-line-height nil)
bcc0d457
RS
2210(defvar ps-header-pad 0
2211 "Vertical and horizontal space in points (1/72 inch) between the header frame
2212and the text it contains.")
12d89a2e 2213
bcc0d457 2214;; Define accessors to the dimensions list.
12d89a2e 2215
bcc0d457
RS
2216(defmacro ps-page-dimensions-get-width (dims) `(nth 0 ,dims))
2217(defmacro ps-page-dimensions-get-height (dims) `(nth 1 ,dims))
12d89a2e 2218
87a16a06 2219(defvar ps-landscape-page-height nil)
12d89a2e 2220
12d89a2e
RS
2221(defvar ps-print-width nil)
2222(defvar ps-print-height nil)
2223
2224(defvar ps-height-remaining)
2225(defvar ps-width-remaining)
2226
bcc0d457
RS
2227(defvar ps-print-color-scale nil)
2228
87a16a06
RS
2229\f
2230;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2231;; Internal Variables
2232
2233
2234(defvar ps-print-face-extension-alist nil
a18ed129 2235 "Alist of symbolic faces *WITH* extension features (box, outline, etc).
87a16a06
RS
2236An element of this list has the following form:
2237
2238 (FACE . [BITS FG BG])
2239
2240 FACE is a symbol denoting a face name
2241 BITS is a bit vector, where each bit correspond
2242 to a feature (bold, underline, etc)
2243 (see documentation for `ps-print-face-map-alist')
2244 FG foreground color (string or nil)
2245 BG background color (string or nil)
2246
a18ed129
RS
2247Don't change this list directly; instead,
2248use `ps-extend-face' and `ps-extend-face-list'.
2249See documentation for `ps-extend-face' for valid extension symbol.")
2250
2251
2252(defvar ps-print-face-alist nil
2253 "Alist of symbolic faces *WITHOUT* extension features (box, outline, etc).
2254
2255An element of this list has the same form as an element of
2256`ps-print-face-extension-alist'.
2257
2258Don't change this list directly; this list is used by `ps-face-attributes',
2259`ps-map-face' and `ps-build-reference-face-lists'.")
87a16a06
RS
2260
2261
2262(defconst ps-print-face-map-alist
2263 '((bold . 1)
2264 (italic . 2)
2265 (underline . 4)
2266 (strikeout . 8)
2267 (overline . 16)
2268 (shadow . 32)
2269 (box . 64)
2270 (outline . 128))
2271 "Alist of all features and the corresponding bit mask.
2272Each symbol correspond to one bit in a bit vector.")
2273
2274\f
2275;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
a18ed129 2276;; Remapping Faces
87a16a06
RS
2277
2278
2279;;;###autoload
2280(defun ps-extend-face-list (face-extension-list &optional merge-p)
2281 "Extend face in `ps-print-face-extension-alist'.
2282
a18ed129
RS
2283If optional MERGE-P is non-nil, extensions in FACE-EXTENSION-LIST are merged
2284with face extension in `ps-print-face-extension-alist'; otherwise, overrides.
87a16a06
RS
2285
2286The elements in FACE-EXTENSION-LIST is like those for `ps-extend-face'.
2287
2288See `ps-extend-face' for documentation."
2289 (while face-extension-list
2290 (ps-extend-face (car face-extension-list) merge-p)
2291 (setq face-extension-list (cdr face-extension-list))))
2292
2293
2294;;;###autoload
2295(defun ps-extend-face (face-extension &optional merge-p)
2296 "Extend face in `ps-print-face-extension-alist'.
2297
a18ed129
RS
2298If optional MERGE-P is non-nil, extensions in FACE-EXTENSION-LIST are merged
2299with face extensions in `ps-print-face-extension-alist'; otherwise, overrides.
87a16a06
RS
2300
2301The elements of FACE-EXTENSION list have the form:
2302
2303 (FACE-NAME FOREGROUND BACKGROUND EXTENSION...)
2304
2305FACE-NAME is a face name symbol.
2306
2307FOREGROUND and BACKGROUND may be nil or a string that denotes the
2308foreground and background colors respectively.
2309
2310EXTENSION is one of the following symbols:
2311 bold - use bold font.
2312 italic - use italic font.
2313 underline - put a line under text.
2314 strikeout - like underline, but the line is in middle of text.
2315 overline - like underline, but the line is over the text.
2316 shadow - text will have a shadow.
2317 box - text will be surrounded by a box.
a18ed129 2318 outline - print characters as hollow outlines.
87a16a06
RS
2319
2320If EXTENSION is any other symbol, it is ignored."
2321 (let* ((face-name (nth 0 face-extension))
2322 (foreground (nth 1 face-extension))
2323 (background (nth 2 face-extension))
2324 (ps-face (cdr (assq face-name ps-print-face-extension-alist)))
2325 (face-vector (or ps-face (vector 0 nil nil)))
2326 (face-bit (ps-extension-bit face-extension)))
2327 ;; extend face
2328 (aset face-vector 0 (if merge-p
2329 (logior (aref face-vector 0) face-bit)
2330 face-bit))
2331 (and foreground (stringp foreground) (aset face-vector 1 foreground))
2332 (and background (stringp background) (aset face-vector 2 background))
2333 ;; if face does not exist, insert it
2334 (or ps-face
2335 (setq ps-print-face-extension-alist
2336 (cons (cons face-name face-vector)
2337 ps-print-face-extension-alist)))))
2338
2339
2340(defun ps-extension-bit (face-extension)
2341 (let ((face-bit 0))
2342 ;; map valid symbol extension to bit vector
2343 (setq face-extension (cdr (cdr face-extension)))
2344 (while (setq face-extension (cdr face-extension))
2345 (setq face-bit (logior face-bit
2346 (or (cdr (assq (car face-extension)
2347 ps-print-face-map-alist))
2348 0))))
2349 face-bit))
2350
2351\f
2352;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2353;; Internal functions and variables
2354
2355
a18ed129
RS
2356(defun ps-print-without-faces (from to &optional filename region-p)
2357 (ps-printing-region region-p)
87a16a06
RS
2358 (ps-generate (current-buffer) from to 'ps-generate-postscript)
2359 (ps-do-despool filename))
2360
2361
a18ed129
RS
2362(defun ps-spool-without-faces (from to &optional region-p)
2363 (ps-printing-region region-p)
87a16a06
RS
2364 (ps-generate (current-buffer) from to 'ps-generate-postscript))
2365
2366
a18ed129
RS
2367(defun ps-print-with-faces (from to &optional filename region-p)
2368 (ps-printing-region region-p)
87a16a06
RS
2369 (ps-generate (current-buffer) from to 'ps-generate-postscript-with-faces)
2370 (ps-do-despool filename))
2371
2372
a18ed129
RS
2373(defun ps-spool-with-faces (from to &optional region-p)
2374 (ps-printing-region region-p)
87a16a06
RS
2375 (ps-generate (current-buffer) from to 'ps-generate-postscript-with-faces))
2376
2377
a18ed129
RS
2378(defsubst ps-count-lines (from to)
2379 (+ (count-lines from to)
2380 (save-excursion (goto-char to)
2381 (if (= (current-column) 0) 1 0))))
87a16a06
RS
2382
2383
a18ed129
RS
2384(defvar ps-printing-region nil
2385 "Variable used to indicate if it is printing a region.
2386If non-nil, it is a cons, the car of which is the line number
2387where the region begins, and its cdr is the total number of lines
2388in the buffer. Formatting functions can use this information
2389to print the original line number (and not the number of lines printed),
2390and to indicate in the header that the printout is of a partial file.")
87a16a06
RS
2391
2392
a18ed129
RS
2393(defun ps-printing-region (region-p)
2394 (setq ps-printing-region
2395 (and region-p
2396 (cons (ps-count-lines (point-min) (region-beginning))
2397 (ps-count-lines (point-min) (point-max))))))
87a16a06
RS
2398
2399\f
12d89a2e
RS
2400;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2401;; Internal functions
2402
bcc0d457 2403(defun ps-line-lengths-internal ()
87a16a06 2404 "Display the correspondence between a line length and a font size,
bcc0d457
RS
2405using the current ps-print setup.
2406Try: pr -t file | awk '{printf \"%3d %s\n\", length($0), $0}' | sort -r | head"
2407 (let ((buf (get-buffer-create "*Line-lengths*"))
2408 (ifs ps-font-size) ; initial font size
2409 (icw ps-avg-char-width) ; initial character width
2410 (print-width (progn (ps-get-page-dimensions)
2411 ps-print-width))
2412 (ps-setup (ps-setup)) ; setup for the current buffer
2413 (fs-min 5) ; minimum font size
2414 cw-min ; minimum character width
2415 nb-cpl-max ; maximum nb of characters per line
2416 (fs-max 14) ; maximum font size
2417 cw-max ; maximum character width
2418 nb-cpl-min ; minimum nb of characters per line
2419 fs ; current font size
2420 cw ; current character width
2421 nb-cpl ; current nb of characters per line
2422 )
2423 (setq cw-min (/ (* icw fs-min) ifs)
2424 nb-cpl-max (floor (/ print-width cw-min))
2425 cw-max (/ (* icw fs-max) ifs)
2426 nb-cpl-min (floor (/ print-width cw-max)))
2427 (setq nb-cpl nb-cpl-min)
2428 (set-buffer buf)
2429 (goto-char (point-max))
2430 (if (not (bolp)) (insert "\n"))
2431 (insert ps-setup)
2432 (insert "nb char per line / font size\n")
2433 (while (<= nb-cpl nb-cpl-max)
2434 (setq cw (/ print-width (float nb-cpl))
2435 fs (/ (* ifs cw) icw))
2436 (insert (format "%3s %s\n" nb-cpl fs))
2437 (setq nb-cpl (1+ nb-cpl)))
2438 (insert "\n")
2439 (display-buffer buf 'not-this-window)))
2440
2441(defun ps-nb-pages (nb-lines)
87a16a06 2442 "Display an approximate correspondence between a font size and the number
bcc0d457
RS
2443of pages the number of lines would require to print
2444using the current ps-print setup."
2445 (let ((buf (get-buffer-create "*Nb-Pages*"))
2446 (ifs ps-font-size) ; initial font size
2447 (ilh ps-line-height) ; initial line height
2448 (page-height (progn (ps-get-page-dimensions)
2449 ps-print-height))
2450 (ps-setup (ps-setup)) ; setup for the current buffer
2451 (fs-min 4) ; minimum font size
2452 lh-min ; minimum line height
2453 nb-lpp-max ; maximum nb of lines per page
2454 nb-page-min ; minimum nb of pages
2455 (fs-max 14) ; maximum font size
2456 lh-max ; maximum line height
2457 nb-lpp-min ; minimum nb of lines per page
2458 nb-page-max ; maximum nb of pages
2459 fs ; current font size
2460 lh ; current line height
2461 nb-lpp ; current nb of lines per page
2462 nb-page ; current nb of pages
2463 )
2464 (setq lh-min (/ (* ilh fs-min) ifs)
2465 nb-lpp-max (floor (/ page-height lh-min))
2466 nb-page-min (ceiling (/ (float nb-lines) nb-lpp-max))
2467 lh-max (/ (* ilh fs-max) ifs)
2468 nb-lpp-min (floor (/ page-height lh-max))
2469 nb-page-max (ceiling (/ (float nb-lines) nb-lpp-min)))
2470 (setq nb-page nb-page-min)
2471 (set-buffer buf)
2472 (goto-char (point-max))
2473 (if (not (bolp)) (insert "\n"))
2474 (insert ps-setup)
2475 (insert (format "%d lines\n" nb-lines))
2476 (insert "nb page / font size\n")
2477 (while (<= nb-page nb-page-max)
2478 (setq nb-lpp (ceiling (/ nb-lines (float nb-page)))
2479 lh (/ page-height nb-lpp)
2480 fs (/ (* ifs lh) ilh))
2481 (insert (format "%s %s\n" nb-page fs))
2482 (setq nb-page (1+ nb-page)))
2483 (insert "\n")
2484 (display-buffer buf 'not-this-window)))
2485
2486(defun ps-select-font ()
2487 "Choose the font name and size (scaling data)."
2488 (let ((assoc (assq ps-font-family ps-font-info-database))
2489 l fn fb fi bi sz lh sw aw)
2490 (if (null assoc)
2491 (error "Don't have data to scale font %s. Known fonts families are %s"
2492 ps-font-family
2493 (mapcar 'car ps-font-info-database)))
2494 (setq l (cdr assoc)
2495 fn (prog1 (car l) (setq l (cdr l))) ; need `pop'
2496 fb (prog1 (car l) (setq l (cdr l)))
2497 fi (prog1 (car l) (setq l (cdr l)))
2498 bi (prog1 (car l) (setq l (cdr l)))
2499 sz (prog1 (car l) (setq l (cdr l)))
2500 lh (prog1 (car l) (setq l (cdr l)))
2501 sw (prog1 (car l) (setq l (cdr l)))
2502 aw (prog1 (car l) (setq l (cdr l))))
2503
2504 (setq ps-font fn)
2505 (setq ps-font-bold fb)
2506 (setq ps-font-italic fi)
2507 (setq ps-font-bold-italic bi)
2508 ;; These data just need to be rescaled:
2509 (setq ps-line-height (/ (* lh ps-font-size) sz))
2510 (setq ps-space-width (/ (* sw ps-font-size) sz))
2511 (setq ps-avg-char-width (/ (* aw ps-font-size) sz))
2512 ps-font-family))
2513
2514(defun ps-select-header-font ()
2515 "Choose the font name and size (scaling data) for the header."
2516 (let ((assoc (assq ps-header-font-family ps-font-info-database))
2517 l fn fb fi bi sz lh sw aw)
2518 (if (null assoc)
2519 (error "Don't have data to scale font %s. Known fonts families are %s"
2520 ps-font-family
2521 (mapcar 'car ps-font-info-database)))
2522 (setq l (cdr assoc)
2523 fn (prog1 (car l) (setq l (cdr l))) ; need `pop'
2524 fb (prog1 (car l) (setq l (cdr l)))
2525 fi (prog1 (car l) (setq l (cdr l)))
2526 bi (prog1 (car l) (setq l (cdr l)))
2527 sz (prog1 (car l) (setq l (cdr l)))
2528 lh (prog1 (car l) (setq l (cdr l)))
2529 sw (prog1 (car l) (setq l (cdr l)))
2530 aw (prog1 (car l) (setq l (cdr l))))
2531
2532 ;; Font name
2533 (setq ps-header-font fn)
2534 (setq ps-header-title-font fb)
2535 ;; Line height: These data just need to be rescaled:
2536 (setq ps-header-title-line-height (/ (* lh ps-header-title-font-size) sz))
2537 (setq ps-header-line-height (/ (* lh ps-header-font-size) sz))
2538 ps-header-font-family))
2539
12d89a2e 2540(defun ps-get-page-dimensions ()
bcc0d457
RS
2541 (let ((page-dimensions (cdr (assq ps-paper-type ps-page-dimensions-database)))
2542 page-width page-height)
2543 (cond
2544 ((null page-dimensions)
2545 (error "`ps-paper-type' must be one of:\n%s"
2546 (mapcar 'car ps-page-dimensions-database)))
2547 ((< ps-number-of-columns 1)
87a16a06 2548 (error "The number of columns %d should not be negative" ps-number-of-columns)))
bcc0d457
RS
2549
2550 (ps-select-font)
2551 (ps-select-header-font)
2552
2553 (setq page-width (ps-page-dimensions-get-width page-dimensions)
2554 page-height (ps-page-dimensions-get-height page-dimensions))
2555
2556 ;; Landscape mode
2557 (if ps-landscape-mode
2558 ;; exchange width and height
2559 (setq page-width (prog1 page-height (setq page-height page-width))))
2560
2561 ;; It is used to get the lower right corner (only in landscape mode)
2562 (setq ps-landscape-page-height page-height)
2563
2564 ;; | lm | text | ic | text | ic | text | rm |
2565 ;; page-width == lm + n * pw + (n - 1) * ic + rm
2566 ;; => pw == (page-width - lm -rm - (n - 1) * ic) / n
2567 (setq ps-print-width
2568 (/ (- page-width
2569 ps-left-margin ps-right-margin
2570 (* (1- ps-number-of-columns) ps-inter-column))
2571 ps-number-of-columns))
2572 (if (<= ps-print-width 0)
2573 (error "Bad horizontal layout:
2574page-width == %s
2575ps-left-margin == %s
2576ps-right-margin == %s
2577ps-inter-column == %s
2578ps-number-of-columns == %s
2579| lm | text | ic | text | ic | text | rm |
2580page-width == lm + n * print-width + (n - 1) * ic + rm
2581=> print-width == %d !"
2582 page-width
2583 ps-left-margin
2584 ps-right-margin
2585 ps-inter-column
2586 ps-number-of-columns
2587 ps-print-width))
2588
2589 (setq ps-print-height
2590 (- page-height ps-bottom-margin ps-top-margin))
2591 (if (<= ps-print-height 0)
2592 (error "Bad vertical layout:
2593ps-top-margin == %s
2594ps-bottom-margin == %s
2595page-height == bm + print-height + tm
2596=> print-height == %d !"
2597 ps-top-margin
2598 ps-bottom-margin
2599 ps-print-height))
2600 ;; If headers are turned on, deduct the height of the header from
2601 ;; the print height.
2602 (cond
2603 (ps-print-header
2604 (setq ps-header-pad
2605 (* ps-header-line-pad ps-header-title-line-height))
2606 (setq ps-print-height
2607 (- ps-print-height
2608 ps-header-offset
2609 ps-header-pad
2610 ps-header-title-line-height
2611 (* ps-header-line-height (- ps-header-lines 1))
2612 ps-header-pad))))
2613 (if (<= ps-print-height 0)
2614 (error "Bad vertical layout:
2615ps-top-margin == %s
2616ps-bottom-margin == %s
2617ps-header-offset == %s
2618ps-header-pad == %s
2619header-height == %s
2620page-height == bm + print-height + tm - ho - hh
2621=> print-height == %d !"
2622 ps-top-margin
2623 ps-bottom-margin
2624 ps-header-offset
2625 ps-header-pad
2626 (+ ps-header-pad
2627 ps-header-title-line-height
2628 (* ps-header-line-height (- ps-header-lines 1))
2629 ps-header-pad)
2630 ps-print-height))))
ef2cbb24 2631
12d89a2e 2632(defun ps-print-preprint (&optional filename)
ef2cbb24
RS
2633 (if (and filename
2634 (or (numberp filename)
2635 (listp filename)))
12d89a2e
RS
2636 (let* ((name (concat (buffer-name) ".ps"))
2637 (prompt (format "Save PostScript to file: (default %s) "
03f962fb
RS
2638 name))
2639 (res (read-file-name prompt default-directory name nil)))
2640 (if (file-directory-p res)
2641 (expand-file-name name (file-name-as-directory res))
2642 res))))
12d89a2e
RS
2643
2644;; The following functions implement a simple list-buffering scheme so
2645;; that ps-print doesn't have to repeatedly switch between buffers
2646;; while spooling. The functions ps-output and ps-output-string build
2647;; up the lists; the function ps-flush-output takes the lists and
2648;; insert its contents into the spool buffer (*PostScript*).
2649
2650(defun ps-output-string-prim (string)
2651 (insert "(") ;insert start-string delimiter
2652 (save-excursion ;insert string
2653 (insert string))
2654
2655 ;; Find and quote special characters as necessary for PS
2656 (while (re-search-forward "[()\\]" nil t)
2657 (save-excursion
2658 (forward-char -1)
2659 (insert "\\")))
ef2cbb24 2660
12d89a2e
RS
2661 (goto-char (point-max))
2662 (insert ")")) ;insert end-string delimiter
ef2cbb24 2663
12d89a2e
RS
2664(defun ps-init-output-queue ()
2665 (setq ps-output-head (list ""))
2666 (setq ps-output-tail ps-output-head))
ef2cbb24 2667
12d89a2e
RS
2668(defun ps-output (&rest args)
2669 (setcdr ps-output-tail args)
2670 (while (cdr ps-output-tail)
2671 (setq ps-output-tail (cdr ps-output-tail))))
ef2cbb24 2672
12d89a2e
RS
2673(defun ps-output-string (string)
2674 (ps-output t string))
ef2cbb24 2675
87a16a06
RS
2676(defun ps-output-list (the-list)
2677 (mapcar 'ps-output the-list))
2678
12d89a2e
RS
2679(defun ps-flush-output ()
2680 (save-excursion
2681 (set-buffer ps-spool-buffer)
2682 (goto-char (point-max))
2683 (while ps-output-head
2684 (let ((it (car ps-output-head)))
2685 (if (not (eq t it))
2686 (insert it)
2687 (setq ps-output-head (cdr ps-output-head))
2688 (ps-output-string-prim (car ps-output-head))))
2689 (setq ps-output-head (cdr ps-output-head))))
2690 (ps-init-output-queue))
2691
2692(defun ps-insert-file (fname)
2693 (ps-flush-output)
12d89a2e 2694 ;; Check to see that the file exists and is readable; if not, throw
87a16a06
RS
2695 ;; an error.
2696 (or (file-readable-p fname)
12d89a2e 2697 (error "Could not read file `%s'" fname))
12d89a2e
RS
2698 (save-excursion
2699 (set-buffer ps-spool-buffer)
2700 (goto-char (point-max))
2701 (insert-file fname)))
06fb6aab 2702
12d89a2e
RS
2703;; These functions insert the arrays that define the contents of the
2704;; headers.
ef2cbb24 2705
12d89a2e
RS
2706(defun ps-generate-header-line (fonttag &optional content)
2707 (ps-output " [ " fonttag " ")
2708 (cond
2709 ;; Literal strings should be output as is -- the string must
2710 ;; contain its own PS string delimiters, '(' and ')', if necessary.
2711 ((stringp content)
2712 (ps-output content))
2713
2714 ;; Functions are called -- they should return strings; they will be
2715 ;; inserted as strings and the PS string delimiters added.
2716 ((and (symbolp content) (fboundp content))
2717 (ps-output-string (funcall content)))
2718
2719 ;; Variables will have their contents inserted. They should
2720 ;; contain strings, and will be inserted as strings.
2721 ((and (symbolp content) (boundp content))
2722 (ps-output-string (symbol-value content)))
2723
2724 ;; Anything else will get turned into an empty string.
2725 (t
2726 (ps-output-string "")))
2727 (ps-output " ]\n"))
2728
2729(defun ps-generate-header (name contents)
2730 (ps-output "/" name " [\n")
2731 (if (> ps-header-lines 0)
2732 (let ((count 1))
2733 (ps-generate-header-line "/h0" (car contents))
2734 (while (and (< count ps-header-lines)
2735 (setq contents (cdr contents)))
2736 (ps-generate-header-line "/h1" (car contents))
2737 (setq count (+ count 1)))
2738 (ps-output "] def\n"))))
2739
2740(defun ps-output-boolean (name bool)
2741 (ps-output (format "/%s %s def\n" name (if bool "true" "false"))))
ef2cbb24 2742
06fb6aab 2743
87a16a06
RS
2744(defun ps-background-pages (page-list func)
2745 (if page-list
2746 (mapcar
2747 '(lambda (pages)
2748 (let ((start (if (consp pages) (car pages) pages))
2749 (end (if (consp pages) (cdr pages) pages)))
2750 (and (integerp start) (integerp end) (<= start end)
2751 (add-to-list 'ps-background-pages (vector start end func)))))
2752 page-list)
2753 (setq ps-background-all-pages (cons func ps-background-all-pages))))
2754
2755
2756(defun ps-get-boundingbox ()
2757 (save-excursion
2758 (set-buffer ps-spool-buffer)
2759 (save-excursion
2760 (if (re-search-forward
2761 "^%%BoundingBox:\\s-+\\([0-9.]+\\)\\s-+\\([0-9.]+\\)\\s-+\\([0-9.]+\\)\\s-+\\([0-9.]+\\)"
2762 nil t)
2763 (vector (string-to-number ; lower x
2764 (buffer-substring (match-beginning 1) (match-end 1)))
2765 (string-to-number ; lower y
2766 (buffer-substring (match-beginning 2) (match-end 2)))
2767 (string-to-number ; upper x
2768 (buffer-substring (match-beginning 3) (match-end 3)))
2769 (string-to-number ; upper y
2770 (buffer-substring (match-beginning 4) (match-end 4))))
2771 (vector 0 0 0 0)))))
2772
2773
2774;; Emacs understands the %f format; we'll use it to limit color RGB values
2775;; to three decimals to cut down some on the size of the PostScript output.
2776;; Lucid emacsen will have to make do with %s (princ) for floats.
2777
2778(defvar ps-float-format (if (eq ps-print-emacs-type 'emacs)
2779 "%0.3f " ; emacs
2780 "%s ")) ; Lucid emacsen
2781
2782
2783(defun ps-float-format (value &optional default)
2784 (let ((literal (or value default)))
2785 (if literal
2786 (format (if (numberp literal)
2787 ps-float-format
2788 "%s ")
2789 literal)
2790 " ")))
2791
2792
2793(defun ps-background-text ()
2794 (mapcar
2795 '(lambda (text)
2796 (setq ps-background-text-count (1+ ps-background-text-count))
2797 (ps-output (format "/ShowBackText-%d {\n" ps-background-text-count))
2798 (ps-output-string (nth 0 text)) ; text
2799 (ps-output
2800 "\n"
2801 (ps-float-format (nth 4 text) 200.0) ; font size
2802 (format "/%s " (or (nth 3 text) "Times-Roman")) ; font name
2803 (ps-float-format (nth 6 text)
2804 "PrintHeight PrintPageWidth atan") ; rotation
2805 (ps-float-format (nth 5 text) 0.85) ; gray
2806 (ps-float-format (nth 1 text) "0") ; x position
2807 (ps-float-format (nth 2 text) "BottomMargin") ; y position
2808 "\nShowBackText} def\n")
2809 (ps-background-pages (nthcdr 7 text) ; page list
2810 (format "ShowBackText-%d\n"
2811 ps-background-text-count)))
2812 ps-print-background-text))
2813
2814
2815(defun ps-background-image ()
2816 (mapcar
2817 '(lambda (image)
2818 (let ((image-file (expand-file-name (nth 0 image))))
2819 (if (file-readable-p image-file)
2820 (progn
2821 (setq ps-background-image-count (1+ ps-background-image-count))
2822 (ps-output
2823 (format "/ShowBackImage-%d {\n--back-- " ps-background-image-count)
2824 (ps-float-format (nth 5 image) 0.0) ; rotation
2825 (ps-float-format (nth 3 image) 1.0) ; x scale
2826 (ps-float-format (nth 4 image) 1.0) ; y scale
2827 (ps-float-format (nth 1 image) ; x position
2828 "PrintPageWidth 2 div")
2829 (ps-float-format (nth 2 image) ; y position
2830 "PrintHeight 2 div BottomMargin add")
2831 "\nBeginBackImage\n")
2832 (ps-insert-file image-file)
2833 ;; coordinate adjustment to centralize image
2834 ;; around x and y position
2835 (let ((box (ps-get-boundingbox)))
2836 (save-excursion
2837 (set-buffer ps-spool-buffer)
2838 (save-excursion
2839 (if (re-search-backward "^--back--" nil t)
2840 (replace-match
2841 (format "%s %s"
2842 (ps-float-format
2843 (- (+ (/ (- (aref box 2) (aref box 0)) 2.0)
2844 (aref box 0))))
2845 (ps-float-format
2846 (- (+ (/ (- (aref box 3) (aref box 1)) 2.0)
2847 (aref box 1)))))
2848 t)))))
2849 (ps-output "\nEndBackImage} def\n")
2850 (ps-background-pages (nthcdr 6 image) ; page list
2851 (format "ShowBackImage-%d\n"
2852 ps-background-image-count))))))
2853 ps-print-background-image))
2854
2855
a18ed129 2856(defun ps-background (page-number)
87a16a06
RS
2857 (let (has-local-background)
2858 (mapcar '(lambda (range)
a18ed129
RS
2859 (and (<= (aref range 0) page-number)
2860 (<= page-number (aref range 1))
87a16a06
RS
2861 (if has-local-background
2862 (ps-output (aref range 2))
2863 (setq has-local-background t)
2864 (ps-output "/printLocalBackground {\n"
2865 (aref range 2)))))
2866 ps-background-pages)
2867 (and has-local-background (ps-output "} def\n"))))
2868
2869
ef2cbb24 2870(defun ps-begin-file ()
bcc0d457 2871 (ps-get-page-dimensions)
a18ed129 2872 (setq ps-showline-count (if ps-printing-region (car ps-printing-region) 1)
87a16a06
RS
2873 ps-background-text-count 0
2874 ps-background-image-count 0
2875 ps-background-pages nil
2876 ps-background-all-pages nil)
12d89a2e
RS
2877
2878 (ps-output ps-adobe-tag)
a18ed129 2879 (ps-output "%%Title: " (buffer-name)) ;Take job name from name of
12d89a2e 2880 ;first buffer printed
87a16a06
RS
2881 (ps-output "\n%%Creator: " (user-full-name))
2882 (ps-output "\n%%CreationDate: "
2883 (time-stamp-hh:mm:ss) " " (time-stamp-mon-dd-yyyy)
2884 "\n%%Orientation: "
2885 (if ps-landscape-mode "Landscape" "Portrait"))
2886 (ps-output "\n%% DocumentFonts: Times-Roman Times-Italic "
12d89a2e 2887 ps-font " " ps-font-bold " " ps-font-italic " "
bcc0d457 2888 ps-font-bold-italic " "
87a16a06
RS
2889 ps-header-font " " ps-header-title-font)
2890 (ps-output "\n%%Pages: (atend)\n")
12d89a2e
RS
2891 (ps-output "%%EndComments\n\n")
2892
bcc0d457
RS
2893 (ps-output-boolean "LandscapeMode" ps-landscape-mode)
2894 (ps-output (format "/NumberOfColumns %d def\n" ps-number-of-columns))
12d89a2e 2895
bcc0d457 2896 (ps-output (format "/LandscapePageHeight %s def\n" ps-landscape-page-height))
87a16a06
RS
2897 (ps-output (format "/PrintPageWidth %s def\n"
2898 (- (* (+ ps-print-width ps-inter-column)
2899 ps-number-of-columns)
2900 ps-inter-column)))
bcc0d457
RS
2901 (ps-output (format "/PrintWidth %s def\n" ps-print-width))
2902 (ps-output (format "/PrintHeight %s def\n" ps-print-height))
12d89a2e 2903
bcc0d457
RS
2904 (ps-output (format "/LeftMargin %s def\n" ps-left-margin))
2905 (ps-output (format "/RightMargin %s def\n" ps-right-margin)) ; not used
2906 (ps-output (format "/InterColumn %s def\n" ps-inter-column))
2907
2908 (ps-output (format "/BottomMargin %s def\n" ps-bottom-margin))
2909 (ps-output (format "/TopMargin %s def\n" ps-top-margin)) ; not used
2910 (ps-output (format "/HeaderOffset %s def\n" ps-header-offset))
2911 (ps-output (format "/HeaderPad %s def\n" ps-header-pad))
06fb6aab 2912
bcc0d457
RS
2913 (ps-output-boolean "PrintHeader" ps-print-header)
2914 (ps-output-boolean "PrintHeaderFrame" ps-print-header-frame)
2915 (ps-output-boolean "ShowNofN" ps-show-n-of-n)
2916 (ps-output-boolean "Duplex" ps-spool-duplex)
2917
87a16a06
RS
2918 (ps-output (format "/LineHeight %s def\n" ps-line-height)
2919 (format "/LinesPerColumn %d def\n"
a18ed129 2920 (round (/ (+ ps-print-height
87a16a06
RS
2921 (* ps-line-height 0.45))
2922 ps-line-height))))
2923
535efc38
RS
2924 (ps-output-boolean "Zebra" ps-zebra-stripes)
2925 (ps-output (format "/NumberOfZebra %d def\n" ps-zebra-stripe-height))
87a16a06
RS
2926
2927 (ps-output-boolean "PrintLineNumber" ps-line-number)
a18ed129
RS
2928 (ps-output (format "/Lines %d def\n"
2929 (if ps-printing-region
2930 (cdr ps-printing-region)
2931 (ps-count-lines (point-min) (point-max)))))
87a16a06
RS
2932
2933 (ps-background-text)
2934 (ps-background-image)
2935 (setq ps-background-all-pages (nreverse ps-background-all-pages)
2936 ps-background-pages (nreverse ps-background-pages))
12d89a2e 2937
bcc0d457 2938 (ps-output ps-print-prologue-1)
12d89a2e 2939
87a16a06
RS
2940 (ps-output "/printGlobalBackground {\n")
2941 (ps-output-list ps-background-all-pages)
2942 (ps-output "} def\n/printLocalBackground {\n} def\n")
2943
bcc0d457
RS
2944 ;; Header fonts
2945 (ps-output ; /h0 14 /Helvetica-Bold Font
2946 (format "/h0 %s /%s DefFont\n" ps-header-title-font-size ps-header-title-font))
2947 (ps-output ; /h1 12 /Helvetica Font
2948 (format "/h1 %s /%s DefFont\n" ps-header-font-size ps-header-font))
2949
2950 (ps-output ps-print-prologue-2)
2951
2952 ;; Text fonts
2953 (ps-output (format "/f0 %s /%s DefFont\n" ps-font-size ps-font))
2954 (ps-output (format "/f1 %s /%s DefFont\n" ps-font-size ps-font-bold))
2955 (ps-output (format "/f2 %s /%s DefFont\n" ps-font-size ps-font-italic))
2956 (ps-output (format "/f3 %s /%s DefFont\n" ps-font-size ps-font-bold-italic))
2957
2958 (ps-output "\nBeginDoc\n\n")
12d89a2e 2959 (ps-output "%%EndPrologue\n"))
ef2cbb24 2960
12d89a2e
RS
2961(defun ps-header-dirpart ()
2962 (let ((fname (buffer-file-name)))
2963 (if fname
2964 (if (string-equal (buffer-name) (file-name-nondirectory fname))
2965 (file-name-directory fname)
2966 fname)
2967 "")))
ef2cbb24 2968
12d89a2e 2969(defun ps-get-buffer-name ()
bcc0d457
RS
2970 (cond
2971 ;; Indulge Jim this little easter egg:
2972 ((string= (buffer-name) "ps-print.el")
2973 "Hey, Cool! It's ps-print.el!!!")
2974 ;; Indulge Jack this other little easter egg:
2975 ((string= (buffer-name) "sokoban.el")
2976 "Super! C'est sokoban.el!")
87a16a06 2977 (t (concat
a18ed129 2978 (and ps-printing-region "Subset of: ")
87a16a06
RS
2979 (buffer-name)
2980 (and (buffer-modified-p) " (unsaved)")))))
ef2cbb24 2981
12d89a2e
RS
2982(defun ps-begin-job ()
2983 (setq ps-page-count 0))
ef2cbb24
RS
2984
2985(defun ps-end-file ()
87a16a06 2986 (ps-output "\n%%Trailer\n")
090be653 2987 (ps-output (format "%%%%Pages: %d\n" (1+ (/ (1- ps-page-count)
87a16a06
RS
2988 ps-number-of-columns))))
2989 (ps-output "\nEndDoc\n\n%%EOF\n"))
2990
2991
ef2cbb24
RS
2992(defun ps-next-page ()
2993 (ps-end-page)
12d89a2e
RS
2994 (ps-flush-output)
2995 (ps-begin-page))
2996
a18ed129
RS
2997(defun ps-header-page (&optional inc-p)
2998 (if (zerop (mod ps-page-count ps-number-of-columns))
2999 ;; Print only when a new real page begins.
3000 (let ((page-number (1+ (/ ps-page-count ps-number-of-columns))))
3001 (ps-output (format "\n%%%%Page: %d %d\n" page-number page-number))
3002 (ps-output "BeginDSCPage\n")
3003 (ps-background page-number)
3004 (and inc-p (incf ps-page-count)))
3005 ;; Print when any other page begins.
3006 (ps-output "BeginDSCPage\n")))
3007
12d89a2e
RS
3008(defun ps-begin-page (&optional dummypage)
3009 (ps-get-page-dimensions)
bcc0d457 3010 (setq ps-width-remaining ps-print-width)
12d89a2e
RS
3011 (setq ps-height-remaining ps-print-height)
3012
a18ed129 3013 (ps-header-page)
12d89a2e 3014
87a16a06
RS
3015 (ps-output (format "/LineNumber %d def\n" ps-showline-count)
3016 (format "/PageNumber %d def\n" (incf ps-page-count)))
12d89a2e
RS
3017 (ps-output "/PageCount 0 def\n")
3018
090be653
RS
3019 (when ps-print-header
3020 (ps-generate-header "HeaderLinesLeft" ps-left-header)
3021 (ps-generate-header "HeaderLinesRight" ps-right-header)
3022 (ps-output (format "%d SetHeaderLines\n" ps-header-lines)))
12d89a2e
RS
3023
3024 (ps-output "BeginPage\n")
87a16a06
RS
3025 (ps-set-font ps-current-font)
3026 (ps-set-bg ps-current-bg)
3027 (ps-set-color ps-current-color))
ef2cbb24
RS
3028
3029(defun ps-end-page ()
a18ed129 3030 (ps-output "EndPage\nEndDSCPage\n"))
12d89a2e
RS
3031
3032(defun ps-dummy-page ()
a18ed129
RS
3033 (ps-header-page t)
3034 (ps-output "/PrintHeader false def
12d89a2e
RS
3035BeginPage
3036EndPage
3037EndDSCPage\n"))
06fb6aab 3038
ef2cbb24 3039(defun ps-next-line ()
87a16a06 3040 (setq ps-showline-count (1+ ps-showline-count))
12d89a2e 3041 (if (< ps-height-remaining ps-line-height)
ef2cbb24 3042 (ps-next-page)
bcc0d457 3043 (setq ps-width-remaining ps-print-width)
12d89a2e
RS
3044 (setq ps-height-remaining (- ps-height-remaining ps-line-height))
3045 (ps-hard-lf)))
ef2cbb24
RS
3046
3047(defun ps-continue-line ()
12d89a2e
RS
3048 (if (< ps-height-remaining ps-line-height)
3049 (ps-next-page)
bcc0d457 3050 (setq ps-width-remaining ps-print-width)
12d89a2e
RS
3051 (setq ps-height-remaining (- ps-height-remaining ps-line-height))
3052 (ps-soft-lf)))
3053
bcc0d457
RS
3054;; [jack] Why hard and soft ?
3055
12d89a2e
RS
3056(defun ps-hard-lf ()
3057 (ps-output "HL\n"))
3058
3059(defun ps-soft-lf ()
3060 (ps-output "SL\n"))
3061
3062(defun ps-find-wrappoint (from to char-width)
3063 (let ((avail (truncate (/ ps-width-remaining char-width)))
3064 (todo (- to from)))
3065 (if (< todo avail)
3066 (cons to (* todo char-width))
3067 (cons (+ from avail) ps-width-remaining))))
3068
3069(defun ps-basic-plot-string (from to &optional bg-color)
3070 (let* ((wrappoint (ps-find-wrappoint from to ps-avg-char-width))
3071 (to (car wrappoint))
055e7bf2 3072 (string (buffer-substring-no-properties from to)))
12d89a2e 3073 (ps-output-string string)
bcc0d457 3074 (ps-output " S\n")
12d89a2e
RS
3075 wrappoint))
3076
3077(defun ps-basic-plot-whitespace (from to &optional bg-color)
3078 (let* ((wrappoint (ps-find-wrappoint from to ps-space-width))
3079 (to (car wrappoint)))
12d89a2e
RS
3080 (ps-output (format "%d W\n" (- to from)))
3081 wrappoint))
3082
3083(defun ps-plot (plotfunc from to &optional bg-color)
ef2cbb24 3084 (while (< from to)
12d89a2e
RS
3085 (let* ((wrappoint (funcall plotfunc from to bg-color))
3086 (plotted-to (car wrappoint))
3087 (plotted-width (cdr wrappoint)))
3088 (setq from plotted-to)
3089 (setq ps-width-remaining (- ps-width-remaining plotted-width))
3090 (if (< from to)
3091 (ps-continue-line))))
ef2cbb24
RS
3092 (if ps-razzle-dazzle
3093 (let* ((q-todo (- (point-max) (point-min)))
12d89a2e 3094 (q-done (- (point) (point-min)))
ef2cbb24 3095 (chunkfrac (/ q-todo 8))
12d89a2e 3096 (chunksize (if (> chunkfrac 1000) 1000 chunkfrac)))
ef2cbb24 3097 (if (> (- q-done ps-razchunk) chunksize)
00aa16af 3098 (let (foo)
ef2cbb24
RS
3099 (setq ps-razchunk q-done)
3100 (setq foo
3101 (if (< q-todo 100)
12d89a2e
RS
3102 (/ (* 100 q-done) q-todo)
3103 (/ q-done (/ q-todo 100))))
090be653 3104 (message "Formatting...%3d%%" foo))))))
12d89a2e
RS
3105
3106(defun ps-set-font (font)
3107 (setq ps-current-font font)
3108 (ps-output (format "/f%d F\n" ps-current-font)))
3109
12d89a2e
RS
3110(defun ps-set-bg (color)
3111 (if (setq ps-current-bg color)
3112 (ps-output (format ps-color-format (nth 0 color) (nth 1 color)
3113 (nth 2 color))
3114 " true BG\n")
3115 (ps-output "false BG\n")))
3116
3117(defun ps-set-color (color)
a18ed129 3118 (setq ps-current-color (or color ps-default-fg))
043620f4
KH
3119 (ps-output (format ps-color-format (nth 0 ps-current-color)
3120 (nth 1 ps-current-color) (nth 2 ps-current-color))
3121 " FG\n"))
12d89a2e 3122
12d89a2e 3123
87a16a06 3124(defvar ps-current-effect 0)
12d89a2e 3125
87a16a06
RS
3126
3127(defun ps-plot-region (from to font &optional fg-color bg-color effects)
12d89a2e
RS
3128 (if (not (equal font ps-current-font))
3129 (ps-set-font font))
06fb6aab 3130
12d89a2e
RS
3131 ;; Specify a foreground color only if one's specified and it's
3132 ;; different than the current.
3133 (if (not (equal fg-color ps-current-color))
3134 (ps-set-color fg-color))
06fb6aab 3135
12d89a2e
RS
3136 (if (not (equal bg-color ps-current-bg))
3137 (ps-set-bg bg-color))
06fb6aab 3138
87a16a06
RS
3139 ;; Specify effects (underline, overline, box, etc)
3140 (cond
3141 ((not (integerp effects))
3142 (ps-output "0 EF\n")
3143 (setq ps-current-effect 0))
3144 ((/= effects ps-current-effect)
3145 (ps-output (number-to-string effects) " EF\n")
3146 (setq ps-current-effect effects)))
ef2cbb24 3147
12d89a2e 3148 ;; Starting at the beginning of the specified region...
ef2cbb24
RS
3149 (save-excursion
3150 (goto-char from)
12d89a2e
RS
3151
3152 ;; ...break the region up into chunks separated by tabs, linefeeds,
87a16a06 3153 ;; pagefeeds, control characters, and plot each chunk.
ef2cbb24 3154 (while (< from to)
87a16a06 3155 (if (re-search-forward "[\000-\037\177-\377]" to t)
a18ed129 3156 ;; region with some control characters
87a16a06
RS
3157 (let ((match (char-after (match-beginning 0))))
3158 (if (= match ?\t) ; tab
3159 (let ((linestart
3160 (save-excursion (beginning-of-line) (point))))
3161 (ps-plot 'ps-basic-plot-string from (- (point) 1)
3162 bg-color)
3163 (forward-char -1)
3164 (setq from (+ linestart (current-column)))
3165 (if (re-search-forward "[ \t]+" to t)
3166 (ps-plot 'ps-basic-plot-whitespace
3167 from (+ linestart (current-column))
3168 bg-color)))
3169 ;; any other control character except tab
3170 (ps-plot 'ps-basic-plot-string from (- (point) 1) bg-color)
3171 (cond
3172 ((= match ?\n) ; newline
3173 (ps-next-line))
3174
3175 ((= match ?\f) ; form feed
3176 (ps-next-page))
3177
3178 ((<= match ?\037) ; characters from ^@ to ^_
3179 (ps-control-character (format "^%c" (+ match ?@))))
3180
3181 ((= match ?\177) ; del (127) is printed ^?
3182 (ps-control-character "^?"))
3183
3184 (t ; characters from 128 to 255
3185 (ps-control-character (format "\\%o" match)))))
3186 (setq from (point)))
3187 ;; region without control characters
3188 (ps-plot 'ps-basic-plot-string from to bg-color)
3189 (setq from to)))))
3190
3191(defun ps-control-character (str)
3192 (let* ((from (1- (point)))
3193 (len (length str))
3194 (to (+ from len))
3195 (wrappoint (ps-find-wrappoint from to ps-avg-char-width)))
3196 (if (< (car wrappoint) to)
3197 (ps-continue-line))
3198 (setq ps-width-remaining (- ps-width-remaining (* len ps-avg-char-width)))
3199 (ps-output-string str)
3200 (ps-output " S\n")))
ef2cbb24 3201
12d89a2e
RS
3202(defun ps-color-value (x-color-value)
3203 ;; Scale 16-bit X-COLOR-VALUE to PostScript color value in [0, 1] interval.
3204 (/ x-color-value ps-print-color-scale))
ef2cbb24 3205
043620f4
KH
3206(defun ps-color-values (x-color)
3207 (cond ((fboundp 'x-color-values)
3208 (x-color-values x-color))
3209 ((fboundp 'pixel-components)
3210 (pixel-components x-color))
3211 (t (error "No available function to determine X color values."))))
3212
87a16a06 3213
a18ed129
RS
3214(defun ps-face-attributes (face)
3215 "Return face attribute vector.
87a16a06 3216
a18ed129
RS
3217If FACE is not in `ps-print-face-extension-alist' or in
3218`ps-print-face-alist', insert it on `ps-print-face-alist' and
3219return the attribute vector.
87a16a06
RS
3220
3221If FACE is not a valid face name, it is used default face."
a18ed129
RS
3222 (cdr (or (assq face ps-print-face-extension-alist)
3223 (assq face ps-print-face-alist)
3224 (let* ((the-face (if (facep face) face 'default))
3225 (new-face (ps-screen-to-bit-face the-face)))
3226 (or (and (eq the-face 'default)
3227 (assq the-face ps-print-face-alist))
3228 (setq ps-print-face-alist (cons new-face ps-print-face-alist)))
3229 new-face))))
87a16a06 3230
043620f4
KH
3231
3232(defun ps-face-attribute-list (face-or-list)
3233 (if (listp face-or-list)
87a16a06
RS
3234 ;; list of faces
3235 (let ((effects 0) foreground background face-attr face)
043620f4 3236 (while face-or-list
87a16a06
RS
3237 (setq face (car face-or-list)
3238 face-attr (ps-face-attributes face)
3239 effects (logior effects (aref face-attr 0)))
3240 (or foreground (setq foreground (aref face-attr 1)))
3241 (or background (setq background (aref face-attr 2)))
043620f4 3242 (setq face-or-list (cdr face-or-list)))
87a16a06
RS
3243 (vector effects foreground background))
3244 ;; simple face
043620f4
KH
3245 (ps-face-attributes face-or-list)))
3246
87a16a06 3247
12d89a2e
RS
3248(defun ps-plot-with-face (from to face)
3249 (if face
87a16a06
RS
3250 (let* ((face-bit (ps-face-attribute-list face))
3251 (effect (aref face-bit 0))
3252 (foreground (aref face-bit 1))
3253 (background (aref face-bit 2))
12d89a2e
RS
3254 (fg-color (if (and ps-print-color-p foreground)
3255 (mapcar 'ps-color-value
043620f4 3256 (ps-color-values foreground))
12d89a2e
RS
3257 ps-default-color))
3258 (bg-color (if (and ps-print-color-p background)
3259 (mapcar 'ps-color-value
043620f4 3260 (ps-color-values background)))))
87a16a06
RS
3261 (ps-plot-region from to (logand effect 3)
3262 fg-color bg-color (lsh effect -2)))
3263 (ps-plot-region from to 0))
3264 (goto-char to))
12d89a2e
RS
3265
3266
12d89a2e
RS
3267(defun ps-xemacs-face-kind-p (face kind kind-regex kind-list)
3268 (let* ((frame-font (or (face-font face) (face-font 'default)))
3269 (kind-cons (assq kind (x-font-properties frame-font)))
3270 (kind-spec (cdr-safe kind-cons))
3271 (case-fold-search t))
3272
3273 (or (and kind-spec (string-match kind-regex kind-spec))
3274 ;; Kludge-compatible:
3275 (memq face kind-list))))
3276
3277(defun ps-face-bold-p (face)
6770a60f 3278 (if (eq ps-print-emacs-type 'emacs)
06fb6aab
RS
3279 (or (face-bold-p face)
3280 (memq face ps-bold-faces))
12d89a2e
RS
3281 (ps-xemacs-face-kind-p face 'WEIGHT_NAME "bold\\|demibold"
3282 ps-bold-faces)))
3283
3284(defun ps-face-italic-p (face)
6770a60f 3285 (if (eq ps-print-emacs-type 'emacs)
06fb6aab
RS
3286 (or (face-italic-p face)
3287 (memq face ps-italic-faces))
00aa16af
RS
3288 (or
3289 (ps-xemacs-face-kind-p face 'ANGLE_NAME "i\\|o" ps-italic-faces)
3290 (ps-xemacs-face-kind-p face 'SLANT "i\\|o" ps-italic-faces))))
12d89a2e
RS
3291
3292(defun ps-face-underlined-p (face)
3293 (or (face-underline-p face)
3294 (memq face ps-underlined-faces)))
3295
a18ed129 3296
043620f4
KH
3297;; Ensure that face-list is fbound.
3298(or (fboundp 'face-list) (defalias 'face-list 'list-faces))
12d89a2e 3299
a18ed129 3300
12d89a2e 3301(defun ps-build-reference-face-lists ()
a18ed129 3302 (setq ps-print-face-alist nil)
12d89a2e 3303 (if ps-auto-font-detect
a18ed129
RS
3304 (mapcar 'ps-map-face (face-list))
3305 (mapcar 'ps-set-face-bold ps-bold-faces)
3306 (mapcar 'ps-set-face-italic ps-italic-faces)
3307 (mapcar 'ps-set-face-underline ps-underlined-faces))
12d89a2e 3308 (setq ps-build-face-reference nil))
ef2cbb24 3309
a18ed129
RS
3310
3311(defun ps-set-face-bold (face)
3312 (ps-set-face-attribute face 1))
3313
3314(defun ps-set-face-italic (face)
3315 (ps-set-face-attribute face 2))
3316
3317(defun ps-set-face-underline (face)
3318 (ps-set-face-attribute face 4))
3319
3320
3321(defun ps-set-face-attribute (face effect)
3322 (let ((face-bit (cdr (ps-map-face face))))
3323 (aset face-bit 0 (logior (aref face-bit 0) effect))))
3324
3325
3326(defun ps-map-face (face)
3327 (let* ((face-map (ps-screen-to-bit-face face))
3328 (ps-face-bit (cdr (assq (car face-map) ps-print-face-alist))))
3329 (if ps-face-bit
3330 ;; if face exists, merge both
3331 (let ((face-bit (cdr face-map)))
3332 (aset ps-face-bit 0 (logior (aref ps-face-bit 0) (aref face-bit 0)))
3333 (or (aref ps-face-bit 1) (aset ps-face-bit 1 (aref face-bit 1)))
3334 (or (aref ps-face-bit 2) (aset ps-face-bit 2 (aref face-bit 2))))
3335 ;; if face does not exist, insert it
3336 (setq ps-print-face-alist (cons face-map ps-print-face-alist)))
3337 face-map))
3338
3339
3340(defun ps-screen-to-bit-face (face)
3341 (cons face
3342 (vector (logior (if (ps-face-bold-p face) 1 0) ; bold
3343 (if (ps-face-italic-p face) 2 0) ; italic
3344 (if (ps-face-underlined-p face) 4 0)) ; underline
3345 (face-foreground face)
3346 (face-background face))))
3347
3348
ef2cbb24
RS
3349(defun ps-mapper (extent list)
3350 (nconc list (list (list (extent-start-position extent) 'push extent)
06fb6aab 3351 (list (extent-end-position extent) 'pull extent)))
ef2cbb24
RS
3352 nil)
3353
00aa16af
RS
3354(defun ps-extent-sorter (a b)
3355 (< (extent-priority a) (extent-priority b)))
043620f4
KH
3356
3357(defun ps-print-ensure-fontified (start end)
3358 (if (and (boundp 'lazy-lock-mode) lazy-lock-mode)
3359 (if (fboundp 'lazy-lock-fontify-region)
06fb6aab
RS
3360 (lazy-lock-fontify-region start end) ; the new
3361 (lazy-lock-fontify-buffer)))) ; the old
043620f4 3362
ef2cbb24 3363(defun ps-generate-postscript-with-faces (from to)
87a16a06
RS
3364 ;; Some initialization...
3365 (setq ps-current-effect 0)
3366
00aa16af 3367 ;; Build the reference lists of faces if necessary.
12d89a2e
RS
3368 (if (or ps-always-build-face-reference
3369 ps-build-face-reference)
3370 (progn
3371 (message "Collecting face information...")
3372 (ps-build-reference-face-lists)))
00aa16af
RS
3373 ;; Set the color scale. We do it here instead of in the defvar so
3374 ;; that ps-print can be dumped into emacs. This expression can't be
3375 ;; evaluated at dump-time because X isn't initialized.
3376 (setq ps-print-color-scale
3377 (if ps-print-color-p
043620f4 3378 (float (car (ps-color-values "white")))
00aa16af
RS
3379 1.0))
3380 ;; Generate some PostScript.
ef2cbb24
RS
3381 (save-restriction
3382 (narrow-to-region from to)
12d89a2e
RS
3383 (let ((face 'default)
3384 (position to))
043620f4 3385 (ps-print-ensure-fontified from to)
87a16a06
RS
3386 (cond
3387 ((or (eq ps-print-emacs-type 'lucid)
3388 (eq ps-print-emacs-type 'xemacs))
3389 ;; Build the list of extents...
3390 (let ((a (cons 'dummy nil))
3391 record type extent extent-list)
3392 (map-extents 'ps-mapper nil from to a)
3393 (setq a (sort (cdr a) 'car-less-than-car))
3394
3395 (setq extent-list nil)
3396
3397 ;; Loop through the extents...
3398 (while a
3399 (setq record (car a))
3400
3401 (setq position (car record))
3402 (setq record (cdr record))
3403
3404 (setq type (car record))
3405 (setq record (cdr record))
3406
3407 (setq extent (car record))
3408
3409 ;; Plot up to this record.
3410 ;; XEmacs 19.12: for some reason, we're getting into a
3411 ;; situation in which some of the records have
3412 ;; positions less than 'from'. Since we've narrowed
3413 ;; the buffer, this'll generate errors. This is a
3414 ;; hack, but don't call ps-plot-with-face unless from >
3415 ;; point-min.
3416 (if (and (>= from (point-min))
3417 (<= position (point-max)))
3418 (ps-plot-with-face from position face))
3419
3420 (cond
3421 ((eq type 'push)
3422 (if (extent-face extent)
3423 (setq extent-list (sort (cons extent extent-list)
3424 'ps-extent-sorter))))
3425
3426 ((eq type 'pull)
3427 (setq extent-list (sort (delq extent extent-list)
3428 'ps-extent-sorter))))
3429
3430 (setq face
3431 (if extent-list
3432 (extent-face (car extent-list))
3433 'default))
3434
3435 (setq from position)
3436 (setq a (cdr a)))))
3437
3438 ((eq ps-print-emacs-type 'emacs)
3439 (let ((property-change from)
3440 (overlay-change from))
3441 (while (< from to)
3442 (if (< property-change to) ; Don't search for property change
12d89a2e 3443 ; unless previous search succeeded.
87a16a06
RS
3444 (setq property-change
3445 (next-property-change from nil to)))
3446 (if (< overlay-change to) ; Don't search for overlay change
12d89a2e 3447 ; unless previous search succeeded.
87a16a06
RS
3448 (setq overlay-change
3449 (min (next-overlay-change from) to)))
3450 (setq position
3451 (min property-change overlay-change))
3452 ;; The code below is not quite correct,
3453 ;; because a non-nil overlay invisible property
3454 ;; which is inactive according to the current value
3455 ;; of buffer-invisibility-spec nonetheless overrides
3456 ;; a face text property.
3457 (setq face
3458 (cond ((let ((prop (get-text-property from 'invisible)))
3459 ;; Decide whether this invisible property
3460 ;; really makes the text invisible.
3461 (if (eq buffer-invisibility-spec t)
3462 (not (null prop))
3463 (or (memq prop buffer-invisibility-spec)
3464 (assq prop buffer-invisibility-spec))))
3465 nil)
3466 ((get-text-property from 'face))
3467 (t 'default)))
3468 (let ((overlays (overlays-at from))
3469 (face-priority -1)) ; text-property
3470 (while overlays
3471 (let* ((overlay (car overlays))
3472 (overlay-face (overlay-get overlay 'face))
3473 (overlay-invisible (overlay-get overlay 'invisible))
3474 (overlay-priority (or (overlay-get overlay
3475 'priority)
3476 0)))
3477 (if (and (or overlay-invisible overlay-face)
3478 (> overlay-priority face-priority))
3479 (setq face (cond ((if (eq buffer-invisibility-spec t)
3480 (not (null overlay-invisible))
3481 (or (memq overlay-invisible
3482 buffer-invisibility-spec)
3483 (assq overlay-invisible
3484 buffer-invisibility-spec)))
3485 nil)
3486 ((and face overlay-face)))
3487 face-priority overlay-priority)))
3488 (setq overlays (cdr overlays))))
3489 ;; Plot up to this record.
3490 (ps-plot-with-face from position face)
3491 (setq from position)))))
3492 (ps-plot-with-face from to face))))
ef2cbb24
RS
3493
3494(defun ps-generate-postscript (from to)
12d89a2e 3495 (ps-plot-region from to 0 nil))
ef2cbb24
RS
3496
3497(defun ps-generate (buffer from to genfunc)
87a16a06
RS
3498 (save-excursion
3499 (let ((from (min to from))
3500 (to (max to from))
3501 ;; This avoids trouble if chars with read-only properties
3502 ;; are copied into ps-spool-buffer.
3503 (inhibit-read-only t))
3504 (save-restriction
3505 (narrow-to-region from to)
3506 (if ps-razzle-dazzle
3507 (message "Formatting...%3d%%" (setq ps-razchunk 0)))
3508 (set-buffer buffer)
3509 (setq ps-source-buffer buffer)
3510 (setq ps-spool-buffer (get-buffer-create ps-spool-buffer-name))
3511 (ps-init-output-queue)
3512 (let (safe-marker completed-safely needs-begin-file)
3513 (unwind-protect
00aa16af
RS
3514 (progn
3515 (set-buffer ps-spool-buffer)
ef2cbb24 3516
87a16a06
RS
3517 ;; Get a marker and make it point to the current end of the
3518 ;; buffer, If an error occurs, we'll delete everything from
3519 ;; the end of this marker onwards.
3520 (setq safe-marker (make-marker))
3521 (set-marker safe-marker (point-max))
3522
3523 (goto-char (point-min))
3524 (if (looking-at (regexp-quote ps-adobe-tag))
3525 nil
3526 (setq needs-begin-file t))
3527 (save-excursion
3528 (set-buffer ps-source-buffer)
3529 (if needs-begin-file (ps-begin-file))
3530 (ps-begin-job)
3531 (ps-begin-page))
3532 (set-buffer ps-source-buffer)
3533 (funcall genfunc from to)
3534 (ps-end-page)
3535
3536 (if (and ps-spool-duplex
3537 (= (mod ps-page-count 2) 1))
3538 (ps-dummy-page))
3539 (ps-flush-output)
3540
3541 ;; Back to the PS output buffer to set the page count
3542 (set-buffer ps-spool-buffer)
3543 (goto-char (point-max))
3544 (while (re-search-backward "^/PageCount 0 def$" nil t)
3545 (replace-match (format "/PageCount %d def" ps-page-count) t))
3546
3547 ;; Setting this variable tells the unwind form that the
3548 ;; the postscript was generated without error.
3549 (setq completed-safely t))
3550
3551 ;; Unwind form: If some bad mojo occurred while generating
3552 ;; postscript, delete all the postscript that was generated.
3553 ;; This protects the previously spooled files from getting
3554 ;; corrupted.
3555 (if (and (markerp safe-marker) (not completed-safely))
3556 (progn
3557 (set-buffer ps-spool-buffer)
3558 (delete-region (marker-position safe-marker) (point-max))))))
3559
3560 (if ps-razzle-dazzle
3561 (message "Formatting...done"))))))
ef2cbb24
RS
3562
3563(defun ps-do-despool (filename)
12d89a2e 3564 (if (or (not (boundp 'ps-spool-buffer))
bcc0d457 3565 (not (symbol-value 'ps-spool-buffer)))
12d89a2e 3566 (message "No spooled PostScript to print")
ef2cbb24 3567 (ps-end-file)
12d89a2e 3568 (ps-flush-output)
ef2cbb24
RS
3569 (if filename
3570 (save-excursion
3571 (if ps-razzle-dazzle
3572 (message "Saving..."))
12d89a2e 3573 (set-buffer ps-spool-buffer)
ef2cbb24
RS
3574 (setq filename (expand-file-name filename))
3575 (write-region (point-min) (point-max) filename)
ef2cbb24
RS
3576 (if ps-razzle-dazzle
3577 (message "Wrote %s" filename)))
ef2cbb24
RS
3578 ;; Else, spool to the printer
3579 (if ps-razzle-dazzle
3580 (message "Printing..."))
ef2cbb24 3581 (save-excursion
12d89a2e 3582 (set-buffer ps-spool-buffer)
87a16a06
RS
3583 (if (and (eq system-type 'ms-dos)
3584 (stringp (symbol-value 'dos-ps-printer)))
3585 (write-region (point-min) (point-max)
3586 (symbol-value 'dos-ps-printer) t 0)
62901aee
RS
3587 (let ((binary-process-input t)) ; for MS-DOS
3588 (apply 'call-process-region
3589 (point-min) (point-max) ps-lpr-command nil
3590 (if (fboundp 'start-process) 0 nil)
3591 nil
3592 ps-lpr-switches))))
ef2cbb24 3593 (if ps-razzle-dazzle
12d89a2e
RS
3594 (message "Printing...done")))
3595 (kill-buffer ps-spool-buffer)))
3596
3597(defun ps-kill-emacs-check ()
3598 (let (ps-buffer)
3599 (if (and (setq ps-buffer (get-buffer ps-spool-buffer-name))
3600 (buffer-modified-p ps-buffer))
3601 (if (y-or-n-p "Unprinted PostScript waiting; print now? ")
3602 (ps-despool)))
3603 (if (and (setq ps-buffer (get-buffer ps-spool-buffer-name))
3604 (buffer-modified-p ps-buffer))
3605 (if (yes-or-no-p "Unprinted PostScript waiting; exit anyway? ")
3606 nil
3607 (error "Unprinted PostScript")))))
3608
3609(if (fboundp 'add-hook)
bcc0d457 3610 (funcall 'add-hook 'kill-emacs-hook 'ps-kill-emacs-check)
12d89a2e
RS
3611 (if kill-emacs-hook
3612 (message "Won't override existing kill-emacs-hook")
3613 (setq kill-emacs-hook 'ps-kill-emacs-check)))
ef2cbb24 3614
12d89a2e 3615;;; Sample Setup Code:
ef2cbb24 3616
12d89a2e 3617;; This stuff is for anybody that's brave enough to look this far,
87a16a06
RS
3618;; and able to figure out how to use it. It isn't really part of
3619;; ps-print, but I'll leave it here in hopes it might be useful:
ef2cbb24 3620
043620f4
KH
3621;; WARNING!!! The following code is *sample* code only. Don't use it
3622;; unless you understand what it does!
3623
87a16a06
RS
3624(defmacro ps-prsc ()
3625 `(if (eq ps-print-emacs-type 'emacs) [f22] 'f22))
3626(defmacro ps-c-prsc ()
3627 `(if (eq ps-print-emacs-type 'emacs) [C-f22] '(control f22)))
3628(defmacro ps-s-prsc ()
3629 `(if (eq ps-print-emacs-type 'emacs) [S-f22] '(shift f22)))
00aa16af 3630
a18ed129
RS
3631;; A hook to bind to `rmail-mode-hook' to locally bind prsc and set the
3632;; `ps-left-headers' specially for mail messages.
3633(defun ps-rmail-mode-hook ()
3634 (local-set-key (ps-prsc) 'ps-rmail-print-message-from-summary)
3635 (setq ps-header-lines 3
3636 ps-left-header
3637 ;; The left headers will display the message's subject, its
3638 ;; author, and the name of the folder it was in.
3639 '(ps-article-subject ps-article-author buffer-name)))
3640
3641;; See `ps-gnus-print-article-from-summary'. This function does the
3642;; same thing for rmail.
3643(defun ps-rmail-print-message-from-summary ()
3644 (interactive)
3645 (ps-print-message-from-summary 'rmail-summary-buffer "RMAIL"))
3646
3647;; Used in `ps-rmail-print-article-from-summary',
3648;; `ps-gnus-print-article-from-summary' and `ps-vm-print-message-from-summary'.
3649(defun ps-print-message-from-summary (summary-buffer summary-default)
3650 (let ((ps-buf (or (and (boundp summary-buffer)
3651 (symbol-value summary-buffer))
3652 summary-default)))
3653 (and (get-buffer ps-buf)
3654 (save-excursion
3655 (set-buffer ps-buf)
3656 (ps-spool-buffer-with-faces)))))
3657
12d89a2e 3658;; Look in an article or mail message for the Subject: line. To be
87a16a06 3659;; placed in `ps-left-headers'.
12d89a2e 3660(defun ps-article-subject ()
ef2cbb24 3661 (save-excursion
12d89a2e 3662 (goto-char (point-min))
45a870d9 3663 (if (re-search-forward "^Subject:[ \t]+\\(.*\\)$" nil t)
055e7bf2 3664 (buffer-substring-no-properties (match-beginning 1) (match-end 1))
12d89a2e
RS
3665 "Subject ???")))
3666
3667;; Look in an article or mail message for the From: line. Sorta-kinda
3668;; understands RFC-822 addresses and can pull the real name out where
87a16a06 3669;; it's provided. To be placed in `ps-left-headers'.
12d89a2e
RS
3670(defun ps-article-author ()
3671 (save-excursion
3672 (goto-char (point-min))
a97592dd 3673 (if (re-search-forward "^From:[ \t]+\\(.*\\)$" nil t)
87a16a06
RS
3674 (let ((fromstring (buffer-substring-no-properties (match-beginning 1)
3675 (match-end 1))))
12d89a2e
RS
3676 (cond
3677
3678 ;; Try first to match addresses that look like
3679 ;; thompson@wg2.waii.com (Jim Thompson)
3680 ((string-match ".*[ \t]+(\\(.*\\))" fromstring)
3681 (substring fromstring (match-beginning 1) (match-end 1)))
3682
3683 ;; Next try to match addresses that look like
3684 ;; Jim Thompson <thompson@wg2.waii.com>
3685 ((string-match "\\(.*\\)[ \t]+<.*>" fromstring)
3686 (substring fromstring (match-beginning 1) (match-end 1)))
3687
3688 ;; Couldn't find a real name -- show the address instead.
3689 (t fromstring)))
3690 "From ???")))
3691
a18ed129 3692;; A hook to bind to `gnus-article-prepare-hook'. This will set the
87a16a06
RS
3693;; `ps-left-headers' specially for gnus articles. Unfortunately,
3694;; `gnus-article-mode-hook' is called only once, the first time the *Article*
12d89a2e
RS
3695;; buffer enters that mode, so it would only work for the first time
3696;; we ran gnus. The second time, this hook wouldn't get set up. The
87a16a06 3697;; only alternative is `gnus-article-prepare-hook'.
12d89a2e
RS
3698(defun ps-gnus-article-prepare-hook ()
3699 (setq ps-header-lines 3)
3700 (setq ps-left-header
3701 ;; The left headers will display the article's subject, its
3702 ;; author, and the newsgroup it was in.
3703 (list 'ps-article-subject 'ps-article-author 'gnus-newsgroup-name)))
3704
a18ed129
RS
3705;; A hook to bind to `vm-mode-hook' to locally bind prsc and set the
3706;; `ps-left-headers' specially for mail messages.
12d89a2e 3707(defun ps-vm-mode-hook ()
00aa16af 3708 (local-set-key (ps-prsc) 'ps-vm-print-message-from-summary)
12d89a2e
RS
3709 (setq ps-header-lines 3)
3710 (setq ps-left-header
3711 ;; The left headers will display the message's subject, its
3712 ;; author, and the name of the folder it was in.
3713 (list 'ps-article-subject 'ps-article-author 'buffer-name)))
3714
3715;; Every now and then I forget to switch from the *Summary* buffer to
3716;; the *Article* before hitting prsc, and a nicely formatted list of
3717;; article subjects shows up at the printer. This function, bound to
3718;; prsc for the gnus *Summary* buffer means I don't have to switch
3719;; buffers first.
87a16a06 3720;; sb: Updated for Gnus 5.
12d89a2e
RS
3721(defun ps-gnus-print-article-from-summary ()
3722 (interactive)
a18ed129 3723 (ps-print-message-from-summary 'gnus-article-buffer "*Article*"))
ef2cbb24 3724
87a16a06 3725;; See `ps-gnus-print-article-from-summary'. This function does the
12d89a2e
RS
3726;; same thing for vm.
3727(defun ps-vm-print-message-from-summary ()
3728 (interactive)
a18ed129 3729 (ps-print-message-from-summary 'vm-mail-buffer ""))
ef2cbb24 3730
87a16a06 3731;; A hook to bind to bind to `gnus-summary-setup-buffer' to locally bind
12d89a2e
RS
3732;; prsc.
3733(defun ps-gnus-summary-setup ()
00aa16af 3734 (local-set-key (ps-prsc) 'ps-gnus-print-article-from-summary))
12d89a2e
RS
3735
3736;; Look in an article or mail message for the Subject: line. To be
87a16a06 3737;; placed in `ps-left-headers'.
12d89a2e
RS
3738(defun ps-info-file ()
3739 (save-excursion
3740 (goto-char (point-min))
a97592dd 3741 (if (re-search-forward "File:[ \t]+\\([^, \t\n]*\\)" nil t)
055e7bf2 3742 (buffer-substring-no-properties (match-beginning 1) (match-end 1))
12d89a2e
RS
3743 "File ???")))
3744
3745;; Look in an article or mail message for the Subject: line. To be
87a16a06 3746;; placed in `ps-left-headers'.
12d89a2e
RS
3747(defun ps-info-node ()
3748 (save-excursion
3749 (goto-char (point-min))
a97592dd 3750 (if (re-search-forward "Node:[ \t]+\\([^,\t\n]*\\)" nil t)
055e7bf2 3751 (buffer-substring-no-properties (match-beginning 1) (match-end 1))
12d89a2e
RS
3752 "Node ???")))
3753
3754(defun ps-info-mode-hook ()
3755 (setq ps-left-header
3756 ;; The left headers will display the node name and file name.
3757 (list 'ps-info-node 'ps-info-file)))
3758
043620f4
KH
3759;; WARNING! The following function is a *sample* only, and is *not*
3760;; meant to be used as a whole unless you understand what the effects
87a16a06
RS
3761;; will be! (In fact, this is a copy of Jim's setup for ps-print --
3762;; I'd be very surprised if it was useful to *anybody*, without
043620f4
KH
3763;; modification.)
3764
12d89a2e 3765(defun ps-jts-ps-setup ()
00aa16af
RS
3766 (global-set-key (ps-prsc) 'ps-spool-buffer-with-faces) ;f22 is prsc
3767 (global-set-key (ps-s-prsc) 'ps-spool-region-with-faces)
3768 (global-set-key (ps-c-prsc) 'ps-despool)
12d89a2e
RS
3769 (add-hook 'gnus-article-prepare-hook 'ps-gnus-article-prepare-hook)
3770 (add-hook 'gnus-summary-mode-hook 'ps-gnus-summary-setup)
3771 (add-hook 'vm-mode-hook 'ps-vm-mode-hook)
00aa16af 3772 (add-hook 'vm-mode-hooks 'ps-vm-mode-hook)
12d89a2e
RS
3773 (add-hook 'Info-mode-hook 'ps-info-mode-hook)
3774 (setq ps-spool-duplex t)
3775 (setq ps-print-color-p nil)
3776 (setq ps-lpr-command "lpr")
bcc0d457
RS
3777 (setq ps-lpr-switches '("-Jjct,duplex_long"))
3778 'ps-jts-ps-setup)
3779
3780;; WARNING! The following function is a *sample* only, and is *not*
3781;; meant to be used as a whole unless it corresponds to your needs.
3782;; (In fact, this is a copy of Jack's setup for ps-print --
3783;; I would not be that surprised if it was useful to *anybody*,
3784;; without modification.)
3785
3786(defun ps-jack-setup ()
87a16a06 3787 (setq ps-print-color-p nil
bcc0d457
RS
3788 ps-lpr-command "lpr"
3789 ps-lpr-switches (list)
3790
87a16a06
RS
3791 ps-paper-type 'a4
3792 ps-landscape-mode t
bcc0d457
RS
3793 ps-number-of-columns 2
3794
3795 ps-left-margin (/ (* 72 1.0) 2.54) ; 1.0 cm
3796 ps-right-margin (/ (* 72 1.0) 2.54) ; 1.0 cm
3797 ps-inter-column (/ (* 72 1.0) 2.54) ; 1.0 cm
3798 ps-bottom-margin (/ (* 72 1.5) 2.54) ; 1.5 cm
3799 ps-top-margin (/ (* 72 1.5) 2.54) ; 1.5 cm
3800 ps-header-offset (/ (* 72 1.0) 2.54) ; 1.0 cm
3801 ps-header-line-pad .15
3802 ps-print-header t
3803 ps-print-header-frame t
3804 ps-header-lines 2
3805 ps-show-n-of-n t
3806 ps-spool-duplex nil
3807
3808 ps-font-family 'Courier
3809 ps-font-size 5.5
3810 ps-header-font-family 'Helvetica
3811 ps-header-font-size 6
3812 ps-header-title-font-size 8)
3813 'ps-jack-setup)
12d89a2e
RS
3814
3815(provide 'ps-print)
b87c5d3d 3816
12d89a2e 3817;;; ps-print.el ends here