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