1 ;;;; output-ps.scm -- implement Scheme output interface for PostScript
3 ;;;; source file of the GNU LilyPond music typesetter
5 ;;;; (c) 1998--2009 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)
26 ;;; helper functions, not part of output interface
30 ;; ice-9 format uses a lot of memory
31 ;; using simple-format almost halves lilypond cell usage
34 (if (or (nan? num) (inf? num))
36 (ly:warning (_ "Found infinity or nan in output. Substituting 0.0"))
37 (if (ly:get-option 'strict-infinity-checking)
40 (ly:number->string num)))
42 (define (number-pair->string4 numpair)
43 (ly:format "~4l" numpair))
46 ;;; Lily output interface, PostScript implementation --- cleanup and docme
50 (define (bezier-sandwich lst thick)
51 (ly:format "~l ~4f draw_bezier_sandwich"
52 (map number-pair->string4 lst)
56 (ly:format "~a (\\~a) show"
57 (ps-font-command font)
58 (ly:inexact->string i 8)))
60 (define (circle radius thick fill)
62 "~a ~4f ~4f draw_circle"
68 (define (dashed-line thick on off dx dy phase)
69 (ly:format "~4f ~4f ~4f [ ~4f ~4f ] ~4f draw_dashed_line"
77 ;; what the heck is this interface ?
78 (define (dashed-slur thick on off l)
79 (ly:format "~l ~4f [ ~4f ~4f ] 0 draw_dashed_slur"
80 (let ((control-points (append (cddr l) (list (car l) (cadr l)))))
81 (map number-pair->string4 control-points))
86 (define (dot x y radius)
87 (ly:format " ~4l draw_dot" (list radius x y)))
89 (define (draw-line thick x1 y1 x2 y2)
90 (ly:format "~4f ~4f ~4f ~4f ~4f draw_line"
94 (define (ellipse x-radius y-radius thick fill)
96 "~a ~4f ~4f ~4f draw_ellipse"
100 x-radius y-radius thick))
102 (define (embedded-ps string)
105 (define (glyph-string postscript-font-name
110 (define (glyph-spec w x y g)
111 (let ((prefix (if (string? g) "/" "")))
112 (ly:format "~4f ~4f ~4f ~a~a"
118 "/~a /CIDFont findresource ~a output-scale div scalefont setfont
122 "/~a ~a output-scale div selectfont
127 (string-join (map (lambda (x) (apply glyph-spec x))
128 (reverse w-x-y-named-glyphs)) "\n")
129 (length w-x-y-named-glyphs)))
132 (define (grob-cause offset grob)
133 (if (ly:get-option 'point-and-click)
134 (let* ((cause (ly:grob-property grob 'cause))
135 (music-origin (if (ly:stream-event? cause)
136 (ly:event-property cause 'origin))))
137 (if (ly:input-location? music-origin)
138 (let* ((location (ly:input-file-line-char-column music-origin))
139 (raw-file (car location))
140 (file (if (is-absolute? raw-file)
142 (string-append (ly-getcwd) "/" raw-file)))
143 (x-ext (ly:grob-extent grob grob X))
144 (y-ext (ly:grob-extent grob grob Y)))
146 (if (and (< 0 (interval-length x-ext))
147 (< 0 (interval-length y-ext)))
148 (ly:format "~4f ~4f ~4f ~4f (textedit://~a:~a:~a:~a) mark_URI\n"
149 (+ (car offset) (car x-ext))
150 (+ (cdr offset) (car y-ext))
151 (+ (car offset) (cdr x-ext))
152 (+ (cdr offset) (cdr y-ext))
157 ;; backslash is interpreted by GS.
158 (ly:string-substitute "\\" "/"
159 (ly:string-substitute " " "%20" file))
167 (define (named-glyph font glyph)
168 (ly:format "~a /~a glyphshow " ;;Why is there a space at the end?
169 (ps-font-command font)
175 (define (oval x-radius y-radius thick fill)
177 "~a ~4f ~4f ~4f draw_oval"
181 x-radius y-radius thick))
183 (define (placebox x y s)
184 (if (not (string-null? s))
185 (ly:format "~4f ~4f moveto ~a\n" x y s)
188 (define (polygon points blot-diameter filled?)
189 (ly:format "~a ~4l ~a ~4f draw_polygon"
190 (if filled? "true" "false")
192 (- (/ (length points) 2) 1)
195 (define (repeat-slash width slope beam-thickness)
196 (define (euclidean-length x y)
197 (sqrt (+ (* x x) (* y y))))
199 (let ((x-width (euclidean-length beam-thickness (/ beam-thickness slope)))
200 (height (* width slope)))
201 (ly:format "~4l draw_repeat_slash"
202 (list x-width width height))))
205 (define (round-filled-box left right bottom top blotdiam)
206 (let* ((halfblot (/ blotdiam 2))
207 (x (- halfblot left))
208 (width (- right (+ halfblot x)))
209 (y (- halfblot bottom))
210 (height (- top (+ halfblot y))))
211 (ly:format "~4l draw_round_box"
212 (list width height x y blotdiam))))
214 ;; save current color on stack and set new color
215 (define (setcolor r g b)
216 (ly:format "gsave ~4l setrgbcolor\n"
219 ;; restore color from stack
220 (define (resetcolor) "grestore\n")
222 ;; rotation around given point
223 (define (setrotation ang x y)
224 (ly:format "gsave ~4l translate ~a rotate ~4l translate\n"
227 (list (* -1 x) (* -1 y))))
229 (define (resetrotation ang x y)
233 (define (text font s)
234 ;; (ly:warning (_ "TEXT backend-command encountered in Pango backend"))
235 ;; (ly:warning (_ "Arguments: ~a ~a"" font str))
237 (let* ((space-length (cdar (ly:text-dimension font " ")))
238 (space-move (string-append (number->string space-length)
239 ;; how much precision do we need here?
241 (out-vec (decode-byte-string s)))
244 (ps-font-command font) " "
252 (string-append "/" (symbol->string sym) " glyphshow")))
258 (define (url-link url x y)
259 (ly:format "~a ~a currentpoint vector_add ~a ~a currentpoint vector_add (~a) mark_URI"
266 (define (path thickness exps)
267 (define (convert-path-exps exps)
274 ((memq head '(rmoveto rlineto lineto moveto)) 2)
275 ((memq head '(rcurveto curveto)) 6)
276 ((eq? head 'closepath) 0)
278 (args (take rest arity))
281 ;; WARNING: this is a vulnerability: a user can output arbitrary PS code here.
286 (convert-path-exps (drop rest arity))))
291 "gsave currentpoint translate 1 setlinecap ~a setlinewidth\n~l stroke grestore"
293 (convert-path-exps exps)))