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