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