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