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.
14 ;;;; * font properties
15 ;;;; * customisation of title markup, piece title markup?
17 ;;;; * document output-interface
19 (debug-enable 'backtrace)
21 (define-module (scm output-ps))
22 (define this-module (current-module))
33 ;;; Lily output interface, PostScript implementation --- cleanup and docme
36 (define-public (ps-output-expression expr port)
37 (display (expression->string expr) port))
40 ;; alist containing fontname -> fontcommand assoc (both strings)
41 (define font-name-alist '())
43 ;; WIP -- stencils from markup? values of output-scopes
44 (define header-stencil #f)
46 (define BASELINE-SKIP 3)
48 ;; /lilypondpaperoutputscale 1.75729901757299 def
49 ;;/lily-output-units 2.83464 def %% milimeter
50 ;;/output-scale lilypondpaperoutputscale lily-output-units mul def
52 ;; output-scale = 1.75729901757299 * 2.83464 = 4.9813100871731003736
54 (define OUTPUT-SCALE 4.98)
57 ;;; helper functions, not part of output interface
58 (define (escape-parentheses s)
59 (regexp-substitute/global #f "(^|[^\\])([\\(\\)])" s 'pre 1 "\\" 2 'post))
61 (define (offset-add a b)
62 (cons (+ (car a) (car b))
65 (define LATIN1-ENCODING-ALIST
73 (define LATIN1-ENCODING-COMMANDS
74 "/oumlaut { (o) show gsave -1 0 rmoveto (\\177) show grestore } bind def
75 /ograve { (o) show gsave -1 0 rmoveto (\\022) show grestore } def
76 /oacute { (o) show gsave -1 0 rmoveto (\\023) show grestore } def
77 /ocircumflex { (o) show gsave -1 0 rmoveto (^) show grestore } def
78 /otilde { (o) show gsave -1 0 rmoveto (~) show grestore } def
79 /oslash { (o) show gsave -1 0 rmoveto (\\034) show grestore } def
82 (define (ps-encoding text)
83 (let ((s (escape-parentheses text)))
84 (define (helper alist-list s)
85 (if (not (pair? alist-list))
87 (helper (cdr alist-list)
88 (regexp-substitute/global
89 #f (caar alist-list) s
90 'pre (string-append ") show " (cdar alist-list) " (")
92 (helper LATIN1-ENCODING-ALIST s)))
95 (define (ps-string-def prefix key val)
96 (string-append "/" prefix (symbol->string key) " ("
97 (escape-parentheses val)
100 (define (ps-number-def prefix key val)
101 (let ((s (if (integer? val)
103 (number->string (exact->inexact val)))))
104 (string-append "/" prefix (symbol->string key) " " s " def\n")))
106 (define (tex-font? fontname)
107 (equal? (substring fontname 0 2) "cm"))
111 ;;; Output-interface functions
112 (define (beam width slope thick blot)
114 (numbers->string (list slope width thick blot)) " draw_beam" ))
117 (define (bezier-sandwich l thick)
119 (apply string-append (map number-pair->string l))
120 (ly:number->string thick)
121 " draw_bezier_sandwich "))
123 (define (bracket arch_angle arch_width arch_height height arch_thick thick)
126 (list arch_angle arch_width arch_height height arch_thick thick))
131 "(\\" (inexact->string i 8) ") show " ))
134 (string-append "% " s "\n"))
136 (define (dashed-line thick on off dx dy)
138 (ly:number->string dx)
140 (ly:number->string dy)
142 (ly:number->string thick)
144 (ly:number->string on)
146 (ly:number->string off)
147 " ] 0 draw_dashed_line"))
149 ;; what the heck is this interface ?
150 (define (dashed-slur thick dash l)
152 (apply string-append (map number-pair->string l))
153 (ly:number->string thick)
155 (ly:number->string dash)
158 (ly:number->string (* 10 thick))
159 " ] 0 draw_dashed_slur"))
161 (define (define-fonts internal-external-name-mag-pairs)
163 (define (fontname->designsize fontname)
164 (let ((i (string-index fontname char-numeric?)))
165 (string->number (substring fontname i))))
167 ;; (define (font-load-command name-mag command)
168 (define (font-load-command lst)
169 (let* ((key-name-size (car lst))
171 (value-name-size (car value))
172 (command (cdr value))
173 (fontname (car value-name-size))
174 (designsize (if (tex-font? fontname)
175 (/ 12 (fontname->designsize fontname))
176 ;; This is about 12/20 :-)
177 (cdr key-name-size)))
178 (fontsize (cdr value-name-size))
179 (scaling (* 12 (/ fontsize designsize)))
180 (scaling (/ fontsize (/ designsize 12))))
182 ;; frobnicate NAME to jibe with external definitions.
183 (define (possibly-mangle-fontname fontname)
185 ((tex-font? fontname)
186 ;; FIXME: we need proper Fontmap for CM fonts, like so:
187 ;; /CMR10 (cmr10.pfb);
188 ;; (string-upcase fontname)
189 (string-append fontname ".pfb"))
190 ((or (equal? (substring fontname 0 4) "feta")
191 (equal? (substring fontname 0 8) "parmesan"))
192 (regexp-substitute/global
193 #f "(feta|parmesan)([a-z-]*)([0-9]+)"
194 fontname 'pre "GNU-LilyPond-" 1 2 "-" 3 'post))
200 (format (current-error-port) "key-name-size ~S\n" key-name-size)
201 (format (current-error-port) "value ~S\n" value)
202 (format (current-error-port) "value-name-size ~S\n" value-name-size)
203 (format (current-error-port) "command ~S\n" command)
204 (format (current-error-port) "designsize ~S\n" designsize)
205 (format (current-error-port) "fontname ~S\n" fontname)
206 (format (current-error-port) "fontsize ~S\n" fontsize)
207 (format (current-error-port) "scaling ~S\n" scaling)))
211 " { /" (possibly-mangle-fontname fontname) " findfont "
212 (ly:number->string scaling)
213 "output-scale div scalefont setfont } bind def \n")))
215 (define (ps-encoded-fontswitch name-mag-pair)
216 (let* ((key (car name-mag-pair))
217 (value (cdr name-mag-pair))
218 (fontname (car value))
219 (scaling (cdr value)))
220 (cons key (cons value
222 "lilyfont" fontname "-" (number->string scaling))))))
224 (set! font-name-alist
225 (map ps-encoded-fontswitch internal-external-name-mag-pairs))
226 (apply string-append (map font-load-command font-name-alist)))
228 (define (define-origin file line col) "")
230 (define (dot x y radius)
234 (list x y radius)) " draw_dot"))
236 (define (draw-line thick x1 y1 x2 y2)
240 (ly:number->string thick)
242 (ly:number->string x1)
244 (ly:number->string y1)
246 (ly:number->string x2)
248 (ly:number->string y2)
252 "\nend-lilypond-output\n")
254 (define (expression->string expr)
255 (eval expr this-module))
257 (define (ez-ball ch letter-col ball-col)
260 (numbers->string (list letter-col ball-col))
261 " /Helvetica-Bold " ;; ugh
264 (define (filledbox breapth width depth height)
265 (string-append (numbers->string (list breapth width depth height))
268 (define (fontify name-mag-pair exp)
270 (define (select-font name-mag-pair)
271 (let ((c (assoc name-mag-pair font-name-alist)))
274 (string-append " " (cddr c) " ")
277 (format "Programming error: No such font: ~S" name-mag-pair))
279 (display "FAILED\n" (current-error-port))
280 (if #f ;(pair? name-mag-pair))
281 (display (object-type (car name-mag-pair)) (current-error-port))
282 (write name-mag-pair (current-error-port)))
283 (if #f ; (pair? font-name-alist)
285 (object-type (caaar font-name-alist)) (current-error-port))
286 (write font-name-alist (current-error-port)))
288 ;; (format #f "\n%FAILED: (select-font ~S)\n" name-mag-pair))
291 (string-append (select-font name-mag-pair) exp))
293 (define (header creator generate)
296 "%%Creator: " creator generate "\n"))
300 ;; URG: now we can't use scm output without Lily
301 (ly:gulp-file "lilyponddefs.ps")
302 "{exch pop //systemdict /run get exec}\n\n"
303 (ly:gulp-file "music-drawing-routines.ps")
304 "{ exch pop //systemdict /run get exec }\n\n"
305 ;; ps-testing wreaks havoc when used with lilypond-book.
306 ;; -- is this still true with new modules system?
307 ;; (if (defined? 'ps-testing) "\n /testing true def" "")
308 ; "\n /testing true def"
311 (define (horizontal-line x1 x2 th)
312 (draw-line th x1 0 x2 0))
314 (define (lily-def key val)
315 (let ((prefix "lilypondpaper"))
317 (substring key 0 (min (string-length prefix) (string-length key)))
319 (string-append "/" key " {" val "} bind def\n")
320 (string-append "/" key " (" val ") def\n"))))
322 (define (make-title port)
324 (let ((x-ext (ly:stencil-get-extent header-stencil Y))
325 (y-ext (ly:stencil-get-extent header-stencil X)))
326 (display (start-system
327 (/ (interval-length x-ext) OUTPUT-SCALE)
328 (/ (interval-length y-ext) OUTPUT-SCALE))
330 (output-stencil port (ly:stencil-get-expr header-stencil) '(0 . 0))
331 (display (stop-system) port)))
334 (define (no-origin) "")
336 ;; FIXME: duplictates output-scopes, duplicated in other backends
337 ;; FIXME: silly interface name
338 (define (output-paper-def pd)
339 (let ((prefix "lilypondpaper"))
341 (define (scope-entry->string key var)
342 (let ((val (variable-ref var)))
344 ((string? val) (ps-string-def prefix key val))
345 ((number? val) (ps-number-def prefix key val))
350 (module-map scope-entry->string (ly:output-def-scope pd)))))
352 ;; FIXME: duplicated in other output backends
353 ;; FIXME: silly interface name
354 (define (output-scopes paper scopes fields basename)
356 ;; FIXME: customise/generate these
361 `((linewidth . ,(ly:paper-get-number paper 'linewidth))
362 (font-family . roman))
363 (ly:paper-lookup paper 'font-defaults))))
367 (define (scope-entry->string key var)
368 (let ((val (variable-ref var)))
370 (if (memq key fields)
371 (header-to-file basename key val))
376 (format (current-error-port) "PROPS:~S\n" val)
377 (set! props (cons val props))
380 ;; define strings, for /make-lilypond-title to pick up
381 ((string? val) (ps-string-def prefix key val))
383 ;; generate stencil from markup
384 ((markup? val) (set! stencils
388 (interpret-markup paper props val))))
390 ((number? val) (ps-number-def prefix key val))
393 (define (output-scope scope)
394 (apply string-append (module-map scope-entry->string scope)))
396 (let ((s (string-append (apply string-append (map output-scope scopes)))))
397 (set! header-stencil (stack-lines DOWN 0 BASELINE-SKIP stencils))
399 ;; match systems, which are also aligned to center
400 (ly:stencil-align-to! header-stencil Y CENTER)
403 (ly:stencil-get-expr header-stencil)
406 ;; hmm, looks like recursing call is always last statement, does guile
408 (define (output-stencil port expr offset)
410 (let ((head (car expr)))
412 ((ly:input-location? head)
413 (display (apply define-origin (ly:input-location head)) port)
414 (output-stencil port (cadr expr) offset))
415 ((eq? head 'no-origin)
416 (display (expression->string head) port)
417 (output-stencil port (cadr expr) offset))
418 ((eq? head 'translate-stencil)
419 (output-stencil port (caddr expr) (offset-add offset (cadr expr))))
420 ((eq? head 'combine-stencil)
421 (output-stencil port (cadr expr) offset)
422 (output-stencil port (caddr expr) offset))
424 (display (placebox (car offset) (cdr offset)
425 (expression->string expr)) port))))))
427 (define (placebox x y s)
429 (ly:number->string x) " " (ly:number->string y) " {" s "} place-box\n"))
431 (define (polygon points blotdiameter)
434 (numbers->string points)
435 (ly:number->string (/ (length points) 2))
436 (ly:number->string blotdiameter)
439 (define (repeat-slash wid slope thick)
441 (numbers->string (list wid slope thick))
442 " draw_repeat_slash"))
444 (define (round-filled-box x y width height blotdiam)
448 (list x y width height blotdiam)) " draw_round_box"))
450 (define (start-system width height)
452 "\n" (ly:number->string height)
455 "set-ps-scale-to-lily-scale\n"))
457 (define (stem breapth width depth height)
459 (numbers->string (list breapth width depth height))
462 (define (stop-last-system)
465 (define (stop-system)
468 (define (symmetric-x-triangle thick w h)
470 (numbers->string (list h w thick))
471 " draw_symmetric_x_triangle"))
474 ;; (string-append "(" (escape-parentheses s) ") show "))
475 (string-append "(" (ps-encoding s) ") show "))
478 (define (top-of-file)
480 (header (string-append "GNU LilyPond (" (lilypond-version) "), ")
481 (strftime "%c" (localtime (current-time))))
482 LATIN1-ENCODING-COMMANDS
486 (string-append "Engraved by LilyPond (" (lilypond-version) ")"))))
491 (define (zigzag-line centre? zzw zzh thick dx dy)
493 (if centre? "true" "false")
495 (ly:number->string zzw)
497 (ly:number->string zzh)
499 (ly:number->string thick)
501 (ly:number->string dx)
503 (ly:number->string dy)
504 " draw_zigzag_line "))