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
57 (define format simple-format)
59 (define (escape-parentheses s)
60 (regexp-substitute/global #f "(^|[^\\])([\\(\\)])" s 'pre 1 "\\" 2 'post))
63 (if (or (nan? num) (inf? num))
65 (ly:warning (_ "Found infinity or nan in output. Substituting 0.0"))
66 (if (ly:get-option 'strict-infinity-checking)
69 (ly:number->string num)))
71 (define (number-pair->string4 numpair)
72 (string-append (str4 (car numpair))
74 (str4 (cdr numpair))))
76 (define (numbers->string4 numlist)
77 (string-join (map str4 numlist) " "))
80 (define-public (ps-string-def prefix key val)
81 (format #f "/ ~a~a (~a) def\n"
84 (escape-parentheses val)))
86 (define (ps-number-def prefix key val)
87 (let ((s (if (integer? val)
88 (ly:number->string val)
89 (ly:number->string (exact->inexact val)))))
90 (format #f "/~a~a ~a def\n"
92 (symbol->string key) s)))
96 ;;; Lily output interface, PostScript implementation --- cleanup and docme
100 (define (bezier-sandwich lst thick)
101 (format #f "~a ~a draw_bezier_sandwich"
102 (string-join (map number-pair->string4 lst) " ")
105 (define (char font i)
106 (format #f "~a (\\~a) show"
107 (ps-font-command font)
108 (ly:inexact->string i 8)))
110 (define (circle radius thick fill)
112 "~a ~a ~a draw_circle"
116 (str4 radius) (str4 thick)))
118 (define (dashed-line thick on off dx dy phase)
119 (format #f "~a ~a ~a [ ~a ~a ] ~a draw_dashed_line"
129 ;; what the heck is this interface ?
130 (define (dashed-slur thick on off l)
131 (format #f "~a ~a [ ~a ~a ] 0 draw_dashed_slur"
132 (let ((control-points (append (cddr l) (list (car l) (cadr l)))))
133 (string-join (map number-pair->string4 control-points) " "))
138 (define (dot x y radius)
139 (format #f " ~a draw_dot"
140 (numbers->string4 (list radius x y))))
142 (define (draw-line thick x1 y1 x2 y2)
143 (format #f "~a ~a ~a ~a ~a draw_line"
150 (define (embedded-ps string)
153 (define (glyph-string postscript-font-name
158 (define (glyph-spec w x y g)
159 (let ((prefix (if (string? g) "/" "")))
160 (format #f "~a ~a ~a~a"
167 "/~a /CIDFont findresource ~a output-scale div scalefont setfont
171 "/~a ~a output-scale div selectfont
176 (string-join (map (lambda (x) (apply glyph-spec x))
177 (reverse w-x-y-named-glyphs)) "\n")
178 (length w-x-y-named-glyphs)))
181 (define (grob-cause offset grob)
182 (let* ((cause (ly:grob-property grob 'cause))
183 (music-origin (if (ly:stream-event? cause)
184 (ly:event-property cause 'origin))))
185 (if (not (ly:input-location? music-origin))
187 (let* ((location (ly:input-file-line-char-column music-origin))
188 (raw-file (car location))
189 (file (if (is-absolute? raw-file)
191 (string-append (ly-getcwd) "/" raw-file)))
192 (x-ext (ly:grob-extent grob grob X))
193 (y-ext (ly:grob-extent grob grob Y)))
195 (if (and (< 0 (interval-length x-ext))
196 (< 0 (interval-length y-ext)))
197 (format #f "~a ~a ~a ~a (textedit://~a:~a:~a:~a) mark_URI\n"
198 (str4 (+ (car offset) (car x-ext)))
199 (str4 (+ (cdr offset) (car y-ext)))
200 (str4 (+ (car offset) (cdr x-ext)))
201 (str4 (+ (cdr offset) (cdr y-ext)))
206 ;; backslash is interpreted by GS.
207 (ly:string-substitute "\\" "/"
208 (ly:string-substitute " " "%20" file))
214 (define (lily-def key val)
215 (let ((prefix "lilypondlayout"))
217 (substring key 0 (min (string-length prefix) (string-length key)))
219 (format "/~a { ~a } bind def\n" key val)
220 (format "/~a (~a) def\n" key val))))
222 (define (named-glyph font glyph)
223 (format #f "~a /~a glyphshow " ;;Why is there a space at the end?
224 (ps-font-command font)
230 (define (placebox x y s)
238 (define (polygon points blot-diameter filled?)
239 (format #f "~a ~a ~a ~a draw_polygon"
240 (if filled? "true" "false")
241 (numbers->string4 points)
242 (number->string (- (/ (length points) 2) 1))
243 (str4 blot-diameter)))
245 (define (repeat-slash width slope beam-thickness)
246 (define (euclidean-length x y)
247 (sqrt (+ (* x x) (* y y))))
249 (let ((x-width (euclidean-length beam-thickness (/ beam-thickness slope)))
250 (height (* width slope)))
251 (format #f "~a draw_repeat_slash"
252 (numbers->string4 (list x-width width height)))))
255 (define (round-filled-box left right bottom top blotdiam)
256 (let* ((halfblot (/ blotdiam 2))
257 (x (- halfblot left))
258 (width (- right (+ halfblot x)))
259 (y (- halfblot bottom))
260 (height (- top (+ halfblot y))))
261 (format #f "~a draw_round_box"
263 (list width height x y blotdiam)))))
265 ;; save current color on stack and set new color
266 (define (setcolor r g b)
267 (format #f "gsave ~a setrgbcolor\n"
268 (numbers->string4 (list r g b))))
270 ;; restore color from stack
271 (define (resetcolor) "grestore \n")
273 ;; rotation around given point
274 (define (setrotation ang x y)
275 (format #f "gsave ~a translate ~a rotate ~a translate\n"
276 (numbers->string4 (list x y))
278 (numbers->string4 (list (* -1 x) (* -1 y)))))
280 (define (resetrotation ang x y)
284 (define (text font s)
285 ;; (ly:warning (_ "TEXT backend-command encountered in Pango backend"))
286 ;; (ly:warning (_ "Arguments: ~a ~a"" font str))
288 (let* ((space-length (cdar (ly:text-dimension font " ")))
289 (space-move (string-append (number->string space-length)
290 ;; how much precision do we need here?
292 (out-vec (decode-byte-string s)))
295 (ps-font-command font) " "
303 (string-append "/" (symbol->string sym) " glyphshow")))
309 (define (url-link url x y)
310 (format #f "~a ~a ~a ~a (~a) mark_URI"
317 (define (utf-8-string pango-font-description string)
318 (ly:warning (_ "utf-8-string encountered in PS backend")))
321 (define (zigzag-line centre? zzw zzh thick dx dy)
322 (format #f "~a ~a ~a ~a 0 0 ~a ~a draw_zigzag_line"
323 (if centre? "true" "false")
331 (define (path thickness exps)
332 (define (convert-path-exps exps)
339 ((memq head '(rmoveto rlineto lineto moveto)) 2)
340 ((memq head '(rcurveto curveto)) 6)
342 (args (take rest arity))
345 ;; WARNING: this is a vulnerability: a user can output arbitrary PS code here.
348 (string-join (map (lambda (x) (format #f "~a " x)) args) " ")
350 (convert-path-exps (drop rest arity))))
355 "1 setlinecap ~a setlinewidth\n~a stroke"
357 (string-join (convert-path-exps exps) " ")))