(ps-generate-string-list): Function deleted.
[bpt/emacs.git] / lisp / ps-mule.el
CommitLineData
e8af40ee 1;;; ps-mule.el --- provide multi-byte character facility to ps-print
2cb842ae 2
edf0b0c5
VJL
3;; Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003
4;; Free Software Foundation, Inc.
2cb842ae 5
e8af40ee 6;; Author: Vinicius Jose Latorre <vinicius@cpqd.com.br>
8b313639
GM
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>
e8af40ee 10;; Keywords: wp, print, PostScript, multibyte, mule
edf0b0c5 11;; Time-stamp: <2003/05/14 22:19:41 vinicius>
2cb842ae
KH
12
13;; This file is part of GNU Emacs.
14
15;; GNU Emacs is free software; you can redistribute it and/or modify
16;; it under the terms of the GNU General Public License as published by
17;; the Free Software Foundation; either version 2, or (at your option)
18;; any later version.
19
20;; GNU Emacs is distributed in the hope that it will be useful,
21;; but WITHOUT ANY WARRANTY; without even the implied warranty of
22;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23;; GNU General Public License for more details.
24
25;; You should have received a copy of the GNU General Public License
26;; along with GNU Emacs; see the file COPYING. If not, write to the
27;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
28;; Boston, MA 02111-1307, USA.
29
30;;; Commentary:
31
c276ee05 32;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2cb842ae
KH
33;;
34;; About ps-mule
35;; -------------
36;;
37;; This package is used for ps-print to print multi-byte buffer.
38;;
39;; See also ps-print.el.
40;;
41;;
42;; Printing Multi-byte Buffer
43;; --------------------------
44;;
45;; The variable `ps-multibyte-buffer' specifies the ps-print multi-byte buffer
46;; handling.
47;;
48;; Valid values for `ps-multibyte-buffer' are:
49;;
c276ee05
KH
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).
2cb842ae
KH
58;;
59;; `non-latin-printer' This is the value to use when you have a japanese
60;; or korean PostScript printer and want to print
61;; buffer with ASCII, Latin-1, Japanese (JISX0208 and
62;; JISX0201-Kana) and Korean characters. At present,
63;; it was not tested the Korean characters printing.
64;; If you have a korean PostScript printer, please,
65;; test it.
66;;
67;; `bdf-font' This is the value to use when you want to print
68;; buffer with BDF fonts. BDF fonts include both latin
69;; and non-latin fonts. BDF (Bitmap Distribution
70;; Format) is a format used for distributing X's font
71;; source file. BDF fonts are included in
922be019 72;; `intlfonts-1.2' which is a collection of X11 fonts
2cb842ae
KH
73;; for all characters supported by Emacs. In order to
74;; use this value, be sure to have installed
922be019 75;; `intlfonts-1.2' and set the variable
2cb842ae
KH
76;; `bdf-directory-list' appropriately (see ps-bdf.el
77;; for documentation of this variable).
78;;
79;; `bdf-font-except-latin' This is like `bdf-font' except that it is used
80;; PostScript default fonts to print ASCII and Latin-1
81;; characters. This is convenient when you want or
82;; need to use both latin and non-latin characters on
83;; the same buffer. See `ps-font-family',
84;; `ps-header-font-family' and `ps-font-info-database'.
85;;
86;; Any other value is treated as nil.
87;;
88;; The default is nil.
89;;
c276ee05 90;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2cb842ae
KH
91
92;;; Code:
93
922be019
GM
94(eval-and-compile
95 (require 'ps-print)
96
97 ;; to avoid XEmacs compilation gripes
98 (defvar leading-code-private-22 157)
99 (or (fboundp 'charset-bytes)
100 (defun charset-bytes (charset) 1)) ; ascii
101 (or (fboundp 'charset-dimension)
102 (defun charset-dimension (charset) 1)) ; ascii
103 (or (fboundp 'charset-id)
104 (defun charset-id (charset) 0)) ; ascii
105 (or (fboundp 'charset-width)
106 (defun charset-width (charset) 1)) ; ascii
107 (or (fboundp 'find-charset-region)
108 (defun find-charset-region (beg end &optional table)
109 (list 'ascii)))
922be019
GM
110 (or (fboundp 'char-width)
111 (defun char-width (char) 1)) ; ascii
112 (or (fboundp 'chars-in-region)
113 (defun chars-in-region (beg end)
114 (- (max beg end) (min beg end))))
115 (or (fboundp 'forward-point)
116 (defun forward-point (arg)
117 (save-excursion
118 (let ((count (abs arg))
119 (step (if (zerop arg)
120 0
121 (/ arg arg))))
122 (while (and (> count 0)
123 (< (point-min) (point)) (< (point) (point-max)))
124 (forward-char step)
125 (setq count (1- count)))
126 (+ (point) (* count step))))))
127 (or (fboundp 'decompose-composite-char)
128 (defun decompose-composite-char (char &optional type
129 with-composition-rule)
130 nil))
131 (or (fboundp 'encode-coding-string)
132 (defun encode-coding-string (string coding-system &optional nocopy)
133 (if nocopy
134 string
135 (copy-sequence string))))
136 (or (fboundp 'coding-system-p)
137 (defun coding-system-p (obj) nil))
138 (or (fboundp 'ccl-execute-on-string)
139 (defun ccl-execute-on-string (ccl-prog status str
140 &optional contin unibyte-p)
141 str))
142 (or (fboundp 'define-ccl-program)
143 (defmacro define-ccl-program (name ccl-program &optional doc)
dc2f8de4
GM
144 `(defconst ,name nil ,doc)))
145 (or (fboundp 'multibyte-string-p)
146 (defun multibyte-string-p (str)
147 (let ((len (length str))
148 (i 0)
149 multibyte)
150 (while (and (< i len) (not (setq multibyte (> (aref str i) 255))))
151 (setq i (1+ i)))
152 multibyte)))
153 (or (fboundp 'string-make-multibyte)
d0d08a25
EZ
154 (defalias 'string-make-multibyte 'copy-sequence))
155 (or (fboundp 'encode-char)
156 (defun encode-char (ch ccs)
157 ch)))
2cb842ae 158
c276ee05 159
3ea591bd
KH
160;;;###autoload
161(defcustom ps-multibyte-buffer nil
162 "*Specifies the multi-byte buffer handling.
163
164Valid values are:
165
166 nil This is the value to use the default settings which
167 is by default for printing buffer with only ASCII
168 and Latin characters. The default setting can be
169 changed by setting the variable
170 `ps-mule-font-info-database-default' differently.
171 The initial value of this variable is
172 `ps-mule-font-info-database-latin' (see
173 documentation).
174
175 `non-latin-printer' This is the value to use when you have a Japanese
176 or Korean PostScript printer and want to print
177 buffer with ASCII, Latin-1, Japanese (JISX0208 and
178 JISX0201-Kana) and Korean characters. At present,
179 it was not tested the Korean characters printing.
180 If you have a korean PostScript printer, please,
181 test it.
182
183 `bdf-font' This is the value to use when you want to print
184 buffer with BDF fonts. BDF fonts include both latin
185 and non-latin fonts. BDF (Bitmap Distribution
186 Format) is a format used for distributing X's font
187 source file. BDF fonts are included in
922be019 188 `intlfonts-1.2' which is a collection of X11 fonts
3ea591bd
KH
189 for all characters supported by Emacs. In order to
190 use this value, be sure to have installed
922be019 191 `intlfonts-1.2' and set the variable
3ea591bd
KH
192 `bdf-directory-list' appropriately (see ps-bdf.el for
193 documentation of this variable).
194
195 `bdf-font-except-latin' This is like `bdf-font' except that it is used
196 PostScript default fonts to print ASCII and Latin-1
197 characters. This is convenient when you want or
198 need to use both latin and non-latin characters on
199 the same buffer. See `ps-font-family',
200 `ps-header-font-family' and `ps-font-info-database'.
201
202Any other value is treated as nil."
9909b395
GM
203 :type '(choice (const non-latin-printer) (const bdf-font)
204 (const bdf-font-except-latin) (const :tag "nil" nil))
3ea591bd 205 :group 'ps-print-font)
c276ee05 206
2cb842ae 207
2cb842ae 208(eval-and-compile
922be019
GM
209 ;; For Emacs 20.2 and the earlier version.
210 (if (and (boundp 'mule-version)
211 (not (string< (symbol-value 'mule-version) "4.0")))
212 ;; mule package is loaded
2cb842ae
KH
213 (progn
214 (defalias 'ps-mule-next-point '1+)
215 (defalias 'ps-mule-chars-in-string 'length)
216 (defalias 'ps-mule-string-char 'aref)
217 (defsubst ps-mule-next-index (str i) (1+ i)))
922be019 218 ;; mule package isn't loaded or mule version lesser than 4.0
2cb842ae
KH
219 (defun ps-mule-next-point (arg)
220 (save-excursion (goto-char arg) (forward-char 1) (point)))
221 (defun ps-mule-chars-in-string (string)
222 (/ (length string)
223 (charset-bytes (char-charset (string-to-char string)))))
224 (defun ps-mule-string-char (string idx)
225 (string-to-char (substring string idx)))
226 (defun ps-mule-next-index (string i)
d0da93b3 227 (+ i (charset-bytes (char-charset (string-to-char string)))))
922be019 228 )
76875dcb
KH
229 (if (boundp 'mule-version)
230 ;; For Emacs 20.4 and the earlier version.
231 (if (string< (symbol-value 'mule-version) "5.0")
232 ;; mule package is loaded and mule version is lesser than 5.0
233 (progn
234 (defun encode-composition-rule (rule)
235 (if (= (car rule) 4) (setcar rule 10))
236 (if (= (cdr rule) 4) (setcdr rule 10))
237 (+ (* (car rule) 12) (cdr rule)))
238 (defun ps-mule-search-composition (from to)
239 (save-excursion
240 (goto-char from)
241 (search-forward "\200" to t)))
242 (defun ps-mule-get-composition (pos)
243 (let ((ch (char-after pos)))
244 (and ch (eq (char-charset ch) 'composition)
245 (let ((components
246 (decompose-composite-char ch 'vector t)))
247 (list pos (ps-mule-next-point pos) components
248 (integerp (aref components 1)) nil
249 (char-width ch)))))))
250 (defun ps-mule-search-composition (from to)
251 (let (cmp-info)
252 (while (and (< from to)
253 (setq cmp-info (find-composition from to))
254 (not (nth 2 cmp-info)))
255 (setq from (nth 1 cmp-info)))
256 (< from to)))
257 (defun ps-mule-get-composition (pos)
258 (find-composition pos nil nil t)))
259
922be019
GM
260 ;; mule package isn't loaded
261 (or (fboundp 'encode-composition-rule)
262 (defun encode-composition-rule (rule)
263 130))
76875dcb
KH
264 (defun ps-mule-search-composition (&rest ignore)
265 nil)
266 (defun ps-mule-get-composition (&rest ignore)
267 nil)
d0da93b3
KH
268 ))
269
2cb842ae
KH
270(defvar ps-mule-font-info-database
271 nil
272 "Alist of charsets with the corresponding font information.
273Each element has the form:
274
76875dcb 275 (CHARSET (FONT-TYPE FONT-SRC FONT-NAME ENCODING BYTES)) ...)
2cb842ae
KH
276
277Where
278
279CHARSET is a charset (symbol) for this font family,
280
281FONT-TYPE is a font type: normal, bold, italic, or bold-italic.
282
283FONT-SRC is a font source: builtin, ps-bdf, vflib, or nil.
284
922be019 285 If FONT-SRC is builtin, FONT-NAME is a built-in PostScript font name.
2cb842ae 286
5d5bea97
EZ
287 If FONT-SRC is bdf, FONT-NAME is a BDF font file name, or a list of
288 alternative font names. To use this font, the external library `ps-bdf'
289 is required.
2cb842ae
KH
290
291 If FONT-SRC is vflib, FONT-NAME is the name of a font that VFlib knows.
292 To use this font, the external library `vflib' is required.
293
294 If FONT-SRC is nil, a proper ASCII font in the variable
76875dcb
KH
295 `ps-font-info-database' is used, and FONT-NAME is ignored.
296 This is useful for Latin-1 characters.
2cb842ae 297
76875dcb
KH
298ENCODING is a charset to encode a character of CHARSET to a glyph
299code of the specifies font. ENCODING may be a function that does
300this encoding. In this case, the function is called with one
301argument, the character to encode, and it should return an
302encoded code. ENCODING may be nil, in which case CHARSET is used
303to encode a character.
2cb842ae 304
76875dcb
KH
305BYTES specifies how many bytes each character has in the encoded
306byte sequence; it should be 1 or 2. If ENCODING is a charset,
307BYTES may be nil, in chich case the dimension of ENCODING is
308used.
2cb842ae
KH
309
310All multi-byte characters are printed by fonts specified in this database
311regardless of a font family of ASCII characters. The exception is Latin-1
312characters which are printed by the same font as ASCII characters, thus obey
313font family.
314
315See also the variable `ps-font-info-database'.")
316
317(defconst ps-mule-font-info-database-latin
76875dcb
KH
318 '((iso-8859-1
319 (normal nil nil)))
2cb842ae
KH
320 "Sample setting of `ps-mule-font-info-database' to use latin fonts.")
321
c276ee05 322(defcustom ps-mule-font-info-database-default
2a772306 323 ps-mule-font-info-database-latin
8fdd56af
GM
324 "*The default setting to use when `ps-multibyte-buffer' is nil."
325 :type '(symbol :tag "Multi-Byte Buffer Database Font Default")
c276ee05 326 :group 'ps-print-font)
00cbf820 327
2cb842ae
KH
328(defconst ps-mule-font-info-database-ps
329 '((katakana-jisx0201
76875dcb
KH
330 (normal builtin "Ryumin-Light.Katakana")
331 (bold builtin "GothicBBB-Medium.Katakana")
332 (bold-italic builtin "GothicBBB-Medium.Katakana"))
2cb842ae 333 (latin-jisx0201
76875dcb
KH
334 (normal builtin "Ryumin-Light.Hankaku")
335 (bold builtin "GothicBBB-Medium.Hankaku"))
2cb842ae 336 (japanese-jisx0208
76875dcb
KH
337 (normal builtin "Ryumin-Light-H")
338 (bold builtin "GothicBBB-Medium-H"))
2cb842ae 339 (korean-ksc5601
76875dcb
KH
340 (normal builtin "Munhwa-Regular-KSC-EUC-H")
341 (bold builtin "Munhwa-Bold-KSC-EUC-H"))
2cb842ae
KH
342 )
343 "Sample setting of the `ps-mule-font-info-database' to use builtin PS font.
344
345Currently, data for Japanese and Korean PostScript printers are listed.")
346
347(defconst ps-mule-font-info-database-bdf
76875dcb
KH
348 '((iso-8859-1
349 (normal bdf ("lt1-24-etl.bdf" "etl24-latin1.bdf"))
350 (bold bdf ("lt1-16b-etl.bdf" "etl16b-latin1.bdf"))
351 (italic bdf ("lt1-16i-etl.bdf" "etl16i-latin1.bdf"))
352 (bold-italic bdf ("lt1-16bi-etl.bdf" "etl16bi-latin1.bdf")))
353 (iso-8859-2
354 (normal bdf ("lt2-24-etl.bdf" "etl24-latin2.bdf")))
355 (iso-8859-3
356 (normal bdf ("lt3-24-etl.bdf" "etl24-latin3.bdf")))
357 (iso-8859-4
358 (normal bdf ("lt4-24-etl.bdf" "etl24-latin4.bdf")))
2cb842ae 359 (thai-tis620
76875dcb 360 (normal bdf ("thai24.bdf" "thai-24.bdf") iso-8859-11))
2cb842ae 361 (greek-iso8859-7
76875dcb 362 (normal bdf ("grk24-etl.bdf" "etl24-greek.bdf") iso-8859-7))
2cb842ae 363 (hebrew-iso8859-8
76875dcb
KH
364 (normal bdf ("heb24-etl.bdf" "etl24-hebrew.bdf") iso-8859-8))
365 (jisx0201
366 (normal bdf "12x24rk.bdf" jisx0201))
2cb842ae 367 (cyrillic-iso8859-5
76875dcb
KH
368 (normal bdf ("cyr24-etl.bdf" "etl24-cyrillic.bdf") iso-8859-5))
369 (iso-8859-9
370 (normal bdf ("lt5-24-etl.bdf" "etl24-latin5.bdf") iso-8859-9))
2cb842ae 371 (chinese-gb2312
76875dcb 372 (normal bdf "gb24st.bdf"))
2cb842ae 373 (japanese-jisx0208
76875dcb 374 (normal bdf "jiskan24.bdf"))
2cb842ae 375 (korean-ksc5601
76875dcb 376 (normal bdf "hanglm24.bdf"))
2cb842ae 377 (japanese-jisx0212
76875dcb 378 (normal bdf ("jksp40.bdf" "jisksp40.bdf")))
2cb842ae 379 (chinese-cns11643-1
76875dcb 380 (normal bdf ("cns1-40.bdf" "cns-1-40.bdf")))
2cb842ae 381 (chinese-cns11643-2
76875dcb
KH
382 (normal bdf ("cns2-40.bdf" "cns-2-40.bdf")))
383 (big5
384 (normal bdf "taipei24.bdf"))
2cb842ae 385 (chinese-sisheng
76875dcb 386 (normal bdf ("sish24-etl.bdf" "etl24-sisheng.bdf")))
2cb842ae 387 (ipa
76875dcb
KH
388 (normal bdf ("ipa24-etl.bdf" "etl24-ipa.bdf") ipa))
389 (viscii
390 (normal bdf ("visc24-etl.bdf" "etl24-viscii.bdf")))
2cb842ae 391 (arabic-digit
76875dcb 392 (normal bdf ("arab24-0-etl.bdf" "etl24-arabic0.bdf")))
2cb842ae 393 (arabic-1-column
76875dcb 394 (normal bdf ("arab24-1-etl.bdf" "etl24-arabic1.bdf")))
2cb842ae 395 (lao
76875dcb 396 (normal bdf ("lao24-mule.bdf" "mule-lao-24.bdf") ps-mule-encode-lao 1))
2cb842ae 397 (arabic-2-column
76875dcb
KH
398 (normal bdf ("arab24-2-etl.bdf" "etl24-arabic2.bdf")))
399 (devanagari-cdac
400 (normal bdf "dvsr0ntt-32.bdf"))
401 (malayalam-cdac
402 (normal bdf "mlkr0ntt-32.bdf"))
403 (tamil-cdac
404 (normal bdf "tmvl0ntt-32.bdf"))
2cb842ae 405 (indian-is13194
76875dcb 406 (normal bdf ("isci24-mule.bdf" "mule-iscii-24.bdf")))
2cb842ae 407 (indian-1-column
76875dcb 408 (normal bdf ("ind1c24-mule.bdf" "mule-indian-1col-24.bdf")))
2cb842ae 409 (ethiopic
76875dcb 410 (normal bdf ("ethio24f-uni.bdf" "ethiomx24f-uni.bdf") unicode-bmp))
2cb842ae 411 (chinese-cns11643-3
76875dcb 412 (normal bdf ("cns3-40.bdf" "cns-3-40.bdf")))
2cb842ae 413 (chinese-cns11643-4
76875dcb 414 (normal bdf ("cns4-40.bdf" "cns-4-40.bdf")))
2cb842ae 415 (chinese-cns11643-5
76875dcb 416 (normal bdf ("cns5-40.bdf" "cns-5-40.bdf")))
2cb842ae 417 (chinese-cns11643-6
76875dcb 418 (normal bdf ("cns6-40.bdf" "cns-6-40.bdf")))
2cb842ae 419 (chinese-cns11643-7
76875dcb 420 (normal bdf ("cns7-40.bdf" "cns-7-40.bdf")))
2cb842ae 421 (indian-2-column
76875dcb 422 (normal bdf ("ind24-mule.bdf" "mule-indian-24.bdf")))
2cb842ae 423 (tibetan
76875dcb
KH
424 (normal bdf ("tib24p-mule.bdf" "tib24-mule.bdf" "mule-tibmdx-24.bdf")))
425 (unicode-bmp
426 (normal bdf "etl24-unicode.bdf"))
427 )
2cb842ae
KH
428 "Sample setting of the `ps-mule-font-info-database' to use BDF fonts.
429BDF (Bitmap Distribution Format) is a format used for distributing X's font
430source file.
431
d0d08a25
EZ
432Current default value list for BDF fonts is included in `intlfonts-1.2'
433which is a collection of X11 fonts for all characters supported by Emacs.
2cb842ae 434
d0d08a25
EZ
435Using this list as default value to `ps-mule-font-info-database', all
436characters including ASCII and Latin-1 are printed by BDF fonts.
2cb842ae
KH
437
438See also `ps-mule-font-info-database-ps-bdf'.")
439
440(defconst ps-mule-font-info-database-ps-bdf
441 (cons (car ps-mule-font-info-database-latin)
442 (cdr (cdr ps-mule-font-info-database-bdf)))
443 "Sample setting of the `ps-mule-font-info-database' to use BDF fonts.
444
d0d08a25
EZ
445Current default value list for BDF fonts is included in `intlfonts-1.2'
446which is a collection of X11 fonts for all characters supported by Emacs.
2cb842ae 447
d0d08a25
EZ
448Using this list as default value to `ps-mule-font-info-database', all
449characters except ASCII and Latin-1 characters are printed with BDF fonts.
450ASCII and Latin-1 characters are printed with PostScript font specified
451by `ps-font-family' and `ps-header-font-family'.
2cb842ae
KH
452
453See also `ps-mule-font-info-database-bdf'.")
454
76875dcb
KH
455(defvar ps-mule-font-spec-list nil
456 "Array of FONT-SPEC lists for each font type.
2cb842ae 457
76875dcb
KH
458Elements are for `normal' font, `bold' font, `italic' font, and
459`bold-italic' font in this order.
460
461Each element is a list of FONT-SPEC which has this form:
2cb842ae 462
76875dcb
KH
463 (ID CHARSET (FONT-SRC FONT-NAME ENCODING) EXTRA-DATA)
464
465Where
466
467ID is a number for this FONT-SPEC and is unique in the list.
468
469CHARSET, FONT-SRC, FONT-NAME, ENCODING are the same as those in
470`ps-mule-font-info-database' (which see).
471
472EXTRA-DATA is a data attached by external libraries.
473
474Each list is ordered by the current charset priorities.
475
476This variable is setup by `ps-mule-begin-job' from
477`ps-mule-font-info-database'.")
478
479;; Functions to access each element of FONT-SPEC.
480(defsubst ps-mule-font-spec-id (font-spec) (aref font-spec 0))
481(defsubst ps-mule-font-spec-charset (font-spec) (aref font-spec 1))
482(defsubst ps-mule-font-spec-font-id (font-spec) (aref font-spec 2))
483(defsubst ps-mule-font-spec-src (font-spec) (aref font-spec 3))
484(defsubst ps-mule-font-spec-name (font-spec) (aref font-spec 4))
485(defsubst ps-mule-font-spec-set-name (font-spec name)
486 (aset font-spec 4 name))
487(defsubst ps-mule-font-spec-encoding (font-spec) (aref font-spec 5))
488(defsubst ps-mule-font-spec-bytes (font-spec) (aref font-spec 6))
489(defsubst ps-mule-font-spec-extra (font-spec) (aref font-spec 7))
490(defsubst ps-mule-font-spec-set-extra (font-spec extra)
491 (aset font-spec 7 extra))
492
493;; Functions to encode character into glyph code.
494(defun ps-mule-encode-lao (char)
495 (- char #x0DE0))
496
497(defun ps-mule-encode-char (char font-spec)
498 (let ((encoding (ps-mule-font-spec-encoding font-spec)))
499 (cond ((charsetp encoding)
500 (encode-char char encoding))
501 ((fboundp encoding)
502 (funcall encoding char))
503 (t
504 char))))
505
506;; Array of FONT-SPEC-TABLEs; Nth element is for FONT-TYPE N.
507;;
508;; FONT-TYPE is 0, 1, 2, or 3 representing normal, bold, italic, and
509;; bold-italic respectively.
510;;
511;; FONT-SPEC-TABLE is a char-table of FONT-SPECs. It records which
512;; character is printed by which FONT-SPEC. It has one extra slot
513;; whose value is an alist of the form:
514;; (CHARSET . FONT-SPEC)
515;; FONT-SPEC is a vecotr of the form:
516;; (ID FONT-SRC FONT-NAME ENCODING EXTRA)
517(defvar ps-mule-font-spec-tables nil)
518
519;; Array of FONT-TYPEs; Nth element FONT-NUMBER N.
520;;
521;; FONT-NUMBER is 0, 1, 2, 3, 4, 5, 6 representing fonts f0, f1, f2,
522;; f3, h0, h1, and H0.
523(defconst ps-mule-font-number-to-type [ 0 1 2 3 1 0 0 ])
524
525(defsubst ps-mule-get-font-spec (char font-spec-table font-spec)
526 "Return a font spec for printing CHAR with FONT-SPEC-TABLE.
527
528FONT-SPEC, if non-nil, is a font spec to try at first.
529
530See the documentation of `ps-mule-font-spec-tables' for the
531format of font spec."
532
533 (or (aref font-spec-table char)
534 (aset font-spec-table char
535 (or (and (< char 256)
536 (cdr (car (char-table-extra-slot font-spec-table 0))))
537 (and font-spec
538 (encode-char char (ps-mule-font-spec-charset font-spec))
539 font-spec)
540 (catch 'tag
541 (dolist (elt (char-table-extra-slot font-spec-table 0))
542 (and (encode-char char (car elt))
543 (throw 'tag (cdr elt)))))
544 ;; Record that no font-spec exist for CHAR.
545 t))))
2cb842ae
KH
546
547(defconst ps-mule-external-libraries
76875dcb
KH
548 '((builtin nil nil nil nil nil nil)
549 (bdf ps-bdf nil bdf-generate-prologue
550 bdf-check-font bdf-generate-font bdf-generate-glyph)
551 (pcf nil nil pcf-generate-prologue
552 pcf-check-font pcf-generate-font pcf-generate-glyph)
553 (vflib nil nil vflib-generate-prologue
554 vflib-check-font vflib-generate-font vflib-generate-glyphs))
2cb842ae
KH
555 "Alist of information of external libraries to support PostScript printing.
556Each element has the form:
557
76875dcb
KH
558 (FONT-SRC FEATURE INITIALIZED-P
559 PROLOGUE-FUNC CHECK-FUNC FONT-FUNC GLYPH-FUNC)
2cb842ae
KH
560
561FONT-SRC is the font source: builtin, bdf, pcf, or vflib.
562
563FEATURE is the feature that provide a facility to handle FONT-SRC. Except for
564`builtin' FONT-SRC, this feature is automatically `require'd before handling
565FONT-SRC. Currently, we only have the feature `ps-bdf'.
566
567INITIALIZED-P indicates if this library is initialized or not.
568
76875dcb
KH
569PROLOGUE-FUNC is a function to generate PostScript code which
570define several PostScript procedures that will be called by
571FONT-FUNC and GLYPHS-FUNC. It is called with no argument, and
572should return a list of strings.
2cb842ae 573
76875dcb
KH
574CHECK-FUNC is a function to check if a font is available or not.
575It is called with one argument FONT-SPEC, and should return
576non-nil iff the font specified in FONT-SPEC is available.
2cb842ae 577
76875dcb
KH
578FONT-FUNC is a function to generate PostScript code which define
579a new font. It is called with one argument FONT-SPEC, and should
580return a list of strings.
581
582GLYPH-FUNC is a function to generate PostScript code which define a glyph of
583characters. It is called with two arguments FONT-SPEC and CODE,
2cb842ae
KH
584and should return a list of strings.")
585
76875dcb
KH
586(defsubst ps-mule-exlib-feature (exlib) (nth 1 exlib))
587(defsubst ps-mule-exlib-initialized-p (exlib) (nth 2 exlib))
588(defsubst ps-mule-exlib-set-initialized-p (exlib val)
589 (setcar (nthcdr 2 exlib) val))
590(defsubst ps-mule-exlib-prologue (exlib) (nth 3 exlib))
591(defsubst ps-mule-exlib-check (exlib) (nth 4 exlib))
592(defsubst ps-mule-exlib-font (exlib) (nth 5 exlib))
593(defsubst ps-mule-exlib-glyph (exlib) (nth 6 exlib))
594
595(defun ps-mule-init-external-library (exlib)
596 "Initialize external library specified by EXLIB for PostScript printing.
597See the documentation of `ps-mule-external-libraries' for EXLIB's meaning."
598 (or (ps-mule-exlib-initialized-p exlib)
599 (let ((prologue-func (ps-mule-exlib-prologue exlib)))
600 (if prologue-func
601 (let ((feature (ps-mule-exlib-feature exlib)))
602 (if feature
603 (require feature))
604 (ps-output-prologue (funcall prologue-func))))
605 (ps-mule-exlib-set-initialized-p exlib t))))
606
607(defvar ps-mule-output-list nil)
608
609(defun ps-mule-check-font (font-spec)
610 "Check if a font specified in FONT-SPEC is available or not."
611 (let ((font-src (ps-mule-font-spec-src font-spec)))
2cb842ae 612 (or (not font-src)
76875dcb
KH
613 (let ((exlib (assq font-src ps-mule-external-libraries)))
614 (ps-mule-init-external-library exlib)
615 (or (not (ps-mule-exlib-check exlib))
616 (funcall (ps-mule-exlib-check exlib) font-spec))))))
2cb842ae 617
76875dcb
KH
618(defun ps-mule-prepare-font (font-spec)
619 "Generate PostScript codes defining a new font of FONT-TYPE for CHARSET."
620 (let* ((font-src (ps-mule-font-spec-src font-spec))
621 (exlib (assq font-src ps-mule-external-libraries))
622 (id (ps-mule-font-spec-id font-spec))
623 (ftag (format "%02X" id))
624 (font-func (ps-mule-exlib-font exlib))
625 output-list)
626 (if font-func
627 (setq output-list (funcall font-func font-spec))
628 (setq output-list
629 (format "/F%s /%s findfont def\n"
630 ftag (or (ps-mule-font-spec-name font-spec) "Courier")))
631 (ps-mule-font-spec-set-extra font-spec t))
632 (and output-list
633 (nconc ps-mule-output-list (list output-list)))))
634
635(defun ps-mule-prepare-glyph (char font-spec)
636 "Generate PostScript codes to print CHAR by FONT-SPEC.
637
638If CHAR is a cons (FROM TO), generate codes for characters
639specified by the character code range FROM and TO.
640
641The generated code is inserted on prologue part."
642 (if (vectorp font-spec)
643 (progn
644 (or (ps-mule-font-spec-extra font-spec)
645 (ps-mule-prepare-font font-spec))
646 (let ((glyph-func (ps-mule-exlib-glyph
647 (assq (ps-mule-font-spec-src font-spec)
648 ps-mule-external-libraries))))
649 (if glyph-func
650 (let (from to output-list)
651 (if (consp char)
652 (setq from (car char) to (cdr char))
653 (setq from char to char))
654 (while (<= from to)
655 (setq output-list
656 (funcall glyph-func font-spec from))
657 (and output-list
658 (ps-output-prologue output-list))
659 (setq from (1+ from)))))))))
2cb842ae
KH
660
661;; This is a PostScript code inserted in the header of generated PostScript.
662(defconst ps-mule-prologue
663 "%%%% Start of Mule Section
664
76875dcb
KH
665%% Redefine fonts for multiple charsets.
666/ReDefFont { % fontname encoding fdepvector size | -
667 20 dict begin
668 3 index findfont {
669 1 index /FID ne 2 index /UniqueID ne and {def} {pop pop} ifelse
670 } forall
671 /FontType 0 def
672 /FMapType 3 def
673 /EscChar 0 def
674 % FontMatrix ::= [ size 0 0 size 0 0 ]
675 /FontMatrix exch [ exch dup 0 exch 0 exch 0 0 ] def
676 /FDepVector exch def
677 /Encoding exch def
678 currentdict
679 end % fontname dic
680 definefont pop
bc4c1aae 681} def
2cb842ae
KH
682"
683 "PostScript code for printing multi-byte characters.")
684
685(defvar ps-mule-prologue-generated nil)
686
76875dcb
KH
687;; EscChar used in generated composite fonts.
688(defconst ps-mule-esc-char 0)
689
2cb842ae
KH
690(defun ps-mule-prologue-generated ()
691 (unless ps-mule-prologue-generated
692 (ps-output-prologue ps-mule-prologue)
76875dcb 693 (ps-output-prologue (format "/EscChar %d def\n" ps-mule-esc-char))
2cb842ae
KH
694 (setq ps-mule-prologue-generated t)))
695
76875dcb
KH
696(defsubst ps-mule-eight-bit-char (byte)
697 (if (< byte 128)
698 byte
699 (decode-char 'eight-bit byte)))
2cb842ae 700
76875dcb
KH
701(defun ps-mule-encode-region (from to font-spec-table)
702 "Generate PostScript code for plotting characters in the region FROM and TO.
2cb842ae 703
76875dcb
KH
704FONT-NUM is 0, 1, 2, 3, 4, 5, or 6, each represents font tags
705f0, f1, f2, f3, h0, h1, and H0 respectively."
706 (let* ((font-spec nil)
707 (font-id 0)
708 (string-list nil)
709 ;; At most 4-byte (EscChar FONT-ID CODE1 CODE2) per character.
710 (str (make-string (* (- to from) 4) 0))
711 (i 0))
712 (goto-char from)
713 (while (< (point) to)
714 (let* ((char (following-char))
715 (this-spec (ps-mule-get-font-spec char font-spec-table font-spec))
716 this-id)
717 (if (vectorp this-spec)
718 (setq this-id (ps-mule-font-spec-font-id this-spec))
719 ;; Can't print CHAR. Replace it with '?'.
720 (setq char ??
721 this-spec (ps-mule-get-font-spec char font-spec-table nil)
722 this-id (ps-mule-font-spec-font-id this-spec)))
723 (or (= font-id this-id)
724 (progn
725 (if font-spec
726 (setq string-list (cons (substring str 0 i) string-list)
727 i 0))
728 (setq font-id this-id)
729 (or (= font-id 0)
730 (progn
731 (aset str i ps-mule-esc-char)
732 (setq i (1+ i))
733 (aset str i font-id)
734 (setq i (1+ i))))))
735 (setq font-spec this-spec)
736 (if (< char 128)
737 (aset str i char)
738 (let* ((code (ps-mule-encode-char char font-spec)))
739 (if (= (ps-mule-font-spec-bytes font-spec) 1)
740 (aset str i (ps-mule-eight-bit-char code))
741 (aset str i (ps-mule-eight-bit-char (/ code 256)))
742 (setq i (1+ i))
743 (aset str i (ps-mule-eight-bit-char (% code 256))))))
744 (setq i (1+ i))
745 (forward-char 1)))
746 (nreverse (cons (substring str 0 i) string-list))))
747
748(defun ps-mule-plot-composition (composition font-spec-table)
749 "Generate PostScript code for plotting COMPOSITION with FONT-NUM."
750 (ps-output "[")
751 (let ((components (copy-sequence (nth 2 composition)))
752 (font-spec nil))
753 (dotimes (i (length components))
754 (let ((elt (aref components i))
755 this-spec)
756 (if (consp elt)
757 ;; ELT is a composition rule.
758 (ps-output (format " %d" (encode-composition-rule elt)))
759 ;; ELT is a glyph character.
760 (setq this-spec
761 (ps-mule-get-font-spec elt font-spec-table font-spec))
762 (or (vectorp this-spec)
763 ;; Can't print CHAR. Replace it with '?'.
764 (setq elt ??
765 this-spec
766 (ps-mule-get-font-spec elt font-spec-table font-spec)))
767 (setq font-spec this-spec)
768 (let* ((bytes (ps-mule-font-spec-bytes font-spec))
769 (code (ps-mule-encode-char elt font-spec))
770 (font-id (ps-mule-font-spec-font-id font-spec))
771 (str (make-string (if (= font-id 0) 1 (+ 2 bytes)) 0)))
772 (if (= font-id 0)
773 (aset str 0 (ps-mule-eight-bit-char code))
774 (aset str 0 ps-mule-esc-char)
775 (aset str 1 (ps-mule-eight-bit-char font-id))
776 (if (= bytes 1)
777 (aset str 2 (ps-mule-eight-bit-char code))
778 (aset str 2 (ps-mule-eight-bit-char (/ code 256)))
779 (aset str 3 (ps-mule-eight-bit-char (% code 256)))))
780 (ps-output "[")
781 (ps-output-string str)
782 (ps-output (if (eq (ps-mule-font-spec-src font-spec) 'bdf)
783 (format "/C%02X-%X" (ps-mule-font-spec-id font-spec)
784 elt)
785 "false"))
786 (ps-output "]"))))))
787 (ps-output " ] " (if (nth 3 composition) "RLC" "RBC") "\n"))
2cb842ae 788
2cb842ae 789(defun ps-mule-plot-string (from to &optional bg-color)
922be019 790 "Generate PostScript code for plotting characters in the region FROM and TO.
2cb842ae 791
76875dcb 792Optional argument BG-COLOR is ignored.
d0da93b3
KH
793
794Returns the value:
795
796 (ENDPOS . RUN-WIDTH)
797
798Where ENDPOS is the end position of the sequence and RUN-WIDTH is the width of
799the sequence."
76875dcb
KH
800 (let* ((average-width (ps-avg-char-width 'ps-font-for-text))
801 (point (point))
802 (composition (find-composition from to nil t))
803 (stop (if composition (car composition) to))
804 (ascii-or-latin-1 "[\000-\377]+")
805 (run-width 0)
806 (endpos nil)
807 (font-spec-table (aref ps-mule-font-spec-tables
808 (aref ps-mule-font-number-to-type
809 ps-current-font)))
810 width)
811 (goto-char from)
812 (while (not endpos)
813 (cond ((= (point) stop)
814 (if (= stop to)
815 (setq endpos stop)
816 (if (< from stop)
817 (dolist (l (ps-mule-encode-region from (point)
818 font-spec-table))
819 (ps-output-string l)
820 (ps-output " S\n")))
821 (setq width (* (nth 5 composition) average-width))
822 (if (< ps-width-remaining (+ run-width width))
823 (setq endpos stop)
824 (ps-mule-plot-composition composition font-spec-table)
825 (setq run-width (+ run-width width)
826 from (nth 1 composition))
827 (goto-char from)
828 (setq composition (find-composition (point) to nil t))
829 (setq stop (if composition (car composition) to)))))
830
831 ((looking-at ascii-or-latin-1)
832 (let ((nchars (- (min (match-end 0) stop) (point))))
833 (setq width (* average-width nchars))
834 (if (< ps-width-remaining (+ run-width width))
835 (setq nchars (truncate (- ps-width-remaining run-width)
836 average-width)
837 run-width (+ run-width (* nchars average-width))
838 endpos (+ (point) nchars))
839 (setq run-width (+ run-width width))
840 (forward-char nchars))))
841
842 (t
843 (while (and (< (point) stop) (not endpos))
844 (setq width (char-width (following-char)))
845 (if (< ps-width-remaining (+ run-width width))
846 (setq endpos (point))
847 (setq run-width (+ run-width width))
848 (forward-char 1))))))
849
850 (if (< from endpos)
851 (dolist (l (ps-mule-encode-region from endpos font-spec-table))
852 (ps-output-string l)
853 (ps-output " S\n")))
854 (goto-char point)
855 (cons endpos run-width)))
856
857;; Character composition support
2cb842ae 858
d0da93b3 859(defvar ps-mule-composition-prologue-generated nil)
2cb842ae 860
d0da93b3 861(defconst ps-mule-composition-prologue
76875dcb 862 "%%%% Procedures for character composition.
d0da93b3 863/RelativeCompositionSkip 0.4 def
2cb842ae
KH
864
865%% Get a bounding box (relative to currentpoint) of STR.
76875dcb
KH
866/GetPathBox { % [ str cname ] | -
867 dup 1 get dup false ne {
868 BitmapDict exch get /bmp exch def
869 %% bmp ::= [ DWIDTH WIDTH HEIGHT XOFF YOFF BITMAP RELATIVE-COMPOSE]
870 /LLY bmp 4 get def
871 /URY LLY bmp 2 get add def
872 /RelativeCompose bmp 6 get dup false ne {
873 dup LLY le { pop 1 } { URY ge { -1 } { 0 } ifelse } ifelse
874 } {
875 pop 0
876 } ifelse def
877 dup 0 get stringwidth pop dup /WIDTH exch def bmp 0 get div
878 dup LLY mul /LLY exch def
879 URY mul /URY exch def
2cb842ae 880 } {
76875dcb
KH
881 pop
882 dup 0 get stringwidth pop /WIDTH exch def
883 gsave 0 0 moveto
884 dup 0 get false charpath flattenpath pathbbox
885 /URY exch def pop /LLY exch def pop
886 grestore
887 /RelativeCompose 0 def
2cb842ae 888 } ifelse
2cb842ae
KH
889} bind def
890
76875dcb
KH
891%% Apply effects except for shadow and outline to the rectangle
892%% specified by TOP BOTTOM LEFT RIGHT.
893/SpecialEffect { % -- | --
d0da93b3
KH
894 currentpoint dup TOP add /yy exch def BOTTOM add /YY exch def
895 dup LEFT add /xx exch def RIGHT add /XX exch def
896 %% Adjust positions for future shadowing.
897 Effect 8 and 0 ne {
898 /yy yy Yshadow add def
899 /XX XX Xshadow add def
900 } if
901 Effect 1 and 0 ne { UnderlinePosition Hline } if % underline
902 Effect 2 and 0 ne { StrikeoutPosition Hline } if % strikeout
903 Effect 4 and 0 ne { OverlinePosition Hline } if % overline
904 bg { % background
905 true
906 Effect 16 and 0 ne {SpaceBackground doBox} { xx yy XX YY doRect} ifelse
907 } if
908 Effect 16 and 0 ne { false 0 doBox } if % box
909} def
2cb842ae 910
76875dcb
KH
911%% Draw COMPONENTS which has the form [ [str0 xoff0 yoff0] ... ] with
912%% effects shadow and outline.
913/ShowComponents { % components | -
914 gsave
915 { gsave aload pop rmoveto
916 Effect 8 and 0 ne { dup doShadow } if
917 Effect 32 and 0 ne { true doOutline } { show } ifelse
918 grestore
d0da93b3 919 } forall
76875dcb 920 grestore
d0da93b3
KH
921 RIGHT 0 rmoveto
922} def
923
924%% Show relative composition.
76875dcb 925/RLC { % [[str0 cname0] [str1 cname1] ... [strN cnameN]] | -
d0da93b3 926 /components exch def
76875dcb
KH
927 [ % push [str xoff yoff] one by one
928 [ components 0 get GetPathBox aload pop pop 0 0 ]
929 %% Bounding box of overall glyphs.
930 /LEFT 0 def
931 /RIGHT WIDTH def
932 /TOP URY def
933 /BOTTOM LLY def
934
935 1 1 components length 1 sub {
936 components exch get
937 [ exch
938 GetPathBox
939 aload pop pop % str
940 0 % xoff
941 RelativeCompose 1 eq { % compose on TOP
942 TOP LLY sub RelativeCompositionSkip add % yoff
943 /TOP TOP URY LLY sub add RelativeCompositionSkip add def
944 } { RelativeCompose -1 eq { % compose under BOTTOM
945 BOTTOM URY sub RelativeCompositionSkip sub % yoff
946 /BOTTOM BOTTOM URY LLY sub sub
947 RelativeCompositionSkip sub def
d0da93b3 948 } {
76875dcb
KH
949 0 % yoff
950 URY TOP gt { /TOP URY def } if
951 LLY BOTTOM lt { /BOTTOM LLY def } if
952 } ifelse } ifelse
953 ]
954 } for
955 ]
956 SpecialEffect % Reflect special effects.
957 ShowComponents % Draw components.
d0da93b3
KH
958} def
959
960%% Show rule-base composition.
76875dcb 961/RBC { % [[str0 cname0] rule1 [str1 cname0] rule2 ... [strN cnameN]] | -
d0da93b3 962 /components exch def
76875dcb
KH
963 [ % push [str xoff yoff] one by one
964 [ components 0 get GetPathBox aload pop pop 0 0 ]
965 %% Bounding box of overall glyphs.
966 /LEFT 0 def
967 /RIGHT WIDTH def
968 /TOP URY def
969 /BOTTOM LLY def
970 1 1 components length 1 sub {
971 components exch get /elt exch def
972 elt type /integertype eq { % rule
973 %% Do the same RULE decoding as the macro
922be019 974 %% COMPOSITION_DECODE_RULE in emacs/src/composite.h.
d0da93b3
KH
975 elt 12 idiv dup 3 mod /grefx exch def 3 idiv /grefy exch def
976 elt 12 mod dup 3 mod /nrefx exch def 3 idiv /nrefy exch def
d0da93b3 977 } { % other strings
76875dcb 978 [
d0da93b3 979 elt GetPathBox
76875dcb 980 aload pop pop
d0da93b3 981 /height URY LLY sub def
76875dcb
KH
982 /left LEFT [ 0 RIGHT LEFT sub dup 2 div exch ] grefx get add
983 [ 0 WIDTH 2 div WIDTH ] nrefx get sub def
d0da93b3
KH
984 /bottom [ TOP 0 BOTTOM TOP BOTTOM add 2 div ] grefy get
985 [ height LLY neg 0 height 2 div ] nrefy get sub def
986 %% Update bounding box
987 left LEFT lt { /LEFT left def } if
76875dcb 988 left WIDTH add RIGHT gt { /RIGHT left WIDTH add def } if
d0da93b3
KH
989 bottom BOTTOM lt { /BOTTOM bottom def } if
990 bottom height add TOP gt { /TOP bottom height add def } if
76875dcb
KH
991 left bottom LLY sub ]
992 } ifelse
993 } for
994 ]
64ed6f71 995
76875dcb
KH
996 LEFT 0 lt { % Adjust xoff to the right.
997 dup { dup 1 get LEFT sub 1 exch put } forall
998 /RIGHT RIGHT LEFT sub def
999 } if
d0da93b3 1000
76875dcb
KH
1001 SpecialEffect % Reflect special effects.
1002 ShowComponents % Draw components.
d0da93b3 1003} def
76875dcb 1004%%%% End of procedures for character composition
2cb842ae 1005"
922be019 1006 "PostScript code for printing character composition.")
2cb842ae 1007
76875dcb
KH
1008(defun ps-mule-composition-prologue-generated ()
1009 (unless ps-mule-composition-prologue-generated
1010 (ps-mule-prologue-generated)
1011 (ps-output-prologue ps-mule-composition-prologue)
1012 (setq ps-mule-composition-prologue-generated t)))
2cb842ae
KH
1013
1014;; Bitmap font support
1015
1016(defvar ps-mule-bitmap-prologue-generated nil)
1017
1018(defconst ps-mule-bitmap-prologue
76875dcb 1019 "%%%% Procedures for bitmap fonts.
2cb842ae 1020
76875dcb
KH
1021%% Create a base bitmap font.
1022/NBF { % fontname fontsize relative-compose baseline-offset enc | --
1023 11 dict begin
1024 /FontType 3 def
1025 /FontMatrix matrix def
1026 /FontBBox [ 0 0 0 0 ] def
1027 /Encoding exch def
2cb842ae
KH
1028 /BaselineOffset exch def
1029 /RelativeCompose exch def
1030 /FontSize exch def
76875dcb
KH
1031 /FontMatrix [ 1 FontSize div 0 0 1 FontSize div 0 0 ] def
1032 /BuildGlyph { % fontdict charname | -
1033 BitmapDict exch get /bmp exch def pop
1034 %% bmp ::= [ DWIDTH WIDTH HEIGHT XOFF YOFF BITMAP RELATIVE-COMPOSE ]
1035 /llx bmp 3 get def
1036 /lly bmp 4 get def
1037 /urx llx bmp 1 get add def
1038 /ury lly bmp 2 get add def
1039 bmp 0 get 0 llx lly urx ury setcachedevice
1040 bmp 5 get length 0 gt {
1041 llx ury translate
1042 bmp 1 get bmp 2 get
1043 true [ 1 0 0 -1 0 0 ] { bmp 5 get } imagemask
1044 } if
1045 } bind def
1046 /BuildChar { % fontdict byte | -
1047 1 index /Encoding get exch get
1048 1 index /BuildGlyph get exec
1049 } bind def
1050 dup currentdict end
1051 definefont def
2cb842ae
KH
1052} bind def
1053
76875dcb
KH
1054%% Create a parent font of 8/8 mapping.
1055/NPF { % fontname encoding fdepvector | -
1056 8 dict begin
1057 /FontType 0 def
1058 /FMapType 2 def
1059 /FontMatrix matrix def
1060 /FDepVector exch def
1061 /Encoding exch def
1062 dup currentdict
1063 end
1064 definefont def
2cb842ae 1065} bind def
2cb842ae 1066
76875dcb 1067%%%% End of procedures for bitmap fonts.
2cb842ae
KH
1068")
1069
1070;; External library support.
1071
76875dcb
KH
1072(defvar ps-mule-bitmap-dict-list nil)
1073(defvar ps-mule-bitmap-parent-table nil)
1074
2cb842ae
KH
1075;; The following three functions are to be called from external
1076;; libraries which support bitmap fonts (e.g. `bdf') to get
1077;; appropriate PostScript code.
1078
1079(defun ps-mule-generate-bitmap-prologue ()
1080 (unless ps-mule-bitmap-prologue-generated
76875dcb
KH
1081 (setq ps-mule-bitmap-prologue-generated t
1082 ps-mule-bitmap-dict-list nil
1083 ps-mule-bitmap-font-record (make-vector 1024 nil))
2cb842ae
KH
1084 (list ps-mule-bitmap-prologue)))
1085
76875dcb
KH
1086(defun ps-mule-generate-bitmap-font (font-spec size relative-compose
1087 baselie-offset bbx)
1088 (let* ((id (ps-mule-font-spec-id font-spec))
1089 (bytes (ps-mule-font-spec-bytes font-spec))
1090 output-list)
1091 (if (= bytes 1)
1092 (setq output-list
1093 (list (format "/E%02X [ 0 1 255 {pop /.notdef} for ] def\n" id)
1094 (format "%%%% %s\n" (ps-mule-font-spec-name font-spec))
1095 (format "/F%02X %f %S %d E%02X NBF\n" id size
1096 relative-compose baselie-offset id)))
1097 (setq output-list
1098 (list (list (format "/E%02X [ 0 1 255 { pop 0 } for ] def\n" id))
1099 (list (format "/V%02X [" id))
1100 " ] def\n"
1101 (format "%%%% %s\n" (ps-mule-font-spec-name font-spec))
1102 (format "/F%02X E%02X V%02X NPF\n" id id id))))
1103 (aset ps-mule-bitmap-font-record id
1104 (vector (= bytes 1) output-list
1105 size relative-compose baselie-offset bbx))
1106 (if ps-mule-bitmap-dict-list
1107 output-list
1108 (setq ps-mule-bitmap-dict-list (list "/BitmapDict <<\n" ">> def\n"))
1109 (cons ps-mule-bitmap-dict-list output-list))))
1110
1111(defun ps-mule-generate-bitmap-glyph (font-spec char code bitmap)
1112 (let* ((id (ps-mule-font-spec-id font-spec))
1113 ;; FONT-RECORD ::= ([(SUBFONT-OUTPUT-LIST ...) | t]
1114 ;; BASEFONT-OUTPUT-LIST SIZE REL-COMP B-OFFSET BBX)
1115 (font-record (aref ps-mule-bitmap-font-record id))
1116 enc-name
1117 output-list)
1118 (if (listp (aref font-record 0))
1119 ;; This is a 2-dimensional font. Create a subfont for this
1120 ;; glyph if not yet created.
1121 (let* ((high (/ code 256))
1122 (id2 (+ (* id 256) high)))
1123 (setq output-list (cdr (assq high (aref font-record 0)))
1124 code (% code 256))
1125 (or output-list
1126 ;; We must create a subfont.
1127 (let ((enc-list (car (aref font-record 1)))
1128 (fdep-list (nth 1 (aref font-record 1))))
1129 (setq output-list
1130 (list
1131 (format "/E%04X [ 0 1 255 {pop /.notdef} for ] def\n"
1132 id2)
1133 (format "/F%04X %f %S %d E%04X NBF\n"
1134 id2 (aref font-record 2) (aref font-record 3)
1135 (aref font-record 4) id2)
1136 (format "E%02X %d %d put\n"
1137 id high (1- (length fdep-list)))))
1138 (nconc enc-list (list output-list))
1139 (nconc fdep-list (list (format " F%04X" id2)))
1140 (aset font-record 0
1141 (cons (cons high output-list) (aref font-record 0)))))
1142 (setq enc-name (format "%04X" id2)))
1143 (setq output-list (aref font-record 1)
1144 enc-name (format "%02X" id)))
1145 (setcdr ps-mule-bitmap-dict-list
1146 (cons (format "/C%02X-%X %s\n" id char bitmap)
1147 (cdr ps-mule-bitmap-dict-list)))
1148 (setcdr output-list
1149 (cons (format "E%s %d /C%02X-%X put\n" enc-name code id char)
1150 (cdr output-list))))
1151 nil)
2cb842ae
KH
1152
1153;; Mule specific initializers.
1154
1155;;;###autoload
1156(defun ps-mule-initialize ()
1157 "Initialize global data for printing multi-byte characters."
76875dcb 1158 (setq ps-mule-prologue-generated nil
d0da93b3 1159 ps-mule-composition-prologue-generated nil
2cb842ae
KH
1160 ps-mule-bitmap-prologue-generated nil)
1161 (mapcar `(lambda (x) (setcar (nthcdr 2 x) nil))
1162 ps-mule-external-libraries))
1163
922be019
GM
1164(defun ps-mule-encode-header-string (string fonttag)
1165 "Generate PostScript code for ploting STRING by font FONTTAG.
76875dcb
KH
1166FONTTAG should be a string \"/h0\", \"/h1\", \"/L0\", or \"/H0\"."
1167 (with-temp-buffer
1168 (insert string)
1169 (ps-mule-encode-region (point-min) (point-max)
1170 (aref ps-mule-font-spec-tables
1171 (aref ps-mule-font-number-to-type
1172 (cond ((string= fonttag "/h0") 4)
1173 ((string= fonttag "/h1") 5)
1174 ((string= fonttag "/L0") 6)
1175 (t 0)))))))
922be019 1176
2cb842ae
KH
1177;;;###autoload
1178(defun ps-mule-begin-job (from to)
1179 "Start printing job for multi-byte chars between FROM and TO.
1180This checks if all multi-byte characters in the region are printable or not."
76875dcb
KH
1181 (auto-compose-region from to)
1182 (if (and (not (find-composition from to))
1183 (save-excursion
1184 (goto-char from)
1185 (= (skip-chars-forward "\x00-\xFF" to) to)))
1186 ;; All characters can be printed by normal PostScript fonts.
1187 (setq ps-basic-plot-string-function 'ps-basic-plot-string
1188 ps-encode-header-string-function 'identity)
1189 (setq ps-basic-plot-string-function 'ps-mule-plot-string
1190 ps-encode-header-string-function 'ps-mule-encode-header-string
1191 ps-mule-font-info-database
1192 (cond ((eq ps-multibyte-buffer 'non-latin-printer)
1193 ps-mule-font-info-database-ps)
1194 ((eq ps-multibyte-buffer 'bdf-font)
1195 ps-mule-font-info-database-bdf)
1196 ((eq ps-multibyte-buffer 'bdf-font-except-latin)
1197 ps-mule-font-info-database-ps-bdf)
1198 (t
1199 ps-mule-font-info-database-default)))
1200
1201 ;; Be sure to have font information for Latin-1.
1202 (or (assq 'iso-8859-1 ps-mule-font-info-database)
1203 (setq ps-mule-font-info-database
1204 (cons '((iso-8859-1 (normal nil nil)))
1205 ps-mule-font-info-database)))
1206
1207 ;; Generate ps-mule-font-spec-tables.
1208 (let ((font-spec-alist (make-vector 4 nil))
1209 (id-max 0)
1210 (font-id 0)
1211 font-info-list)
1212 ;; Generate properly ordered font-info-list from
1213 ;; ps-mule-font-info-database.
1214 (let ((charset-list
1215 (copy-sequence (get-language-info current-language-environment
1216 'charset))))
1217 (setq charset-list (cons 'iso-8859-1 (delq 'iso-8859-1 charset-list)))
1218 (dolist (charset charset-list)
1219 (let ((font-info (assq charset ps-mule-font-info-database)))
1220 (and font-info
1221 (setq font-info-list (cons font-info font-info-list)))))
1222 (dolist (font-info ps-mule-font-info-database)
1223 (or (memq (car font-info) charset-list)
1224 (setq font-info-list (cons font-info font-info-list))))
1225 (setq font-info-list (nreverse font-info-list)))
1226
1227 ;; Store FONT-SPECs in each element of font-spec-alist.
1228 (dolist (font-info font-info-list)
1229 (let ((font-spec-vec (make-vector 4 nil))
1230 (charset (car font-info))
1231 encoding font-spec)
1232 (dolist (e (cdr font-info))
1233 (setq encoding (or (nth 3 e) charset)
1234 font-spec (vector id-max charset font-id
1235 (nth 1 e) (nth 2 e) encoding
1236 (or (nth 4 e) (charset-dimension encoding))
1237 nil)
1238 id-max (1+ id-max))
1239 (if (ps-mule-check-font font-spec)
1240 (aset font-spec-vec
1241 (cond ((eq (car e) 'normal) 0)
1242 ((eq (car e) 'bold) 1)
1243 ((eq (car e) 'italic) 2)
1244 (t 3)) font-spec)))
1245 (when (aref font-spec-vec 0)
1246 (or (aref font-spec-vec 3)
1247 (aset font-spec-vec 3 (or (aref font-spec-vec 1)
1248 (aref font-spec-vec 2)
1249 (aref font-spec-vec 0))))
1250 (or (aref font-spec-vec 1)
1251 (aset font-spec-vec 1 (aref font-spec-vec 0)))
1252 (or (aref font-spec-vec 2)
1253 (aset font-spec-vec 2 (aref font-spec-vec 1)))
1254 (dotimes (i 4)
1255 (aset font-spec-alist i
1256 (nconc (aref font-spec-alist i)
1257 (list (cons charset (aref font-spec-vec i))))))
1258 (setq font-id (1+ font-id)))))
1259
1260 ;; Make four FONT-SPEC-TABLEs and set them in
1261 ;; ps-mule-font-spec-tables. Each char table has one extra slot
1262 ;; whose value is an element of font-spec-alist.
1263 (setq ps-mule-font-spec-tables (make-vector 4 nil))
1264 (put 'font-spec-table 'char-table-extra-slots 1)
1265 (dotimes (i 4)
1266 (let ((table (make-char-table 'font-spec-table)))
1267 (aset ps-mule-font-spec-tables i table)
1268 (set-char-table-extra-slot table 0 (aref font-spec-alist i))
1269 ;; Be sure to have glyphs for "0123456789/" in advance for
1270 ;; page numbering.
1271 (let ((str " 0123456789/"))
1272 (dotimes (i (length str))
1273 (or (vectorp (ps-mule-get-font-spec (aref str i) table nil))
1274 (error "ASCII font not available")))))))
1275
1276 (ps-mule-prologue-generated)
1277 (if (find-composition from to)
1278 (ps-mule-composition-prologue-generated))))
1279
1280(defun ps-mule-restruct-output-list (list tail)
1281 (dolist (elt list)
1282 (if (listp elt)
1283 (setq tail (ps-mule-restruct-output-list elt tail))
1284 (setcdr tail (cons elt (cdr tail)))
1285 (setq tail (cdr tail))))
1286 tail)
1287
1288(defun ps-mule-redefine-font (font-number fonttag size ps-font)
1289 (let* ((font-type (aref ps-mule-font-number-to-type font-number))
1290 (font-spec-alist (char-table-extra-slot
1291 (aref ps-mule-font-spec-tables font-type) 0)))
1292 (ps-output-prologue
1293 (list (if (ps-mule-font-spec-src (cdr (car font-spec-alist)))
1294 ;; We ignore a font specfied in ps-font-info-database.
1295 (format "/V%s VTOP%d def\n" fonttag font-type)
1296 (format "/V%s [ VTOP%d aload pop ] def V%s 0 /%s findfont put\n"
1297 fonttag font-type fonttag ps-font))
1298 (format "/%s ETOP%d V%s %f ReDefFont\n"
1299 fonttag font-type fonttag size)))))
2cb842ae 1300
2cb842ae 1301
76875dcb
KH
1302;;;###autoload
1303(defun ps-mule-end-job ()
1304 "Finish printing job for multi-byte chars."
1305
1306 ;; Prepare root and sub fonts while generating glyphs if necessary.
1307 (let ((output-head (list t))
1308 (ps-mule-output-list (list t)))
1309 (dotimes (i 4)
1310 (map-char-table 'ps-mule-prepare-glyph
1311 (aref ps-mule-font-spec-tables i)))
1312 (ps-mule-restruct-output-list (cdr ps-mule-output-list) output-head)
1313 (ps-output-prologue (cdr output-head)))
1314
1315 ;; Prepare top Encoding and templates of FDepVector.
1316 (dotimes (i 4)
1317 (let ((font-spec-alist (char-table-extra-slot
1318 (aref ps-mule-font-spec-tables i) 0))
1319 font-list font-spec)
1320 (dolist (elt font-spec-alist)
1321 (setq font-spec (cdr elt))
1322 (if (ps-mule-font-spec-extra font-spec)
1323 (push (cons (ps-mule-font-spec-font-id font-spec)
1324 (ps-mule-font-spec-id font-spec))
1325 font-list)))
1326 (setq font-list (nreverse font-list))
1327 (ps-output-prologue
1328 (list (format "/ETOP%d 256 array def\n" i)
1329 (format "0 1 255 { ETOP%d exch 0 put } for\n" i)))
1330 (let ((index 0))
1331 (dolist (font font-list)
1332 (ps-output-prologue (format "ETOP%d %d %d put\n" i (car font) index))
1333 (setq index (1+ index))))
1334 (ps-output-prologue (format "/VTOP%d [%s] def\n" i
1335 (mapconcat #'(lambda (x)
1336 (format "F%02X" (cdr x)))
1337 font-list " ")))))
1338
1339 ;; Redefine fonts f0, f1, f2, f3, h0, h1, H0.
1340 (ps-mule-redefine-font 4 "h0" ps-header-title-font-size-internal
1341 (ps-font 'ps-font-for-header 'bold))
1342 (ps-mule-redefine-font 5 "h1" ps-header-font-size-internal
1343 (ps-font 'ps-font-for-header 'normal))
1344 (ps-mule-redefine-font 6 "H0" ps-footer-font-size-internal
1345 (ps-font 'ps-font-for-footer 'normal))
1346 (let ((font (ps-font-alist 'ps-font-for-text))
1347 (i 0))
1348 (while font
1349 (ps-mule-redefine-font i (format "f%d" i)
1350 ps-font-size-internal
1351 (ps-font 'ps-font-for-text (car (car font))))
1352 (setq font (cdr font)
1353 i (1+ i)))))
2cb842ae
KH
1354
1355(provide 'ps-mule)
1356
1357;;; ps-mule.el ends here