1 ;;;; output-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))
141 ;; Ugh, the Bluesky type1 fonts for computer modern use capitalized
142 ;; postscript font names.
143 (possibly-capitalize-font-name (car name-mag))
145 "20 " (ly:number->string (cdr name-mag)) " mul "
146 "output-scale div scalefont setfont } bind def "
149 (define (ps-encoded-fontswitch name-mag-pair)
150 (let* ((key (car name-mag-pair))
151 (value (cdr name-mag-pair)))
154 (string-append "lilyfont"
157 (number->string (cdr value)))))))
159 (set! font-name-alist (map ps-encoded-fontswitch
160 internal-external-name-mag-pairs))
163 (map (lambda (x) (font-load-command (car x) (cdr x)))
164 (map cdr font-name-alist))))
166 (define (define-origin file line col) "")
168 (define (dot x y radius)
172 (list x y radius)) " draw_dot"))
174 (define (zigzag-line centre? zzw zzh thick dx dy)
176 (if centre? "true" "false")
178 (ly:number->string zzw)
180 (ly:number->string zzh)
182 (ly:number->string thick)
184 (ly:number->string dx)
186 (ly:number->string dy)
187 " draw_zigzag_line "))
189 (define (draw-line thick x1 y1 x2 y2)
193 (ly:number->string thick)
195 (ly:number->string x1)
197 (ly:number->string y1)
199 (ly:number->string x2)
201 (ly:number->string y2)
204 (define (polygon points blotdiameter)
207 (numbers->string points)
208 (ly:number->string (/ (length points) 2))
209 (ly:number->string blotdiameter)
213 "\nend-lilypond-output\n")
215 (define (ez-ball ch letter-col ball-col)
218 (numbers->string (list letter-col ball-col))
219 " /Helvetica-Bold " ;; ugh
222 (define (filledbox breapth width depth height)
223 (string-append (numbers->string (list breapth width depth height))
226 (define (horizontal-line x1 x2 th)
227 (draw-line th x1 0 x2 0))
229 (define (fontify name-mag-pair exp)
231 (define (select-font name-mag-pair)
232 (let ((c (assoc name-mag-pair font-name-alist)))
235 (string-append " " (cddr c) " ")
238 (format "Programming error: No such font: ~S" name-mag-pair))
240 (display "FAILED\n" (current-error-port))
241 (if #f ;(pair? name-mag-pair))
242 (display (object-type (car name-mag-pair)) (current-error-port))
243 (write name-mag-pair (current-error-port)))
244 (if #f ; (pair? font-name-alist)
246 (object-type (caaar font-name-alist)) (current-error-port))
247 (write font-name-alist (current-error-port)))
249 ;; (format #f "\n%FAILED: (select-font ~S)\n" name-mag-pair))
252 (string-append (select-font name-mag-pair) exp))
254 (define (header creator generate)
257 "%%Creator: " creator generate "\n"))
261 ;; URG: now we can't use scm output without Lily
262 (ly:gulp-file "lilyponddefs.ps")
263 "{exch pop //systemdict /run get exec}\n\n"
264 (ly:gulp-file "music-drawing-routines.ps")
265 "{ exch pop //systemdict /run get exec }\n\n"
266 ;; ps-testing wreaks havoc when used with lilypond-book.
267 ;; -- is this still true with new modules system?
268 ;; (if (defined? 'ps-testing) "\n /testing true def" "")
269 ; "\n /testing true def"
272 (define (lily-def key val)
273 (let ((prefix "lilypondpaper"))
275 (substring key 0 (min (string-length prefix) (string-length key)))
277 (string-append "/" key " {" val "} bind def\n")
278 (string-append "/" key " (" val ") def\n"))))
280 (define (no-origin) "")
282 (define (placebox x y s)
284 (ly:number->string x) " " (ly:number->string y) " {" s "} place-box\n"))
286 (define (repeat-slash wid slope thick)
288 (numbers->string (list wid slope thick))
289 " draw_repeat_slash"))
291 (define (round-filled-box x y width height blotdiam)
295 (list x y width height blotdiam)) " draw_round_box"))
297 (define (start-system width height)
299 "\n" (ly:number->string height)
302 "set-ps-scale-to-lily-scale\n"))
304 (define (stem breapth width depth height)
306 (numbers->string (list breapth width depth height))
309 (define (stop-last-system)
312 (define (stop-system)
316 (string-append "(" s ") show "))
322 (define (top-of-file)
323 (header (string-append "GNU LilyPond (" (lilypond-version) "), ")
324 (strftime "%c" (localtime (current-time))))
328 (string-append "Engraved by LilyPond (" (lilypond-version) ")")))
330 (define (output-paper-def pd)
335 (let ((val (variable-ref var))
336 (key (symbol->string sym)))
340 (ps-string-def "lilypondpaper" sym val))
342 (ps-number-def "lilypondpaper" sym
345 (number->string (exact->inexact val)))))
348 (ly:output-def-scope pd))))
351 (define (ps-string-def a b c)
352 (string-append "/" a (symbol->string b) " (" c ") def\n"))
354 (define (ps-number-def a b c)
355 (string-append "/" a (symbol->string b) " " c " def\n"))
358 (define (output-scopes paper scopes fields basename)
360 ;; FIXME: customise/generate these
361 (let ((props '(((font-family . roman)
364 (font-series . medium)
366 (font-shape . upright)
369 (define (output-scope scope)
374 (let ((val (variable-ref var))
375 (tex-key (symbol->string sym)))
377 (if (memq sym fields)
378 (header-to-file basename sym val))
383 (format (current-error-port) "PROPS:~S\n" val)
384 (set! props (cons val props))
387 ;; define strings, for /make-lilypond-title to pick up
388 ((string? val) (ps-string-def "lilypond" sym val))
390 ;; generate stencil from markup
391 ((markup? val) (set! header-stencils
392 (append header-stencils
395 (interpret-markup paper props val)))))
398 ((number? val) (ps-number-def
399 "lilypond" sym (if (integer? val)
402 (exact->inexact val)))))
407 (apply string-append (map output-scope scopes)))))
409 (define (offset-add a b)
410 (cons (+ (car a) (car b))
411 (+ (cdr a) (cdr b))))
413 (define header-stencils '())
415 (define output-port (current-error-port))
417 ;; just calling this from paper-score gives Stack overflow.
423 (if (pair? header-stencils)
424 (map (lambda (x) (output-stencil output-port x '(10 . -10)))
428 ;; hmm, looks like recursing call is always last statement, does guile
430 (define (output-stencil port expr offset)
432 (let ((head (car expr)))
434 ((ly:input-location? head)
435 (display (apply define-origin (ly:input-location head)) port)
436 (output-stencil port (cadr expr) offset))
437 ((eq? head 'no-origin)
438 (display (expression->string head) port)
439 (output-stencil port (cadr expr) offset))
440 ((eq? head 'translate-stencil)
441 (output-stencil port (caddr expr) (offset-add offset (cadr expr))))
442 ((eq? head 'combine-stencil)
443 (output-stencil port (cadr expr) offset)
444 (output-stencil port (caddr expr) offset))
446 (display (placebox (car offset) (cdr offset)
447 (expression->string expr)) port))))))