aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorKenichi Handa1998-12-15 06:39:39 +0000
committerKenichi Handa1998-12-15 06:39:39 +0000
commite62e3e6b5e68bb41e9783c1afad859af6fe816d8 (patch)
treef93b75508395f7ddab81167246507e6d9d956f6a
parent7e6acc2d42c2ce6148546d6b747145d5f69b2f57 (diff)
downloademacs-e62e3e6b5e68bb41e9783c1afad859af6fe816d8.tar.gz
emacs-e62e3e6b5e68bb41e9783c1afad859af6fe816d8.zip
File name changed from bdf.el. Provide ps-bdf
instead of bdf. Require ps-mule instead of ps-print. (bdf-directory-list): Add autoload cookie. Programming uniformization and little code improvement. (bdf-search-and-read): New fun. (bdf-write-cache, bdf-initialize, bdf-info-absolute-path) (bdf-info-mod-time, bdf-info-size, bdf-info-font-bounding-box) (bdf-info-relative-compose, bdf-info-baseline-offset) (bdf-info-code-range, bdf-info-maxlen, bdf-info-offset-vector) (bdf-read-bitmap, bdf-get-bitmaps): Programming uniformization. (bdf-expand-file-name, bdf-file-newer-than-time, bdf-find-file) (bdf-read-cache, bdf-read-font-info, bdf-generate-glyphs): Little code improvement.
-rw-r--r--lisp/ps-bdf.el413
1 files changed, 413 insertions, 0 deletions
diff --git a/lisp/ps-bdf.el b/lisp/ps-bdf.el
index e69de29bb2d..28ef5ab019a 100644
--- a/lisp/ps-bdf.el
+++ b/lisp/ps-bdf.el
@@ -0,0 +1,413 @@
1;;; ps-bdf.el --- BDF font file handler for ps-print.
2
3;; Copyright (C) 1998 Electrotechnical Laboratory, JAPAN.
4;; Licensed to the Free Software Foundation.
5
6;; Keywords: BDF, font, PostScript
7;; Maintainer: Kenichi Handa <handa@etl.go.jp>
8;; Time-stamp: <98/11/26 15:09:23 kenichi>
9
10;; This file is part of GNU Emacs.
11
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.
16
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.
21
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.
26
27;;; Commentary:
28
29;; Functions for getting bitmap information from X's BDF font file are
30;; provided.
31
32;;; Code:
33
34(eval-when-compile (require 'ps-mule))
35
36;;;###autoload
37(defvar bdf-directory-list
38 nil
39 "*List of directories to search for `BDF' font files.")
40
41(defun bdf-expand-file-name (bdfname)
42 "Return an abosolute path name of a `BDF' font file BDFNAME.
43It searches directories listed in the variable `bdf-directory-list'
44for BDFNAME."
45 (if (file-name-absolute-p bdfname)
46 (and (file-readable-p bdfname)
47 bdfname)
48 (let ((dir-list bdf-directory-list)
49 dir)
50 (while (and dir-list
51 (progn
52 (setq dir (expand-file-name bdfname (car dir-list)))
53 (not (file-readable-p dir))))
54 (setq dir nil
55 dir-list (cdr dir-list)))
56 dir)))
57
58(defsubst bdf-file-mod-time (filename)
59 "Return modification time of FILENAME.
60The value is a list of two integers, the first integer has high-order
6116 bits, the second has low 16 bits."
62 (nth 5 (file-attributes filename)))
63
64(defun bdf-file-newer-than-time (filename mod-time)
65 "Return non-nil if and only if FILENAME is newer than MOD-TIME.
66MOD-TIME is a modification time as a list of two integers, the first
67integer has high-order 16 bits, the second has low 16 bits."
68 (let ((file-name (bdf-expand-file-name filename)))
69 (and file-name
70 (let* ((new-mod-time (bdf-file-mod-time file-name))
71 (new-time (car new-mod-time))
72 (time (car mod-time)))
73 (or (> new-time time)
74 (and (= new-time time)
75 (> (nth 1 new-mod-time) (nth 1 mod-time))))))))
76
77(defun bdf-find-file (bdfname)
78 "Return a buffer visiting a bdf file BDFNAME.
79If BDFNAME is not an absolute path, directories listed in
80`bdf-directory-list' is searched.
81If BDFNAME doesn't exist, return nil."
82 (let ((file-name (bdf-expand-file-name bdfname)))
83 (and file-name
84 (let ((buf (generate-new-buffer " *bdf-work*"))
85 (coding-system-for-read 'no-conversion))
86 (save-excursion
87 (set-buffer buf)
88 (insert-file-contents file-name)
89 buf)))))
90
91(defvar bdf-cache-file "~/.bdfcache.el"
92 "Name of cache file which contains information of `BDF' font files.")
93
94(defvar bdf-cache nil
95 "Cached information of `BDF' font files. It is a list of FONT-INFO.
96FONT-INFO is a list of the following format:
97 (BDFFILE ABSOLUTE-PATH MOD-TIME SIZE FONT-BOUNDING-BOX
98 RELATIVE-COMPOSE BASELINE-OFFSET CODE-RANGE MAXLEN OFFSET-VECTOR)
99See the documentation of the function `bdf-read-font-info' for more detail.")
100
101(defun bdf-read-cache ()
102 "Return a cached information about `BDF' font files from a cache file.
103The variable `bdf-cache-file' holds the cache file name.
104If the cache file is not readable, this return nil."
105 (setq bdf-cache nil)
106 (condition-case nil
107 (and (file-readable-p bdf-cache-file)
108 (progn
109 (load-file bdf-cache-file)
110 (if (listp bdf-cache)
111 bdf-cache
112 (setq bdf-cache nil))))
113 (error nil)))
114
115(defun bdf-write-cache ()
116 "Write out cached information of `BDF' font file to a file.
117The variable `bdf-cache-file' holds the cache file name.
118The file is written if and only if the file alreay exists and writable."
119 (and bdf-cache
120 (file-exists-p bdf-cache-file)
121 (file-writable-p bdf-cache-file)
122 (write-region (format "(setq bdf-cache '%S)\n" bdf-cache)
123 nil bdf-cache-file)))
124
125(defun bdf-set-cache (font-info)
126 "Cache FONT-INFO as information about one `BDF' font file.
127FONT-INFO is a list of the following format:
128 (BDFFILE ABSOLUTE-PATH MOD-TIME SIZE FONT-BOUNDING-BOX
129 RELATIVE-COMPOSE BASELINE-OFFSET CODE-RANGE MAXLEN OFFSET-VECTOR)
130See the documentation of the function `bdf-read-font-info' for more detail."
131 (let ((slot (assoc (car font-info) bdf-cache)))
132 (if slot
133 (setcdr slot (cdr font-info))
134 (setq bdf-cache (cons font-info bdf-cache)))))
135
136(defun bdf-initialize ()
137 "Initialize `bdf' library."
138 (and (bdf-read-cache)
139 (add-hook 'kill-emacs-hook 'bdf-write-cache)))
140
141(defun bdf-compact-code (code code-range)
142 (if (or (< code (aref code-range 4))
143 (> code (aref code-range 5)))
144 (setq code (aref code-range 6)))
145 (+ (* (- (lsh code -8) (aref code-range 0))
146 (1+ (- (aref code-range 3) (aref code-range 2))))
147 (- (logand code 255) (aref code-range 2))))
148
149(defun bdf-expand-code (code code-range)
150 (let ((code0-range (1+ (- (aref code-range 3) (aref code-range 2)))))
151 (+ (* (+ (/ code code0-range) (aref code-range 0)) 256)
152 (+ (% code code0-range) (aref code-range 2)))))
153
154(defun bdf-search-and-read (match limit)
155 (goto-char (point-min))
156 (and (search-forward match limit t)
157 (progn
158 (goto-char (match-end 0))
159 (read (current-buffer)))))
160
161(defun bdf-read-font-info (bdfname)
162 "Read `BDF' font file BDFNAME and return information (FONT-INFO) of the file.
163FONT-INFO is a list of the following format:
164 (BDFFILE ABSOLUTE-PATH MOD-TIME FONT-BOUNDING-BOX
165 RELATIVE-COMPOSE BASELINE-OFFSET CODE-RANGE MAXLEN OFFSET-VECTOR)
166
167BDFFILE is a name of a font file (excluding directory part).
168
169ABSOLUTE-PATH is an absolute path of the font file.
170
171MOD-TIME is last modification time as a list of two integers, the
172first integer has high-order 16 bits, the second has low 16 bits.
173
174SIZE is a size of the font. This value is got from SIZE record of the
175font.
176
177FONT-BOUNDING-BOX is the font bounding box as a list of four integers,
178BBX-WIDTH, BBX-HEIGHT, BBX-XOFF, and BBX-YOFF.
179
180RELATIVE-COMPOSE is an integer value of the font's property
181`_MULE_RELATIVE_COMPOSE'. If the font doesn't have this property, the
182value is 0.
183
184BASELINE-OFFSET is an integer value of the font's property
185`_MULE_BASELINE_OFFSET'. If the font doesn't have this property, the
186value is 0.
187
188CODE-RANGE is a vector of minimum 1st byte, maximum 1st byte, minimum
1892nd byte, maximum 2nd byte, minimum code, maximum code, and default
190code. For 1-byte fonts, the first two elements are 0.
191
192MAXLEN is a maximum bytes of one glyph informaion in the font file.
193
194OFFSET-VECTOR is a vector of a file position which starts bitmap data
195of the glyph in the font file.
196
197Nth element of OFFSET-VECTOR is a file position for the glyph of code
198CODE, where N and CODE are in the following relation:
199 (bdf-compact-code CODE) => N, (bdf-expand-code N) => CODE"
200 (let* ((absolute-path (bdf-expand-file-name bdfname))
201 (buf (and absolute-path (bdf-find-file absolute-path)))
202 (maxlen 0)
203 (relative-compose 'false)
204 (baseline-offset 0)
205 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 (save-excursion
215 (set-buffer buf)
216 (goto-char (point-min))
217 (search-forward "\nFONTBOUNDINGBOX")
218 (setq font-bounding-box
219 (vector (read (current-buffer)) (read (current-buffer))
220 (read (current-buffer)) (read (current-buffer))))
221 ;; The following kludgy code is to avoid bugs of fonts
222 ;; jiskan16.bdf and jiskan24.bdf distributed with X.
223 ;; They contain wrong FONTBOUNDINGBOX.
224 (and (> (aref font-bounding-box 3) 0)
225 (string-match "jiskan\\(16\\|24\\)" bdfname)
226 (aset font-bounding-box 3
227 (- (aref font-bounding-box 3))))
228
229 (goto-char (point-min))
230 (search-forward "\nSIZE ")
231 (setq size (read (current-buffer)))
232 ;; The following kludgy code is t avoid bugs of several
233 ;; fonts which have wrong SIZE record.
234 (and (<= size (/ (aref font-bounding-box 1) 2))
235 (setq size (aref font-bounding-box 1)))
236
237 (setq default-char (bdf-search-and-read "\nDEFAULT_CHAR" nil))
238
239 (search-forward "\nSTARTCHAR")
240 (forward-line -1)
241 (let ((limit (point)))
242 (setq relative-compose
243 (or (bdf-search-and-read "\n_MULE_RELATIVE_COMPOSE" limit)
244 'false)
245 baseline-offset
246 (or (bdf-search-and-read "\n_MULE_BASELINE_OFFSET" limit)
247 0)))
248
249 (let ((min-code0 256) (min-code1 256) (min-code 65536)
250 (max-code0 0) (max-code1 0) (max-code 0)
251 glyph glyph-list code0 code1 code bbx offset)
252
253 (while (search-forward "\nSTARTCHAR" nil t)
254 (setq offset (line-beginning-position))
255 (search-forward "\nENCODING")
256 (setq code (read (current-buffer))
257 code0 (lsh code -8)
258 code1 (logand code 255)
259 min-code (min min-code code)
260 max-code (max max-code code)
261 min-code0 (min min-code0 code0)
262 max-code0 (max max-code0 code0)
263 min-code1 (min min-code1 code1)
264 max-code1 (max max-code1 code1))
265 (search-forward "ENDCHAR")
266 (setq maxlen (max maxlen (- (point) offset))
267 glyph-list (cons (cons code offset) glyph-list)))
268
269 (setq code-range
270 (vector min-code0 max-code0 min-code1 max-code1
271 min-code max-code (or default-char min-code))
272 offset-vector
273 (make-vector (1+ (bdf-compact-code max-code code-range))
274 nil))
275
276 (while glyph-list
277 (setq glyph (car glyph-list)
278 glyph-list (cdr glyph-list))
279 (aset offset-vector
280 (bdf-compact-code (car glyph) code-range)
281 (cdr glyph)))))
282
283 (kill-buffer buf))
284 (message "Reading %s...done" bdfname)
285 (list bdfname absolute-path (bdf-file-mod-time absolute-path)
286 size font-bounding-box relative-compose baseline-offset
287 code-range maxlen offset-vector)))
288
289(defsubst bdf-info-absolute-path (font-info) (nth 1 font-info))
290(defsubst bdf-info-mod-time (font-info) (nth 2 font-info))
291(defsubst bdf-info-size (font-info) (nth 3 font-info))
292(defsubst bdf-info-font-bounding-box (font-info) (nth 4 font-info))
293(defsubst bdf-info-relative-compose (font-info) (nth 5 font-info))
294(defsubst bdf-info-baseline-offset (font-info) (nth 6 font-info))
295(defsubst bdf-info-code-range (font-info) (nth 7 font-info))
296(defsubst bdf-info-maxlen (font-info) (nth 8 font-info))
297(defsubst bdf-info-offset-vector (font-info) (nth 9 font-info))
298
299(defun bdf-get-font-info (bdfname)
300 "Return information about `BDF' font file BDFNAME.
301The value FONT-INFO is a list of the following format:
302 (BDFFILE ABSOLUTE-PATH MOD-TIME SIZE FONT-BOUNDING-BOX
303 RELATIVE-COMPOSE BASELINE-OFFSET CODE-RANGE MAXLEN OFFSET-VECTOR)
304See the documentation of the function `bdf-read-font-info' for more detail."
305 (or bdf-cache
306 (bdf-read-cache))
307 (let ((font-info (assoc bdfname bdf-cache)))
308 (if (or (not font-info)
309 (not (file-readable-p (bdf-info-absolute-path font-info)))
310 (bdf-file-newer-than-time bdfname (bdf-info-mod-time font-info)))
311 (progn
312 (setq font-info (bdf-read-font-info bdfname))
313 (bdf-set-cache font-info)))
314 font-info))
315
316(defun bdf-read-bitmap (bdfname offset maxlen)
317 "Read `BDF' font file BDFNAME to get bitmap data at file poistion OFFSET.
318BDFNAME is an abosolute path name of the font file.
319MAXLEN specifies how many bytes we should read at least.
320The value is a list of DWIDTH, BBX, and BITMAP-STRING.
321DWIDTH is a pixel width of a glyph.
322BBX is a bounding box of the glyph.
323BITMAP-STRING is a string representing bits by hexadecimal digits."
324 (let ((coding-system-for-read 'no-conversion)
325 dwidth bbx height yoff bitmap-string)
326 (condition-case nil
327 (with-temp-buffer
328 (insert-file-contents bdfname nil offset (+ offset maxlen))
329 (goto-char (point-min))
330 (search-forward "\nDWIDTH")
331 (setq dwidth (read (current-buffer)))
332 (goto-char (point-min))
333 (search-forward "\nBBX")
334 (setq bbx (vector (read (current-buffer)) (read (current-buffer))
335 (read (current-buffer)) (read (current-buffer)))
336 height (aref bbx 1) yoff (aref bbx 3))
337 (search-forward "\nBITMAP")
338 (forward-line 1)
339 (delete-region (point-min) (point))
340 (and (looking-at "\\(0+\n\\)+")
341 (progn
342 (setq height (- height (count-lines (point) (match-end 0))))
343 (delete-region (point) (match-end 0))))
344 (or (looking-at "ENDCHAR")
345 (progn
346 (search-forward "ENDCHAR" nil 'move)
347 (forward-line -1)
348 (while (looking-at "0+$")
349 (setq yoff (1+ yoff)
350 height (1- height))
351 (forward-line -1))
352 (forward-line 1)))
353 (aset bbx 1 height)
354 (aset bbx 3 yoff)
355 (delete-region (point) (point-max))
356 (goto-char (point-min))
357 (while (not (eobp))
358 (end-of-line)
359 (delete-char 1))
360 (setq bitmap-string (buffer-string)))
361 (error nil))
362 (list dwidth bbx bitmap-string)))
363
364(defun bdf-get-bitmaps (bdfname codes)
365 "Return bitmap information of glyphs of CODES in `BDF' font file BDFNAME.
366CODES is a list of encoding number of glyphs in the file.
367The value is a list of CODE, DWIDTH, BBX, and BITMAP-STRING.
368DWIDTH is a pixel width of a glyph.
369BBX is a bounding box of the glyph.
370BITMAP-STRING is a string representing bits by hexadecimal digits."
371 (let* ((font-info (bdf-get-font-info bdfname))
372 (absolute-path (bdf-info-absolute-path font-info))
373 (font-bounding-box (bdf-info-font-bounding-box font-info))
374 (maxlen (bdf-info-maxlen font-info))
375 (code-range (bdf-info-code-range font-info))
376 (offset-vector (bdf-info-offset-vector font-info)))
377 (mapcar '(lambda (x)
378 (cons x (bdf-read-bitmap
379 absolute-path
380 (aref offset-vector (bdf-compact-code x code-range))
381 maxlen)))
382 codes)))
383
384;;; Interface to ps-print.el
385
386;; Called from ps-mule-init-external-library.
387(defun bdf-generate-prologue ()
388 (or bdf-cache
389 (bdf-initialize))
390 (ps-mule-generate-bitmap-prologue))
391
392;; Called from ps-mule-generate-font.
393(defun bdf-generate-font (charset font-spec)
394 (let* ((font-name (ps-mule-font-spec-name font-spec))
395 (font-info (bdf-get-font-info font-name)))
396 (ps-mule-generate-bitmap-font font-name
397 (ps-mule-font-spec-bytes font-spec)
398 (charset-width charset)
399 (bdf-info-size font-info)
400 (bdf-info-relative-compose font-info)
401 (bdf-info-baseline-offset font-info)
402 (bdf-info-font-bounding-box font-info))))
403
404;; Called from ps-mule-generate-glyphs.
405(defun bdf-generate-glyphs (font-spec code-list bytes)
406 (let ((font-name (ps-mule-font-spec-name font-spec)))
407 (mapcar '(lambda (x)
408 (apply 'ps-mule-generate-bitmap-glyph font-name x))
409 (bdf-get-bitmaps font-name code-list))))
410
411(provide 'ps-bdf)
412
413;;; ps-bdf.el ends here