1 ;;; ps-mule.el --- Provide multi-byte character facility to ps-print.
3 ;; Copyright (C) 1998, 1999 Free Software Foundation, Inc.
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>
9 ;; Keywords: wp, print, PostScript, multibyte, mule
10 ;; Time-stamp: <99/06/24 23:07:11 vinicius>
12 ;; This file is part of GNU Emacs.
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)
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.
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.
31 ;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
36 ;; This package is used for ps-print to print multi-byte buffer.
38 ;; See also ps-print.el.
41 ;; Printing Multi-byte Buffer
42 ;; --------------------------
44 ;; The variable `ps-multibyte-buffer' specifies the ps-print multi-byte buffer
47 ;; Valid values for `ps-multibyte-buffer' are:
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
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,
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).
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'.
85 ;; Any other value is treated as nil.
87 ;; The default is nil.
89 ;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
93 (eval-and-compile (require 'ps-print
))
96 (require 'ps-print-def
) ; Common definitions
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'.
102 ;;(defcustom ps-multibyte-buffer nil
103 ;; "*Specifies the multi-byte buffer handling.
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
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,
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).
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'.
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)
149 ;; For Emacs 20.2 and the earlier version.
151 (if (and (boundp 'mule-version
) ; only if mule package is loaded
152 (not (string< mule-version
"4.0")))
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)
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
)
166 (+ i
(charset-bytes (char-charset (string-to-char string
)))))))
168 (defvar ps-mule-font-info-database
170 "Alist of charsets with the corresponding font information.
171 Each element has the form:
173 (CHARSET (FONT-TYPE FONT-SRC FONT-NAME ENCODING BYTES) ...)
177 CHARSET is a charset (symbol) for this font family,
179 FONT-TYPE is a font type: normal, bold, italic, or bold-italic.
181 FONT-SRC is a font source: builtin, ps-bdf, vflib, or nil.
183 If FONT-SRC is builtin, FONT-NAME is a buitin PostScript font name.
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'
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.
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.
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.
200 BYTES specifies how many bytes each character has in the encoded byte
201 sequence; it should be 1 or 2.
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
208 See also the variable `ps-font-info-database'.")
210 (defconst ps-mule-font-info-database-latin
212 (normal nil nil iso-latin-1
)))
213 "Sample setting of `ps-mule-font-info-database' to use latin fonts.")
215 (defcustom ps-mule-font-info-database-default
216 ps-mule-font-info-database-latin
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")
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
)
227 (other :tag
"nil" nil
))
228 (list (string :tag
"Font Name"))
229 (function :tag
"Encoding")
230 (integer :tag
"Bytes")))))
231 :group
'ps-print-font
)
233 (defconst ps-mule-font-info-database-ps
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))
239 (normal builtin
"Ryumin-Light.Hankaku" ps-mule-encode-7bit
1)
240 (bold builtin
"GothicBBB-Medium.Hankaku" ps-mule-encode-7bit
1))
242 (normal builtin
"Ryumin-Light-H" ps-mule-encode-7bit
2)
243 (bold builtin
"GothicBBB-Medium-H" ps-mule-encode-7bit
2))
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))
248 "Sample setting of the `ps-mule-font-info-database' to use builtin PS font.
250 Currently, data for Japanese and Korean PostScript printers are listed.")
252 (defconst ps-mule-font-info-database-bdf
254 (normal bdf
("lt1-24-etl.bdf" "etl24-latin1.bdf") nil
1)
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))
259 (normal bdf
("lt1-24-etl.bdf" "etl24-latin1.bdf") iso-latin-1
1)
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))
264 (normal bdf
("lt2-24-etl.bdf" "etl24-latin2.bdf") iso-latin-2
1))
266 (normal bdf
("lt3-24-etl.bdf" "etl24-latin3.bdf") iso-latin-3
1))
268 (normal bdf
("lt4-24-etl.bdf" "etl24-latin4.bdf") iso-latin-4
1))
270 (normal bdf
("thai24.bdf" "thai-24.bdf") thai-tis620
1))
272 (normal bdf
("grk24-etl.bdf" "etl24-greek.bdf") greek-iso-8bit
1))
273 ;; (arabic-iso8859-6 nil) ; not yet available
275 (normal bdf
("heb24-etl.bdf" "etl24-hebrew.bdf") hebrew-iso-8bit
1))
277 (normal bdf
"12x24rk.bdf" ps-mule-encode-8bit
1))
279 (normal bdf
"12x24rk.bdf" ps-mule-encode-7bit
1))
281 (normal bdf
("cyr24-etl.bdf" "etl24-cyrillic.bdf") cyrillic-iso-8bit
1))
283 (normal bdf
("lt5-24-etl.bdf" "etl24-latin5.bdf") iso-latin-5
1))
284 (japanese-jisx0208-1978
285 (normal bdf
"jiskan24.bdf" ps-mule-encode-7bit
2))
287 (normal bdf
"gb24st.bdf" ps-mule-encode-7bit
2))
289 (normal bdf
"jiskan24.bdf" ps-mule-encode-7bit
2))
291 (normal bdf
"hanglm24.bdf" ps-mule-encode-7bit
2))
293 (normal bdf
("jksp40.bdf" "jisksp40.bdf") ps-mule-encode-7bit
2))
295 (normal bdf
("cns1-40.bdf" "cns-1-40.bdf") ps-mule-encode-7bit
2))
297 (normal bdf
("cns2-40.bdf" "cns-2-40.bdf") ps-mule-encode-7bit
2))
299 (normal bdf
"taipei24.bdf" chinese-big5
2))
301 (normal bdf
"taipei24.bdf" chinese-big5
2))
303 (normal bdf
("sish24-etl.bdf" "etl24-sisheng.bdf") ps-mule-encode-8bit
1))
305 (normal bdf
("ipa24-etl.bdf" "etl24-ipa.bdf") ps-mule-encode-8bit
1))
306 (vietnamese-viscii-lower
307 (normal bdf
("visc24-etl.bdf" "etl24-viscii.bdf") vietnamese-viscii
1))
308 (vietnamese-viscii-upper
309 (normal bdf
("visc24-etl.bdf" "etl24-viscii.bdf") vietnamese-viscii
1))
311 (normal bdf
("arab24-0-etl.bdf" "etl24-arabic0.bdf") ps-mule-encode-7bit
1))
313 (normal bdf
("arab24-1-etl.bdf" "etl24-arabic1.bdf") ps-mule-encode-7bit
1))
314 ;; (ascii-right-to-left nil) ; not yet available
316 (normal bdf
("lao24-mule.bdf" "mule-lao-24.bdf") lao
1))
318 (normal bdf
("arab24-2-etl.bdf" "etl24-arabic2.bdf") ps-mule-encode-7bit
1))
320 (normal bdf
("isci24-etl.bdf" "mule-iscii-24.bdf") ps-mule-encode-7bit
1))
322 (normal bdf
("ind1c24-mule.bdf" "mule-indian-1col-24.bdf") ps-mule-encode-7bit
2))
324 (normal bdf
("tib1c24-mule.bdf" "mule-tibmdx-1col-24.bdf") ps-mule-encode-7bit
2))
326 (normal bdf
("ethio24f-uni.bdf" "ethiomx24f-uni.bdf") ps-mule-encode-ethiopic
2))
328 (normal bdf
("cns3-40.bdf" "cns-3-40.bdf") ps-mule-encode-7bit
2))
330 (normal bdf
("cns4-40.bdf" "cns-4-40.bdf") ps-mule-encode-7bit
2))
332 (normal bdf
("cns5-40.bdf" "cns-5-40.bdf") ps-mule-encode-7bit
2))
334 (normal bdf
("cns6-40.bdf" "cns-6-40.bdf") ps-mule-encode-7bit
2))
336 (normal bdf
("cns7-40.bdf" "cns-7-40.bdf") ps-mule-encode-7bit
2))
338 (normal bdf
("ind24-mule.bdf" "mule-indian-24.bdf") ps-mule-encode-7bit
2))
340 (normal bdf
("tib24-mule.bdf" "mule-tibmdx-24.bdf") ps-mule-encode-7bit
2)))
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
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.
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.
351 See also `ps-mule-font-info-database-ps-bdf'.")
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.
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.
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'.
366 See also `ps-mule-font-info-database-bdf'.")
368 ;; Two typical encoding functions for PostScript fonts.
370 (defun ps-mule-encode-7bit (string)
371 (ps-mule-encode-bit string
0))
373 (defun ps-mule-encode-8bit (string)
374 (ps-mule-encode-bit string
128))
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))
385 (+ (nth 1 (split-char (ps-mule-string-char string i
))) delta
))
386 (setq i
(ps-mule-next-index string i
)
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
)
396 ;; Special encoding function for Ethiopic.
397 (if (boundp 'mule-version
) ; only if mule package is loaded
398 (define-ccl-program ccl-encode-ethio-unicode
402 (if (r2 == ,leading-code-private-22
)
404 (if (r0 == ,(charset-id 'ethiopic
))
406 (r1 &= 127) (r2 &= 127)
407 (call ccl-encode-ethio-font
)
409 (write-read-repeat r2
))
412 (write-read-repeat r2
))))))
413 ;; to avoid compilation gripes
414 (defvar ccl-encode-ethio-unicode nil
))
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
)
422 ;; unbound mule-version
423 (defun ps-mule-encode-ethiopic (string)
426 ;; A charset which we are now processing.
427 (defvar ps-mule-current-charset nil
)
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:
433 (FONT-SRC FONT-NAME ENCODING BYTES)
435 FONT-SPEC is extracted from `ps-mule-font-info-database'.
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
))))
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
))))))
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
))
452 (defsubst ps-mule-printable-p
(charset)
453 "Non-nil if characters in CHARSET is printable."
454 (ps-mule-get-font-spec charset
'normal
))
456 (defconst ps-mule-external-libraries
460 bdf-generate-prologue bdf-generate-font bdf-generate-glyphs
)
462 pcf-generate-prologue pcf-generate-font pcf-generate-glyphs
)
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:
468 (FONT-SRC FEATURE INITIALIZED-P PROLOGUE-FUNC FONT-FUNC GLYPHS-FUNC)
470 FONT-SRC is the font source: builtin, bdf, pcf, or vflib.
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'.
476 INITIALIZED-P indicates if this library is initialized or not.
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.
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.
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.")
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
)))
496 (let ((func (nth 3 slot
)))
499 (or (featurep (nth 1 slot
)) (require (nth 1 slot
)))
500 (ps-output-prologue (funcall func
))))
501 (setcar (nthcdr 2 slot
) t
)))))
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
)
508 (defun ps-mule-generate-font (font-spec charset
)
509 "Generate PostScript codes to define a new font in FONT-SPEC for CHARSET."
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
))
513 (font-src (ps-mule-font-spec-src font-spec
))
514 (func (nth 4 (assq font-src ps-mule-external-libraries
)))
516 (if (eq charset
'ascii
)
517 (format "f%d" ps-current-font
)
519 (charset-id charset
) ps-current-font
))))
520 (and func
(not font-cache
)
521 (ps-output-prologue (funcall func charset font-spec
)))
523 (list (format "/%s %f /%s Def%sFontMule\n"
524 scaled-font-name ps-font-size-internal font-name
525 (if (eq ps-mule-current-charset
'ascii
) "Ascii" ""))))
527 (setcar (cdr font-cache
)
528 (cons (cons ps-current-font scaled-font-name
)
530 (setq font-cache
(list font-name
531 (list (cons ps-current-font scaled-font-name
))
533 ps-mule-font-cache
(cons font-cache ps-mule-font-cache
)))
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
))))
542 (funcall func font-spec code-list
543 (ps-mule-font-spec-bytes font-spec
))))))
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.
548 The generated code is inserted on prologue part except the code that sets the
549 current font (using PostScript procedure `FM').
551 If optional arg NO-SETFONT is non-nil, don't generate the code for setting the
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
)))
556 (or (and font-cache
(assq ps-current-font
(nth 1 font-cache
)))
557 (setq font-cache
(ps-mule-generate-font font-spec charset
)))
559 (let ((new-font (cdr (assq ps-current-font
(nth 1 font-cache
)))))
560 (or (equal new-font ps-last-font
)
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
))
573 (setq code
(if (= bytes
1)
575 (+ (* (aref string i
) 256) (aref string
(1+ i
)))))
576 (or (memq code cached-codes
)
578 (setq newcodes
(cons code newcodes
))
579 (setcdr cached-codes
(cons code
(cdr cached-codes
)))))
580 (setq i
(+ i bytes
)))
582 (ps-mule-generate-glyphs font-spec newcodes
))))))
585 (defun ps-mule-prepare-ascii-font (string)
586 "Setup special ASCII font for STRING.
587 STRING should contain only ASCII characters."
589 (ps-mule-get-font-spec
591 (car (nth ps-current-font
(ps-font-alist 'ps-font-for-text
))))))
593 (ps-mule-prepare-font font-spec string
'ascii
))))
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
)))
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),
604 (defvar ps-mule-charset-list nil
)
606 ;; This is a PostScript code inserted in the header of generated PostScript.
607 (defconst ps-mule-prologue
608 "%%%% Start of Mule Section
610 %% Working dictionary for general use.
611 /MuleDict 10 dict def
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
620 /BaselineOffset 0 def
622 currentdict /RelativeCompose known not {
623 /RelativeCompose [ 0 0.1 ] def
625 RelativeCompose false ne {
626 [ BaselineOffset RelativeCompose BaselineOffset add
627 [ FontMatrix { FontSize div } forall ] transform ]
628 /RelativeCompose exch def
635 %% Define already scaled font for non-ASCII character sets.
636 /DefFontMule { % fontname size basefont |- --
637 findfont exch scalefont AdjustRelativeCompose definefont pop
640 %% Define already scaled font for ASCII character sets.
641 /DefAsciiFontMule { % fontname size basefont |-
643 findfont dup /Encoding get /ISOLatin1Encoding exch def
644 exch scalefont AdjustRelativeCompose reencodeFontISO
648 %% Set the specified non-ASCII font to use. It doesn't install
650 /FM { % fontname |- --
654 %% Show vacant box for characters which don't have appropriate font.
655 /SB { % count column |- --
656 SpaceWidth mul /w exch def
661 0 Descent rmoveto w 0 rlineto
662 0 LineHeight rlineto w neg 0 rlineto closepath stroke
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.
673 %%%% End of Mule Section
676 "PostScript code for printing multi-byte characters.")
678 (defvar ps-mule-prologue-generated nil
)
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
)))
685 (defun ps-mule-find-wrappoint (from to char-width
)
686 "Find the longest sequence which is printable in the current line.
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'.
691 CHAR-WIDTH is the average width of ASCII characters in the current font.
697 Where ENDPOS is the end position of the sequence and RUN-WIDTH is the width of
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
)
713 (truncate (/ ps-width-remaining char-width
)))))
715 (cons to run-width
)))))
718 (defun ps-mule-plot-string (from to
&optional bg-color
)
719 "Generate PostScript code for ploting characters in the region FROM and TO.
721 It is assumed that all characters in this region belong to the same charset.
723 Optional argument BG-COLOR specifies background color.
729 Where ENDPOS is the end position of the sequence and RUN-WIDTH is the width of
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
)))
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
)))
741 ;; We can't print any more characters in the current line.
745 ;; We surely have a font for printing this character set.
746 (ps-output-string (ps-mule-string-encoding font-spec string
))
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
))
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
))))
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"
767 (if (eq ps-mule-current-charset
'composition
)
768 (char-width (char-after from
))
769 (charset-width ps-mule-current-charset
))))))
772 ;; Composite font support
774 (defvar ps-mule-cmpchar-prologue-generated nil
)
776 (defconst ps-mule-cmpchar-prologue
777 "%%%% Composite character handler
779 /CmpcharRelativeCompose 0 def
780 /CmpcharRelativeSkip 0.4 def
782 %% Get a bounding box (relative to currentpoint) of STR.
783 /GetPathBox { % str |- --
785 currentfont /FontType get 3 eq { %ifelse
788 currentpoint /y exch def /x exch def
789 false charpath flattenpath pathbbox
790 y sub /URY exch def x sub /URX exch def
791 y sub /LLY exch def x sub /LLX exch def
796 %% Beginning of composite char.
797 /BC { % str xoff width |- --
799 /CmpcharWidth exch def
800 currentfont /RelativeCompose known {
801 /CmpcharRelativeCompose currentfont /RelativeCompose get def
803 /CmpcharRelativeCompose false def
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
811 /Effect Effectsave 8 32 add and def % enable only shadow and outline
814 SpaceWidth mul 0 rmoveto dup GetPathBox S
815 /RIGHT currentpoint pop def
817 /y currentpoint exch pop def
818 /HIGH URY y add def /LOW LLY y add def
821 %% End of composite char.
823 /bg bgsave def /bgcolor bgcolorsave def
824 /Effect Effectsave def
826 CmpcharRelativeCompose false eq {
827 CmpcharWidth SpaceWidth mul 0 rmoveto
829 RIGHT currentpoint exch pop moveto
833 %% Rule base composition
834 /RBC { % str xoff gref nref |- --
835 /nref exch def /gref exch def
837 SpaceWidth mul 0 rmoveto
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
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
849 /CmpcharRelativeCompose false def
852 %% Relative composition
856 LLX 0 lt { RIGHT currentpoint exch pop moveto } if
857 CmpcharRelativeCompose type /arraytype eq {
858 LLY CmpcharRelativeCompose 1 get ge { % compose on top
859 currentpoint pop HIGH LLY sub CmpcharRelativeSkip add moveto
860 /HIGH HIGH URY LLY sub add CmpcharRelativeSkip add def
861 } { URY CmpcharRelativeCompose 0 get le { % compose under bottom
862 currentpoint pop LOW URY sub CmpcharRelativeSkip sub moveto
863 /LOW LOW URY LLY sub sub CmpcharRelativeSkip sub def
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
872 %%%% End of composite character handler
875 "PostScript code for printing composite characters.")
877 (defun ps-mule-plot-rule-cmpchar (ch-rule-list total-width font-type
)
879 (rightmost (float (char-width (car ch-rule-list
))))
880 (the-list (cons '(3 .
3) ch-rule-list
))
883 (let* ((this (car the-list
))
886 ;; X-axis info (0:left, 1:center, 2:right)
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
)))
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
)
900 leftmost
(min left leftmost
)
901 rightmost
(max (+ left width
) rightmost
)
902 the-list
(nthcdr 2 the-list
))))
904 (let ((the-list cmpchar-elements
)
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
)))
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
))
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
926 (format " %d %d %d RBC "
927 (nth 1 elt
) (nth 2 elt
) (nth 3 elt
))))))
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
)))
934 (ps-mule-string-encoding font-spec
(char-to-string char
)))
936 ((eq ps-mule-current-charset
'latin-iso8859-1
)
937 (ps-mule-string-ascii (char-to-string char
)))
941 (ps-set-font ps-current-font
)
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
)))
948 (defun ps-mule-string-encoding (font-spec str
)
949 (let ((encoding (ps-mule-font-spec-encoding font-spec
)))
952 (cond ((coding-system-p encoding
)
953 (encode-coding-string str encoding
))
954 ((functionp encoding
)
955 (funcall encoding str
))
957 (error "Invalid coding system or function: %s" encoding
))
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
))
965 ;; Bitmap font support
967 (defvar ps-mule-bitmap-prologue-generated nil
)
969 (defconst ps-mule-bitmap-prologue
970 "%%%% Bitmap font handler
972 /str7 7 string def % working area
974 %% We grow the dictionary one bunch (1024 entries) by one.
975 /BitmapDictArray 256 array def
976 /BitmapDictLength 1024 def
977 /BitmapDictIndex -1 def
979 /NewBitmapDict { % -- |- --
980 /BitmapDictIndex BitmapDictIndex 1 add def
981 BitmapDictArray BitmapDictIndex BitmapDictLength dict put
984 %% Make at least one dictionary.
987 /AddBitmap { % gloval-charname bitmap-data |- --
988 BitmapDictArray BitmapDictIndex get
989 dup length BitmapDictLength ge {
992 BitmapDictArray BitmapDictIndex get
997 /GetBitmap { % gloval-charname |- bitmap-data
998 0 1 BitmapDictIndex { BitmapDictArray exch get begin } for
1000 0 1 BitmapDictIndex { pop end } for
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
1010 %% Character code holder for a 2-byte character.
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
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 ]
1030 /FontMatrix get [ exch { size div } forall ] /mtrx exch def
1031 bmp 3 get bmp 4 get mtrx transform
1032 /LLY exch def /LLX exch def
1033 bmp 1 get bmp 3 get add bmp 2 get bmp 4 get add mtrx transform
1034 /URY exch def /URX exch def
1040 bmp 0 get SpaceWidthRatio ratio div mul size div 0 % wx wy
1041 setcharwidth % We can't use setcachedevice here.
1043 bmp 1 get 0 gt bmp 2 get 0 gt and {
1044 bmp 1 get bmp 2 get % width height
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
1054 1 index /Encoding get exch get
1055 1 index /BuildGlyph get exec
1058 %% Bitmap font creater
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
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
1071 /GlobalFontIndex 0 def
1073 %% fontname dim col fontsize relative-compose baseline-offset fbbx |- --
1077 /BaselineOffset exch def
1078 /RelativeCompose 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
1086 /FontMatrix matrix def
1087 /Encoding EncodingCommon def
1088 /BuildGlyph { BuildGlyphCommon } def
1089 /BuildChar { BuildCharCommon } def
1092 /GlobalFontIndex GlobalFontIndex 1 add def
1095 %% Define a new bitmap font.
1096 %% fontname dim col fontsize relative-compose baseline-offset fbbx |- --
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 ]
1105 %% Define a glyph for the specified font and character.
1106 /NG { % fontname charcode bitmap-data |- --
1108 exch findfont dup /BaselineOffset get bmp 4 get add bmp exch 4 exch put
1110 dup 256 idiv exch 256 mod GlobalCharName
1113 %%%% End of bitmap font handler
1117 ;; External library support.
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.
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
)))
1128 (defun ps-mule-generate-bitmap-font (&rest args
)
1129 (list (apply 'format
"/%s %d %d %f %S %d %S NF\n" args
)))
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"
1134 dwidth
(aref bbx
0) (aref bbx
1) (aref bbx
2) (aref bbx
3)
1137 ;; Mule specific initializers.
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
))
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
)
1162 ps-mule-font-info-database-default
)))
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
)))
1168 (setq charsets
(delq 'ascii
(delq 'unknown
(delq nil charsets
)))
1169 ps-mule-charset-list charsets
)
1172 (and (search-forward "\200" to t
)
1173 (setq ps-mule-charset-list
1174 (cons 'composition ps-mule-charset-list
))))
1178 ((or (eq (car charsets
) 'composition
)
1179 (ps-mule-printable-p (car charsets
)))
1182 "Font for some characters not found, continue anyway? ")
1185 (error "Printing cancelled")))))))
1187 (setq ps-mule-current-charset
'ascii
)
1189 (if ps-mule-charset-list
1190 (let ((the-list ps-mule-charset-list
)
1192 (ps-mule-prologue-generated)
1193 ;; If external functions are necessary, generate prologues for them.
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
))))))
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
)))
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))
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
)))))))
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
)
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]"))
1229 (t (string-as-multibyte "[^\000-\011\013\015-\377]"))))))
1232 (defun ps-mule-begin-page ()
1233 (setq ps-mule-current-charset
'ascii
))
1238 ;;; ps-mule.el ends here