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