gs.el 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. ;;; gs.el --- interface to Ghostscript
  2. ;; Copyright (C) 1998, 2001-2012 Free Software Foundation, Inc.
  3. ;; Maintainer: FSF
  4. ;; Keywords: internal
  5. ;; This file is part of GNU Emacs.
  6. ;; GNU Emacs is free software: you can redistribute it and/or modify
  7. ;; it under the terms of the GNU General Public License as published by
  8. ;; the Free Software Foundation, either version 3 of the License, or
  9. ;; (at your option) any later version.
  10. ;; GNU Emacs is distributed in the hope that it will be useful,
  11. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. ;; GNU General Public License for more details.
  14. ;; You should have received a copy of the GNU General Public License
  15. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  16. ;;; Commentary:
  17. ;; This code is experimental. Don't use it.
  18. ;;; Code:
  19. (defvar gs-program "gs"
  20. "The name of the Ghostscript interpreter.")
  21. (defvar gs-device "x11"
  22. "The Ghostscript device to use to produce images.")
  23. (defvar gs-options
  24. '("-q"
  25. ;"-dNOPAUSE"
  26. "-dSAFER"
  27. "-dBATCH"
  28. "-sDEVICE=<device>"
  29. "<file>")
  30. "List of command line arguments to pass to Ghostscript.
  31. Arguments may contain place-holders `<file>' for the name of the
  32. input file, and `<device>' for the device to use.")
  33. (put 'gs-options 'risky-local-variable t)
  34. (defun gs-options (device file)
  35. "Return a list of command line options with place-holders replaced.
  36. DEVICE is the value to substitute for the place-holder `<device>',
  37. FILE is the value to substitute for the place-holder `<file>'."
  38. (mapcar #'(lambda (option)
  39. (setq option (replace-regexp-in-string "<device>" device option)
  40. option (replace-regexp-in-string "<file>" file option)))
  41. gs-options))
  42. ;; The GHOSTVIEW property (taken from gv 3.5.8).
  43. ;;
  44. ;; Type:
  45. ;;
  46. ;; STRING
  47. ;;
  48. ;; Parameters:
  49. ;;
  50. ;; BPIXMAP ORIENT LLX LLY URX URY XDPI YDPI [LEFT BOTTOM TOP RIGHT]
  51. ;;
  52. ;; Scanf format: "%d %d %d %d %d %d %f %f %d %d %d %d"
  53. ;;
  54. ;; Explanation of parameters:
  55. ;;
  56. ;; BPIXMAP: pixmap id of the backing pixmap for the window. If no
  57. ;; pixmap is to be used, this parameter should be zero. This
  58. ;; parameter must be zero when drawing on a pixmap.
  59. ;;
  60. ;; ORIENT: orientation of the page. The number represents clockwise
  61. ;; rotation of the paper in degrees. Permitted values are 0, 90, 180,
  62. ;; 270.
  63. ;;
  64. ;; LLX, LLY, URX, URY: Bounding box of the drawable. The bounding box
  65. ;; is specified in PostScript points in default user coordinates.
  66. ;;
  67. ;; XDPI, YDPI: Resolution of window. (This can be derived from the
  68. ;; other parameters, but not without roundoff error. These values are
  69. ;; included to avoid this error.)
  70. ;;
  71. ;; LEFT, BOTTOM, TOP, RIGHT: (optional) Margins around the window.
  72. ;; The margins extend the imageable area beyond the boundaries of the
  73. ;; window. This is primarily used for popup zoom windows. I have
  74. ;; encountered several instances of PostScript programs that position
  75. ;; themselves with respect to the imageable area. The margins are
  76. ;; specified in PostScript points. If omitted, the margins are
  77. ;; assumed to be 0.
  78. (declare-function x-display-mm-width "xfns.c" (&optional terminal))
  79. (declare-function x-display-pixel-width "xfns.c" (&optional terminal))
  80. (defun gs-width-in-pt (frame pixel-width)
  81. "Return, on FRAME, pixel width PIXEL-WIDTH translated to pt."
  82. (let ((mm (* (float pixel-width)
  83. (/ (float (x-display-mm-width frame))
  84. (float (x-display-pixel-width frame))))))
  85. (/ (* 25.4 mm) 72.0)))
  86. (declare-function x-display-mm-height "xfns.c" (&optional terminal))
  87. (declare-function x-display-pixel-height "xfns.c" (&optional terminal))
  88. (defun gs-height-in-pt (frame pixel-height)
  89. "Return, on FRAME, pixel height PIXEL-HEIGHT translated to pt."
  90. (let ((mm (* (float pixel-height)
  91. (/ (float (x-display-mm-height frame))
  92. (float (x-display-pixel-height frame))))))
  93. (/ (* 25.4 mm) 72.0)))
  94. (declare-function x-change-window-property "xfns.c"
  95. (prop value &optional frame type format outer-p))
  96. (defun gs-set-ghostview-window-prop (frame spec img-width img-height)
  97. "Set the `GHOSTVIEW' window property of FRAME.
  98. SPEC is a GS image specification. IMG-WIDTH is the width of the
  99. requested image, and IMG-HEIGHT is the height of the requested
  100. image in pixels."
  101. (let* ((box (plist-get (cdr spec) :bounding-box))
  102. (llx (elt box 0))
  103. (lly (elt box 1))
  104. (urx (elt box 2))
  105. (ury (elt box 3))
  106. (rotation (or (plist-get (cdr spec) :rotate) 0))
  107. ;; The pixel width IMG-WIDTH of the pixmap gives the
  108. ;; dots, URX - LLX give the inch.
  109. (in-width (/ (- urx llx) 72.0))
  110. (in-height (/ (- ury lly) 72.0))
  111. (xdpi (/ img-width in-width))
  112. (ydpi (/ img-height in-height)))
  113. (x-change-window-property "GHOSTVIEW"
  114. (format "0 %d %d %d %d %d %g %g"
  115. rotation llx lly urx ury xdpi ydpi)
  116. frame)))
  117. (declare-function x-display-grayscale-p "xfns.c" (&optional terminal))
  118. (defun gs-set-ghostview-colors-window-prop (frame pixel-colors)
  119. "Set the `GHOSTVIEW_COLORS' environment variable depending on FRAME."
  120. (let ((mode (cond ((x-display-color-p frame) "Color")
  121. ((x-display-grayscale-p frame) "Grayscale")
  122. (t "Monochrome"))))
  123. (x-change-window-property "GHOSTVIEW_COLORS"
  124. (format "%s %s" mode pixel-colors)
  125. frame)))
  126. (declare-function x-window-property "xfns.c"
  127. (prop &optional frame type source delete-p vector-ret-p))
  128. ;;;###autoload
  129. (defun gs-load-image (frame spec img-width img-height window-and-pixmap-id
  130. pixel-colors)
  131. "Load a PS image for display on FRAME.
  132. SPEC is an image specification, IMG-HEIGHT and IMG-WIDTH are width
  133. and height of the image in pixels. WINDOW-AND-PIXMAP-ID is a string of
  134. the form \"WINDOW-ID PIXMAP-ID\". Value is non-nil if successful."
  135. (unwind-protect
  136. (let ((file (plist-get (cdr spec) :file))
  137. gs
  138. (timeout 40))
  139. ;; Wait while property gets freed from a previous ghostscript process
  140. ;; sit-for returns nil as soon as input starts being
  141. ;; available, so if we want to give GhostScript a reasonable
  142. ;; chance of starting up, we better use sleep-for. We let
  143. ;; sleep-for wait only half the time because if input is
  144. ;; available, it is more likely that we don't care that much
  145. ;; about garbled redisplay and are in a hurry.
  146. (while (and
  147. ;; Wait while the property is not yet available
  148. (not (zerop (length (x-window-property "GHOSTVIEW"
  149. frame))))
  150. ;; The following was an alternative condition: wait
  151. ;; while there is still a process running. The idea
  152. ;; was to avoid contention between processes. Turned
  153. ;; out even more sluggish.
  154. ;; (get-buffer-process "*GS*")
  155. (not (zerop timeout)))
  156. (unless (sit-for 0.1 t)
  157. (sleep-for 0.05))
  158. (setq timeout (1- timeout)))
  159. ;; No use waiting longer. We might want to try killing off
  160. ;; stuck processes, but there is no point in doing so: either
  161. ;; they are stuck for good, in which case the user would
  162. ;; probably be responsible for that, and killing them off will
  163. ;; make debugging harder, or they are not. In that case, they
  164. ;; will cause incomplete displays. But the same will happen
  165. ;; if they are killed, anyway. The whole is rather
  166. ;; disconcerting, and fast scrolling through a dozen images
  167. ;; will make Emacs freeze for a while. The alternatives are a)
  168. ;; proper implementation not waiting at all but creating
  169. ;; appropriate queues, or b) permanently bad display due to
  170. ;; bad cached images. So remember that this
  171. ;; is just a hack and if people don't like the behavior, they
  172. ;; will most likely like the easy alternatives even less.
  173. ;; And at least the image cache will make the delay apparent
  174. ;; just once.
  175. (gs-set-ghostview-window-prop frame spec img-width img-height)
  176. (gs-set-ghostview-colors-window-prop frame pixel-colors)
  177. (setenv "GHOSTVIEW" window-and-pixmap-id)
  178. (setq gs (apply 'start-process "gs" "*GS*" gs-program
  179. (gs-options gs-device file)))
  180. (set-process-query-on-exit-flag gs nil)
  181. gs)
  182. nil))
  183. ;(defun gs-put-tiger ()
  184. ; (let* ((ps-file "/usr/local/share/ghostscript/5.10/examples/tiger.ps")
  185. ; (spec `(image :type postscript
  186. ; :pt-width 200 :pt-height 200
  187. ; :bounding-box (22 171 567 738)
  188. ; :file ,ps-file)))
  189. ; (put-text-property 1 2 'display spec)))
  190. ;
  191. (provide 'gs)
  192. ;;; gs.el ends here