Sync to HEAD
[bpt/emacs.git] / lisp / ps-bdf.el
CommitLineData
e8af40ee 1;;; ps-bdf.el --- BDF font file handler for ps-print
e62e3e6b 2
76875dcb
KH
3;; Copyright (C) 1998, 1999, 2001 Electrotechnical Laboratory, JAPAN.
4;; Licensed to the Free Software Foundation.
5;; Copyright (C) 2001, 2002, 2003 Free Software Foundation, Inc.
6;; Copyright (C) 2003
7;; National Institute of Advanced Industrial Science and Technology (AIST)
8;; Registration Number H13PRO009
e62e3e6b 9
3ad114e5 10;; Keywords: wp, BDF, font, PostScript
76875dcb 11;; Maintainer: Kenichi Handa <handa@m17n.org>
7a1e1973 12;; Time-stamp: <2003/07/11 21:13:44 vinicius>
e62e3e6b
KH
13
14;; This file is part of GNU Emacs.
15
16;; GNU Emacs is free software; you can redistribute it and/or modify
17;; it under the terms of the GNU General Public License as published by
18;; the Free Software Foundation; either version 2, or (at your option)
19;; any later version.
20
21;; GNU Emacs is distributed in the hope that it will be useful,
22;; but WITHOUT ANY WARRANTY; without even the implied warranty of
23;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24;; GNU General Public License for more details.
25
26;; You should have received a copy of the GNU General Public License
27;; along with GNU Emacs; see the file COPYING. If not, write to the
28;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
29;; Boston, MA 02111-1307, USA.
30
31;;; Commentary:
32
33;; Functions for getting bitmap information from X's BDF font file are
34;; provided.
35
36;;; Code:
37
64d8e7fd
GM
38(eval-and-compile
39 (require 'ps-mule)
40
41 ;; to avoid XEmacs compilation gripes
42 (defvar installation-directory nil)
43 (defvar coding-system-for-read nil))
e62e3e6b
KH
44
45;;;###autoload
3e3cf0a7 46(defvar bdf-directory-list
7a1e1973 47 (if (memq system-type '(ms-dos windows-nt))
0a51cf3d 48 (list (expand-file-name "fonts/bdf" installation-directory))
3e3cf0a7 49 '("/usr/local/share/emacs/fonts/bdf"))
1b3172da 50 "*List of directories to search for `BDF' font files.
5062e90d 51The default value is '(\"/usr/local/share/emacs/fonts/bdf\").")
e62e3e6b 52
3f4f2289
EZ
53;; MS-DOS and MS-Windows users like to move the binary around after
54;; it's built, but the value above is computed at load-up time.
7a1e1973 55(and (memq system-type '(ms-dos windows-nt))
0a51cf3d
EZ
56 (setq bdf-directory-list
57 (list (expand-file-name "fonts/bdf" installation-directory))))
58
e62e3e6b 59(defun bdf-expand-file-name (bdfname)
64d8e7fd 60 "Return an absolute path name of a `BDF' font file BDFNAME.
e62e3e6b
KH
61It searches directories listed in the variable `bdf-directory-list'
62for BDFNAME."
63 (if (file-name-absolute-p bdfname)
64 (and (file-readable-p bdfname)
65 bdfname)
76875dcb
KH
66 (catch 'tag
67 (dolist (dir bdf-directory-list)
68 (let ((absolute-path (expand-file-name bdfname dir)))
69 (if (file-readable-p absolute-path)
70 (throw 'tag absolute-path)))))))
e62e3e6b
KH
71
72(defsubst bdf-file-mod-time (filename)
73 "Return modification time of FILENAME.
74The value is a list of two integers, the first integer has high-order
7516 bits, the second has low 16 bits."
76 (nth 5 (file-attributes filename)))
77
78(defun bdf-file-newer-than-time (filename mod-time)
79 "Return non-nil if and only if FILENAME is newer than MOD-TIME.
80MOD-TIME is a modification time as a list of two integers, the first
81integer has high-order 16 bits, the second has low 16 bits."
76875dcb
KH
82 (let* ((new-mod-time (bdf-file-mod-time filename))
83 (new-time (car new-mod-time))
84 (time (car mod-time)))
85 (or (> new-time time)
86 (and (= new-time time)
87 (> (nth 1 new-mod-time) (nth 1 mod-time))))))
e62e3e6b
KH
88
89(defun bdf-find-file (bdfname)
90 "Return a buffer visiting a bdf file BDFNAME.
76875dcb 91BDFNAME must be an absolute file name.
e62e3e6b 92If BDFNAME doesn't exist, return nil."
76875dcb
KH
93 (and (file-readable-p bdfname)
94 (let ((buf (generate-new-buffer " *bdf-work*"))
95 (coding-system-for-read 'no-conversion))
96 (save-excursion
97 (set-buffer buf)
98 (insert-file-contents bdfname)
99 buf))))
e62e3e6b 100
a5f01960
EZ
101(defvar bdf-cache-file (if (eq system-type 'ms-dos)
102 ;; convert-standard-filename doesn't
103 ;; guarantee that the .el extension will be
104 ;; preserved.
105 "~/_bdfcache.el"
106 (convert-standard-filename "~/.bdfcache.el"))
e62e3e6b
KH
107 "Name of cache file which contains information of `BDF' font files.")
108
109(defvar bdf-cache nil
110 "Cached information of `BDF' font files. It is a list of FONT-INFO.
111FONT-INFO is a list of the following format:
76875dcb 112 (ABSOLUTE-FILE-NAME MOD-TIME SIZE FONT-BOUNDING-BOX
e62e3e6b
KH
113 RELATIVE-COMPOSE BASELINE-OFFSET CODE-RANGE MAXLEN OFFSET-VECTOR)
114See the documentation of the function `bdf-read-font-info' for more detail.")
115
116(defun bdf-read-cache ()
117 "Return a cached information about `BDF' font files from a cache file.
118The variable `bdf-cache-file' holds the cache file name.
119If the cache file is not readable, this return nil."
120 (setq bdf-cache nil)
121 (condition-case nil
122 (and (file-readable-p bdf-cache-file)
123 (progn
124 (load-file bdf-cache-file)
125 (if (listp bdf-cache)
126 bdf-cache
127 (setq bdf-cache nil))))
128 (error nil)))
129
130(defun bdf-write-cache ()
131 "Write out cached information of `BDF' font file to a file.
132The variable `bdf-cache-file' holds the cache file name.
64d8e7fd 133The file is written if and only if the file already exists and writable."
e62e3e6b
KH
134 (and bdf-cache
135 (file-exists-p bdf-cache-file)
136 (file-writable-p bdf-cache-file)
137 (write-region (format "(setq bdf-cache '%S)\n" bdf-cache)
138 nil bdf-cache-file)))
139
140(defun bdf-set-cache (font-info)
141 "Cache FONT-INFO as information about one `BDF' font file.
142FONT-INFO is a list of the following format:
76875dcb 143 (ABSOLUTE-FILE-NAME MOD-TIME SIZE FONT-BOUNDING-BOX
e62e3e6b
KH
144 RELATIVE-COMPOSE BASELINE-OFFSET CODE-RANGE MAXLEN OFFSET-VECTOR)
145See the documentation of the function `bdf-read-font-info' for more detail."
146 (let ((slot (assoc (car font-info) bdf-cache)))
147 (if slot
148 (setcdr slot (cdr font-info))
149 (setq bdf-cache (cons font-info bdf-cache)))))
150
151(defun bdf-initialize ()
152 "Initialize `bdf' library."
153 (and (bdf-read-cache)
154 (add-hook 'kill-emacs-hook 'bdf-write-cache)))
155
156(defun bdf-compact-code (code code-range)
157 (if (or (< code (aref code-range 4))
158 (> code (aref code-range 5)))
159 (setq code (aref code-range 6)))
160 (+ (* (- (lsh code -8) (aref code-range 0))
161 (1+ (- (aref code-range 3) (aref code-range 2))))
162 (- (logand code 255) (aref code-range 2))))
163
164(defun bdf-expand-code (code code-range)
165 (let ((code0-range (1+ (- (aref code-range 3) (aref code-range 2)))))
166 (+ (* (+ (/ code code0-range) (aref code-range 0)) 256)
167 (+ (% code code0-range) (aref code-range 2)))))
168
169(defun bdf-search-and-read (match limit)
170 (goto-char (point-min))
171 (and (search-forward match limit t)
172 (progn
173 (goto-char (match-end 0))
174 (read (current-buffer)))))
175
176(defun bdf-read-font-info (bdfname)
177 "Read `BDF' font file BDFNAME and return information (FONT-INFO) of the file.
76875dcb 178BDFNAME must be an absolute file name.
e62e3e6b 179FONT-INFO is a list of the following format:
76875dcb 180 (BDFFILE MOD-TIME FONT-BOUNDING-BOX
e62e3e6b
KH
181 RELATIVE-COMPOSE BASELINE-OFFSET CODE-RANGE MAXLEN OFFSET-VECTOR)
182
e62e3e6b
KH
183MOD-TIME is last modification time as a list of two integers, the
184first integer has high-order 16 bits, the second has low 16 bits.
185
76875dcb
KH
186SIZE is a size of the font on 72 dpi device. This value is got
187from SIZE record of the font.
e62e3e6b
KH
188
189FONT-BOUNDING-BOX is the font bounding box as a list of four integers,
190BBX-WIDTH, BBX-HEIGHT, BBX-XOFF, and BBX-YOFF.
191
192RELATIVE-COMPOSE is an integer value of the font's property
193`_MULE_RELATIVE_COMPOSE'. If the font doesn't have this property, the
194value is 0.
195
196BASELINE-OFFSET is an integer value of the font's property
197`_MULE_BASELINE_OFFSET'. If the font doesn't have this property, the
198value is 0.
199
200CODE-RANGE is a vector of minimum 1st byte, maximum 1st byte, minimum
2012nd byte, maximum 2nd byte, minimum code, maximum code, and default
202code. For 1-byte fonts, the first two elements are 0.
203
64d8e7fd 204MAXLEN is a maximum bytes of one glyph information in the font file.
e62e3e6b
KH
205
206OFFSET-VECTOR is a vector of a file position which starts bitmap data
207of the glyph in the font file.
208
209Nth element of OFFSET-VECTOR is a file position for the glyph of code
210CODE, where N and CODE are in the following relation:
211 (bdf-compact-code CODE) => N, (bdf-expand-code N) => CODE"
76875dcb 212 (let* ((buf (bdf-find-file bdfname))
e62e3e6b
KH
213 (maxlen 0)
214 (relative-compose 'false)
215 (baseline-offset 0)
216 size
76875dcb 217 dpi
f1180544 218 font-bounding-box
e62e3e6b
KH
219 default-char
220 code-range
221 offset-vector)
222 (if buf
223 (message "Reading %s..." bdfname)
224 (error "BDF file %s doesn't exist" bdfname))
225 (unwind-protect
226 (save-excursion
227 (set-buffer buf)
228 (goto-char (point-min))
229 (search-forward "\nFONTBOUNDINGBOX")
230 (setq font-bounding-box
231 (vector (read (current-buffer)) (read (current-buffer))
232 (read (current-buffer)) (read (current-buffer))))
233 ;; The following kludgy code is to avoid bugs of fonts
234 ;; jiskan16.bdf and jiskan24.bdf distributed with X.
235 ;; They contain wrong FONTBOUNDINGBOX.
236 (and (> (aref font-bounding-box 3) 0)
237 (string-match "jiskan\\(16\\|24\\)" bdfname)
238 (aset font-bounding-box 3
239 (- (aref font-bounding-box 3))))
240
241 (goto-char (point-min))
76875dcb
KH
242 (search-forward "\nFONT ")
243 (if (looking-at "-[^-]*-[^-]*-[^-]*-[^-]*-[^-]*-[^-]*-\\([0-9]+\\)")
244 (setq size (string-to-int (match-string 1)))
245 (search-forward "\nSIZE ")
246 (setq size (read (current-buffer)))
247 ;; The following kludgy code is t avoid bugs of several
248 ;; fonts which have wrong SIZE record.
249 (and (string-match "jiskan" bdfname)
250 (<= size (/ (aref font-bounding-box 1) 3))
251 (setq size (aref font-bounding-box 1)))
252 (setq dpi (read (current-buffer)))
253 (if (and (> dpi 0) (/= dpi 72))
254 (setq size (/ (* size dpi) 72))))
e62e3e6b
KH
255
256 (setq default-char (bdf-search-and-read "\nDEFAULT_CHAR" nil))
257
258 (search-forward "\nSTARTCHAR")
259 (forward-line -1)
260 (let ((limit (point)))
261 (setq relative-compose
262 (or (bdf-search-and-read "\n_MULE_RELATIVE_COMPOSE" limit)
263 'false)
264 baseline-offset
265 (or (bdf-search-and-read "\n_MULE_BASELINE_OFFSET" limit)
266 0)))
267
268 (let ((min-code0 256) (min-code1 256) (min-code 65536)
269 (max-code0 0) (max-code1 0) (max-code 0)
64d8e7fd 270 glyph glyph-list code0 code1 code offset)
e62e3e6b
KH
271
272 (while (search-forward "\nSTARTCHAR" nil t)
273 (setq offset (line-beginning-position))
274 (search-forward "\nENCODING")
275 (setq code (read (current-buffer))
276 code0 (lsh code -8)
277 code1 (logand code 255)
278 min-code (min min-code code)
279 max-code (max max-code code)
280 min-code0 (min min-code0 code0)
281 max-code0 (max max-code0 code0)
282 min-code1 (min min-code1 code1)
283 max-code1 (max max-code1 code1))
284 (search-forward "ENDCHAR")
285 (setq maxlen (max maxlen (- (point) offset))
286 glyph-list (cons (cons code offset) glyph-list)))
287
288 (setq code-range
289 (vector min-code0 max-code0 min-code1 max-code1
290 min-code max-code (or default-char min-code))
291 offset-vector
292 (make-vector (1+ (bdf-compact-code max-code code-range))
293 nil))
294
295 (while glyph-list
296 (setq glyph (car glyph-list)
297 glyph-list (cdr glyph-list))
298 (aset offset-vector
299 (bdf-compact-code (car glyph) code-range)
300 (cdr glyph)))))
301
302 (kill-buffer buf))
303 (message "Reading %s...done" bdfname)
76875dcb 304 (list bdfname (bdf-file-mod-time bdfname)
e62e3e6b
KH
305 size font-bounding-box relative-compose baseline-offset
306 code-range maxlen offset-vector)))
307
76875dcb
KH
308(defsubst bdf-info-absolute-path (font-info) (nth 0 font-info))
309(defsubst bdf-info-mod-time (font-info) (nth 1 font-info))
310(defsubst bdf-info-size (font-info) (nth 2 font-info))
311(defsubst bdf-info-font-bounding-box (font-info) (nth 3 font-info))
312(defsubst bdf-info-relative-compose (font-info) (nth 4 font-info))
313(defsubst bdf-info-baseline-offset (font-info) (nth 5 font-info))
314(defsubst bdf-info-code-range (font-info) (nth 6 font-info))
315(defsubst bdf-info-maxlen (font-info) (nth 7 font-info))
316(defsubst bdf-info-offset-vector (font-info) (nth 8 font-info))
e62e3e6b
KH
317
318(defun bdf-get-font-info (bdfname)
319 "Return information about `BDF' font file BDFNAME.
76875dcb 320BDFNAME must be an absolute file name.
e62e3e6b 321The value FONT-INFO is a list of the following format:
76875dcb 322 (BDFNAME MOD-TIME SIZE FONT-BOUNDING-BOX
e62e3e6b
KH
323 RELATIVE-COMPOSE BASELINE-OFFSET CODE-RANGE MAXLEN OFFSET-VECTOR)
324See the documentation of the function `bdf-read-font-info' for more detail."
325 (or bdf-cache
326 (bdf-read-cache))
327 (let ((font-info (assoc bdfname bdf-cache)))
328 (if (or (not font-info)
76875dcb 329 (not (file-readable-p bdfname))
e62e3e6b
KH
330 (bdf-file-newer-than-time bdfname (bdf-info-mod-time font-info)))
331 (progn
332 (setq font-info (bdf-read-font-info bdfname))
333 (bdf-set-cache font-info)))
334 font-info))
335
76875dcb 336(defun bdf-read-bitmap (bdfname offset maxlen relative-compose)
64d8e7fd
GM
337 "Read `BDF' font file BDFNAME to get bitmap data at file position OFFSET.
338BDFNAME is an absolute path name of the font file.
e62e3e6b
KH
339MAXLEN specifies how many bytes we should read at least.
340The value is a list of DWIDTH, BBX, and BITMAP-STRING.
341DWIDTH is a pixel width of a glyph.
342BBX is a bounding box of the glyph.
343BITMAP-STRING is a string representing bits by hexadecimal digits."
0240cfba 344 (let* ((coding-system-for-read 'no-conversion)
76875dcb 345 (bbx (bdf-info-font-bounding-box (bdf-get-font-info bdfname)))
0240cfba
GM
346 (dwidth (elt bbx 0))
347 (bitmap-string "")
348 height yoff)
e62e3e6b
KH
349 (condition-case nil
350 (with-temp-buffer
351 (insert-file-contents bdfname nil offset (+ offset maxlen))
352 (goto-char (point-min))
353 (search-forward "\nDWIDTH")
354 (setq dwidth (read (current-buffer)))
76875dcb
KH
355 (if (= dwidth 0)
356 (setq dwidth 0.1))
e62e3e6b
KH
357 (goto-char (point-min))
358 (search-forward "\nBBX")
359 (setq bbx (vector (read (current-buffer)) (read (current-buffer))
360 (read (current-buffer)) (read (current-buffer)))
64d8e7fd
GM
361 height (aref bbx 1)
362 yoff (aref bbx 3))
e62e3e6b
KH
363 (search-forward "\nBITMAP")
364 (forward-line 1)
365 (delete-region (point-min) (point))
366 (and (looking-at "\\(0+\n\\)+")
367 (progn
368 (setq height (- height (count-lines (point) (match-end 0))))
369 (delete-region (point) (match-end 0))))
370 (or (looking-at "ENDCHAR")
371 (progn
372 (search-forward "ENDCHAR" nil 'move)
373 (forward-line -1)
374 (while (looking-at "0+$")
375 (setq yoff (1+ yoff)
376 height (1- height))
377 (forward-line -1))
378 (forward-line 1)))
379 (aset bbx 1 height)
380 (aset bbx 3 yoff)
381 (delete-region (point) (point-max))
382 (goto-char (point-min))
383 (while (not (eobp))
384 (end-of-line)
385 (delete-char 1))
386 (setq bitmap-string (buffer-string)))
387 (error nil))
76875dcb
KH
388 (vector dwidth (aref bbx 0) (aref bbx 1) (aref bbx 2) (aref bbx 3)
389 (concat "<" bitmap-string ">")
390 (or relative-compose 'false))))
391
392(defun bdf-get-bitmap (bdfname code)
393 "Return bitmap information of glyph of CODE in `BDF' font file BDFNAME.
394CODE is an encoding number of glyph in the file.
395The value is a list (DWIDTH BBX BITMAP-STRING).
e62e3e6b
KH
396DWIDTH is a pixel width of a glyph.
397BBX is a bounding box of the glyph.
398BITMAP-STRING is a string representing bits by hexadecimal digits."
76875dcb
KH
399 (let* ((info (bdf-get-font-info bdfname))
400 (maxlen (bdf-info-maxlen info))
401 (code-range (bdf-info-code-range info))
402 (offset-vector (bdf-info-offset-vector info)))
403 (bdf-read-bitmap bdfname
404 (aref offset-vector (bdf-compact-code code code-range))
405 maxlen (bdf-info-relative-compose info))))
406
407;;; Interface to ps-mule.el
e62e3e6b
KH
408
409;; Called from ps-mule-init-external-library.
410(defun bdf-generate-prologue ()
411 (or bdf-cache
412 (bdf-initialize))
413 (ps-mule-generate-bitmap-prologue))
414
76875dcb
KH
415;; Called from ps-mule-check-font.
416(defun bdf-check-font (font-spec)
417 (let ((font-name-list (ps-mule-font-spec-name font-spec)))
418 (ps-mule-font-spec-set-name
419 font-spec
420 (if (stringp font-name-list)
421 (bdf-expand-file-name font-name-list)
422 (catch 'tag
423 (dolist (font-name font-name-list)
424 (setq font-name (bdf-expand-file-name font-name))
425 (if font-name
426 (throw 'tag font-name))))))))
427
e62e3e6b 428;; Called from ps-mule-generate-font.
76875dcb
KH
429(defun bdf-generate-font (font-spec)
430 (let ((info (bdf-get-font-info (ps-mule-font-spec-name font-spec))))
431 (ps-mule-font-spec-set-extra
432 font-spec (bdf-info-absolute-path info))
433 (ps-mule-generate-bitmap-font font-spec
434 (bdf-info-size info)
435 (bdf-info-relative-compose info)
436 (bdf-info-baseline-offset info)
437 (bdf-info-font-bounding-box info))))
438
439;; Called from ps-mule-generate-glyph.
440(defun bdf-generate-glyph (font-spec char)
441 (let ((font-name (ps-mule-font-spec-extra font-spec))
442 (code (ps-mule-encode-char char font-spec)))
443 (ps-mule-generate-bitmap-glyph font-spec char code
444 (bdf-get-bitmap font-name code))))
e62e3e6b
KH
445
446(provide 'ps-bdf)
447
6b61353c 448;;; arch-tag: 9b875ba8-565a-4ecf-acaa-30cee732c898
e62e3e6b 449;;; ps-bdf.el ends here