view lisp/ps-bdf.el @ 96044:c1ef445563bb

2008-06-17 Carsten Dominik <dominik@science.uva.nl> * org-colview.el (org-columns-next-allowed-value): Bug fix. * org-colview-xemacs.el (org-columns-next-allowed-value): Bug fix. * org-agenda.el (org-agenda-get-closed): Get the end time into the agenda prefix as well. * org-publish.el (org-publish-org-index): Make a properly indented list. * org.el (org-calendar-agenda-action-key): New option. (org-get-cursor-date): New function. (org-mark-entry-for-agenda-action): New command. (org-overriding-default-time): New variable. (org-read-date): Respect `org-overriding-default-time'. * org-remember.el (org-remember-apply-template): Respect the ovverriding default time. * org-agenda.el (org-agenda-action-marker): New variable. (org-agenda-action): New command. (org-agenda-do-action): New function. 2008-06-17 Carsten Dominik <dominik@science.uva.nl> * org.el (org-schedule, org-deadline): Protect scheduled and deadline tasks against changes that accidently remove the repeater. Also show a message with the new date when done. 2008-06-17 Carsten Dominik <dominik@science.uva.nl> * org.el (org-beginning-of-line): Cater for the case when there are tags but no headline text. (org-align-tags-here): Convert to tabs only when indent-tabs-mode it set. 2008-06-17 Carsten Dominik <dominik@science.uva.nl> * org-mhe.el (org-mhe-get-message-folder-from-index): Make sure the return value is nil instead of "nil" when there is no match. * org-exp.el (org-insert-centered): Use fill-column instead of 80. (org-export-as-ascii): Use string-width to measure the width of the heading. * org.el (org-diary-to-ical-string): No longer kill buffer FROMBUF, this is now done by the caller. * org-exp.el (org-print-icalendar-entries): Move the call to `org-diary-to-ical-string' out of the loop, and kill the buffer afterwords. * org-remember.el (org-remember-visit-immediately): Position cursor after moving to the note. (org-remember-apply-template): Use a text property to record the cursor position. (org-remember-handler): Align tags after pasting the note. 2008-06-17 Carsten Dominik <dominik@science.uva.nl> * org-bbdb.el (org-bbdb-follow-anniversary-link): New function. * org-agenda.el (org-agenda-open-link): If there is an org-bbdb-name property in the current line, jump to that bbdb entry. * org-bbdb.el (org-bbdb-anniversaries): Add the bbdb-name as a text property, so that the agenda knows where this entry comes from. * org-agenda.el (org-agenda-clock-in): Fixed bug in the interaction between clocking-in from the agenda, and automatic task state switching. * org-macs.el (org-with-point-at): Bug fix in macro defintion. * org.el (org-beginning-of-line, org-end-of-line): Make sure the zmacs-region stays after this command in XEmacs. 2008-06-17 Carsten Dominik <dominik@science.uva.nl> * org.el (org-scan-tags): Allow new values for ACTION parameter. * org-remember.el (org-remember-templates): Fix bug in customization type definition. * org.el (org-map-entries): New function. 2008-06-17 Carsten Dominik <dominik@science.uva.nl> * org-agenda.el (org-agenda-skip-comment-trees): New option. (org-agenda-skip): Respect `org-agenda-skip-comment-trees'. 2008-06-17 Carsten Dominik <dominik@science.uva.nl> * org-remember.el (org-jump-to-target-location): New variable. (org-remember-apply-template): Set `org-remember-apply-template' if requested by template. (org-remember-handler): Start an idle timer to jump to remember location. * org-exp.el (org-get-current-options): Add the FILETAGS setting. * org.el (org-set-regexps-and-options): Fix bug with parsing of file tags. (org-get-tags-at): Add the content of `org-file-tags'. * org-exp.el (org-export-handle-comments): Fix bug with several comment lines after each other. (org-number-to-roman, org-number-to-counter): New functions. (org-export-section-number-format): New option. 2008-06-17 Carsten Dominik <dominik@science.uva.nl> * org-exp.el (org-export-protect-examples): Catch the case of a missing end_example line. * org.el (org-set-regexps-and-options): Set `org-file-properties' and `org-file-tags' to nil. * org-colview.el (org-columns-next-allowed-value): Handle next argument NTH to directly select a value. * org-colview-xemacs.el (org-columns-next-allowed-value): Handle next argument NTH to directly select a value. 2008-06-17 Carsten Dominik <dominik@science.uva.nl> * org-agenda.el (org-agenda-scheduled-leaders): Fix docstring. 2008-06-17 Carsten Dominik <dominik@science.uva.nl> * org.el (org-columns-ellipses): New option. 2008-06-17 Carsten Dominik <dominik@science.uva.nl> * org-colview.el (org-columns-add-ellipses): New function. (org-columns-compact-links): New function. (org-columns-cleanup-item): Call `org-columns-compact-links'. (org-columns-display-here): Call `org-agenda-columns-cleanup-item' when in agenda. (org-columns-edit-value): Fixed bug with editing values from agenda column view. (org-columns-redo): Also redo the agenda itself. 2008-06-17 Carsten Dominik <dominik@science.uva.nl> * org-agenda.el (org-agenda-columns-remove-prefix-from-item): New option. * org-colview.el (org-agenda-columns-cleanup-item): New function. * org-exp.el (org-export-ascii-preprocess): Renamed from `org-export-ascii-clean-string'. (org-export-kill-licensed-text) (org-export-define-heading-targets) (org-export-handle-invisible-targets) (org-export-target-internal-links) (org-export-remove-or-extract-drawers) (org-export-remove-archived-trees) (org-export-protect-quoted-subtrees) (org-export-protect-verbatim, org-export-protect-examples) (org-export-select-backend-specific-text) (org-export-mark-blockquote-and-verse) (org-export-remove-comment-blocks-and-subtrees) (org-export-handle-comments, org-export-mark-radio-links) (org-export-remove-special-table-lines) (org-export-normalize-links) (org-export-concatenate-multiline-links) (org-export-concatenate-multiline-emphasis): New functions, obtained from spliiting the export preprocessor. * org-table.el (org-table-recalculate): Improve error message if the row number is invalid. 2008-06-17 Carsten Dominik <dominik@science.uva.nl> * org-archive.el (org-archive-save-context-info): Fix bugs in customization setup and docstring. * org-exp.el (org-export-html-style): Changed the size of in the <pre> element to 90%. 2008-06-17 Carsten Dominik <dominik@science.uva.nl> * org.el (org-find-src-example-start): Function removed. (org-edit-src-find-region-and-lang): New function. 2008-06-17 Carsten Dominik <dominik@science.uva.nl> * org.el (org-edit-src-exit): New function. (org-exit-edit-mode): New minor mode. * org-exp.el (org-export-preprocess-string): Fix bug with removing comment-like lines from protected examples. * org.el (org-edit-src-example, org-find-src-example-start) (org-protect-source-example, org-edit-special): New functions. 2008-06-17 Carsten Dominik <dominik@science.uva.nl> * org-publish.el (org-publish-project-alist): Fix typo in docstring. (org-publish-project-alist): Handle :index-title property. 2008-06-17 Carsten Dominik <dominik@science.uva.nl> * org-export-latex.el (org-export-as-latex): Make sure region bounds are correct. Parse subtree properties relating to export. * org-exp.el (org-export-add-options-to-plist): New function. (org-infile-export-plist): Use `org-export-add-options-to-plist'. 2008-06-17 Carsten Dominik <dominik@science.uva.nl> * org.el (org-default-properties): Add EXPORT_FILE_NAME and EXPORT_TITLE. * org-exp.el (org-export-get-title-from-subtree) (org-export-as-ascii, org-export-as-html): Make sure the original region-beginning and region-end are used, even after moving point. (org-export-get-title-from-subtree): Also try the EXPORT_TITLE property. * org-remember.el (org-remember-last-stored-marker): New variable. (org-remember-goto-last-stored): Use `org-goto-marker-or-bmk'. (org-remember-handler): Also use marker to remember last-stored position. * org.el (org-goto-marker-or-bmk): New function. 2008-06-17 Carsten Dominik <dominik@science.uva.nl> * org.el (org-file-properties): Renamed from `org-local-properties'. (org-scan-tags): Take file tags into account. (org-tags-match-list-sublevels): Default changed to t. * org-exp.el (org-export-as-html): Close paragraph after a footnote. * org.el (org-update-parent-todo-statistics): New function. * org-exp.el (org-icalendar-store-UID): New option. (org-icalendar-force-UID): Option removed. (org-print-icalendar-entries): IMplement UIDs. 2008-06-17 Carsten Dominik <dominik@science.uva.nl> * org-mhe.el (org-mhe-follow-link): Fix bug in mhe searches. 2008-06-17 Carsten Dominik <dominik@science.uva.nl> * org-faces.el (org-column): Document how this face is being used and why sometimes the background faces shine through. * org-mhe.el (org-mhe-follow-link): Improve handling of searches. * org-publish.el (org-publish-attachment): Create publishing directory if it does not yet exist. * org-table.el (org-calc-default-modes): Change default number format to (float 8). * org.el (org-olpath-completing-read): New function. (org-time-clocksum-format): New option. (org-minutes-to-hh:mm-string): Use `org-time-clocksum-format'. * org-clock.el (org-clock-display, org-clock-out) (org-update-mode-line): Use `org-time-clocksum-format'. * org-colview-xemacs.el (org-columns-number-to-string): Use `org-time-clocksum-format'. * org-colview.el (org-columns-number-to-string): Use `org-time-clocksum-format'. 2008-06-17 Carsten Dominik <dominik@science.uva.nl> * org-id.el: New file, move from contrib to core. * org-exp.el (org-icalendar-force-UID): New option. 2008-06-17 Carsten Dominik <dominik@science.uva.nl> * org-exp.el (org-print-icalendar-entries): Make sure DTEND is shifted by one day if theere is a date range without an end time. * org.el (org-try-structure-completion): New function. 2008-06-17 Carsten Dominik <dominik@science.uva.nl> * org.el (org-set-font-lock-defaults): Improve fontification of description lists. (org-insert-item): Handle description lists. (org-adaptive-fill-function): Improve auto indentation in description lists. * org-exp.el (org-export-as-html, org-export-preprocess-string): Implement VERSE environment. (org-export-preprocess-string): Implement the COMMENT environment. * org-export-latex.el (org-export-latex-preprocess): Implement VERSE environment. 2008-06-17 Carsten Dominik <dominik@science.uva.nl> * org-jsinfo.el (org-infojs-opts-table): Add entry for FIXED_TOC option. 2008-06-17 Carsten Dominik <dominik@science.uva.nl> * org-table.el (orgtbl-to-tsv, orgtbl-to-csv): New functions. * org.el (org-quote-csv-field): New functions. * org-table.el (org-table-export-default-format): Remove :splice from default format, we get the same effect by not specifying :tstart and :tend. (org-table-export): Improve setup, distinguish better between interactive and non-interactive use, allow specifying the format on the fly, better protection against wrong file names. (orgtbl-to-generic): Fix documentation. Do not require :tstart and :tend when :splice is omitted. 2008-06-17 Carsten Dominik <dominik@science.uva.nl> * org-clock.el (org-clock-select-task): Make sure the selection letters are 1-9 and A-Z, no special characters. 2008-06-17 Carsten Dominik <dominik@science.uva.nl> * org-exp.el (org-export-htmlize): New group. (org-export-htmlize-output-type) (org-export-htmlize-css-font-prefix): New options. (org-export-htmlize-region-for-paste): New function. (org-export-htmlize-generate-css): New command. 2008-06-17 Carsten Dominik <dominik@science.uva.nl> * org.el (org-set-visibility-according-to-property): New function. (org-ctrl-c-ctrl-c): Do not restart org-mode, just get the options and compute the regular expressions, and update font-lock. (org-property-re): Allow a dash in property names. * org-archive.el (org-extract-archive-file): Insert the file name without the path into the format, to allow the location format to contain a subdirectory. * org-agenda.el (org-agenda-post-command-hook): If point is at end of buffer, and the `org-agenda-type' property undefined, use the value from the character before. * org.el (org-add-planning-info): Don't let indentation for would-be timestamp become extra whitespace at the end of headline. 2008-06-17 Carsten Dominik <dominik@science.uva.nl> * org.el (org-remove-double-quotes, org-file-contents): New functions. * org-exp.el (org-infile-export-plist): Also parse the contents of #+SETUPFILE files, recursively. * org.el (org-set-regexps-and-options): Also parse the contents of #+SETUPFILE files, recursively. * org-exp.el (org-export-handle-include-files): New function. (org-export-preprocess-string): Call `org-export-handle-include-files'. * org.el (org-delete-property-globally) (org-delete-property, org-set-property): Ignore case during completion. (org-set-property): Use `org-completing-read' instead of `completing-read'. * org.el (org-complete-expand-structure-template): New, experimental function. (org-structure-template-alist): New, experimental option. (org-complete): Call `org-complete-expand-structure-template'. 2008-06-17 Bastien Guerry <bzg@altern.org> * org-export-latex.el (org-export-latex-preprocess): Added support for blockquotes. 2008-06-17 Carsten Dominik <dominik@science.uva.nl> * org.el (org-read-date-analyze): Catch the case where only a weekday is given. 2008-06-17 Carsten Dominik <dominik@science.uva.nl> * org.el (org-set-font-lock-defaults): Make the description tag bold. * org-exp.el (org-export-as-html, org-close-li): Implement description lists. 2008-06-17 Jason Riedy <jason@acm.org> * org-table.el (*orgtbl-default-fmt*): New variable. (orgtbl-format-line): Use the value of *orgtbl-default-fmt* when there is no other fmt available. (orgtbl-to-generic): Allow an explicitly nil :tstart or :tend to suppress the appropriate string. (orgtbl-to-orgtbl): New function for translating to another orgtbl table. 2008-06-17 Carsten Dominik <dominik@science.uva.nl> * org.el (org-read-date-analyze): "." as an alias for "+0" in read date. * org-clock.el (org-clock-save-markers-for-cut-and-paste): New function. * org-agenda.el (org-agenda-save-markers-for-cut-and-paste): New function. 2008-06-17 Carsten Dominik <dominik@science.uva.nl> * org-clock.el (org-clock-find-position): Don't include notes into clock drawer. * org-archive.el (org-archive-subtree): No longer remove an extra line after cutting the subtree. `org-cut-subtree' already takes care of this. * org-remember.el (org-remember-handler): Only kill the target buffer if it does not contain the running clock. * org.el (org-markers-to-move): New variable. (org-save-markers-in-region, org-check-and-save-marker) (org-reinstall-markers-in-region): New function. (org-move-subtree-down, org-copy-subtree): Remember relative marker positions before cutting. (org-move-subtree-down, org-paste-subtree): Restore relative marker positions after pasting. * org-remember.el (org-remember-clock-out-on-exit): New option. (org-remember-finalize): Clock out only if the setting in `org-remember-clock-out-on-exit' requires it. (org-remember-handler): Do the cleanup in the buffer, to make sure that the clock marker remains in tact. 2008-06-17 Carsten Dominik <dominik@science.uva.nl> * org-clock.el (org-clock-goto): Widen buffer if necessary. (org-clock-in): Make sure that also tasks outside the narrowed region will be clocked in correctly. (org-clock-insert-selection-line): Widen the buffer so that we can find the correct task heading. * org.el (org-base-buffer): New function. * org-exp.el (org-icalendar-cleanup-string): Make sure '," and ";" are escaped. (org-print-icalendar-entries): Also apply `org-icalendar-cleanup-string' to the headline, not only to the summary property. 2008-06-17 Carsten Dominik <dominik@science.uva.nl> * org-exp.el (org-export-preprocess-hook): New hook. (org-export-preprocess-string): Call `org-export-preprocess-hook'. * org.el (org-font-lock-hook): New variable. (org-font-lock-hook): New function. (org-set-font-lock-defaults): Call `org-font-lock-hook'. 2008-06-17 Carsten Dominik <dominik@science.uva.nl> * org.texi: Modify license to no longer include back- and front cover matters. (Using the mapping API): New section. (Agenda column view): New section. (Moving subtrees): Document archiving to the archive sibling. (Agenda commands): Document columns view in the agenda. (Using the property API): Document the API for multi-valued properties.
author Carsten Dominik <dominik@science.uva.nl>
date Tue, 17 Jun 2008 15:22:00 +0000
parents ee5932bf781d
children 53921407de01
line wrap: on
line source

;;; ps-bdf.el --- BDF font file handler for ps-print

;; Copyright (C) 1998, 1999, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008
;;   Free Software Foundation, Inc.
;; Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007,
;;   2008
;;   National Institute of Advanced Industrial Science and Technology (AIST)
;;   Registration Number H14PRO021

;; Copyright (C) 2003
;;   National Institute of Advanced Industrial Science and Technology (AIST)
;;   Registration Number H13PRO009

;; Keywords: wp, BDF, font, PostScript
;; Maintainer: Kenichi Handa <handa@m17n.org>

;; This file is part of GNU Emacs.

;; GNU Emacs is free software: you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.

;; GNU Emacs is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;; GNU General Public License for more details.

;; You should have received a copy of the GNU General Public License
;; along with GNU Emacs.  If not, see <http://www.gnu.org/licenses/>.

;;; Commentary:

;; Functions for getting bitmap information from X's BDF font file are
;; provided.

;;; Code:

(eval-and-compile
  (require 'ps-mule))

;;;###autoload
(defvar bdf-directory-list
  (if (memq system-type '(ms-dos windows-nt))
      (list (expand-file-name "fonts/bdf" installation-directory))
    '("/usr/local/share/emacs/fonts/bdf"))
  "*List of directories to search for `BDF' font files.
