1 ;;;; ps.scm -- implement Scheme output routines for PostScript
3 ;;;; source file of the GNU LilyPond music typesetter
5 ;;;; (c) 1998--2004 Jan Nieuwenhuizen <janneke@gnu.org>
6 ;;;; Han-Wen Nienhuys <hanwen@cs.uu.nl>
9 (debug-enable 'backtrace)
11 (define-module (scm output-ps))
12 (define this-module (current-module))
21 ;;; Lily output interface --- cleanup and docme
23 ;; TODO: fucks up if outputting strings with parentheses.
26 (define-public (ps-output-expression expr port)
27 (display (expression->string expr) port))
29 (define (expression->string expr)
30 (eval expr this-module))
34 ;; alist containing fontname -> fontcommand assoc (both strings)
35 (define font-name-alist '())
38 ;; Interface functions
39 (define (beam width slope thick blot)
41 (numbers->string (list slope width thick blot)) " draw_beam" ))
44 (define (bezier-sandwich l thick)
46 (apply string-append (map number-pair->string l))
47 (ly:number->string thick)
48 " draw_bezier_sandwich "))
50 (define (bracket arch_angle arch_width arch_height height arch_thick thick)
53 (list arch_angle arch_width arch_height height arch_thick thick))
56 (define (symmetric-x-triangle thick w h)
58 (numbers->string (list h w thick))
59 " draw_symmetric_x_triangle"))
64 "(\\" (inexact->string i 8) ") show " ))
68 (string-append "% " s "\n"))
71 (define (dashed-line thick on off dx dy)
73 (ly:number->string dx)
75 (ly:number->string dy)
77 (ly:number->string thick)
79 (ly:number->string on)
81 (ly:number->string off)
82 " ] 0 draw_dashed_line"))
84 ;; what the heck is this interface ?
85 (define (dashed-slur thick dash l)
87 (apply string-append (map number-pair->string l))
88 (ly:number->string thick)
90 (ly:number->string dash)
93 (ly:number->string (* 10 thick))
94 " ] 0 draw_dashed_slur"))
96 (define lily-traced-cm-fonts
119 (define (define-fonts internal-external-name-mag-pairs)
121 (define (font-load-command name-mag command)
123 ;; frobnicate NAME to jibe with external definitions.
124 (define (possibly-capitalize-font-name name)
126 ((and (equal? (substring name 0 2) "cm")
127 (not (member name lily-traced-cm-fonts)))
129 ;; huh, how is this supposed to work?
130 ;;(string-upcase name)
132 (string-append name ".pfb"))
134 ((equal? (substring name 0 4) "feta")
135 (regexp-substitute/global #f "feta([a-z-]*)([0-9]+)" name 'pre "GNU-LilyPond-feta" 1 "-" 2 'post))
138 ;;(format (current-error-port) "DEFINE-FONTS: ~S\n" internal-external-name-mag-pairs)
143 ;; Ugh, the Bluesky type1 fonts for computer modern use capitalized
144 ;; postscript font names.
145 (possibly-capitalize-font-name (car name-mag))
147 "20 " (ly:number->string (cdr name-mag)) " mul "
148 "output-scale div scalefont setfont } bind def "
151 (define (ps-encoded-fontswitch name-mag-pair)
152 (let* ((key (car name-mag-pair))
153 (value (cdr name-mag-pair)))
156 (string-append "lilyfont"
159 (number->string (cdr value)))))))
161 (set! font-name-alist (map ps-encoded-fontswitch
162 internal-external-name-mag-pairs))
165 (map (lambda (x) (font-load-command (car x) (cdr x)))
166 (map cdr font-name-alist))))
168 (define (define-origin file line col) "")
170 (define (dot x y radius)
174 (list x y radius)) " draw_dot"))
176 (define (zigzag-line centre? zzw zzh thick dx dy)
178 (if centre? "true" "false")
180 (ly:number->string zzw)
182 (ly:number->string zzh)
184 (ly:number->string thick)
186 (ly:number->string dx)
188 (ly:number->string dy)
189 " draw_zigzag_line "))
191 (define (draw-line thick x1 y1 x2 y2)
195 (ly:number->string thick)
197 (ly:number->string x1)
199 (ly:number->string y1)
201 (ly:number->string x2)
203 (ly:number->string y2)
206 (define (polygon points blotdiameter)
209 (numbers->string points)
210 (ly:number->string (/ (length points) 2))
211 (ly:number->string blotdiameter)
215 "\nend-lilypond-output\n")
217 (define (ez-ball ch letter-col ball-col)
220 (numbers->string (list letter-col ball-col))
221 " /Helvetica-Bold " ;; ugh
224 (define (filledbox breapth width depth height)
225 (string-append (numbers->string (list breapth width depth height))
228 (define (horizontal-line x1 x2 th)
229 (draw-line th x1 0 x2 0))
231 (define (fontify name-mag-pair exp)
233 (define (select-font name-mag-pair)
234 (let ((c (assoc name-mag-pair font-name-alist)))
237 (string-append " " (cddr c) " ")
240 (format "Programming error: No such font: ~S" name-mag-pair))
242 (display "FAILED\n" (current-error-port))
243 (if #f ;(pair? name-mag-pair))
244 (display (object-type (car name-mag-pair)) (current-error-port))
245 (write name-mag-pair (current-error-port)))
246 (if #f ; (pair? font-name-alist)
248 (object-type (caaar font-name-alist)) (current-error-port))
249 (write font-name-alist (current-error-port)))
251 ;; (format #f "\n%FAILED: (select-font ~S)\n" name-mag-pair))
254 (string-append (select-font name-mag-pair) exp))
256 (define (header creator generate)
259 "%%Creator: " creator generate "\n"))
263 ;; URG: now we can't use scm output without Lily
264 (ly:gulp-file "lilyponddefs.ps")
265 "{exch pop //systemdict /run get exec}\n\n"
266 (ly:gulp-file "music-drawing-routines.ps")
267 "{ exch pop //systemdict /run get exec }\n\n"
268 ;; ps-testing wreaks havoc when used with lilypond-book.
269 ;; -- is this still true with new modules system?
270 ;; (if (defined? 'ps-testing) "\n /testing true def" "")
271 ; "\n /testing true def"
274 (define (lily-def key val)
275 (let ((prefix "lilypondpaper"))
277 (substring key 0 (min (string-length prefix) (string-length key)))
279 (string-append "/" key " {" val "} bind def\n")
280 (string-append "/" key " (" val ") def\n"))))
282 (define (no-origin) "")
284 (define (placebox x y s)
286 (ly:number->string x) " " (ly:number->string y) " {" s "} place-box\n"))
288 (define (repeat-slash wid slope thick)
290 (numbers->string (list wid slope thick))
291 " draw_repeat_slash"))
293 (define (round-filled-box x y width height blotdiam)
297 (list x y width height blotdiam)) " draw_round_box"))
299 (define (start-system width height)
301 "\n" (ly:number->string height)
304 "set-ps-scale-to-lily-scale\n"
307 (if (pair? header-stencils)
308 (let ((s (output-stencils header-stencils)))
309 (set! header-stencils '())
310 (string-append s (stop-system) (start-system width height)))
313 (define (stem breapth width depth height)
315 (numbers->string (list breapth width depth height))
318 (define (stop-last-system)
321 (define (stop-system)
325 (string-append "(" s ") show "))
331 (define (top-of-file)
332 (header (string-append "GNU LilyPond (" (lilypond-version) "), ")
333 (strftime "%c" (localtime (current-time))))
337 (string-append "Engraved by LilyPond (" (lilypond-version) ")")))
339 (define (output-paper-def pd)
344 (let ((val (variable-ref var))
345 (key (symbol->string sym)))
349 (ps-string-def "lilypondpaper" sym val))
351 (ps-number-def "lilypondpaper" sym
354 (number->string (exact->inexact val)))))
357 (ly:output-def-scope pd))))
360 (define (ps-string-def a b c)
361 (string-append "/" a (symbol->string b) " (" c ") def\n"))
363 (define (ps-number-def a b c)
364 (string-append "/" a (symbol->string b) " " c " def\n"))
367 (define (output-scopes paper scopes fields basename)
369 ;; FIXME: customise/generate these
370 (let ((props '((;;(linewidth . 120)
371 (font-family . roman)
374 (font-series . medium)
376 (font-shape . upright)
379 (define (output-scope scope)
384 (let ((val (variable-ref var))
385 (tex-key (symbol->string sym)))
387 (if (memq sym fields)
388 (header-to-file basename sym val))
391 ;; define strings, for /make-lilypond-title to pick up
392 ((string? val) (ps-string-def "lilypond" sym val))
394 ;; generate stencil from markup
395 ((markup? val) (set! header-stencils
396 (append header-stencils
399 (interpret-markup paper props val)))))
402 ((number? val) (ps-number-def
403 "lilypond" sym (if (integer? val)
406 (exact->inexact val)))))
411 (apply string-append (map output-scope scopes)))))
413 (define (offset-add a b)
414 (cons (+ (car a) (car b))
415 (+ (cdr a) (cdr b))))
417 (define header-stencils '())
419 (define (output-stencils lst)
421 (map (lambda (x) (stencil->string x '(10 . -10))) lst)))
423 ;; hmm, looks like recursing call is always last statement, does guile
425 (define (stencil->string expr o)
427 (let ((head (car expr)))
429 ((ly:input-location? head)
430 (string-append (apply define-origin (ly:input-location head))
431 (stencil->string (cadr expr) o)))
432 ((eq? head 'no-origin)
433 (string-append (expression->string head)
434 (stencil->string (cadr expr) o)))
435 ((eq? head 'translate-stencil)
436 (stencil->string (caddr expr) (offset-add o (cadr expr))))
437 ((eq? head 'combine-stencil)
438 (string-append (stencil->string (cadr expr) o)
439 (stencil->string (caddr expr) o)))
441 (placebox (car o) (cdr o) (expression->string expr)))))