1 ;;;; output-ps.scm -- implement Scheme output interface for PostScript
3 ;;;; source file of the GNU LilyPond music typesetter
5 ;;;; (c) 1998--2006 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
51 ;;; helper functions, not part of output interface
55 ;; ice-9 format uses a lot of memory
56 ;; using simple-format almost halves lilypond cell usage
59 (if (or (nan? num) (inf? num))
61 (ly:warning (_ "Found infinity or nan in output. Substituting 0.0"))
62 (if (ly:get-option 'strict-infinity-checking)
65 (ly:number->string num)))
67 (define (number-pair->string4 numpair)
68 (ly:format "~4l" numpair))
70 (define (numbers->string4 numlist)
71 (string-join (map str4 numlist) " "))
74 (define-public (ps-string-def prefix key val)
75 (ly:format "/ ~a~a (~a) def\n"
78 (escape-parentheses val)))
80 (define (ps-number-def prefix key val)
81 (let ((s (if (integer? val)
82 (ly:number->string val)
83 (ly:number->string (exact->inexact val)))))
84 (ly:format "/~a~a ~a def\n"
86 (symbol->string key) s)))
90 ;;; Lily output interface, PostScript implementation --- cleanup and docme
94 (define (bezier-sandwich lst thick)
95 (ly:format "~l ~4f draw_bezier_sandwich"
96 (map number-pair->string4 lst)
100 (ly:format "~a (\\~a) show"
101 (ps-font-command font)
102 (ly:inexact->string i 8)))
104 (define (circle radius thick fill)
106 "~a ~4f ~4f draw_circle"
112 (define (dashed-line thick on off dx dy phase)
113 (ly:format "~4f ~4f ~4f [ ~4f ~4f ] ~4f draw_dashed_line"
121 ;; what the heck is this interface ?
122 (define (dashed-slur thick on off l)
123 (ly:format "~l ~4f [ ~4f ~4f ] 0 draw_dashed_slur"
124 (let ((control-points (append (cddr l) (list (car l) (cadr l)))))
125 (map number-pair->string4 control-points))
130 (define (dot x y radius)
131 (ly:format " ~4l draw_dot" (list radius x y)))
133 (define (draw-line thick x1 y1 x2 y2)
134 (ly:format "~4f ~4f ~4f ~4f ~4f draw_line"
138 (define (embedded-ps string)
141 (define (glyph-string postscript-font-name
146 (define (glyph-spec w x y g)
147 (let ((prefix (if (string? g) "/" "")))
148 (ly:format "~4f ~4f ~a~a"
154 "/~a /CIDFont findresource ~a output-scale div scalefont setfont
158 "/~a ~a output-scale div selectfont
163 (string-join (map (lambda (x) (apply glyph-spec x))
164 (reverse w-x-y-named-glyphs)) "\n")
165 (length w-x-y-named-glyphs)))
168 (define (grob-cause offset grob)
169 (let* ((cause (ly:grob-property grob 'cause))
170 (music-origin (if (ly:stream-event? cause)
171 (ly:event-property cause 'origin))))
172 (if (not (ly:input-location? music-origin))
174 (let* ((location (ly:input-file-line-char-column music-origin))
175 (raw-file (car location))
176 (file (if (is-absolute? raw-file)
178 (string-append (ly-getcwd) "/" raw-file)))
179 (x-ext (ly:grob-extent grob grob X))
180 (y-ext (ly:grob-extent grob grob Y)))
182 (if (and (< 0 (interval-length x-ext))
183 (< 0 (interval-length y-ext)))
184 (ly:format "~4f ~4f ~4f ~4f (textedit://~a:~a:~a:~a) mark_URI\n"
185 (+ (car offset) (car x-ext))
186 (+ (cdr offset) (car y-ext))
187 (+ (car offset) (cdr x-ext))
188 (+ (cdr offset) (cdr y-ext))
193 ;; backslash is interpreted by GS.
194 (ly:string-substitute "\\" "/"
195 (ly:string-substitute " " "%20" file))
201 (define (lily-def key val)
202 (let ((prefix "lilypondlayout"))
204 (substring key 0 (min (string-length prefix) (string-length key)))
206 (format "/~a { ~a } bind def\n" key val)
207 (format "/~a (~a) def\n" key val))))
209 (define (named-glyph font glyph)
210 (ly:format "~a /~a glyphshow " ;;Why is there a space at the end?
211 (ps-font-command font)
217 (define (placebox x y s)
222 (define (polygon points blot-diameter filled?)
223 (ly:format "~a ~4l ~a ~4f draw_polygon"
224 (if filled? "true" "false")
226 (- (/ (length points) 2) 1)
229 (define (repeat-slash width slope beam-thickness)
230 (define (euclidean-length x y)
231 (sqrt (+ (* x x) (* y y))))
233 (let ((x-width (euclidean-length beam-thickness (/ beam-thickness slope)))
234 (height (* width slope)))
235 (ly:format "~4l draw_repeat_slash"
236 (list x-width width height))))
239 (define (round-filled-box left right bottom top blotdiam)
240 (let* ((halfblot (/ blotdiam 2))
241 (x (- halfblot left))
242 (width (- right (+ halfblot x)))
243 (y (- halfblot bottom))
244 (height (- top (+ halfblot y))))
245 (ly:format "~4l draw_round_box"
246 (list width height x y blotdiam))))
248 ;; save current color on stack and set new color
249 (define (setcolor r g b)
250 (ly:format "gsave ~4l setrgbcolor\n"
253 ;; restore color from stack
254 (define (resetcolor) "grestore \n")
256 ;; rotation around given point
257 (define (setrotation ang x y)
258 (ly:format "gsave ~4l translate ~a rotate ~4l translate\n"
261 (list (* -1 x) (* -1 y))))
263 (define (resetrotation ang x y)
267 (define (text font s)
268 ;; (ly:warning (_ "TEXT backend-command encountered in Pango backend"))
269 ;; (ly:warning (_ "Arguments: ~a ~a"" font str))
271 (let* ((space-length (cdar (ly:text-dimension font " ")))
272 (space-move (string-append (number->string space-length)
273 ;; how much precision do we need here?
275 (out-vec (decode-byte-string s)))
278 (ps-font-command font) " "
286 (string-append "/" (symbol->string sym) " glyphshow")))
292 (define (url-link url x y)
293 (ly:format "~a ~a ~a ~a (~a) mark_URI"
300 (define (utf-8-string pango-font-description string)
301 (ly:warning (_ "utf-8-string encountered in PS backend")))
304 (define (zigzag-line is-center zzw zzh thick dx dy)
305 (ly:format "~a ~4f ~4f ~4f 0 0 ~4f ~4f draw_zigzag_line"
306 (if is-center "true" "false")
314 (define (path thickness exps)
315 (define (convert-path-exps exps)
322 ((memq head '(rmoveto rlineto lineto moveto)) 2)
323 ((memq head '(rcurveto curveto)) 6)
325 (args (take rest arity))
328 ;; WARNING: this is a vulnerability: a user can output arbitrary PS code here.
333 (convert-path-exps (drop rest arity))))
338 "1 setlinecap ~a setlinewidth\n~l stroke"
340 (convert-path-exps exps) ))