1 ;;;; output-ps.scm -- implement Scheme output interface for PostScript
3 ;;;; source file of the GNU LilyPond music typesetter
5 ;;;; (c) 1998--2007 Jan Nieuwenhuizen <janneke@gnu.org>
6 ;;;; Han-Wen Nienhuys <hanwen@xs4all.nl>
8 ;;;; Note: currently misused as testbed for titles with markup, see
9 ;;;; input/test/title-markup.ly
12 ;;;; * %% Papersize in (header ...)
13 ;;;; * text setting, kerning.
14 ;;;; * document output-interface
16 (define-module (scm output-ps)
19 ;; JUNK this -- see lily.scm: ly:all-output-backend-commands
53 ;;; helper functions, not part of output interface
57 ;; ice-9 format uses a lot of memory
58 ;; using simple-format almost halves lilypond cell usage
61 (if (or (nan? num) (inf? num))
63 (ly:warning (_ "Found infinity or nan in output. Substituting 0.0"))
64 (if (ly:get-option 'strict-infinity-checking)
67 (ly:number->string num)))
69 (define (number-pair->string4 numpair)
70 (ly:format "~4l" numpair))
73 ;;; Lily output interface, PostScript implementation --- cleanup and docme
77 (define (bezier-sandwich lst thick)
78 (ly:format "~l ~4f draw_bezier_sandwich"
79 (map number-pair->string4 lst)
83 (ly:format "~a (\\~a) show"
84 (ps-font-command font)
85 (ly:inexact->string i 8)))
87 (define (circle radius thick fill)
89 "~a ~4f ~4f draw_circle"
95 (define (dashed-line thick on off dx dy phase)
96 (ly:format "~4f ~4f ~4f [ ~4f ~4f ] ~4f draw_dashed_line"
104 ;; what the heck is this interface ?
105 (define (dashed-slur thick on off l)
106 (ly:format "~l ~4f [ ~4f ~4f ] 0 draw_dashed_slur"
107 (let ((control-points (append (cddr l) (list (car l) (cadr l)))))
108 (map number-pair->string4 control-points))
113 (define (dot x y radius)
114 (ly:format " ~4l draw_dot" (list radius x y)))
116 (define (draw-line thick x1 y1 x2 y2)
117 (ly:format "~4f ~4f ~4f ~4f ~4f draw_line"
121 (define (ellipse x-radius y-radius thick fill)
123 "~a ~4f ~4f ~4f draw_ellipse"
127 x-radius y-radius thick))
129 (define (embedded-ps string)
132 (define (glyph-string postscript-font-name
137 (define (glyph-spec w x y g)
138 (let ((prefix (if (string? g) "/" "")))
139 (ly:format "~4f ~4f ~a~a"
145 "/~a /CIDFont findresource ~a output-scale div scalefont setfont
149 "/~a ~a output-scale div selectfont
154 (string-join (map (lambda (x) (apply glyph-spec x))
155 (reverse w-x-y-named-glyphs)) "\n")
156 (length w-x-y-named-glyphs)))
159 (define (grob-cause offset grob)
160 (if (ly:get-option 'point-and-click)
161 (let* ((cause (ly:grob-property grob 'cause))
162 (music-origin (if (ly:stream-event? cause)
163 (ly:event-property cause 'origin))))
164 (if (ly:input-location? music-origin)
165 (let* ((location (ly:input-file-line-char-column music-origin))
166 (raw-file (car location))
167 (file (if (is-absolute? raw-file)
169 (string-append (ly-getcwd) "/" raw-file)))
170 (x-ext (ly:grob-extent grob grob X))
171 (y-ext (ly:grob-extent grob grob Y)))
173 (if (and (< 0 (interval-length x-ext))
174 (< 0 (interval-length y-ext)))
175 (ly:format "~4f ~4f ~4f ~4f (textedit://~a:~a:~a:~a) mark_URI\n"
176 (+ (car offset) (car x-ext))
177 (+ (cdr offset) (car y-ext))
178 (+ (car offset) (cdr x-ext))
179 (+ (cdr offset) (cdr y-ext))
184 ;; backslash is interpreted by GS.
185 (ly:string-substitute "\\" "/"
186 (ly:string-substitute " " "%20" file))
194 (define (named-glyph font glyph)
195 (ly:format "~a /~a glyphshow " ;;Why is there a space at the end?
196 (ps-font-command font)
202 (define (oval x-radius y-radius thick fill)
204 "~a ~4f ~4f ~4f draw_oval"
208 x-radius y-radius thick))
210 (define (placebox x y s)
215 (define (polygon points blot-diameter filled?)
216 (ly:format "~a ~4l ~a ~4f draw_polygon"
217 (if filled? "true" "false")
219 (- (/ (length points) 2) 1)
222 (define (repeat-slash width slope beam-thickness)
223 (define (euclidean-length x y)
224 (sqrt (+ (* x x) (* y y))))
226 (let ((x-width (euclidean-length beam-thickness (/ beam-thickness slope)))
227 (height (* width slope)))
228 (ly:format "~4l draw_repeat_slash"
229 (list x-width width height))))
232 (define (round-filled-box left right bottom top blotdiam)
233 (let* ((halfblot (/ blotdiam 2))
234 (x (- halfblot left))
235 (width (- right (+ halfblot x)))
236 (y (- halfblot bottom))
237 (height (- top (+ halfblot y))))
238 (ly:format "~4l draw_round_box"
239 (list width height x y blotdiam))))
241 ;; save current color on stack and set new color
242 (define (setcolor r g b)
243 (ly:format "gsave ~4l setrgbcolor\n"
246 ;; restore color from stack
247 (define (resetcolor) "grestore \n")
249 ;; rotation around given point
250 (define (setrotation ang x y)
251 (ly:format "gsave ~4l translate ~a rotate ~4l translate\n"
254 (list (* -1 x) (* -1 y))))
256 (define (resetrotation ang x y)
260 (define (text font s)
261 ;; (ly:warning (_ "TEXT backend-command encountered in Pango backend"))
262 ;; (ly:warning (_ "Arguments: ~a ~a"" font str))
264 (let* ((space-length (cdar (ly:text-dimension font " ")))
265 (space-move (string-append (number->string space-length)
266 ;; how much precision do we need here?
268 (out-vec (decode-byte-string s)))
271 (ps-font-command font) " "
279 (string-append "/" (symbol->string sym) " glyphshow")))
285 (define (url-link url x y)
286 (ly:format "~a ~a currentpoint vector_add ~a ~a currentpoint vector_add (~a) mark_URI"
293 (define (utf-8-string pango-font-description string)
294 (ly:warning (_ "utf-8-string encountered in PS backend")))
296 (define (path thickness exps)
297 (define (convert-path-exps exps)
304 ((memq head '(rmoveto rlineto lineto moveto)) 2)
305 ((memq head '(rcurveto curveto)) 6)
307 (args (take rest arity))
310 ;; WARNING: this is a vulnerability: a user can output arbitrary PS code here.
315 (convert-path-exps (drop rest arity))))
320 "1 setlinecap ~a setlinewidth\n~l stroke"
322 (convert-path-exps exps) ))