(python-open-block-statement-p): Fix
[emacs.git] / lisp / gs.el
blob2c38a55f6dfe939731d264dd43fce1f205684358
1 ;;; gs.el --- interface to Ghostscript
3 ;; Copyright (C) 1998, 2001, 2004 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 2, or (at your option)
13 ;; 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; see the file COPYING. If not, write to the
22 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 ;; Boston, MA 02111-1307, USA.
25 ;;; Commentary:
27 ;; This code is experimental. Don't use it.
29 ;;; Code:
31 (defvar gs-program "gs"
32 "The name of the Ghostscript interpreter.")
35 (defvar gs-device "x11"
36 "The Ghostscript device to use to produce images.")
39 (defvar gs-options
40 '("-q"
41 ;"-dNOPAUSE"
42 "-dSAFER"
43 "-dBATCH"
44 "-sDEVICE=<device>"
45 "<file>")
46 "List of command line arguments to pass to Ghostscript.
47 Arguments may contain place-holders `<file>' for the name of the
48 input file, and `<device>' for the device to use.")
49 (put 'gs-options 'risky-local-variable t)
51 (defun gs-options (device file)
52 "Return a list of command line options with place-holders replaced.
53 DEVICE is the value to substitute for the place-holder `<device>',
54 FILE is the value to substitute for the place-holder `<file>'."
55 (mapcar #'(lambda (option)
56 (setq option (replace-regexp-in-string "<device>" device option)
57 option (replace-regexp-in-string "<file>" file option)))
58 gs-options))
60 ;; The GHOSTVIEW property (taken from gv 3.5.8).
62 ;; Type:
64 ;; STRING
66 ;; Parameters:
68 ;; BPIXMAP ORIENT LLX LLY URX URY XDPI YDPI [LEFT BOTTOM TOP RIGHT]
70 ;; Scanf format: "%d %d %d %d %d %d %f %f %d %d %d %d"
72 ;; Explanation of parameters:
74 ;; BPIXMAP: pixmap id of the backing pixmap for the window. If no
75 ;; pixmap is to be used, this parameter should be zero. This
76 ;; parameter must be zero when drawing on a pixmap.
78 ;; ORIENT: orientation of the page. The number represents clockwise
79 ;; rotation of the paper in degrees. Permitted values are 0, 90, 180,
80 ;; 270.
82 ;; LLX, LLY, URX, URY: Bounding box of the drawable. The bounding box
83 ;; is specified in PostScript points in default user coordinates.
85 ;; XDPI, YDPI: Resolution of window. (This can be derived from the
86 ;; other parameters, but not without roundoff error. These values are
87 ;; included to avoid this error.)
89 ;; LEFT, BOTTOM, TOP, RIGHT: (optional) Margins around the window.
90 ;; The margins extend the imageable area beyond the boundaries of the
91 ;; window. This is primarily used for popup zoom windows. I have
92 ;; encountered several instances of PostScript programs that position
93 ;; themselves with respect to the imageable area. The margins are
94 ;; specified in PostScript points. If omitted, the margins are
95 ;; assumed to be 0.
97 (defun gs-width-in-pt (frame pixel-width)
98 "Return, on FRAME, pixel width PIXEL-WIDTH tranlated to pt."
99 (let ((mm (* (float pixel-width)
100 (/ (float (x-display-mm-width frame))
101 (float (x-display-pixel-width frame))))))
102 (/ (* 25.4 mm) 72.0)))
105 (defun gs-height-in-pt (frame pixel-height)
106 "Return, on FRAME, pixel height PIXEL-HEIGHT tranlated to pt."
107 (let ((mm (* (float pixel-height)
108 (/ (float (x-display-mm-height frame))
109 (float (x-display-pixel-height frame))))))
110 (/ (* 25.4 mm) 72.0)))
113 (defun gs-set-ghostview-window-prop (frame spec img-width img-height)
114 "Set the `GHOSTVIEW' window property of FRAME.
115 SPEC is a GS image specification. IMG-WIDTH is the width of the
116 requested image, and IMG-HEIGHT is the height of the requested
117 image in pixels."
118 (let* ((box (plist-get (cdr spec) :bounding-box))
119 (llx (elt box 0))
120 (lly (elt box 1))
121 (urx (elt box 2))
122 (ury (elt box 3))
123 (rotation (or (plist-get (cdr spec) :rotate) 0))
124 ;; The pixel width IMG-WIDTH of the pixmap gives the
125 ;; dots, URX - LLX give the inch.
126 (in-width (/ (- urx llx) 72.0))
127 (in-height (/ (- ury lly) 72.0))
128 (xdpi (/ img-width in-width))
129 (ydpi (/ img-height in-height)))
130 (x-change-window-property "GHOSTVIEW"
131 (format "0 %d %d %d %d %d %g %g"
132 rotation llx lly urx ury xdpi ydpi)
133 frame)))
136 (defun gs-set-ghostview-colors-window-prop (frame pixel-colors)
137 "Set the `GHOSTVIEW_COLORS' environment variable depending on FRAME."
138 (let ((mode (cond ((x-display-color-p frame) "Color")
139 ((x-display-grayscale-p frame) "Grayscale")
140 (t "Monochrome"))))
141 (x-change-window-property "GHOSTVIEW_COLORS"
142 (format "%s %s" mode pixel-colors)
143 frame)))
147 ;;;###autoload
148 (defun gs-load-image (frame spec img-width img-height window-and-pixmap-id
149 pixel-colors)
150 "Load a PS image for display on FRAME.
151 SPEC is an image specification, IMG-HEIGHT and IMG-WIDTH are width
152 and height of the image in pixels. WINDOW-AND-PIXMAP-ID is a string of
153 the form \"WINDOW-ID PIXMAP-ID\". Value is non-nil if successful."
154 (unwind-protect
155 (let ((file (plist-get (cdr spec) :file))
157 (timeout 40))
158 ;; Wait while property gets freed from a previous ghostscript process
159 ;; sit-for returns nil as soon as input starts being
160 ;; available, so if we want to give GhostScript a reasonable
161 ;; chance of starting up, we better use sleep-for. We let
162 ;; sleep-for wait only half the time because if input is
163 ;; available, it is more likely that we don't care that much
164 ;; about garbled redisplay and are in a hurry.
165 (while (and
166 ;; Wait while the property is not yet available
167 (not (zerop (length (x-window-property "GHOSTVIEW"
168 frame))))
169 ;; The following was an alternative condition: wait
170 ;; while there is still a process running. The idea
171 ;; was to avoid contention between processes. Turned
172 ;; out even more sluggish.
173 ;; (get-buffer-process "*GS*")
174 (not (zerop timeout)))
175 (unless (sit-for 0 100 t)
176 (sleep-for 0 50))
177 (setq timeout (1- timeout)))
179 ;; No use waiting longer. We might want to try killing off
180 ;; stuck processes, but there is no point in doing so: either
181 ;; they are stuck for good, in which case the user would
182 ;; probably be responsible for that, and killing them off will
183 ;; make debugging harder, or they are not. In that case, they
184 ;; will cause incomplete displays. But the same will happen
185 ;; if they are killed, anyway. The whole is rather
186 ;; disconcerting, and fast scrolling through a dozen images
187 ;; will make Emacs freeze for a while. The alternatives are a)
188 ;; proper implementation not waiting at all but creating
189 ;; appropriate queues, or b) permanently bad display due to
190 ;; bad cached images. So remember that this
191 ;; is just a hack and if people don't like the behaviour, they
192 ;; will most likely like the easy alternatives even less.
193 ;; And at least the image cache will make the delay apparent
194 ;; just once.
195 (gs-set-ghostview-window-prop frame spec img-width img-height)
196 (gs-set-ghostview-colors-window-prop frame pixel-colors)
197 (setenv "GHOSTVIEW" window-and-pixmap-id)
198 (setq gs (apply 'start-process "gs" "*GS*" gs-program
199 (gs-options gs-device file)))
200 (set-process-query-on-exit-flag gs nil)
202 nil))
205 ;(defun gs-put-tiger ()
206 ; (let* ((ps-file "/usr/local/share/ghostscript/5.10/examples/tiger.ps")
207 ; (spec `(image :type postscript
208 ; :pt-width 200 :pt-height 200
209 ; :bounding-box (22 171 567 738)
210 ; :file ,ps-file)))
211 ; (put-text-property 1 2 'display spec)))
214 (provide 'gs)
216 ;;; arch-tag: 06ab51b8-4932-4cfe-9f60-b924a8edb3f0
217 ;;; gs.el ends here