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