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