ps-bdf.el 16 KB

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