]> code.delx.au - gnu-emacs/blob - lisp/ps-mule.el
(ps-mule-bitmap-prologue): Fix arguments to setcharwidth.
[gnu-emacs] / lisp / ps-mule.el
1 ;;; ps-mule.el --- provide multi-byte character facility to ps-print
2
3 ;; Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003
4 ;; Free Software Foundation, Inc.
5
6 ;; Author: Vinicius Jose Latorre <vinicius@cpqd.com.br>
7 ;; Kenichi Handa <handa@etl.go.jp> (multi-byte characters)
8 ;; Maintainer: Kenichi Handa <handa@etl.go.jp> (multi-byte characters)
9 ;; Vinicius Jose Latorre <vinicius@cpqd.com.br>
10 ;; Keywords: wp, print, PostScript, multibyte, mule
11 ;; Time-stamp: <2003/05/14 22:19:41 vinicius>
12
13 ;; This file is part of GNU Emacs.
14
15 ;; GNU Emacs is free software; you can redistribute it and/or modify
16 ;; it under the terms of the GNU General Public License as published by
17 ;; the Free Software Foundation; either version 2, or (at your option)
18 ;; any later version.
19
20 ;; GNU Emacs is distributed in the hope that it will be useful,
21 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
22 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 ;; GNU General Public License for more details.
24
25 ;; You should have received a copy of the GNU General Public License
26 ;; along with GNU Emacs; see the file COPYING. If not, write to the
27 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
28 ;; Boston, MA 02110-1301, USA.
29
30 ;;; Commentary:
31
32 ;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
33 ;;
34 ;; About ps-mule
35 ;; -------------
36 ;;
37 ;; This package is used for ps-print to print multi-byte buffer.
38 ;;
39 ;; See also ps-print.el.
40 ;;
41 ;;
42 ;; Printing Multi-byte Buffer
43 ;; --------------------------
44 ;;
45 ;; The variable `ps-multibyte-buffer' specifies the ps-print multi-byte buffer
46 ;; handling.
47 ;;
48 ;; Valid values for `ps-multibyte-buffer' are:
49 ;;
50 ;; nil This is the value to use the default settings which
51 ;; is by default for printing buffer with only ASCII
52 ;; and Latin characters. The default setting can be
53 ;; changed by setting the variable
54 ;; `ps-mule-font-info-database-default' differently.
55 ;; The initial value of this variable is
56 ;; `ps-mule-font-info-database-latin' (see
57 ;; documentation).
58 ;;
59 ;; `non-latin-printer' This is the value to use when you have a japanese
60 ;; or korean PostScript printer and want to print
61 ;; buffer with ASCII, Latin-1, Japanese (JISX0208 and
62 ;; JISX0201-Kana) and Korean characters. At present,
63 ;; it was not tested the Korean characters printing.
64 ;; If you have a korean PostScript printer, please,
65 ;; test it.
66 ;;
67 ;; `bdf-font' This is the value to use when you want to print
68 ;; buffer with BDF fonts. BDF fonts include both latin
69 ;; and non-latin fonts. BDF (Bitmap Distribution
70 ;; Format) is a format used for distributing X's font
71 ;; source file. BDF fonts are included in
72 ;; `intlfonts-1.2' which is a collection of X11 fonts
73 ;; for all characters supported by Emacs. In order to
74 ;; use this value, be sure to have installed
75 ;; `intlfonts-1.2' and set the variable
76 ;; `bdf-directory-list' appropriately (see ps-bdf.el
77 ;; for documentation of this variable).
78 ;;
79 ;; `bdf-font-except-latin' This is like `bdf-font' except that it is used
80 ;; PostScript default fonts to print ASCII and Latin-1
81 ;; characters. This is convenient when you want or
82 ;; need to use both latin and non-latin characters on
83 ;; the same buffer. See `ps-font-family',
84 ;; `ps-header-font-family' and `ps-font-info-database'.
85 ;;
86 ;; Any other value is treated as nil.
87 ;;
88 ;; The default is nil.
89 ;;
90 ;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
91
92 ;;; Code:
93
94 (eval-and-compile
95 (require 'ps-print)
96
97 ;; to avoid XEmacs compilation gripes
98 (defvar leading-code-private-22 157)
99 (or (fboundp 'charset-bytes)
100 (defun charset-bytes (charset) 1)) ; ascii
101 (or (fboundp 'charset-dimension)
102 (defun charset-dimension (charset) 1)) ; ascii
103 (or (fboundp 'charset-id)
104 (defun charset-id (charset) 0)) ; ascii
105 (or (fboundp 'charset-width)
106 (defun charset-width (charset) 1)) ; ascii
107 (or (fboundp 'find-charset-region)
108 (defun find-charset-region (beg end &optional table)
109 (list 'ascii)))
110 (or (fboundp 'char-valid-p)
111 (defun char-valid-p (char)
112 (< (following-char) 256)))
113 (or (fboundp 'split-char)
114 (defun split-char (char)
115 (list (if (char-valid-p char)
116 'ascii
117 'unknow)
118 char)))
119 (or (fboundp 'char-width)
120 (defun char-width (char) 1)) ; ascii
121 (or (fboundp 'chars-in-region)
122 (defun chars-in-region (beg end)
123 (- (max beg end) (min beg end))))
124 (or (fboundp 'forward-point)
125 (defun forward-point (arg)
126 (save-excursion
127 (let ((count (abs arg))
128 (step (if (zerop arg)
129 0
130 (/ arg arg))))
131 (while (and (> count 0)
132 (< (point-min) (point)) (< (point) (point-max)))
133 (forward-char step)
134 (setq count (1- count)))
135 (+ (point) (* count step))))))
136 (or (fboundp 'decompose-composite-char)
137 (defun decompose-composite-char (char &optional type
138 with-composition-rule)
139 nil))
140 (or (fboundp 'encode-coding-string)
141 (defun encode-coding-string (string coding-system &optional nocopy)
142 (if nocopy
143 string
144 (copy-sequence string))))
145 (or (fboundp 'coding-system-p)
146 (defun coding-system-p (obj) nil))
147 (or (fboundp 'ccl-execute-on-string)
148 (defun ccl-execute-on-string (ccl-prog status str
149 &optional contin unibyte-p)
150 str))
151 (or (fboundp 'define-ccl-program)
152 (defmacro define-ccl-program (name ccl-program &optional doc)
153 `(defconst ,name nil ,doc)))
154 (or (fboundp 'multibyte-string-p)
155 (defun multibyte-string-p (str)
156 (let ((len (length str))
157 (i 0)
158 multibyte)
159 (while (and (< i len) (not (setq multibyte (> (aref str i) 255))))
160 (setq i (1+ i)))
161 multibyte)))
162 (or (fboundp 'string-make-multibyte)
163 (defalias 'string-make-multibyte 'copy-sequence))
164 (or (fboundp 'encode-char)
165 (defun encode-char (ch ccs)
166 ch)))
167
168
169 ;;;###autoload
170 (defcustom ps-multibyte-buffer nil
171 "*Specifies the multi-byte buffer handling.
172
173 Valid values are:
174
175 nil This is the value to use the default settings which
176 is by default for printing buffer with only ASCII
177 and Latin characters. The default setting can be
178 changed by setting the variable
179 `ps-mule-font-info-database-default' differently.
180 The initial value of this variable is
181 `ps-mule-font-info-database-latin' (see
182 documentation).
183
184 `non-latin-printer' This is the value to use when you have a Japanese
185 or Korean PostScript printer and want to print
186 buffer with ASCII, Latin-1, Japanese (JISX0208 and
187 JISX0201-Kana) and Korean characters. At present,
188 it was not tested the Korean characters printing.
189 If you have a korean PostScript printer, please,
190 test it.
191
192 `bdf-font' This is the value to use when you want to print
193 buffer with BDF fonts. BDF fonts include both latin
194 and non-latin fonts. BDF (Bitmap Distribution
195 Format) is a format used for distributing X's font
196 source file. BDF fonts are included in
197 `intlfonts-1.2' which is a collection of X11 fonts
198 for all characters supported by Emacs. In order to
199 use this value, be sure to have installed
200 `intlfonts-1.2' and set the variable
201 `bdf-directory-list' appropriately (see ps-bdf.el for
202 documentation of this variable).
203
204 `bdf-font-except-latin' This is like `bdf-font' except that it is used
205 PostScript default fonts to print ASCII and Latin-1
206 characters. This is convenient when you want or
207 need to use both latin and non-latin characters on
208 the same buffer. See `ps-font-family',
209 `ps-header-font-family' and `ps-font-info-database'.
210
211 Any other value is treated as nil."
212 :type '(choice (const non-latin-printer) (const bdf-font)
213 (const bdf-font-except-latin) (const :tag "nil" nil))
214 :group 'ps-print-font)
215
216 (defvar ps-mule-font-info-database
217 nil
218 "Alist of charsets with the corresponding font information.
219 Each element has the form:
220
221 (CHARSET (FONT-TYPE FONT-SRC FONT-NAME ENCODING BYTES) ...)
222
223 Where
224
225 CHARSET is a charset (symbol) for this font family,
226
227 FONT-TYPE is a font type: normal, bold, italic, or bold-italic.
228
229 FONT-SRC is a font source: builtin, bdf, vflib, or nil.
230
231 If FONT-SRC is builtin, FONT-NAME is a built-in PostScript font name.
232
233 If FONT-SRC is bdf, FONT-NAME is a BDF font file name, or a list of
234 alternative font names. To use this font, the external library `ps-bdf'
235 is required.
236
237 If FONT-SRC is vflib, FONT-NAME is the name of a font that VFlib knows.
238 To use this font, the external library `vflib' is required.
239
240 If FONT-SRC is nil, a proper ASCII font in the variable
241 `ps-font-info-database' is used. This is useful for Latin-1 characters.
242
243 ENCODING is a coding system to encode a string of characters of CHARSET into a
244 proper string matching an encoding of the specified font. ENCODING may be a
245 function that does this encoding. In this case, the function is called with
246 one argument, the string to encode, and it should return an encoded string.
247
248 BYTES specifies how many bytes each character has in the encoded byte
249 sequence; it should be 1 or 2.
250
251 All multi-byte characters are printed by fonts specified in this database
252 regardless of a font family of ASCII characters. The exception is Latin-1
253 characters which are printed by the same font as ASCII characters, thus obey
254 font family.
255
256 See also the variable `ps-font-info-database'.")
257
258 (defconst ps-mule-font-info-database-latin
259 '((latin-iso8859-1
260 (normal nil nil iso-latin-1)))
261 "Sample setting of `ps-mule-font-info-database' to use latin fonts.")
262
263 (defcustom ps-mule-font-info-database-default
264 ps-mule-font-info-database-latin
265 "*The default setting to use when `ps-multibyte-buffer' is nil."
266 :type '(symbol :tag "Multi-Byte Buffer Database Font Default")
267 :group 'ps-print-font)
268
269 (defconst ps-mule-font-info-database-ps
270 '((katakana-jisx0201
271 (normal builtin "Ryumin-Light.Katakana" ps-mule-encode-7bit 1)
272 (bold builtin "GothicBBB-Medium.Katakana" ps-mule-encode-7bit 1)
273 (bold-italic builtin "GothicBBB-Medium.Katakana" ps-mule-encode-7bit 1))
274 (latin-jisx0201
275 (normal builtin "Ryumin-Light.Hankaku" ps-mule-encode-7bit 1)
276 (bold builtin "GothicBBB-Medium.Hankaku" ps-mule-encode-7bit 1))
277 (japanese-jisx0208
278 (normal builtin "Ryumin-Light-H" ps-mule-encode-7bit 2)
279 (bold builtin "GothicBBB-Medium-H" ps-mule-encode-7bit 2))
280 (korean-ksc5601
281 (normal builtin "Munhwa-Regular-KSC-EUC-H" ps-mule-encode-7bit 2)
282 (bold builtin "Munhwa-Bold-KSC-EUC-H" ps-mule-encode-7bit 2))
283 )
284 "Sample setting of the `ps-mule-font-info-database' to use builtin PS font.
285
286 Currently, data for Japanese and Korean PostScript printers are listed.")
287
288 (defconst ps-mule-font-info-database-bdf
289 '((ascii
290 (normal bdf ("lt1-24-etl.bdf" "etl24-latin1.bdf") nil 1)
291 (bold bdf ("lt1-16b-etl.bdf" "etl16b-latin1.bdf") nil 1)
292 (italic bdf ("lt1-16i-etl.bdf" "etl16i-latin1.bdf") nil 1)
293 (bold-italic bdf ("lt1-16bi-etl.bdf" "etl16bi-latin1.bdf") nil 1))
294 (latin-iso8859-1
295 (normal bdf ("lt1-24-etl.bdf" "etl24-latin1.bdf") iso-latin-1 1)
296 (bold bdf ("lt1-16b-etl.bdf" "etl16b-latin1.bdf") iso-latin-1 1)
297 (italic bdf ("lt1-16i-etl.bdf" "etl16i-latin1.bdf") iso-latin-1 1)
298 (bold-italic bdf ("lt1-16bi-etl.bdf" "etl16bi-latin1.bdf") iso-latin-1 1))
299 (latin-iso8859-2
300 (normal bdf ("lt2-24-etl.bdf" "etl24-latin2.bdf") iso-latin-2 1))
301 (latin-iso8859-3
302 (normal bdf ("lt3-24-etl.bdf" "etl24-latin3.bdf") iso-latin-3 1))
303 (latin-iso8859-4
304 (normal bdf ("lt4-24-etl.bdf" "etl24-latin4.bdf") iso-latin-4 1))
305 (thai-tis620
306 (normal bdf ("thai24.bdf" "thai-24.bdf") thai-tis620 1))
307 (greek-iso8859-7
308 (normal bdf ("grk24-etl.bdf" "etl24-greek.bdf") greek-iso-8bit 1))
309 ;; (arabic-iso8859-6 nil) ; not yet available
310 (hebrew-iso8859-8
311 (normal bdf ("heb24-etl.bdf" "etl24-hebrew.bdf") hebrew-iso-8bit 1))
312 (katakana-jisx0201
313 (normal bdf "12x24rk.bdf" ps-mule-encode-8bit 1))
314 (latin-jisx0201
315 (normal bdf "12x24rk.bdf" ps-mule-encode-7bit 1))
316 (cyrillic-iso8859-5
317 (normal bdf ("cyr24-etl.bdf" "etl24-cyrillic.bdf") cyrillic-iso-8bit 1))
318 (latin-iso8859-9
319 (normal bdf ("lt5-24-etl.bdf" "etl24-latin5.bdf") iso-latin-5 1))
320 (japanese-jisx0208-1978
321 (normal bdf "jiskan24.bdf" ps-mule-encode-7bit 2))
322 (chinese-gb2312
323 (normal bdf "gb24st.bdf" ps-mule-encode-7bit 2))
324 (japanese-jisx0208
325 (normal bdf "jiskan24.bdf" ps-mule-encode-7bit 2))
326 (korean-ksc5601
327 (normal bdf "hanglm24.bdf" ps-mule-encode-7bit 2))
328 (japanese-jisx0212
329 (normal bdf ("jksp40.bdf" "jisksp40.bdf") ps-mule-encode-7bit 2))
330 (chinese-cns11643-1
331 (normal bdf ("cns1-40.bdf" "cns-1-40.bdf") ps-mule-encode-7bit 2))
332 (chinese-cns11643-2
333 (normal bdf ("cns2-40.bdf" "cns-2-40.bdf") ps-mule-encode-7bit 2))
334 (chinese-big5-1
335 (normal bdf "taipei24.bdf" chinese-big5 2))
336 (chinese-big5-2
337 (normal bdf "taipei24.bdf" chinese-big5 2))
338 (chinese-sisheng
339 (normal bdf ("sish24-etl.bdf" "etl24-sisheng.bdf") ps-mule-encode-7bit 1))
340 (ipa
341 (normal bdf ("ipa24-etl.bdf" "etl24-ipa.bdf") ps-mule-encode-8bit 1))
342 (vietnamese-viscii-lower
343 (normal bdf ("visc24-etl.bdf" "etl24-viscii.bdf") vietnamese-viscii 1))
344 (vietnamese-viscii-upper
345 (normal bdf ("visc24-etl.bdf" "etl24-viscii.bdf") vietnamese-viscii 1))
346 (arabic-digit
347 (normal bdf ("arab24-0-etl.bdf" "etl24-arabic0.bdf") ps-mule-encode-7bit 1))
348 (arabic-1-column
349 (normal bdf ("arab24-1-etl.bdf" "etl24-arabic1.bdf") ps-mule-encode-7bit 1))
350 ;; (ascii-right-to-left nil) ; not yet available
351 (lao
352 (normal bdf ("lao24-mule.bdf" "mule-lao-24.bdf") lao 1))
353 (arabic-2-column
354 (normal bdf ("arab24-2-etl.bdf" "etl24-arabic2.bdf") ps-mule-encode-7bit 1))
355 (indian-is13194
356 (normal bdf ("isci24-mule.bdf" "mule-iscii-24.bdf") ps-mule-encode-7bit 1))
357 (indian-1-column
358 (normal bdf ("ind1c24-mule.bdf" "mule-indian-1col-24.bdf") ps-mule-encode-7bit 2))
359 (tibetan-1-column
360 (normal bdf ("tib1c24-mule.bdf" "mule-tibmdx-1col-24.bdf") ps-mule-encode-7bit 2))
361 (ethiopic
362 (normal bdf ("ethio24f-uni.bdf" "ethiomx24f-uni.bdf") ps-mule-encode-ethiopic 2))
363 (chinese-cns11643-3
364 (normal bdf ("cns3-40.bdf" "cns-3-40.bdf") ps-mule-encode-7bit 2))
365 (chinese-cns11643-4
366 (normal bdf ("cns4-40.bdf" "cns-4-40.bdf") ps-mule-encode-7bit 2))
367 (chinese-cns11643-5
368 (normal bdf ("cns5-40.bdf" "cns-5-40.bdf") ps-mule-encode-7bit 2))
369 (chinese-cns11643-6
370 (normal bdf ("cns6-40.bdf" "cns-6-40.bdf") ps-mule-encode-7bit 2))
371 (chinese-cns11643-7
372 (normal bdf ("cns7-40.bdf" "cns-7-40.bdf") ps-mule-encode-7bit 2))
373 (indian-2-column
374 (normal bdf ("ind24-mule.bdf" "mule-indian-24.bdf") ps-mule-encode-7bit 2))
375 (tibetan
376 (normal bdf ("tib24p-mule.bdf" "tib24-mule.bdf" "mule-tibmdx-24.bdf")
377 ps-mule-encode-7bit 2))
378 (mule-unicode-0100-24ff
379 (normal bdf "etl24-unicode.bdf" ps-mule-encode-ucs2 2))
380 (mule-unicode-2500-33ff
381 (normal bdf "etl24-unicode.bdf" ps-mule-encode-ucs2 2))
382 (mule-unicode-e000-ffff
383 (normal bdf "etl24-unicode.bdf" ps-mule-encode-ucs2 2)))
384 "Sample setting of the `ps-mule-font-info-database' to use BDF fonts.
385 BDF (Bitmap Distribution Format) is a format used for distributing X's font
386 source file.
387
388 Current default value list for BDF fonts is included in `intlfonts-1.2'
389 which is a collection of X11 fonts for all characters supported by Emacs.
390
391 Using this list as default value to `ps-mule-font-info-database', all
392 characters including ASCII and Latin-1 are printed by BDF fonts.
393
394 See also `ps-mule-font-info-database-ps-bdf'.")
395
396 (defconst ps-mule-font-info-database-ps-bdf
397 (cons (car ps-mule-font-info-database-latin)
398 (cdr (cdr ps-mule-font-info-database-bdf)))
399 "Sample setting of the `ps-mule-font-info-database' to use BDF fonts.
400
401 Current default value list for BDF fonts is included in `intlfonts-1.2'
402 which is a collection of X11 fonts for all characters supported by Emacs.
403
404 Using this list as default value to `ps-mule-font-info-database', all
405 characters except ASCII and Latin-1 characters are printed with BDF fonts.
406 ASCII and Latin-1 characters are printed with PostScript font specified
407 by `ps-font-family' and `ps-header-font-family'.
408
409 See also `ps-mule-font-info-database-bdf'.")
410
411 ;; Two typical encoding functions for PostScript fonts.
412
413 (defun ps-mule-encode-7bit (string)
414 (ps-mule-encode-bit string 0))
415
416 (defun ps-mule-encode-8bit (string)
417 (ps-mule-encode-bit string 128))
418
419 (defun ps-mule-encode-bit (string delta)
420 (let* ((dim (charset-dimension (char-charset (string-to-char string))))
421 (len (* (length string) dim))
422 (str (make-string len 0))
423 (i 0)
424 (j 0))
425 (if (= dim 1)
426 (while (< j len)
427 (aset str j
428 (+ (nth 1 (split-char (aref string i))) delta))
429 (setq i (1+ i)
430 j (1+ j)))
431 (while (< j len)
432 (let ((split (split-char (aref string i))))
433 (aset str j (+ (nth 1 split) delta))
434 (aset str (1+ j) (+ (nth 2 split) delta))
435 (setq i (1+ i)
436 j (+ j 2)))))
437 str))
438
439 ;; Special encoding function for Ethiopic.
440 (if (boundp 'mule-version) ; only if mule package is loaded
441 (define-ccl-program ccl-encode-ethio-unicode
442 `(1
443 ((read r2)
444 (loop
445 (if (r2 == ,leading-code-private-22)
446 ((read r0)
447 (if (r0 == ,(charset-id 'ethiopic))
448 ((read r1 r2)
449 (r1 &= 127) (r2 &= 127)
450 (call ccl-encode-ethio-font)
451 (write r1)
452 (write-read-repeat r2))
453 ((write r2 r0)
454 (repeat))))
455 (write-read-repeat r2))))))
456 ;; to avoid compilation gripes
457 (defvar ccl-encode-ethio-unicode nil))
458
459 (if (boundp 'mule-version)
460 ;; bound mule-version
461 (defun ps-mule-encode-ethiopic (string)
462 (ccl-execute-on-string (symbol-value 'ccl-encode-ethio-unicode)
463 (make-vector 9 nil)
464 string))
465 ;; unbound mule-version
466 (defun ps-mule-encode-ethiopic (string)
467 string))
468
469 ;; Special encoding for mule-unicode-* characters.
470 (defun ps-mule-encode-ucs2 (string)
471 (let* ((len (length string))
472 (str (make-string (* 2 len) 0))
473 (i 0)
474 (j 0)
475 ch hi lo)
476 (while (< i len)
477 (setq ch (encode-char (aref string i) 'ucs)
478 hi (lsh ch -8)
479 lo (logand ch 255))
480 (aset str j hi)
481 (aset str (1+ j) lo)
482 (setq i (1+ i)
483 j (+ j 2)))
484 str))
485
486 ;; A charset which we are now processing.
487 (defvar ps-mule-current-charset nil)
488
489 (defun ps-mule-get-font-spec (charset font-type)
490 "Return FONT-SPEC for printing characters CHARSET with FONT-TYPE.
491 FONT-SPEC is a list that has the form:
492
493 (FONT-SRC FONT-NAME ENCODING BYTES)
494
495 FONT-SPEC is extracted from `ps-mule-font-info-database'.
496
497 See the documentation of `ps-mule-font-info-database' for the meaning of each
498 element of the list."
499 (let ((slot (cdr (assq charset ps-mule-font-info-database))))
500 (and slot
501 (cdr (or (assq font-type slot)
502 (and (eq font-type 'bold-italic)
503 (or (assq 'bold slot) (assq 'italic slot)))
504 (assq 'normal slot))))))
505
506 ;; Functions to access each element of FONT-SPEC.
507 (defsubst ps-mule-font-spec-src (font-spec) (car font-spec))
508 (defsubst ps-mule-font-spec-name (font-spec) (nth 1 font-spec))
509 (defsubst ps-mule-font-spec-encoding (font-spec) (nth 2 font-spec))
510 (defsubst ps-mule-font-spec-bytes (font-spec) (nth 3 font-spec))
511
512 (defsubst ps-mule-printable-p (charset)
513 "Non-nil if characters in CHARSET is printable."
514 ;; ASCII and Latin-1 are always printable.
515 (or (eq charset 'ascii)
516 (eq charset 'latin-iso8859-1)
517 (ps-mule-get-font-spec charset 'normal)))
518
519 (defconst ps-mule-external-libraries
520 '((builtin nil nil
521 nil nil nil)
522 (bdf ps-bdf nil
523 bdf-generate-prologue bdf-generate-font bdf-generate-glyphs)
524 (pcf nil nil
525 pcf-generate-prologue pcf-generate-font pcf-generate-glyphs)
526 (vflib nil nil
527 vflib-generate-prologue vflib-generate-font vflib-generate-glyphs))
528 "Alist of information of external libraries to support PostScript printing.
529 Each element has the form:
530
531 (FONT-SRC FEATURE INITIALIZED-P PROLOGUE-FUNC FONT-FUNC GLYPHS-FUNC)
532
533 FONT-SRC is the font source: builtin, bdf, pcf, or vflib.
534
535 FEATURE is the feature that provide a facility to handle FONT-SRC. Except for
536 `builtin' FONT-SRC, this feature is automatically `require'd before handling
537 FONT-SRC. Currently, we only have the feature `ps-bdf'.
538
539 INITIALIZED-P indicates if this library is initialized or not.
540
541 PROLOGUE-FUNC is a function to generate PostScript code which define several
542 PostScript procedures that will be called by FONT-FUNC and GLYPHS-FUNC. It is
543 called with no argument, and should return a list of strings.
544
545 FONT-FUNC is a function to generate PostScript code which define a new font. It
546 is called with one argument FONT-SPEC, and should return a list of strings.
547
548 GLYPHS-FUNC is a function to generate PostScript code which define glyphs of
549 characters. It is called with three arguments FONT-SPEC, CODE-LIST, and BYTES,
550 and should return a list of strings.")
551
552 (defun ps-mule-init-external-library (font-spec)
553 "Initialize external library specified by FONT-SPEC for PostScript printing.
554 See the documentation of `ps-mule-get-font-spec' for FONT-SPEC's meaning."
555 (let* ((font-src (ps-mule-font-spec-src font-spec))
556 (slot (assq font-src ps-mule-external-libraries)))
557 (or (not font-src)
558 (nth 2 slot)
559 (let ((func (nth 3 slot)))
560 (if func
561 (progn
562 (require (nth 1 slot))
563 (ps-output-prologue (funcall func))))
564 (setcar (nthcdr 2 slot) t)))))
565
566 ;; Cached glyph information of fonts, alist of:
567 ;; (FONT-NAME ((FONT-TYPE-NUMBER . SCALED-FONT-NAME) ...)
568 ;; cache CODE0 CODE1 ...)
569 (defvar ps-mule-font-cache nil)
570
571 (defun ps-mule-generate-font (font-spec charset &optional header-p)
572 "Generate PostScript codes to define a new font in FONT-SPEC for CHARSET.
573
574 If optional 3rd arg HEADER-P is non-nil, generate codes to define a header
575 font."
576 (let* ((font-name (ps-mule-font-spec-name font-spec))
577 (font-name (if (consp font-name) (car font-name) font-name))
578 (font-cache (assoc font-name ps-mule-font-cache))
579 (font-src (ps-mule-font-spec-src font-spec))
580 (func (nth 4 (assq font-src ps-mule-external-libraries)))
581 (font-size (if header-p (if (eq ps-current-font 0)
582 ps-header-title-font-size-internal
583 ps-header-font-size-internal)
584 ps-font-size-internal))
585 (current-font (+ ps-current-font (if header-p 10 0)))
586 (scaled-font-name
587 (cond (header-p
588 (format "h%d" ps-current-font))
589 ((eq charset 'ascii)
590 (format "f%d" ps-current-font))
591 (t
592 (format "f%02x-%d" (charset-id charset) ps-current-font)))))
593 (and func (not font-cache)
594 (ps-output-prologue (funcall func charset font-spec)))
595 (ps-output-prologue
596 (list (format "/%s %f /%s Def%sFontMule\n"
597 scaled-font-name font-size font-name
598 (if (or header-p
599 (eq ps-mule-current-charset 'ascii))
600 "Ascii" ""))))
601 (if font-cache
602 (setcar (cdr font-cache)
603 (cons (cons current-font scaled-font-name)
604 (nth 1 font-cache)))
605 (setq font-cache (list font-name
606 (list (cons current-font scaled-font-name))
607 'cache)
608 ps-mule-font-cache (cons font-cache ps-mule-font-cache)))
609 font-cache))
610
611 (defun ps-mule-generate-glyphs (font-spec code-list)
612 "Generate PostScript codes which generate glyphs for CODE-LIST of FONT-SPEC."
613 (let* ((font-src (ps-mule-font-spec-src font-spec))
614 (func (nth 5 (assq font-src ps-mule-external-libraries))))
615 (and func
616 (ps-output-prologue
617 (funcall func font-spec code-list
618 (ps-mule-font-spec-bytes font-spec))))))
619
620 (defun ps-mule-prepare-font (font-spec string charset
621 &optional no-setfont header-p)
622 "Generate PostScript codes to print STRING of CHARSET by font FONT-SPEC.
623
624 The generated code is inserted on prologue part except the code that sets the
625 current font (using PostScript procedure `FM').
626
627 If optional 4th arg NO-SETFONT is non-nil, don't generate the code for setting
628 the current font.
629
630 If optional 5th arg HEADER-P is non-nil, generate a code for setting a header
631 font."
632 (let* ((font-name (ps-mule-font-spec-name font-spec))
633 (font-name (if (consp font-name) (car font-name) font-name))
634 (current-font (+ ps-current-font (if header-p 10 0)))
635 (font-cache (assoc font-name ps-mule-font-cache)))
636 (or (and font-cache (assq current-font (nth 1 font-cache)))
637 (setq font-cache (ps-mule-generate-font font-spec charset header-p)))
638 (or no-setfont
639 (let ((new-font (cdr (assq current-font (nth 1 font-cache)))))
640 (or (equal new-font ps-last-font)
641 (progn
642 (ps-output (format "/%s FM\n" new-font))
643 (setq ps-last-font new-font)))))
644 (if (nth 5 (assq (ps-mule-font-spec-src font-spec)
645 ps-mule-external-libraries))
646 ;; We have to generate PostScript codes which define glyphs.
647 (let* ((cached-codes (nthcdr 2 font-cache))
648 (bytes (ps-mule-font-spec-bytes font-spec))
649 (len (length string))
650 (i 0)
651 newcodes code)
652 (while (< i len)
653 (setq code (if (= bytes 1)
654 (aref string i)
655 (+ (* (aref string i) 256) (aref string (1+ i)))))
656 (or (memq code cached-codes)
657 (progn
658 (setq newcodes (cons code newcodes))
659 (setcdr cached-codes (cons code (cdr cached-codes)))))
660 (setq i (+ i bytes)))
661 (and newcodes
662 (ps-mule-generate-glyphs font-spec newcodes))))))
663
664 ;;;###autoload
665 (defun ps-mule-prepare-ascii-font (string)
666 "Setup special ASCII font for STRING.
667 STRING should contain only ASCII characters."
668 (let ((font-spec
669 (ps-mule-get-font-spec
670 'ascii
671 (car (nth ps-current-font (ps-font-alist 'ps-font-for-text))))))
672 (and font-spec
673 (ps-mule-prepare-font font-spec string 'ascii))))
674
675 ;;;###autoload
676 (defun ps-mule-set-ascii-font ()
677 (unless (eq ps-mule-current-charset 'ascii)
678 (ps-set-font ps-current-font)
679 (setq ps-mule-current-charset 'ascii)))
680
681 ;; List of charsets of multi-byte characters in a text being printed.
682 ;; If the text doesn't contain any multi-byte characters (i.e. only ASCII),
683 ;; the value is nil.
684 (defvar ps-mule-charset-list nil)
685
686 ;; This is a PostScript code inserted in the header of generated PostScript.
687 (defconst ps-mule-prologue
688 "%%%% Start of Mule Section
689
690 %% Working dictionary for general use.
691 /MuleDict 10 dict def
692
693 %% Adjust /RelativeCompose properly by checking /BaselineOffset.
694 /AdjustRelativeCompose { % fontdict |- fontdict
695 dup length 2 add dict begin
696 { 1 index /FID ne { def } { pop pop } ifelse } forall
697 currentdict /BaselineOffset known {
698 BaselineOffset false eq { /BaselineOffset 0 def } if
699 } {
700 /BaselineOffset 0 def
701 } ifelse
702 currentdict /RelativeCompose known not {
703 /RelativeCompose [ 0 0.1 ] def
704 } {
705 RelativeCompose false ne {
706 [ BaselineOffset RelativeCompose BaselineOffset add
707 [ FontMatrix { FontSize div } forall ] transform ]
708 /RelativeCompose exch def
709 } if
710 } ifelse
711 currentdict
712 end
713 } def
714
715 %% Define already scaled font for non-ASCII character sets.
716 /DefFontMule { % fontname size basefont |- --
717 findfont exch scalefont AdjustRelativeCompose definefont pop
718 } bind def
719
720 %% Define already scaled font for ASCII character sets.
721 /DefAsciiFontMule { % fontname size basefont |-
722 MuleDict begin
723 findfont dup /Encoding get /ISOLatin1Encoding exch def
724 exch scalefont AdjustRelativeCompose reencodeFontISO
725 end
726 } def
727
728 /CurrentFont false def
729
730 %% Set the specified font to use.
731 %% For non-ASCII font, don't install Ascent, etc.
732 /FM { % fontname |- --
733 /font exch def
734 font /f0 eq font /f1 eq font /f2 eq font /f3 eq or or or {
735 font F
736 } {
737 font findfont setfont
738 } ifelse
739 } bind def
740
741 %% Show vacant box for characters which don't have appropriate font.
742 /SB { % count column |- --
743 SpaceWidth mul /w exch def
744 1 exch 1 exch { %for
745 pop
746 gsave
747 0 setlinewidth
748 0 Descent rmoveto w 0 rlineto
749 0 LineHeight rlineto w neg 0 rlineto closepath stroke
750 grestore
751 w 0 rmoveto
752 } for
753 } bind def
754
755 %% Flag to tell if we are now handling a composition. This is
756 %% defined here because both composition handler and bitmap font
757 %% handler require it.
758 /Composing false def
759
760 %%%% End of Mule Section
761
762 "
763 "PostScript code for printing multi-byte characters.")
764
765 (defvar ps-mule-prologue-generated nil)
766
767 (defun ps-mule-prologue-generated ()
768 (unless ps-mule-prologue-generated
769 (ps-output-prologue ps-mule-prologue)
770 (setq ps-mule-prologue-generated t)))
771
772 (defun ps-mule-find-wrappoint (from to char-width &optional composition)
773 "Find the longest sequence which is printable in the current line.
774
775 The search starts at FROM and goes until TO.
776
777 Optional 4th arg COMPOSITION, if non-nil, is information of
778 composition starting at FROM.
779
780 If COMPOSITION is nil, it is assumed that all characters between FROM
781 and TO belong to a charset in `ps-mule-current-charset'. Otherwise,
782 it is assumed that all characters between FROM and TO belong to the
783 same composition.
784
785 CHAR-WIDTH is the average width of ASCII characters in the current font.
786
787 Returns the value:
788
789 (ENDPOS . RUN-WIDTH)
790
791 Where ENDPOS is the end position of the sequence and RUN-WIDTH is the width of
792 the sequence."
793 (if (or composition (eq ps-mule-current-charset 'composition))
794 ;; We must draw one char by one.
795 (let ((run-width (if composition
796 (nth 5 composition)
797 (* (char-width (char-after from)) char-width))))
798 (if (> run-width ps-width-remaining)
799 (cons from ps-width-remaining)
800 (cons (if composition
801 (nth 1 composition)
802 (1+ from))
803 run-width)))
804 ;; We assume that all characters in this range have the same width.
805 (setq char-width (* char-width (charset-width ps-mule-current-charset)))
806 (let ((run-width (* (abs (- from to)) char-width)))
807 (if (> run-width ps-width-remaining)
808 (cons (min to
809 (save-excursion
810 (goto-char from)
811 (forward-point
812 (truncate (/ ps-width-remaining char-width)))))
813 ps-width-remaining)
814 (cons to run-width)))))
815
816 ;;;###autoload
817 (defun ps-mule-plot-string (from to &optional bg-color)
818 "Generate PostScript code for plotting characters in the region FROM and TO.
819
820 It is assumed that all characters in this region belong to the same charset.
821
822 Optional argument BG-COLOR specifies background color.
823
824 Returns the value:
825
826 (ENDPOS . RUN-WIDTH)
827
828 Where ENDPOS is the end position of the sequence and RUN-WIDTH is the width of
829 the sequence."
830 (let ((ch (char-after from)))
831 (setq ps-mule-current-charset
832 (char-charset (or (aref ps-print-translation-table ch) ch))))
833 (let* ((wrappoint (ps-mule-find-wrappoint
834 from to (ps-avg-char-width 'ps-font-for-text)))
835 (to (car wrappoint))
836 (font-type (car (nth ps-current-font
837 (ps-font-alist 'ps-font-for-text))))
838 (font-spec (ps-mule-get-font-spec ps-mule-current-charset font-type))
839 (string (buffer-substring-no-properties from to)))
840 (dotimes (i (length string))
841 (let ((ch (aref ps-print-translation-table (aref string i))))
842 (if ch
843 (aset string i ch))))
844 (cond
845 ((= from to)
846 ;; We can't print any more characters in the current line.
847 nil)
848
849 (font-spec
850 ;; We surely have a font for printing this character set.
851 (ps-output-string (ps-mule-string-encoding font-spec string))
852 (ps-output " S\n"))
853
854 ((eq ps-mule-current-charset 'latin-iso8859-1)
855 ;; Latin-1 can be printed by a normal ASCII font.
856 (ps-output-string (ps-mule-string-ascii string))
857 (ps-output " S\n"))
858
859 ;; This case is obsolete for Emacs 21.
860 ((eq ps-mule-current-charset 'composition)
861 (ps-mule-plot-composition from (1+ from) bg-color))
862
863 (t
864 ;; No way to print this charset. Just show a vacant box of an
865 ;; appropriate width.
866 (ps-output (format "%d %d SB\n"
867 (length string)
868 (if (eq ps-mule-current-charset 'composition)
869 (char-width (char-after from))
870 (charset-width ps-mule-current-charset))))))
871 wrappoint))
872
873 ;;;###autoload
874 (defun ps-mule-plot-composition (from to &optional bg-color)
875 "Generate PostScript code for plotting composition in the region FROM and TO.
876
877 It is assumed that all characters in this region belong to the same
878 composition.
879
880 Optional argument BG-COLOR specifies background color.
881
882 Returns the value:
883
884 (ENDPOS . RUN-WIDTH)
885
886 Where ENDPOS is the end position of the sequence and RUN-WIDTH is the width of
887 the sequence."
888 (let* ((composition (find-composition from nil nil t))
889 (wrappoint (ps-mule-find-wrappoint
890 from to (ps-avg-char-width 'ps-font-for-text)
891 composition))
892 (to (car wrappoint))
893 (font-type (car (nth ps-current-font
894 (ps-font-alist 'ps-font-for-text)))))
895 (if (< from to)
896 ;; We can print this composition in the current line.
897 (let ((components (nth 2 composition)))
898 (ps-mule-plot-components
899 (ps-mule-prepare-font-for-components components font-type)
900 (if (nth 3 composition) "RLC" "RBC"))))
901 wrappoint))
902
903 ;; Prepare font of FONT-TYPE for printing COMPONENTS. By side effect,
904 ;; change character elements in COMPONENTS to the form:
905 ;; ENCODED-STRING or (FONTNAME . ENCODED-STRING)
906 ;; and change rule elements to the encoded value (integer).
907 ;; The latter form is used if we much change font for the character.
908
909 (defun ps-mule-prepare-font-for-components (components font-type)
910 (let ((len (length components))
911 (i 0)
912 elt)
913 (while (< i len)
914 (setq elt (aref components i))
915 (if (consp elt)
916 ;; ELT is a composition rule.
917 (setq elt (encode-composition-rule elt))
918 ;; ELT is a glyph character.
919 (let* ((charset (char-charset elt))
920 (font (or (eq charset ps-mule-current-charset)
921 (if (eq charset 'ascii)
922 (format "/f%d" ps-current-font)
923 (format "/f%02x-%d"
924 (charset-id charset) ps-current-font))))
925 str)
926 (setq ps-mule-current-charset charset
927 str (ps-mule-string-encoding
928 (ps-mule-get-font-spec charset font-type)
929 (char-to-string elt)
930 'no-setfont))
931 (if (stringp font)
932 (setq elt (cons font str) ps-last-font font)
933 (setq elt str))))
934 (aset components i elt)
935 (setq i (1+ i))))
936 components)
937
938 (defun ps-mule-plot-components (components tail)
939 (let ((elt (aref components 0))
940 (len (length components))
941 (i 1))
942 (ps-output "[ ")
943 (if (stringp elt)
944 (ps-output-string elt)
945 (ps-output (car elt) " ")
946 (ps-output-string (cdr elt)))
947 (while (< i len)
948 (setq elt (aref components i) i (1+ i))
949 (ps-output " ")
950 (cond ((stringp elt)
951 (ps-output-string elt))
952 ((consp elt)
953 (ps-output (car elt) " ")
954 (ps-output-string (cdr elt)))
955 (t ; i.e. (integerp elt)
956 (ps-output (format "%d" elt)))))
957 (ps-output " ] " tail "\n")))
958
959 ;; Composite font support
960
961 (defvar ps-mule-composition-prologue-generated nil)
962
963 (defconst ps-mule-composition-prologue
964 "%%%% Character composition handler
965 /RelativeCompositionSkip 0.4 def
966
967 %% Get a bounding box (relative to currentpoint) of STR.
968 /GetPathBox { % str |- --
969 gsave
970 currentfont /FontType get 3 eq { %ifelse
971 stringwidth pop pop
972 } {
973 currentpoint /y exch def /x exch def
974 false charpath flattenpath pathbbox
975 y sub /URY exch def x sub /URX exch def
976 y sub /LLY exch def x sub /LLX exch def
977 } ifelse
978 grestore
979 } bind def
980
981 %% Apply effects (underline, strikeout, overline, box) to the
982 %% rectangle specified by TOP BOTTOM LEFT RIGHT.
983 /SpecialEffect { % -- |- --
984 currentpoint dup TOP add /yy exch def BOTTOM add /YY exch def
985 dup LEFT add /xx exch def RIGHT add /XX exch def
986 %% Adjust positions for future shadowing.
987 Effect 8 and 0 ne {
988 /yy yy Yshadow add def
989 /XX XX Xshadow add def
990 } if
991 Effect 1 and 0 ne { UnderlinePosition Hline } if % underline
992 Effect 2 and 0 ne { StrikeoutPosition Hline } if % strikeout
993 Effect 4 and 0 ne { OverlinePosition Hline } if % overline
994 bg { % background
995 true
996 Effect 16 and 0 ne {SpaceBackground doBox} { xx yy XX YY doRect} ifelse
997 } if
998 Effect 16 and 0 ne { false 0 doBox } if % box
999 } def
1000
1001 %% Show STR with effects (shadow, outline).
1002 /ShowWithEffect { % str |- --
1003 Effect 8 and 0 ne { dup doShadow } if
1004 Effect 32 and 0 ne { true doOutline } { show } ifelse
1005 } def
1006
1007 %% Draw COMPONENTS which have the form [ font0? [str0 xoff0 yoff0] ... ].
1008 /ShowComponents { % components |- -
1009 LEFT 0 lt { LEFT neg 0 rmoveto } if
1010 {
1011 dup type /nametype eq { % font
1012 FM
1013 } { % [ str xoff yoff ]
1014 gsave
1015 aload pop rmoveto ShowWithEffect
1016 grestore
1017 } ifelse
1018 } forall
1019 RIGHT 0 rmoveto
1020 } def
1021
1022 %% Show relative composition.
1023 /RLC { % [ font0? str0 font1? str1 ... fontN? strN ] |- --
1024 /components exch def
1025 /Composing true def
1026 /first true def
1027 gsave
1028 [ components {
1029 /elt exch def
1030 elt type /nametype eq { % font
1031 elt dup FM
1032 } { first { % first string
1033 /first false def
1034 elt GetPathBox
1035 %% Bounding box of overall glyphs.
1036 /LEFT LLX def
1037 /RIGHT URX def
1038 /TOP URY def
1039 /BOTTOM LLY def
1040 currentfont /RelativeCompose known {
1041 /relative currentfont /RelativeCompose get def
1042 } {
1043 %% Disable relative composition by setting sufficiently low
1044 %% and high positions.
1045 /relative [ -100000 100000 ] def
1046 } ifelse
1047 [ elt 0 0 ]
1048 } { % other strings
1049 elt GetPathBox
1050 [ elt % str
1051 LLX 0 lt { RIGHT } { 0 } ifelse % xoff
1052 LLY relative 1 get ge { % compose on TOP
1053 TOP LLY sub RelativeCompositionSkip add % yoff
1054 /TOP TOP URY LLY sub add RelativeCompositionSkip add def
1055 } { URY relative 0 get le { % compose under BOTTOM
1056 BOTTOM URY sub RelativeCompositionSkip sub % yoff
1057 /BOTTOM BOTTOM URY LLY sub sub
1058 RelativeCompositionSkip sub def
1059 } {
1060 0 % yoff
1061 URY TOP gt { /TOP URY def } if
1062 LLY BOTTOM lt { /BOTTOM LLY def } if
1063 } ifelse } ifelse
1064 ]
1065 URX RIGHT gt { /RIGHT URX def } if
1066 } ifelse } ifelse
1067 } forall ] /components exch def
1068 grestore
1069
1070 %% Reflect special effects.
1071 SpecialEffect
1072
1073 %% Draw components while ignoring effects other than shadow and outline.
1074 components ShowComponents
1075 /Composing false def
1076
1077 } def
1078
1079 %% Show rule-base composition.
1080 /RBC { % [ font0? str0 rule1 font1? str1 rule2 ... strN ] |- --
1081 /components exch def
1082 /Composing true def
1083 /first true def
1084 gsave
1085 [ components {
1086 /elt exch def
1087 elt type /nametype eq { % font
1088 elt dup FM
1089 } { elt type /integertype eq { % rule
1090 %% This RULE decoding should be compatible with macro
1091 %% COMPOSITION_DECODE_RULE in emacs/src/composite.h.
1092 elt 12 idiv dup 3 mod /grefx exch def 3 idiv /grefy exch def
1093 elt 12 mod dup 3 mod /nrefx exch def 3 idiv /nrefy exch def
1094 } { first { % first string
1095 /first false def
1096 elt GetPathBox
1097 %% Bounding box of overall glyphs.
1098 /LEFT LLX def
1099 /RIGHT URX def
1100 /TOP URY def
1101 /BOTTOM LLY def
1102 /WIDTH RIGHT LEFT sub def
1103 [ elt 0 0 ]
1104 } { % other strings
1105 elt GetPathBox
1106 /width URX LLX sub def
1107 /height URY LLY sub def
1108 /left LEFT [ 0 WIDTH 2 div WIDTH ] grefx get add
1109 [ 0 width 2 div width ] nrefx get sub def
1110 /bottom [ TOP 0 BOTTOM TOP BOTTOM add 2 div ] grefy get
1111 [ height LLY neg 0 height 2 div ] nrefy get sub def
1112 %% Update bounding box
1113 left LEFT lt { /LEFT left def } if
1114 left width add RIGHT gt { /RIGHT left width add def } if
1115 /WIDTH RIGHT LEFT sub def
1116 bottom BOTTOM lt { /BOTTOM bottom def } if
1117 bottom height add TOP gt { /TOP bottom height add def } if
1118 [ elt left LLX sub bottom LLY sub ]
1119 } ifelse } ifelse } ifelse
1120 } forall ] /components exch def
1121 grestore
1122
1123 %% Reflect special effects.
1124 SpecialEffect
1125
1126 %% Draw components while ignoring effects other than shadow and outline.
1127 components ShowComponents
1128
1129 /Composing false def
1130 } def
1131 %%%% End of character composition handler
1132
1133 "
1134 "PostScript code for printing character composition.")
1135
1136 (defun ps-mule-string-ascii (str)
1137 (ps-set-font ps-current-font)
1138 (string-as-unibyte (encode-coding-string str 'iso-latin-1)))
1139
1140 ;; Encode STR for a font specified by FONT-SPEC and return the result.
1141 ;; If necessary, it generates the PostScript code for the font and glyphs to
1142 ;; print STR. If optional 4th arg HEADER-P is non-nil, it is assumed that STR
1143 ;; is for headers.
1144 (defun ps-mule-string-encoding (font-spec str &optional no-setfont header-p)
1145 (let ((encoding (ps-mule-font-spec-encoding font-spec)))
1146 (setq str
1147 (string-as-unibyte
1148 (cond ((coding-system-p encoding)
1149 (encode-coding-string str encoding))
1150 ((functionp encoding)
1151 (funcall encoding str))
1152 (encoding
1153 (error "Invalid coding system or function: %s" encoding))
1154 (t
1155 str))))
1156 (if (ps-mule-font-spec-src font-spec)
1157 (ps-mule-prepare-font font-spec str ps-mule-current-charset
1158 (or no-setfont header-p)
1159 header-p)
1160 (or no-setfont
1161 (ps-set-font ps-current-font)))
1162 str))
1163
1164 ;; Bitmap font support
1165
1166 (defvar ps-mule-bitmap-prologue-generated nil)
1167
1168 (defconst ps-mule-bitmap-prologue
1169 "%%%% Bitmap font handler
1170
1171 /str7 7 string def % working area
1172
1173 %% We grow the dictionary one bunch (1024 entries) by one.
1174 /BitmapDictArray 256 array def
1175 /BitmapDictLength 1024 def
1176 /BitmapDictIndex -1 def
1177
1178 /NewBitmapDict { % -- |- --
1179 /BitmapDictIndex BitmapDictIndex 1 add def
1180 BitmapDictArray BitmapDictIndex BitmapDictLength dict put
1181 } bind def
1182
1183 %% Make at least one dictionary.
1184 NewBitmapDict
1185
1186 /AddBitmap { % gloval-charname bitmap-data |- --
1187 BitmapDictArray BitmapDictIndex get
1188 dup length BitmapDictLength ge {
1189 pop
1190 NewBitmapDict
1191 BitmapDictArray BitmapDictIndex get
1192 } if
1193 3 1 roll put
1194 } bind def
1195
1196 /GetBitmap { % gloval-charname |- bitmap-data
1197 0 1 BitmapDictIndex { BitmapDictArray exch get begin } for
1198 load
1199 0 1 BitmapDictIndex { pop end } for
1200 } bind def
1201
1202 %% Return a global character name which can be used as a key in the
1203 %% bitmap dictionary.
1204 /GlobalCharName { % fontidx code1 code2 |- gloval-charname
1205 exch 256 mul add exch 65536 mul add 16777216 add 16 str7 cvrs 0 66 put
1206 str7 cvn
1207 } bind def
1208
1209 %% Character code holder for a 2-byte character.
1210 /FirstCode -1 def
1211
1212 %% Glyph rendering procedure
1213 /BuildGlyphCommon { % fontdict charname |- --
1214 1 index /FontDimension get 1 eq { /FirstCode 0 store } if
1215 NameIndexDict exch get % STACK: fontdict charcode
1216 FirstCode 0 lt { %ifelse
1217 %% This is the first byte of a 2-byte character. Just
1218 %% remember it for the moment.
1219 /FirstCode exch store
1220 pop
1221 0 0 setcharwidth
1222 } {
1223 1 index /FontSize get /size exch def
1224 1 index /FontSpaceWidthRatio get /ratio exch def
1225 1 index /FontIndex get exch FirstCode exch
1226 GlobalCharName GetBitmap /bmp exch def
1227 %% bmp == [ DWIDTH BBX-WIDTH BBX-HEIGHT BBX-XOFF BBX-YOFF BITMAP ]
1228 Composing { %ifelse
1229 /FontMatrix get [ exch { size div } forall ] /mtrx exch def
1230 bmp 3 get bmp 4 get mtrx transform
1231 /LLY exch def /LLX exch def
1232 bmp 1 get bmp 3 get add bmp 2 get bmp 4 get add mtrx transform
1233 /URY exch def /URX exch def
1234 } {
1235 pop
1236 } ifelse
1237 /FirstCode -1 store
1238
1239 bmp 0 get size div 0 % wx wy
1240 setcharwidth % We can't use setcachedevice here.
1241
1242 bmp 1 get 0 gt bmp 2 get 0 gt and {
1243 bmp 1 get bmp 2 get % width height
1244 true % polarity
1245 [ size 0 0 size neg bmp 3 get neg bmp 2 get bmp 4 get add ] % matrix
1246 bmp 5 1 getinterval cvx % datasrc
1247 imagemask
1248 } if
1249 } ifelse
1250 } bind def
1251
1252 /BuildCharCommon {
1253 1 index /Encoding get exch get
1254 1 index /BuildGlyph get exec
1255 } bind def
1256
1257 %% Bitmap font creator
1258
1259 %% Common Encoding shared by all bitmap fonts.
1260 /EncodingCommon 256 array def
1261 %% Mapping table from character name to character code.
1262 /NameIndexDict 256 dict def
1263 0 1 255 { %for
1264 /idx exch def
1265 /idxname idx 256 add 16 (XXX) cvrs dup 0 67 put cvn def % `C' == 67
1266 EncodingCommon idx idxname put
1267 NameIndexDict idxname idx put
1268 } for
1269
1270 /GlobalFontIndex 0 def
1271
1272 %% fontname dim col fontsize relative-compose baseline-offset fbbx |- --
1273 /BitmapFont {
1274 15 dict begin
1275 /FontBBox exch def
1276 /BaselineOffset exch def
1277 /RelativeCompose exch def
1278 /FontSize exch def
1279 /FontBBox [ FontBBox { FontSize div } forall ] def
1280 FontBBox 2 get FontBBox 0 get sub exch div
1281 /FontSpaceWidthRatio exch def
1282 /FontDimension exch def
1283 /FontIndex GlobalFontIndex def
1284 /FontType 3 def
1285 /FontMatrix matrix def
1286 /Encoding EncodingCommon def
1287 /BuildGlyph { BuildGlyphCommon } def
1288 /BuildChar { BuildCharCommon } def
1289 currentdict end
1290 definefont pop
1291 /GlobalFontIndex GlobalFontIndex 1 add def
1292 } bind def
1293
1294 %% Define a new bitmap font.
1295 %% fontname dim col fontsize relative-compose baseline-offset fbbx |- --
1296 /NF {
1297 /fbbx exch def
1298 %% Convert BDF's FontBoundingBox to PostScript's FontBBox
1299 [ fbbx 2 get fbbx 3 get
1300 fbbx 2 get fbbx 0 get add fbbx 3 get fbbx 1 get add ]
1301 BitmapFont
1302 } bind def
1303
1304 %% Define a glyph for the specified font and character.
1305 /NG { % fontname charcode bitmap-data |- --
1306 /bmp exch def
1307 exch findfont dup /BaselineOffset get bmp 4 get add bmp exch 4 exch put
1308 /FontIndex get exch
1309 dup 256 idiv exch 256 mod GlobalCharName
1310 bmp AddBitmap
1311 } bind def
1312 %%%% End of bitmap font handler
1313
1314 ")
1315
1316 ;; External library support.
1317
1318 ;; The following three functions are to be called from external
1319 ;; libraries which support bitmap fonts (e.g. `bdf') to get
1320 ;; appropriate PostScript code.
1321
1322 (defun ps-mule-generate-bitmap-prologue ()
1323 (unless ps-mule-bitmap-prologue-generated
1324 (setq ps-mule-bitmap-prologue-generated t)
1325 (list ps-mule-bitmap-prologue)))
1326
1327 (defun ps-mule-generate-bitmap-font (&rest args)
1328 (list (apply 'format "/%s %d %d %f %S %d %S NF\n" args)))
1329
1330 (defun ps-mule-generate-bitmap-glyph (font-name code dwidth bbx bitmap)
1331 (format "/%s %d [ %d %d %d %d %d <%s> ] NG\n"
1332 font-name code
1333 dwidth (aref bbx 0) (aref bbx 1) (aref bbx 2) (aref bbx 3)
1334 bitmap))
1335
1336 ;; Mule specific initializers.
1337
1338 ;;;###autoload
1339 (defun ps-mule-initialize ()
1340 "Initialize global data for printing multi-byte characters."
1341 (setq ps-mule-font-cache nil
1342 ps-mule-prologue-generated nil
1343 ps-mule-composition-prologue-generated nil
1344 ps-mule-bitmap-prologue-generated nil)
1345 (mapcar `(lambda (x) (setcar (nthcdr 2 x) nil))
1346 ps-mule-external-libraries))
1347
1348 (defvar ps-mule-header-charsets nil)
1349
1350 ;;;###autoload
1351 (defun ps-mule-encode-header-string (string fonttag)
1352 "Generate PostScript code for ploting STRING by font FONTTAG.
1353 FONTTAG should be a string \"/h0\" or \"/h1\"."
1354 (setq string (cond ((not (stringp string))
1355 "")
1356 ((multibyte-string-p string)
1357 (copy-sequence string))
1358 (t
1359 (string-make-multibyte string))))
1360 (when ps-mule-header-charsets
1361 (if (eq (car ps-mule-header-charsets) 'latin-iso8859-1)
1362 ;; Latin1 characters can be printed by the standard PostScript
1363 ;; font. Converts the other non-ASCII characters to `?'.
1364 (let ((len (length string))
1365 (i 0))
1366 (while (< i len)
1367 (or (memq (char-charset (aref string i)) '(ascii latin-iso8859-1))
1368 (aset string i ??))
1369 (setq i (1+ i)))
1370 (setq string (encode-coding-string string 'iso-latin-1)))
1371 ;; We must prepare a font for the first non-ASCII and non-Latin1
1372 ;; character in STRING.
1373 (let* ((ps-current-font (if (string= fonttag "/h0") 0 1))
1374 (ps-mule-current-charset (car ps-mule-header-charsets))
1375 (font-type (car (nth ps-current-font
1376 (ps-font-alist 'ps-font-for-header))))
1377 (font-spec (ps-mule-get-font-spec ps-mule-current-charset
1378 font-type)))
1379 (if (or (not font-spec)
1380 (/= (charset-dimension ps-mule-current-charset) 1))
1381 ;; We don't have a proper font, or we can't print them on
1382 ;; header because this kind of charset is not ASCII
1383 ;; compatible.
1384 (let ((len (length string))
1385 (i 0))
1386 (while (< i len)
1387 (or (memq (char-charset (aref string i))
1388 '(ascii latin-iso8859-1))
1389 (aset string i ??))
1390 (setq i (1+ i)))
1391 (setq string (encode-coding-string string 'iso-latin-1)))
1392 (let ((charsets (list 'ascii (car ps-mule-header-charsets)))
1393 (len (length string))
1394 (i 0))
1395 (while (< i len)
1396 (or (memq (char-charset (aref string i)) charsets)
1397 (aset string i ??))
1398 (setq i (1+ i))))
1399 (setq string (ps-mule-string-encoding font-spec string nil t))))))
1400 string)
1401
1402 (defun ps-mule-show-warning (charsets from to header-footer-list)
1403 (let ((table (make-category-table))
1404 (buf (current-buffer))
1405 (max-unprintable-chars 15)
1406 char-pos-list)
1407 (define-category ?u "Unprintable charset" table)
1408 (dolist (cs charsets)
1409 (modify-category-entry (make-char cs) ?u table))
1410 (with-category-table table
1411 (save-excursion
1412 (goto-char from)
1413 (while (and (<= (length char-pos-list) max-unprintable-chars)
1414 (re-search-forward "\\cu" to t))
1415 (push (cons (preceding-char) (1- (point))) char-pos-list))))
1416 (with-output-to-temp-buffer "*Warning*"
1417 (with-current-buffer standard-output
1418 (when char-pos-list
1419 (let ((func #'(lambda (buf pos)
1420 (when (buffer-live-p buf)
1421 (pop-to-buffer buf)
1422 (goto-char pos))))
1423 (more nil))
1424 (if (>= (length char-pos-list) max-unprintable-chars)
1425 (setq char-pos-list (cdr char-pos-list)
1426 more t))
1427 (insert "These characters in the buffer can't be printed:\n")
1428 (dolist (elt (nreverse char-pos-list))
1429 (insert " ")
1430 (insert-text-button (string (car elt))
1431 :type 'help-xref
1432 'help-echo
1433 "mouse-2, RET: jump to this character"
1434 'help-function func
1435 'help-args (list buf (cdr elt)))
1436 (insert ","))
1437 (if more
1438 (insert " and more...")
1439 ;; Delete the last comma.
1440 (delete-char -1))
1441 (insert "\nClick them to jump to the buffer position,\n"
1442 (substitute-command-keys "\
1443 or \\[universal-argument] \\[what-cursor-position] will give information about them.\n"))))
1444
1445 (with-category-table table
1446 (let (string-list idx)
1447 (dolist (elt header-footer-list)
1448 (when (stringp elt)
1449 (when (string-match "\\cu+" elt)
1450 (setq elt (copy-sequence elt))
1451 (put-text-property (match-beginning 0) (match-end 0)
1452 'face 'highlight elt)
1453 (while (string-match "\\cu+" elt (match-end 0))
1454 (put-text-property (match-beginning 0) (match-end 0)
1455 'face 'highlight elt))
1456 (push elt string-list))))
1457 (when string-list
1458 (insert
1459 "These highlighted characters in header/footer can't be printed:\n")
1460 (dolist (elt string-list)
1461 (insert " " elt "\n")))))))))
1462
1463 ;;;###autoload
1464 (defun ps-mule-begin-job (from to)
1465 "Start printing job for multi-byte chars between FROM and TO.
1466 This checks if all multi-byte characters in the region are printable or not."
1467 (setq ps-mule-charset-list nil
1468 ps-mule-header-charsets nil
1469 ps-mule-font-info-database
1470 (cond ((eq ps-multibyte-buffer 'non-latin-printer)
1471 ps-mule-font-info-database-ps)
1472 ((eq ps-multibyte-buffer 'bdf-font)
1473 ps-mule-font-info-database-bdf)
1474 ((eq ps-multibyte-buffer 'bdf-font-except-latin)
1475 ps-mule-font-info-database-ps-bdf)
1476 (t
1477 ps-mule-font-info-database-default)))
1478 (and (boundp 'enable-multibyte-characters)
1479 enable-multibyte-characters
1480 ;; Initialize `ps-mule-charset-list'. If some characters aren't
1481 ;; printable, warn it.
1482 (let ((header-footer-list (ps-header-footer-string))
1483 unprintable-charsets)
1484 (setq ps-mule-charset-list
1485 (delq 'ascii (delq 'eight-bit-control
1486 (delq 'eight-bit-graphic
1487 (find-charset-region
1488 from to ps-print-translation-table))))
1489 ps-mule-header-charsets
1490 (delq 'ascii (delq 'eight-bit-control
1491 (delq 'eight-bit-graphic
1492 (find-charset-string
1493 (mapconcat
1494 'identity header-footer-list "")
1495 ps-print-translation-table)))))
1496 (dolist (cs ps-mule-charset-list)
1497 (or (ps-mule-printable-p cs)
1498 (push cs unprintable-charsets)))
1499 (dolist (cs ps-mule-header-charsets)
1500 (or (ps-mule-printable-p cs)
1501 (memq cs unprintable-charsets)
1502 (push cs unprintable-charsets)))
1503 (when unprintable-charsets
1504 (ps-mule-show-warning unprintable-charsets from to
1505 header-footer-list)
1506 (or
1507 (y-or-n-p "Font for some characters not found, continue anyway? ")
1508 (error "Printing cancelled")))
1509
1510 (or ps-mule-composition-prologue-generated
1511 (let ((use-composition (nth 2 (find-composition from to))))
1512 (or use-composition
1513 (let (str)
1514 (while header-footer-list
1515 (setq str (car header-footer-list))
1516 (if (and (stringp str)
1517 (nth 2 (find-composition 0 (length str) str)))
1518 (setq use-composition t
1519 header-footer-list nil)
1520 (setq header-footer-list (cdr header-footer-list))))))
1521 (when use-composition
1522 (progn
1523 (ps-mule-prologue-generated)
1524 (ps-output-prologue ps-mule-composition-prologue)
1525 (setq ps-mule-composition-prologue-generated t)))))))
1526
1527 (setq ps-mule-current-charset 'ascii)
1528
1529 (if (or ps-mule-charset-list ps-mule-header-charsets)
1530 (dolist (elt (append ps-mule-header-charsets ps-mule-charset-list))
1531 (ps-mule-prologue-generated)
1532 (ps-mule-init-external-library (ps-mule-get-font-spec elt 'normal))))
1533
1534 ;; If ASCII font is also specified in ps-mule-font-info-database,
1535 ;; use it instead of what specified in ps-font-info-database.
1536 (let ((font-spec (ps-mule-get-font-spec 'ascii 'normal)))
1537 (if font-spec
1538 (progn
1539 (ps-mule-prologue-generated)
1540 (ps-mule-init-external-library font-spec)
1541 (let ((font (ps-font-alist 'ps-font-for-text))
1542 (ps-current-font 0))
1543 (while font
1544 ;; Be sure to download a glyph for SPACE in advance.
1545 (ps-mule-prepare-font (ps-mule-get-font-spec 'ascii (car font))
1546 " " 'ascii 'no-setfont)
1547 (setq font (cdr font)
1548 ps-current-font (1+ ps-current-font)))))))
1549
1550 ;; If the header contains non-ASCII and non-Latin1 characters, prepare a font
1551 ;; and glyphs for the first occurrence of such characters.
1552 (if (and ps-mule-header-charsets
1553 (not (eq (car ps-mule-header-charsets) 'latin-iso8859-1))
1554 (= (charset-dimension (car ps-mule-header-charsets)) 1))
1555 (let ((font-spec (ps-mule-get-font-spec (car ps-mule-header-charsets)
1556 'normal)))
1557 (if font-spec
1558 ;; Be sure to download glyphs for "0123456789/" in advance for page
1559 ;; numbering.
1560 (let ((ps-current-font 0))
1561 (ps-mule-prepare-font font-spec "0123456789/" 'ascii t t)))))
1562
1563 (if ps-mule-charset-list
1564 ;; We must change this regexp for multi-byte buffer.
1565 (setq ps-control-or-escape-regexp
1566 (cond ((eq ps-print-control-characters '8-bit)
1567 "[^\040-\176]")
1568 ((eq ps-print-control-characters 'control-8-bit)
1569 (string-as-multibyte "[^\040-\176\240-\377]"))
1570 ((eq ps-print-control-characters 'control)
1571 (string-as-multibyte "[^\040-\176\200-\377]"))
1572 (t (string-as-multibyte "[^\000-\011\013\015-\377]"))))))
1573
1574 ;;;###autoload
1575 (defun ps-mule-begin-page ()
1576 (setq ps-mule-current-charset 'ascii))
1577
1578
1579 (provide 'ps-mule)
1580
1581 ;;; arch-tag: bca017b2-66a7-4e59-8584-103e749eadbe
1582 ;;; ps-mule.el ends here