2000-05-25 Michael Kifer <kifer@cs.sunysb.edu>
[emacs.git] / lisp / gs.el
blobcbce2002cf47ad6e90f0df7522e7332c1d3bb0bd
1 ;;; gs.el --- interface to Ghostscript
3 ;; Copyright (C) 1998 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 "-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.")
50 (defun gs-replace-in-string (string find repl)
51 "Return STRING with all occurrences of FIND replaced by REPL.
52 FIND is a regular expression."
53 (while (string-match find string)
54 (setq string (replace-match repl nil t string)))
55 string)
58 (defun gs-options (device file)
59 "Return a list of command line options with place-holders replaced.
60 DEVICE is the value to substitute for the place-holder `<device>',
61 FILE is the value to substitute for the place-holder `<file>'."
62 (mapcar #'(lambda (option)
63 (setq option (gs-replace-in-string option "<device>" device)
64 option (gs-replace-in-string option "<file>" file)))
65 gs-options))
68 ;; The GHOSTVIEW property (taken from gv 3.5.8).
69 ;;
70 ;; Type:
72 ;; STRING
73 ;;
74 ;; Parameters:
75 ;;
76 ;; BPIXMAP ORIENT LLX LLY URX URY XDPI YDPI [LEFT BOTTOM TOP RIGHT]
77 ;;
78 ;; Scanf format: "%d %d %d %d %d %d %f %f %d %d %d %d"
79 ;;
80 ;; Explanation of parameters:
81 ;;
82 ;; BPIXMAP: pixmap id of the backing pixmap for the window. If no
83 ;; pixmap is to be used, this parameter should be zero. This
84 ;; parameter must be zero when drawing on a pixmap.
85 ;;
86 ;; ORIENT: orientation of the page. The number represents clockwise
87 ;; rotation of the paper in degrees. Permitted values are 0, 90, 180,
88 ;; 270.
89 ;;
90 ;; LLX, LLY, URX, URY: Bounding box of the drawable. The bounding box
91 ;; is specified in PostScript points in default user coordinates.
92 ;;
93 ;; XDPI, YDPI: Resolution of window. (This can be derived from the
94 ;; other parameters, but not without roundoff error. These values are
95 ;; included to avoid this error.)
96 ;;
97 ;; LEFT, BOTTOM, TOP, RIGHT: (optional) Margins around the window.
98 ;; The margins extend the imageable area beyond the boundaries of the
99 ;; window. This is primarily used for popup zoom windows. I have
100 ;; encountered several instances of PostScript programs that position
101 ;; themselves with respect to the imageable area. The margins are
102 ;; specified in PostScript points. If omitted, the margins are
103 ;; assumed to be 0.
105 (defun gs-width-in-pt (frame pixel-width)
106 "Return, on FRAME, pixel width PIXEL-WIDTH tranlated to pt."
107 (let ((mm (* (float pixel-width)
108 (/ (float (x-display-mm-width frame))
109 (float (x-display-pixel-width frame))))))
110 (/ (* 25.4 mm) 72.0)))
113 (defun gs-height-in-pt (frame pixel-height)
114 "Return, on FRAME, pixel height PIXEL-HEIGHT tranlated to pt."
115 (let ((mm (* (float pixel-height)
116 (/ (float (x-display-mm-height frame))
117 (float (x-display-pixel-height frame))))))
118 (/ (* 25.4 mm) 72.0)))
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 (nth 0 box))
128 (lly (nth 1 box))
129 (urx (nth 2 box))
130 (ury (nth 3 box))
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)))
144 (defun gs-set-ghostview-colors-window-prop (frame pixel-colors)
145 "Set the `GHOSTVIEW_COLORS' environment variable depending on FRAME."
146 (let ((mode (cond ((x-display-color-p frame) "Color")
147 ((x-display-grayscale-p frame) "Grayscale")
148 (t "Monochrome"))))
149 (x-change-window-property "GHOSTVIEW_COLORS"
150 (format "%s %s" mode pixel-colors))))
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 (gs-set-ghostview-window-prop frame spec img-width img-height)
165 (gs-set-ghostview-colors-window-prop frame pixel-colors)
166 (setenv "GHOSTVIEW" window-and-pixmap-id)
167 (setq gs (apply 'start-process "gs" "*GS*" gs-program
168 (gs-options gs-device file)))
169 (process-kill-without-query gs)
171 nil))
174 ;(defun gs-put-tiger ()
175 ; (let* ((ps-file "/usr/local/share/ghostscript/5.10/examples/tiger.ps")
176 ; (spec `(image :type postscript
177 ; :pt-width 200 :pt-height 200
178 ; :bounding-box (22 171 567 738)
179 ; :file ,ps-file)))
180 ; (put-text-property 1 2 'display spec)))
183 (provide 'gs)
185 ;; gs.el ends here.