The default value is '(\"/usr/local/share/emacs/fonts/bdf\").")

;; MS-DOS and MS-Windows users like to move the binary around after
;; it's built, but the value above is computed at load-up time.
(and (memq system-type '(ms-dos windows-nt))
     (setq bdf-directory-list
	   (list (expand-file-name "fonts/bdf" installation-directory))))

(defun bdf-expand-file-name (bdfname)
  "Return an absolute path name of a `BDF' font file BDFNAME.
It searches directories listed in the variable `bdf-directory-list'
for BDFNAME."
  (if (file-name-absolute-p bdfname)
      (and (file-readable-p bdfname)
	   bdfname)
    (catch 'tag
      (dolist (dir bdf-directory-list)
	(let ((absolute-path (expand-file-name bdfname dir)))
	  (if (file-readable-p absolute-path)
	    (throw 'tag absolute-path)))))))

(defsubst bdf-file-mod-time (filename)
  "Return modification time of FILENAME.
The value is a list of two integers, the first integer has high-order
16 bits, the second has low 16 bits."
  (nth 5 (file-attributes filename)))

(defun bdf-file-newer-than-time (filename mod-time)
  "Return non-nil if and only if FILENAME is newer than MOD-TIME.
MOD-TIME is a modification time as a list of two integers, the first
integer has high-order 16 bits, the second has low 16 bits."
  (let* ((new-mod-time (bdf-file-mod-time filename))
	 (new-time (car new-mod-time))
	 (time (car mod-time)))
    (or (> new-time time)
	(and (= new-time time)
	     (> (nth 1 new-mod-time) (nth 1 mod-time))))))

(defun bdf-find-file (bdfname)
  "Return a buffer visiting a bdf file BDFNAME.
BDFNAME must be an absolute file name.
If BDFNAME doesn't exist, return nil."
  (and (file-readable-p bdfname)
       (let ((buf (generate-new-buffer " *bdf-work*"))
	     (coding-system-for-read 'no-conversion))
	 (save-excursion
	   (set-buffer buf)
	   (insert-file-contents bdfname)
	   buf))))

(defvar bdf-cache-file (if (eq system-type 'ms-dos)
			   ;; convert-standard-filename doesn't
			   ;; guarantee that the .el extension will be
			   ;; preserved.
			   "~/_bdfcache.el"
			 (convert-standard-filename "~/.bdfcache.el"))
  "Name of cache file which contains information of `BDF' font files.")

(defvar bdf-cache nil
  "Cached information of `BDF' font files.  It is a list of FONT-INFO.
FONT-INFO is a list of the following format:
    (ABSOLUTE-FILE-NAME MOD-TIME SIZE FONT-BOUNDING-BOX
     RELATIVE-COMPOSE BASELINE-OFFSET CODE-RANGE MAXLEN OFFSET-VECTOR)
See the documentation of the function `bdf-read-font-info' for more detail.")

(defun bdf-read-cache ()
  "Return a cached information about `BDF' font files from a cache file.
The variable `bdf-cache-file' holds the cache file name.
If the cache file is not readable, this return nil."
  (setq bdf-cache nil)
  (condition-case nil
      (and (file-readable-p bdf-cache-file)
	   (progn
	     (load-file bdf-cache-file)
	     (if (listp bdf-cache)
		 bdf-cache
	       (setq bdf-cache nil))))
    (error nil)))

(defun bdf-write-cache ()
  "Write out cached information of `BDF' font file to a file.
The variable `bdf-cache-file' holds the cache file name.
The file is written if and only if the file already exists and writable."
  (and bdf-cache
       (file-exists-p bdf-cache-file)
       (file-writable-p bdf-cache-file)
       (write-region (format "(setq bdf-cache '%S)\n" bdf-cache)
		     nil bdf-cache-file)))

(defun bdf-set-cache (font-info)
  "Cache FONT-INFO as information about one `BDF' font file.
FONT-INFO is a list of the following format:
    (ABSOLUTE-FILE-NAME MOD-TIME SIZE FONT-BOUNDING-BOX
     RELATIVE-COMPOSE BASELINE-OFFSET CODE-RANGE MAXLEN OFFSET-VECTOR)
See the documentation of the function `bdf-read-font-info' for more detail."
  (let ((slot (assoc (car font-info) bdf-cache)))
    (if slot
	(setcdr slot (cdr font-info))
      (setq bdf-cache (cons font-info bdf-cache)))))

(defun bdf-initialize ()
  "Initialize `bdf' library."
  (and (bdf-read-cache)
       (add-hook 'kill-emacs-hook 'bdf-write-cache)))

(defun bdf-compact-code (code code-range)
  (if (or (< code (aref code-range 4))
	  (> code (aref code-range 5)))
      (setq code (aref code-range 6)))
  (+ (* (- (lsh code -8) (aref code-range 0))
	(1+ (- (aref code-range 3) (aref code-range 2))))
     (- (logand code 255) (aref code-range 2))))

(defun bdf-expand-code (code code-range)
  (let ((code0-range (1+ (- (aref code-range 3) (aref code-range 2)))))
    (+ (* (+ (/ code code0-range) (aref code-range 0)) 256)
       (+ (% code code0-range) (aref code-range 2)))))

(defun bdf-search-and-read (match limit)
  (goto-char (point-min))
  (and (search-forward match limit t)
       (progn
	 (goto-char (match-end 0))
	 (read (current-buffer)))))

(defun bdf-read-font-info (bdfname)
  "Read `BDF' font file BDFNAME and return information (FONT-INFO) of the file.
BDFNAME must be an absolute file name.
FONT-INFO is a list of the following format:
    (BDFFILE MOD-TIME FONT-BOUNDING-BOX
     RELATIVE-COMPOSE BASELINE-OFFSET CODE-RANGE MAXLEN OFFSET-VECTOR)

MOD-TIME is last modification time as a list of two integers, the
first integer has high-order 16 bits, the second has low 16 bits.

SIZE is a size of the font on 72 dpi device.  This value is got
from SIZE record of the font.

FONT-BOUNDING-BOX is the font bounding box as a list of four integers,
BBX-WIDTH, BBX-HEIGHT, BBX-XOFF, and BBX-YOFF.

RELATIVE-COMPOSE is an integer value of the font's property
`_MULE_RELATIVE_COMPOSE'.  If the font doesn't have this property, the
value is 0.

BASELINE-OFFSET is an integer value of the font's property
`_MULE_BASELINE_OFFSET'.  If the font doesn't have this property, the
value is 0.

CODE-RANGE is a vector of minimum 1st byte, maximum 1st byte, minimum
2nd byte, maximum 2nd byte, minimum code, maximum code, and default
code.  For 1-byte fonts, the first two elements are 0.

MAXLEN is a maximum bytes of one glyph information in the font file.

OFFSET-VECTOR is a vector of a file position which starts bitmap data
of the glyph in the font file.

Nth element of OFFSET-VECTOR is a file position for the glyph of code
CODE, where N and CODE are in the following relation:
    (bdf-compact-code CODE) => N, (bdf-expand-code N) => CODE"
  (let* ((buf (bdf-find-file bdfname))
	 (maxlen 0)
	 (relative-compose 'false)
	 (baseline-offset 0)
	 size
	 dpi
	 font-bounding-box
	 default-char
	 code-range
	 offset-vector)
    (if buf
	(message "Reading %s..." bdfname)
      (error "BDF file %s doesn't exist" bdfname))
    (unwind-protect
	(save-excursion
	  (set-buffer buf)
	  (goto-char (point-min))
	  (search-forward "\nFONTBOUNDINGBOX")
	  (setq font-bounding-box
		(vector (read (current-buffer)) (read (current-buffer))
			(read (current-buffer)) (read (current-buffer))))
	  ;; The following kludgy code is to avoid bugs of fonts
	  ;; jiskan16.bdf and jiskan24.bdf distributed with X.
	  ;; They contain wrong FONTBOUNDINGBOX.
	  (and (> (aref font-bounding-box 3) 0)
	       (string-match "jiskan\\(16\\|24\\)" bdfname)
	       (aset font-bounding-box 3
		     (- (aref font-bounding-box 3))))

	  (goto-char (point-min))
	  (search-forward "\nFONT ")
	  (if (looking-at "-[^-]*-[^-]*-[^-]*-[^-]*-[^-]*-[^-]*-\\([0-9]+\\)")
	      (setq size (string-to-number (match-string 1)))
	    (search-forward "\nSIZE ")
	    (setq size (read (current-buffer)))
	    ;; The following kludgy code is to avoid bugs of several
	    ;; fonts which have wrong SIZE record.
	    (and (string-match "jiskan" bdfname)
		 (<= size (/ (aref font-bounding-box 1) 3))
		 (setq size (aref font-bounding-box 1)))
	    (setq dpi (read (current-buffer)))
	    (if (and (> dpi 0) (/= dpi 72))
		(setq size (/ (* size dpi) 72))))

	  (setq default-char (bdf-search-and-read "\nDEFAULT_CHAR" nil))

	  (search-forward "\nSTARTCHAR")
	  (forward-line -1)
	  (let ((limit (point)))
	    (setq relative-compose
		  (or (bdf-search-and-read "\n_MULE_RELATIVE_COMPOSE" limit)
		      'false)
		  baseline-offset
		  (or (bdf-search-and-read "\n_MULE_BASELINE_OFFSET" limit)
		      0)))

	  (let ((min-code0 256) (min-code1 256) (min-code 65536)
		(max-code0 0) (max-code1 0) (max-code 0)
		glyph glyph-list code0 code1 code offset)

	    (while (search-forward "\nSTARTCHAR" nil t)
	      (setq offset (line-beginning-position))
	      (search-forward "\nENCODING")
	      (setq code (read (current-buffer)))
	      (if (< code 0)
		  (search-forward "ENDCHAR")
		(setq code0 (lsh code -8)
		      code1 (logand code 255)
		      min-code (min min-code code)
		      max-code (max max-code code)
		      min-code0 (min min-code0 code0)
		      max-code0 (max max-code0 code0)
		      min-code1 (min min-code1 code1)
		      max-code1 (max max-code1 code1))
		(search-forward "ENDCHAR")
		(setq maxlen (max maxlen (- (point) offset))
		      glyph-list (cons (cons code offset) glyph-list))))

	    (setq code-range
		  (vector min-code0 max-code0 min-code1 max-code1
			  min-code max-code (or default-char min-code))
		  offset-vector
		  (make-vector (1+ (bdf-compact-code max-code code-range))
			       nil))

	    (while glyph-list
	      (setq glyph (car glyph-list)
		    glyph-list (cdr glyph-list))
	      (aset offset-vector
		    (bdf-compact-code (car glyph) code-range)
		    (cdr glyph)))))

    (kill-buffer buf))
  (message "Reading %s...done" bdfname)
  (list bdfname (bdf-file-mod-time bdfname)
	size font-bounding-box relative-compose baseline-offset
	code-range maxlen offset-vector)))

(defsubst bdf-info-absolute-path (font-info)     (nth 0 font-info))
(defsubst bdf-info-mod-time (font-info)          (nth 1 font-info))
(defsubst bdf-info-size (font-info)              (nth 2 font-info))
(defsubst bdf-info-font-bounding-box (font-info) (nth 3 font-info))
(defsubst bdf-info-relative-compose (font-info)  (nth 4 font-info))
(defsubst bdf-info-baseline-offset (font-info)   (nth 5 font-info))
(defsubst bdf-info-code-range (font-info)        (nth 6 font-info))
(defsubst bdf-info-maxlen (font-info)            (nth 7 font-info))
(defsubst bdf-info-offset-vector (font-info)     (nth 8 font-info))

(defun bdf-get-font-info (bdfname)
  "Return information about `BDF' font file BDFNAME.
BDFNAME must be an absolute file name.
The value FONT-INFO is a list of the following format:
    (BDFNAME MOD-TIME SIZE FONT-BOUNDING-BOX
     RELATIVE-COMPOSE BASELINE-OFFSET CODE-RANGE MAXLEN OFFSET-VECTOR)
See the documentation of the function `bdf-read-font-info' for more detail."
  (or bdf-cache
      (bdf-read-cache))
  (let ((font-info (assoc bdfname bdf-cache)))
    (if (or (not font-info)
	    (not (file-readable-p bdfname))
	    (bdf-file-newer-than-time bdfname (bdf-info-mod-time font-info)))
	(progn
	  (setq font-info (bdf-read-font-info bdfname))
	  (bdf-set-cache font-info)))
    font-info))

(defun bdf-read-bitmap (bdfname offset maxlen relative-compose)
  "Read `BDF' font file BDFNAME to get bitmap data at file position OFFSET.
BDFNAME is an absolute path name of the font file.
MAXLEN specifies how many bytes we should read at least.
The value is a list of DWIDTH, BBX, and BITMAP-STRING.
DWIDTH is a pixel width of a glyph.
BBX is a bounding box of the glyph.
BITMAP-STRING is a string representing bits by hexadecimal digits."
  (let* ((coding-system-for-read 'no-conversion)
	 (bbx (bdf-info-font-bounding-box (bdf-get-font-info bdfname)))
	 (dwidth (elt bbx 0))
	 (bitmap-string "")
	 height yoff)
    (condition-case nil
	(with-temp-buffer
	  (insert-file-contents bdfname nil offset (+ offset maxlen))
	  (goto-char (point-min))
	  (search-forward "\nDWIDTH")
	  (setq dwidth (read (current-buffer)))
	  (if (= dwidth 0)
	      (setq dwidth 0.1))
	  (goto-char (point-min))
	  (search-forward "\nBBX")
	  (setq bbx (vector (read (current-buffer)) (read (current-buffer))
			    (read (current-buffer)) (read (current-buffer)))
		height (aref bbx 1)
		yoff (aref bbx 3))
	  (search-forward "\nBITMAP")
	  (forward-line 1)
	  (delete-region (point-min) (point))
	  (and (looking-at "\\(0+\n\\)+")
	       (progn
		 (setq height (- height (count-lines (point) (match-end 0))))
		 (delete-region (point) (match-end 0))))
	  (or (looking-at "ENDCHAR")
	      (progn
		(search-forward "ENDCHAR" nil 'move)
		(forward-line -1)
		(while (looking-at "0+$")
		  (setq yoff (1+ yoff)
			height (1- height))
		  (forward-line -1))
		(forward-line 1)))
	  (aset bbx 1 height)
	  (aset bbx 3 yoff)
	  (delete-region (point) (point-max))
	  (goto-char (point-min))
	  (while (not (eobp))
	    (end-of-line)
	    (delete-char 1))
	  (setq bitmap-string (buffer-string)))
      (error nil))
    (vector dwidth (aref bbx 0) (aref bbx 1) (aref bbx 2) (aref bbx 3)
	    (concat "<" bitmap-string ">")
	    (or relative-compose 'false))))

(defun bdf-get-bitmap (bdfname code)
  "Return bitmap information of glyph of CODE in `BDF' font file BDFNAME.
CODE is an encoding number of glyph in the file.
The value is a list (DWIDTH BBX BITMAP-STRING).
DWIDTH is a pixel width of a glyph.
BBX is a bounding box of the glyph.
BITMAP-STRING is a string representing bits by hexadecimal digits."
  (let* ((info (bdf-get-font-info bdfname))
	 (maxlen (bdf-info-maxlen info))
	 (code-range (bdf-info-code-range info))
	 (offset-vector (bdf-info-offset-vector info)))
    (bdf-read-bitmap bdfname
		     (aref offset-vector (bdf-compact-code code code-range))
		     maxlen (bdf-info-relative-compose info))))

;;; Interface to ps-mule.el

;; Called from ps-mule-init-external-library.
(defun bdf-generate-prologue ()
  (or bdf-cache
      (bdf-initialize))
  (ps-mule-generate-bitmap-prologue))

;; Called from ps-mule-check-font.
(defun bdf-check-font (font-spec)
  (let ((font-name-list (ps-mule-font-spec-name font-spec)))
    (ps-mule-font-spec-set-name
     font-spec
     (if (stringp font-name-list)
	 (bdf-expand-file-name font-name-list)
       (catch 'tag
	 (dolist (font-name font-name-list)
	   (setq font-name (bdf-expand-file-name font-name))
	   (if font-name
	       (throw 'tag font-name))))))))

;; Called from ps-mule-generate-font.
(defun bdf-generate-font (font-spec)
  (let ((info (bdf-get-font-info (ps-mule-font-spec-name font-spec))))
    (ps-mule-font-spec-set-extra
     font-spec (bdf-info-absolute-path info))
    (ps-mule-generate-bitmap-font font-spec
				  (bdf-info-size info)
				  (bdf-info-relative-compose info)
				  (bdf-info-baseline-offset info)
				  (bdf-info-font-bounding-box info))))

;; Called from ps-mule-generate-glyph.
(defun bdf-generate-glyph (font-spec char)
  (let ((font-name (ps-mule-font-spec-extra font-spec))
	(code (ps-mule-encode-char char font-spec)))
    (ps-mule-generate-bitmap-glyph font-spec char code
				   (bdf-get-bitmap font-name code))))

(provide 'ps-bdf)

;; arch-tag: 9b875ba8-565a-4ecf-acaa-30cee732c898
;;; ps-bdf.el ends here