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