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