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