Commit | Line | Data |
---|---|---|
7840ced1 GM |
1 | ;;; gs.el --- interface to Ghostscript |
2 | ||
0d30b337 | 3 | ;; Copyright (C) 1998, 2001, 2002, 2003, 2004, |
409cc4a3 | 4 | ;; 2005, 2006, 2007, 2008 Free Software Foundation, Inc. |
7840ced1 GM |
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 | |
b4aa6026 | 13 | ;; the Free Software Foundation; either version 3, or (at your option) |
7840ced1 GM |
14 | ;; 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; see the file COPYING. If not, write to the | |
086add15 LK |
23 | ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, |
24 | ;; Boston, MA 02110-1301, USA. | |
7840ced1 GM |
25 | |
26 | ;;; Commentary: | |
27 | ||
28 | ;; This code is experimental. Don't use it. | |
29 | ||
30 | ;;; Code: | |
31 | ||
32 | (defvar gs-program "gs" | |
33 | "The name of the Ghostscript interpreter.") | |
34 | ||
35 | ||
36 | (defvar gs-device "x11" | |
37 | "The Ghostscript device to use to produce images.") | |
38 | ||
39 | ||
e95b0c08 | 40 | (defvar gs-options |
7840ced1 GM |
41 | '("-q" |
42 | ;"-dNOPAUSE" | |
2a1e88fa | 43 | "-dSAFER" |
7840ced1 GM |
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.") | |
2a1e88fa | 50 | (put 'gs-options 'risky-local-variable t) |
7840ced1 | 51 | |
7840ced1 GM |
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) | |
e95b0c08 SS |
57 | (setq option (replace-regexp-in-string "<device>" device option) |
58 | option (replace-regexp-in-string "<file>" file option))) | |
7840ced1 | 59 | gs-options)) |
e95b0c08 | 60 | |
7840ced1 | 61 | ;; The GHOSTVIEW property (taken from gv 3.5.8). |
e95b0c08 | 62 | ;; |
7840ced1 GM |
63 | ;; Type: |
64 | ;; | |
65 | ;; STRING | |
e95b0c08 | 66 | ;; |
7840ced1 | 67 | ;; Parameters: |
e95b0c08 | 68 | ;; |
7840ced1 | 69 | ;; BPIXMAP ORIENT LLX LLY URX URY XDPI YDPI [LEFT BOTTOM TOP RIGHT] |
e95b0c08 | 70 | ;; |
7840ced1 | 71 | ;; Scanf format: "%d %d %d %d %d %d %f %f %d %d %d %d" |
e95b0c08 | 72 | ;; |
7840ced1 | 73 | ;; Explanation of parameters: |
e95b0c08 | 74 | ;; |
7840ced1 GM |
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. | |
e95b0c08 | 78 | ;; |
7840ced1 GM |
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. | |
e95b0c08 | 82 | ;; |
7840ced1 GM |
83 | ;; LLX, LLY, URX, URY: Bounding box of the drawable. The bounding box |
84 | ;; is specified in PostScript points in default user coordinates. | |
e95b0c08 | 85 | ;; |
7840ced1 GM |
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.) | |
e95b0c08 | 89 | ;; |
7840ced1 GM |
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. | |
97 | ||
98 | (defun gs-width-in-pt (frame pixel-width) | |
99 | "Return, on FRAME, pixel width PIXEL-WIDTH tranlated 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))) | |
104 | ||
105 | ||
106 | (defun gs-height-in-pt (frame pixel-height) | |
107 | "Return, on FRAME, pixel height PIXEL-HEIGHT tranlated to pt." | |
108 | (let ((mm (* (float pixel-height) | |
109 | (/ (float (x-display-mm-height frame)) | |
110 | (float (x-display-pixel-height frame)))))) | |
111 | (/ (* 25.4 mm) 72.0))) | |
e95b0c08 | 112 | |
7840ced1 GM |
113 | |
114 | (defun gs-set-ghostview-window-prop (frame spec img-width img-height) | |
115 | "Set the `GHOSTVIEW' window property of FRAME. | |
116 | SPEC is a GS image specification. IMG-WIDTH is the width of the | |
117 | requested image, and IMG-HEIGHT is the height of the requested | |
118 | image in pixels." | |
119 | (let* ((box (plist-get (cdr spec) :bounding-box)) | |
62bfa682 GM |
120 | (llx (elt box 0)) |
121 | (lly (elt box 1)) | |
122 | (urx (elt box 2)) | |
123 | (ury (elt box 3)) | |
7840ced1 GM |
124 | (rotation (or (plist-get (cdr spec) :rotate) 0)) |
125 | ;; The pixel width IMG-WIDTH of the pixmap gives the | |
126 | ;; dots, URX - LLX give the inch. | |
127 | (in-width (/ (- urx llx) 72.0)) | |
128 | (in-height (/ (- ury lly) 72.0)) | |
129 | (xdpi (/ img-width in-width)) | |
130 | (ydpi (/ img-height in-height))) | |
131 | (x-change-window-property "GHOSTVIEW" | |
132 | (format "0 %d %d %d %d %d %g %g" | |
133 | rotation llx lly urx ury xdpi ydpi) | |
134 | frame))) | |
135 | ||
136 | ||
137 | (defun gs-set-ghostview-colors-window-prop (frame pixel-colors) | |
138 | "Set the `GHOSTVIEW_COLORS' environment variable depending on FRAME." | |
139 | (let ((mode (cond ((x-display-color-p frame) "Color") | |
140 | ((x-display-grayscale-p frame) "Grayscale") | |
141 | (t "Monochrome")))) | |
142 | (x-change-window-property "GHOSTVIEW_COLORS" | |
e98c601b GM |
143 | (format "%s %s" mode pixel-colors) |
144 | frame))) | |
e95b0c08 | 145 | |
7840ced1 GM |
146 | |
147 | ; | |
148 | ;;;###autoload | |
149 | (defun gs-load-image (frame spec img-width img-height window-and-pixmap-id | |
150 | pixel-colors) | |
151 | "Load a PS image for display on FRAME. | |
152 | SPEC is an image specification, IMG-HEIGHT and IMG-WIDTH are width | |
153 | and height of the image in pixels. WINDOW-AND-PIXMAP-ID is a string of | |
154 | the form \"WINDOW-ID PIXMAP-ID\". Value is non-nil if successful." | |
155 | (unwind-protect | |
156 | (let ((file (plist-get (cdr spec) :file)) | |
e98c601b | 157 | gs |
1332a0f2 GM |
158 | (timeout 40)) |
159 | ;; Wait while property gets freed from a previous ghostscript process | |
160 | ;; sit-for returns nil as soon as input starts being | |
161 | ;; available, so if we want to give GhostScript a reasonable | |
162 | ;; chance of starting up, we better use sleep-for. We let | |
163 | ;; sleep-for wait only half the time because if input is | |
164 | ;; available, it is more likely that we don't care that much | |
165 | ;; about garbled redisplay and are in a hurry. | |
166 | (while (and | |
167 | ;; Wait while the property is not yet available | |
168 | (not (zerop (length (x-window-property "GHOSTVIEW" | |
169 | frame)))) | |
170 | ;; The following was an alternative condition: wait | |
171 | ;; while there is still a process running. The idea | |
172 | ;; was to avoid contention between processes. Turned | |
173 | ;; out even more sluggish. | |
174 | ;; (get-buffer-process "*GS*") | |
175 | (not (zerop timeout))) | |
176 | (unless (sit-for 0 100 t) | |
177 | (sleep-for 0 50)) | |
178 | (setq timeout (1- timeout))) | |
179 | ||
180 | ;; No use waiting longer. We might want to try killing off | |
181 | ;; stuck processes, but there is no point in doing so: either | |
182 | ;; they are stuck for good, in which case the user would | |
183 | ;; probably be responsible for that, and killing them off will | |
184 | ;; make debugging harder, or they are not. In that case, they | |
185 | ;; will cause incomplete displays. But the same will happen | |
186 | ;; if they are killed, anyway. The whole is rather | |
187 | ;; disconcerting, and fast scrolling through a dozen images | |
188 | ;; will make Emacs freeze for a while. The alternatives are a) | |
189 | ;; proper implementation not waiting at all but creating | |
190 | ;; appropriate queues, or b) permanently bad display due to | |
191 | ;; bad cached images. So remember that this | |
192 | ;; is just a hack and if people don't like the behaviour, they | |
193 | ;; will most likely like the easy alternatives even less. | |
194 | ;; And at least the image cache will make the delay apparent | |
195 | ;; just once. | |
7840ced1 GM |
196 | (gs-set-ghostview-window-prop frame spec img-width img-height) |
197 | (gs-set-ghostview-colors-window-prop frame pixel-colors) | |
198 | (setenv "GHOSTVIEW" window-and-pixmap-id) | |
199 | (setq gs (apply 'start-process "gs" "*GS*" gs-program | |
200 | (gs-options gs-device file))) | |
1a597f4f | 201 | (set-process-query-on-exit-flag gs nil) |
7840ced1 GM |
202 | gs) |
203 | nil)) | |
204 | ||
205 | ||
206 | ;(defun gs-put-tiger () | |
207 | ; (let* ((ps-file "/usr/local/share/ghostscript/5.10/examples/tiger.ps") | |
eabac825 | 208 | ; (spec `(image :type postscript |
7840ced1 GM |
209 | ; :pt-width 200 :pt-height 200 |
210 | ; :bounding-box (22 171 567 738) | |
211 | ; :file ,ps-file))) | |
212 | ; (put-text-property 1 2 'display spec))) | |
e95b0c08 | 213 | ; |
7840ced1 GM |
214 | |
215 | (provide 'gs) | |
216 | ||
cbee283d | 217 | ;; arch-tag: 06ab51b8-4932-4cfe-9f60-b924a8edb3f0 |
55535639 | 218 | ;;; gs.el ends here |