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