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 ;;;; * special characters, encoding.
13 ;;;; * text setting, kerning.
15 ;;;; * font properties
16 ;;;; * construction/customisation of title markup
18 ;;;; * document output-interface
20 (debug-enable 'backtrace)
22 (define-module (scm output-ps))
23 (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)
48 ;;; helper functions, not part of output interface
49 (define (escape-parentheses s)
50 (regexp-substitute/global #f "(^|[^\\])([\\(\\)])" s 'pre 1 "\\" 2 'post))
52 (define (offset-add a b)
53 (cons (+ (car a) (car b))
57 (define (ps-string-def prefix key val)
58 (string-append "/" prefix (symbol->string key) " ("
59 (escape-parentheses val)
62 (define (ps-number-def prefix key val)
63 (let ((s (if (integer? val)
65 (number->string (exact->inexact val)))))
66 (string-append "/" prefix (symbol->string key) " " s " def\n")))
68 (define (tex-font? fontname)
69 (equal? (substring fontname 0 2) "cm"))
73 ;;; Output-interface functions
74 (define (beam width slope thick blot)
76 (numbers->string (list slope width thick blot)) " draw_beam" ))
79 (define (bezier-sandwich l thick)
81 (apply string-append (map number-pair->string l))
82 (ly:number->string thick)
83 " draw_bezier_sandwich "))
85 (define (bracket arch_angle arch_width arch_height height arch_thick thick)
88 (list arch_angle arch_width arch_height height arch_thick thick))
93 "(\\" (inexact->string i 8) ") show " ))
96 (string-append "% " s "\n"))
98 (define (dashed-line thick on off dx dy)
100 (ly:number->string dx)
102 (ly:number->string dy)
104 (ly:number->string thick)
106 (ly:number->string on)
108 (ly:number->string off)
109 " ] 0 draw_dashed_line"))
111 ;; what the heck is this interface ?
112 (define (dashed-slur thick dash l)
114 (apply string-append (map number-pair->string l))
115 (ly:number->string thick)
117 (ly:number->string dash)
120 (ly:number->string (* 10 thick))
121 " ] 0 draw_dashed_slur"))
123 (define (define-fonts internal-external-name-mag-pairs)
125 (define (fontname->designsize fontname)
126 (let ((i (string-index fontname char-numeric?)))
127 (string->number (substring fontname i))))
129 ;; (define (font-load-command name-mag command)
130 (define (font-load-command lst)
131 (let* ((key-name-size (car lst))
133 (value-name-size (car value))
134 (command (cdr value))
135 (fontname (car value-name-size))
136 (designsize (if (tex-font? fontname)
137 (/ 12 (fontname->designsize fontname))
138 ;; This is about 12/20 :-)
139 (cdr key-name-size)))
140 (fontsize (cdr value-name-size))
141 (scaling (* 12 (/ fontsize designsize)))
142 (scaling (/ fontsize (/ designsize 12))))
144 ;; frobnicate NAME to jibe with external definitions.
145 (define (possibly-mangle-fontname fontname)
147 ((tex-font? fontname)
148 ;; FIXME: we need proper Fontmap for CM fonts, like so:
149 ;; /CMR10 (cmr10.pfb);
150 ;; (string-upcase fontname)
151 (string-append fontname ".pfb"))
152 ((or (equal? (substring fontname 0 4) "feta")
153 (equal? (substring fontname 0 8) "parmesan"))
154 (regexp-substitute/global
155 #f "(feta|parmesan)([a-z-]*)([0-9]+)"
156 fontname 'pre "GNU-LilyPond-" 1 2 "-" 3 'post))
162 (format (current-error-port) "key-name-size ~S\n" key-name-size)
163 (format (current-error-port) "value ~S\n" value)
164 (format (current-error-port) "value-name-size ~S\n" value-name-size)
165 (format (current-error-port) "command ~S\n" command)
166 (format (current-error-port) "designsize ~S\n" designsize)
167 (format (current-error-port) "fontname ~S\n" fontname)
168 (format (current-error-port) "fontsize ~S\n" fontsize)
169 (format (current-error-port) "scaling ~S\n" scaling)))
173 " { /" (possibly-mangle-fontname fontname) " findfont "
174 (ly:number->string scaling)
175 "output-scale div scalefont setfont } bind def \n")))
177 (define (ps-encoded-fontswitch name-mag-pair)
178 (let* ((key (car name-mag-pair))
179 (value (cdr name-mag-pair))
180 (fontname (car value))
181 (scaling (cdr value)))
182 (cons key (cons value
184 "lilyfont" fontname "-" (number->string scaling))))))
186 (set! font-name-alist
187 (map ps-encoded-fontswitch internal-external-name-mag-pairs))
188 (apply string-append (map font-load-command font-name-alist)))
190 (define (define-origin file line col) "")
192 (define (dot x y radius)
196 (list x y radius)) " draw_dot"))
198 (define (draw-line thick x1 y1 x2 y2)
202 (ly:number->string thick)
204 (ly:number->string x1)
206 (ly:number->string y1)
208 (ly:number->string x2)
210 (ly:number->string y2)
214 "\nend-lilypond-output\n")
216 (define (expression->string expr)
217 (eval expr this-module))
219 (define (ez-ball ch letter-col ball-col)
222 (numbers->string (list letter-col ball-col))
223 " /Helvetica-Bold " ;; ugh
226 (define (filledbox breapth width depth height)
227 (string-append (numbers->string (list breapth width depth height))
230 (define (fontify name-mag-pair exp)
232 (define (select-font name-mag-pair)
233 (let ((c (assoc name-mag-pair font-name-alist)))
236 (string-append " " (cddr c) " ")
239 (format "Programming error: No such font: ~S" name-mag-pair))
241 (display "FAILED\n" (current-error-port))
242 (if #f ;(pair? name-mag-pair))
243 (display (object-type (car name-mag-pair)) (current-error-port))
244 (write name-mag-pair (current-error-port)))
245 (if #f ; (pair? font-name-alist)
247 (object-type (caaar font-name-alist)) (current-error-port))
248 (write font-name-alist (current-error-port)))
250 ;; (format #f "\n%FAILED: (select-font ~S)\n" name-mag-pair))
253 (string-append (select-font name-mag-pair) exp))
255 (define (header creator generate)
258 "%%Creator: " creator generate "\n"))
262 ;; URG: now we can't use scm output without Lily
263 (ly:gulp-file "lilyponddefs.ps")
264 "{exch pop //systemdict /run get exec}\n\n"
265 (ly:gulp-file "music-drawing-routines.ps")
266 "{ exch pop //systemdict /run get exec }\n\n"
267 ;; ps-testing wreaks havoc when used with lilypond-book.
268 ;; -- is this still true with new modules system?
269 ;; (if (defined? 'ps-testing) "\n /testing true def" "")
270 ; "\n /testing true def"
273 (define (horizontal-line x1 x2 th)
274 (draw-line th x1 0 x2 0))
276 (define (lily-def key val)
277 (let ((prefix "lilypondpaper"))
279 (substring key 0 (min (string-length prefix) (string-length key)))
281 (string-append "/" key " {" val "} bind def\n")
282 (string-append "/" key " (" val ") def\n"))))
285 (define (make-title port)
287 (let ((x-ext (ly:stencil-get-extent header-stencil Y))
288 (y-ext (ly:stencil-get-extent header-stencil X)))
289 ;;(display (start-system (interval-length x-ext) (interval-length y-ext))
290 (display (start-system
291 ;; output-scale trouble?
292 (/ (interval-length x-ext) 2)
293 (/ (interval-length y-ext) 2))
295 (output-stencil port (ly:stencil-get-expr header-stencil) '(0 . 0))
296 (display (stop-system) port)))
299 (define (no-origin) "")
301 ;; FIXME: duplictates output-scopes, duplicated in other backends
302 ;; FIXME: silly interface name
303 (define (output-paper-def pd)
304 (let ((prefix "lilypondpaper"))
306 (define (scope-entry->string key var)
307 (let ((val (variable-ref var)))
309 ((string? val) (ps-string-def prefix key val))
310 ((number? val) (ps-number-def prefix key val))
315 (module-map scope-entry->string (ly:output-def-scope pd)))))
317 ;; FIXME: duplicated in other output backends
318 ;; FIXME: silly interface name
319 (define (output-scopes paper scopes fields basename)
321 ;; FIXME: customise/generate these
322 (let ((props '(((font-family . roman)
325 (font-series . medium)
327 (font-shape . upright)
333 (define (scope-entry->string key var)
334 (let ((val (variable-ref var)))
336 (if (memq key fields)
337 (header-to-file basename key val))
342 (format (current-error-port) "PROPS:~S\n" val)
343 (set! props (cons val props))
346 ;; define strings, for /make-lilypond-title to pick up
347 ((string? val) (ps-string-def prefix key val))
349 ;; generate stencil from markup
350 ((markup? val) (set! stencils
354 (interpret-markup paper props val))))
356 ((number? val) (ps-number-def prefix key val))
359 (define (output-scope scope)
360 (apply string-append (module-map scope-entry->string scope)))
362 (let ((s (string-append (apply string-append (map output-scope scopes)))))
363 (set! header-stencil (stack-lines DOWN 0 baseline-skip stencils))
366 (ly:stencil-get-expr header-stencil)
369 ;; hmm, looks like recursing call is always last statement, does guile
371 (define (output-stencil port expr offset)
373 (let ((head (car expr)))
375 ((ly:input-location? head)
376 (display (apply define-origin (ly:input-location head)) port)
377 (output-stencil port (cadr expr) offset))
378 ((eq? head 'no-origin)
379 (display (expression->string head) port)
380 (output-stencil port (cadr expr) offset))
381 ((eq? head 'translate-stencil)
382 (output-stencil port (caddr expr) (offset-add offset (cadr expr))))
383 ((eq? head 'combine-stencil)
384 (output-stencil port (cadr expr) offset)
385 (output-stencil port (caddr expr) offset))
387 (display (placebox (car offset) (cdr offset)
388 (expression->string expr)) port))))))
390 (define (placebox x y s)
392 (ly:number->string x) " " (ly:number->string y) " {" s "} place-box\n"))
394 (define (polygon points blotdiameter)
397 (numbers->string points)
398 (ly:number->string (/ (length points) 2))
399 (ly:number->string blotdiameter)
402 (define (repeat-slash wid slope thick)
404 (numbers->string (list wid slope thick))
405 " draw_repeat_slash"))
407 (define (round-filled-box x y width height blotdiam)
411 (list x y width height blotdiam)) " draw_round_box"))
413 (define (symmetric-x-triangle thick w h)
415 (numbers->string (list h w thick))
416 " draw_symmetric_x_triangle"))
418 (define (start-system width height)
420 "\n" (ly:number->string height)
423 "set-ps-scale-to-lily-scale\n"))
425 (define (stem breapth width depth height)
427 (numbers->string (list breapth width depth height))
430 (define (stop-last-system)
433 (define (stop-system)
437 (string-append "(" (escape-parentheses s) ") show "))
440 (define (top-of-file)
441 (header (string-append "GNU LilyPond (" (lilypond-version) "), ")
442 (strftime "%c" (localtime (current-time))))
446 (string-append "Engraved by LilyPond (" (lilypond-version) ")")))
451 (define (zigzag-line centre? zzw zzh thick dx dy)
453 (if centre? "true" "false")
455 (ly:number->string zzw)
457 (ly:number->string zzh)
459 (ly:number->string thick)
461 (ly:number->string dx)
463 (ly:number->string dy)
464 " draw_zigzag_line "))