Merge changes from emacs-23 branch
[bpt/emacs.git] / lisp / ps-bdf.el
CommitLineData
e8af40ee 1;;; ps-bdf.el --- BDF font file handler for ps-print
e62e3e6b 2
acaf905b 3;; Copyright (C) 1998-1999, 2001-2012 Free Software Foundation, Inc.
409cc4a3 4;; Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007,
5df4f04c 5;; 2008, 2009, 2010, 2011
ce03bf76
KH
6;; National Institute of Advanced Industrial Science and Technology (AIST)
7;; Registration Number H14PRO021
76875dcb
KH
8;; Copyright (C) 2003
9;; National Institute of Advanced Industrial Science and Technology (AIST)
10;; Registration Number H13PRO009
e62e3e6b 11
c3fba29d
GM
12;; Author: Kenichi Handa <handa@m17n.org>
13;; (according to ack.texi)
3ad114e5 14;; Keywords: wp, BDF, font, PostScript
bd78fa1d 15;; Package: ps-print
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
befe199d 39(require 'ps-mule)
e62e3e6b
KH
40
41;;;###autoload
6530de7d 42(defcustom bdf-directory-list
7a1e1973 43 (if (memq system-type '(ms-dos windows-nt))
0a51cf3d 44 (list (expand-file-name "fonts/bdf" installation-directory))
3e3cf0a7 45 '("/usr/local/share/emacs/fonts/bdf"))
6530de7d
VJL
46 "List of directories to search for `BDF' font files.
47The 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)
e62e3e6b 51
3f4f2289
EZ
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.
7a1e1973 54(and (memq system-type '(ms-dos windows-nt))
0a51cf3d
EZ
55 (setq bdf-directory-list
56 (list (expand-file-name "fonts/bdf" installation-directory))))
57
e62e3e6b 58(defun bdf-expand-file-name (bdfname)
64d8e7fd 59 "Return an absolute path name of a `BDF' font file BDFNAME.
e62e3e6b
KH
60It searches directories listed in the variable `bdf-directory-list'
61for BDFNAME."
62 (if (file-name-absolute-p bdfname)
63 (and (file-readable-p bdfname)
64 bdfname)
76875dcb
KH
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)))))))
e62e3e6b
KH
70
71(defsubst bdf-file-mod-time (filename)
72 "Return modification time of FILENAME.
73The value is a list of two integers, the first integer has high-order
7416 bits, the second has low 16 bits."
75 (nth 5 (file-attributes filename)))
76
77(defun bdf-file-newer-than-time (filename mod-time)
78 "Return non-nil if and only if FILENAME is newer than MOD-TIME.
79MOD-TIME is a modification time as a list of two integers, the first
80integer has high-order 16 bits, the second has low 16 bits."
76875dcb
KH
81 (let* ((new-mod-time (bdf-file-mod-time filename))
82 (new-time (car new-mod-time))
83 (time (car mod-time)))
84 (or (> new-time time)
85 (and (= new-time time)
86 (> (nth 1 new-mod-time) (nth 1 mod-time))))))
e62e3e6b
KH
87
88(defun bdf-find-file (bdfname)
89 "Return a buffer visiting a bdf file BDFNAME.
76875dcb 90BDFNAME must be an absolute file name.
e62e3e6b 91If BDFNAME doesn't exist, return nil."
76875dcb
KH
92 (and (file-readable-p bdfname)
93 (let ((buf (generate-new-buffer " *bdf-work*"))
94 (coding-system-for-read 'no-conversion))
7fdbcd83 95 (with-current-buffer buf
76875dcb
KH
96 (insert-file-contents bdfname)
97 buf))))
e62e3e6b 98
a5f01960
EZ
99(defvar bdf-cache-file (if (eq system-type 'ms-dos)
100 ;; convert-standard-filename doesn't
101 ;; guarantee that the .el extension will be
102 ;; preserved.
103 "~/_bdfcache.el"
104 (convert-standard-filename "~/.bdfcache.el"))
e62e3e6b
KH
105 "Name of cache file which contains information of `BDF' font files.")
106
107(defvar bdf-cache nil
108 "Cached information of `BDF' font files. It is a list of FONT-INFO.
109FONT-INFO is a list of the following format:
76875dcb 110 (ABSOLUTE-FILE-NAME MOD-TIME SIZE FONT-BOUNDING-BOX
e62e3e6b
KH
111 RELATIVE-COMPOSE BASELINE-OFFSET CODE-RANGE MAXLEN OFFSET-VECTOR)
112See the documentation of the function `bdf-read-font-info' for more detail.")
113
114(defun bdf-read-cache ()
115 "Return a cached information about `BDF' font files from a cache file.
116The variable `bdf-cache-file' holds the cache file name.
117If the cache file is not readable, this return nil."
118 (setq bdf-cache nil)
119 (condition-case nil
120 (and (file-readable-p bdf-cache-file)
121 (progn
122 (load-file bdf-cache-file)
123 (if (listp bdf-cache)
124 bdf-cache
125 (setq bdf-cache nil))))
126 (error nil)))
127
128(defun bdf-write-cache ()
129 "Write out cached information of `BDF' font file to a file.
130The variable `bdf-cache-file' holds the cache file name.
64d8e7fd 131The file is written if and only if the file already exists and writable."
e62e3e6b
KH
132 (and bdf-cache
133 (file-exists-p bdf-cache-file)
134 (file-writable-p bdf-cache-file)
135 (write-region (format "(setq bdf-cache '%S)\n" bdf-cache)
136 nil bdf-cache-file)))
137
138(defun bdf-set-cache (font-info)
139 "Cache FONT-INFO as information about one `BDF' font file.
140FONT-INFO is a list of the following format:
76875dcb 141 (ABSOLUTE-FILE-NAME MOD-TIME SIZE FONT-BOUNDING-BOX
e62e3e6b
KH
142 RELATIVE-COMPOSE BASELINE-OFFSET CODE-RANGE MAXLEN OFFSET-VECTOR)
143See the documentation of the function `bdf-read-font-info' for more detail."
144 (let ((slot (assoc (car font-info) bdf-cache)))
145 (if slot
146 (setcdr slot (cdr font-info))
147 (setq bdf-cache (cons font-info bdf-cache)))))
148
149(defun bdf-initialize ()
150 "Initialize `bdf' library."
151 (and (bdf-read-cache)
152 (add-hook 'kill-emacs-hook 'bdf-write-cache)))
153
154(defun bdf-compact-code (code code-range)
155 (if (or (< code (aref code-range 4))
156 (> code (aref code-range 5)))
157 (setq code (aref code-range 6)))
158 (+ (* (- (lsh code -8) (aref code-range 0))
159 (1+ (- (aref code-range 3) (aref code-range 2))))
160 (- (logand code 255) (aref code-range 2))))
161
162(defun bdf-expand-code (code code-range)
163 (let ((code0-range (1+ (- (aref code-range 3) (aref code-range 2)))))
164 (+ (* (+ (/ code code0-range) (aref code-range 0)) 256)
165 (+ (% code code0-range) (aref code-range 2)))))
166
167(defun bdf-search-and-read (match limit)
168 (goto-char (point-min))
169 (and (search-forward match limit t)
170 (progn
171 (goto-char (match-end 0))
172 (read (current-buffer)))))
173
174(defun bdf-read-font-info (bdfname)
175 "Read `BDF' font file BDFNAME and return information (FONT-INFO) of the file.
76875dcb 176BDFNAME must be an absolute file name.
e62e3e6b 177FONT-INFO is a list of the following format:
76875dcb 178 (BDFFILE MOD-TIME FONT-BOUNDING-BOX
e62e3e6b
KH
179 RELATIVE-COMPOSE BASELINE-OFFSET CODE-RANGE MAXLEN OFFSET-VECTOR)
180
e62e3e6b
KH
181MOD-TIME is last modification time as a list of two integers, the
182first integer has high-order 16 bits, the second has low 16 bits.
183
76875dcb
KH
184SIZE is a size of the font on 72 dpi device. This value is got
185from SIZE record of the font.
e62e3e6b
KH
186
187FONT-BOUNDING-BOX is the font bounding box as a list of four integers,
188BBX-WIDTH, BBX-HEIGHT, BBX-XOFF, and BBX-YOFF.
189
190RELATIVE-COMPOSE is an integer value of the font's property
191`_MULE_RELATIVE_COMPOSE'. If the font doesn't have this property, the
192value is 0.
193
194BASELINE-OFFSET is an integer value of the font's property
195`_MULE_BASELINE_OFFSET'. If the font doesn't have this property, the
196value is 0.
197
198CODE-RANGE is a vector of minimum 1st byte, maximum 1st byte, minimum
1992nd byte, maximum 2nd byte, minimum code, maximum code, and default
200code. For 1-byte fonts, the first two elements are 0.
201
64d8e7fd 202MAXLEN is a maximum bytes of one glyph information in the font file.
e62e3e6b
KH
203
204OFFSET-VECTOR is a vector of a file position which starts bitmap data
205of the glyph in the font file.
206
207Nth element of OFFSET-VECTOR is a file position for the glyph of code
208CODE, where N and CODE are in the following relation:
209 (bdf-compact-code CODE) => N, (bdf-expand-code N) => CODE"
76875dcb 210 (let* ((buf (bdf-find-file bdfname))
e62e3e6b
KH
211 (maxlen 0)
212 (relative-compose 'false)
213 (baseline-offset 0)
214 size
76875dcb 215 dpi
f1180544 216 font-bounding-box
e62e3e6b
KH
217 default-char
218 code-range
219 offset-vector)
220 (if buf
221 (message "Reading %s..." bdfname)
222 (error "BDF file %s doesn't exist" bdfname))
223 (unwind-protect
7fdbcd83 224 (with-current-buffer buf
e62e3e6b
KH
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
447;;; ps-bdf.el ends here