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 ;; * testbed for titles with markup
10 ;; * font size and designsize
11 ;; * FIXME: breaks when outputting strings with parentheses.
14 (debug-enable 'backtrace)
16 (define-module (scm output-ps))
17 (define this-module (current-module))
25 ;;; Lily output interface, PostScript implementation --- cleanup and docme
28 (define-public (ps-output-expression expr port)
29 (display (expression->string expr) port))
32 (define (expression->string expr)
33 (eval expr this-module))
37 ;; alist containing fontname -> fontcommand assoc (both strings)
38 (define font-name-alist '())
40 ;; WIP -- stencils from markup? values of output-scopes
41 (define header-stencil #f)
43 ;; Interface functions
44 (define (beam width slope thick blot)
46 (numbers->string (list slope width thick blot)) " draw_beam" ))
49 (define (bezier-sandwich l thick)
51 (apply string-append (map number-pair->string l))
52 (ly:number->string thick)
53 " draw_bezier_sandwich "))
55 (define (bracket arch_angle arch_width arch_height height arch_thick thick)
58 (list arch_angle arch_width arch_height height arch_thick thick))
61 (define (symmetric-x-triangle thick w h)
63 (numbers->string (list h w thick))
64 " draw_symmetric_x_triangle"))
69 "(\\" (inexact->string i 8) ") show " ))
73 (string-append "% " s "\n"))
76 (define (dashed-line thick on off dx dy)
78 (ly:number->string dx)
80 (ly:number->string dy)
82 (ly:number->string thick)
84 (ly:number->string on)
86 (ly:number->string off)
87 " ] 0 draw_dashed_line"))
89 ;; what the heck is this interface ?
90 (define (dashed-slur thick dash l)
92 (apply string-append (map number-pair->string l))
93 (ly:number->string thick)
95 (ly:number->string dash)
98 (ly:number->string (* 10 thick))
99 " ] 0 draw_dashed_slur"))
101 (define lily-traced-cm-fonts
124 (define (define-fonts internal-external-name-mag-pairs)
126 (define (font-load-command name-mag command)
128 ;; frobnicate NAME to jibe with external definitions.
129 (define (possibly-capitalize-font-name name)
131 ((and (equal? (substring name 0 2) "cm")
132 (not (member name lily-traced-cm-fonts)))
134 ;; huh, how is this supposed to work?
135 ;;(string-upcase name)
137 (string-append name ".pfb"))
139 ((equal? (substring name 0 4) "feta")
140 (regexp-substitute/global #f "feta([a-z-]*)([0-9]+)" name 'pre "GNU-LilyPond-feta" 1 "-" 2 'post))
146 ;; Ugh, the Bluesky type1 fonts for computer modern use capitalized
147 ;; postscript font names.
148 (possibly-capitalize-font-name (car name-mag))
150 "20 " (ly:number->string (cdr name-mag)) " mul "
151 "output-scale div scalefont setfont } bind def "
154 (define (ps-encoded-fontswitch name-mag-pair)
155 (let* ((key (car name-mag-pair))
156 (value (cdr name-mag-pair)))
159 (string-append "lilyfont"
162 (number->string (cdr value)))))))
164 (set! font-name-alist (map ps-encoded-fontswitch
165 internal-external-name-mag-pairs))
168 (map (lambda (x) (font-load-command (car x) (cdr x)))
169 (map cdr font-name-alist))))
171 (define (define-origin file line col) "")
173 (define (dot x y radius)
177 (list x y radius)) " draw_dot"))
179 (define (zigzag-line centre? zzw zzh thick dx dy)
181 (if centre? "true" "false")
183 (ly:number->string zzw)
185 (ly:number->string zzh)
187 (ly:number->string thick)
189 (ly:number->string dx)
191 (ly:number->string dy)
192 " draw_zigzag_line "))
194 (define (draw-line thick x1 y1 x2 y2)
198 (ly:number->string thick)
200 (ly:number->string x1)
202 (ly:number->string y1)
204 (ly:number->string x2)
206 (ly:number->string y2)
209 (define (polygon points blotdiameter)
212 (numbers->string points)
213 (ly:number->string (/ (length points) 2))
214 (ly:number->string blotdiameter)
218 "\nend-lilypond-output\n")
220 (define (ez-ball ch letter-col ball-col)
223 (numbers->string (list letter-col ball-col))
224 " /Helvetica-Bold " ;; ugh
227 (define (filledbox breapth width depth height)
228 (string-append (numbers->string (list breapth width depth height))
231 (define (horizontal-line x1 x2 th)
232 (draw-line th x1 0 x2 0))
234 (define (fontify name-mag-pair exp)
236 (define (select-font name-mag-pair)
237 (let ((c (assoc name-mag-pair font-name-alist)))
240 (string-append " " (cddr c) " ")
243 (format "Programming error: No such font: ~S" name-mag-pair))
245 (display "FAILED\n" (current-error-port))
246 (if #f ;(pair? name-mag-pair))
247 (display (object-type (car name-mag-pair)) (current-error-port))
248 (write name-mag-pair (current-error-port)))
249 (if #f ; (pair? font-name-alist)
251 (object-type (caaar font-name-alist)) (current-error-port))
252 (write font-name-alist (current-error-port)))
254 ;; (format #f "\n%FAILED: (select-font ~S)\n" name-mag-pair))
257 (string-append (select-font name-mag-pair) exp))
259 (define (header creator generate)
262 "%%Creator: " creator generate "\n"))
266 ;; URG: now we can't use scm output without Lily
267 (ly:gulp-file "lilyponddefs.ps")
268 "{exch pop //systemdict /run get exec}\n\n"
269 (ly:gulp-file "music-drawing-routines.ps")
270 "{ exch pop //systemdict /run get exec }\n\n"
271 ;; ps-testing wreaks havoc when used with lilypond-book.
272 ;; -- is this still true with new modules system?
273 ;; (if (defined? 'ps-testing) "\n /testing true def" "")
274 ; "\n /testing true def"
277 (define (lily-def key val)
278 (let ((prefix "lilypondpaper"))
280 (substring key 0 (min (string-length prefix) (string-length key)))
282 (string-append "/" key " {" val "} bind def\n")
283 (string-append "/" key " (" val ") def\n"))))
285 (define (no-origin) "")
287 (define (placebox x y s)
289 (ly:number->string x) " " (ly:number->string y) " {" s "} place-box\n"))
291 (define (repeat-slash wid slope thick)
293 (numbers->string (list wid slope thick))
294 " draw_repeat_slash"))
296 (define (round-filled-box x y width height blotdiam)
300 (list x y width height blotdiam)) " draw_round_box"))
302 (define (start-system width height)
304 "\n" (ly:number->string height)
307 "set-ps-scale-to-lily-scale\n"))
309 (define (stem breapth width depth height)
311 (numbers->string (list breapth width depth height))
314 (define (stop-last-system)
317 (define (stop-system)
321 (string-append "(" s ") show "))
327 (define (top-of-file)
328 (header (string-append "GNU LilyPond (" (lilypond-version) "), ")
329 (strftime "%c" (localtime (current-time))))
333 (string-append "Engraved by LilyPond (" (lilypond-version) ")")))
335 (define (output-paper-def pd)
340 (let ((val (variable-ref var))
341 (key (symbol->string sym)))
345 (ps-string-def "lilypondpaper" sym val))
347 (ps-number-def "lilypondpaper" sym
350 (number->string (exact->inexact val)))))
353 (ly:output-def-scope pd))))
356 (define (ps-string-def a b c)
357 (string-append "/" a (symbol->string b) " (" c ") def\n"))
359 (define (ps-number-def a b c)
360 (string-append "/" a (symbol->string b) " " c " def\n"))
363 (define (output-scopes paper scopes fields basename)
365 ;; FIXME: customise/generate these
366 (let ((props '(((font-family . roman)
369 (font-series . medium)
371 (font-shape . upright)
376 (define (output-scope-entry sym var)
377 (let ((val (variable-ref var))
378 (tex-key (symbol->string sym)))
380 (if (memq sym fields)
381 (header-to-file basename sym val))
386 (format (current-error-port) "PROPS:~S\n" val)
387 (set! props (cons val props))
390 ;; define strings, for /make-lilypond-title to pick up
391 ((string? val) (ps-string-def "lilypond" sym val))
393 ;; generate stencil from markup
394 ((markup? val) (set! stencils
398 (interpret-markup paper props val))))
400 ((number? val) (ps-number-def
401 "lilypond" sym (if (integer? val)
404 (exact->inexact val)))))
407 (define (output-scope scope)
408 (apply string-append (module-map output-scope-entry scope)))
410 (let ((s (string-append (apply string-append (map output-scope scopes)))))
411 (set! header-stencil (stack-lines DOWN 0 baseline-skip stencils))
414 (ly:stencil-get-expr header-stencil)
417 (define (offset-add a b)
418 (cons (+ (car a) (car b))
419 (+ (cdr a) (cdr b))))
421 (define (make-title port)
423 (let ((x-ext (ly:stencil-get-extent header-stencil Y))
424 (y-ext (ly:stencil-get-extent header-stencil X)))
425 (display (start-system (interval-length x-ext) (interval-length y-ext))
427 (output-stencil port (ly:stencil-get-expr header-stencil) '(0 . 0))
428 (display (stop-system) port)))
431 ;; hmm, looks like recursing call is always last statement, does guile
433 (define (output-stencil port expr offset)
435 (let ((head (car expr)))
437 ((ly:input-location? head)
438 (display (apply define-origin (ly:input-location head)) port)
439 (output-stencil port (cadr expr) offset))
440 ((eq? head 'no-origin)
441 (display (expression->string head) port)
442 (output-stencil port (cadr expr) offset))
443 ((eq? head 'translate-stencil)
444 (output-stencil port (caddr expr) (offset-add offset (cadr expr))))
445 ((eq? head 'combine-stencil)
446 (output-stencil port (cadr expr) offset)
447 (output-stencil port (caddr expr) offset))
449 (display (placebox (car offset) (cdr offset)
450 (expression->string expr)) port))))))