(quail-devanagari-compose-characters):
[bpt/emacs.git] / lisp / ps-print.el
CommitLineData
535efc38 1;;; ps-print.el --- Print text from the buffer as PostScript
12d89a2e 2
12b88fff 3;; Copyright (C) 1993, 94, 95, 96, 97, 1998 Free Software Foundation, Inc.
ef2cbb24 4
e65df0a1 5;; Author: Jim Thompson (was <thompson@wg2.waii.com>)
024ced4d 6;; Author: Jacques Duthen (was <duthen@cegelec-red.fr>)
e65df0a1 7;; Author: Vinicius Jose Latorre <vinicius@cpqd.com.br>
024ced4d
KH
8;; Author: Kenichi Handa <handa@etl.go.jp> (multi-byte characters)
9;; Maintainer: Kenichi Handa <handa@etl.go.jp> (multi-byte characters)
e65df0a1
KH
10;; Maintainer: Vinicius Jose Latorre <vinicius@cpqd.com.br>
11;; Keywords: print, PostScript
d3ab8dac
KH
12;; Time-stamp: <98/10/13 15:42:23 vinicius>
13;; Version: 4.1.1
e65df0a1 14
d3ab8dac
KH
15(defconst ps-print-version "4.1.1"
16 "ps-print.el, v 4.1.1 <98/10/13 vinicius>
090be653 17
535efc38 18Vinicius's last change version -- this file may have been edited as part of
090be653
RS
19Emacs without changes to the version number. When reporting bugs,
20please also report the version of Emacs, if any, that ps-print was
21distributed with.
22
23Please send all bug fixes and enhancements to
8bd22fcf 24 Vinicius Jose Latorre <vinicius@cpqd.com.br>.
090be653 25")
ef2cbb24 26
86c10ecb 27;; This file is part of GNU Emacs.
ef2cbb24
RS
28
29;; GNU Emacs is free software; you can redistribute it and/or modify
30;; it under the terms of the GNU General Public License as published by
31;; the Free Software Foundation; either version 2, or (at your option)
32;; any later version.
33
34;; GNU Emacs is distributed in the hope that it will be useful,
35;; but WITHOUT ANY WARRANTY; without even the implied warranty of
36;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
37;; GNU General Public License for more details.
38
39;; You should have received a copy of the GNU General Public License
b578f267
EN
40;; along with GNU Emacs; see the file COPYING. If not, write to the
41;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
42;; Boston, MA 02111-1307, USA.
ef2cbb24 43
12d89a2e 44;;; Commentary:
ef2cbb24
RS
45
46;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
47;;
12d89a2e 48;; About ps-print
ef2cbb24 49;; --------------
bcc0d457 50;;
ef2cbb24
RS
51;; This package provides printing of Emacs buffers on PostScript
52;; printers; the buffer's bold and italic text attributes are
d3ab8dac 53;; preserved in the printer output. ps-print is intended for use with
00aa16af
RS
54;; Emacs 19 or Lucid Emacs, together with a fontifying package such as
55;; font-lock or hilit.
12d89a2e 56;;
87a16a06
RS
57;; ps-print uses the same face attributes defined through font-lock or hilit
58;; to print a PostScript file, but some faces are better seeing on the screen
59;; than on paper, specially when you have a black/white PostScript printer.
60;;
61;; ps-print allows a remap of face to another one that it is better to print,
62;; for example, the face font-lock-comment-face (if you are using font-lock)
63;; could have bold or italic attribute when printing, besides foreground color.
64;; This remap improves printing look (see How Ps-Print Maps Faces).
65;;
bcc0d457 66;;
12d89a2e 67;; Using ps-print
ef2cbb24 68;; --------------
ef2cbb24 69;;
12d89a2e
RS
70;; The Commands
71;;
d3ab8dac 72;; ps-print provides eight commands for generating PostScript images
12d89a2e
RS
73;; of Emacs buffers:
74;;
75;; ps-print-buffer
76;; ps-print-buffer-with-faces
77;; ps-print-region
78;; ps-print-region-with-faces
79;; ps-spool-buffer
80;; ps-spool-buffer-with-faces
81;; ps-spool-region
82;; ps-spool-region-with-faces
83;;
84;; These commands all perform essentially the same function: they
85;; generate PostScript images suitable for printing on a PostScript
86;; printer or displaying with GhostScript. These commands are
87;; collectively referred to as "ps-print- commands".
88;;
89;; The word "print" or "spool" in the command name determines when the
90;; PostScript image is sent to the printer:
ef2cbb24 91;;
12d89a2e
RS
92;; print - The PostScript image is immediately sent to the
93;; printer;
ef2cbb24 94;;
12d89a2e
RS
95;; spool - The PostScript image is saved temporarily in an
96;; Emacs buffer. Many images may be spooled locally
97;; before printing them. To send the spooled images
bcc0d457 98;; to the printer, use the command `ps-despool'.
ef2cbb24 99;;
12d89a2e
RS
100;; The spooling mechanism was designed for printing lots of small
101;; files (mail messages or netnews articles) to save paper that would
102;; otherwise be wasted on banner pages, and to make it easier to find
103;; your output at the printer (it's easier to pick up one 50-page
104;; printout than to find 50 single-page printouts).
06fb6aab 105;;
d3ab8dac 106;; ps-print has a hook in the `kill-emacs-hook' so that you won't
a7acbbe4 107;; accidentally quit from Emacs while you have unprinted PostScript
12d89a2e
RS
108;; waiting in the spool buffer. If you do attempt to exit with
109;; spooled PostScript, you'll be asked if you want to print it, and if
110;; you decline, you'll be asked to confirm the exit; this is modeled
111;; on the confirmation that Emacs uses for modified buffers.
112;;
113;; The word "buffer" or "region" in the command name determines how
114;; much of the buffer is printed:
115;;
116;; buffer - Print the entire buffer.
117;;
118;; region - Print just the current region.
119;;
120;; The -with-faces suffix on the command name means that the command
121;; will include font, color, and underline information in the
122;; PostScript image, so the printed image can look as pretty as the
123;; buffer. The ps-print- commands without the -with-faces suffix
124;; don't include font, color, or underline information; images printed
125;; with these commands aren't as pretty, but are faster to generate.
126;;
127;; Two ps-print- command examples:
128;;
129;; ps-print-buffer - print the entire buffer,
130;; without font, color, or
131;; underline information, and
132;; send it immediately to the
133;; printer.
134;;
135;; ps-spool-region-with-faces - print just the current region;
136;; include font, color, and
137;; underline information, and
138;; spool the image in Emacs to
139;; send to the printer later.
140;;
141;;
142;; Invoking Ps-Print
bcc0d457 143;; -----------------
ef2cbb24 144;;
12d89a2e 145;; To print your buffer, type
ef2cbb24 146;;
12d89a2e 147;; M-x ps-print-buffer
ef2cbb24 148;;
12d89a2e
RS
149;; or substitute one of the other seven ps-print- commands. The
150;; command will generate the PostScript image and print or spool it as
151;; specified. By giving the command a prefix argument
152;;
153;; C-u M-x ps-print-buffer
154;;
155;; it will save the PostScript image to a file instead of sending it
156;; to the printer; you will be prompted for the name of the file to
157;; save the image to. The prefix argument is ignored by the commands
158;; that spool their images, but you may save the spooled images to a
bcc0d457 159;; file by giving a prefix argument to `ps-despool':
12d89a2e
RS
160;;
161;; C-u M-x ps-despool
162;;
bcc0d457 163;; When invoked this way, `ps-despool' will prompt you for the name of
12d89a2e
RS
164;; the file to save to.
165;;
bcc0d457
RS
166;; Any of the `ps-print-' commands can be bound to keys; I recommend
167;; binding `ps-spool-buffer-with-faces', `ps-spool-region-with-faces',
168;; and `ps-despool'. Here are the bindings I use on my Sun 4 keyboard:
12d89a2e
RS
169;;
170;; (global-set-key 'f22 'ps-spool-buffer-with-faces) ;f22 is prsc
ef2cbb24
RS
171;; (global-set-key '(shift f22) 'ps-spool-region-with-faces)
172;; (global-set-key '(control f22) 'ps-despool)
173;;
12d89a2e
RS
174;;
175;; The Printer Interface
bcc0d457 176;; ---------------------
12d89a2e 177;;
bcc0d457 178;; The variables `ps-lpr-command' and `ps-lpr-switches' determine what
12d89a2e 179;; command is used to send the PostScript images to the printer, and
bcc0d457
RS
180;; what arguments to give the command. These are analogous to
181;; `lpr-command' and `lpr-switches'.
87a16a06 182;;
bcc0d457
RS
183;; Make sure that they contain appropriate values for your system;
184;; see the usage notes below and the documentation of these variables.
185;;
d3ab8dac
KH
186;; The variable `ps-printer-name' determine the name of a local printer for
187;; printing PostScript files.
188;;
bcc0d457
RS
189;; NOTE: `ps-lpr-command' and `ps-lpr-switches' take their initial values
190;; from the variables `lpr-command' and `lpr-switches'. If you have
191;; `lpr-command' set to invoke a pretty-printer such as `enscript',
192;; then ps-print won't work properly. `ps-lpr-command' must name
12d89a2e 193;; a program that does not format the files it prints.
d3ab8dac
KH
194;; `ps-printer-name' takes its initial value from the variable
195;; `printer-name'.
12d89a2e
RS
196;;
197;;
bcc0d457
RS
198;; The Page Layout
199;; ---------------
12d89a2e 200;;
bcc0d457
RS
201;; All dimensions are floats in PostScript points.
202;; 1 inch == 2.54 cm == 72 points
203;; 1 cm == (/ 1 2.54) inch == (/ 72 2.54) points
12d89a2e 204;;
bcc0d457
RS
205;; The variable `ps-paper-type' determines the size of paper ps-print
206;; formats for; it should contain one of the symbols:
207;; `a4' `a3' `letter' `legal' `letter-small' `tabloid'
208;; `ledger' `statement' `executive' `a4small' `b4' `b5'
12d89a2e 209;;
bcc0d457
RS
210;; The variable `ps-landscape-mode' determines the orientation
211;; of the printing on the page:
212;; nil means `portrait' mode, non-nil means `landscape' mode.
213;; There is no oblique mode yet, though this is easy to do in ps.
87a16a06 214;;
bcc0d457
RS
215;; In landscape mode, the text is NOT scaled: you may print 70 lines
216;; in portrait mode and only 50 lignes in landscape mode.
217;; The margins represent margins in the printed paper:
218;; the top margin is the margin between the top of the page
219;; and the printed header, whatever the orientation is.
043620f4 220;;
bcc0d457
RS
221;; The variable `ps-number-of-columns' determines the number of columns
222;; both in landscape and portrait mode.
223;; You can use:
224;; - (the standard) one column portrait mode
225;; - (my favorite) two columns landscape mode (which spares trees)
226;; but also
227;; - one column landscape mode for files with very long lines.
228;; - multi-column portrait or landscape mode
12d89a2e 229;;
12d89a2e 230;;
bcc0d457
RS
231;; Horizontal layout
232;; -----------------
12d89a2e 233;;
bcc0d457
RS
234;; The horizontal layout is determined by the variables
235;; `ps-left-margin' `ps-inter-column' `ps-right-margin'
236;; as follows:
12d89a2e 237;;
bcc0d457
RS
238;; ------------------------------------------
239;; | | | | | | | |
240;; | lm | text | ic | text | ic | text | rm |
241;; | | | | | | | |
242;; ------------------------------------------
12d89a2e 243;;
bcc0d457
RS
244;; If `ps-number-of-columns' is 1, `ps-inter-column' is not relevant.
245;; Usually, lm = rm > 0 and ic = lm
246;; If (ic < 0), the text of adjacent columns can overlap.
12d89a2e 247;;
12d89a2e 248;;
bcc0d457
RS
249;; Vertical layout
250;; ---------------
251;;
252;; The vertical layout is determined by the variables
253;; `ps-bottom-margin' `ps-top-margin' `ps-header-offset'
254;; as follows:
255;;
256;; |--------| |--------|
257;; | tm | | tm |
258;; |--------| |--------|
259;; | header | | |
260;; |--------| | |
261;; | ho | | |
262;; |--------| or | text |
263;; | | | |
264;; | text | | |
265;; | | | |
266;; |--------| |--------|
267;; | bm | | bm |
268;; |--------| |--------|
269;;
270;; If `ps-print-header' is nil, `ps-header-offset' is not relevant.
271;; The margins represent margins in the printed paper:
272;; the top margin is the margin between the top of the page
273;; and the printed header, whatever the orientation is.
12d89a2e
RS
274;;
275;;
276;; Headers
bcc0d457 277;; -------
12d89a2e 278;;
d3ab8dac 279;; ps-print can print headers at the top of each column or at the top
12b88fff
RS
280;; of each page; the default headers contain the following four items:
281;; on the left, the name of the buffer and, if the buffer is visiting
282;; a file, the file's directory; on the right, the page number and
283;; date of printing. The default headers look something like this:
12d89a2e
RS
284;;
285;; ps-print.el 1/21
286;; /home/jct/emacs-lisp/ps/new 94/12/31
06fb6aab 287;;
12d89a2e 288;; When printing on duplex printers, left and right are reversed so
bcc0d457 289;; that the page numbers are toward the outside (cf. `ps-spool-duplex').
12d89a2e 290;;
bcc0d457
RS
291;; Headers are configurable:
292;; To turn them off completely, set `ps-print-header' to nil.
293;; To turn off the header's gaudy framing box,
294;; set `ps-print-header-frame' to nil.
295;;
12b88fff
RS
296;; To print only one header at the top of each page,
297;; set `ps-print-only-one-header' to t.
298;;
bcc0d457 299;; The font family and size of text in the header are determined
06fb6aab 300;; by the variables `ps-header-font-family', `ps-header-font-size' and
bcc0d457
RS
301;; `ps-header-title-font-size' (see below).
302;;
303;; The variable `ps-header-line-pad' determines the portion of a header
304;; title line height to insert between the header frame and the text
305;; it contains, both in the vertical and horizontal directions:
306;; .5 means half a line.
307
308;; Page numbers are printed in `n/m' format, indicating page n of m pages;
309;; to omit the total page count and just print the page number,
310;; set `ps-show-n-of-n' to nil.
12d89a2e
RS
311;;
312;; The amount of information in the header can be changed by changing
bcc0d457 313;; the number of lines. To show less, set `ps-header-lines' to 1, and
12d89a2e 314;; the header will show only the buffer name and page number. To show
bcc0d457 315;; more, set `ps-header-lines' to 3, and the header will show the time of
12d89a2e
RS
316;; printing below the date.
317;;
318;; To change the content of the headers, change the variables
bcc0d457
RS
319;; `ps-left-header' and `ps-right-header'.
320;; These variables are lists, specifying top-to-bottom the text
321;; to display on the left or right side of the header.
322;; Each element of the list should be a string or a symbol.
323;; Strings are inserted directly into the PostScript arrays,
324;; and should contain the PostScript string delimiters '(' and ')'.
12d89a2e
RS
325;;
326;; Symbols in the header format lists can either represent functions
327;; or variables. Functions are called, and should return a string to
328;; show in the header. Variables should contain strings to display in
329;; the header. In either case, function or variable, the PostScript
a7acbbe4 330;; string delimiters are added by ps-print, and should not be part of
12d89a2e
RS
331;; the returned value.
332;;
333;; Here's an example: say we want the left header to display the text
334;;
335;; Moe
336;; Larry
337;; Curly
338;;
339;; where we have a function to return "Moe"
340;;
341;; (defun moe-func ()
342;; "Moe")
343;;
344;; a variable specifying "Larry"
345;;
346;; (setq larry-var "Larry")
347;;
bcc0d457 348;; and a literal for "Curly". Here's how `ps-left-header' should be
12d89a2e
RS
349;; set:
350;;
351;; (setq ps-left-header (list 'moe-func 'larry-var "(Curly)"))
352;;
353;; Note that Curly has the PostScript string delimiters inside his
bcc0d457 354;; quotes -- those aren't misplaced lisp delimiters!
87a16a06 355;;
bcc0d457
RS
356;; Without them, PostScript would attempt to call the undefined
357;; function Curly, which would result in a PostScript error.
87a16a06 358;;
bcc0d457
RS
359;; Since most printers don't report PostScript errors except by
360;; aborting the print job, this kind of error can be hard to track down.
87a16a06 361;;
bcc0d457 362;; Consider yourself warned!
12d89a2e
RS
363;;
364;;
d3ab8dac
KH
365;; PostScript Prologue Header
366;; --------------------------
367;;
368;; It is possible to add PostScript prologue header comments besides that
369;; ps-print generates by setting the variable `ps-print-prologue-header'.
370;;
371;; `ps-print-prologue-header' may be a string or a symbol function which
372;; returns a string. Note that this string is inserted on PostScript prologue
373;; header section which is used to define some document characteristic through
374;; PostScript special comments, like "%%Requirements: jog\n".
375;;
376;; By default `ps-print-prologue-header' is nil.
377;;
378;; ps-print always inserts the %%Requirements: comment, so if you need to insert
379;; more requirements put them first in `ps-print-prologue-header' using the
380;; "%%+" comment. For example, if you need to set numcopies to 3 and jog on
381;; requirements and set %%LanguageLevel: to 2, do:
382;;
383;; (setq ps-print-prologue-header
384;; "%%+ numcopies(3) jog\n%%LanguageLevel: 2\n")
385;;
386;; The duplex requirement is inserted by ps-print (see section Duplex Printers).
387;;
388;; Do not forget to terminate the string with "\n".
389;;
390;; For more information about PostScript document comments, see:
391;; PostScript Language Reference Manual (2nd edition)
392;; Adobe Systems Incorporated
393;; Appendix G: Document Structuring Conventions -- Version 3.0
394;;
395;;
12d89a2e 396;; Duplex Printers
bcc0d457 397;; ---------------
12d89a2e
RS
398;;
399;; If you have a duplex-capable printer (one that prints both sides of
bcc0d457 400;; the paper), set `ps-spool-duplex' to t.
d3ab8dac 401;; ps-print will insert blank pages to make sure each buffer starts
bcc0d457
RS
402;; on the correct side of the paper.
403;; Don't forget to set `ps-lpr-switches' to select duplex printing
404;; for your printer.
405;;
06fb6aab 406;;
857686a6
RS
407;; Control And 8-bit Characters
408;; ----------------------------
409;;
410;; The variable `ps-print-control-characters' specifies whether you want to see
411;; a printable form for control and 8-bit characters, that is, instead of
6bdb808e 412;; sending, for example, a ^D (\004) to printer, it is sent the string "^D".
857686a6
RS
413;;
414;; Valid values for `ps-print-control-characters' are:
415;;
c82b4a75
KH
416;; 8-bit This is the value to use when you want an ASCII encoding of
417;; any control or non-ASCII character. Control characters are
418;; encoded as "^D", and non-ASCII characters have an
6bdb808e
RS
419;; octal encoding.
420;;
c82b4a75 421;; control-8-bit This is the value to use when you want an ASCII encoding of
6bdb808e
RS
422;; any control character, whether it is 7 or 8-bit.
423;; European 8-bits accented characters are printed according
424;; the current font.
425;;
c82b4a75 426;; control Only ASCII control characters have an ASCII encoding.
6bdb808e
RS
427;; European 8-bits accented characters are printed according
428;; the current font.
429;;
c82b4a75 430;; nil No ASCII encoding. Any character is printed according the
6bdb808e 431;; current font.
857686a6
RS
432;;
433;; Any other value is treated as nil.
434;;
496725ad 435;; The default is `control-8-bit'.
857686a6
RS
436;;
437;; Characters TAB, NEWLINE and FORMFEED are always treated by ps-print engine.
438;;
439;;
024ced4d
KH
440;; Printing Multi-byte Buffer
441;; --------------------------
d3ab8dac 442;;
024ced4d 443;; The variable `ps-multibyte-buffer' specifies the ps-print multi-byte buffer
d3ab8dac
KH
444;; handling.
445;;
446;; Valid values for `ps-multibyte-buffer' are:
447;;
448;; nil This is the value to use when you are printing
449;; buffer with only ASCII and Latin characters.
450;;
451;; `non-latin-printer' This is the value to use when you have a japanese
452;; or korean PostScript printer and want to print
453;; buffer with ASCII, Latin-1, Japanese (JISX0208 and
454;; JISX0201-Kana) and Korean characters. At present,
455;; it was not tested the Korean characters printing.
456;; If you have a korean PostScript printer, please,
457;; test it.
458;;
459;; `bdf-font' This is the value to use when you want to print
460;; buffer with BDF fonts. BDF fonts include both latin
461;; and non-latin fonts. BDF (Bitmap Distribution
462;; Format) is a format used for distributing X's font
463;; source file. BDF fonts are included in
464;; `intlfonts-1.1' which is a collection of X11 fonts
465;; for all characters supported by Emacs. In order to
466;; use this value, be sure to have installed
467;; `intlfonts-1.1' and set the variable
468;; `bdf-directory-list' appropriately (see bdf.el for
469;; documentation of this variable).
470;;
471;; `bdf-font-except-latin' This is like `bdf-font' except that it is used
472;; PostScript default fonts to print ASCII and Latin-1
473;; characters. This is convenient when you want or
474;; need to use both latin and non-latin characters on
475;; the same buffer. See `ps-font-family',
476;; `ps-header-font-family' and `ps-font-info-database'.
e65df0a1 477;;
d3ab8dac 478;; Any other value is treated as nil.
e65df0a1 479;;
d3ab8dac 480;; The default is nil.
e65df0a1
KH
481;;
482;;
87a16a06
RS
483;; Line Number
484;; -----------
485;;
a18ed129
RS
486;; The variable `ps-line-number' specifies whether to number each line;
487;; non-nil means do so. The default is nil (don't number each line).
87a16a06
RS
488;;
489;;
490;; Zebra Stripes
491;; -------------
492;;
a18ed129
RS
493;; Zebra stripes are a kind of background that appear "underneath" the text
494;; and can make the text easier to read. They look like this:
87a16a06
RS
495;;
496;; XXXXXXXXXXXXXXXXXXXXXXXX
497;; XXXXXXXXXXXXXXXXXXXXXXXX
535efc38
RS
498;; XXXXXXXXXXXXXXXXXXXXXXXX
499;;
87a16a06
RS
500;;
501;;
502;; XXXXXXXXXXXXXXXXXXXXXXXX
503;; XXXXXXXXXXXXXXXXXXXXXXXX
535efc38 504;; XXXXXXXXXXXXXXXXXXXXXXXX
87a16a06 505;;
06fb6aab 506;; The blocks of X's represent rectangles filled with a light gray color.
a18ed129
RS
507;; Each rectangle extends all the way across the page.
508;;
509;; The height, in lines, of each rectangle is controlled by
535efc38
RS
510;; the variable `ps-zebra-stripe-height', which is 3 by default.
511;; The distance between stripes equals the height of a stripe.
8bd22fcf 512;;
01961237 513;; The variable `ps-zebra-stripes' controls whether to print zebra stripes.
a18ed129
RS
514;; Non-nil means yes, nil means no. The default is nil.
515;;
516;; See also section How Ps-Print Has A Text And/Or Image On Background.
87a16a06 517;;
87a16a06 518;;
12b88fff
RS
519;; Hooks
520;; -----
521;;
d3ab8dac 522;; ps-print has the following hook variables:
12b88fff
RS
523;;
524;; `ps-print-hook'
525;; It is evaluated once before any printing process. This is the right
526;; place to initialize ps-print global data.
527;; For an example, see section Adding a New Font Family.
528;;
529;; `ps-print-begin-page-hook'
530;; It is evaluated on each real beginning of page, that is, ps-print
531;; considers each beginning of column as a beginning of page, and a real
532;; beginning of page is when the beginning of column coincides with a
533;; paper change on your printer.
534;;
535;; `ps-print-begin-column-hook'
536;; It is evaluated on each beginning of column, except in the beginning
537;; of column that `ps-print-begin-page-hook' is evaluated.
538;;
539;;
540;; Font Managing
bcc0d457
RS
541;; -------------
542;;
d3ab8dac 543;; ps-print now knows rather precisely some fonts:
bcc0d457
RS
544;; the variable `ps-font-info-database' contains information
545;; for a list of font families (currently mainly `Courier' `Helvetica'
546;; `Times' `Palatino' `Helvetica-Narrow' `NewCenturySchlbk').
547;; Each font family contains the font names for standard, bold, italic
548;; and bold-italic characters, a reference size (usually 10) and the
549;; corresponding line height, width of a space and average character width.
06fb6aab 550;;
bcc0d457
RS
551;; The variable `ps-font-family' determines which font family
552;; is to be used for ordinary text.
553;; If its value does not correspond to a known font family,
554;; an error message is printed into the `*Messages*' buffer,
555;; which lists the currently available font families.
556;;
557;; The variable `ps-font-size' determines the size (in points)
915293a2 558;; of the font for ordinary text, when generating PostScript.
bcc0d457
RS
559;; Its value is a float.
560;;
561;; Similarly, the variable `ps-header-font-family' determines
562;; which font family is to be used for text in the header.
563;; The variable `ps-header-font-size' determines the font size,
564;; in points, for text in the header.
565;; The variable `ps-header-title-font-size' determines the font size,
566;; in points, for the top line of text in the header.
567;;
568;;
12b88fff 569;; Adding a New Font Family
bcc0d457
RS
570;; ------------------------
571;;
572;; To use a new font family, you MUST first teach ps-print
87a16a06 573;; this font, i.e., add its information to `ps-font-info-database',
bcc0d457
RS
574;; otherwise ps-print cannot correctly place line and page breaks.
575;;
87a16a06 576;; For example, assuming `Helvetica' is unknown,
bcc0d457
RS
577;; you first need to do the following ONLY ONCE:
578;;
579;; - create a new buffer
580;; - generate the PostScript image to a file (C-u M-x ps-print-buffer)
581;; - open this file and find the line:
582;; `% 3 cm 20 cm moveto 10 /Courier ReportFontInfo showpage'
a18ed129 583;; - delete the leading `%' (which is the PostScript comment character)
bcc0d457
RS
584;; - replace in this line `Courier' by the new font (say `Helvetica')
585;; to get the line:
586;; `3 cm 20 cm moveto 10 /Helvetica ReportFontInfo showpage'
587;; - send this file to the printer (or to ghostscript).
588;; You should read the following on the output page:
589;;
590;; For Helvetica 10 point, the line height is 11.56, the space width is 2.78
591;; and a crude estimate of average character width is 5.09243
592;;
593;; - Add these values to the `ps-font-info-database':
594;; (setq ps-font-info-database
12b88fff
RS
595;; (append
596;; '((Helvetica ; the family key
597;; (fonts (normal . "Helvetica")
598;; (bold . "Helvetica-Bold")
599;; (italic . "Helvetica-Oblique")
600;; (bold-italic . "Helvetica-BoldOblique"))
601;; (size . 10.0)
602;; (line-height . 11.56)
603;; (space-width . 2.78)
604;; (avg-char-width . 5.09243)))
605;; ps-font-info-database))
bcc0d457
RS
606;; - Now you can use this font family with any size:
607;; (setq ps-font-family 'Helvetica)
608;; - if you want to use this family in another emacs session, you must
609;; put into your `~/.emacs':
610;; (require 'ps-print)
611;; (setq ps-font-info-database (append ...)))
612;; if you don't want to load ps-print, you have to copy the whole value:
613;; (setq ps-font-info-database '(<your stuff> <the standard stuff>))
12b88fff
RS
614;; or, use `ps-print-hook' (see section Hooks):
615;; (add-hook 'ps-print-hook
616;; '(lambda () (setq ps-font-info-database (append ...))))
bcc0d457
RS
617;;
618;; You can create new `mixed' font families like:
12b88fff
RS
619;; (my-mixed-family
620;; (fonts (normal . "Courier-Bold")
621;; (bold . "Helvetica")
622;; (italic . "Zapf-Chancery-MediumItalic")
623;; (bold-italic . "NewCenturySchlbk-BoldItalic")
624;; (w3-table-hack-x-face . "LineDrawNormal"))
625;; (size . 10.0)
626;; (line-height . 10.55)
627;; (space-width . 6.0)
628;; (avg-char-width . 6.0))
d3ab8dac 629;;
bcc0d457
RS
630;; Now you can use your new font family with any size:
631;; (setq ps-font-family 'my-mixed-family)
632;;
12b88fff
RS
633;; Note that on above example the `w3-table-hack-x-face' entry refers to
634;; a face symbol, so when printing this face it'll be used the font
635;; `LineDrawNormal'. If the face `w3-table-hack-x-face' is remapped to
636;; use bold and/or italic attribute, the corresponding entry (bold, italic
637;; or bold-italic) will be used instead of `w3-table-hack-x-face' entry.
638;;
639;; Note also that the font family entry order is irrelevant, so the above
640;; example could also be written:
641;; (my-mixed-family
642;; (size . 10.0)
643;; (fonts (w3-table-hack-x-face . "LineDrawNormal")
644;; (bold . "Helvetica")
645;; (bold-italic . "NewCenturySchlbk-BoldItalic")
646;; (italic . "Zapf-Chancery-MediumItalic")
647;; (normal . "Courier-Bold"))
648;; (avg-char-width . 6.0)
649;; (space-width . 6.0)
650;; (line-height . 10.55))
651;;
652;; Despite the note above, it is recommended that some convention about
653;; entry order be used.
654;;
bcc0d457
RS
655;; You can get information on all the fonts resident in YOUR printer
656;; by uncommenting the line:
657;; % 3 cm 20 cm moveto ReportAllFontInfo showpage
658;;
a18ed129
RS
659;; The PostScript file should be sent to YOUR PostScript printer.
660;; If you send it to ghostscript or to another PostScript printer,
bcc0d457
RS
661;; you may get slightly different results.
662;; Anyway, as ghostscript fonts are autoload, you won't get
663;; much font info.
664;;
665;;
666;; How Ps-Print Deals With Faces
667;; -----------------------------
12d89a2e 668;;
bcc0d457
RS
669;; The ps-print-*-with-faces commands attempt to determine which faces
670;; should be printed in bold or italic, but their guesses aren't
671;; always right. For example, you might want to map colors into faces
672;; so that blue faces print in bold, and red faces in italic.
12d89a2e 673;;
857686a6
RS
674;; It is possible to force ps-print to consider specific faces bold,
675;; italic or underline, no matter what font they are displayed in, by setting
676;; the variables `ps-bold-faces', `ps-italic-faces' and `ps-underlined-faces'.
677;; These variables contain lists of faces that ps-print should consider bold,
678;; italic or underline; to set them, put code like the following into your
679;; .emacs file:
12d89a2e 680;;
12b88fff 681;; (setq ps-bold-faces '(my-blue-face))
bcc0d457 682;; (setq ps-italic-faces '(my-red-face))
857686a6 683;; (setq ps-underlined-faces '(my-green-face))
bcc0d457
RS
684;;
685;; Faces like bold-italic that are both bold and italic should go in
686;; *both* lists.
687;;
d3ab8dac 688;; ps-print keeps internal lists of which fonts are bold and which are
bcc0d457
RS
689;; italic; these lists are built the first time you invoke ps-print.
690;; For the sake of efficiency, the lists are built only once; the same
691;; lists are referred in later invocations of ps-print.
692;;
693;; Because these lists are built only once, it's possible for them to
694;; get out of sync, if a face changes, or if new faces are added. To
695;; get the lists back in sync, you can set the variable
696;; `ps-build-face-reference' to t, and the lists will be rebuilt the
857686a6
RS
697;; next time ps-print is invoked. If you need that the lists always be
698;; rebuilt when ps-print is invoked, set the variable
699;; `ps-always-build-face-reference' to t.
bcc0d457
RS
700;;
701;;
702;; How Ps-Print Deals With Color
703;; -----------------------------
704;;
d3ab8dac 705;; ps-print detects faces with foreground and background colors
bcc0d457
RS
706;; defined and embeds color information in the PostScript image.
707;; The default foreground and background colors are defined by the
708;; variables `ps-default-fg' and `ps-default-bg'.
709;; On black-and-white printers, colors are displayed in grayscale.
710;; To turn off color output, set `ps-print-color-p' to nil.
711;;
712;;
87a16a06
RS
713;; How Ps-Print Maps Faces
714;; -----------------------
715;;
716;; As ps-print uses PostScript to print buffers, it is possible to have
717;; other attributes associated with faces. So the new attributes used
718;; by ps-print are:
719;;
720;; strikeout - like underline, but the line is in middle of text.
721;; overline - like underline, but the line is over the text.
722;; shadow - text will have a shadow.
723;; box - text will be surrounded by a box.
a18ed129 724;; outline - print characters as hollow outlines.
87a16a06 725;;
06fb6aab 726;; See the documentation for `ps-extend-face'.
87a16a06
RS
727;;
728;; Let's, for example, remap font-lock-keyword-face to another foreground color
729;; and bold attribute:
730;;
a18ed129 731;; (ps-extend-face '(font-lock-keyword-face "RoyalBlue" nil bold) 'MERGE)
87a16a06 732;;
6c8f2753
RS
733;; If you want to use a new face, define it first with `defface',
734;; and then call `ps-extend-face' to specify how to print it.
735;;
87a16a06
RS
736;;
737;; How Ps-Print Has A Text And/Or Image On Background
738;; --------------------------------------------------
739;;
d3ab8dac 740;; ps-print can print texts and/or EPS PostScript images on background; it is
87a16a06
RS
741;; possible to define the following text attributes: font name, font size,
742;; initial position, angle, gray scale and pages to print.
743;;
744;; It has the following EPS PostScript images attributes: file name containing
745;; the image, initial position, X and Y scales, angle and pages to print.
746;;
747;; See documentation for `ps-print-background-text' and
748;; `ps-print-background-image'.
749;;
750;; For example, if we wish to print text "preliminary" on all pages and text
751;; "special" on page 5 and from page 11 to page 17, we could specify:
752;;
753;; (setq ps-print-background-text
754;; '(("preliminary")
755;; ("special"
756;; "LeftMargin" "BottomMargin PrintHeight add" ; X and Y position
757;; ; (upper left corner)
758;; nil nil nil
12b88fff 759;; "PrintHeight neg PrintPageWidth atan" ; angle
87a16a06
RS
760;; 5 (11 . 17)) ; page list
761;; ))
762;;
763;; Similarly, we could print image "~/images/EPS-image1.ps" on all pages and
764;; image "~/images/EPS-image2.ps" on page 5 and from page 11 to page 17, we
765;; specify:
766;;
767;; (setq ps-print-background-image
768;; '(("~/images/EPS-image1.ps"
769;; "LeftMargin" "BottomMargin") ; X and Y position (lower left corner)
770;; ("~/images/EPS-image2.ps"
771;; "LeftMargin" "BottomMargin PrintHeight 2 div add" ; X and Y position
772;; ; (upper left corner)
773;; nil nil nil
774;; 5 (11 . 17)) ; page list
775;; ))
776;;
777;; If it is not possible to read (or does not exist) an image file, that file
778;; is ignored.
779;;
780;; The printing order is:
781;;
782;; 1. Print zebra stripes
783;; 2. Print background texts that it should be on all pages
784;; 3. Print background images that it should be on all pages
785;; 4. Print background texts only for current page (if any)
786;; 5. Print background images only for current page (if any)
787;; 6. Print header
a18ed129 788;; 7. Print buffer text (with faces, if specified) and line number
87a16a06
RS
789;;
790;;
bcc0d457
RS
791;; Utilities
792;; ---------
793;;
794;; Some tools are provided to help you customize your font setup.
795;;
796;; `ps-setup' returns (some part of) the current setup.
797;;
798;; To avoid wrapping too many lines, you may want to adjust the
799;; left and right margins and the font size. On UN*X systems, do:
800;; pr -t file | awk '{printf "%3d %s\n", length($0), $0}' | sort -r | head
801;; to determine the longest lines of your file.
87a16a06 802;; Then, the command `ps-line-lengths' will give you the correspondence
bcc0d457
RS
803;; between a line length (number of characters) and the maximum font
804;; size which doesn't wrap such a line with the current ps-print setup.
805;;
806;; The commands `ps-nb-pages-buffer' and `ps-nb-pages-region' display
87a16a06 807;; the correspondence between a number of pages and the maximum font
bcc0d457
RS
808;; size which allow the number of lines of the current buffer or of
809;; its current region to fit in this number of pages.
a18ed129
RS
810;;
811;; NOTE: line folding is not taken into account in this process and could
812;; change the results.
b87c5d3d 813;;
b87c5d3d 814;;
b87c5d3d
RS
815;; New since version 1.5
816;; ---------------------
b87c5d3d 817;;
bcc0d457 818;; Color output capability.
b87c5d3d 819;; Automatic detection of font attributes (bold, italic).
b87c5d3d 820;; Configurable headers with page numbers.
b87c5d3d 821;; Slightly faster.
b87c5d3d 822;; Support for different paper sizes.
b87c5d3d
RS
823;; Better conformance to PostScript Document Structure Conventions.
824;;
ef2cbb24 825;;
bcc0d457
RS
826;; New since version 2.8
827;; ---------------------
828;;
d3ab8dac
KH
829;; [vinicius] 980922 Vinicius Jose Latorre <vinicius@cpqd.com.br>
830;;
831;; PostScript prologue header comment insertion.
832;; Skip invisible text better.
833;;
e65df0a1
KH
834;; [keinichi] 980819 Kein'ichi Handa <handa@etl.go.jp>
835;;
024ced4d 836;; Multi-byte buffer handling.
e65df0a1 837;;
12b88fff
RS
838;; [vinicius] 980306 Vinicius Jose Latorre <vinicius@cpqd.com.br>
839;;
e65df0a1 840;; Skip invisible text.
12b88fff
RS
841;;
842;; [vinicius] 971130 Vinicius Jose Latorre <vinicius@cpqd.com.br>
843;;
844;; Hooks: `ps-print-hook', `ps-print-begin-page-hook' and
845;; `ps-print-begin-column-hook'.
846;; Put one header per page over the columns.
847;; Better database font management.
848;; Better control characters handling.
849;;
857686a6 850;; [vinicius] 971121 Vinicius Jose Latorre <vinicius@cpqd.com.br>
87a16a06 851;;
12b88fff 852;; Dynamic evaluation at print time of `ps-lpr-switches'.
87a16a06
RS
853;; Handle control characters.
854;; Face remapping.
855;; New face attributes.
856;; Line number.
857;; Zebra stripes.
858;; Text and/or image on background.
859;;
bcc0d457
RS
860;; [jack] 960517 Jacques Duthen <duthen@cegelec-red.fr>
861;;
a18ed129 862;; Font family and float size for text and header.
bcc0d457
RS
863;; Landscape mode.
864;; Multiple columns.
865;; Tools for page setup.
866;;
867;;
d3ab8dac 868;; Known bugs and limitations of ps-print
ef2cbb24 869;; --------------------------------------
bcc0d457 870;;
043620f4
KH
871;; Although color printing will work in XEmacs 19.12, it doesn't work
872;; well; in particular, bold or italic fonts don't print in the right
873;; background color.
874;;
875;; Invisible properties aren't correctly ignored in XEmacs 19.12.
876;;
b87c5d3d 877;; Automatic font-attribute detection doesn't work well, especially
00aa16af 878;; with hilit19 and older versions of get-create-face. Users having
bcc0d457 879;; problems with auto-font detection should use the lists
857686a6
RS
880;; `ps-italic-faces', `ps-bold-faces' and `ps-underlined-faces' and/or
881;; turn off automatic detection by setting `ps-auto-font-detect' to nil.
00aa16af 882;;
043620f4 883;; Automatic font-attribute detection doesn't work with XEmacs 19.12
857686a6
RS
884;; in tty mode; use the lists `ps-italic-faces', `ps-bold-faces' and
885;; `ps-underlined-faces' instead.
12d89a2e 886;;
00aa16af 887;; Still too slow; could use some hand-optimization.
ef2cbb24 888;;
12d89a2e 889;; Default background color isn't working.
ef2cbb24
RS
890;;
891;; Faces are always treated as opaque.
892;;
12d89a2e 893;; Epoch and Emacs 18 not supported. At all.
ef2cbb24 894;;
06fb6aab 895;; Fixed-pitch fonts work better for line folding, but are not required.
bcc0d457
RS
896;;
897;; `ps-nb-pages-buffer' and `ps-nb-pages-region' don't take care
898;; of folding lines.
ef2cbb24 899;;
12d89a2e 900;;
d3ab8dac 901;; Things to change
bcc0d457 902;; ----------------
ef2cbb24 903;;
d3ab8dac 904;; 2-up and 4-up capabilities.
12b88fff 905;; Avoid page break inside a paragraph.
bcc0d457 906;; Add `ps-non-bold-faces' and `ps-non-italic-faces' (should be easy).
bcc0d457
RS
907;; Improve the memory management for big files (hard?).
908;; `ps-nb-pages-buffer' and `ps-nb-pages-region' should take care
909;; of folding lines.
ef2cbb24 910;;
ef2cbb24 911;;
12d89a2e
RS
912;; Acknowledgements
913;; ----------------
12b88fff 914;;
024ced4d 915;; Thanks to Kein'ichi Handa <handa@etl.go.jp> for multi-byte buffer handling.
915293a2
KH
916;;
917;; Thanks to Matthew O Persico <Matthew.Persico@lazard.com> for line number on
918;; empty columns.
919;;
920;; Thanks to Theodore Jump <tjump@cais.com> for adjust PostScript code order on
921;; last page.
922;;
6bdb808e
RS
923;; Thanks to Roland Ducournau <ducour@lirmm.fr> for
924;; `ps-print-control-characters' variable documentation.
925;;
12b88fff
RS
926;; Thanks to Marcus G Daniels <marcus@cathcart.sysc.pdx.edu> for a better
927;; database font management.
928;;
929;; Thanks to Martin Boyer <gamin@videotron.ca> for some ideas on putting one
6bdb808e
RS
930;; header per page over the columns and correct line numbers when printing a
931;; region.
12b88fff
RS
932;;
933;; Thanks to Steven L Baur <steve@miranova.com> for dynamic evaluation at
934;; print time of `ps-lpr-switches'.
935;;
6bdb808e
RS
936;; Thanks to Kevin Rodgers <kevinr@ihs.com> for handling control characters
937;; (his code was severely modified, but the main idea was kept).
938;;
12b88fff
RS
939;; Thanks to some suggestions on:
940;; * Face color map: Marco Melgazzi <marco@techie.com>
941;; * XEmacs compatibility: William J. Henney <will@astrosmo.unam.mx>
984e7bd9 942;; * Check `ps-paper-type': Sudhakar Frederick <sfrederi@asc.corp.mot.com>
12b88fff 943;;
857686a6
RS
944;; Thanks to Jacques Duthen <duthen@cegelec-red.fr> (Jack) for the 3.4 version
945;; I started from. [vinicius]
946;;
bcc0d457
RS
947;; Thanks to Jim Thompson <?@?> for the 2.8 version I started from.
948;; [jack]
949;;
12d89a2e
RS
950;; Thanks to Kevin Rodgers <kevinr@ihs.com> for adding support for
951;; color and the invisible property.
ef2cbb24 952;;
12d89a2e
RS
953;; Thanks to Avishai Yacobi, avishaiy@mcil.comm.mot.com, for writing
954;; the initial port to Emacs 19. His code is no longer part of
955;; ps-print, but his work is still appreciated.
ef2cbb24 956;;
12d89a2e
RS
957;; Thanks to Remi Houdaille and Michel Train, michel@metasoft.fdn.org,
958;; for adding underline support. Their code also is no longer part of
959;; ps-print, but their efforts are not forgotten.
960;;
961;; Thanks also to all of you who mailed code to add features to
962;; ps-print; although I didn't use your code, I still appreciate your
963;; sharing it with me.
964;;
965;; Thanks to all who mailed comments, encouragement, and criticism.
966;; Thanks also to all who responded to my survey; I had too many
967;; responses to reply to them all, but I greatly appreciate your
968;; interest.
969;;
970;; Jim
971;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
ef2cbb24
RS
972
973;;; Code:
974
090be653
RS
975(unless (featurep 'lisp-float-type)
976 (error "`ps-print' requires floating point support"))
ef2cbb24
RS
977
978;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
12d89a2e
RS
979;; User Variables:
980
bcc0d457
RS
981;;; Interface to the command system
982
e0af0d3e 983(defgroup ps-print nil
8bd22fcf 984 "PostScript generator for Emacs 19"
e0af0d3e
RS
985 :prefix "ps-"
986 :group 'wp)
987
988(defgroup ps-print-horizontal nil
989 "Horizontal page layout"
990 :prefix "ps-"
991 :tag "Horizontal"
992 :group 'ps-print)
993
994(defgroup ps-print-vertical nil
995 "Vertical page layout"
996 :prefix "ps-"
997 :tag "Vertical"
998 :group 'ps-print)
999
1000(defgroup ps-print-header nil
1001 "Headers layout"
1002 :prefix "ps-"
1003 :tag "Header"
1004 :group 'ps-print)
1005
1006(defgroup ps-print-font nil
1007 "Fonts customization"
1008 :prefix "ps-"
1009 :tag "Font"
1010 :group 'ps-print)
1011
1012(defgroup ps-print-color nil
1013 "Color customization"
1014 :prefix "ps-"
1015 :tag "Color"
1016 :group 'ps-print)
1017
1018(defgroup ps-print-face nil
1019 "Faces customization"
1020 :prefix "ps-"
1021 :tag "PS Faces"
1022 :group 'ps-print
1023 :group 'faces)
1024
1025
d3ab8dac 1026(defcustom ps-multibyte-buffer nil
024ced4d 1027 "*Specifies the multi-byte buffer handling.
d3ab8dac
KH
1028
1029Valid values are:
1030
1031 nil This is the value to use when you are printing
1032 buffer with only ASCII and Latin characters.
1033
1034 `non-latin-printer' This is the value to use when you have a japanese
1035 or korean PostScript printer and want to print
1036 buffer with ASCII, Latin-1, Japanese (JISX0208 and
1037 JISX0201-Kana) and Korean characters. At present,
1038 it was not tested the Korean characters printing.
1039 If you have a korean PostScript printer, please,
1040 test it.
1041
1042 `bdf-font' This is the value to use when you want to print
1043 buffer with BDF fonts. BDF fonts include both latin
1044 and non-latin fonts. BDF (Bitmap Distribution
1045 Format) is a format used for distributing X's font
1046 source file. BDF fonts are included in
1047 `intlfonts-1.1' which is a collection of X11 fonts
1048 for all characters supported by Emacs. In order to
1049 use this value, be sure to have installed
1050 `intlfonts-1.1' and set the variable
1051 `bdf-directory-list' appropriately (see bdf.el for
1052 documentation of this variable).
1053
1054 `bdf-font-except-latin' This is like `bdf-font' except that it is used
1055 PostScript default fonts to print ASCII and Latin-1
1056 characters. This is convenient when you want or
1057 need to use both latin and non-latin characters on
1058 the same buffer. See `ps-font-family',
1059 `ps-header-font-family' and `ps-font-info-database'.
1060
1061Any other value is treated as nil."
1062 :type '(choice (const non-latin-printer) (const bdf-font)
1063 (const bdf-font-except-latin) (other :tag "nil" nil))
1064 :group 'ps-print-font)
1065
1066(defcustom ps-print-prologue-header nil
1067 "*PostScript prologue header comments besides that ps-print generates.
1068
1069`ps-print-prologue-header' may be a string or a symbol function which
1070returns a string. Note that this string is inserted on PostScript prologue
1071header section which is used to define some document characteristic through
1072PostScript special comments, like \"%%Requirements: jog\\n\".
1073
1074ps-print always inserts the %%Requirements: comment, so if you need to insert
1075more requirements put them first in `ps-print-prologue-header' using the
1076\"%%+\" comment. For example, if you need to set numcopies to 3 and jog on
1077requirements and set %%LanguageLevel: to 2, do:
1078
1079(setq ps-print-prologue-header
1080 \"%%+ numcopies(3) jog\\n%%LanguageLevel: 2\\n\")
1081
1082The duplex requirement is inserted by ps-print (see `ps-spool-duplex').
1083
1084Do not forget to terminate the string with \"\\n\".
1085
1086For more information about PostScript document comments, see:
1087 PostScript Language Reference Manual (2nd edition)
1088 Adobe Systems Incorporated
1089 Appendix G: Document Structuring Conventions -- Version 3.0"
1090 :type '(choice string symbol (other :tag "nil" nil))
1091 :group 'ps-print)
1092
03820514
RS
1093(defcustom ps-printer-name printer-name
1094 "*The name of a local printer for printing PostScript files.
1095
1096On Unix-like systems, a string value should be a name understood by
1097lpr's -P option; otherwise the value should be nil.
1098
1099On MS-DOS and MS-Windows systems, if the value is a string, then it is
1100taken as the name of the device to which PostScript files are written.
1101By default it is the same as `printer-name'; typical non-default
1102settings would be \"LPT1\" to \"LPT3\" for parallel printers, or
1103\"COM1\" to \"COM4\" or \"AUX\" for serial printers, or
1104\"//hostname/printer\" for a shared network printer. You can also set
1105it to a name of a file, in which case the output gets appended to that
1106file. \(Note that `ps-print' package already has facilities for
1107printing to a file, so you might as well use them instead of changing
1108the setting of this variable.\) If you want to silently discard the
1109printed output, set this to \"NUL\".
1110
1111On DOS/Windows, if the value is anything but a string, PostScript files
1112will be piped to the program given by `ps-lpr-command', with switches
1113given by `ps-lpr-switches', which see."
1114 :type '(choice file (other :tag "Pipe to ps-lpr-command" pipe))
1115 :group 'ps-print)
1116
e0af0d3e
RS
1117(defcustom ps-lpr-command lpr-command
1118 "*The shell command for printing a PostScript file."
1119 :type 'string
1120 :group 'ps-print)
1121
1122(defcustom ps-lpr-switches lpr-switches
1123 "*A list of extra switches to pass to `ps-lpr-command'."
1124 :type '(repeat string)
1125 :group 'ps-print)
12d89a2e 1126
bcc0d457 1127;;; Page layout
12d89a2e 1128
bcc0d457
RS
1129;; All page dimensions are in PostScript points.
1130;; 1 inch == 2.54 cm == 72 points
1131;; 1 cm == (/ 1 2.54) inch == (/ 72 2.54) points
1132
1133;; Letter 8.5 inch x 11.0 inch
1134;; Legal 8.5 inch x 14.0 inch
1135;; A4 8.26 inch x 11.69 inch = 21.0 cm x 29.7 cm
1136
1137;; LetterSmall 7.68 inch x 10.16 inch
1138;; Tabloid 11.0 inch x 17.0 inch
1139;; Ledger 17.0 inch x 11.0 inch
1140;; Statement 5.5 inch x 8.5 inch
1141;; Executive 7.5 inch x 10.0 inch
1142;; A3 11.69 inch x 16.5 inch = 29.7 cm x 42.0 cm
1143;; A4Small 7.47 inch x 10.85 inch
1144;; B4 10.125 inch x 14.33 inch
1145;; B5 7.16 inch x 10.125 inch
1146
e0af0d3e 1147(defcustom ps-page-dimensions-database
bcc0d457
RS
1148 (list (list 'a4 (/ (* 72 21.0) 2.54) (/ (* 72 29.7) 2.54))
1149 (list 'a3 (/ (* 72 29.7) 2.54) (/ (* 72 42.0) 2.54))
1150 (list 'letter (* 72 8.5) (* 72 11.0))
1151 (list 'legal (* 72 8.5) (* 72 14.0))
1152 (list 'letter-small (* 72 7.68) (* 72 10.16))
1153 (list 'tabloid (* 72 11.0) (* 72 17.0))
1154 (list 'ledger (* 72 17.0) (* 72 11.0))
1155 (list 'statement (* 72 5.5) (* 72 8.5))
1156 (list 'executive (* 72 7.5) (* 72 10.0))
1157 (list 'a4small (* 72 7.47) (* 72 10.85))
1158 (list 'b4 (* 72 10.125) (* 72 14.33))
1159 (list 'b5 (* 72 7.16) (* 72 10.125)))
1160 "*List associating a symbolic paper type to its width and height.
e0af0d3e
RS
1161see `ps-paper-type'."
1162 :type '(repeat (list :tag "Paper Type"
1163 (symbol :tag "Name")
1164 (number :tag "Width")
1165 (number :tag "Height")))
1166 :group 'ps-print)
1167
857686a6 1168;;;###autoload
e0af0d3e 1169(defcustom ps-paper-type 'letter
bcc0d457 1170 "*Specifies the size of paper to format for.
090be653 1171Should be one of the paper types defined in `ps-page-dimensions-database', for
e0af0d3e
RS
1172example `letter', `legal' or `a4'."
1173 :type '(symbol :validate (lambda (wid)
87a16a06
RS
1174 (if (assq (widget-value wid)
1175 ps-page-dimensions-database)
e0af0d3e
RS
1176 nil
1177 (widget-put wid :error "Unknown paper size")
1178 wid)))
1179 :group 'ps-print)
1180
87a16a06 1181(defcustom ps-landscape-mode nil
e0af0d3e
RS
1182 "*Non-nil means print in landscape mode."
1183 :type 'boolean
1184 :group 'ps-print)
1185
857686a6
RS
1186(defcustom ps-print-control-characters 'control-8-bit
1187 "*Specifies the printable form for control and 8-bit characters.
6bdb808e 1188That is, instead of sending, for example, a ^D (\004) to printer,
915293a2 1189it is sent the string \"^D\".
6bdb808e 1190
857686a6 1191Valid values are:
6bdb808e 1192
984e7bd9 1193 `8-bit' This is the value to use when you want an ASCII encoding of
d3ab8dac
KH
1194 any control or non-ASCII character. Control characters are
1195 encoded as \"^D\", and non-ASCII characters have an
1196 octal encoding.
6bdb808e 1197
984e7bd9 1198 `control-8-bit' This is the value to use when you want an ASCII encoding of
d3ab8dac
KH
1199 any control character, whether it is 7 or 8-bit.
1200 European 8-bits accented characters are printed according
1201 the current font.
6bdb808e 1202
c82b4a75 1203 `control' Only ASCII control characters have an ASCII encoding.
d3ab8dac
KH
1204 European 8-bits accented characters are printed according
1205 the current font.
6bdb808e 1206
984e7bd9 1207 nil No ASCII encoding. Any character is printed according the
d3ab8dac 1208 current font.
6bdb808e 1209
857686a6 1210Any other value is treated as nil."
12b88fff 1211 :type '(choice (const 8-bit) (const control-8-bit)
ab2739aa 1212 (const control) (other :tag "nil" nil))
857686a6
RS
1213 :group 'ps-print)
1214
e0af0d3e
RS
1215(defcustom ps-number-of-columns (if ps-landscape-mode 2 1)
1216 "*Specifies the number of columns"
87a16a06
RS
1217 :type 'number
1218 :group 'ps-print)
1219
535efc38 1220(defcustom ps-zebra-stripes nil
87a16a06 1221 "*Non-nil means print zebra stripes.
06fb6aab 1222See also documentation for `ps-zebra-stripe-height'."
87a16a06
RS
1223 :type 'boolean
1224 :group 'ps-print)
1225
535efc38 1226(defcustom ps-zebra-stripe-height 3
87a16a06 1227 "*Number of zebra stripe lines.
06fb6aab 1228See also documentation for `ps-zebra-stripes'."
87a16a06
RS
1229 :type 'number
1230 :group 'ps-print)
1231
1232(defcustom ps-line-number nil
1233 "*Non-nil means print line number."
1234 :type 'boolean
1235 :group 'ps-print)
1236
1237(defcustom ps-print-background-image nil
1238 "*EPS image list to be printed on background.
1239
1240The elements are:
1241
1242 (FILENAME X Y XSCALE YSCALE ROTATION PAGES...)
1243
1244FILENAME is a file name which contains an EPS image or some PostScript
1245programming like EPS.
1246FILENAME is ignored, if it doesn't exist or is read protected.
1247
1248X and Y are relative positions on paper to put the image.
1249If X and Y are nil, the image is centralized on paper.
1250
1251XSCALE and YSCALE are scale factor to be applied to image before printing.
1252If XSCALE and YSCALE are nil, the original size is used.
1253
1254ROTATION is the image rotation angle; if nil, the default is 0.
1255
1256PAGES designates the page to print background image.
1257PAGES may be a number or a cons cell (FROM . TO) designating FROM page
1258to TO page.
1259If PAGES is nil, print background image on all pages.
1260
1261X, Y, XSCALE, YSCALE and ROTATION may be a floating point number,
1262an integer number or a string. If it is a string, the string should contain
1263PostScript programming that returns a float or integer value.
1264
1265For example, if you wish to print an EPS image on all pages do:
1266
1267 '((\"~/images/EPS-image.ps\"))"
35378a09
KH
1268 :type '(repeat (list file
1269 (choice :tag "X" number string (const nil))
1270 (choice :tag "Y" number string (const nil))
1271 (choice :tag "X Scale" number string (const nil))
1272 (choice :tag "Y Scale" number string (const nil))
1273 (choice :tag "Rotation" number string (const nil))
1274 (repeat :tag "Pages" :inline t
1275 (radio integer
1276 (cons :tag "Range"
1277 (integer :tag "From")
1278 (integer :tag "To"))))))
87a16a06
RS
1279 :group 'ps-print)
1280
1281(defcustom ps-print-background-text nil
1282 "*Text list to be printed on background.
1283
1284The elements are:
1285
1286 (STRING X Y FONT FONTSIZE GRAY ROTATION PAGES...)
1287
1288STRING is the text to be printed on background.
1289
1290X and Y are positions on paper to put the text.
1291If X and Y are nil, the text is positioned at lower left corner.
1292
1293FONT is a font name to be used on printing the text.
1294If nil, \"Times-Roman\" is used.
1295
1296FONTSIZE is font size to be used, if nil, 200 is used.
1297
1298GRAY is the text gray factor (should be very light like 0.8).
1299If nil, the default is 0.85.
1300
1301ROTATION is the text rotation angle; if nil, the angle is given by
1302the diagonal from lower left corner to upper right corner.
1303
1304PAGES designates the page to print background text.
1305PAGES may be a number or a cons cell (FROM . TO) designating FROM page
1306to TO page.
1307If PAGES is nil, print background text on all pages.
1308
1309X, Y, FONTSIZE, GRAY and ROTATION may be a floating point number,
1310an integer number or a string. If it is a string, the string should contain
1311PostScript programming that returns a float or integer value.
1312
1313For example, if you wish to print text \"Preliminary\" on all pages do:
1314
1315 '((\"Preliminary\"))"
35378a09
KH
1316 :type '(repeat (list string
1317 (choice :tag "X" number string (const nil))
1318 (choice :tag "Y" number string (const nil))
1319 (choice :tag "Font" string (const nil))
1320 (choice :tag "Fontsize" number string (const nil))
1321 (choice :tag "Gray" number string (const nil))
1322 (choice :tag "Rotation" number string (const nil))
1323 (repeat :tag "Pages" :inline t
1324 (radio integer
1325 (cons :tag "Range"
1326 (integer :tag "From")
1327 (integer :tag "To"))))))
e0af0d3e 1328 :group 'ps-print)
bcc0d457
RS
1329
1330;;; Horizontal layout
1331
1332;; ------------------------------------------
1333;; | | | | | | | |
1334;; | lm | text | ic | text | ic | text | rm |
1335;; | | | | | | | |
1336;; ------------------------------------------
1337
e0af0d3e
RS
1338(defcustom ps-left-margin (/ (* 72 2.0) 2.54) ; 2 cm
1339 "*Left margin in points (1/72 inch)."
1340 :type 'number
1341 :group 'ps-print-horizontal)
bcc0d457 1342
e0af0d3e
RS
1343(defcustom ps-right-margin (/ (* 72 2.0) 2.54) ; 2 cm
1344 "*Right margin in points (1/72 inch)."
1345 :type 'number
1346 :group 'ps-print-horizontal)
bcc0d457 1347
e0af0d3e
RS
1348(defcustom ps-inter-column (/ (* 72 2.0) 2.54) ; 2 cm
1349 "*Horizontal space between columns in points (1/72 inch)."
1350 :type 'number
1351 :group 'ps-print-horizontal)
bcc0d457
RS
1352
1353;;; Vertical layout
1354
1355;; |--------|
1356;; | tm |
1357;; |--------|
1358;; | header |
1359;; |--------|
1360;; | ho |
1361;; |--------|
1362;; | text |
1363;; |--------|
1364;; | bm |
1365;; |--------|
1366
e0af0d3e
RS
1367(defcustom ps-bottom-margin (/ (* 72 1.5) 2.54) ; 1.5 cm
1368 "*Bottom margin in points (1/72 inch)."
1369 :type 'number
1370 :group 'ps-print-vertical)
bcc0d457 1371
e0af0d3e
RS
1372(defcustom ps-top-margin (/ (* 72 1.5) 2.54) ; 1.5 cm
1373 "*Top margin in points (1/72 inch)."
1374 :type 'number
1375 :group 'ps-print-vertical)
bcc0d457 1376
e0af0d3e
RS
1377(defcustom ps-header-offset (/ (* 72 1.0) 2.54) ; 1.0 cm
1378 "*Vertical space in points (1/72 inch) between the main text and the header."
1379 :type 'number
1380 :group 'ps-print-vertical)
bcc0d457 1381
e0af0d3e 1382(defcustom ps-header-line-pad 0.15
bcc0d457 1383 "*Portion of a header title line height to insert between the header frame
e0af0d3e
RS
1384and the text it contains, both in the vertical and horizontal directions."
1385 :type 'number
1386 :group 'ps-print-vertical)
bcc0d457
RS
1387
1388;;; Header setup
12d89a2e 1389
e0af0d3e 1390(defcustom ps-print-header t
86c10ecb
RS
1391 "*Non-nil means print a header at the top of each page.
1392By default, the header displays the buffer name, page number, and, if
1393the buffer is visiting a file, the file's directory. Headers are
ae7f6761 1394customizable by changing variables `ps-left-header' and
e0af0d3e
RS
1395`ps-right-header'."
1396 :type 'boolean
1397 :group 'ps-print-header)
1398
12b88fff
RS
1399(defcustom ps-print-only-one-header nil
1400 "*Non-nil means print only one header at the top of each page.
1401This is useful when printing more than one column, so it is possible
1402to have only one header over all columns or one header per column.
1403See also `ps-print-header'."
1404 :type 'boolean
1405 :group 'ps-print-header)
1406
e0af0d3e
RS
1407(defcustom ps-print-header-frame t
1408 "*Non-nil means draw a gaudy frame around the header."
1409 :type 'boolean
1410 :group 'ps-print-header)
1411
1412(defcustom ps-header-lines 2
8bd22fcf 1413 "*Number of lines to display in page header, when generating PostScript."
e0af0d3e
RS
1414 :type 'integer
1415 :group 'ps-print-header)
bcc0d457
RS
1416(make-variable-buffer-local 'ps-header-lines)
1417
e0af0d3e 1418(defcustom ps-show-n-of-n t
00aa16af 1419 "*Non-nil means show page numbers as N/M, meaning page N of M.
8bd22fcf
KH
1420NOTE: page numbers are displayed as part of headers,
1421 see variable `ps-print-headers'."
e0af0d3e
RS
1422 :type 'boolean
1423 :group 'ps-print-header)
12d89a2e 1424
e0af0d3e 1425(defcustom ps-spool-duplex nil ; Not many people have duplex
bcc0d457
RS
1426 ; printers, so default to nil.
1427 "*Non-nil indicates spooling is for a two-sided printer.
1428For a duplex printer, the `ps-spool-*' commands will insert blank pages
1429as needed between print jobs so that the next buffer printed will
1430start on the right page. Also, if headers are turned on, the headers
1431will be reversed on duplex printers so that the page numbers fall to
e0af0d3e
RS
1432the left on even-numbered pages."
1433 :type 'boolean
1434 :group 'ps-print-header)
bcc0d457
RS
1435
1436;;; Fonts
1437
e0af0d3e 1438(defcustom ps-font-info-database
bcc0d457 1439 '((Courier ; the family key
12b88fff
RS
1440 (fonts (normal . "Courier")
1441 (bold . "Courier-Bold")
1442 (italic . "Courier-Oblique")
1443 (bold-italic . "Courier-BoldOblique"))
1444 (size . 10.0)
1445 (line-height . 10.55)
1446 (space-width . 6.0)
1447 (avg-char-width . 6.0))
bcc0d457 1448 (Helvetica ; the family key
12b88fff
RS
1449 (fonts (normal . "Helvetica")
1450 (bold . "Helvetica-Bold")
1451 (italic . "Helvetica-Oblique")
1452 (bold-italic . "Helvetica-BoldOblique"))
1453 (size . 10.0)
1454 (line-height . 11.56)
1455 (space-width . 2.78)
1456 (avg-char-width . 5.09243))
bcc0d457 1457 (Times
12b88fff
RS
1458 (fonts (normal . "Times-Roman")
1459 (bold . "Times-Bold")
1460 (italic . "Times-Italic")
1461 (bold-italic . "Times-BoldItalic"))
1462 (size . 10.0)
1463 (line-height . 11.0)
1464 (space-width . 2.5)
334cc3b7 1465 (avg-char-width . 4.71432))
bcc0d457 1466 (Palatino
12b88fff
RS
1467 (fonts (normal . "Palatino-Roman")
1468 (bold . "Palatino-Bold")
1469 (italic . "Palatino-Italic")
1470 (bold-italic . "Palatino-BoldItalic"))
1471 (size . 10.0)
1472 (line-height . 12.1)
1473 (space-width . 2.5)
1474 (avg-char-width . 5.08676))
bcc0d457 1475 (Helvetica-Narrow
12b88fff
RS
1476 (fonts (normal . "Helvetica-Narrow")
1477 (bold . "Helvetica-Narrow-Bold")
1478 (italic . "Helvetica-Narrow-Oblique")
1479 (bold-italic . "Helvetica-Narrow-BoldOblique"))
1480 (size . 10.0)
1481 (line-height . 11.56)
1482 (space-width . 2.2796)
1483 (avg-char-width . 4.17579))
bcc0d457 1484 (NewCenturySchlbk
12b88fff
RS
1485 (fonts (normal . "NewCenturySchlbk-Roman")
1486 (bold . "NewCenturySchlbk-Bold")
1487 (italic . "NewCenturySchlbk-Italic")
1488 (bold-italic . "NewCenturySchlbk-BoldItalic"))
1489 (size . 10.0)
334cc3b7 1490 (line-height . 12.15)
12b88fff
RS
1491 (space-width . 2.78)
1492 (avg-char-width . 5.31162))
bcc0d457
RS
1493 ;; got no bold for the next ones
1494 (AvantGarde-Book
12b88fff
RS
1495 (fonts (normal . "AvantGarde-Book")
1496 (italic . "AvantGarde-BookOblique"))
1497 (size . 10.0)
1498 (line-height . 11.77)
1499 (space-width . 2.77)
1500 (avg-char-width . 5.45189))
bcc0d457 1501 (AvantGarde-Demi
12b88fff
RS
1502 (fonts (normal . "AvantGarde-Demi")
1503 (italic . "AvantGarde-DemiOblique"))
1504 (size . 10.0)
1505 (line-height . 12.72)
1506 (space-width . 2.8)
1507 (avg-char-width . 5.51351))
bcc0d457 1508 (Bookman-Demi
12b88fff
RS
1509 (fonts (normal . "Bookman-Demi")
1510 (italic . "Bookman-DemiItalic"))
1511 (size . 10.0)
1512 (line-height . 11.77)
1513 (space-width . 3.4)
1514 (avg-char-width . 6.05946))
bcc0d457 1515 (Bookman-Light
12b88fff
RS
1516 (fonts (normal . "Bookman-Light")
1517 (italic . "Bookman-LightItalic"))
1518 (size . 10.0)
1519 (line-height . 11.79)
1520 (space-width . 3.2)
1521 (avg-char-width . 5.67027))
bcc0d457
RS
1522 ;; got no bold and no italic for the next ones
1523 (Symbol
12b88fff
RS
1524 (fonts (normal . "Symbol"))
1525 (size . 10.0)
1526 (line-height . 13.03)
1527 (space-width . 2.5)
1528 (avg-char-width . 3.24324))
bcc0d457 1529 (Zapf-Dingbats
12b88fff
RS
1530 (fonts (normal . "Zapf-Dingbats"))
1531 (size . 10.0)
1532 (line-height . 9.63)
1533 (space-width . 2.78)
1534 (avg-char-width . 2.78))
bcc0d457 1535 (Zapf-Chancery-MediumItalic
12b88fff
RS
1536 (fonts (normal . "Zapf-Chancery-MediumItalic"))
1537 (size . 10.0)
1538 (line-height . 11.45)
1539 (space-width . 2.2)
1540 (avg-char-width . 4.10811))
87a16a06 1541 )
bcc0d457
RS
1542 "*Font info database: font family (the key), name, bold, italic, bold-italic,
1543reference size, line height, space width, average character width.
1544To get the info for another specific font (say Helvetica), do the following:
1545- create a new buffer
1546- generate the PostScript image to a file (C-u M-x ps-print-buffer)
8bd22fcf 1547- open this file and delete the leading `%' (which is the PostScript
bcc0d457 1548 comment character) from the line
87a16a06 1549 `% 3 cm 20 cm moveto 10 /Courier ReportFontInfo showpage'
bcc0d457 1550 to get the line
87a16a06 1551 `3 cm 20 cm moveto 10 /Helvetica ReportFontInfo showpage'
bcc0d457 1552- add the values to `ps-font-info-database'.
e0af0d3e
RS
1553You can get all the fonts of YOUR printer using `ReportAllFontInfo'."
1554 :type '(repeat (list :tag "Font Definition"
12b88fff 1555 (symbol :tag "Font Family")
334cc3b7
AS
1556 (cons :format "%v"
1557 (const :format "" fonts)
1558 (repeat :tag "Faces"
1559 (cons (choice (const normal)
12b88fff
RS
1560 (const bold)
1561 (const italic)
1562 (const bold-italic)
1563 (symbol :tag "Face"))
1564 (string :tag "Font Name"))))
334cc3b7
AS
1565 (cons :format "%v"
1566 (const :format "" size)
12b88fff 1567 (number :tag "Reference Size"))
334cc3b7
AS
1568 (cons :format "%v"
1569 (const :format "" line-height)
12b88fff 1570 (number :tag "Line Height"))
334cc3b7
AS
1571 (cons :format "%v"
1572 (const :format "" space-width)
12b88fff 1573 (number :tag "Space Width"))
334cc3b7
AS
1574 (cons :format "%v"
1575 (const :format "" avg-char-width)
12b88fff 1576 (number :tag "Average Character Width"))))
e0af0d3e
RS
1577 :group 'ps-print-font)
1578
1579(defcustom ps-font-family 'Courier
d3ab8dac 1580 "*Font family name for ordinary text, when generating PostScript."
e0af0d3e
RS
1581 :type 'symbol
1582 :group 'ps-print-font)
1583
1584(defcustom ps-font-size (if ps-landscape-mode 7 8.5)
d3ab8dac 1585 "*Font size, in points, for ordinary text, when generating PostScript."
e0af0d3e
RS
1586 :type 'number
1587 :group 'ps-print-font)
1588
1589(defcustom ps-header-font-family 'Helvetica
d3ab8dac 1590 "*Font family name for text in the header, when generating PostScript."
e0af0d3e
RS
1591 :type 'symbol
1592 :group 'ps-print-font)
1593
1594(defcustom ps-header-font-size (if ps-landscape-mode 10 12)
d3ab8dac 1595 "*Font size, in points, for text in the header, when generating PostScript."
e0af0d3e
RS
1596 :type 'number
1597 :group 'ps-print-font)
1598
1599(defcustom ps-header-title-font-size (if ps-landscape-mode 12 14)
d3ab8dac 1600 "*Font size, in points, for the top line of text in header, in PostScript."
e0af0d3e
RS
1601 :type 'number
1602 :group 'ps-print-font)
bcc0d457
RS
1603
1604;;; Colors
1605
87a16a06
RS
1606;; Printing color requires x-color-values.
1607(defcustom ps-print-color-p (or (fboundp 'x-color-values) ; Emacs
857686a6
RS
1608 (fboundp 'color-instance-rgb-components))
1609 ; XEmacs
e0af0d3e
RS
1610 "*If non-nil, print the buffer's text in color."
1611 :type 'boolean
1612 :group 'ps-print-color)
12d89a2e 1613
e0af0d3e
RS
1614(defcustom ps-default-fg '(0.0 0.0 0.0)
1615 "*RGB values of the default foreground color. Defaults to black."
1616 :type '(list (number :tag "Red") (number :tag "Green") (number :tag "Blue"))
1617 :group 'ps-print-color)
12d89a2e 1618
e0af0d3e
RS
1619(defcustom ps-default-bg '(1.0 1.0 1.0)
1620 "*RGB values of the default background color. Defaults to white."
1621 :type '(list (number :tag "Red") (number :tag "Green") (number :tag "Blue"))
1622 :group 'ps-print-color)
12d89a2e 1623
e0af0d3e 1624(defcustom ps-auto-font-detect t
12d89a2e 1625 "*Non-nil means automatically detect bold/italic face attributes.
7f72c06f 1626If nil, we rely solely on the lists `ps-bold-faces', `ps-italic-faces',
e0af0d3e
RS
1627and `ps-underlined-faces'."
1628 :type 'boolean
1629 :group 'ps-print-font)
12d89a2e 1630
e0af0d3e 1631(defcustom ps-bold-faces
090be653
RS
1632 (unless ps-print-color-p
1633 '(font-lock-function-name-face
1634 font-lock-builtin-face
1635 font-lock-variable-name-face
1636 font-lock-keyword-face
1637 font-lock-warning-face))
86c10ecb 1638 "*A list of the \(non-bold\) faces that should be printed in bold font.
8bd22fcf 1639This applies to generating PostScript."
e0af0d3e
RS
1640 :type '(repeat face)
1641 :group 'ps-print-face)
12d89a2e 1642
e0af0d3e 1643(defcustom ps-italic-faces
090be653
RS
1644 (unless ps-print-color-p
1645 '(font-lock-variable-name-face
8bd22fcf 1646 font-lock-type-face
090be653
RS
1647 font-lock-string-face
1648 font-lock-comment-face
1649 font-lock-warning-face))
86c10ecb 1650 "*A list of the \(non-italic\) faces that should be printed in italic font.
8bd22fcf 1651This applies to generating PostScript."
e0af0d3e
RS
1652 :type '(repeat face)
1653 :group 'ps-print-face)
12d89a2e 1654
e0af0d3e 1655(defcustom ps-underlined-faces
090be653
RS
1656 (unless ps-print-color-p
1657 '(font-lock-function-name-face
883212ce 1658 font-lock-constant-face
090be653 1659 font-lock-warning-face))
86c10ecb 1660 "*A list of the \(non-underlined\) faces that should be printed underlined.
8bd22fcf 1661This applies to generating PostScript."
e0af0d3e
RS
1662 :type '(repeat face)
1663 :group 'ps-print-face)
12d89a2e 1664
e0af0d3e 1665(defcustom ps-left-header
12d89a2e 1666 (list 'ps-get-buffer-name 'ps-header-dirpart)
bcc0d457 1667 "*The items to display (each on a line) on the left part of the page header.
8bd22fcf 1668This applies to generating PostScript.
12d89a2e 1669
86c10ecb 1670The value should be a list of strings and symbols, each representing an
12d89a2e
RS
1671entry in the PostScript array HeaderLinesLeft.
1672
1673Strings are inserted unchanged into the array; those representing
1674PostScript string literals should be delimited with PostScript string
1675delimiters '(' and ')'.
1676
1677For symbols with bound functions, the function is called and should
1678return a string to be inserted into the array. For symbols with bound
1679values, the value should be a string to be inserted into the array.
1680In either case, function or variable, the string value has PostScript
e0af0d3e
RS
1681string delimiters added to it."
1682 :type '(repeat (choice string symbol))
a6c6e755 1683 :group 'ps-print-header)
12d89a2e
RS
1684(make-variable-buffer-local 'ps-left-header)
1685
e0af0d3e 1686(defcustom ps-right-header
090be653 1687 (list "/pagenumberstring load" 'time-stamp-mon-dd-yyyy 'time-stamp-hh:mm:ss)
bcc0d457 1688 "*The items to display (each on a line) on the right part of the page header.
8bd22fcf 1689This applies to generating PostScript.
12d89a2e 1690
86c10ecb 1691See the variable `ps-left-header' for a description of the format of
e0af0d3e
RS
1692this variable."
1693 :type '(repeat (choice string symbol))
a6c6e755 1694 :group 'ps-print-header)
12d89a2e 1695(make-variable-buffer-local 'ps-right-header)
ef2cbb24 1696
e0af0d3e
RS
1697(defcustom ps-razzle-dazzle t
1698 "*Non-nil means report progress while formatting buffer."
1699 :type 'boolean
1700 :group 'ps-print)
12d89a2e 1701
a18ed129 1702(defcustom ps-adobe-tag "%!PS-Adobe-3.0\n"
12d89a2e
RS
1703 "*Contains the header line identifying the output as PostScript.
1704By default, `ps-adobe-tag' contains the standard identifier. Some
a18ed129
RS
1705printers require slightly different versions of this line."
1706 :type 'string
1707 :group 'ps-print)
12d89a2e 1708
e0af0d3e 1709(defcustom ps-build-face-reference t
12d89a2e
RS
1710 "*Non-nil means build the reference face lists.
1711
d3ab8dac 1712ps-print sets this value to nil after it builds its internal reference
12d89a2e
RS
1713lists of bold and italic faces. By settings its value back to t, you
1714can force ps-print to rebuild the lists the next time you invoke one
86c10ecb 1715of the ...-with-faces commands.
12d89a2e
RS
1716
1717You should set this value back to t after you change the attributes of
1718any face, or create new faces. Most users shouldn't have to worry
e0af0d3e
RS
1719about its setting, though."
1720 :type 'boolean
1721 :group 'ps-print-face)
12d89a2e 1722
e0af0d3e 1723(defcustom ps-always-build-face-reference nil
12d89a2e
RS
1724 "*Non-nil means always rebuild the reference face lists.
1725
1726If this variable is non-nil, ps-print will rebuild its internal
1727reference lists of bold and italic faces *every* time one of the
496725ad 1728...-with-faces commands is called. Most users shouldn't need to set this
e0af0d3e
RS
1729variable."
1730 :type 'boolean
1731 :group 'ps-print-face)
ef2cbb24
RS
1732
1733;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
12d89a2e 1734;; User commands
ef2cbb24 1735
00aa16af 1736;;;###autoload
ef2cbb24 1737(defun ps-print-buffer (&optional filename)
12d89a2e 1738 "Generate and print a PostScript image of the buffer.
ef2cbb24 1739
5ac0cb14
RS
1740Interactively, when you use a prefix argument (C-u), the command
1741prompts the user for a file name, and saves the PostScript image
1742in that file instead of sending it to the printer.
ef2cbb24 1743
5ac0cb14 1744Noninteractively, the argument FILENAME is treated as follows: if it
ef2cbb24 1745is nil, send the image to the printer. If FILENAME is a string, save
79080fa0 1746the PostScript image in a file with that name."
00aa16af 1747 (interactive (list (ps-print-preprint current-prefix-arg)))
87a16a06 1748 (ps-print-without-faces (point-min) (point-max) filename))
ef2cbb24
RS
1749
1750
00aa16af 1751;;;###autoload
ef2cbb24 1752(defun ps-print-buffer-with-faces (&optional filename)
12d89a2e 1753 "Generate and print a PostScript image of the buffer.
12d89a2e 1754Like `ps-print-buffer', but includes font, color, and underline
107e7c70
KH
1755information in the generated image. This command works only if you
1756are using a window system, so it has a way to determine color values."
00aa16af 1757 (interactive (list (ps-print-preprint current-prefix-arg)))
87a16a06 1758 (ps-print-with-faces (point-min) (point-max) filename))
ef2cbb24 1759
ef2cbb24 1760
00aa16af 1761;;;###autoload
ef2cbb24 1762(defun ps-print-region (from to &optional filename)
12d89a2e 1763 "Generate and print a PostScript image of the region.
12d89a2e 1764Like `ps-print-buffer', but prints just the current region."
00aa16af 1765 (interactive (list (point) (mark) (ps-print-preprint current-prefix-arg)))
a18ed129 1766 (ps-print-without-faces from to filename t))
ef2cbb24 1767
ef2cbb24 1768
00aa16af 1769;;;###autoload
ef2cbb24 1770(defun ps-print-region-with-faces (from to &optional filename)
12d89a2e 1771 "Generate and print a PostScript image of the region.
12d89a2e 1772Like `ps-print-region', but includes font, color, and underline
107e7c70
KH
1773information in the generated image. This command works only if you
1774are using a window system, so it has a way to determine color values."
00aa16af 1775 (interactive (list (point) (mark) (ps-print-preprint current-prefix-arg)))
a18ed129 1776 (ps-print-with-faces from to filename t))
ef2cbb24 1777
ef2cbb24 1778
00aa16af 1779;;;###autoload
ef2cbb24 1780(defun ps-spool-buffer ()
12d89a2e 1781 "Generate and spool a PostScript image of the buffer.
12d89a2e
RS
1782Like `ps-print-buffer' except that the PostScript image is saved in a
1783local buffer to be sent to the printer later.
ef2cbb24 1784
12d89a2e 1785Use the command `ps-despool' to send the spooled images to the printer."
ef2cbb24 1786 (interactive)
87a16a06 1787 (ps-spool-without-faces (point-min) (point-max)))
ef2cbb24 1788
ef2cbb24 1789
00aa16af 1790;;;###autoload
ef2cbb24 1791(defun ps-spool-buffer-with-faces ()
12d89a2e 1792 "Generate and spool a PostScript image of the buffer.
12d89a2e 1793Like `ps-spool-buffer', but includes font, color, and underline
107e7c70 1794information in the generated image. This command works only if you
1cd7962f 1795are using a window system, so it has a way to determine color values.
ef2cbb24 1796
12d89a2e 1797Use the command `ps-despool' to send the spooled images to the printer."
ef2cbb24 1798 (interactive)
87a16a06 1799 (ps-spool-with-faces (point-min) (point-max)))
ef2cbb24 1800
ef2cbb24 1801
00aa16af 1802;;;###autoload
ef2cbb24 1803(defun ps-spool-region (from to)
12d89a2e 1804 "Generate a PostScript image of the region and spool locally.
12d89a2e 1805Like `ps-spool-buffer', but spools just the current region.
ef2cbb24 1806
12d89a2e 1807Use the command `ps-despool' to send the spooled images to the printer."
ef2cbb24 1808 (interactive "r")
a18ed129 1809 (ps-spool-without-faces from to t))
ef2cbb24 1810
ef2cbb24 1811
00aa16af 1812;;;###autoload
ef2cbb24 1813(defun ps-spool-region-with-faces (from to)
12d89a2e 1814 "Generate a PostScript image of the region and spool locally.
12d89a2e 1815Like `ps-spool-region', but includes font, color, and underline
107e7c70 1816information in the generated image. This command works only if you
1cd7962f 1817are using a window system, so it has a way to determine color values.
ef2cbb24 1818
12d89a2e 1819Use the command `ps-despool' to send the spooled images to the printer."
ef2cbb24 1820 (interactive "r")
a18ed129 1821 (ps-spool-with-faces from to t))
ef2cbb24 1822
00aa16af 1823;;;###autoload
ef2cbb24
RS
1824(defun ps-despool (&optional filename)
1825 "Send the spooled PostScript to the printer.
1826
79080fa0
RS
1827Interactively, when you use a prefix argument (C-u), the command
1828prompts the user for a file name, and saves the spooled PostScript
1829image in that file instead of sending it to the printer.
ef2cbb24
RS
1830
1831More specifically, the FILENAME argument is treated as follows: if it
1832is nil, send the image to the printer. If FILENAME is a string, save
79080fa0 1833the PostScript image in a file with that name."
00aa16af
RS
1834 (interactive (list (ps-print-preprint current-prefix-arg)))
1835 (ps-do-despool filename))
12d89a2e 1836
bcc0d457
RS
1837;;;###autoload
1838(defun ps-line-lengths ()
06fb6aab 1839 "Display the correspondence between a line length and a font size,
bcc0d457
RS
1840using the current ps-print setup.
1841Try: pr -t file | awk '{printf \"%3d %s\n\", length($0), $0}' | sort -r | head"
1842 (interactive)
1843 (ps-line-lengths-internal))
1844
1845;;;###autoload
1846(defun ps-nb-pages-buffer (nb-lines)
06fb6aab
RS
1847 "Display number of pages to print this buffer, for various font heights.
1848The table depends on the current ps-print setup."
bcc0d457
RS
1849 (interactive (list (count-lines (point-min) (point-max))))
1850 (ps-nb-pages nb-lines))
1851
1852;;;###autoload
1853(defun ps-nb-pages-region (nb-lines)
06fb6aab
RS
1854 "Display number of pages to print the region, for various font heights.
1855The table depends on the current ps-print setup."
bcc0d457
RS
1856 (interactive (list (count-lines (mark) (point))))
1857 (ps-nb-pages nb-lines))
1858
1859;;;###autoload
1860(defun ps-setup ()
496725ad 1861 "Return the current PostScript-generation setup."
a18ed129
RS
1862 (format
1863 "
1864\(setq ps-print-color-p %s
d3ab8dac
KH
1865 ps-lpr-command %S
1866 ps-lpr-switches %S
1867 ps-printer-name %S
bcc0d457 1868
d3ab8dac 1869 ps-paper-type %S
8bd22fcf
KH
1870 ps-landscape-mode %s
1871 ps-number-of-columns %s
bcc0d457 1872
8bd22fcf 1873 ps-zebra-stripes %s
01961237 1874 ps-zebra-stripe-height %s
8bd22fcf 1875 ps-line-number %s
a18ed129 1876
d3ab8dac
KH
1877 ps-print-control-characters %S
1878
1879 ps-print-background-image %S
857686a6 1880
d3ab8dac 1881 ps-print-background-text %S
a18ed129 1882
d3ab8dac 1883 ps-print-prologue-header %S
a18ed129 1884
d3ab8dac
KH
1885 ps-left-margin %s
1886 ps-right-margin %s
1887 ps-inter-column %s
1888 ps-bottom-margin %s
1889 ps-top-margin %s
1890 ps-header-offset %s
1891 ps-header-line-pad %s
1892 ps-print-header %s
1893 ps-print-only-one-header %s
1894 ps-print-header-frame %s
1895 ps-header-lines %s
1896 ps-show-n-of-n %s
1897 ps-spool-duplex %s
bcc0d457 1898
d3ab8dac
KH
1899 ps-multibyte-buffer %S
1900 ps-font-family %S
a18ed129 1901 ps-font-size %s
d3ab8dac 1902 ps-header-font-family %S
a18ed129
RS
1903 ps-header-font-size %s
1904 ps-header-title-font-size %s)
bcc0d457 1905"
a18ed129
RS
1906 ps-print-color-p
1907 ps-lpr-command
d3ab8dac
KH
1908 (ps-print-quote ps-lpr-switches)
1909 ps-printer-name
1910 (ps-print-quote ps-paper-type)
a18ed129
RS
1911 ps-landscape-mode
1912 ps-number-of-columns
01961237
RS
1913 ps-zebra-stripes
1914 ps-zebra-stripe-height
a18ed129 1915 ps-line-number
d3ab8dac
KH
1916 (ps-print-quote ps-print-control-characters)
1917 (ps-print-quote ps-print-background-image)
1918 (ps-print-quote ps-print-background-text)
1919 (ps-print-quote ps-print-prologue-header)
a18ed129
RS
1920 ps-left-margin
1921 ps-right-margin
1922 ps-inter-column
1923 ps-bottom-margin
1924 ps-top-margin
1925 ps-header-offset
1926 ps-header-line-pad
1927 ps-print-header
d3ab8dac 1928 ps-print-only-one-header
a18ed129
RS
1929 ps-print-header-frame
1930 ps-header-lines
1931 ps-show-n-of-n
1932 ps-spool-duplex
d3ab8dac
KH
1933 (ps-print-quote ps-multibyte-buffer)
1934 (ps-print-quote ps-font-family)
a18ed129 1935 ps-font-size
d3ab8dac 1936 (ps-print-quote ps-header-font-family)
a18ed129
RS
1937 ps-header-font-size
1938 ps-header-title-font-size))
bcc0d457 1939
12d89a2e
RS
1940;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1941;; Utility functions and variables:
1942
d3ab8dac
KH
1943(defun ps-print-quote (sym)
1944 (and sym
1945 (if (or (symbolp sym) (listp sym))
1946 (format "'%S" sym)
1947 sym)))
1948
6770a60f
RS
1949(defvar ps-print-emacs-type
1950 (cond ((string-match "XEmacs" emacs-version) 'xemacs)
1951 ((string-match "Lucid" emacs-version) 'lucid)
1952 ((string-match "Epoch" emacs-version) 'epoch)
1953 (t 'emacs)))
1954
1955(if (or (eq ps-print-emacs-type 'lucid)
1956 (eq ps-print-emacs-type 'xemacs))
043620f4
KH
1957 (if (< emacs-minor-version 12)
1958 (setq ps-print-color-p nil))
12d89a2e
RS
1959 (require 'faces)) ; face-font, face-underline-p,
1960 ; x-font-regexp
1961
857686a6
RS
1962;; Return t if the device (which can be changed during an emacs session)
1963;; can handle colors.
1964;; This is function is not yet implemented for GNU emacs.
e65df0a1
KH
1965(cond ((and (eq ps-print-emacs-type 'xemacs)
1966 (>= emacs-minor-version 12)) ; xemacs
1967 (defun ps-color-device ()
1968 (eq (device-class) 'color))
1969 )
1970
1971 (t ; emacs
1972 (defun ps-color-device ()
1973 t)
1974 ))
1975
857686a6 1976
12d89a2e
RS
1977(require 'time-stamp)
1978
bcc0d457
RS
1979(defvar ps-print-prologue-1
1980 "% ISOLatin1Encoding stolen from ps_init.ps in GhostScript 2.6.1.4:
12d89a2e 1981/ISOLatin1Encoding where { pop } {
bcc0d457
RS
1982% -- The ISO Latin-1 encoding vector isn't known, so define it.
1983% -- The first half is the same as the standard encoding,
1984% -- except for minus instead of hyphen at code 055.
12d89a2e
RS
1985/ISOLatin1Encoding
1986StandardEncoding 0 45 getinterval aload pop
1987 /minus
1988StandardEncoding 46 82 getinterval aload pop
1989%*** NOTE: the following are missing in the Adobe documentation,
1990%*** but appear in the displayed table:
1991%*** macron at 0225, dieresis at 0230, cedilla at 0233, space at 0240.
bcc0d457 1992% 0200 (128)
12d89a2e
RS
1993 /.notdef /.notdef /.notdef /.notdef /.notdef /.notdef /.notdef /.notdef
1994 /.notdef /.notdef /.notdef /.notdef /.notdef /.notdef /.notdef /.notdef
1995 /dotlessi /grave /acute /circumflex /tilde /macron /breve /dotaccent
1996 /dieresis /.notdef /ring /cedilla /.notdef /hungarumlaut /ogonek /caron
bcc0d457 1997% 0240 (160)
12d89a2e
RS
1998 /space /exclamdown /cent /sterling
1999 /currency /yen /brokenbar /section
2000 /dieresis /copyright /ordfeminine /guillemotleft
2001 /logicalnot /hyphen /registered /macron
2002 /degree /plusminus /twosuperior /threesuperior
2003 /acute /mu /paragraph /periodcentered
2004 /cedilla /onesuperior /ordmasculine /guillemotright
2005 /onequarter /onehalf /threequarters /questiondown
bcc0d457 2006% 0300 (192)
12d89a2e
RS
2007 /Agrave /Aacute /Acircumflex /Atilde
2008 /Adieresis /Aring /AE /Ccedilla
2009 /Egrave /Eacute /Ecircumflex /Edieresis
2010 /Igrave /Iacute /Icircumflex /Idieresis
2011 /Eth /Ntilde /Ograve /Oacute
2012 /Ocircumflex /Otilde /Odieresis /multiply
2013 /Oslash /Ugrave /Uacute /Ucircumflex
2014 /Udieresis /Yacute /Thorn /germandbls
bcc0d457 2015% 0340 (224)
12d89a2e
RS
2016 /agrave /aacute /acircumflex /atilde
2017 /adieresis /aring /ae /ccedilla
2018 /egrave /eacute /ecircumflex /edieresis
2019 /igrave /iacute /icircumflex /idieresis
2020 /eth /ntilde /ograve /oacute
2021 /ocircumflex /otilde /odieresis /divide
2022 /oslash /ugrave /uacute /ucircumflex
2023 /udieresis /yacute /thorn /ydieresis
2024256 packedarray def
2025} ifelse
2026
2027/reencodeFontISO { %def
2028 dup
87a16a06 2029 length 12 add dict % Make a new font (a new dict the same size
bcc0d457 2030 % as the old one) with room for our new symbols.
12d89a2e 2031
bcc0d457 2032 begin % Make the new font the current dictionary.
12d89a2e
RS
2033
2034
2035 { 1 index /FID ne
2036 { def } { pop pop } ifelse
bcc0d457
RS
2037 } forall % Copy each of the symbols from the old dictionary
2038 % to the new one except for the font ID.
12d89a2e 2039
12b88fff
RS
2040 currentdict /FontType get 0 ne {
2041 /Encoding ISOLatin1Encoding def % Override the encoding with
12d89a2e 2042 % the ISOLatin1 encoding.
12b88fff 2043 } if
12d89a2e
RS
2044
2045 % Use the font's bounding box to determine the ascent, descent,
2046 % and overall height; don't forget that these values have to be
2047 % transformed using the font's matrix.
bcc0d457
RS
2048
2049% ^ (x2 y2)
2050% | |
2051% | v
2052% | +----+ - -
2053% | | | ^
2054% | | | | Ascent (usually > 0)
2055% | | | |
2056% (0 0) -> +--+----+-------->
2057% | | |
2058% | | v Descent (usually < 0)
2059% (x1 y1) --> +----+ - -
2060
12b88fff 2061 currentdict /FontType get 0 ne {
e65df0a1 2062 /FontBBox load aload pop % -- x1 y1 x2 y2
12b88fff
RS
2063 FontMatrix transform /Ascent exch def pop
2064 FontMatrix transform /Descent exch def pop
2065 } {
2066 /PrimaryFont FDepVector 0 get def
2067 PrimaryFont /FontBBox get aload pop
2068 PrimaryFont /FontMatrix get transform /Ascent exch def pop
2069 PrimaryFont /FontMatrix get transform /Descent exch def pop
2070 } ifelse
2071
bcc0d457 2072 /FontHeight Ascent Descent sub def % use `sub' because descent < 0
12d89a2e 2073
bcc0d457 2074 % Define these in case they're not in the FontInfo
87a16a06
RS
2075 % (also, here they're easier to get to).
2076 /UnderlinePosition Descent 0.70 mul def
2077 /OverlinePosition Descent UnderlinePosition sub Ascent add def
2078 /StrikeoutPosition Ascent 0.30 mul def
e65df0a1
KH
2079 /LineThickness FontHeight 0.05 mul def
2080 /Xshadow FontHeight 0.08 mul def
2081 /Yshadow FontHeight -0.09 mul def
87a16a06
RS
2082 /SpaceBackground Descent neg UnderlinePosition add def
2083 /XBox Descent neg def
2084 /YBox LineThickness 0.7 mul def
12d89a2e 2085
bcc0d457
RS
2086 currentdict % Leave the new font on the stack
2087 end % Stop using the font as the current dictionary.
2088 definefont % Put the font into the font dictionary
2089 pop % Discard the returned font.
12d89a2e 2090} bind def
ef2cbb24 2091
bcc0d457 2092/DefFont { % Font definition
12d89a2e
RS
2093 findfont exch scalefont reencodeFontISO
2094} def
2095
bcc0d457 2096/F { % Font selection
12d89a2e 2097 findfont
87a16a06
RS
2098 dup /Ascent get /Ascent exch def
2099 dup /Descent get /Descent exch def
2100 dup /FontHeight get /FontHeight exch def
2101 dup /UnderlinePosition get /UnderlinePosition exch def
2102 dup /OverlinePosition get /OverlinePosition exch def
2103 dup /StrikeoutPosition get /StrikeoutPosition exch def
2104 dup /LineThickness get /LineThickness exch def
2105 dup /Xshadow get /Xshadow exch def
2106 dup /Yshadow get /Yshadow exch def
2107 dup /SpaceBackground get /SpaceBackground exch def
2108 dup /XBox get /XBox exch def
2109 dup /YBox get /YBox exch def
12d89a2e
RS
2110 setfont
2111} def
2112
2113/FG /setrgbcolor load def
2114
2115/bg false def
2116/BG {
2117 dup /bg exch def
87a16a06
RS
2118 {mark 4 1 roll ]}
2119 {[ 1.0 1.0 1.0 ]}
2120 ifelse
2121 /bgcolor exch def
12d89a2e
RS
2122} def
2123
bcc0d457
RS
2124% B width C
2125% +-----------+
2126% | Ascent (usually > 0)
2127% A + +
2128% | Descent (usually < 0)
2129% +-----------+
2130% E width D
2131
12d89a2e 2132/dobackground { % width --
bcc0d457 2133 currentpoint % -- width x y
12d89a2e
RS
2134 gsave
2135 newpath
bcc0d457
RS
2136 moveto % A (x y)
2137 0 Ascent rmoveto % B
2138 dup 0 rlineto % C
2139 0 Descent Ascent sub rlineto % D
2140 neg 0 rlineto % E
12d89a2e
RS
2141 closepath
2142 bgcolor aload pop setrgbcolor
2143 fill
2144 grestore
2145} def
2146
bcc0d457
RS
2147/eolbg { % dobackground until right margin
2148 PrintWidth % -- x-eol
2149 currentpoint pop % -- cur-x
2150 sub % -- width until eol
2151 dobackground
12d89a2e
RS
2152} def
2153
87a16a06 2154/PLN {PrintLineNumber {doLineNumber}if} def
12d89a2e
RS
2155
2156/SL { % Soft Linefeed
2157 bg { eolbg } if
bcc0d457 2158 0 currentpoint exch pop LineHeight sub moveto
12d89a2e
RS
2159} def
2160
87a16a06 2161/HL {SL PLN} def % Hard Linefeed
12d89a2e
RS
2162
2163% Some debug
2164/dcp { currentpoint exch 40 string cvs print (, ) print = } def
87a16a06 2165/dp { print 2 copy exch 40 string cvs print (, ) print = } def
12d89a2e
RS
2166
2167/W {
bcc0d457
RS
2168 ( ) stringwidth % Get the width of a space in the current font.
2169 pop % Discard the Y component.
2170 mul % Multiply the width of a space
2171 % by the number of spaces to plot
12d89a2e
RS
2172 bg { dup dobackground } if
2173 0 rmoveto
87a16a06
RS
2174} def
2175
2176/Effect 0 def
2177/EF {/Effect exch def} def
2178
2179% stack: string |- --
2180% effect: 1 - underline 2 - strikeout 4 - overline
2181% 8 - shadow 16 - box 32 - outline
2182/S {
2183 /xx currentpoint dup Descent add /yy exch def
2184 Ascent add /YY exch def def
2185 dup stringwidth pop xx add /XX exch def
2186 Effect 8 and 0 ne {
2187 /yy yy Yshadow add def
2188 /XX XX Xshadow add def
2189 } if
2190 bg {
2191 true
2192 Effect 16 and 0 ne
2193 {SpaceBackground doBox}
2194 {xx yy XX YY doRect}
2195 ifelse
2196 } if % background
2197 Effect 16 and 0 ne {false 0 doBox}if % box
2198 Effect 8 and 0 ne {dup doShadow}if % shadow
2199 Effect 32 and 0 ne
2200 {true doOutline} % outline
2201 {show} % normal text
2202 ifelse
2203 Effect 1 and 0 ne {UnderlinePosition Hline}if % underline
2204 Effect 2 and 0 ne {StrikeoutPosition Hline}if % strikeout
2205 Effect 4 and 0 ne {OverlinePosition Hline}if % overline
2206} bind def
2207
2208% stack: position |- --
2209/Hline {
2210 currentpoint exch pop add dup
2211 gsave
2212 newpath
2213 xx exch moveto
2214 XX exch lineto
2215 closepath
2216 LineThickness setlinewidth stroke
2217 grestore
2218} bind def
2219
2220% stack: fill-or-not delta |- --
2221/doBox {
2222 /dd exch def
2223 xx XBox sub dd sub yy YBox sub dd sub
2224 XX XBox add dd add YY YBox add dd add
2225 doRect
2226} bind def
2227
2228% stack: fill-or-not lower-x lower-y upper-x upper-y |- --
2229/doRect {
2230 /rYY exch def
2231 /rXX exch def
2232 /ryy exch def
2233 /rxx exch def
2234 gsave
2235 newpath
2236 rXX rYY moveto
2237 rxx rYY lineto
2238 rxx ryy lineto
2239 rXX ryy lineto
2240 closepath
2241 % top of stack: fill-or-not
2242 {FillBgColor}
2243 {LineThickness setlinewidth stroke}
2244 ifelse
2245 grestore
2246} bind def
2247
2248% stack: string |- --
2249/doShadow {
2250 gsave
2251 Xshadow Yshadow rmoveto
2252 false doOutline
2253 grestore
2254} bind def
2255
2256/st 1 string def
2257
2258% stack: string fill-or-not |- --
2259/doOutline {
2260 /-fillp- exch def
2261 /-ox- currentpoint /-oy- exch def def
2262 gsave
2263 LineThickness setlinewidth
2264 {
2265 st 0 3 -1 roll put
2266 st dup true charpath
2267 -fillp- {gsave FillBgColor grestore}if
2268 stroke stringwidth
2269 -oy- add /-oy- exch def
2270 -ox- add /-ox- exch def
2271 -ox- -oy- moveto
2272 } forall
2273 grestore
2274 -ox- -oy- moveto
2275} bind def
2276
2277% stack: --
2278/FillBgColor {bgcolor aload pop setrgbcolor fill} bind def
2279
2280/L0 6 /Times-Italic DefFont
2281
2282% stack: --
2283/doLineNumber {
f68af055
RS
2284 /LineNumber where
2285 {
2286 pop
2287 currentfont
2288 gsave
2289 0.0 0.0 0.0 setrgbcolor
2290 /L0 findfont setfont
2291 LineNumber Lines ge
2292 {(end )}
2293 {LineNumber 6 string cvs ( ) strcat}
2294 ifelse
2295 dup stringwidth pop neg 0 rmoveto
2296 show
2297 grestore
2298 setfont
2299 /LineNumber LineNumber 1 add def
2300 } if
87a16a06
RS
2301} def
2302
2303% stack: --
2304/printZebra {
2305 gsave
2306 0.985 setgray
857686a6 2307 /double-zebra ZebraHeight ZebraHeight add def
87a16a06
RS
2308 /yiter double-zebra LineHeight mul neg def
2309 /xiter PrintWidth InterColumn add def
2310 NumberOfColumns {LinesPerColumn doColumnZebra xiter 0 rmoveto}repeat
2311 grestore
2312} def
2313
2314% stack: lines-per-column |- --
2315/doColumnZebra {
2316 gsave
857686a6 2317 dup double-zebra idiv {ZebraHeight doZebra 0 yiter rmoveto}repeat
87a16a06 2318 double-zebra mod
857686a6 2319 dup 0 le {pop}{dup ZebraHeight gt {pop ZebraHeight}if doZebra}ifelse
87a16a06
RS
2320 grestore
2321} def
2322
2323% stack: zebra-height (in lines) |- --
2324/doZebra {
2325 /zh exch 0.05 sub LineHeight mul def
2326 gsave
2327 0 LineHeight 0.65 mul rmoveto
2328 PrintWidth 0 rlineto
2329 0 zh neg rlineto
2330 PrintWidth neg 0 rlineto
2331 0 zh rlineto
2332 fill
2333 grestore
2334} def
2335
2336% tx ty rotation xscale yscale xpos ypos BeginBackImage
2337/BeginBackImage {
2338 /-save-image- save def
2339 /showpage {}def
2340 translate
2341 scale
2342 rotate
2343 translate
2344} def
2345
2346/EndBackImage {
2347 -save-image- restore
2348} def
2349
2350% string fontsize fontname rotation gray xpos ypos ShowBackText
2351/ShowBackText {
2352 gsave
2353 translate
2354 setgray
2355 rotate
2356 findfont exch dup /-offset- exch -0.25 mul def scalefont setfont
2357 0 -offset- moveto
2358 /-saveLineThickness- LineThickness def
2359 /LineThickness 1 def
2360 false doOutline
2361 /LineThickness -saveLineThickness- def
2362 grestore
12d89a2e
RS
2363} def
2364
bcc0d457 2365/BeginDoc {
e65df0a1
KH
2366 % ---- Remember space width of the normal text font `f0'.
2367 /SpaceWidth /f0 findfont setfont ( ) stringwidth pop def
bcc0d457
RS
2368 % ---- save the state of the document (useful for ghostscript!)
2369 /docState save def
9bf0a587 2370 /JackGhostscript where {pop 1 27.7 29.7 div scale}if
f8c70761
AI
2371 % ---- [andrewi] set PageSize based on chosen dimensions
2372 /setpagedevice where {
2373 pop
2374 1 dict dup
9bf0a587
AI
2375 /PageSize [ PrintPageWidth LeftMargin add RightMargin add
2376 LandscapePageHeight ] put
f8c70761 2377 setpagedevice
9bf0a587
AI
2378 }{
2379 LandscapeMode {
2380 % ---- translate to bottom-right corner of Portrait page
2381 LandscapePageHeight 0 translate
2382 90 rotate
2383 }if
2384 }ifelse
bcc0d457
RS
2385 /ColumnWidth PrintWidth InterColumn add def
2386 % ---- translate to lower left corner of TEXT
2387 LeftMargin BottomMargin translate
2388 % ---- define where printing will start
2389 /f0 F % this installs Ascent
2390 /PrintStartY PrintHeight Ascent sub def
2391 /ColumnIndex 1 def
2392} def
2393
2394/EndDoc {
2395 % ---- on last page but not last column, spit out the page
2396 ColumnIndex 1 eq not { showpage } if
2397 % ---- restore the state of the document (useful for ghostscript!)
2398 docState restore
2399} def
2400
12d89a2e 2401/BeginDSCPage {
bcc0d457 2402 % ---- when 1st column, save the state of the page
a18ed129 2403 ColumnIndex 1 eq { /pageState save def } if
bcc0d457
RS
2404 % ---- save the state of the column
2405 /columnState save def
12d89a2e
RS
2406} def
2407
12b88fff
RS
2408/PrintHeaderWidth PrintOnlyOneHeader{PrintPageWidth}{PrintWidth}ifelse def
2409
12d89a2e 2410/BeginPage {
a18ed129
RS
2411 % ---- when 1st column, print all background effects
2412 ColumnIndex 1 eq {
2413 0 PrintStartY moveto % move to where printing will start
2414 Zebra {printZebra}if
2415 printGlobalBackground
2416 printLocalBackground
2417 } if
12d89a2e 2418 PrintHeader {
12b88fff
RS
2419 PrintOnlyOneHeader{ColumnIndex 1 eq}{true}ifelse {
2420 PrintHeaderFrame {HeaderFrame}if
2421 HeaderText
2422 } if
12d89a2e 2423 } if
bcc0d457 2424 0 PrintStartY moveto % move to where printing will start
87a16a06 2425 PLN
12d89a2e
RS
2426} def
2427
2428/EndPage {
2429 bg { eolbg } if
12d89a2e
RS
2430} def
2431
2432/EndDSCPage {
bcc0d457
RS
2433 ColumnIndex NumberOfColumns eq {
2434 % ---- on last column, spit out the page
2435 showpage
2436 % ---- restore the state of the page
2437 pageState restore
2438 /ColumnIndex 1 def
2439 } { % else
2440 % ---- restore the state of the current column
2441 columnState restore
2442 % ---- and translate to the next column
2443 ColumnWidth 0 translate
2444 /ColumnIndex ColumnIndex 1 add def
2445 } ifelse
12d89a2e
RS
2446} def
2447
bcc0d457 2448/SetHeaderLines { % nb-lines --
12d89a2e 2449 /HeaderLines exch def
bcc0d457
RS
2450 % ---- bottom up
2451 HeaderPad
2452 HeaderLines 1 sub HeaderLineHeight mul add
2453 HeaderTitleLineHeight add
2454 HeaderPad add
2455 /HeaderHeight exch def
12d89a2e
RS
2456} def
2457
bcc0d457
RS
2458% |---------|
2459% | tm |
2460% |---------|
2461% | header |
2462% |-+-------| <-- (x y)
2463% | ho |
2464% |---------|
2465% | text |
2466% |-+-------| <-- (0 0)
2467% | bm |
2468% |---------|
2469
2470/HeaderFrameStart { % -- x y
2471 0 PrintHeight HeaderOffset add
12d89a2e
RS
2472} def
2473
2474/HeaderFramePath {
12b88fff
RS
2475 PrintHeaderWidth 0 rlineto
2476 0 HeaderHeight rlineto
2477 PrintHeaderWidth neg 0 rlineto
2478 0 HeaderHeight neg rlineto
12d89a2e
RS
2479} def
2480
2481/HeaderFrame {
2482 gsave
2483 0.4 setlinewidth
bcc0d457 2484 % ---- fill a black rectangle (the shadow of the next one)
12d89a2e
RS
2485 HeaderFrameStart moveto
2486 1 -1 rmoveto
2487 HeaderFramePath
2488 0 setgray fill
bcc0d457 2489 % ---- do the next rectangle ...
12d89a2e
RS
2490 HeaderFrameStart moveto
2491 HeaderFramePath
bcc0d457
RS
2492 gsave 0.9 setgray fill grestore % filled with grey
2493 gsave 0 setgray stroke grestore % drawn with black
12d89a2e
RS
2494 grestore
2495} def
2496
2497/HeaderStart {
2498 HeaderFrameStart
bcc0d457
RS
2499 exch HeaderPad add exch % horizontal pad
2500 % ---- bottom up
2501 HeaderPad add % vertical pad
2502 HeaderDescent sub
2503 HeaderLineHeight HeaderLines 1 sub mul add
12d89a2e
RS
2504} def
2505
2506/strcat {
2507 dup length 3 -1 roll dup length dup 4 -1 roll add string dup
2508 0 5 -1 roll putinterval
2509 dup 4 2 roll exch putinterval
2510} def
2511
2512/pagenumberstring {
2513 PageNumber 32 string cvs
2514 ShowNofN {
2515 (/) strcat
2516 PageCount 32 string cvs strcat
2517 } if
2518} def
2519
2520/HeaderText {
2521 HeaderStart moveto
2522
bcc0d457
RS
2523 HeaderLinesRight HeaderLinesLeft % -- rightLines leftLines
2524
2525 % ---- hack: `PN 1 and' == `PN 2 modulo'
2526
2527 % ---- if duplex and even page number, then exchange left and right
12d89a2e
RS
2528 Duplex PageNumber 1 and 0 eq and { exch } if
2529
bcc0d457 2530 { % ---- process the left lines
12d89a2e
RS
2531 aload pop
2532 exch F
2533 gsave
2534 dup xcheck { exec } if
2535 show
2536 grestore
2537 0 HeaderLineHeight neg rmoveto
2538 } forall
2539
2540 HeaderStart moveto
2541
bcc0d457 2542 { % ---- process the right lines
12d89a2e
RS
2543 aload pop
2544 exch F
2545 gsave
2546 dup xcheck { exec } if
2547 dup stringwidth pop
12b88fff 2548 PrintHeaderWidth exch sub HeaderPad 2 mul sub 0 rmoveto
12d89a2e
RS
2549 show
2550 grestore
2551 0 HeaderLineHeight neg rmoveto
2552 } forall
2553} def
2554
2555/ReportFontInfo {
2556 2 copy
bcc0d457 2557 /t0 3 1 roll DefFont
12d89a2e 2558 /t0 F
00aa16af 2559 /lh FontHeight def
12d89a2e
RS
2560 /sw ( ) stringwidth pop def
2561 /aw (01234567890abcdefghijklmnopqrstuvwxyz) dup length exch
2562 stringwidth pop exch div def
bcc0d457 2563 /t1 12 /Helvetica-Oblique DefFont
12d89a2e 2564 /t1 F
12d89a2e
RS
2565 gsave
2566 (For ) show
2567 128 string cvs show
2568 ( ) show
2569 32 string cvs show
2570 ( point, the line height is ) show
2571 lh 32 string cvs show
2572 (, the space width is ) show
2573 sw 32 string cvs show
2574 (,) show
2575 grestore
00aa16af 2576 0 FontHeight neg rmoveto
bcc0d457
RS
2577 gsave
2578 (and a crude estimate of average character width is ) show
2579 aw 32 string cvs show
2580 (.) show
2581 grestore
2582 0 FontHeight neg rmoveto
2583} def
2584
2585/cm { % cm to point
2586 72 mul 2.54 div
2587} def
2588
2589/ReportAllFontInfo {
2590 FontDirectory
2591 { % key = font name value = font dictionary
2592 pop 10 exch ReportFontInfo
2593 } forall
12d89a2e
RS
2594} def
2595
bcc0d457
RS
2596% 3 cm 20 cm moveto 10 /Courier ReportFontInfo showpage
2597% 3 cm 20 cm moveto ReportAllFontInfo showpage
2598
2599")
2600
2601(defvar ps-print-prologue-2
2602 "
2603% ---- These lines must be kept together because...
2604
2605/h0 F
2606/HeaderTitleLineHeight FontHeight def
2607
2608/h1 F
2609/HeaderLineHeight FontHeight def
2610/HeaderDescent Descent def
2611
2612% ---- ...because `F' has a side-effect on `FontHeight' and `Descent'
2613
12d89a2e
RS
2614")
2615
2616;; Start Editing Here:
ef2cbb24 2617
12d89a2e
RS
2618(defvar ps-source-buffer nil)
2619(defvar ps-spool-buffer-name "*PostScript*")
2620(defvar ps-spool-buffer nil)
ef2cbb24 2621
12d89a2e
RS
2622(defvar ps-output-head nil)
2623(defvar ps-output-tail nil)
ef2cbb24 2624
7da17ab6 2625(defvar ps-page-postscript 0)
12d89a2e 2626(defvar ps-page-count 0)
87a16a06
RS
2627(defvar ps-showline-count 1)
2628
857686a6
RS
2629(defvar ps-control-or-escape-regexp nil)
2630
87a16a06
RS
2631(defvar ps-background-pages nil)
2632(defvar ps-background-all-pages nil)
2633(defvar ps-background-text-count 0)
2634(defvar ps-background-image-count 0)
ef2cbb24 2635
12d89a2e 2636(defvar ps-current-font 0)
d3ab8dac 2637(defvar ps-default-color (and ps-print-color-p ps-default-fg)) ; black
12d89a2e
RS
2638(defvar ps-current-color ps-default-color)
2639(defvar ps-current-bg nil)
2640
2641(defvar ps-razchunk 0)
2642
d3ab8dac 2643(defvar ps-color-p nil)
bcc0d457
RS
2644(defvar ps-color-format
2645 (if (eq ps-print-emacs-type 'emacs)
12d89a2e 2646
12b88fff
RS
2647 ;; Emacs understands the %f format; we'll use it to limit color RGB
2648 ;; values to three decimals to cut down some on the size of the
2649 ;; PostScript output.
2650 "%0.3f %0.3f %0.3f"
12d89a2e 2651
12b88fff 2652 ;; Lucid emacsen will have to make do with %s (princ) for floats.
bcc0d457 2653 "%s %s %s"))
12d89a2e
RS
2654
2655;; These values determine how much print-height to deduct when headers
2656;; are turned on. This is a pretty clumsy way of handling it, but
2657;; it'll do for now.
12d89a2e 2658
bcc0d457 2659(defvar ps-header-pad 0
496725ad
RS
2660 "Vertical and horizontal space between the header frame and the text.
2661This is in units of points (1/72 inch).")
12d89a2e 2662
bcc0d457 2663;; Define accessors to the dimensions list.
12d89a2e 2664
bcc0d457
RS
2665(defmacro ps-page-dimensions-get-width (dims) `(nth 0 ,dims))
2666(defmacro ps-page-dimensions-get-height (dims) `(nth 1 ,dims))
12d89a2e 2667
87a16a06 2668(defvar ps-landscape-page-height nil)
12d89a2e 2669
12d89a2e
RS
2670(defvar ps-print-width nil)
2671(defvar ps-print-height nil)
2672
8bd22fcf
KH
2673(defvar ps-height-remaining nil)
2674(defvar ps-width-remaining nil)
12d89a2e 2675
bcc0d457
RS
2676(defvar ps-print-color-scale nil)
2677
87a16a06
RS
2678\f
2679;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2680;; Internal Variables
2681
2682
2683(defvar ps-print-face-extension-alist nil
a18ed129 2684 "Alist of symbolic faces *WITH* extension features (box, outline, etc).
87a16a06
RS
2685An element of this list has the following form:
2686
2687 (FACE . [BITS FG BG])
2688
2689 FACE is a symbol denoting a face name
2690 BITS is a bit vector, where each bit correspond
2691 to a feature (bold, underline, etc)
2692 (see documentation for `ps-print-face-map-alist')
2693 FG foreground color (string or nil)
2694 BG background color (string or nil)
2695
a18ed129
RS
2696Don't change this list directly; instead,
2697use `ps-extend-face' and `ps-extend-face-list'.
2698See documentation for `ps-extend-face' for valid extension symbol.")
2699
2700
2701(defvar ps-print-face-alist nil
2702 "Alist of symbolic faces *WITHOUT* extension features (box, outline, etc).
2703
2704An element of this list has the same form as an element of
2705`ps-print-face-extension-alist'.
2706
2707Don't change this list directly; this list is used by `ps-face-attributes',
2708`ps-map-face' and `ps-build-reference-face-lists'.")
87a16a06
RS
2709
2710
2711(defconst ps-print-face-map-alist
2712 '((bold . 1)
2713 (italic . 2)
2714 (underline . 4)
2715 (strikeout . 8)
2716 (overline . 16)
2717 (shadow . 32)
2718 (box . 64)
2719 (outline . 128))
2720 "Alist of all features and the corresponding bit mask.
2721Each symbol correspond to one bit in a bit vector.")
2722
2723\f
2724;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
a18ed129 2725;; Remapping Faces
87a16a06
RS
2726
2727
2728;;;###autoload
2729(defun ps-extend-face-list (face-extension-list &optional merge-p)
2730 "Extend face in `ps-print-face-extension-alist'.
2731
a18ed129
RS
2732If optional MERGE-P is non-nil, extensions in FACE-EXTENSION-LIST are merged
2733with face extension in `ps-print-face-extension-alist'; otherwise, overrides.
87a16a06
RS
2734
2735The elements in FACE-EXTENSION-LIST is like those for `ps-extend-face'.
2736
2737See `ps-extend-face' for documentation."
2738 (while face-extension-list
2739 (ps-extend-face (car face-extension-list) merge-p)
2740 (setq face-extension-list (cdr face-extension-list))))
2741
2742
2743;;;###autoload
2744(defun ps-extend-face (face-extension &optional merge-p)
2745 "Extend face in `ps-print-face-extension-alist'.
2746
6bdb808e 2747If optional MERGE-P is non-nil, extensions in FACE-EXTENSION list are merged
a18ed129 2748with face extensions in `ps-print-face-extension-alist'; otherwise, overrides.
87a16a06
RS
2749
2750The elements of FACE-EXTENSION list have the form:
2751
2752 (FACE-NAME FOREGROUND BACKGROUND EXTENSION...)
2753
2754FACE-NAME is a face name symbol.
2755
2756FOREGROUND and BACKGROUND may be nil or a string that denotes the
2757foreground and background colors respectively.
2758
2759EXTENSION is one of the following symbols:
2760 bold - use bold font.
2761 italic - use italic font.
2762 underline - put a line under text.
2763 strikeout - like underline, but the line is in middle of text.
2764 overline - like underline, but the line is over the text.
2765 shadow - text will have a shadow.
2766 box - text will be surrounded by a box.
a18ed129 2767 outline - print characters as hollow outlines.
87a16a06
RS
2768
2769If EXTENSION is any other symbol, it is ignored."
2770 (let* ((face-name (nth 0 face-extension))
2771 (foreground (nth 1 face-extension))
2772 (background (nth 2 face-extension))
2773 (ps-face (cdr (assq face-name ps-print-face-extension-alist)))
2774 (face-vector (or ps-face (vector 0 nil nil)))
2775 (face-bit (ps-extension-bit face-extension)))
2776 ;; extend face
2777 (aset face-vector 0 (if merge-p
2778 (logior (aref face-vector 0) face-bit)
2779 face-bit))
2780 (and foreground (stringp foreground) (aset face-vector 1 foreground))
2781 (and background (stringp background) (aset face-vector 2 background))
2782 ;; if face does not exist, insert it
2783 (or ps-face
2784 (setq ps-print-face-extension-alist
2785 (cons (cons face-name face-vector)
2786 ps-print-face-extension-alist)))))
2787
2788
2789(defun ps-extension-bit (face-extension)
2790 (let ((face-bit 0))
2791 ;; map valid symbol extension to bit vector
2792 (setq face-extension (cdr (cdr face-extension)))
2793 (while (setq face-extension (cdr face-extension))
2794 (setq face-bit (logior face-bit
2795 (or (cdr (assq (car face-extension)
2796 ps-print-face-map-alist))
2797 0))))
2798 face-bit))
2799
2800\f
857686a6
RS
2801;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2802;; Adapted from font-lock:
2803;; Originally face attributes were specified via `font-lock-face-attributes'.
2804;; Users then changed the default face attributes by setting that variable.
2805;; However, we try and be back-compatible and respect its value if set except
2806;; for faces where M-x customize has been used to save changes for the face.
2807
2808(defun ps-font-lock-face-attributes ()
2809 (and (boundp 'font-lock-mode) (symbol-value 'font-lock-mode)
2810 (boundp 'font-lock-face-attributes)
2811 (let ((face-attributes font-lock-face-attributes))
2812 (while face-attributes
6bdb808e
RS
2813 (let* ((face-attribute
2814 (car (prog1 face-attributes
2815 (setq face-attributes (cdr face-attributes)))))
857686a6
RS
2816 (face (car face-attribute)))
2817 ;; Rustle up a `defface' SPEC from a
2818 ;; `font-lock-face-attributes' entry.
2819 (unless (get face 'saved-face)
2820 (let ((foreground (nth 1 face-attribute))
2821 (background (nth 2 face-attribute))
2822 (bold-p (nth 3 face-attribute))
2823 (italic-p (nth 4 face-attribute))
2824 (underline-p (nth 5 face-attribute))
2825 face-spec)
2826 (when foreground
2827 (setq face-spec (cons ':foreground
2828 (cons foreground face-spec))))
2829 (when background
2830 (setq face-spec (cons ':background
2831 (cons background face-spec))))
2832 (when bold-p
2833 (setq face-spec (append '(:bold t) face-spec)))
2834 (when italic-p
2835 (setq face-spec (append '(:italic t) face-spec)))
2836 (when underline-p
2837 (setq face-spec (append '(:underline t) face-spec)))
2838 (custom-declare-face face (list (list t face-spec)) nil)
2839 )))))))
2840
2841\f
87a16a06
RS
2842;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2843;; Internal functions and variables
2844
2845
12b88fff
RS
2846(make-local-hook 'ps-print-hook)
2847(make-local-hook 'ps-print-begin-page-hook)
2848(make-local-hook 'ps-print-begin-column-hook)
2849
2850
a18ed129 2851(defun ps-print-without-faces (from to &optional filename region-p)
857686a6 2852 (ps-spool-without-faces from to region-p)
87a16a06
RS
2853 (ps-do-despool filename))
2854
2855
a18ed129 2856(defun ps-spool-without-faces (from to &optional region-p)
12b88fff 2857 (run-hooks 'ps-print-hook)
a18ed129 2858 (ps-printing-region region-p)
87a16a06
RS
2859 (ps-generate (current-buffer) from to 'ps-generate-postscript))
2860
2861
a18ed129 2862(defun ps-print-with-faces (from to &optional filename region-p)
857686a6 2863 (ps-spool-with-faces from to region-p)
87a16a06
RS
2864 (ps-do-despool filename))
2865
2866
a18ed129 2867(defun ps-spool-with-faces (from to &optional region-p)
12b88fff 2868 (run-hooks 'ps-print-hook)
a18ed129 2869 (ps-printing-region region-p)
87a16a06
RS
2870 (ps-generate (current-buffer) from to 'ps-generate-postscript-with-faces))
2871
2872
a18ed129
RS
2873(defsubst ps-count-lines (from to)
2874 (+ (count-lines from to)
857686a6
RS
2875 (save-excursion
2876 (goto-char to)
2877 (if (= (current-column) 0) 1 0))))
87a16a06
RS
2878
2879
a18ed129 2880(defvar ps-printing-region nil
496725ad 2881 "Variable used to indicate if ps-print is printing a region.
a18ed129
RS
2882If non-nil, it is a cons, the car of which is the line number
2883where the region begins, and its cdr is the total number of lines
2884in the buffer. Formatting functions can use this information
2885to print the original line number (and not the number of lines printed),
2886and to indicate in the header that the printout is of a partial file.")
87a16a06
RS
2887
2888
a18ed129
RS
2889(defun ps-printing-region (region-p)
2890 (setq ps-printing-region
2891 (and region-p
2892 (cons (ps-count-lines (point-min) (region-beginning))
2893 (ps-count-lines (point-min) (point-max))))))
87a16a06
RS
2894
2895\f
12d89a2e
RS
2896;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2897;; Internal functions
2898
7ae35a2f 2899(defsubst ps-font-alist (font-sym)
12b88fff
RS
2900 (get font-sym 'fonts))
2901
2902(defun ps-font (font-sym font-type)
2903 "Font family name for text of `font-type', when generating PostScript."
7ae35a2f 2904 (let* ((font-list (ps-font-alist font-sym))
12b88fff 2905 (normal-font (cdr (assq 'normal font-list))))
6bdb808e
RS
2906 (while (and font-list (not (eq font-type (car (car font-list)))))
2907 (setq font-list (cdr font-list)))
2908 (or (cdr (car font-list)) normal-font)))
12b88fff
RS
2909
2910(defun ps-fonts (font-sym)
7ae35a2f 2911 (mapcar 'cdr (ps-font-alist font-sym)))
12b88fff
RS
2912
2913(defun ps-font-number (font-sym font-type)
7ae35a2f 2914 (or (ps-alist-position font-type (ps-font-alist font-sym))
12b88fff
RS
2915 0))
2916
2917(defsubst ps-line-height (font-sym)
2918 "The height of a line, for generating PostScript.
2919This is the value that ps-print uses to determine the height,
2920y-dimension, of the lines of text it has printed, and thus affects the
2921point at which page-breaks are placed.
2922The line-height is *not* the same as the point size of the font."
2923 (get font-sym 'line-height))
2924
2925(defsubst ps-title-line-height (font-sym)
2926 "The height of a `title' line, for generating PostScript.
2927This is the value that ps-print uses to determine the height,
2928y-dimension, of the lines of text it has printed, and thus affects the
2929point at which page-breaks are placed.
2930The title-line-height is *not* the same as the point size of the font."
2931 (get font-sym 'title-line-height))
2932
2933(defsubst ps-space-width (font-sym)
2934 "The width of a space character, for generating PostScript.
2935This value is used in expanding tab characters."
2936 (get font-sym 'space-width))
2937
2938(defsubst ps-avg-char-width (font-sym)
2939 "The average width, in points, of a character, for generating PostScript.
2940This is the value that ps-print uses to determine the length,
2941x-dimension, of the text it has printed, and thus affects the point at
2942which long lines wrap around."
2943 (get font-sym 'avg-char-width))
2944
e65df0a1
KH
2945\f
2946;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
024ced4d 2947;; For handling multi-byte characters -- Begin.
e65df0a1
KH
2948;;
2949;; The following comments apply only to this part (through the next ^L).
2950;; Author: Kenichi Handa <handa@etl.go.jp>
2951;; Maintainer: Kenichi Handa <handa@etl.go.jp>
2952
2953(eval-and-compile
d3ab8dac 2954 (if (not (string< mule-version "4.0"))
e65df0a1
KH
2955 (progn
2956 (defalias 'ps-mule-next-point '1+)
2957 (defalias 'ps-mule-chars-in-string 'length)
2958 (defalias 'ps-mule-string-char 'aref)
2959 (defsubst ps-mule-next-index (str i) (1+ i)))
2960 (defun set-buffer-multibyte (arg)
2961 (setq enable-multibyte-characters arg))
2962 (defun string-as-unibyte (arg) arg)
2963 (defun string-as-multibyte (arg) arg)
2964 (defun charset-after (&optional arg)
2965 (char-charset (char-after arg)))
2966 (defun ps-mule-next-point (arg)
2967 (save-excursion (goto-char arg) (forward-char 1) (point)))
2968 (defun ps-mule-chars-in-string (string)
d59cd657
RS
2969 (length string))
2970 (defalias 'ps-mule-string-char 'aref)
e65df0a1 2971 (defun ps-mule-next-index (str i)
d59cd657 2972 (1+ i)))
e65df0a1
KH
2973 )
2974
2975(defvar ps-mule-font-info-database
d3ab8dac
KH
2976 nil
2977 "Alist of charsets with the corresponding font information.
e65df0a1 2978Each element has the form:
d3ab8dac 2979
e65df0a1 2980 (CHARSET (FONT-TYPE FONT-SRC FONT-NAME ENCODING BYTES) ...)
d3ab8dac
KH
2981
2982Where
e65df0a1
KH
2983
2984CHARSET is a charset (symbol) for this font family,
2985
d3ab8dac 2986FONT-TYPE is a font type: normal, bold, italic, or bold-italic.
e65df0a1 2987
d3ab8dac 2988FONT-SRC is a font source: builtin, bdf, vflib, or nil.
e65df0a1
KH
2989
2990 If FONT-SRC is builtin, FONT-NAME is a buitin PostScript font name.
2991
d3ab8dac
KH
2992 If FONT-SRC is bdf, FONT-NAME is a BDF font file name. To use this font,
2993 the external library `bdf' is required.
e65df0a1 2994
d3ab8dac
KH
2995 If FONT-SRC is vflib, FONT-NAME is the name of a font that VFlib knows.
2996 To use this font, the external library `vflib' is required.
e65df0a1
KH
2997
2998 If FONT-SRC is nil, a proper ASCII font in the variable
d3ab8dac 2999 `ps-font-info-database' is used. This is useful for Latin-1 characters.
e65df0a1 3000
d3ab8dac
KH
3001ENCODING is a coding system to encode a string of characters of CHARSET into a
3002proper string matching an encoding of the specified font. ENCODING may be a
3003function that does this encoding. In this case, the function is called with
3004one argument, the string to encode, and it should return an encoded string.
e65df0a1 3005
d3ab8dac
KH
3006BYTES specifies how many bytes each character has in the encoded byte
3007sequence; it should be 1 or 2.
e65df0a1 3008
024ced4d 3009All multi-byte characters are printed by fonts specified in this database
d3ab8dac
KH
3010regardless of a font family of ASCII characters. The exception is Latin-1
3011characters which are printed by the same font as ASCII characters, thus obey
3012font family.
e65df0a1
KH
3013
3014See also the variable `ps-font-info-database'.")
3015
d3ab8dac
KH
3016(defconst ps-mule-font-info-database-latin
3017 '((latin-iso8859-1
3018 (normal nil nil iso-latin-1)))
3019 "Sample setting of `ps-mule-font-info-database' to use latin fonts.")
3020
e65df0a1
KH
3021(defconst ps-mule-font-info-database-ps
3022 '((katakana-jisx0201
3023 (normal builtin "Ryumin-Light.Katakana" ps-mule-encode-7bit 1)
3024 (bold builtin "GothicBBB-Medium.Katakana" ps-mule-encode-7bit 1)
3025 (bold-italic builtin "GothicBBB-Medium.Katakana" ps-mule-encode-7bit 1))
3026 (latin-jisx0201
3027 (normat builtin "Ryumin-Light.Hankaku" ps-mule-encode-7bit 1)
3028 (bold builtin "GothicBBB-Medium.Hankaku" ps-mule-encode-7bit 1))
3029 (japanese-jisx0208
3030 (normal builtin "Ryumin-Light-H" ps-mule-encode-7bit 2)
3031 (bold builtin "GothicBBB-Medium-H" ps-mule-encode-7bit 2))
3032 (korean-ksc5601
3033 (normal builtin "Batang-Medium-KSC-H" ps-mule-encode-7bit 2)
3034 (bold builtin " Gulim-Medium-KSC-H" ps-mule-encode-7bit 2))
3035 )
3036 "Sample setting of the `ps-mule-font-info-database' to use builtin PS font.
3037
3038Currently, data for Japanese and Korean PostScript printers are listed.")
3039
3040(defconst ps-mule-font-info-database-bdf
be415ea7
KH
3041 '((ascii
3042 (normal bdf "etl24-latin1.bdf" nil 1)
3043 (bold bdf "etl16b-latin1.bdf" iso-latin-1 1)
3044 (italic bdf "etl16i-latin1.bdf" iso-latin-1 1)
3045 (bold-italic bdf "etl16bi-latin1.bdf" iso-latin-1 1))
3046 (latin-iso8859-1
3047 (normal bdf "etl24-latin1.bdf" iso-latin-1 1)
3048 (bold bdf "etl16b-latin1.bdf" iso-latin-1 1)
3049 (italic bdf "etl16i-latin1.bdf" iso-latin-1 1)
3050 (bold-italic bdf "etl16bi-latin1.bdf" iso-latin-1 1))
e65df0a1
KH
3051 (latin-iso8859-2
3052 (normal bdf "etl24-latin2.bdf" iso-latin-2 1))
3053 (latin-iso8859-3
3054 (normal bdf "etl24-latin3.bdf" iso-latin-3 1))
3055 (latin-iso8859-4
3056 (normal bdf "etl24-latin4.bdf" iso-latin-4 1))
3057 (thai-tis620
3058 (normal bdf "thai-24.bdf" thai-tis620 1))
3059 (greek-iso8859-7
3060 (normal bdf "etl24-greek.bdf" greek-iso-8bit 1))
3061 ;; (arabic-iso8859-6 nil) ; not yet available
3062 (hebrew-iso8859-8
3063 (normal bdf "etl24-hebrew.bdf" hebrew-iso-8bit 1))
3064 (katakana-jisx0201
3065 (normal bdf "12x24rk.bdf" ps-mule-encode-8bit 1))
3066 (latin-jisx0201
3067 (normal bdf "12x24rk.bdf" ps-mule-encode-7bit 1))
3068 (cyrillic-iso8859-5
3069 (normal bdf "etl24-cyrillic.bdf" cyrillic-iso-8bit 1))
3070 (latin-iso8859-9
3071 (normal bdf "etl24-latin5.bdf" iso-latin-5 1))
3072 (japanese-jisx0208-1978
3073 (normal bdf "jiskan24.bdf" ps-mule-encode-7bit 2))
3074 (chinese-gb2312
3075 (normal bdf "gb24st.bdf" ps-mule-encode-7bit 2))
3076 (japanese-jisx0208
3077 (normal bdf "jiskan24.bdf" ps-mule-encode-7bit 2))
3078 (korean-ksc5601
3079 (normal bdf "hanglm24.bdf" ps-mule-encode-7bit 2))
3080 (japanese-jisx0212
3081 (normal bdf "jisksp40.bdf" ps-mule-encode-7bit 2))
3082 (chinese-cns11643-1
3083 (normal bdf "cns-1-40.bdf" ps-mule-encode-7bit 2))
3084 (chinese-cns11643-2
3085 (normal bdf "cns-2-40.bdf" ps-mule-encode-7bit 2))
3086 (chinese-big5-1
3087 (normal bdf "taipei24.bdf" chinese-big5 2))
3088 (chinese-big5-2
3089 (normal bdf "taipei24.bdf" chinese-big5 2))
3090 (chinese-sisheng
3091 (normal bdf "etl24-sisheng.bdf" ps-mule-encode-8bit 1))
3092 (ipa
3093 (normal bdf "etl24-ipa.bdf" ps-mule-encode-8bit 1))
3094 (vietnamese-viscii-lower
3095 (normal bdf "etl24-viscii.bdf" vietnamese-viscii 1))
3096 (vietnamese-viscii-upper
3097 (normal bdf "etl24-viscii.bdf" vietnamese-viscii 1))
3098 (arabic-digit
3099 (normal bdf "etl24-arabic0.bdf" ps-mule-encode-7bit 1))
3100 (arabic-1-column
3101 (normal bdf "etl24-arabic1.bdf" ps-mule-encode-7bit 1))
3102 ;; (ascii-right-to-left nil) ; not yet available
3103 (lao
3104 (normal bdf "mule-lao-24.bdf" lao 1))
3105 (arabic-2-column
3106 (normal bdf "etl24-arabic2.bdf" ps-mule-encode-7bit 1))
3107 (indian-is13194
3108 (normal bdf "mule-iscii-24.bdf" ps-mule-encode-7bit 1))
3109 (indian-1-column
3110 (normal bdf "mule-indian-1col-24.bdf" ps-mule-encode-7bit 2))
3111 (tibetan-1-column
3112 (normal bdf "mule-tibmdx-1col-24.bdf" ps-mule-encode-7bit 2))
3113 (ethiopic
3114 (normal bdf "ethiomx24f-uni.bdf" ps-mule-encode-ethiopic 2))
3115 (chinese-cns11643-3
3116 (normal bdf "cns-3-40.bdf" ps-mule-encode-7bit 2))
3117 (chinese-cns11643-4
3118 (normal bdf "cns-4-40.bdf" ps-mule-encode-7bit 2))
3119 (chinese-cns11643-5
3120 (normal bdf "cns-5-40.bdf" ps-mule-encode-7bit 2))
3121 (chinese-cns11643-6
3122 (normal bdf "cns-6-40.bdf" ps-mule-encode-7bit 2))
3123 (chinese-cns11643-7
3124 (normal bdf "cns-7-40.bdf" ps-mule-encode-7bit 2))
3125 (indian-2-column
3126 (normal bdf "mule-indian-24.bdf" ps-mule-encode-7bit 2))
3127 (tibetan
3128 (normal bdf "mule-tibmdx-24.bdf" ps-mule-encode-7bit 2)))
3129 "Sample setting of the `ps-mule-font-info-database' to use BDF fonts.
d3ab8dac
KH
3130BDF (Bitmap Distribution Format) is a format used for distributing X's font
3131source file.
3132
3133Current default value list for BDF fonts is included in `intlfonts-1.1' which is
3134a collection of X11 fonts for all characters supported by Emacs.
e65df0a1 3135
d3ab8dac
KH
3136Using this list as default value to `ps-mule-font-info-database', all characters
3137including ASCII and Latin-1 are printed by BDF fonts.
be415ea7 3138
d3ab8dac 3139See also `ps-mule-font-info-database-ps-bdf'.")
be415ea7
KH
3140
3141(defconst ps-mule-font-info-database-ps-bdf
d3ab8dac 3142 (cons (car ps-mule-font-info-database-latin)
be415ea7 3143 (cdr (cdr ps-mule-font-info-database-bdf)))
d3ab8dac 3144 "Sample setting of the `ps-mule-font-info-database' to use BDF fonts.
be415ea7 3145
d3ab8dac
KH
3146Current default value list for BDF fonts is included in `intlfonts-1.1' which is
3147a collection of X11 fonts for all characters supported by Emacs.
be415ea7 3148
d3ab8dac
KH
3149Using this list as default value to `ps-mule-font-info-database', all characters
3150except ASCII and Latin-1 characters are printed by BDF fonts. ASCII and Latin-1
3151characters are printed by PostScript font specified by `ps-font-family' and
3152`ps-header-font-family'.
be415ea7
KH
3153
3154See also `ps-mule-font-info-database-bdf'.")
e65df0a1
KH
3155
3156;; Two typical encoding functions for PostScript fonts.
3157
3158(defun ps-mule-encode-7bit (string)
d3ab8dac 3159 (ps-mule-encode-bit string 0))
e65df0a1
KH
3160
3161(defun ps-mule-encode-8bit (string)
d3ab8dac
KH
3162 (ps-mule-encode-bit string 128))
3163
3164(defun ps-mule-encode-bit (string delta)
3165 (let* ((dim (charset-dimension (char-charset (ps-mule-string-char string 0))))
e65df0a1
KH
3166 (len (* (ps-mule-chars-in-string string) dim))
3167 (str (make-string len 0))
d3ab8dac
KH
3168 (i 0)
3169 (j 0))
e65df0a1
KH
3170 (if (= dim 1)
3171 (while (< j len)
3172 (aset str j
d3ab8dac 3173 (+ (nth 1 (split-char (ps-mule-string-char string i))) delta))
e65df0a1
KH
3174 (setq i (ps-mule-next-index string i)
3175 j (1+ j)))
3176 (while (< j len)
3177 (let ((split (split-char (ps-mule-string-char string i))))
d3ab8dac
KH
3178 (aset str j (+ (nth 1 split) delta))
3179 (aset str (1+ j) (+ (nth 2 split) delta))
e65df0a1
KH
3180 (setq i (ps-mule-next-index string i)
3181 j (+ j 2)))))
3182 str))
3183
3184;; Special encoding function for Ethiopic.
3185(define-ccl-program ccl-encode-ethio-unicode
3186 `(1
be415ea7
KH
3187 ((read r2)
3188 (loop
3189 (if (r2 == ,leading-code-private-22)
3190 ((read r0)
3191 (if (r0 == ,(charset-id 'ethiopic))
3192 ((read r1 r2)
3193 (r1 &= 127) (r2 &= 127)
3194 (call ccl-encode-ethio-font)
3195 (write r1)
3196 (write-read-repeat r2))
3197 ((write r2 r0)
3198 (repeat))))
3199 (write-read-repeat r2))))))
e65df0a1
KH
3200
3201(defun ps-mule-encode-ethiopic (string)
3202 (ccl-execute-on-string (symbol-value 'ccl-encode-ethio-unicode)
3203 (make-vector 9 nil)
3204 string))
3205
3206;; A charset which we are now processing.
3207(defvar ps-mule-current-charset nil)
3208
3209(defun ps-mule-get-font-spec (charset font-type)
d3ab8dac
KH
3210 "Return FONT-SPEC for printing characters CHARSET with FONT-TYPE.
3211FONT-SPEC is a list that has the form:
3212
3213 (FONT-SRC FONT-NAME ENCODING BYTES)
3214
3215FONT-SPEC is extracted from `ps-mule-font-info-database'.
3216
3217See the documentation of `ps-mule-font-info-database' for the meaning of each
3218element of the list."
e65df0a1 3219 (let ((slot (cdr (assq charset ps-mule-font-info-database))))
d3ab8dac
KH
3220 (and slot
3221 (cdr (or (assq font-type slot)
3222 (and (eq font-type 'bold-italic)
3223 (or (assq 'bold slot) (assq 'italic slot)))
3224 (assq 'normal slot))))))
e65df0a1
KH
3225
3226;; Functions to access each element of FONT-SPEC.
3227(defsubst ps-mule-font-spec-src (font-spec) (car font-spec))
3228(defsubst ps-mule-font-spec-name (font-spec) (nth 1 font-spec))
3229(defsubst ps-mule-font-spec-encoding (font-spec) (nth 2 font-spec))
3230(defsubst ps-mule-font-spec-bytes (font-spec) (nth 3 font-spec))
3231
3232(defsubst ps-mule-printable-p (charset)
3233 "Non-nil if characters in CHARSET is printable."
3234 (ps-mule-get-font-spec charset 'normal))
3235
3236(defconst ps-mule-external-libraries
3237 '((builtin nil
3238 nil nil nil)
3239 (bdf nil
3240 bdf-generate-prologue bdf-generate-font bdf-generate-glyphs)
3241 (pcf nil
3242 pcf-generate-prologue pcf-generate-font pcf-generate-glyphs)
3243 (vflib nil
3244 vflib-generate-prologue vflib-generate-font vflib-generate-glyphs))
3245 "Alist of information of external libraries to support PostScript printing.
3246Each element has the form:
d3ab8dac 3247
e65df0a1
KH
3248 (FONT-SRC INITIALIZED-P PROLOGUE-FUNC FONT-FUNC GLYPHS-FUNC)
3249
d3ab8dac
KH
3250FONT-SRC is the font source: builtin, bdf, pcf, or vflib. Except for `builtin',
3251libraries must have the same name as indicated by FONT-SRC. Currently, we only
3252have the `bdf' library.
e65df0a1 3253
d3ab8dac 3254INITIALIZED-P indicates if this library is initialized or not.
e65df0a1 3255
d3ab8dac
KH
3256PROLOGUE-FUNC is a function to generate PostScript code which define several
3257PostScript procedures that will be called by FONT-FUNC and GLYPHS-FUNC. It is
3258called with no argument, and should return a list of strings.
e65df0a1 3259
d3ab8dac
KH
3260FONT-FUNC is a function to generate PostScript code which define a new font. It
3261is called with one argument FONT-SPEC, and should return a list of strings.
e65df0a1 3262
d3ab8dac
KH
3263GLYPHS-FUNC is a function to generate PostScript code which define glyphs of
3264characters. It is called with three arguments FONT-SPEC, CODE-LIST, and BYTES,
3265and should return a list of strings.")
e65df0a1
KH
3266
3267(defun ps-mule-init-external-library (font-spec)
d3ab8dac
KH
3268 "Initialize external library specified by FONT-SPEC for PostScript printing.
3269See the documentation of `ps-mule-get-font-spec' for FONT-SPEC's meaning."
e65df0a1
KH
3270 (let* ((font-src (ps-mule-font-spec-src font-spec))
3271 (slot (assq font-src ps-mule-external-libraries)))
3272 (or (not font-src)
3273 (nth 1 slot)
3274 (let ((func (nth 2 slot)))
3275 (if func
3276 (progn
3277 (or (featurep font-src) (require font-src))
3278 (ps-output-prologue (funcall func))))
3279 (setcar (cdr slot) t)))))
3280
3281;; Cached glyph information of fonts, alist of:
3282;; (FONT-NAME ((FONT-TYPE-NUMBER . SCALED-FONT-NAME) ...)
3283;; cache CODE0 CODE1 ...)
3284(defvar ps-mule-font-cache nil)
3285
3286(defun ps-mule-generate-font (font-spec charset)
3287 "Generate PostScript codes to define a new font in FONT-SPEC for CHARSET."
3288 (let* ((font-cache (assoc (ps-mule-font-spec-name font-spec)
3289 ps-mule-font-cache))
3290 (font-src (ps-mule-font-spec-src font-spec))
3291 (font-name (ps-mule-font-spec-name font-spec))
3292 (func (nth 3 (assq font-src ps-mule-external-libraries)))
3293 (scaled-font-name
3294 (if (eq charset 'ascii)
3295 (format "f%d" ps-current-font)
3296 (format "f%02x-%d"
3297 (charset-id charset) ps-current-font))))
d3ab8dac
KH
3298 (and func (not font-cache)
3299 (ps-output-prologue (funcall func charset font-spec)))
e65df0a1
KH
3300 (ps-output-prologue
3301 (list (format "/%s %f /%s Def%sFontMule\n"
3302 scaled-font-name ps-font-size font-name
3303 (if (eq ps-mule-current-charset 'ascii) "Ascii" ""))))
3304 (if font-cache
3305 (setcar (cdr font-cache)
3306 (cons (cons ps-current-font scaled-font-name)
3307 (nth 1 font-cache)))
3308 (setq font-cache (list font-name
3309 (list (cons ps-current-font scaled-font-name))
d3ab8dac
KH
3310 'cache)
3311 ps-mule-font-cache (cons font-cache ps-mule-font-cache)))
e65df0a1
KH
3312 font-cache))
3313
3314(defun ps-mule-generate-glyphs (font-spec code-list)
3315 "Generate PostScript codes which generate glyphs for CODE-LIST of FONT-SPEC."
3316 (let* ((font-src (ps-mule-font-spec-src font-spec))
3317 (func (nth 4 (assq font-src ps-mule-external-libraries))))
d3ab8dac
KH
3318 (and func
3319 (ps-output-prologue
3320 (funcall func font-spec code-list
3321 (ps-mule-font-spec-bytes font-spec))))))
e65df0a1
KH
3322
3323(defvar ps-last-font nil)
3324
d3ab8dac
KH
3325(defun ps-mule-prepare-font (font-spec string charset &optional no-setfont)
3326 "Generate PostScript codes to print STRING of CHARSET by font FONT-SPEC.
3327
3328The generated code is inserted on prologue part except the code that sets the
3329current font (using PostScript procedure `FM').
3330
3331If optional arg NO-SETFONT is non-nil, don't generate the code for setting the
3332current font."
e65df0a1
KH
3333 (let ((font-cache (assoc (ps-mule-font-spec-name font-spec)
3334 ps-mule-font-cache)))
3335 (or (and font-cache (assq ps-current-font (nth 1 font-cache)))
3336 (setq font-cache (ps-mule-generate-font font-spec charset)))
3337 (or no-setfont
3338 (let ((new-font (cdr (assq ps-current-font (nth 1 font-cache)))))
3339 (or (equal new-font ps-last-font)
3340 (progn
3341 (ps-output (format "/%s FM\n" new-font))
3342 (setq ps-last-font new-font)))))
3343 (if (nth 4 (assq (ps-mule-font-spec-src font-spec)
3344 ps-mule-external-libraries))
3345 ;; We have to generate PostScript codes which define glyphs.
3346 (let* ((cached-codes (nthcdr 2 font-cache))
3347 (newcodes nil)
3348 (bytes (ps-mule-font-spec-bytes font-spec))
3349 (len (length string))
3350 (i 0)
3351 code)
3352 (while (< i len)
d3ab8dac
KH
3353 (setq code (if (= bytes 1)
3354 (aref string i)
3355 (+ (* (aref string i) 256) (aref string (1+ i)))))
e65df0a1
KH
3356 (or (memq code cached-codes)
3357 (progn
3358 (setq newcodes (cons code newcodes))
3359 (setcdr cached-codes (cons code (cdr cached-codes)))))
3360 (setq i (+ i bytes)))
d3ab8dac
KH
3361 (and newcodes
3362 (ps-mule-generate-glyphs font-spec newcodes))))))
e65df0a1 3363
024ced4d
KH
3364;; List of charsets of multi-byte characters in a text being printed.
3365;; If the text doesn't contain any multi-byte characters (i.e. only
e65df0a1
KH
3366;; ASCII), the value is nil.
3367(defvar ps-mule-charset-list nil)
3368
e65df0a1
KH
3369(defvar ps-mule-prologue-generated nil)
3370
d3ab8dac 3371;; This is a PostScript code inserted in the header of generated PostScript.
e65df0a1
KH
3372(defconst ps-mule-prologue
3373 "%%%% Start of Mule Section
3374
d3ab8dac 3375%% Working dictionary for general use.
e65df0a1
KH
3376/MuleDict 10 dict def
3377
3378%% Define already scaled font for non-ASCII character sets.
3379/DefFontMule { % fontname size basefont |- --
3380 findfont exch scalefont definefont pop
3381} bind def
3382
3383%% Define already scaled font for ASCII character sets.
3384/DefAsciiFontMule { % fontname size basefont |-
3385 MuleDict begin
3386 findfont dup /Encoding get /ISOLatin1Encoding exch def
3387 exch scalefont reencodeFontISO
3388 end
3389} def
3390
3391%% Set the specified non-ASCII font to use. It doesn't install
3392%% Ascent, etc.
3393/FM { % fontname |- --
3394 findfont setfont
3395} bind def
3396
3397%% Show vacant box for characters which don't have appropriate font.
3398/SB { % count column |- --
3399 SpaceWidth mul /w exch def
3400 1 exch 1 exch { %for
3401 pop
3402 gsave
3403 0 setlinewidth
3404 0 Descent rmoveto w 0 rlineto
3405 0 LineHeight rlineto w neg 0 rlineto closepath stroke
3406 grestore
3407 w 0 rmoveto
3408 } for
3409} bind def
3410
3411%% Flag to tell if we are now handling a composite character. This is
3412%% defined here because both composite character handler and bitmap font
3413%% handler require it.
3414/Cmpchar false def
3415
3416%%%% End of Mule Section
3417
3418"
024ced4d 3419 "PostScript code for printing multi-byte characters.")
e65df0a1
KH
3420
3421(defun ps-mule-skip-same-charset (charset)
3422 "Skip characters of CHARSET following the current point."
d3ab8dac
KH
3423 (while (eq (charset-after) charset)
3424 (forward-char 1)))
e65df0a1
KH
3425
3426(defun ps-mule-find-wrappoint (from to char-width)
d3ab8dac
KH
3427 "Find the longest sequence which is printable in the current line.
3428
3429The search starts at FROM and goes until TO. It is assumed that all characters
3430between FROM and TO belong to a charset in `ps-mule-current-charset'.
e65df0a1 3431
d3ab8dac 3432CHAR-WIDTH is the average width of ASCII characters in the current font.
e65df0a1 3433
d3ab8dac 3434Returns the value:
e65df0a1 3435
d3ab8dac
KH
3436 (ENDPOS . RUN-WIDTH)
3437
3438Where ENDPOS is the end position of the sequence and RUN-WIDTH is the width of
3439the sequence."
e65df0a1
KH
3440 (let (run-width)
3441 (if (eq ps-mule-current-charset 'composition)
3442 ;; We must draw one char by one.
3443 (let ((ch (char-after from)))
3444 (setq run-width (* (char-width ch) char-width))
3445 (if (> run-width ps-width-remaining)
3446 (setq run-width ps-width-remaining)
3447 (setq from (ps-mule-next-point from))))
3448 ;; We assume that all characters in this range have the same width.
3449 (let ((width (charset-width ps-mule-current-charset)))
3450 (setq run-width (* (- to from) char-width width))
3451 (if (> run-width ps-width-remaining)
3452 (setq from (min
3453 (+ from (truncate (/ ps-width-remaining char-width)))
3454 to)
3455 run-width ps-width-remaining)
3456 (setq from to))))
3457 (cons from run-width)))
3458
3459(defun ps-mule-plot-string (from to &optional bg-color)
3460 "Generate PostScript code for ploting characters in the region FROM and TO.
d3ab8dac
KH
3461
3462It is assumed that all characters in this region belong to a charset in
3463`ps-mule-current-charset'.
3464
3465Optional argument BG-COLOR specifies background color.
3466
3467Returns the value:
3468
3469 (ENDPOS . RUN-WIDTH)
3470
3471Where ENDPOS is the end position of the sequence and RUN-WIDTH is the width of
3472the sequence."
e65df0a1
KH
3473 (let* ((wrappoint (ps-mule-find-wrappoint
3474 from to (ps-avg-char-width 'ps-font-for-text)))
3475 (to (car wrappoint))
3476 (font-type (car (nth ps-current-font
3477 (ps-font-alist 'ps-font-for-text))))
3478 (font-spec (ps-mule-get-font-spec ps-mule-current-charset font-type))
e65df0a1
KH
3479 (string (buffer-substring-no-properties from to)))
3480 (cond
3481 ((= from to)
3482 ;; We can't print any more characters in the current line.
3483 nil)
3484
3485 (font-spec
3486 ;; We surely have a font for printing this character set.
d3ab8dac 3487 (ps-output-string (ps-mule-string-encoding font-spec string))
e65df0a1
KH
3488 (ps-output " S\n"))
3489
3490 ((eq ps-mule-current-charset 'latin-iso8859-1)
3491 ;; Latin-1 can be printed by a normal ASCII font.
d3ab8dac 3492 (ps-output-string (ps-mule-string-ascii string))
e65df0a1
KH
3493 (ps-output " S\n"))
3494
3495 ((eq ps-mule-current-charset 'composition)
3496 (let* ((ch (char-after from))
3497 (width (char-width ch))
3498 (ch-list (decompose-composite-char ch 'list t)))
3499 (if (consp (nth 1 ch-list))
3500 (ps-mule-plot-rule-cmpchar ch-list width font-type)
3501 (ps-mule-plot-cmpchar ch-list width t font-type))))
3502
3503 (t
3504 ;; No way to print this charset. Just show a vacant box of an
3505 ;; appropriate width.
3506 (ps-output (format "%d %d SB\n"
3507 (length string)
3508 (if (eq ps-mule-current-charset 'composition)
3509 (char-width (char-after from))
3510 (charset-width ps-mule-current-charset))))))
3511 wrappoint))
3512
3513;; Composite font support
3514
3515(defvar ps-mule-cmpchar-prologue-generated nil)
3516
3517(defconst ps-mule-cmpchar-prologue
3518 "%%%% Composite character handler
3519/CmpcharWidth 0 def
3520/CmpcharRelativeCompose 0 def
3521/CmpcharRelativeSkip 0.4 def
3522
3523%% Get a bounding box (relative to currentpoint) of STR.
3524/GetPathBox { % str |- --
3525 gsave
3526 currentfont /FontType get 3 eq { %ifelse
3527 stringwidth pop pop
3528 } {
3529 currentpoint /y exch def pop
3530 false charpath flattenpath pathbbox
3531 y sub /URY exch def pop
3532 y sub /LLY exch def pop
3533 } ifelse
3534 grestore
3535} bind def
3536
3537%% Beginning of composite char.
3538/BC { % str xoff width |- --
3539 /Cmpchar true def
3540 /CmpcharWidth exch def
3541 currentfont /RelativeCompose known {
3542 /CmpcharRelativeCompose currentfont /RelativeCompose get def
3543 } {
3544 /CmpcharRelativeCompose false def
3545 } ifelse
3546 /bgsave bg def /bgcolorsave bgcolor def
3547 /Effectsave Effect def
3548 gsave % Reflect effect only at first
3549 /Effect Effect 1 2 add 4 add 16 add and def
3550 /f0 findfont setfont ( ) 0 CmpcharWidth getinterval S
3551 grestore
3552 /Effect Effectsave 8 32 add and def % enable only shadow and outline
3553 false BG
3554 gsave SpaceWidth mul 0 rmoveto dup GetPathBox S grestore
3555 /y currentpoint exch pop def
3556 /HIGH URY y add def /LOW LLY y add def
3557} bind def
3558
3559%% End of composite char.
3560/EC { % -- |- --
3561 /bg bgsave def /bgcolor bgcolorsave def
3562 /Effect Effectsave def
3563 /Cmpchar false def
3564 CmpcharWidth SpaceWidth mul 0 rmoveto
3565} bind def
3566
3567%% Rule base composition
3568/RBC { % str xoff gref nref |- --
3569 /nref exch def /gref exch def
3570 gsave
3571 SpaceWidth mul 0 rmoveto
3572 dup
3573 GetPathBox
3574 [ HIGH currentpoint exch pop LOW HIGH LOW add 2 div ] gref get
3575 [ URY LLY sub LLY neg 0 URY LLY sub 2 div ] nref get
3576 sub /btm exch def
3577 /top btm URY LLY sub add def
3578 top HIGH gt { /HIGH top def } if
3579 btm LOW lt { /LOW btm def } if
3580 currentpoint pop btm LLY sub moveto
3581 S
3582 grestore
d3ab8dac 3583} bind def
e65df0a1
KH
3584
3585%% Relative composition
3586/RLC { % str |- --
3587 gsave
3588 dup GetPathBox
3589 CmpcharRelativeCompose type /integertype eq {
3590 LLY CmpcharRelativeCompose gt { % compose on top
3591 currentpoint pop HIGH LLY sub CmpcharRelativeSkip add moveto
3592 /HIGH HIGH URY LLY sub add CmpcharRelativeSkip add def
3593 } { URY 0 le { % compose under bottom
3594 currentpoint pop LOW LLY add CmpcharRelativeSkip sub moveto
3595 /LOW LOW URY LLY sub sub CmpcharRelativeSkip sub def
3596 } if } ifelse } if
3597 S
3598 grestore
3599} bind def
3600%%%% End of composite character handler
3601
3602"
3603 "PostScript code for printing composite characters.")
3604
3605(defun ps-mule-plot-rule-cmpchar (ch-rule-list total-width font-type)
3606 (let* ((leftmost 0.0)
3607 (rightmost (float (char-width (car ch-rule-list))))
d3ab8dac 3608 (the-list (cons '(3 . 3) ch-rule-list))
e65df0a1 3609 (cmpchar-elements nil))
d3ab8dac
KH
3610 (while the-list
3611 (let* ((this (car the-list))
e65df0a1
KH
3612 (gref (car this))
3613 (nref (cdr this))
3614 ;; X-axis info (0:left, 1:center, 2:right)
3615 (gref-x (% gref 3))
3616 (nref-x (% nref 3))
3617 ;; Y-axis info (0:top, 1:base, 2:bottom, 3:center)
3618 (gref-y (if (= gref 4) 3 (/ gref 3)))
3619 (nref-y (if (= nref 4) 3 (/ nref 3)))
d3ab8dac 3620 (width (float (char-width (car (cdr the-list)))))
e65df0a1
KH
3621 left)
3622 (setq left (+ leftmost
3623 (/ (* (- rightmost leftmost) gref-x) 2.0)
d3ab8dac
KH
3624 (- (/ (* nref-x width) 2.0)))
3625 cmpchar-elements (cons (list (car (cdr the-list))
3626 left gref-y nref-y)
3627 cmpchar-elements)
3628 leftmost (min left leftmost)
3629 rightmost (max (+ left width) rightmost)
3630 the-list (nthcdr 2 the-list))))
e65df0a1 3631 (if (< leftmost 0)
d3ab8dac
KH
3632 (let ((the-list cmpchar-elements))
3633 (while the-list
3634 (setcar (cdr (car the-list))
3635 (- (nth 1 (car the-list)) leftmost))
3636 (setq the-list (cdr the-list)))))
e65df0a1
KH
3637 (ps-mule-plot-cmpchar (nreverse cmpchar-elements)
3638 total-width nil font-type)))
3639
3640(defun ps-mule-plot-cmpchar (elements total-width relativep font-type)
d3ab8dac
KH
3641 (let* ((elt (car elements))
3642 (ch (if relativep elt (car elt))))
3643 (ps-output-string (ps-mule-prepare-cmpchar-font ch font-type))
e65df0a1 3644 (ps-output (format " %d %d BC "
d3ab8dac
KH
3645 (if relativep 0 (nth 1 elt))
3646 total-width))
3647 (while (setq elements (cdr elements))
3648 (setq elt (car elements)
3649 ch (if relativep elt (car elt)))
3650 (ps-output-string (ps-mule-prepare-cmpchar-font ch font-type))
3651 (ps-output (if relativep
3652 " RLC "
3653 (format " %d %d %d RBC "
3654 (nth 1 elt) (nth 2 elt) (nth 3 elt))))))
e65df0a1 3655 (ps-output "EC\n"))
d3ab8dac 3656
e65df0a1
KH
3657(defun ps-mule-prepare-cmpchar-font (char font-type)
3658 (let* ((ps-mule-current-charset (char-charset char))
d3ab8dac 3659 (font-spec (ps-mule-get-font-spec ps-mule-current-charset font-type)))
e65df0a1 3660 (cond (font-spec
d3ab8dac 3661 (ps-mule-string-encoding font-spec (char-to-string char)))
e65df0a1
KH
3662
3663 ((eq ps-mule-current-charset 'latin-iso8859-1)
d3ab8dac 3664 (ps-mule-string-ascii (char-to-string char)))
e65df0a1
KH
3665
3666 (t
3667 ;; No font for CHAR.
3668 (ps-set-font ps-current-font)
d3ab8dac
KH
3669 " "))))
3670
3671(defun ps-mule-string-ascii (str)
3672 (ps-set-font ps-current-font)
3673 (string-as-unibyte (encode-coding-string str 'iso-latin-1)))
3674
3675(defun ps-mule-string-encoding (font-spec str)
3676 (let ((encoding (ps-mule-font-spec-encoding font-spec)))
3677 (cond ((coding-system-p encoding)
3678 (setq str (encode-coding-string str encoding)))
3679 ((functionp encoding)
3680 (setq str (funcall encoding str)))
3681 (encoding
3682 (error "Invalid coding system or function: %s" encoding)))
3683 (setq str (string-as-unibyte str))
3684 (if (ps-mule-font-spec-src font-spec)
3685 (ps-mule-prepare-font font-spec str ps-mule-current-charset)
3686 (ps-set-font ps-current-font))
e65df0a1
KH
3687 str))
3688
3689;; Bitmap font support
3690
3691(defvar ps-mule-bitmap-prologue-generated nil)
3692
3693(defconst ps-mule-bitmap-prologue
3694 "%%%% Bitmap font handler
3695
3696/str7 7 string def % working area
3697
3698%% We grow the dictionary one bunch (1024 entries) by one.
3699/BitmapDictArray 256 array def
3700/BitmapDictLength 1024 def
3701/BitmapDictIndex -1 def
3702
3703/NewBitmapDict { % -- |- --
3704 /BitmapDictIndex BitmapDictIndex 1 add def
3705 BitmapDictArray BitmapDictIndex BitmapDictLength dict put
3706} bind def
3707
3708%% Make at least one dictionary.
3709NewBitmapDict
3710
3711/AddBitmap { % gloval-charname bitmap-data |- --
3712 BitmapDictArray BitmapDictIndex get
3713 dup length BitmapDictLength ge {
3714 pop
3715 NewBitmapDict
3716 BitmapDictArray BitmapDictIndex get
3717 } if
3718 3 1 roll put
3719} bind def
3720
3721/GetBitmap { % gloval-charname |- bitmap-data
3722 0 1 BitmapDictIndex { BitmapDictArray exch get begin } for
3723 load
3724 0 1 BitmapDictIndex { pop end } for
3725} bind def
3726
3727%% Return a global character name which can be used as a key in the
3728%% bitmap dictionary.
3729/GlobalCharName { % fontidx code1 code2 |- gloval-charname
3730 exch 256 mul add exch 65536 mul add 16777216 add 16 str7 cvrs 0 66 put
3731 str7 cvn
3732} bind def
d3ab8dac 3733
e65df0a1
KH
3734%% Character code holder for a 2-byte character.
3735/FirstCode -1 def
3736
3737%% Glyph rendering procedure
3738/BuildGlyphCommon { % fontdict charname |- --
3739 1 index /FontDimension get 1 eq { /FirstCode 0 store } if
3740 NameIndexDict exch get % STACK: fontdict charcode
3741 FirstCode 0 lt { %ifelse
3742 %% This is the first byte of a 2-byte character. Just
3743 %% remember it for the moment.
3744 /FirstCode exch store
3745 pop
3746 0 0 setcharwidth
3747 } {
3748 1 index /FontSize get /size exch def
be415ea7 3749 1 index /FontSpaceWidthRatio get /ratio exch def
e65df0a1
KH
3750 1 index /FontIndex get exch FirstCode exch
3751 GlobalCharName GetBitmap /bmp exch def
3752 %% bmp == [ DWIDTH BBX-WIDTH BBX-HEIGHT BBX-XOFF BBX-YOFF BITMAP ]
3753 Cmpchar { %ifelse
3754 /FontMatrix get [ exch { size div } forall ] /mtrx exch def
3755 bmp 3 get bmp 4 get mtrx transform
3756 /LLY exch def pop
3757 bmp 1 get bmp 3 get add bmp 2 get bmp 4 get add mtrx transform
3758 /URY exch def pop
3759 } {
3760 pop
3761 } ifelse
3762 /FirstCode -1 store
3763
be415ea7 3764 bmp 0 get SpaceWidthRatio ratio div mul size div 0 % wx wy
e65df0a1
KH
3765 setcharwidth % We can't use setcachedevice here.
3766
3767 bmp 1 get 0 gt bmp 2 get 0 gt and {
3768 bmp 1 get bmp 2 get % width height
3769 true % polarity
3770 [ size 0 0 size neg bmp 3 get neg bmp 2 get bmp 4 get add ] % matrix
3771 bmp 5 1 getinterval cvx % datasrc
3772 imagemask
3773 } if
3774 } ifelse
d3ab8dac 3775} bind def
e65df0a1
KH
3776
3777/BuildCharCommon {
3778 1 index /Encoding get exch get
3779 1 index /BuildGlyph get exec
3780} bind def
3781
3782%% Bitmap font creater
3783
3784%% Common Encoding shared by all bitmap fonts.
3785/EncodingCommon 256 array def
3786%% Mapping table from character name to character code.
3787/NameIndexDict 256 dict def
37880 1 255 { %for
3789 /idx exch def
3790 /idxname idx 256 add 16 (XXX) cvrs dup 0 67 put cvn def % `C' == 67
3791 EncodingCommon idx idxname put
3792 NameIndexDict idxname idx put
3793} for
3794
3795/GlobalFontIndex 0 def
3796
be415ea7 3797%% fontname dim col fontsize relative-compose baseline-offset fbbx |- --
e65df0a1 3798/BitmapFont {
be415ea7 3799 15 dict begin
e65df0a1
KH
3800 /FontBBox exch def
3801 /BaselineOffset exch def
3802 /RelativeCompose exch def
3803 /FontSize exch def
3804 /FontBBox [ FontBBox { FontSize div } forall ] def
be415ea7
KH
3805 FontBBox 2 get FontBBox 0 get sub exch div
3806 /FontSpaceWidthRatio exch def
e65df0a1
KH
3807 /FontDimension exch def
3808 /FontIndex GlobalFontIndex def
3809 /FontType 3 def
3810 /FontMatrix matrix def
3811 /Encoding EncodingCommon def
3812 /BuildGlyph { BuildGlyphCommon } def
3813 /BuildChar { BuildCharCommon } def
3814 currentdict end
3815 definefont pop
3816 /GlobalFontIndex GlobalFontIndex 1 add def
3817} bind def
3818
3819%% Define a new bitmap font.
be415ea7 3820%% fontname dim col fontsize relative-compose baseline-offset fbbx |- --
e65df0a1
KH
3821/NF {
3822 /fbbx exch def
3823 %% Convert BDF's FontBoundingBox to PostScript's FontBBox
3824 [ fbbx 2 get fbbx 3 get
3825 fbbx 2 get fbbx 0 get add fbbx 3 get fbbx 1 get add ]
3826 BitmapFont
3827} bind def
3828
3829%% Define a glyph for the specified font and character.
3830/NG { % fontname charcode bitmap-data |- --
3831 /bmp exch def
3832 exch findfont dup /BaselineOffset get bmp 4 get add bmp exch 4 exch put
3833 /FontIndex get exch
3834 dup 256 idiv exch 256 mod GlobalCharName
3835 bmp AddBitmap
3836} bind def
3837%%%% End of bitmap font handler
3838
3839")
3840
3841;; External library support.
3842
3843;; The following three functions are to be called from external
3844;; libraries which support bitmap fonts (e.g. `bdf') to get
3845;; appropriate PostScript code.
3846
3847(defun ps-mule-generate-bitmap-prologue ()
3848 (unless ps-mule-bitmap-prologue-generated
3849 (setq ps-mule-bitmap-prologue-generated t)
3850 (list ps-mule-bitmap-prologue)))
3851
3852(defun ps-mule-generate-bitmap-font (&rest args)
be415ea7 3853 (list (apply 'format "/%s %d %d %f %S %d %S NF\n" args)))
e65df0a1
KH
3854
3855(defun ps-mule-generate-bitmap-glyph (font-name code dwidth bbx bitmap)
3856 (format "/%s %d [ %d %d %d %d %d <%s> ] NG\n"
3857 font-name code
3858 dwidth (aref bbx 0) (aref bbx 1) (aref bbx 2) (aref bbx 3)
3859 bitmap))
3860
3861;; Mule specific initializers.
3862
3863(defun ps-mule-initialize ()
024ced4d 3864 "Produce Poscript code in the prologue part for multi-byte characters."
d3ab8dac
KH
3865 (setq ps-mule-font-info-database
3866 (cond ((eq ps-multibyte-buffer 'non-latin-printer)
3867 ps-mule-font-info-database-ps)
3868 ((eq ps-multibyte-buffer 'bdf-font)
3869 ps-mule-font-info-database-bdf)
3870 ((eq ps-multibyte-buffer 'bdf-font-except-latin)
3871 ps-mule-font-info-database-ps-bdf)
3872 (t
3873 ps-mule-font-info-database-latin))
3874 ps-mule-current-charset 'ascii
e65df0a1
KH
3875 ps-mule-font-cache nil
3876 ps-mule-prologue-generated nil
3877 ps-mule-cmpchar-prologue-generated nil
3878 ps-mule-bitmap-prologue-generated nil)
d3ab8dac 3879 (mapcar `(lambda (x) (setcar (cdr x) nil))
e65df0a1
KH
3880 ps-mule-external-libraries))
3881
3882(defun ps-mule-begin (from to)
d3ab8dac
KH
3883 (and (boundp 'enable-multibyte-characters)
3884 enable-multibyte-characters
3885 ;; Initialize `ps-mule-charset-list'. If some characters aren't
3886 ;; printable, warn it.
3887 (let ((charsets (delete 'ascii (find-charset-region from to))))
3888 (setq ps-mule-charset-list charsets)
3889 (save-excursion
3890 (goto-char from)
3891 (and (search-forward "\200" to t)
3892 (setq ps-mule-charset-list
3893 (cons 'composition ps-mule-charset-list))))
3894 (while charsets
3895 (cond
3896 ((or (eq (car charsets) 'composition)
3897 (ps-mule-printable-p (car charsets)))
3898 (setq charsets (cdr charsets)))
3899 ((y-or-n-p "Font for some characters not found, continue anyway? ")
3900 (setq charsets nil))
3901 (t
3902 (error "Printing cancelled"))))))
e65df0a1
KH
3903
3904 (if ps-mule-charset-list
d3ab8dac 3905 (let ((the-list ps-mule-charset-list)
e65df0a1
KH
3906 font-spec)
3907 (unless ps-mule-prologue-generated
3908 (ps-output-prologue ps-mule-prologue)
3909 (setq ps-mule-prologue-generated t))
3910 ;; If external functions are necessary, generate prologues for them.
d3ab8dac
KH
3911 (while the-list
3912 (cond ((and (eq (car the-list) 'composition)
3913 (not ps-mule-cmpchar-prologue-generated))
3914 (ps-output-prologue ps-mule-cmpchar-prologue)
3915 (setq ps-mule-cmpchar-prologue-generated t))
3916 ((setq font-spec (ps-mule-get-font-spec (car the-list) 'normal))
3917 (ps-mule-init-external-library font-spec)))
3918 (setq the-list (cdr the-list)))))
e65df0a1
KH
3919
3920 ;; If ASCII font is also specified in ps-mule-font-info-database,
3921 ;; use it istead of what specified in ps-font-info-database.
3922 (let ((font-spec (ps-mule-get-font-spec 'ascii 'normal)))
3923 (if font-spec
3924 (progn
3925 (unless ps-mule-prologue-generated
3926 (ps-output-prologue ps-mule-prologue)
3927 (setq ps-mule-prologue-generated t))
3928 (ps-mule-init-external-library font-spec)
3929 (let ((font (ps-font-alist 'ps-font-for-text))
3930 (i 0))
3931 (while font
3932 (let ((ps-current-font i))
3933 ;; Be sure to download a glyph for SPACE in advance.
3934 (ps-mule-prepare-font
3935 (ps-mule-get-font-spec 'ascii (car font))
3936 " " 'ascii 'no-setfont))
d3ab8dac
KH
3937 (setq font (cdr font)
3938 i (1+ i))))))))
e65df0a1 3939
024ced4d 3940;; For handling multi-byte characters -- End.
e65df0a1 3941;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
d3ab8dac 3942\f
e65df0a1 3943
bcc0d457 3944(defun ps-line-lengths-internal ()
87a16a06 3945 "Display the correspondence between a line length and a font size,
bcc0d457
RS
3946using the current ps-print setup.
3947Try: pr -t file | awk '{printf \"%3d %s\n\", length($0), $0}' | sort -r | head"
3948 (let ((buf (get-buffer-create "*Line-lengths*"))
3949 (ifs ps-font-size) ; initial font size
12b88fff 3950 (icw (ps-avg-char-width 'ps-font-for-text)) ; initial character width
bcc0d457
RS
3951 (print-width (progn (ps-get-page-dimensions)
3952 ps-print-width))
3953 (ps-setup (ps-setup)) ; setup for the current buffer
3954 (fs-min 5) ; minimum font size
3955 cw-min ; minimum character width
3956 nb-cpl-max ; maximum nb of characters per line
3957 (fs-max 14) ; maximum font size
3958 cw-max ; maximum character width
3959 nb-cpl-min ; minimum nb of characters per line
3960 fs ; current font size
3961 cw ; current character width
3962 nb-cpl ; current nb of characters per line
3963 )
3964 (setq cw-min (/ (* icw fs-min) ifs)
3965 nb-cpl-max (floor (/ print-width cw-min))
3966 cw-max (/ (* icw fs-max) ifs)
8bd22fcf
KH
3967 nb-cpl-min (floor (/ print-width cw-max))
3968 nb-cpl nb-cpl-min)
bcc0d457
RS
3969 (set-buffer buf)
3970 (goto-char (point-max))
8bd22fcf
KH
3971 (or (bolp) (insert "\n"))
3972 (insert ps-setup
3973 "nb char per line / font size\n")
bcc0d457 3974 (while (<= nb-cpl nb-cpl-max)
8bd22fcf
KH
3975 (setq cw (/ print-width (float nb-cpl))
3976 fs (/ (* ifs cw) icw))
bcc0d457
RS
3977 (insert (format "%3s %s\n" nb-cpl fs))
3978 (setq nb-cpl (1+ nb-cpl)))
3979 (insert "\n")
3980 (display-buffer buf 'not-this-window)))
3981
3982(defun ps-nb-pages (nb-lines)
496725ad
RS
3983 "Display correspondence between font size and the number of pages.
3984The correspondence is based on having NB-LINES lines of text,
3985and on the current ps-print setup."
bcc0d457
RS
3986 (let ((buf (get-buffer-create "*Nb-Pages*"))
3987 (ifs ps-font-size) ; initial font size
12b88fff 3988 (ilh (ps-line-height 'ps-font-for-text)) ; initial line height
bcc0d457
RS
3989 (page-height (progn (ps-get-page-dimensions)
3990 ps-print-height))
3991 (ps-setup (ps-setup)) ; setup for the current buffer
3992 (fs-min 4) ; minimum font size
3993 lh-min ; minimum line height
3994 nb-lpp-max ; maximum nb of lines per page
3995 nb-page-min ; minimum nb of pages
3996 (fs-max 14) ; maximum font size
3997 lh-max ; maximum line height
3998 nb-lpp-min ; minimum nb of lines per page
3999 nb-page-max ; maximum nb of pages
4000 fs ; current font size
4001 lh ; current line height
4002 nb-lpp ; current nb of lines per page
4003 nb-page ; current nb of pages
4004 )
4005 (setq lh-min (/ (* ilh fs-min) ifs)
4006 nb-lpp-max (floor (/ page-height lh-min))
4007 nb-page-min (ceiling (/ (float nb-lines) nb-lpp-max))
4008 lh-max (/ (* ilh fs-max) ifs)
4009 nb-lpp-min (floor (/ page-height lh-max))
8bd22fcf
KH
4010 nb-page-max (ceiling (/ (float nb-lines) nb-lpp-min))
4011 nb-page nb-page-min)
bcc0d457
RS
4012 (set-buffer buf)
4013 (goto-char (point-max))
8bd22fcf
KH
4014 (or (bolp) (insert "\n"))
4015 (insert ps-setup
4016 (format "%d lines\n" nb-lines)
4017 "nb page / font size\n")
bcc0d457
RS
4018 (while (<= nb-page nb-page-max)
4019 (setq nb-lpp (ceiling (/ nb-lines (float nb-page)))
4020 lh (/ page-height nb-lpp)
4021 fs (/ (* ifs lh) ilh))
4022 (insert (format "%s %s\n" nb-page fs))
4023 (setq nb-page (1+ nb-page)))
4024 (insert "\n")
4025 (display-buffer buf 'not-this-window)))
4026
6bdb808e
RS
4027;; macros used in `ps-select-font'
4028(defmacro ps-lookup (key) `(cdr (assq ,key font-entry)))
4029(defmacro ps-size-scale (key) `(/ (* (ps-lookup ,key) font-size) size))
4030
12b88fff
RS
4031(defun ps-select-font (font-family sym font-size title-font-size)
4032 (let ((font-entry (cdr (assq font-family ps-font-info-database))))
4033 (or font-entry
4034 (error "Don't have data to scale font %s. Known fonts families are %s"
4035 font-family
4036 (mapcar 'car ps-font-info-database)))
6bdb808e
RS
4037 (let ((size (ps-lookup 'size)))
4038 (put sym 'fonts (ps-lookup 'fonts))
4039 (put sym 'space-width (ps-size-scale 'space-width))
4040 (put sym 'avg-char-width (ps-size-scale 'avg-char-width))
4041 (put sym 'line-height (ps-size-scale 'line-height))
4042 (put sym 'title-line-height
4043 (/ (* (ps-lookup 'line-height) title-font-size) size)))))
bcc0d457 4044
12d89a2e 4045(defun ps-get-page-dimensions ()
bcc0d457
RS
4046 (let ((page-dimensions (cdr (assq ps-paper-type ps-page-dimensions-database)))
4047 page-width page-height)
4048 (cond
4049 ((null page-dimensions)
4050 (error "`ps-paper-type' must be one of:\n%s"
4051 (mapcar 'car ps-page-dimensions-database)))
4052 ((< ps-number-of-columns 1)
12b88fff 4053 (error "The number of columns %d should be positive"
8bd22fcf 4054 ps-number-of-columns)))
bcc0d457 4055
12b88fff
RS
4056 (ps-select-font ps-font-family 'ps-font-for-text
4057 ps-font-size ps-font-size)
4058 (ps-select-font ps-header-font-family 'ps-font-for-header
4059 ps-header-font-size ps-header-title-font-size)
bcc0d457
RS
4060
4061 (setq page-width (ps-page-dimensions-get-width page-dimensions)
4062 page-height (ps-page-dimensions-get-height page-dimensions))
4063
4064 ;; Landscape mode
4065 (if ps-landscape-mode
4066 ;; exchange width and height
4067 (setq page-width (prog1 page-height (setq page-height page-width))))
4068
4069 ;; It is used to get the lower right corner (only in landscape mode)
4070 (setq ps-landscape-page-height page-height)
4071
4072 ;; | lm | text | ic | text | ic | text | rm |
4073 ;; page-width == lm + n * pw + (n - 1) * ic + rm
4074 ;; => pw == (page-width - lm -rm - (n - 1) * ic) / n
8bd22fcf
KH
4075 (setq ps-print-width (/ (- page-width
4076 ps-left-margin ps-right-margin
4077 (* (1- ps-number-of-columns) ps-inter-column))
4078 ps-number-of-columns))
bcc0d457
RS
4079 (if (<= ps-print-width 0)
4080 (error "Bad horizontal layout:
4081page-width == %s
4082ps-left-margin == %s
4083ps-right-margin == %s
4084ps-inter-column == %s
4085ps-number-of-columns == %s
4086| lm | text | ic | text | ic | text | rm |
4087page-width == lm + n * print-width + (n - 1) * ic + rm
4088=> print-width == %d !"
4089 page-width
4090 ps-left-margin
4091 ps-right-margin
4092 ps-inter-column
4093 ps-number-of-columns
4094 ps-print-width))
4095
4096 (setq ps-print-height
4097 (- page-height ps-bottom-margin ps-top-margin))
4098 (if (<= ps-print-height 0)
4099 (error "Bad vertical layout:
4100ps-top-margin == %s
4101ps-bottom-margin == %s
4102page-height == bm + print-height + tm
4103=> print-height == %d !"
4104 ps-top-margin
4105 ps-bottom-margin
4106 ps-print-height))
4107 ;; If headers are turned on, deduct the height of the header from
4108 ;; the print height.
8bd22fcf 4109 (if ps-print-header
12b88fff
RS
4110 (setq ps-header-pad (* ps-header-line-pad
4111 (ps-title-line-height 'ps-font-for-header))
8bd22fcf
KH
4112 ps-print-height (- ps-print-height
4113 ps-header-offset
4114 ps-header-pad
12b88fff
RS
4115 (ps-title-line-height 'ps-font-for-header)
4116 (* (ps-line-height 'ps-font-for-header)
4117 (1- ps-header-lines))
8bd22fcf 4118 ps-header-pad)))
bcc0d457
RS
4119 (if (<= ps-print-height 0)
4120 (error "Bad vertical layout:
4121ps-top-margin == %s
4122ps-bottom-margin == %s
4123ps-header-offset == %s
4124ps-header-pad == %s
4125header-height == %s
4126page-height == bm + print-height + tm - ho - hh
4127=> print-height == %d !"
4128 ps-top-margin
4129 ps-bottom-margin
4130 ps-header-offset
4131 ps-header-pad
4132 (+ ps-header-pad
12b88fff
RS
4133 (ps-title-line-height 'ps-font-for-header)
4134 (* (ps-line-height 'ps-font-for-header)
4135 (1- ps-header-lines))
bcc0d457
RS
4136 ps-header-pad)
4137 ps-print-height))))
ef2cbb24 4138
12d89a2e 4139(defun ps-print-preprint (&optional filename)
8bd22fcf
KH
4140 (and filename
4141 (or (numberp filename)
4142 (listp filename))
d3ab8dac
KH
4143 (let* ((name (concat (file-name-nondirectory (or (buffer-file-name)
4144 (buffer-name)))
4145 ".ps"))
8bd22fcf
KH
4146 (prompt (format "Save PostScript to file: (default %s) " name))
4147 (res (read-file-name prompt default-directory name nil)))
d3ab8dac
KH
4148 (while (cond ((not (file-writable-p res))
4149 (ding)
4150 (setq prompt "is unwritable"))
4151 ((file-exists-p res)
4152 (setq prompt "exists")
4153 (not (y-or-n-p (format "File `%s' exists; overwrite? "
4154 res))))
4155 (t nil))
4156 (setq res (read-file-name
4157 (format "File %s; save PostScript to file: " prompt)
4158 (file-name-directory res) nil nil
4159 (file-name-nondirectory res))))
8bd22fcf
KH
4160 (if (file-directory-p res)
4161 (expand-file-name name (file-name-as-directory res))
4162 res))))
12d89a2e
RS
4163
4164;; The following functions implement a simple list-buffering scheme so
4165;; that ps-print doesn't have to repeatedly switch between buffers
857686a6
RS
4166;; while spooling. The functions `ps-output' and `ps-output-string' build
4167;; up the lists; the function `ps-flush-output' takes the lists and
12d89a2e
RS
4168;; insert its contents into the spool buffer (*PostScript*).
4169
857686a6
RS
4170(defvar ps-string-escape-codes
4171 (let ((table (make-vector 256 nil))
4172 (char ?\000))
4173 ;; control characters
4174 (while (<= char ?\037)
4175 (aset table char (format "\\%03o" char))
4176 (setq char (1+ char)))
4177 ;; printable characters
4178 (while (< char ?\177)
4179 (aset table char (format "%c" char))
4180 (setq char (1+ char)))
4181 ;; DEL and 8-bit characters
4182 (while (<= char ?\377)
4183 (aset table char (format "\\%o" char))
4184 (setq char (1+ char)))
4185 ;; Override ASCII formatting characters with named escape code:
4186 (aset table ?\n "\\n") ; [NL] linefeed
4187 (aset table ?\r "\\r") ; [CR] carriage return
4188 (aset table ?\t "\\t") ; [HT] horizontal tab
4189 (aset table ?\b "\\b") ; [BS] backspace
4190 (aset table ?\f "\\f") ; [NP] form feed
4191 ;; Escape PostScript escape and string delimiter characters:
4192 (aset table ?\\ "\\\\")
4193 (aset table ?\( "\\(")
4194 (aset table ?\) "\\)")
4195 table)
4196 "Vector used to map characters to PostScript string escape codes.")
4197
12d89a2e
RS
4198(defun ps-output-string-prim (string)
4199 (insert "(") ;insert start-string delimiter
4200 (save-excursion ;insert string
e65df0a1 4201 (insert (string-as-unibyte string)))
12d89a2e 4202 ;; Find and quote special characters as necessary for PS
c82b4a75 4203 ;; This skips everything except control chars, non-ASCII chars, (, ) and \.
b61e2c11
RS
4204 (while (progn (skip-chars-forward " -'*-[]-~") (not (eobp)))
4205 (let ((special (following-char)))
e65df0a1
KH
4206 (delete-char 1)
4207 (insert (aref ps-string-escape-codes special))))
12d89a2e
RS
4208 (goto-char (point-max))
4209 (insert ")")) ;insert end-string delimiter
ef2cbb24 4210
12d89a2e 4211(defun ps-init-output-queue ()
8bd22fcf
KH
4212 (setq ps-output-head '("")
4213 ps-output-tail ps-output-head))
ef2cbb24 4214
12d89a2e
RS
4215(defun ps-output (&rest args)
4216 (setcdr ps-output-tail args)
4217 (while (cdr ps-output-tail)
4218 (setq ps-output-tail (cdr ps-output-tail))))
ef2cbb24 4219
12d89a2e
RS
4220(defun ps-output-string (string)
4221 (ps-output t string))
ef2cbb24 4222
87a16a06
RS
4223(defun ps-output-list (the-list)
4224 (mapcar 'ps-output the-list))
4225
e65df0a1
KH
4226;; Output strings in the list ARGS in the PostScript prologue part.
4227(defun ps-output-prologue (args)
4228 (ps-output 'prologue (if (stringp args) (list args) args)))
4229
12d89a2e
RS
4230(defun ps-flush-output ()
4231 (save-excursion
4232 (set-buffer ps-spool-buffer)
4233 (goto-char (point-max))
4234 (while ps-output-head
4235 (let ((it (car ps-output-head)))
e65df0a1
KH
4236 (cond
4237 ((eq t it)
4238 (setq ps-output-head (cdr ps-output-head))
4239 (ps-output-string-prim (car ps-output-head)))
4240 ((eq 'prologue it)
12d89a2e 4241 (setq ps-output-head (cdr ps-output-head))
e65df0a1
KH
4242 (save-excursion
4243 (search-backward "\nBeginDoc")
4244 (forward-char 1)
4245 (apply 'insert (car ps-output-head))))
4246 (t
4247 (insert it))))
12d89a2e
RS
4248 (setq ps-output-head (cdr ps-output-head))))
4249 (ps-init-output-queue))
4250
4251(defun ps-insert-file (fname)
4252 (ps-flush-output)
12d89a2e 4253 ;; Check to see that the file exists and is readable; if not, throw
87a16a06
RS
4254 ;; an error.
4255 (or (file-readable-p fname)
12d89a2e 4256 (error "Could not read file `%s'" fname))
12d89a2e
RS
4257 (save-excursion
4258 (set-buffer ps-spool-buffer)
4259 (goto-char (point-max))
4260 (insert-file fname)))
06fb6aab 4261
12d89a2e
RS
4262;; These functions insert the arrays that define the contents of the
4263;; headers.
ef2cbb24 4264
12d89a2e
RS
4265(defun ps-generate-header-line (fonttag &optional content)
4266 (ps-output " [ " fonttag " ")
4267 (cond
4268 ;; Literal strings should be output as is -- the string must
4269 ;; contain its own PS string delimiters, '(' and ')', if necessary.
4270 ((stringp content)
4271 (ps-output content))
4272
4273 ;; Functions are called -- they should return strings; they will be
4274 ;; inserted as strings and the PS string delimiters added.
4275 ((and (symbolp content) (fboundp content))
4276 (ps-output-string (funcall content)))
4277
4278 ;; Variables will have their contents inserted. They should
4279 ;; contain strings, and will be inserted as strings.
4280 ((and (symbolp content) (boundp content))
4281 (ps-output-string (symbol-value content)))
4282
4283 ;; Anything else will get turned into an empty string.
4284 (t
4285 (ps-output-string "")))
4286 (ps-output " ]\n"))
4287
4288(defun ps-generate-header (name contents)
4289 (ps-output "/" name " [\n")
4290 (if (> ps-header-lines 0)
4291 (let ((count 1))
4292 (ps-generate-header-line "/h0" (car contents))
4293 (while (and (< count ps-header-lines)
4294 (setq contents (cdr contents)))
4295 (ps-generate-header-line "/h1" (car contents))
8bd22fcf 4296 (setq count (1+ count)))
12d89a2e
RS
4297 (ps-output "] def\n"))))
4298
4299(defun ps-output-boolean (name bool)
4300 (ps-output (format "/%s %s def\n" name (if bool "true" "false"))))
ef2cbb24 4301
06fb6aab 4302
87a16a06
RS
4303(defun ps-background-pages (page-list func)
4304 (if page-list
4305 (mapcar
4306 '(lambda (pages)
4307 (let ((start (if (consp pages) (car pages) pages))
4308 (end (if (consp pages) (cdr pages) pages)))
4309 (and (integerp start) (integerp end) (<= start end)
4310 (add-to-list 'ps-background-pages (vector start end func)))))
4311 page-list)
4312 (setq ps-background-all-pages (cons func ps-background-all-pages))))
4313
4314
4315(defun ps-get-boundingbox ()
4316 (save-excursion
4317 (set-buffer ps-spool-buffer)
4318 (save-excursion
4319 (if (re-search-forward
4320 "^%%BoundingBox:\\s-+\\([0-9.]+\\)\\s-+\\([0-9.]+\\)\\s-+\\([0-9.]+\\)\\s-+\\([0-9.]+\\)"
4321 nil t)
4322 (vector (string-to-number ; lower x
4323 (buffer-substring (match-beginning 1) (match-end 1)))
4324 (string-to-number ; lower y
4325 (buffer-substring (match-beginning 2) (match-end 2)))
4326 (string-to-number ; upper x
4327 (buffer-substring (match-beginning 3) (match-end 3)))
4328 (string-to-number ; upper y
4329 (buffer-substring (match-beginning 4) (match-end 4))))
4330 (vector 0 0 0 0)))))
4331
4332
4333;; Emacs understands the %f format; we'll use it to limit color RGB values
4334;; to three decimals to cut down some on the size of the PostScript output.
4335;; Lucid emacsen will have to make do with %s (princ) for floats.
4336
4337(defvar ps-float-format (if (eq ps-print-emacs-type 'emacs)
4338 "%0.3f " ; emacs
4339 "%s ")) ; Lucid emacsen
4340
4341
4342(defun ps-float-format (value &optional default)
4343 (let ((literal (or value default)))
4344 (if literal
4345 (format (if (numberp literal)
4346 ps-float-format
4347 "%s ")
4348 literal)
4349 " ")))
4350
4351
4352(defun ps-background-text ()
4353 (mapcar
4354 '(lambda (text)
4355 (setq ps-background-text-count (1+ ps-background-text-count))
4356 (ps-output (format "/ShowBackText-%d {\n" ps-background-text-count))
4357 (ps-output-string (nth 0 text)) ; text
4358 (ps-output
4359 "\n"
4360 (ps-float-format (nth 4 text) 200.0) ; font size
4361 (format "/%s " (or (nth 3 text) "Times-Roman")) ; font name
4362 (ps-float-format (nth 6 text)
4363 "PrintHeight PrintPageWidth atan") ; rotation
4364 (ps-float-format (nth 5 text) 0.85) ; gray
4365 (ps-float-format (nth 1 text) "0") ; x position
4366 (ps-float-format (nth 2 text) "BottomMargin") ; y position
4367 "\nShowBackText} def\n")
4368 (ps-background-pages (nthcdr 7 text) ; page list
4369 (format "ShowBackText-%d\n"
4370 ps-background-text-count)))
4371 ps-print-background-text))
4372
4373
4374(defun ps-background-image ()
4375 (mapcar
4376 '(lambda (image)
4377 (let ((image-file (expand-file-name (nth 0 image))))
4378 (if (file-readable-p image-file)
4379 (progn
4380 (setq ps-background-image-count (1+ ps-background-image-count))
4381 (ps-output
4382 (format "/ShowBackImage-%d {\n--back-- " ps-background-image-count)
4383 (ps-float-format (nth 5 image) 0.0) ; rotation
4384 (ps-float-format (nth 3 image) 1.0) ; x scale
4385 (ps-float-format (nth 4 image) 1.0) ; y scale
4386 (ps-float-format (nth 1 image) ; x position
4387 "PrintPageWidth 2 div")
4388 (ps-float-format (nth 2 image) ; y position
4389 "PrintHeight 2 div BottomMargin add")
4390 "\nBeginBackImage\n")
4391 (ps-insert-file image-file)
4392 ;; coordinate adjustment to centralize image
4393 ;; around x and y position
4394 (let ((box (ps-get-boundingbox)))
4395 (save-excursion
4396 (set-buffer ps-spool-buffer)
4397 (save-excursion
4398 (if (re-search-backward "^--back--" nil t)
4399 (replace-match
4400 (format "%s %s"
4401 (ps-float-format
4402 (- (+ (/ (- (aref box 2) (aref box 0)) 2.0)
4403 (aref box 0))))
4404 (ps-float-format
4405 (- (+ (/ (- (aref box 3) (aref box 1)) 2.0)
4406 (aref box 1)))))
4407 t)))))
4408 (ps-output "\nEndBackImage} def\n")
4409 (ps-background-pages (nthcdr 6 image) ; page list
4410 (format "ShowBackImage-%d\n"
4411 ps-background-image-count))))))
4412 ps-print-background-image))
4413
4414
a18ed129 4415(defun ps-background (page-number)
87a16a06
RS
4416 (let (has-local-background)
4417 (mapcar '(lambda (range)
a18ed129
RS
4418 (and (<= (aref range 0) page-number)
4419 (<= page-number (aref range 1))
87a16a06
RS
4420 (if has-local-background
4421 (ps-output (aref range 2))
4422 (setq has-local-background t)
4423 (ps-output "/printLocalBackground {\n"
4424 (aref range 2)))))
4425 ps-background-pages)
4426 (and has-local-background (ps-output "} def\n"))))
4427
4428
0140c600
EZ
4429;; Return a list of the distinct elements of LIST.
4430;; Elements are compared with `equal'.
4431(defun ps-remove-duplicates (list)
4432 (let (new (tail list))
4433 (while tail
4434 (or (member (car tail) new)
4435 (setq new (cons (car tail) new)))
4436 (setq tail (cdr tail)))
4437 (nreverse new)))
4438
c82b4a75 4439
6bdb808e
RS
4440;; Find the first occurrence of ITEM in LIST.
4441;; Return the index of the matching item, or nil if not found.
4442;; Elements are compared with `eq'.
7ae35a2f 4443(defun ps-alist-position (item list)
6bdb808e
RS
4444 (let ((tail list) (index 0) found)
4445 (while tail
7ae35a2f 4446 (if (setq found (eq (car (car tail)) item))
6bdb808e
RS
4447 (setq tail nil)
4448 (setq index (1+ index)
4449 tail (cdr tail))))
4450 (and found index)))
4451
4452
ef2cbb24 4453(defun ps-begin-file ()
bcc0d457 4454 (ps-get-page-dimensions)
7da17ab6 4455 (setq ps-page-postscript 0
87a16a06
RS
4456 ps-background-text-count 0
4457 ps-background-image-count 0
4458 ps-background-pages nil
4459 ps-background-all-pages nil)
12d89a2e 4460
8bd22fcf
KH
4461 (ps-output ps-adobe-tag
4462 "%%Title: " (buffer-name) ; Take job name from name of
4463 ; first buffer printed
4464 "\n%%Creator: " (user-full-name)
857686a6
RS
4465 " (using ps-print v" ps-print-version
4466 ")\n%%CreationDate: "
87a16a06
RS
4467 (time-stamp-hh:mm:ss) " " (time-stamp-mon-dd-yyyy)
4468 "\n%%Orientation: "
8bd22fcf 4469 (if ps-landscape-mode "Landscape" "Portrait")
d3ab8dac 4470 "\n%%DocumentNeededResources: font Times-Roman Times-Italic\n%%+ font "
12b88fff 4471 (mapconcat 'identity
0140c600 4472 (ps-remove-duplicates
12b88fff
RS
4473 (append (ps-fonts 'ps-font-for-text)
4474 (list (ps-font 'ps-font-for-header 'normal)
0140c600 4475 (ps-font 'ps-font-for-header 'bold))))
d3ab8dac
KH
4476 "\n%%+ font ")
4477 "\n%%Pages: (atend)\n%%Requirements:"
4478 (if ps-spool-duplex " duplex\n" "\n"))
4479
4480 (let ((comments (if (functionp ps-print-prologue-header)
4481 (funcall ps-print-prologue-header)
4482 ps-print-prologue-header)))
4483 (and (stringp comments)
4484 (ps-output comments)))
4485
4486 (ps-output "%%EndComments\n\n%%BeginPrologue\n\n")
12d89a2e 4487
bcc0d457 4488 (ps-output-boolean "LandscapeMode" ps-landscape-mode)
8bd22fcf 4489 (ps-output (format "/NumberOfColumns %d def\n" ps-number-of-columns)
12d89a2e 4490
8bd22fcf
KH
4491 (format "/LandscapePageHeight %s def\n" ps-landscape-page-height)
4492 (format "/PrintPageWidth %s def\n"
87a16a06
RS
4493 (- (* (+ ps-print-width ps-inter-column)
4494 ps-number-of-columns)
8bd22fcf
KH
4495 ps-inter-column))
4496 (format "/PrintWidth %s def\n" ps-print-width)
4497 (format "/PrintHeight %s def\n" ps-print-height)
12d89a2e 4498
8bd22fcf
KH
4499 (format "/LeftMargin %s def\n" ps-left-margin)
4500 (format "/RightMargin %s def\n" ps-right-margin) ; not used
4501 (format "/InterColumn %s def\n" ps-inter-column)
bcc0d457 4502
8bd22fcf
KH
4503 (format "/BottomMargin %s def\n" ps-bottom-margin)
4504 (format "/TopMargin %s def\n" ps-top-margin) ; not used
4505 (format "/HeaderOffset %s def\n" ps-header-offset)
4506 (format "/HeaderPad %s def\n" ps-header-pad))
06fb6aab 4507
12b88fff
RS
4508 (ps-output-boolean "PrintHeader" ps-print-header)
4509 (ps-output-boolean "PrintOnlyOneHeader" ps-print-only-one-header)
4510 (ps-output-boolean "PrintHeaderFrame" ps-print-header-frame)
4511 (ps-output-boolean "ShowNofN" ps-show-n-of-n)
4512 (ps-output-boolean "Duplex" ps-spool-duplex)
bcc0d457 4513
12b88fff
RS
4514 (let ((line-height (ps-line-height 'ps-font-for-text)))
4515 (ps-output (format "/LineHeight %s def\n" line-height)
4516 (format "/LinesPerColumn %d def\n"
4517 (round (/ (+ ps-print-height
4518 (* line-height 0.45))
4519 line-height)))))
87a16a06 4520
535efc38 4521 (ps-output-boolean "Zebra" ps-zebra-stripes)
87a16a06 4522 (ps-output-boolean "PrintLineNumber" ps-line-number)
7da17ab6 4523 (ps-output (format "/ZebraHeight %d def\n" ps-zebra-stripe-height))
87a16a06
RS
4524
4525 (ps-background-text)
4526 (ps-background-image)
4527 (setq ps-background-all-pages (nreverse ps-background-all-pages)
4528 ps-background-pages (nreverse ps-background-pages))
12d89a2e 4529
bcc0d457 4530 (ps-output ps-print-prologue-1)
12d89a2e 4531
87a16a06
RS
4532 (ps-output "/printGlobalBackground {\n")
4533 (ps-output-list ps-background-all-pages)
4534 (ps-output "} def\n/printLocalBackground {\n} def\n")
4535
bcc0d457 4536 ;; Header fonts
f8c70761 4537 (ps-output (format "/h0 %s (%s) cvn DefFont\n" ; /h0 14 /Helvetica-Bold DefFont
12b88fff
RS
4538 ps-header-title-font-size (ps-font 'ps-font-for-header
4539 'bold))
f8c70761 4540 (format "/h1 %s (%s) cvn DefFont\n" ; /h1 12 /Helvetica DefFont
12b88fff
RS
4541 ps-header-font-size (ps-font 'ps-font-for-header
4542 'normal)))
bcc0d457
RS
4543
4544 (ps-output ps-print-prologue-2)
4545
4546 ;; Text fonts
7ae35a2f 4547 (let ((font (ps-font-alist 'ps-font-for-text))
6bdb808e
RS
4548 (i 0))
4549 (while font
f8c70761 4550 (ps-output (format "/f%d %s (%s) cvn DefFont\n"
6bdb808e
RS
4551 i
4552 ps-font-size
4553 (ps-font 'ps-font-for-text (car (car font)))))
4554 (setq font (cdr font)
4555 i (1+ i))))
bcc0d457 4556
be415ea7
KH
4557 (let ((font-entry (cdr (assq ps-font-family ps-font-info-database))))
4558 (ps-output (format "/SpaceWidthRatio %f def\n"
4559 (/ (ps-lookup 'space-width) (ps-lookup 'size)))))
4560
e65df0a1
KH
4561 (ps-mule-initialize)
4562
9bf0a587 4563 (ps-output "%%EndPrologue\n%%BeginSetup\nBeginDoc\n%%EndSetup\n\n"))
ef2cbb24 4564
12d89a2e
RS
4565(defun ps-header-dirpart ()
4566 (let ((fname (buffer-file-name)))
4567 (if fname
4568 (if (string-equal (buffer-name) (file-name-nondirectory fname))
4569 (file-name-directory fname)
4570 fname)
4571 "")))
ef2cbb24 4572
12d89a2e 4573(defun ps-get-buffer-name ()
bcc0d457
RS
4574 (cond
4575 ;; Indulge Jim this little easter egg:
4576 ((string= (buffer-name) "ps-print.el")
4577 "Hey, Cool! It's ps-print.el!!!")
4578 ;; Indulge Jack this other little easter egg:
4579 ((string= (buffer-name) "sokoban.el")
4580 "Super! C'est sokoban.el!")
87a16a06 4581 (t (concat
a18ed129 4582 (and ps-printing-region "Subset of: ")
87a16a06
RS
4583 (buffer-name)
4584 (and (buffer-modified-p) " (unsaved)")))))
ef2cbb24 4585
12d89a2e 4586(defun ps-begin-job ()
7da17ab6
RS
4587 (save-excursion
4588 (set-buffer ps-spool-buffer)
4589 (goto-char (point-max))
4590 (and (re-search-backward "^%%Trailer$" nil t)
4591 (delete-region (match-beginning 0) (point-max))))
4592 (setq ps-showline-count (if ps-printing-region (car ps-printing-region) 1)
4593 ps-page-count 0
857686a6 4594 ps-control-or-escape-regexp
e65df0a1
KH
4595 (if ps-mule-charset-list
4596 (cond ((eq ps-print-control-characters '8-bit)
4597 "[^\040-\176]")
4598 ((eq ps-print-control-characters 'control-8-bit)
4599 (string-as-multibyte "[^\040-\176\240-\377]"))
4600 ((eq ps-print-control-characters 'control)
4601 (string-as-multibyte "[^\040-\176\200-\377]"))
4602 (t (string-as-multibyte "[^\000-\011\013\015-\377")))
4603 (cond ((eq ps-print-control-characters '8-bit)
4604 (string-as-unibyte "[\000-\037\177-\377]"))
4605 ((eq ps-print-control-characters 'control-8-bit)
4606 (string-as-unibyte "[\000-\037\177-\237]"))
4607 ((eq ps-print-control-characters 'control)
4608 "[\000-\037\177]")
4609 (t "[\t\n\f]")))))
ef2cbb24 4610
12b88fff
RS
4611(defmacro ps-page-number ()
4612 `(1+ (/ (1- ps-page-count) ps-number-of-columns)))
4613
ef2cbb24 4614(defun ps-end-file ()
984e7bd9 4615 (ps-output "\n%%Trailer\n%%Pages: "
7da17ab6 4616 (format "%d" ps-page-postscript)
984e7bd9 4617 "\n\nEndDoc\n\n%%EOF\n"))
87a16a06
RS
4618
4619
ef2cbb24
RS
4620(defun ps-next-page ()
4621 (ps-end-page)
12d89a2e
RS
4622 (ps-flush-output)
4623 (ps-begin-page))
4624
12b88fff 4625(defun ps-header-page ()
7da17ab6
RS
4626 ;; set total line and page number when printing has finished
4627 ;; (see `ps-generate')
12b88fff
RS
4628 (if (prog1
4629 (zerop (mod ps-page-count ps-number-of-columns))
6bdb808e 4630 (setq ps-page-count (1+ ps-page-count)))
a18ed129 4631 ;; Print only when a new real page begins.
7da17ab6
RS
4632 (progn
4633 (setq ps-page-postscript (1+ ps-page-postscript))
4634 (ps-output (format "\n%%%%Page: %d %d\n"
4635 ps-page-postscript ps-page-postscript))
4636 (ps-output "/Lines 0 def\n/PageCount 0 def\nBeginDSCPage\n")
4637 (ps-background ps-page-postscript)
12b88fff 4638 (run-hooks 'ps-print-begin-page-hook))
a18ed129 4639 ;; Print when any other page begins.
7da17ab6 4640 (ps-output "/Lines 0 def\n/PageCount 0 def\nBeginDSCPage\n")
12b88fff 4641 (run-hooks 'ps-print-begin-column-hook)))
a18ed129 4642
8bd22fcf 4643(defun ps-begin-page ()
12d89a2e 4644 (ps-get-page-dimensions)
8bd22fcf 4645 (setq ps-width-remaining ps-print-width
e65df0a1
KH
4646 ps-height-remaining ps-print-height
4647 ps-mule-current-charset 'ascii)
12d89a2e 4648
a18ed129 4649 (ps-header-page)
12d89a2e 4650
87a16a06 4651 (ps-output (format "/LineNumber %d def\n" ps-showline-count)
12b88fff
RS
4652 (format "/PageNumber %d def\n" (if ps-print-only-one-header
4653 (ps-page-number)
4654 ps-page-count)))
12d89a2e 4655
090be653
RS
4656 (when ps-print-header
4657 (ps-generate-header "HeaderLinesLeft" ps-left-header)
4658 (ps-generate-header "HeaderLinesRight" ps-right-header)
4659 (ps-output (format "%d SetHeaderLines\n" ps-header-lines)))
12d89a2e
RS
4660
4661 (ps-output "BeginPage\n")
87a16a06
RS
4662 (ps-set-font ps-current-font)
4663 (ps-set-bg ps-current-bg)
4664 (ps-set-color ps-current-color))
ef2cbb24
RS
4665
4666(defun ps-end-page ()
a18ed129 4667 (ps-output "EndPage\nEndDSCPage\n"))
12d89a2e
RS
4668
4669(defun ps-dummy-page ()
12b88fff 4670 (ps-header-page)
a18ed129 4671 (ps-output "/PrintHeader false def
12d89a2e
RS
4672BeginPage
4673EndPage
4674EndDSCPage\n"))
06fb6aab 4675
ef2cbb24 4676(defun ps-next-line ()
87a16a06 4677 (setq ps-showline-count (1+ ps-showline-count))
12b88fff
RS
4678 (let ((lh (ps-line-height 'ps-font-for-text)))
4679 (if (< ps-height-remaining lh)
4680 (ps-next-page)
4681 (setq ps-width-remaining ps-print-width
4682 ps-height-remaining (- ps-height-remaining lh))
4683 (ps-output "HL\n"))))
ef2cbb24
RS
4684
4685(defun ps-continue-line ()
12b88fff
RS
4686 (let ((lh (ps-line-height 'ps-font-for-text)))
4687 (if (< ps-height-remaining lh)
4688 (ps-next-page)
4689 (setq ps-width-remaining ps-print-width
4690 ps-height-remaining (- ps-height-remaining lh))
4691 (ps-output "SL\n"))))
12d89a2e
RS
4692
4693(defun ps-find-wrappoint (from to char-width)
4694 (let ((avail (truncate (/ ps-width-remaining char-width)))
4695 (todo (- to from)))
4696 (if (< todo avail)
4697 (cons to (* todo char-width))
4698 (cons (+ from avail) ps-width-remaining))))
4699
4700(defun ps-basic-plot-string (from to &optional bg-color)
12b88fff
RS
4701 (let* ((wrappoint (ps-find-wrappoint from to
4702 (ps-avg-char-width 'ps-font-for-text)))
12d89a2e 4703 (to (car wrappoint))
e65df0a1
KH
4704 (string (buffer-substring-no-properties from to))
4705 (font-spec
4706 (ps-mule-get-font-spec
4707 'ascii
4708 (car (nth ps-current-font (ps-font-alist 'ps-font-for-text))))))
4709 (and font-spec
4710 (ps-mule-prepare-font font-spec string 'ascii))
12d89a2e 4711 (ps-output-string string)
bcc0d457 4712 (ps-output " S\n")
12d89a2e
RS
4713 wrappoint))
4714
4715(defun ps-basic-plot-whitespace (from to &optional bg-color)
12b88fff
RS
4716 (let* ((wrappoint (ps-find-wrappoint from to
4717 (ps-space-width 'ps-font-for-text)))
12d89a2e 4718 (to (car wrappoint)))
12d89a2e
RS
4719 (ps-output (format "%d W\n" (- to from)))
4720 wrappoint))
4721
4722(defun ps-plot (plotfunc from to &optional bg-color)
ef2cbb24 4723 (while (< from to)
12d89a2e
RS
4724 (let* ((wrappoint (funcall plotfunc from to bg-color))
4725 (plotted-to (car wrappoint))
4726 (plotted-width (cdr wrappoint)))
8bd22fcf
KH
4727 (setq from plotted-to
4728 ps-width-remaining (- ps-width-remaining plotted-width))
12d89a2e
RS
4729 (if (< from to)
4730 (ps-continue-line))))
ef2cbb24
RS
4731 (if ps-razzle-dazzle
4732 (let* ((q-todo (- (point-max) (point-min)))
12d89a2e 4733 (q-done (- (point) (point-min)))
ef2cbb24 4734 (chunkfrac (/ q-todo 8))
857686a6 4735 (chunksize (min chunkfrac 1000)))
ef2cbb24 4736 (if (> (- q-done ps-razchunk) chunksize)
8bd22fcf 4737 (progn
ef2cbb24 4738 (setq ps-razchunk q-done)
8bd22fcf
KH
4739 (message "Formatting...%3d%%"
4740 (if (< q-todo 100)
4741 (/ (* 100 q-done) q-todo)
4742 (/ q-done (/ q-todo 100)))
4743 ))))))
12d89a2e
RS
4744
4745(defun ps-set-font (font)
e65df0a1
KH
4746 (setq ps-last-font (format "f%d" (setq ps-current-font font)))
4747 (ps-output (format "/%s F\n" ps-last-font)))
12d89a2e 4748
12d89a2e
RS
4749(defun ps-set-bg (color)
4750 (if (setq ps-current-bg color)
8bd22fcf
KH
4751 (ps-output (format ps-color-format
4752 (nth 0 color) (nth 1 color) (nth 2 color))
12d89a2e
RS
4753 " true BG\n")
4754 (ps-output "false BG\n")))
4755
4756(defun ps-set-color (color)
a18ed129 4757 (setq ps-current-color (or color ps-default-fg))
8bd22fcf
KH
4758 (ps-output (format ps-color-format
4759 (nth 0 ps-current-color)
043620f4
KH
4760 (nth 1 ps-current-color) (nth 2 ps-current-color))
4761 " FG\n"))
12d89a2e 4762
12d89a2e 4763
87a16a06 4764(defvar ps-current-effect 0)
12d89a2e 4765
87a16a06
RS
4766
4767(defun ps-plot-region (from to font &optional fg-color bg-color effects)
12d89a2e
RS
4768 (if (not (equal font ps-current-font))
4769 (ps-set-font font))
06fb6aab 4770
12d89a2e
RS
4771 ;; Specify a foreground color only if one's specified and it's
4772 ;; different than the current.
4773 (if (not (equal fg-color ps-current-color))
4774 (ps-set-color fg-color))
06fb6aab 4775
12d89a2e
RS
4776 (if (not (equal bg-color ps-current-bg))
4777 (ps-set-bg bg-color))
06fb6aab 4778
87a16a06
RS
4779 ;; Specify effects (underline, overline, box, etc)
4780 (cond
4781 ((not (integerp effects))
4782 (ps-output "0 EF\n")
4783 (setq ps-current-effect 0))
4784 ((/= effects ps-current-effect)
4785 (ps-output (number-to-string effects) " EF\n")
4786 (setq ps-current-effect effects)))
ef2cbb24 4787
e65df0a1
KH
4788 (setq ps-mule-current-charset 'ascii)
4789
12d89a2e 4790 ;; Starting at the beginning of the specified region...
ef2cbb24
RS
4791 (save-excursion
4792 (goto-char from)
12d89a2e
RS
4793
4794 ;; ...break the region up into chunks separated by tabs, linefeeds,
87a16a06 4795 ;; pagefeeds, control characters, and plot each chunk.
ef2cbb24 4796 (while (< from to)
857686a6 4797 (if (re-search-forward ps-control-or-escape-regexp to t)
024ced4d 4798 ;; region with some control characters or some multi-byte characters
12b88fff
RS
4799 (let* ((match-point (match-beginning 0))
4800 (match (char-after match-point)))
e65df0a1
KH
4801 (when (< from match-point)
4802 (unless (eq ps-mule-current-charset 'ascii)
4803 (ps-set-font ps-current-font)
4804 (setq ps-mule-current-charset 'ascii))
4805 (ps-plot 'ps-basic-plot-string from match-point bg-color))
857686a6
RS
4806 (cond
4807 ((= match ?\t) ; tab
be415ea7 4808 (let ((linestart (line-beginning-position)))
857686a6
RS
4809 (forward-char -1)
4810 (setq from (+ linestart (current-column)))
e65df0a1
KH
4811 (when (re-search-forward "[ \t]+" to t)
4812 (unless (eq ps-mule-current-charset 'ascii)
4813 (ps-set-font ps-current-font)
4814 (setq ps-mule-current-charset 'ascii))
4815 (ps-plot 'ps-basic-plot-whitespace
4816 from (+ linestart (current-column))
4817 bg-color))))
857686a6
RS
4818
4819 ((= match ?\n) ; newline
4820 (ps-next-line))
4821
4822 ((= match ?\f) ; form feed
12b88fff
RS
4823 ;; do not skip page if previous character is NEWLINE and
4824 ;; it is a beginning of page.
4825 (or (and (= (char-after (1- match-point)) ?\n)
4826 (= ps-height-remaining ps-print-height))
4827 (ps-next-page)))
e65df0a1 4828
024ced4d 4829 ((> match 255) ; a multi-byte character
e65df0a1
KH
4830 (let ((charset (char-charset match)))
4831 (or (eq charset 'composition)
4832 (ps-mule-skip-same-charset charset))
4833 (setq ps-mule-current-charset charset)
4834 (ps-plot 'ps-mule-plot-string match-point (point) bg-color)))
857686a6
RS
4835 ; characters from ^@ to ^_ and
4836 (t ; characters from 127 to 255
4837 (ps-control-character match)))
87a16a06 4838 (setq from (point)))
024ced4d 4839 ;; region without control characters nor multi-byte characters
e65df0a1
KH
4840 (when (not (eq ps-mule-current-charset 'ascii))
4841 (ps-set-font ps-current-font)
4842 (setq ps-mule-current-charset 'ascii))
87a16a06
RS
4843 (ps-plot 'ps-basic-plot-string from to bg-color)
4844 (setq from to)))))
4845
857686a6
RS
4846(defvar ps-string-control-codes
4847 (let ((table (make-vector 256 nil))
4848 (char ?\000))
4849 ;; control character
4850 (while (<= char ?\037)
4851 (aset table char (format "^%c" (+ char ?@)))
4852 (setq char (1+ char)))
4853 ;; printable character
4854 (while (< char ?\177)
4855 (aset table char (format "%c" char))
4856 (setq char (1+ char)))
4857 ;; DEL
4858 (aset table char "^?")
4859 ;; 8-bit character
4860 (while (<= (setq char (1+ char)) ?\377)
4861 (aset table char (format "\\%o" char)))
4862 table)
4863 "Vector used to map characters to a printable string.")
4864
4865(defun ps-control-character (char)
4866 (let* ((str (aref ps-string-control-codes char))
4867 (from (1- (point)))
87a16a06
RS
4868 (len (length str))
4869 (to (+ from len))
12b88fff
RS
4870 (char-width (ps-avg-char-width 'ps-font-for-text))
4871 (wrappoint (ps-find-wrappoint from to char-width)))
87a16a06
RS
4872 (if (< (car wrappoint) to)
4873 (ps-continue-line))
12b88fff 4874 (setq ps-width-remaining (- ps-width-remaining (* len char-width)))
87a16a06
RS
4875 (ps-output-string str)
4876 (ps-output " S\n")))
ef2cbb24 4877
12d89a2e
RS
4878(defun ps-color-value (x-color-value)
4879 ;; Scale 16-bit X-COLOR-VALUE to PostScript color value in [0, 1] interval.
4880 (/ x-color-value ps-print-color-scale))
ef2cbb24 4881
d3ab8dac
KH
4882
4883(cond ((eq ps-print-emacs-type 'emacs) ; emacs
4884
4885 (defun ps-color-values (x-color)
4886 (if (fboundp 'x-color-values)
4887 (x-color-values x-color)
4888 (error "No available function to determine X color values.")))
4889 )
4890 ; xemacs
4891 ; lucid
4892 (t ; epoch
4893 (defun ps-color-values (x-color)
4894 (cond ((fboundp 'x-color-values)
4895 (x-color-values x-color))
4896 ((and (fboundp 'color-instance-rgb-components)
4897 (ps-color-device))
4898 (color-instance-rgb-components
4899 (if (color-instance-p x-color)
4900 x-color
4901 (make-color-instance
4902 (if (color-specifier-p x-color)
4903 (color-name x-color)
4904 x-color)))))
4905 (t (error "No available function to determine X color values."))))
4906 ))
043620f4 4907
87a16a06 4908
a18ed129
RS
4909(defun ps-face-attributes (face)
4910 "Return face attribute vector.
87a16a06 4911
a18ed129
RS
4912If FACE is not in `ps-print-face-extension-alist' or in
4913`ps-print-face-alist', insert it on `ps-print-face-alist' and
4914return the attribute vector.
87a16a06
RS
4915
4916If FACE is not a valid face name, it is used default face."
a18ed129
RS
4917 (cdr (or (assq face ps-print-face-extension-alist)
4918 (assq face ps-print-face-alist)
4919 (let* ((the-face (if (facep face) face 'default))
4920 (new-face (ps-screen-to-bit-face the-face)))
4921 (or (and (eq the-face 'default)
4922 (assq the-face ps-print-face-alist))
4923 (setq ps-print-face-alist (cons new-face ps-print-face-alist)))
4924 new-face))))
87a16a06 4925
043620f4
KH
4926
4927(defun ps-face-attribute-list (face-or-list)
4928 (if (listp face-or-list)
87a16a06 4929 ;; list of faces
857686a6
RS
4930 (let ((effects 0)
4931 foreground background face-attr)
043620f4 4932 (while face-or-list
857686a6 4933 (setq face-attr (ps-face-attributes (car face-or-list))
87a16a06
RS
4934 effects (logior effects (aref face-attr 0)))
4935 (or foreground (setq foreground (aref face-attr 1)))
4936 (or background (setq background (aref face-attr 2)))
043620f4 4937 (setq face-or-list (cdr face-or-list)))
87a16a06
RS
4938 (vector effects foreground background))
4939 ;; simple face
043620f4
KH
4940 (ps-face-attributes face-or-list)))
4941
87a16a06 4942
12b88fff
RS
4943(defconst ps-font-type (vector nil 'bold 'italic 'bold-italic))
4944
4945
12d89a2e 4946(defun ps-plot-with-face (from to face)
12b88fff
RS
4947 (cond
4948 ((null face) ; print text with null face
87a16a06 4949 (ps-plot-region from to 0))
12b88fff
RS
4950 ((eq face 'emacs--invisible--face)) ; skip invisible text!!!
4951 (t ; otherwise, text has a valid face
4952 (let* ((face-bit (ps-face-attribute-list face))
4953 (effect (aref face-bit 0))
4954 (foreground (aref face-bit 1))
4955 (background (aref face-bit 2))
d3ab8dac 4956 (fg-color (if (and ps-color-p foreground)
12b88fff
RS
4957 (mapcar 'ps-color-value
4958 (ps-color-values foreground))
4959 ps-default-color))
d3ab8dac 4960 (bg-color (and ps-color-p background
12b88fff
RS
4961 (mapcar 'ps-color-value
4962 (ps-color-values background)))))
4963 (ps-plot-region
4964 from to
4965 (ps-font-number 'ps-font-for-text
4966 (or (aref ps-font-type (logand effect 3))
4967 face))
4968 fg-color bg-color (lsh effect -2)))))
87a16a06 4969 (goto-char to))
12d89a2e
RS
4970
4971
e65df0a1
KH
4972(cond ((eq ps-print-emacs-type 'emacs) ; emacs
4973
4974 (defun ps-face-bold-p (face)
4975 (or (face-bold-p face)
4976 (memq face ps-bold-faces)))
4977
4978 (defun ps-face-italic-p (face)
4979 (or (face-italic-p face)
4980 (memq face ps-italic-faces)))
4981 )
4982 ; xemacs
4983 ; lucid
e65df0a1 4984 (t ; epoch
d3ab8dac
KH
4985 (defun ps-xemacs-face-kind-p (face kind kind-regex kind-list)
4986 (let* ((frame-font (or (face-font-instance face)
4987 (face-font-instance 'default)))
4988 (kind-cons (and frame-font
4989 (assq kind
4990 (font-instance-properties frame-font))))
4991 (kind-spec (cdr-safe kind-cons))
4992 (case-fold-search t))
4993 (or (and kind-spec (string-match kind-regex kind-spec))
4994 ;; Kludge-compatible:
4995 (memq face kind-list))))
4996
e65df0a1 4997 (defun ps-face-bold-p (face)
d3ab8dac
KH
4998 (ps-xemacs-face-kind-p face 'WEIGHT_NAME "bold\\|demibold"
4999 ps-bold-faces))
e65df0a1
KH
5000
5001 (defun ps-face-italic-p (face)
5002 (or (ps-xemacs-face-kind-p face 'ANGLE_NAME "i\\|o" ps-italic-faces)
5003 (ps-xemacs-face-kind-p face 'SLANT "i\\|o" ps-italic-faces)))
5004 ))
5005
12d89a2e
RS
5006
5007(defun ps-face-underlined-p (face)
5008 (or (face-underline-p face)
5009 (memq face ps-underlined-faces)))
5010
a18ed129 5011
043620f4
KH
5012;; Ensure that face-list is fbound.
5013(or (fboundp 'face-list) (defalias 'face-list 'list-faces))
12d89a2e 5014
a18ed129 5015
12d89a2e 5016(defun ps-build-reference-face-lists ()
857686a6
RS
5017 ;; Ensure that face database is updated with faces on
5018 ;; `font-lock-face-attributes' (obsolete stuff)
5019 (ps-font-lock-face-attributes)
5020 ;; Now, rebuild reference face lists
a18ed129 5021 (setq ps-print-face-alist nil)
12d89a2e 5022 (if ps-auto-font-detect
a18ed129
RS
5023 (mapcar 'ps-map-face (face-list))
5024 (mapcar 'ps-set-face-bold ps-bold-faces)
5025 (mapcar 'ps-set-face-italic ps-italic-faces)
5026 (mapcar 'ps-set-face-underline ps-underlined-faces))
12d89a2e 5027 (setq ps-build-face-reference nil))
ef2cbb24 5028
a18ed129
RS
5029
5030(defun ps-set-face-bold (face)
5031 (ps-set-face-attribute face 1))
5032
5033(defun ps-set-face-italic (face)
5034 (ps-set-face-attribute face 2))
5035
5036(defun ps-set-face-underline (face)
5037 (ps-set-face-attribute face 4))
5038
5039
5040(defun ps-set-face-attribute (face effect)
5041 (let ((face-bit (cdr (ps-map-face face))))
5042 (aset face-bit 0 (logior (aref face-bit 0) effect))))
5043
5044
5045(defun ps-map-face (face)
5046 (let* ((face-map (ps-screen-to-bit-face face))
5047 (ps-face-bit (cdr (assq (car face-map) ps-print-face-alist))))
5048 (if ps-face-bit
5049 ;; if face exists, merge both
5050 (let ((face-bit (cdr face-map)))
5051 (aset ps-face-bit 0 (logior (aref ps-face-bit 0) (aref face-bit 0)))
5052 (or (aref ps-face-bit 1) (aset ps-face-bit 1 (aref face-bit 1)))
5053 (or (aref ps-face-bit 2) (aset ps-face-bit 2 (aref face-bit 2))))
5054 ;; if face does not exist, insert it
5055 (setq ps-print-face-alist (cons face-map ps-print-face-alist)))
5056 face-map))
5057
5058
5059(defun ps-screen-to-bit-face (face)
5060 (cons face
5061 (vector (logior (if (ps-face-bold-p face) 1 0) ; bold
5062 (if (ps-face-italic-p face) 2 0) ; italic
5063 (if (ps-face-underlined-p face) 4 0)) ; underline
5064 (face-foreground face)
5065 (face-background face))))
5066
5067
d3ab8dac
KH
5068(cond ((not (eq ps-print-emacs-type 'emacs))
5069 ; xemacs
5070 ; lucid
5071 ; epoch
5072 (defun ps-mapper (extent list)
5073 (nconc list (list (list (extent-start-position extent) 'push extent)
5074 (list (extent-end-position extent) 'pull extent)))
5075 nil)
5076
5077 (defun ps-extent-sorter (a b)
5078 (< (extent-priority a) (extent-priority b)))
5079 ))
ef2cbb24 5080
043620f4
KH
5081
5082(defun ps-print-ensure-fontified (start end)
857686a6 5083 (and (boundp 'lazy-lock-mode) (symbol-value 'lazy-lock-mode)
d3ab8dac 5084 (lazy-lock-fontify-region start end)))
043620f4 5085
ef2cbb24 5086(defun ps-generate-postscript-with-faces (from to)
87a16a06 5087 ;; Some initialization...
857686a6 5088 (setq ps-current-effect 0)
87a16a06 5089
00aa16af 5090 ;; Build the reference lists of faces if necessary.
12d89a2e
RS
5091 (if (or ps-always-build-face-reference
5092 ps-build-face-reference)
5093 (progn
5094 (message "Collecting face information...")
5095 (ps-build-reference-face-lists)))
00aa16af
RS
5096 ;; Set the color scale. We do it here instead of in the defvar so
5097 ;; that ps-print can be dumped into emacs. This expression can't be
5098 ;; evaluated at dump-time because X isn't initialized.
d3ab8dac
KH
5099 (setq ps-color-p (and ps-print-color-p (ps-color-device))
5100 ps-print-color-scale (if ps-color-p
5101 (float (car (ps-color-values "white")))
5102 1.0))
00aa16af 5103 ;; Generate some PostScript.
ef2cbb24
RS
5104 (save-restriction
5105 (narrow-to-region from to)
d3ab8dac 5106 (ps-print-ensure-fontified from to)
12d89a2e
RS
5107 (let ((face 'default)
5108 (position to))
87a16a06
RS
5109 (cond
5110 ((or (eq ps-print-emacs-type 'lucid)
5111 (eq ps-print-emacs-type 'xemacs))
5112 ;; Build the list of extents...
5113 (let ((a (cons 'dummy nil))
5114 record type extent extent-list)
5115 (map-extents 'ps-mapper nil from to a)
8bd22fcf
KH
5116 (setq a (sort (cdr a) 'car-less-than-car)
5117 extent-list nil)
87a16a06
RS
5118
5119 ;; Loop through the extents...
5120 (while a
8bd22fcf 5121 (setq record (car a)
87a16a06 5122
8bd22fcf
KH
5123 position (car record)
5124 record (cdr record)
87a16a06 5125
8bd22fcf
KH
5126 type (car record)
5127 record (cdr record)
87a16a06 5128
8bd22fcf 5129 extent (car record))
87a16a06
RS
5130
5131 ;; Plot up to this record.
5132 ;; XEmacs 19.12: for some reason, we're getting into a
5133 ;; situation in which some of the records have
5134 ;; positions less than 'from'. Since we've narrowed
5135 ;; the buffer, this'll generate errors. This is a
5136 ;; hack, but don't call ps-plot-with-face unless from >
5137 ;; point-min.
8bd22fcf
KH
5138 (and (>= from (point-min)) (<= position (point-max))
5139 (ps-plot-with-face from position face))
87a16a06
RS
5140
5141 (cond
5142 ((eq type 'push)
d3ab8dac
KH
5143 (and (extent-face extent)
5144 (setq extent-list (sort (cons extent extent-list)
5145 'ps-extent-sorter))))
87a16a06
RS
5146
5147 ((eq type 'pull)
5148 (setq extent-list (sort (delq extent extent-list)
5149 'ps-extent-sorter))))
5150
d3ab8dac
KH
5151 (setq face (if extent-list
5152 (extent-face (car extent-list))
5153 'default)
8bd22fcf
KH
5154 from position
5155 a (cdr a)))))
87a16a06
RS
5156
5157 ((eq ps-print-emacs-type 'emacs)
5158 (let ((property-change from)
e65df0a1
KH
5159 (overlay-change from)
5160 (save-buffer-invisibility-spec buffer-invisibility-spec)
c82b4a75 5161 (buffer-invisibility-spec nil))
87a16a06 5162 (while (< from to)
d3ab8dac 5163 (and (< property-change to) ; Don't search for property change
12d89a2e 5164 ; unless previous search succeeded.
d3ab8dac
KH
5165 (setq property-change (next-property-change from nil to)))
5166 (and (< overlay-change to) ; Don't search for overlay change
12d89a2e 5167 ; unless previous search succeeded.
d3ab8dac
KH
5168 (setq overlay-change (min (next-overlay-change from) to)))
5169 (setq position (min property-change overlay-change))
87a16a06
RS
5170 ;; The code below is not quite correct,
5171 ;; because a non-nil overlay invisible property
5172 ;; which is inactive according to the current value
5173 ;; of buffer-invisibility-spec nonetheless overrides
5174 ;; a face text property.
5175 (setq face
5176 (cond ((let ((prop (get-text-property from 'invisible)))
5177 ;; Decide whether this invisible property
5178 ;; really makes the text invisible.
e65df0a1 5179 (if (eq save-buffer-invisibility-spec t)
87a16a06 5180 (not (null prop))
e65df0a1
KH
5181 (or (memq prop save-buffer-invisibility-spec)
5182 (assq prop save-buffer-invisibility-spec))))
12b88fff 5183 'emacs--invisible--face)
87a16a06
RS
5184 ((get-text-property from 'face))
5185 (t 'default)))
5186 (let ((overlays (overlays-at from))
5187 (face-priority -1)) ; text-property
d3ab8dac
KH
5188 (while (and overlays
5189 (not (eq face 'emacs--invisible--face)))
87a16a06 5190 (let* ((overlay (car overlays))
87a16a06 5191 (overlay-invisible (overlay-get overlay 'invisible))
d3ab8dac 5192 (overlay-priority (or (overlay-get overlay 'priority)
87a16a06 5193 0)))
d3ab8dac 5194 (and (> overlay-priority face-priority)
e65df0a1
KH
5195 (setq face
5196 (cond ((if (eq save-buffer-invisibility-spec t)
5197 (not (null overlay-invisible))
5198 (or (memq overlay-invisible
5199 save-buffer-invisibility-spec)
5200 (assq overlay-invisible
5201 save-buffer-invisibility-spec)))
c82b4a75 5202 'emacs--invisible--face)
d3ab8dac
KH
5203 ((overlay-get overlay 'face))
5204 (t face))
8bd22fcf 5205 face-priority overlay-priority)))
87a16a06
RS
5206 (setq overlays (cdr overlays))))
5207 ;; Plot up to this record.
5208 (ps-plot-with-face from position face)
5209 (setq from position)))))
5210 (ps-plot-with-face from to face))))
ef2cbb24
RS
5211
5212(defun ps-generate-postscript (from to)
12d89a2e 5213 (ps-plot-region from to 0 nil))
ef2cbb24
RS
5214
5215(defun ps-generate (buffer from to genfunc)
87a16a06
RS
5216 (save-excursion
5217 (let ((from (min to from))
5218 (to (max to from))
5219 ;; This avoids trouble if chars with read-only properties
5220 ;; are copied into ps-spool-buffer.
5221 (inhibit-read-only t))
5222 (save-restriction
5223 (narrow-to-region from to)
857686a6
RS
5224 (and ps-razzle-dazzle
5225 (message "Formatting...%3d%%" (setq ps-razchunk 0)))
8bd22fcf
KH
5226 (setq ps-source-buffer buffer
5227 ps-spool-buffer (get-buffer-create ps-spool-buffer-name))
87a16a06
RS
5228 (ps-init-output-queue)
5229 (let (safe-marker completed-safely needs-begin-file)
5230 (unwind-protect
00aa16af
RS
5231 (progn
5232 (set-buffer ps-spool-buffer)
7da17ab6 5233 (set-buffer-multibyte nil)
915293a2 5234
87a16a06
RS
5235 ;; Get a marker and make it point to the current end of the
5236 ;; buffer, If an error occurs, we'll delete everything from
5237 ;; the end of this marker onwards.
5238 (setq safe-marker (make-marker))
5239 (set-marker safe-marker (point-max))
5240
5241 (goto-char (point-min))
8bd22fcf
KH
5242 (or (looking-at (regexp-quote ps-adobe-tag))
5243 (setq needs-begin-file t))
87a16a06
RS
5244 (save-excursion
5245 (set-buffer ps-source-buffer)
d3ab8dac 5246 (and needs-begin-file (ps-begin-file))
e65df0a1 5247 (ps-mule-begin from to)
87a16a06
RS
5248 (ps-begin-job)
5249 (ps-begin-page))
5250 (set-buffer ps-source-buffer)
5251 (funcall genfunc from to)
5252 (ps-end-page)
5253
8bd22fcf
KH
5254 (and ps-spool-duplex (= (mod ps-page-count 2) 1)
5255 (ps-dummy-page))
7da17ab6 5256 (ps-end-file)
87a16a06
RS
5257 (ps-flush-output)
5258
5259 ;; Back to the PS output buffer to set the page count
7da17ab6
RS
5260 (let ((total-lines (if ps-printing-region
5261 (cdr ps-printing-region)
5262 (ps-count-lines (point-min) (point-max))))
5263 (total-pages (if ps-print-only-one-header
5264 (ps-page-number)
5265 ps-page-count)))
5266 (set-buffer ps-spool-buffer)
5267 (goto-char (point-min))
5268 (while (re-search-forward "^/Lines 0 def\n/PageCount 0 def$"
5269 nil t)
5270 (replace-match (format "/Lines %d def\n/PageCount %d def"
5271 total-lines total-pages) t)))
87a16a06
RS
5272
5273 ;; Setting this variable tells the unwind form that the
8bd22fcf 5274 ;; the PostScript was generated without error.
87a16a06
RS
5275 (setq completed-safely t))
5276
5277 ;; Unwind form: If some bad mojo occurred while generating
8bd22fcf 5278 ;; PostScript, delete all the PostScript that was generated.
87a16a06
RS
5279 ;; This protects the previously spooled files from getting
5280 ;; corrupted.
8bd22fcf
KH
5281 (and (markerp safe-marker) (not completed-safely)
5282 (progn
5283 (set-buffer ps-spool-buffer)
5284 (delete-region (marker-position safe-marker) (point-max))))))
87a16a06 5285
857686a6 5286 (and ps-razzle-dazzle (message "Formatting...done"))))))
ef2cbb24 5287
e65df0a1 5288
857686a6 5289;; Permit dynamic evaluation at print time of `ps-lpr-switches'.
ef2cbb24 5290(defun ps-do-despool (filename)
12d89a2e 5291 (if (or (not (boundp 'ps-spool-buffer))
bcc0d457 5292 (not (symbol-value 'ps-spool-buffer)))
12d89a2e 5293 (message "No spooled PostScript to print")
ef2cbb24
RS
5294 (if filename
5295 (save-excursion
857686a6 5296 (and ps-razzle-dazzle (message "Saving..."))
12d89a2e 5297 (set-buffer ps-spool-buffer)
ef2cbb24 5298 (setq filename (expand-file-name filename))
7ffaf659
EZ
5299 (let ((coding-system-for-write 'raw-text-unix))
5300 (write-region (point-min) (point-max) filename))
857686a6 5301 (and ps-razzle-dazzle (message "Wrote %s" filename)))
ef2cbb24 5302 ;; Else, spool to the printer
857686a6 5303 (and ps-razzle-dazzle (message "Printing..."))
ef2cbb24 5304 (save-excursion
12d89a2e 5305 (set-buffer ps-spool-buffer)
200127fd
AI
5306 (let* ((coding-system-for-write 'raw-text-unix)
5307 (ps-printer-name (or ps-printer-name printer-name))
5308 (ps-lpr-switches
a9537848
GV
5309 (append
5310 (and (stringp ps-printer-name)
5311 (list (concat "-P" ps-printer-name)))
5312 ps-lpr-switches)))
03820514 5313 (if (and (memq system-type '(ms-dos windows-nt))
a9537848
GV
5314 (or (and (boundp 'dos-ps-printer)
5315 (stringp (symbol-value 'dos-ps-printer)))
5316 (stringp ps-printer-name)))
5317 (let ((printer (or (and (boundp 'dos-ps-printer)
5318 (stringp (symbol-value 'dos-ps-printer))
5319 (symbol-value 'dos-ps-printer))
5320 ps-printer-name))
5321 ;; It seems that we must be careful about the
5322 ;; directory name that gets added by write-region
5323 ;; when using the standard "PRN" or "LPTx" ports.
5324 ;; The call can fail if the directory is on a
5325 ;; network drive.
5326 (safe-dir (or (getenv "windir") (getenv "TMPDIR") "c:/")))
5327 (write-region (point-min) (point-max)
5328 (expand-file-name printer safe-dir) t 0))
62901aee
RS
5329 (apply 'call-process-region
5330 (point-min) (point-max) ps-lpr-command nil
857686a6 5331 (and (fboundp 'start-process) 0)
62901aee 5332 nil
857686a6
RS
5333 (ps-flatten-list ; dynamic evaluation
5334 (mapcar 'ps-eval-switch ps-lpr-switches))))))
5335 (and ps-razzle-dazzle (message "Printing...done")))
12d89a2e
RS
5336 (kill-buffer ps-spool-buffer)))
5337
857686a6
RS
5338;; Dynamic evaluation
5339(defun ps-eval-switch (arg)
5340 (cond ((stringp arg) arg)
5341 ((functionp arg) (apply arg nil))
5342 ((symbolp arg) (symbol-value arg))
5343 ((consp arg) (apply (car arg) (cdr arg)))
5344 (t nil)))
5345
5346;; `ps-flatten-list' is defined here (copied from "message.el" and
5347;; enhanced to handle dotted pairs as well) until we can get some
5348;; sensible autoloads, or `flatten-list' gets put somewhere decent.
5349
5350;; (ps-flatten-list '((a . b) c (d . e) (f g h) i . j))
5351;; => (a b c d e f g h i j)
5352
5353(defun ps-flatten-list (&rest list)
5354 (ps-flatten-list-1 list))
5355
5356(defun ps-flatten-list-1 (list)
5357 (cond ((null list) nil)
5358 ((consp list) (append (ps-flatten-list-1 (car list))
5359 (ps-flatten-list-1 (cdr list))))
5360 (t (list list))))
5361
12d89a2e
RS
5362(defun ps-kill-emacs-check ()
5363 (let (ps-buffer)
8bd22fcf
KH
5364 (and (setq ps-buffer (get-buffer ps-spool-buffer-name))
5365 (buffer-modified-p ps-buffer)
5366 (y-or-n-p "Unprinted PostScript waiting; print now? ")
5367 (ps-despool))
5368 (and (setq ps-buffer (get-buffer ps-spool-buffer-name))
5369 (buffer-modified-p ps-buffer)
5370 (not (yes-or-no-p "Unprinted PostScript waiting; exit anyway? "))
5371 (error "Unprinted PostScript"))))
12d89a2e 5372
d3ab8dac
KH
5373(cond ((fboundp 'add-hook)
5374 (funcall 'add-hook 'kill-emacs-hook 'ps-kill-emacs-check))
5375 (kill-emacs-hook
5376 (message "Won't override existing `kill-emacs-hook'"))
5377 (t
5378 (setq kill-emacs-hook 'ps-kill-emacs-check)))
ef2cbb24 5379
12d89a2e 5380;;; Sample Setup Code:
ef2cbb24 5381
12d89a2e 5382;; This stuff is for anybody that's brave enough to look this far,
87a16a06
RS
5383;; and able to figure out how to use it. It isn't really part of
5384;; ps-print, but I'll leave it here in hopes it might be useful:
ef2cbb24 5385
043620f4
KH
5386;; WARNING!!! The following code is *sample* code only. Don't use it
5387;; unless you understand what it does!
5388
87a16a06
RS
5389(defmacro ps-prsc ()
5390 `(if (eq ps-print-emacs-type 'emacs) [f22] 'f22))
5391(defmacro ps-c-prsc ()
5392 `(if (eq ps-print-emacs-type 'emacs) [C-f22] '(control f22)))
5393(defmacro ps-s-prsc ()
5394 `(if (eq ps-print-emacs-type 'emacs) [S-f22] '(shift f22)))
00aa16af 5395
a18ed129
RS
5396;; A hook to bind to `rmail-mode-hook' to locally bind prsc and set the
5397;; `ps-left-headers' specially for mail messages.
5398(defun ps-rmail-mode-hook ()
5399 (local-set-key (ps-prsc) 'ps-rmail-print-message-from-summary)
5400 (setq ps-header-lines 3
5401 ps-left-header
5402 ;; The left headers will display the message's subject, its
5403 ;; author, and the name of the folder it was in.
5404 '(ps-article-subject ps-article-author buffer-name)))
5405
5406;; See `ps-gnus-print-article-from-summary'. This function does the
5407;; same thing for rmail.
5408(defun ps-rmail-print-message-from-summary ()
5409 (interactive)
5410 (ps-print-message-from-summary 'rmail-summary-buffer "RMAIL"))
5411
5412;; Used in `ps-rmail-print-article-from-summary',
5413;; `ps-gnus-print-article-from-summary' and `ps-vm-print-message-from-summary'.
5414(defun ps-print-message-from-summary (summary-buffer summary-default)
5415 (let ((ps-buf (or (and (boundp summary-buffer)
5416 (symbol-value summary-buffer))
5417 summary-default)))
5418 (and (get-buffer ps-buf)
5419 (save-excursion
5420 (set-buffer ps-buf)
5421 (ps-spool-buffer-with-faces)))))
5422
12d89a2e 5423;; Look in an article or mail message for the Subject: line. To be
87a16a06 5424;; placed in `ps-left-headers'.
12d89a2e 5425(defun ps-article-subject ()
ef2cbb24 5426 (save-excursion
12d89a2e 5427 (goto-char (point-min))
45a870d9 5428 (if (re-search-forward "^Subject:[ \t]+\\(.*\\)$" nil t)
055e7bf2 5429 (buffer-substring-no-properties (match-beginning 1) (match-end 1))
12d89a2e
RS
5430 "Subject ???")))
5431
5432;; Look in an article or mail message for the From: line. Sorta-kinda
5433;; understands RFC-822 addresses and can pull the real name out where
87a16a06 5434;; it's provided. To be placed in `ps-left-headers'.
12d89a2e
RS
5435(defun ps-article-author ()
5436 (save-excursion
5437 (goto-char (point-min))
a97592dd 5438 (if (re-search-forward "^From:[ \t]+\\(.*\\)$" nil t)
87a16a06
RS
5439 (let ((fromstring (buffer-substring-no-properties (match-beginning 1)
5440 (match-end 1))))
12d89a2e
RS
5441 (cond
5442
5443 ;; Try first to match addresses that look like
5444 ;; thompson@wg2.waii.com (Jim Thompson)
5445 ((string-match ".*[ \t]+(\\(.*\\))" fromstring)
5446 (substring fromstring (match-beginning 1) (match-end 1)))
5447
5448 ;; Next try to match addresses that look like
5449 ;; Jim Thompson <thompson@wg2.waii.com>
5450 ((string-match "\\(.*\\)[ \t]+<.*>" fromstring)
5451 (substring fromstring (match-beginning 1) (match-end 1)))
5452
5453 ;; Couldn't find a real name -- show the address instead.
5454 (t fromstring)))
5455 "From ???")))
5456
a18ed129 5457;; A hook to bind to `gnus-article-prepare-hook'. This will set the
87a16a06
RS
5458;; `ps-left-headers' specially for gnus articles. Unfortunately,
5459;; `gnus-article-mode-hook' is called only once, the first time the *Article*
12d89a2e
RS
5460;; buffer enters that mode, so it would only work for the first time
5461;; we ran gnus. The second time, this hook wouldn't get set up. The
87a16a06 5462;; only alternative is `gnus-article-prepare-hook'.
12d89a2e 5463(defun ps-gnus-article-prepare-hook ()
8bd22fcf
KH
5464 (setq ps-header-lines 3
5465 ps-left-header
12d89a2e
RS
5466 ;; The left headers will display the article's subject, its
5467 ;; author, and the newsgroup it was in.
8bd22fcf 5468 '(ps-article-subject ps-article-author gnus-newsgroup-name)))
12d89a2e 5469
a18ed129
RS
5470;; A hook to bind to `vm-mode-hook' to locally bind prsc and set the
5471;; `ps-left-headers' specially for mail messages.
12d89a2e 5472(defun ps-vm-mode-hook ()
00aa16af 5473 (local-set-key (ps-prsc) 'ps-vm-print-message-from-summary)
8bd22fcf
KH
5474 (setq ps-header-lines 3
5475 ps-left-header
12d89a2e
RS
5476 ;; The left headers will display the message's subject, its
5477 ;; author, and the name of the folder it was in.
8bd22fcf 5478 '(ps-article-subject ps-article-author buffer-name)))
12d89a2e
RS
5479
5480;; Every now and then I forget to switch from the *Summary* buffer to
5481;; the *Article* before hitting prsc, and a nicely formatted list of
5482;; article subjects shows up at the printer. This function, bound to
5483;; prsc for the gnus *Summary* buffer means I don't have to switch
5484;; buffers first.
87a16a06 5485;; sb: Updated for Gnus 5.
12d89a2e
RS
5486(defun ps-gnus-print-article-from-summary ()
5487 (interactive)
a18ed129 5488 (ps-print-message-from-summary 'gnus-article-buffer "*Article*"))
ef2cbb24 5489
87a16a06 5490;; See `ps-gnus-print-article-from-summary'. This function does the
12d89a2e
RS
5491;; same thing for vm.
5492(defun ps-vm-print-message-from-summary ()
5493 (interactive)
a18ed129 5494 (ps-print-message-from-summary 'vm-mail-buffer ""))
ef2cbb24 5495
87a16a06 5496;; A hook to bind to bind to `gnus-summary-setup-buffer' to locally bind
12d89a2e
RS
5497;; prsc.
5498(defun ps-gnus-summary-setup ()
00aa16af 5499 (local-set-key (ps-prsc) 'ps-gnus-print-article-from-summary))
12d89a2e
RS
5500
5501;; Look in an article or mail message for the Subject: line. To be
87a16a06 5502;; placed in `ps-left-headers'.
12d89a2e
RS
5503(defun ps-info-file ()
5504 (save-excursion
5505 (goto-char (point-min))
a97592dd 5506 (if (re-search-forward "File:[ \t]+\\([^, \t\n]*\\)" nil t)
055e7bf2 5507 (buffer-substring-no-properties (match-beginning 1) (match-end 1))
12d89a2e
RS
5508 "File ???")))
5509
5510;; Look in an article or mail message for the Subject: line. To be
87a16a06 5511;; placed in `ps-left-headers'.
12d89a2e
RS
5512(defun ps-info-node ()
5513 (save-excursion
5514 (goto-char (point-min))
a97592dd 5515 (if (re-search-forward "Node:[ \t]+\\([^,\t\n]*\\)" nil t)
055e7bf2 5516 (buffer-substring-no-properties (match-beginning 1) (match-end 1))
12d89a2e
RS
5517 "Node ???")))
5518
5519(defun ps-info-mode-hook ()
5520 (setq ps-left-header
5521 ;; The left headers will display the node name and file name.
8bd22fcf 5522 '(ps-info-node ps-info-file)))
12d89a2e 5523
043620f4
KH
5524;; WARNING! The following function is a *sample* only, and is *not*
5525;; meant to be used as a whole unless you understand what the effects
87a16a06
RS
5526;; will be! (In fact, this is a copy of Jim's setup for ps-print --
5527;; I'd be very surprised if it was useful to *anybody*, without
043620f4
KH
5528;; modification.)
5529
12d89a2e 5530(defun ps-jts-ps-setup ()
00aa16af
RS
5531 (global-set-key (ps-prsc) 'ps-spool-buffer-with-faces) ;f22 is prsc
5532 (global-set-key (ps-s-prsc) 'ps-spool-region-with-faces)
5533 (global-set-key (ps-c-prsc) 'ps-despool)
12d89a2e
RS
5534 (add-hook 'gnus-article-prepare-hook 'ps-gnus-article-prepare-hook)
5535 (add-hook 'gnus-summary-mode-hook 'ps-gnus-summary-setup)
5536 (add-hook 'vm-mode-hook 'ps-vm-mode-hook)
00aa16af 5537 (add-hook 'vm-mode-hooks 'ps-vm-mode-hook)
12d89a2e 5538 (add-hook 'Info-mode-hook 'ps-info-mode-hook)
8bd22fcf
KH
5539 (setq ps-spool-duplex t
5540 ps-print-color-p nil
5541 ps-lpr-command "lpr"
5542 ps-lpr-switches '("-Jjct,duplex_long"))
bcc0d457
RS
5543 'ps-jts-ps-setup)
5544
5545;; WARNING! The following function is a *sample* only, and is *not*
5546;; meant to be used as a whole unless it corresponds to your needs.
5547;; (In fact, this is a copy of Jack's setup for ps-print --
5548;; I would not be that surprised if it was useful to *anybody*,
5549;; without modification.)
5550
5551(defun ps-jack-setup ()
87a16a06 5552 (setq ps-print-color-p nil
bcc0d457 5553 ps-lpr-command "lpr"
8bd22fcf 5554 ps-lpr-switches nil
bcc0d457 5555
87a16a06
RS
5556 ps-paper-type 'a4
5557 ps-landscape-mode t
bcc0d457
RS
5558 ps-number-of-columns 2
5559
5560 ps-left-margin (/ (* 72 1.0) 2.54) ; 1.0 cm
5561 ps-right-margin (/ (* 72 1.0) 2.54) ; 1.0 cm
5562 ps-inter-column (/ (* 72 1.0) 2.54) ; 1.0 cm
5563 ps-bottom-margin (/ (* 72 1.5) 2.54) ; 1.5 cm
5564 ps-top-margin (/ (* 72 1.5) 2.54) ; 1.5 cm
5565 ps-header-offset (/ (* 72 1.0) 2.54) ; 1.0 cm
5566 ps-header-line-pad .15
5567 ps-print-header t
5568 ps-print-header-frame t
5569 ps-header-lines 2
5570 ps-show-n-of-n t
5571 ps-spool-duplex nil
5572
5573 ps-font-family 'Courier
5574 ps-font-size 5.5
5575 ps-header-font-family 'Helvetica
5576 ps-header-font-size 6
5577 ps-header-title-font-size 8)
5578 'ps-jack-setup)
12d89a2e
RS
5579
5580(provide 'ps-print)
b87c5d3d 5581
12d89a2e 5582;;; ps-print.el ends here