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>
8 ;;;; Note: currently misused as testbed for titles with markup, see
9 ;;;; input/test/title-markup.ly
12 ;;;; * UGR: SPACE character in CM* fonts
13 ;;;; * text setting, kerning?
14 ;;;; * font size and designsize
16 ;;;; * font properties
17 ;;;; * construction/customisation of title markup
19 ;;;; * document output-interface
21 (debug-enable 'backtrace)
23 (define-module (scm output-ps))
24 (define this-module (current-module))
34 ;;; Lily output interface, PostScript implementation --- cleanup and docme
37 (define-public (ps-output-expression expr port)
38 (display (expression->string expr) port))
41 ;; alist containing fontname -> fontcommand assoc (both strings)
42 (define font-name-alist '())
44 ;; WIP -- stencils from markup? values of output-scopes
45 (define header-stencil #f)
47 (define lily-traced-cm-fonts
69 ;;; helper functions, not part of output interface
70 (define (escape-parentheses s)
71 (regexp-substitute/global #f "(^|[^\\])([\\(\\)])" s 'pre 1 "\\" 2 'post))
73 (define (offset-add a b)
74 (cons (+ (car a) (car b))
78 (define (ps-string-def prefix key val)
79 (string-append "/" prefix (symbol->string key) " ("
80 (escape-parentheses val)
83 (define (ps-number-def prefix key val)
84 (let ((s (if (integer? val)
86 (number->string (exact->inexact val)))))
87 (string-append "/" prefix (symbol->string key) " " s " def\n")))
91 ;;; Output-interface functions
92 (define (beam width slope thick blot)
94 (numbers->string (list slope width thick blot)) " draw_beam" ))
97 (define (bezier-sandwich l thick)
99 (apply string-append (map number-pair->string l))
100 (ly:number->string thick)
101 " draw_bezier_sandwich "))
103 (define (bracket arch_angle arch_width arch_height height arch_thick thick)
106 (list arch_angle arch_width arch_height height arch_thick thick))
111 "(\\" (inexact->string i 8) ") show " ))
114 (string-append "% " s "\n"))
116 (define (dashed-line thick on off dx dy)
118 (ly:number->string dx)
120 (ly:number->string dy)
122 (ly:number->string thick)
124 (ly:number->string on)
126 (ly:number->string off)
127 " ] 0 draw_dashed_line"))
129 ;; what the heck is this interface ?
130 (define (dashed-slur thick dash l)
132 (apply string-append (map number-pair->string l))
133 (ly:number->string thick)
135 (ly:number->string dash)
138 (ly:number->string (* 10 thick))
139 " ] 0 draw_dashed_slur"))
141 (define (define-fonts internal-external-name-mag-pairs)
143 (define (font-load-command name-mag command)
145 ;; frobnicate NAME to jibe with external definitions.
146 (define (possibly-capitalize-font-name name)
148 ((and (equal? (substring name 0 2) "cm")
149 (not (member name lily-traced-cm-fonts)))
151 ;; huh, how is this supposed to work?
152 ;;(string-upcase name)
154 (string-append name ".pfb"))
156 ((equal? (substring name 0 4) "feta")
157 (regexp-substitute/global #f "feta([a-z-]*)([0-9]+)" name 'pre "GNU-LilyPond-feta" 1 "-" 2 'post))
163 ;; Ugh, the Bluesky type1 fonts for computer modern use capitalized
164 ;; postscript font names.
165 (possibly-capitalize-font-name (car name-mag))
167 "20 " (ly:number->string (cdr name-mag)) " mul "
168 "output-scale div scalefont setfont } bind def "
171 (define (ps-encoded-fontswitch name-mag-pair)
172 (let* ((key (car name-mag-pair))
173 (value (cdr name-mag-pair)))
176 (string-append "lilyfont"
179 (number->string (cdr value)))))))
181 (set! font-name-alist (map ps-encoded-fontswitch
182 internal-external-name-mag-pairs))
185 (map (lambda (x) (font-load-command (car x) (cdr x)))
186 (map cdr font-name-alist))))
188 (define (define-origin file line col) "")
190 (define (dot x y radius)
194 (list x y radius)) " draw_dot"))
196 (define (draw-line thick x1 y1 x2 y2)
200 (ly:number->string thick)
202 (ly:number->string x1)
204 (ly:number->string y1)
206 (ly:number->string x2)
208 (ly:number->string y2)
212 "\nend-lilypond-output\n")
214 (define (expression->string expr)
215 (eval expr this-module))
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 (fontify name-mag-pair exp)
230 (define (select-font name-mag-pair)
231 (let ((c (assoc name-mag-pair font-name-alist)))
234 (string-append " " (cddr c) " ")
237 (format "Programming error: No such font: ~S" name-mag-pair))
239 (display "FAILED\n" (current-error-port))
240 (if #f ;(pair? name-mag-pair))
241 (display (object-type (car name-mag-pair)) (current-error-port))
242 (write name-mag-pair (current-error-port)))
243 (if #f ; (pair? font-name-alist)
245 (object-type (caaar font-name-alist)) (current-error-port))
246 (write font-name-alist (current-error-port)))
248 ;; (format #f "\n%FAILED: (select-font ~S)\n" name-mag-pair))
251 (string-append (select-font name-mag-pair) exp))
253 (define (header creator generate)
256 "%%Creator: " creator generate "\n"))
260 ;; URG: now we can't use scm output without Lily
261 (ly:gulp-file "lilyponddefs.ps")
262 "{exch pop //systemdict /run get exec}\n\n"
263 (ly:gulp-file "music-drawing-routines.ps")
264 "{ exch pop //systemdict /run get exec }\n\n"
265 ;; ps-testing wreaks havoc when used with lilypond-book.
266 ;; -- is this still true with new modules system?
267 ;; (if (defined? 'ps-testing) "\n /testing true def" "")
268 ; "\n /testing true def"
271 (define (horizontal-line x1 x2 th)
272 (draw-line th x1 0 x2 0))
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"))))
283 (define (make-title port)
285 (let ((x-ext (ly:stencil-get-extent header-stencil Y))
286 (y-ext (ly:stencil-get-extent header-stencil X)))
287 (display (start-system (interval-length x-ext) (interval-length y-ext))
289 (output-stencil port (ly:stencil-get-expr header-stencil) '(0 . 0))
290 (display (stop-system) port)))
293 (define (no-origin) "")
295 ;; FIXME: duplictates output-scopes, duplicated in other backends
296 ;; FIXME: silly interface name
297 (define (output-paper-def pd)
298 (let ((prefix "lilypondpaper"))
300 (define (scope-entry->string key var)
301 (let ((val (variable-ref var)))
303 ((string? val) (ps-string-def prefix key val))
304 ((number? val) (ps-number-def prefix key val))
309 (module-map scope-entry->string (ly:output-def-scope pd)))))
311 ;; FIXME: duplicated in other output backends
312 ;; FIXME: silly interface name
313 (define (output-scopes paper scopes fields basename)
315 ;; FIXME: customise/generate these
316 (let ((props '(((font-family . roman)
319 (font-series . medium)
321 (font-shape . upright)
327 (define (scope-entry->string key var)
328 (let ((val (variable-ref var)))
330 (if (memq key fields)
331 (header-to-file basename key val))
336 (format (current-error-port) "PROPS:~S\n" val)
337 (set! props (cons val props))
340 ;; define strings, for /make-lilypond-title to pick up
341 ((string? val) (ps-string-def prefix key val))
343 ;; generate stencil from markup
344 ((markup? val) (set! stencils
348 (interpret-markup paper props val))))
350 ((number? val) (ps-number-def prefix key val))
353 (define (output-scope scope)
354 (apply string-append (module-map scope-entry->string scope)))
356 (let ((s (string-append (apply string-append (map output-scope scopes)))))
357 (set! header-stencil (stack-lines DOWN 0 baseline-skip stencils))
360 (ly:stencil-get-expr header-stencil)
363 ;; hmm, looks like recursing call is always last statement, does guile
365 (define (output-stencil port expr offset)
367 (let ((head (car expr)))
369 ((ly:input-location? head)
370 (display (apply define-origin (ly:input-location head)) port)
371 (output-stencil port (cadr expr) offset))
372 ((eq? head 'no-origin)
373 (display (expression->string head) port)
374 (output-stencil port (cadr expr) offset))
375 ((eq? head 'translate-stencil)
376 (output-stencil port (caddr expr) (offset-add offset (cadr expr))))
377 ((eq? head 'combine-stencil)
378 (output-stencil port (cadr expr) offset)
379 (output-stencil port (caddr expr) offset))
381 (display (placebox (car offset) (cdr offset)
382 (expression->string expr)) port))))))
384 (define (placebox x y s)
386 (ly:number->string x) " " (ly:number->string y) " {" s "} place-box\n"))
388 (define (polygon points blotdiameter)
391 (numbers->string points)
392 (ly:number->string (/ (length points) 2))
393 (ly:number->string blotdiameter)
396 (define (repeat-slash wid slope thick)
398 (numbers->string (list wid slope thick))
399 " draw_repeat_slash"))
401 (define (round-filled-box x y width height blotdiam)
405 (list x y width height blotdiam)) " draw_round_box"))
407 (define (symmetric-x-triangle thick w h)
409 (numbers->string (list h w thick))
410 " draw_symmetric_x_triangle"))
412 (define (start-system width height)
414 "\n" (ly:number->string height)
417 "set-ps-scale-to-lily-scale\n"))
419 (define (stem breapth width depth height)
421 (numbers->string (list breapth width depth height))
424 (define (stop-last-system)
427 (define (stop-system)
431 (string-append "(" (escape-parentheses s) ") show "))
434 (define (top-of-file)
435 (header (string-append "GNU LilyPond (" (lilypond-version) "), ")
436 (strftime "%c" (localtime (current-time))))
440 (string-append "Engraved by LilyPond (" (lilypond-version) ")")))
445 (define (zigzag-line centre? zzw zzh thick dx dy)
447 (if centre? "true" "false")
449 (ly:number->string zzw)
451 (ly:number->string zzh)
453 (ly:number->string thick)
455 (ly:number->string dx)
457 (ly:number->string dy)
458 " draw_zigzag_line "))