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