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