HideIfDef mode bug fixes and enhancements. This is #2 of 3 patches based
[bpt/emacs.git] / lisp / ps-bdf.el
CommitLineData
e8af40ee 1;;; ps-bdf.el --- BDF font file handler for ps-print
e62e3e6b 2
ba318903 3;; Copyright (C) 1998-1999, 2001-2014 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.
0c93aa38 73The value is a list of integers in the same format as `current-time'."
e62e3e6b
KH
74 (nth 5 (file-attributes filename)))
75
76(defun bdf-file-newer-than-time (filename mod-time)
77 "Return non-nil if and only if FILENAME is newer than MOD-TIME.
0c93aa38
PE
78MOD-TIME is a modification time as a list of integers in the same
79format as `current-time'."
80 (let ((new-mod-time (bdf-file-mod-time filename)))
81 (time-less-p mod-time new-mod-time)))
e62e3e6b
KH
82
83(defun bdf-find-file (bdfname)
84 "Return a buffer visiting a bdf file BDFNAME.
76875dcb 85BDFNAME must be an absolute file name.
e62e3e6b 86If BDFNAME doesn't exist, return nil."
76875dcb
KH
87 (and (file-readable-p bdfname)
88 (let ((buf (generate-new-buffer " *bdf-work*"))
89 (coding-system-for-read 'no-conversion))
7fdbcd83 90 (with-current-buffer buf
76875dcb
KH
91 (insert-file-contents bdfname)
92 buf))))
e62e3e6b 93
940e5099 94(defvar bdf-cache-file (locate-user-emacs-file "bdfcache.el" ".bdfcache.el")
e62e3e6b
KH
95 "Name of cache file which contains information of `BDF' font files.")
96
97(defvar bdf-cache nil
98 "Cached information of `BDF' font files. It is a list of FONT-INFO.
99FONT-INFO is a list of the following format:
76875dcb 100 (ABSOLUTE-FILE-NAME MOD-TIME SIZE FONT-BOUNDING-BOX
e62e3e6b
KH
101 RELATIVE-COMPOSE BASELINE-OFFSET CODE-RANGE MAXLEN OFFSET-VECTOR)
102See the documentation of the function `bdf-read-font-info' for more detail.")
103
104(defun bdf-read-cache ()
105 "Return a cached information about `BDF' font files from a cache file.
106The variable `bdf-cache-file' holds the cache file name.
107If 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)))
117
118(defun bdf-write-cache ()
119 "Write out cached information of `BDF' font file to a file.
120The variable `bdf-cache-file' holds the cache file name.
64d8e7fd 121The file is written if and only if the file already exists and writable."
e62e3e6b
KH
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)))
127
128(defun bdf-set-cache (font-info)
129 "Cache FONT-INFO as information about one `BDF' font file.
130FONT-INFO is a list of the following format:
76875dcb 131 (ABSOLUTE-FILE-NAME MOD-TIME SIZE FONT-BOUNDING-BOX
e62e3e6b
KH
132 RELATIVE-COMPOSE BASELINE-OFFSET CODE-RANGE MAXLEN OFFSET-VECTOR)
133See 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)))))
138
139(defun bdf-initialize ()
140 "Initialize `bdf' library."
141 (and (bdf-read-cache)
142 (add-hook 'kill-emacs-hook 'bdf-write-cache)))
143
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))))
151
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)))))
156
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)))))
163
164(defun bdf-read-font-info (bdfname)
165 "Read `BDF' font file BDFNAME and return information (FONT-INFO) of the file.
76875dcb 166BDFNAME must be an absolute file name.
e62e3e6b 167FONT-INFO is a list of the following format:
76875dcb 168 (BDFFILE MOD-TIME FONT-BOUNDING-BOX
e62e3e6b
KH
169 RELATIVE-COMPOSE BASELINE-OFFSET CODE-RANGE MAXLEN OFFSET-VECTOR)
170
0c93aa38
PE
171MOD-TIME is last modification time as a list of integers in the
172same format as `current-time'.
e62e3e6b 173
76875dcb
KH
174SIZE is a size of the font on 72 dpi device. This value is got
175from SIZE record of the font.
e62e3e6b
KH
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
64d8e7fd 192MAXLEN is a maximum bytes of one glyph information in the font file.
e62e3e6b
KH
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"
76875dcb 200 (let* ((buf (bdf-find-file bdfname))
e62e3e6b
KH
201 (maxlen 0)
202 (relative-compose 'false)
203 (baseline-offset 0)
204 size
76875dcb 205 dpi
f1180544 206 font-bounding-box
e62e3e6b
KH
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
7fdbcd83 214 (with-current-buffer buf
e62e3e6b
KH
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))))
227
228 (goto-char (point-min))
76875dcb
KH
229 (search-forward "\nFONT ")
230 (if (looking-at "-[^-]*-[^-]*-[^-]*-[^-]*-[^-]*-[^-]*-\\([0-9]+\\)")
4836694e 231 (setq size (string-to-number (match-string 1)))
76875dcb
KH
232 (search-forward "\nSIZE ")
233 (setq size (read (current-buffer)))
20525d8c 234 ;; The following kludgy code is to avoid bugs of several
76875dcb
KH
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))))
e62e3e6b
KH
242
243 (setq default-char (bdf-search-and-read "\nDEFAULT_CHAR" nil))
244
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)))
254
255 (let ((min-code0 256) (min-code1 256) (min-code 65536)
256 (max-code0 0) (max-code1 0) (max-code 0)
64d8e7fd 257 glyph glyph-list code0 code1 code offset)
e62e3e6b
KH
258
259 (while (search-forward "\nSTARTCHAR" nil t)
260 (setq offset (line-beginning-position))
261 (search-forward "\nENCODING")
3509543c
KH
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))))
e62e3e6b
KH
276
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))
283
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)))))
290
291 (kill-buffer buf))
292 (message "Reading %s...done" bdfname)
76875dcb 293 (list bdfname (bdf-file-mod-time bdfname)
e62e3e6b
KH
294 size font-bounding-box relative-compose baseline-offset
295 code-range maxlen offset-vector)))
296
76875dcb
KH
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))
e62e3e6b
KH
306
307(defun bdf-get-font-info (bdfname)
308 "Return information about `BDF' font file BDFNAME.
76875dcb 309BDFNAME must be an absolute file name.
e62e3e6b 310The value FONT-INFO is a list of the following format:
76875dcb 311 (BDFNAME MOD-TIME SIZE FONT-BOUNDING-BOX
e62e3e6b
KH
312 RELATIVE-COMPOSE BASELINE-OFFSET CODE-RANGE MAXLEN OFFSET-VECTOR)
313See 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)
76875dcb 318 (not (file-readable-p bdfname))
e62e3e6b
KH
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))
324
76875dcb 325(defun bdf-read-bitmap (bdfname offset maxlen relative-compose)
64d8e7fd
GM
326 "Read `BDF' font file BDFNAME to get bitmap data at file position OFFSET.
327BDFNAME is an absolute path name of the font file.
e62e3e6b
KH
328MAXLEN specifies how many bytes we should read at least.
329The value is a list of DWIDTH, BBX, and BITMAP-STRING.
330DWIDTH is a pixel width of a glyph.
331BBX is a bounding box of the glyph.
332BITMAP-STRING is a string representing bits by hexadecimal digits."
0240cfba 333 (let* ((coding-system-for-read 'no-conversion)
76875dcb 334 (bbx (bdf-info-font-bounding-box (bdf-get-font-info bdfname)))
0240cfba
GM
335 (dwidth (elt bbx 0))
336 (bitmap-string "")
337 height yoff)
e62e3e6b
KH
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)))
76875dcb
KH
344 (if (= dwidth 0)
345 (setq dwidth 0.1))
e62e3e6b
KH
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)))
64d8e7fd
GM
350 height (aref bbx 1)
351 yoff (aref bbx 3))
e62e3e6b
KH
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))
76875dcb
KH
377 (vector dwidth (aref bbx 0) (aref bbx 1) (aref bbx 2) (aref bbx 3)
378 (concat "<" bitmap-string ">")
379 (or relative-compose 'false))))
380
381(defun bdf-get-bitmap (bdfname code)
382 "Return bitmap information of glyph of CODE in `BDF' font file BDFNAME.
383CODE is an encoding number of glyph in the file.
384The value is a list (DWIDTH BBX BITMAP-STRING).
e62e3e6b
KH
385DWIDTH is a pixel width of a glyph.
386BBX is a bounding box of the glyph.
387BITMAP-STRING is a string representing bits by hexadecimal digits."
76875dcb
KH
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))))
395
396;;; Interface to ps-mule.el
e62e3e6b
KH
397
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))
403
76875dcb
KH
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))))))))
416
e62e3e6b 417;; Called from ps-mule-generate-font.
76875dcb
KH
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))))
427
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))))
e62e3e6b
KH
434
435(provide 'ps-bdf)
436
437;;; ps-bdf.el ends here