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