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