(mouse-choose-completion): Replace `buffer-substring' with
[emacs.git] / lisp / ps-mule.el
blob1be2eafba9d8f3ca6e2dd52c987942d751b85c5d
1 ;;; ps-mule.el --- provide multi-byte character facility to ps-print
3 ;; Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004,
4 ;; 2005 Free Software Foundation, Inc.
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>
13 ;; This file is part of GNU Emacs.
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.
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.
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.
30 ;;; Commentary:
32 ;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
34 ;; About ps-mule
35 ;; -------------
37 ;; This package is used for ps-print to print multi-byte buffer.
39 ;; See also ps-print.el.
42 ;; Printing Multi-byte Buffer
43 ;; --------------------------
45 ;; The variable `ps-multibyte-buffer' specifies the ps-print multi-byte buffer
46 ;; handling.
48 ;; Valid values for `ps-multibyte-buffer' are:
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).
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.
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).
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'.
86 ;; Any other value is treated as nil.
88 ;; The default is nil.
90 ;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
92 ;;; Code:
94 (eval-and-compile
95 (require 'ps-print)
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)
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)))
169 ;;;###autoload
170 (defcustom ps-multibyte-buffer nil
171 "*Specifies the multi-byte buffer handling.
173 Valid values are:
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).
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.
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).
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'.
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)
216 (defvar ps-mule-font-info-database
218 "Alist of charsets with the corresponding font information.
219 Each element has the form:
221 (CHARSET (FONT-TYPE FONT-SRC FONT-NAME ENCODING BYTES) ...)
223 Where
225 CHARSET is a charset (symbol) for this font family,
227 FONT-TYPE is a font type: normal, bold, italic, or bold-italic.
229 FONT-SRC is a font source: builtin, bdf, vflib, or nil.
231 If FONT-SRC is builtin, FONT-NAME is a built-in PostScript font name.
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.
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.
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.
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.
248 BYTES specifies how many bytes each character has in the encoded byte
249 sequence; it should be 1 or 2.
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.
256 See also the variable `ps-font-info-database'.")
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.")
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)
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))
284 "Sample setting of the `ps-mule-font-info-database' to use builtin PS font.
286 Currently, data for Japanese and Korean PostScript printers are listed.")
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.
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.
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.
394 See also `ps-mule-font-info-database-ps-bdf'.")
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.
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.
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'.
409 See also `ps-mule-font-info-database-bdf'.")
411 ;; Two typical encoding functions for PostScript fonts.
413 (defun ps-mule-encode-7bit (string)
414 (ps-mule-encode-bit string 0))
416 (defun ps-mule-encode-8bit (string)
417 (ps-mule-encode-bit string 128))
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))
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
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))
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))
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))
486 ;; A charset which we are now processing.
487 (defvar ps-mule-current-charset nil)
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:
493 (FONT-SRC FONT-NAME ENCODING BYTES)
495 FONT-SPEC is extracted from `ps-mule-font-info-database'.
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))))))
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))
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)))
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:
531 (FONT-SRC FEATURE INITIALIZED-P PROLOGUE-FUNC FONT-FUNC GLYPHS-FUNC)
533 FONT-SRC is the font source: builtin, bdf, pcf, or vflib.
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'.
539 INITIALIZED-P indicates if this library is initialized or not.
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.
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.
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.")
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)))))
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)
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.
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))
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))
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))))))
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.
624 The generated code is inserted on prologue part except the code that sets the
625 current font (using PostScript procedure `FM').
627 If optional 4th arg NO-SETFONT is non-nil, don't generate the code for setting
628 the current font.
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))))))
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))))
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)))
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)
686 ;; This is a PostScript code inserted in the header of generated PostScript.
687 (defconst ps-mule-prologue
688 "%%%% Start of Mule Section
690 %% Working dictionary for general use.
691 /MuleDict 10 dict def
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
700 /BaselineOffset 0 def
701 } ifelse
702 currentdict /RelativeCompose known not {
703 /RelativeCompose [ 0 0.1 ] def
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
713 } def
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
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
726 } def
728 /CurrentFont false def
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
737 font findfont setfont
738 } ifelse
739 } bind def
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
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
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
760 %%%% End of Mule Section
763 "PostScript code for printing multi-byte characters.")
765 (defvar ps-mule-prologue-generated nil)
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)))
772 (defun ps-mule-find-wrappoint (from to char-width &optional composition)
773 "Find the longest sequence which is printable in the current line.
775 The search starts at FROM and goes until TO.
777 Optional 4th arg COMPOSITION, if non-nil, is information of
778 composition starting at FROM.
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.
785 CHAR-WIDTH is the average width of ASCII characters in the current font.
787 Returns the value:
789 (ENDPOS . RUN-WIDTH)
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)))))
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.
820 It is assumed that all characters in this region belong to the same charset.
822 Optional argument BG-COLOR specifies background color.
824 Returns the value:
826 (ENDPOS . RUN-WIDTH)
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)
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"))
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"))
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))
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))
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.
877 It is assumed that all characters in this region belong to the same
878 composition.
880 Optional argument BG-COLOR specifies background color.
882 Returns the value:
884 (ENDPOS . RUN-WIDTH)
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))
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.
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)
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")))
959 ;; Composite font support
961 (defvar ps-mule-composition-prologue-generated nil)
963 (defconst ps-mule-composition-prologue
964 "%%%% Character composition handler
965 /RelativeCompositionSkip 0.4 def
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
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
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
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
1007 %% Draw COMPONENTS which have the form [ font0? [str0 xoff0 yoff0] ... ].
1008 /ShowComponents { % components |- -
1009 LEFT 0 lt { LEFT neg 0 rmoveto } if
1011 dup type /nametype eq { % font
1013 } { % [ str xoff yoff ]
1014 gsave
1015 aload pop rmoveto ShowWithEffect
1016 grestore
1017 } ifelse
1018 } forall
1019 RIGHT 0 rmoveto
1020 } def
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 relative false eq {
1043 %% Disable relative composition by setting sufficiently low
1044 %% and high positions.
1045 /relative [ -100000 100000 ] def
1046 } if
1048 /relative [ -100000 100000 ] def
1049 } ifelse
1050 [ elt 0 0 ]
1051 } { % other strings
1052 elt GetPathBox
1053 [ elt % str
1054 LLX 0 lt { RIGHT } { 0 } ifelse % xoff
1055 LLY relative 1 get ge { % compose on TOP
1056 TOP LLY sub RelativeCompositionSkip add % yoff
1057 /TOP TOP URY LLY sub add RelativeCompositionSkip add def
1058 } { URY relative 0 get le { % compose under BOTTOM
1059 BOTTOM URY sub RelativeCompositionSkip sub % yoff
1060 /BOTTOM BOTTOM URY LLY sub sub
1061 RelativeCompositionSkip sub def
1063 0 % yoff
1064 URY TOP gt { /TOP URY def } if
1065 LLY BOTTOM lt { /BOTTOM LLY def } if
1066 } ifelse } ifelse
1068 URX RIGHT gt { /RIGHT URX def } if
1069 } ifelse } ifelse
1070 } forall ] /components exch def
1071 grestore
1073 %% Reflect special effects.
1074 SpecialEffect
1076 %% Draw components while ignoring effects other than shadow and outline.
1077 components ShowComponents
1078 /Composing false def
1080 } def
1082 %% Show rule-base composition.
1083 /RBC { % [ font0? str0 rule1 font1? str1 rule2 ... strN ] |- --
1084 /components exch def
1085 /Composing true def
1086 /first true def
1087 gsave
1088 [ components {
1089 /elt exch def
1090 elt type /nametype eq { % font
1091 elt dup FM
1092 } { elt type /integertype eq { % rule
1093 %% This RULE decoding should be compatible with macro
1094 %% COMPOSITION_DECODE_RULE in emacs/src/composite.h.
1095 elt 12 idiv dup 3 mod /grefx exch def 3 idiv /grefy exch def
1096 elt 12 mod dup 3 mod /nrefx exch def 3 idiv /nrefy exch def
1097 } { first { % first string
1098 /first false def
1099 elt GetPathBox
1100 %% Bounding box of overall glyphs.
1101 /LEFT LLX def
1102 /RIGHT URX def
1103 /TOP URY def
1104 /BOTTOM LLY def
1105 /WIDTH RIGHT LEFT sub def
1106 [ elt 0 0 ]
1107 } { % other strings
1108 elt GetPathBox
1109 /width URX LLX sub def
1110 /height URY LLY sub def
1111 /left LEFT [ 0 WIDTH 2 div WIDTH ] grefx get add
1112 [ 0 width 2 div width ] nrefx get sub def
1113 /bottom [ TOP 0 BOTTOM TOP BOTTOM add 2 div ] grefy get
1114 [ height LLY neg 0 height 2 div ] nrefy get sub def
1115 %% Update bounding box
1116 left LEFT lt { /LEFT left def } if
1117 left width add RIGHT gt { /RIGHT left width add def } if
1118 /WIDTH RIGHT LEFT sub def
1119 bottom BOTTOM lt { /BOTTOM bottom def } if
1120 bottom height add TOP gt { /TOP bottom height add def } if
1121 [ elt left LLX sub bottom LLY sub ]
1122 } ifelse } ifelse } ifelse
1123 } forall ] /components exch def
1124 grestore
1126 %% Reflect special effects.
1127 SpecialEffect
1129 %% Draw components while ignoring effects other than shadow and outline.
1130 components ShowComponents
1132 /Composing false def
1133 } def
1134 %%%% End of character composition handler
1137 "PostScript code for printing character composition.")
1139 (defun ps-mule-string-ascii (str)
1140 (ps-set-font ps-current-font)
1141 (string-as-unibyte (encode-coding-string str 'iso-latin-1)))
1143 ;; Encode STR for a font specified by FONT-SPEC and return the result.
1144 ;; If necessary, it generates the PostScript code for the font and glyphs to
1145 ;; print STR. If optional 4th arg HEADER-P is non-nil, it is assumed that STR
1146 ;; is for headers.
1147 (defun ps-mule-string-encoding (font-spec str &optional no-setfont header-p)
1148 (let ((encoding (ps-mule-font-spec-encoding font-spec)))
1149 (setq str
1150 (string-as-unibyte
1151 (cond ((coding-system-p encoding)
1152 (encode-coding-string str encoding))
1153 ((functionp encoding)
1154 (funcall encoding str))
1155 (encoding
1156 (error "Invalid coding system or function: %s" encoding))
1158 str))))
1159 (if (ps-mule-font-spec-src font-spec)
1160 (ps-mule-prepare-font font-spec str ps-mule-current-charset
1161 (or no-setfont header-p)
1162 header-p)
1163 (or no-setfont
1164 (ps-set-font ps-current-font)))
1165 str))
1167 ;; Bitmap font support
1169 (defvar ps-mule-bitmap-prologue-generated nil)
1171 (defconst ps-mule-bitmap-prologue
1172 "%%%% Bitmap font handler
1174 /str7 7 string def % working area
1176 %% We grow the dictionary one bunch (1024 entries) by one.
1177 /BitmapDictArray 256 array def
1178 /BitmapDictLength 1024 def
1179 /BitmapDictIndex -1 def
1181 /NewBitmapDict { % -- |- --
1182 /BitmapDictIndex BitmapDictIndex 1 add def
1183 BitmapDictArray BitmapDictIndex BitmapDictLength dict put
1184 } bind def
1186 %% Make at least one dictionary.
1187 NewBitmapDict
1189 /AddBitmap { % gloval-charname bitmap-data |- --
1190 BitmapDictArray BitmapDictIndex get
1191 dup length BitmapDictLength ge {
1193 NewBitmapDict
1194 BitmapDictArray BitmapDictIndex get
1195 } if
1196 3 1 roll put
1197 } bind def
1199 /GetBitmap { % gloval-charname |- bitmap-data
1200 0 1 BitmapDictIndex { BitmapDictArray exch get begin } for
1201 load
1202 0 1 BitmapDictIndex { pop end } for
1203 } bind def
1205 %% Return a global character name which can be used as a key in the
1206 %% bitmap dictionary.
1207 /GlobalCharName { % fontidx code1 code2 |- gloval-charname
1208 exch 256 mul add exch 65536 mul add 16777216 add 16 str7 cvrs 0 66 put
1209 str7 cvn
1210 } bind def
1212 %% Character code holder for a 2-byte character.
1213 /FirstCode -1 def
1215 %% Glyph rendering procedure
1216 /BuildGlyphCommon { % fontdict charname |- --
1217 1 index /FontDimension get 1 eq { /FirstCode 0 store } if
1218 NameIndexDict exch get % STACK: fontdict charcode
1219 FirstCode 0 lt { %ifelse
1220 %% This is the first byte of a 2-byte character. Just
1221 %% remember it for the moment.
1222 /FirstCode exch store
1224 0 0 setcharwidth
1226 1 index /FontSize get /size exch def
1227 1 index /FontSpaceWidthRatio get /ratio exch def
1228 1 index /FontIndex get exch FirstCode exch
1229 GlobalCharName GetBitmap /bmp exch def
1230 %% bmp == [ DWIDTH BBX-WIDTH BBX-HEIGHT BBX-XOFF BBX-YOFF BITMAP ]
1231 Composing { %ifelse
1232 /FontMatrix get [ exch { size div } forall ] /mtrx exch def
1233 bmp 3 get bmp 4 get mtrx transform
1234 /LLY exch def /LLX exch def
1235 bmp 1 get bmp 3 get add bmp 2 get bmp 4 get add mtrx transform
1236 /URY exch def /URX exch def
1239 } ifelse
1240 /FirstCode -1 store
1242 bmp 0 get size div 0 % wx wy
1243 setcharwidth % We can't use setcachedevice here.
1245 bmp 1 get 0 gt bmp 2 get 0 gt and {
1246 bmp 1 get bmp 2 get % width height
1247 true % polarity
1248 [ size 0 0 size neg bmp 3 get neg bmp 2 get bmp 4 get add ] % matrix
1249 bmp 5 1 getinterval cvx % datasrc
1250 imagemask
1251 } if
1252 } ifelse
1253 } bind def
1255 /BuildCharCommon {
1256 1 index /Encoding get exch get
1257 1 index /BuildGlyph get exec
1258 } bind def
1260 %% Bitmap font creator
1262 %% Common Encoding shared by all bitmap fonts.
1263 /EncodingCommon 256 array def
1264 %% Mapping table from character name to character code.
1265 /NameIndexDict 256 dict def
1266 0 1 255 { %for
1267 /idx exch def
1268 /idxname idx 256 add 16 (XXX) cvrs dup 0 67 put cvn def % `C' == 67
1269 EncodingCommon idx idxname put
1270 NameIndexDict idxname idx put
1271 } for
1273 /GlobalFontIndex 0 def
1275 %% fontname dim col fontsize relative-compose baseline-offset fbbx |- --
1276 /BitmapFont {
1277 15 dict begin
1278 /FontBBox exch def
1279 /BaselineOffset exch def
1280 /RelativeCompose exch def
1281 /FontSize exch def
1282 /FontBBox [ FontBBox { FontSize div } forall ] def
1283 FontBBox 2 get FontBBox 0 get sub exch div
1284 /FontSpaceWidthRatio exch def
1285 /FontDimension exch def
1286 /FontIndex GlobalFontIndex def
1287 /FontType 3 def
1288 /FontMatrix matrix def
1289 /Encoding EncodingCommon def
1290 /BuildGlyph { BuildGlyphCommon } def
1291 /BuildChar { BuildCharCommon } def
1292 currentdict end
1293 definefont pop
1294 /GlobalFontIndex GlobalFontIndex 1 add def
1295 } bind def
1297 %% Define a new bitmap font.
1298 %% fontname dim col fontsize relative-compose baseline-offset fbbx |- --
1299 /NF {
1300 /fbbx exch def
1301 %% Convert BDF's FontBoundingBox to PostScript's FontBBox
1302 [ fbbx 2 get fbbx 3 get
1303 fbbx 2 get fbbx 0 get add fbbx 3 get fbbx 1 get add ]
1304 BitmapFont
1305 } bind def
1307 %% Define a glyph for the specified font and character.
1308 /NG { % fontname charcode bitmap-data |- --
1309 /bmp exch def
1310 exch findfont dup /BaselineOffset get bmp 4 get add bmp exch 4 exch put
1311 /FontIndex get exch
1312 dup 256 idiv exch 256 mod GlobalCharName
1313 bmp AddBitmap
1314 } bind def
1315 %%%% End of bitmap font handler
1319 ;; External library support.
1321 ;; The following three functions are to be called from external
1322 ;; libraries which support bitmap fonts (e.g. `bdf') to get
1323 ;; appropriate PostScript code.
1325 (defun ps-mule-generate-bitmap-prologue ()
1326 (unless ps-mule-bitmap-prologue-generated
1327 (setq ps-mule-bitmap-prologue-generated t)
1328 (list ps-mule-bitmap-prologue)))
1330 (defun ps-mule-generate-bitmap-font (&rest args)
1331 (list (apply 'format "/%s %d %d %f %S %d %S NF\n" args)))
1333 (defun ps-mule-generate-bitmap-glyph (font-name code dwidth bbx bitmap)
1334 (format "/%s %d [ %d %d %d %d %d <%s> ] NG\n"
1335 font-name code
1336 dwidth (aref bbx 0) (aref bbx 1) (aref bbx 2) (aref bbx 3)
1337 bitmap))
1339 ;; Mule specific initializers.
1341 ;;;###autoload
1342 (defun ps-mule-initialize ()
1343 "Initialize global data for printing multi-byte characters."
1344 (setq ps-mule-font-cache nil
1345 ps-mule-prologue-generated nil
1346 ps-mule-composition-prologue-generated nil
1347 ps-mule-bitmap-prologue-generated nil)
1348 (mapcar `(lambda (x) (setcar (nthcdr 2 x) nil))
1349 ps-mule-external-libraries))
1351 (defvar ps-mule-header-charsets nil)
1353 ;;;###autoload
1354 (defun ps-mule-encode-header-string (string fonttag)
1355 "Generate PostScript code for ploting STRING by font FONTTAG.
1356 FONTTAG should be a string \"/h0\" or \"/h1\"."
1357 (setq string (cond ((not (stringp string))
1359 ((multibyte-string-p string)
1360 (copy-sequence string))
1362 (string-make-multibyte string))))
1363 (when ps-mule-header-charsets
1364 (if (eq (car ps-mule-header-charsets) 'latin-iso8859-1)
1365 ;; Latin1 characters can be printed by the standard PostScript
1366 ;; font. Converts the other non-ASCII characters to `?'.
1367 (let ((len (length string))
1368 (i 0))
1369 (while (< i len)
1370 (or (memq (char-charset (aref string i)) '(ascii latin-iso8859-1))
1371 (aset string i ??))
1372 (setq i (1+ i)))
1373 (setq string (encode-coding-string string 'iso-latin-1)))
1374 ;; We must prepare a font for the first non-ASCII and non-Latin1
1375 ;; character in STRING.
1376 (let* ((ps-current-font (if (string= fonttag "/h0") 0 1))
1377 (ps-mule-current-charset (car ps-mule-header-charsets))
1378 (font-type (car (nth ps-current-font
1379 (ps-font-alist 'ps-font-for-header))))
1380 (font-spec (ps-mule-get-font-spec ps-mule-current-charset
1381 font-type)))
1382 (if (or (not font-spec)
1383 (/= (charset-dimension ps-mule-current-charset) 1))
1384 ;; We don't have a proper font, or we can't print them on
1385 ;; header because this kind of charset is not ASCII
1386 ;; compatible.
1387 (let ((len (length string))
1388 (i 0))
1389 (while (< i len)
1390 (or (memq (char-charset (aref string i))
1391 '(ascii latin-iso8859-1))
1392 (aset string i ??))
1393 (setq i (1+ i)))
1394 (setq string (encode-coding-string string 'iso-latin-1)))
1395 (let ((charsets (list 'ascii (car ps-mule-header-charsets)))
1396 (len (length string))
1397 (i 0))
1398 (while (< i len)
1399 (or (memq (char-charset (aref string i)) charsets)
1400 (aset string i ??))
1401 (setq i (1+ i))))
1402 (setq string (ps-mule-string-encoding font-spec string nil t))))))
1403 string)
1405 (defun ps-mule-show-warning (charsets from to header-footer-list)
1406 (let ((table (make-category-table))
1407 (buf (current-buffer))
1408 (max-unprintable-chars 15)
1409 char-pos-list)
1410 (define-category ?u "Unprintable charset" table)
1411 (dolist (cs charsets)
1412 (modify-category-entry (make-char cs) ?u table))
1413 (with-category-table table
1414 (save-excursion
1415 (goto-char from)
1416 (while (and (<= (length char-pos-list) max-unprintable-chars)
1417 (re-search-forward "\\cu" to t))
1418 (or (aref ps-print-translation-table (preceding-char))
1419 (push (cons (preceding-char) (1- (point))) char-pos-list)))))
1420 (with-output-to-temp-buffer "*Warning*"
1421 (with-current-buffer standard-output
1422 (when char-pos-list
1423 (let ((func #'(lambda (buf pos)
1424 (when (buffer-live-p buf)
1425 (pop-to-buffer buf)
1426 (goto-char pos))))
1427 (more nil))
1428 (if (>= (length char-pos-list) max-unprintable-chars)
1429 (setq char-pos-list (cdr char-pos-list)
1430 more t))
1431 (insert "These characters in the buffer can't be printed:\n")
1432 (dolist (elt (nreverse char-pos-list))
1433 (insert " ")
1434 (insert-text-button (string (car elt))
1435 :type 'help-xref
1436 'help-echo
1437 "mouse-2, RET: jump to this character"
1438 'help-function func
1439 'help-args (list buf (cdr elt)))
1440 (insert ","))
1441 (if more
1442 (insert " and more...")
1443 ;; Delete the last comma.
1444 (delete-char -1))
1445 (insert "\nClick them to jump to the buffer position,\n"
1446 (substitute-command-keys "\
1447 or \\[universal-argument] \\[what-cursor-position] will give information about them.\n"))))
1449 (with-category-table table
1450 (let (string-list idx)
1451 (dolist (elt header-footer-list)
1452 (when (stringp elt)
1453 (when (string-match "\\cu+" elt)
1454 (setq elt (copy-sequence elt))
1455 (put-text-property (match-beginning 0) (match-end 0)
1456 'face 'highlight elt)
1457 (while (string-match "\\cu+" elt (match-end 0))
1458 (put-text-property (match-beginning 0) (match-end 0)
1459 'face 'highlight elt))
1460 (push elt string-list))))
1461 (when string-list
1462 (insert
1463 "These highlighted characters in header/footer can't be printed:\n")
1464 (dolist (elt string-list)
1465 (insert " " elt "\n")))))))))
1467 ;;;###autoload
1468 (defun ps-mule-begin-job (from to)
1469 "Start printing job for multi-byte chars between FROM and TO.
1470 This checks if all multi-byte characters in the region are printable or not."
1471 (setq ps-mule-charset-list nil
1472 ps-mule-header-charsets nil
1473 ps-mule-font-info-database
1474 (cond ((eq ps-multibyte-buffer 'non-latin-printer)
1475 ps-mule-font-info-database-ps)
1476 ((eq ps-multibyte-buffer 'bdf-font)
1477 ps-mule-font-info-database-bdf)
1478 ((eq ps-multibyte-buffer 'bdf-font-except-latin)
1479 ps-mule-font-info-database-ps-bdf)
1481 ps-mule-font-info-database-default)))
1482 (and (boundp 'enable-multibyte-characters)
1483 enable-multibyte-characters
1484 ;; Initialize `ps-mule-charset-list'. If some characters aren't
1485 ;; printable, warn it.
1486 (let ((header-footer-list (ps-header-footer-string))
1487 unprintable-charsets)
1488 (setq ps-mule-charset-list
1489 (delq 'ascii (delq 'eight-bit-control
1490 (delq 'eight-bit-graphic
1491 (find-charset-region
1492 from to ps-print-translation-table))))
1493 ps-mule-header-charsets
1494 (delq 'ascii (delq 'eight-bit-control
1495 (delq 'eight-bit-graphic
1496 (find-charset-string
1497 (mapconcat
1498 'identity header-footer-list "")
1499 ps-print-translation-table)))))
1500 (dolist (cs ps-mule-charset-list)
1501 (or (ps-mule-printable-p cs)
1502 (push cs unprintable-charsets)))
1503 (dolist (cs ps-mule-header-charsets)
1504 (or (ps-mule-printable-p cs)
1505 (memq cs unprintable-charsets)
1506 (push cs unprintable-charsets)))
1507 (when unprintable-charsets
1508 (ps-mule-show-warning unprintable-charsets from to
1509 header-footer-list)
1511 (y-or-n-p "Font for some characters not found, continue anyway? ")
1512 (error "Printing cancelled")))
1514 (or ps-mule-composition-prologue-generated
1515 (let ((use-composition (nth 2 (find-composition from to))))
1516 (or use-composition
1517 (let (str)
1518 (while header-footer-list
1519 (setq str (car header-footer-list))
1520 (if (and (stringp str)
1521 (nth 2 (find-composition 0 (length str) str)))
1522 (setq use-composition t
1523 header-footer-list nil)
1524 (setq header-footer-list (cdr header-footer-list))))))
1525 (when use-composition
1526 (progn
1527 (ps-mule-prologue-generated)
1528 (ps-output-prologue ps-mule-composition-prologue)
1529 (setq ps-mule-composition-prologue-generated t)))))))
1531 (setq ps-mule-current-charset 'ascii)
1533 (if (or ps-mule-charset-list ps-mule-header-charsets)
1534 (dolist (elt (append ps-mule-header-charsets ps-mule-charset-list))
1535 (ps-mule-prologue-generated)
1536 (ps-mule-init-external-library (ps-mule-get-font-spec elt 'normal))))
1538 ;; If ASCII font is also specified in ps-mule-font-info-database,
1539 ;; use it instead of what specified in ps-font-info-database.
1540 (let ((font-spec (ps-mule-get-font-spec 'ascii 'normal)))
1541 (if font-spec
1542 (progn
1543 (ps-mule-prologue-generated)
1544 (ps-mule-init-external-library font-spec)
1545 (let ((font (ps-font-alist 'ps-font-for-text))
1546 (ps-current-font 0))
1547 (while font
1548 ;; Be sure to download a glyph for SPACE in advance.
1549 (ps-mule-prepare-font (ps-mule-get-font-spec 'ascii (car font))
1550 " " 'ascii 'no-setfont)
1551 (setq font (cdr font)
1552 ps-current-font (1+ ps-current-font)))))))
1554 ;; If the header contains non-ASCII and non-Latin1 characters, prepare a font
1555 ;; and glyphs for the first occurrence of such characters.
1556 (if (and ps-mule-header-charsets
1557 (not (eq (car ps-mule-header-charsets) 'latin-iso8859-1))
1558 (= (charset-dimension (car ps-mule-header-charsets)) 1))
1559 (let ((font-spec (ps-mule-get-font-spec (car ps-mule-header-charsets)
1560 'normal)))
1561 (if font-spec
1562 ;; Be sure to download glyphs for "0123456789/" in advance for page
1563 ;; numbering.
1564 (let ((ps-current-font 0))
1565 (ps-mule-prepare-font font-spec "0123456789/" 'ascii t t)))))
1567 (if ps-mule-charset-list
1568 ;; We must change this regexp for multi-byte buffer.
1569 (setq ps-control-or-escape-regexp
1570 (cond ((eq ps-print-control-characters '8-bit)
1571 "[^\040-\176]")
1572 ((eq ps-print-control-characters 'control-8-bit)
1573 (string-as-multibyte "[^\040-\176\240-\377]"))
1574 ((eq ps-print-control-characters 'control)
1575 (string-as-multibyte "[^\040-\176\200-\377]"))
1576 (t (string-as-multibyte "[^\000-\011\013\015-\377]"))))))
1578 ;;;###autoload
1579 (defun ps-mule-begin-page ()
1580 (setq ps-mule-current-charset 'ascii))
1583 (provide 'ps-mule)
1585 ;;; arch-tag: bca017b2-66a7-4e59-8584-103e749eadbe
1586 ;;; ps-mule.el ends here