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
53 (define OUTPUT-SCALE 4.98)
55 (define PROPS `(((font-family . roman)
57 (baseline-skip . ,BASELINE-SKIP)
58 (font-series . medium)
60 (font-shape . upright)
64 ;;; helper functions, not part of output interface
65 (define (escape-parentheses s)
66 (regexp-substitute/global #f "(^|[^\\])([\\(\\)])" s 'pre 1 "\\" 2 'post))
68 (define (offset-add a b)
69 (cons (+ (car a) (car b))
72 (define LATIN1-ENCODING-ALIST
80 (define LATIN1-ENCODING-COMMANDS
81 "/oumlaut { (o) show gsave -1 0 rmoveto (\\177) show grestore } bind def
82 /ograve { (o) show gsave -1 0 rmoveto (\\022) show grestore } def
83 /oacute { (o) show gsave -1 0 rmoveto (\\023) show grestore } def
84 /ocircumflex { (o) show gsave -1 0 rmoveto (^) show grestore } def
85 /otilde { (o) show gsave -1 0 rmoveto (~) show grestore } def
86 /oslash { (o) show gsave -1 0 rmoveto (\\034) show grestore } def
89 (define (ps-encoding text)
90 (let ((s (escape-parentheses text)))
91 (define (helper alist-list s)
92 (if (not (pair? alist-list))
94 (helper (cdr alist-list)
95 (regexp-substitute/global
96 #f (caar alist-list) s
97 'pre (string-append ") show " (cdar alist-list) " (")
99 (helper LATIN1-ENCODING-ALIST s)))
102 (define (ps-string-def prefix key val)
103 (string-append "/" prefix (symbol->string key) " ("
104 (escape-parentheses val)
107 (define (ps-number-def prefix key val)
108 (let ((s (if (integer? val)
110 (number->string (exact->inexact val)))))
111 (string-append "/" prefix (symbol->string key) " " s " def\n")))
113 (define (tex-font? fontname)
114 (equal? (substring fontname 0 2) "cm"))
118 ;;; Output-interface functions
119 (define (beam width slope thick blot)
121 (numbers->string (list slope width thick blot)) " draw_beam" ))
124 (define (bezier-sandwich l thick)
126 (apply string-append (map number-pair->string l))
127 (ly:number->string thick)
128 " draw_bezier_sandwich "))
130 (define (bracket arch_angle arch_width arch_height height arch_thick thick)
133 (list arch_angle arch_width arch_height height arch_thick thick))
138 "(\\" (inexact->string i 8) ") show " ))
141 (string-append "% " s "\n"))
143 (define (dashed-line thick on off dx dy)
145 (ly:number->string dx)
147 (ly:number->string dy)
149 (ly:number->string thick)
151 (ly:number->string on)
153 (ly:number->string off)
154 " ] 0 draw_dashed_line"))
156 ;; what the heck is this interface ?
157 (define (dashed-slur thick dash l)
159 (apply string-append (map number-pair->string l))
160 (ly:number->string thick)
162 (ly:number->string dash)
165 (ly:number->string (* 10 thick))
166 " ] 0 draw_dashed_slur"))
168 (define (define-fonts internal-external-name-mag-pairs)
170 (define (fontname->designsize fontname)
171 (let ((i (string-index fontname char-numeric?)))
172 (string->number (substring fontname i))))
174 ;; (define (font-load-command name-mag command)
175 (define (font-load-command lst)
176 (let* ((key-name-size (car lst))
178 (value-name-size (car value))
179 (command (cdr value))
180 (fontname (car value-name-size))
181 (designsize (if (tex-font? fontname)
182 (/ 12 (fontname->designsize fontname))
183 ;; This is about 12/20 :-)
184 (cdr key-name-size)))
185 (fontsize (cdr value-name-size))
186 (scaling (* 12 (/ fontsize designsize)))
187 (scaling (/ fontsize (/ designsize 12))))
189 ;; frobnicate NAME to jibe with external definitions.
190 (define (possibly-mangle-fontname fontname)
192 ((tex-font? fontname)
193 ;; FIXME: we need proper Fontmap for CM fonts, like so:
194 ;; /CMR10 (cmr10.pfb);
195 ;; (string-upcase fontname)
196 (string-append fontname ".pfb"))
197 ((or (equal? (substring fontname 0 4) "feta")
198 (equal? (substring fontname 0 8) "parmesan"))
199 (regexp-substitute/global
200 #f "(feta|parmesan)([a-z-]*)([0-9]+)"
201 fontname 'pre "GNU-LilyPond-" 1 2 "-" 3 'post))
207 (format (current-error-port) "key-name-size ~S\n" key-name-size)
208 (format (current-error-port) "value ~S\n" value)
209 (format (current-error-port) "value-name-size ~S\n" value-name-size)
210 (format (current-error-port) "command ~S\n" command)
211 (format (current-error-port) "designsize ~S\n" designsize)
212 (format (current-error-port) "fontname ~S\n" fontname)
213 (format (current-error-port) "fontsize ~S\n" fontsize)
214 (format (current-error-port) "scaling ~S\n" scaling)))
218 " { /" (possibly-mangle-fontname fontname) " findfont "
219 (ly:number->string scaling)
220 "output-scale div scalefont setfont } bind def \n")))
222 (define (ps-encoded-fontswitch name-mag-pair)
223 (let* ((key (car name-mag-pair))
224 (value (cdr name-mag-pair))
225 (fontname (car value))
226 (scaling (cdr value)))
227 (cons key (cons value
229 "lilyfont" fontname "-" (number->string scaling))))))
231 (set! font-name-alist
232 (map ps-encoded-fontswitch internal-external-name-mag-pairs))
233 (apply string-append (map font-load-command font-name-alist)))
235 (define (define-origin file line col) "")
237 (define (dot x y radius)
241 (list x y radius)) " draw_dot"))
243 (define (draw-line thick x1 y1 x2 y2)
247 (ly:number->string thick)
249 (ly:number->string x1)
251 (ly:number->string y1)
253 (ly:number->string x2)
255 (ly:number->string y2)
259 "\nend-lilypond-output\n")
261 (define (expression->string expr)
262 (eval expr this-module))
264 (define (ez-ball ch letter-col ball-col)
267 (numbers->string (list letter-col ball-col))
268 " /Helvetica-Bold " ;; ugh
271 (define (filledbox breapth width depth height)
272 (string-append (numbers->string (list breapth width depth height))
275 (define (fontify name-mag-pair exp)
277 (define (select-font name-mag-pair)
278 (let ((c (assoc name-mag-pair font-name-alist)))
281 (string-append " " (cddr c) " ")
284 (format "Programming error: No such font: ~S" name-mag-pair))
286 (display "FAILED\n" (current-error-port))
287 (if #f ;(pair? name-mag-pair))
288 (display (object-type (car name-mag-pair)) (current-error-port))
289 (write name-mag-pair (current-error-port)))
290 (if #f ; (pair? font-name-alist)
292 (object-type (caaar font-name-alist)) (current-error-port))
293 (write font-name-alist (current-error-port)))
295 ;; (format #f "\n%FAILED: (select-font ~S)\n" name-mag-pair))
298 (string-append (select-font name-mag-pair) exp))
300 (define (header creator generate)
303 "%%Creator: " creator generate "\n"))
307 ;; URG: now we can't use scm output without Lily
308 (ly:gulp-file "lilyponddefs.ps")
309 "{exch pop //systemdict /run get exec}\n\n"
310 (ly:gulp-file "music-drawing-routines.ps")
311 "{ exch pop //systemdict /run get exec }\n\n"
312 ;; ps-testing wreaks havoc when used with lilypond-book.
313 ;; -- is this still true with new modules system?
314 ;; (if (defined? 'ps-testing) "\n /testing true def" "")
315 ; "\n /testing true def"
318 (define (horizontal-line x1 x2 th)
319 (draw-line th x1 0 x2 0))
321 (define (lily-def key val)
322 (let ((prefix "lilypondpaper"))
324 (substring key 0 (min (string-length prefix) (string-length key)))
326 (string-append "/" key " {" val "} bind def\n")
327 (string-append "/" key " (" val ") def\n"))))
329 (define (make-title port)
331 (let ((x-ext (ly:stencil-get-extent header-stencil Y))
332 (y-ext (ly:stencil-get-extent header-stencil X)))
333 (display (start-system
334 (/ (interval-length x-ext) OUTPUT-SCALE)
335 (/ (interval-length y-ext) OUTPUT-SCALE))
337 (output-stencil port (ly:stencil-get-expr header-stencil) '(0 . 0))
338 (display (stop-system) port)))
341 (define (no-origin) "")
343 ;; FIXME: duplictates output-scopes, duplicated in other backends
344 ;; FIXME: silly interface name
345 (define (output-paper-def pd)
346 (let ((prefix "lilypondpaper"))
348 (define (scope-entry->string key var)
349 (let ((val (variable-ref var)))
351 ((string? val) (ps-string-def prefix key val))
352 ((number? val) (ps-number-def prefix key val))
357 (module-map scope-entry->string (ly:output-def-scope pd)))))
359 ;; FIXME: duplicated in other output backends
360 ;; FIXME: silly interface name
361 (define (output-scopes paper scopes fields basename)
363 ;; FIXME: customise/generate these
364 (let* ((linewidth (ly:paper-get-number paper 'linewidth))
365 (props (cons (acons 'linewidth linewidth '()) PROPS))
369 (define (scope-entry->string key var)
370 (let ((val (variable-ref var)))
372 (if (memq key fields)
373 (header-to-file basename key val))
378 (format (current-error-port) "PROPS:~S\n" val)
379 (set! props (cons val props))
382 ;; define strings, for /make-lilypond-title to pick up
383 ((string? val) (ps-string-def prefix key val))
385 ;; generate stencil from markup
386 ((markup? val) (set! stencils
390 (interpret-markup paper props val))))
392 ((number? val) (ps-number-def prefix key val))
395 (define (output-scope scope)
396 (apply string-append (module-map scope-entry->string scope)))
398 (let ((s (string-append (apply string-append (map output-scope scopes)))))
399 (set! header-stencil (stack-lines DOWN 0 BASELINE-SKIP stencils))
401 ;; match systems, which are also aligned to center
402 (ly:stencil-align-to! header-stencil Y CENTER)
405 (ly:stencil-get-expr header-stencil)
408 ;; hmm, looks like recursing call is always last statement, does guile
410 (define (output-stencil port expr offset)
412 (let ((head (car expr)))
414 ((ly:input-location? head)
415 (display (apply define-origin (ly:input-location head)) port)
416 (output-stencil port (cadr expr) offset))
417 ((eq? head 'no-origin)
418 (display (expression->string head) port)
419 (output-stencil port (cadr expr) offset))
420 ((eq? head 'translate-stencil)
421 (output-stencil port (caddr expr) (offset-add offset (cadr expr))))
422 ((eq? head 'combine-stencil)
423 (output-stencil port (cadr expr) offset)
424 (output-stencil port (caddr expr) offset))
426 (display (placebox (car offset) (cdr offset)
427 (expression->string expr)) port))))))
429 (define (placebox x y s)
431 (ly:number->string x) " " (ly:number->string y) " {" s "} place-box\n"))
433 (define (polygon points blotdiameter)
436 (numbers->string points)
437 (ly:number->string (/ (length points) 2))
438 (ly:number->string blotdiameter)
441 (define (repeat-slash wid slope thick)
443 (numbers->string (list wid slope thick))
444 " draw_repeat_slash"))
446 (define (round-filled-box x y width height blotdiam)
450 (list x y width height blotdiam)) " draw_round_box"))
452 (define (start-system width height)
454 "\n" (ly:number->string height)
457 "set-ps-scale-to-lily-scale\n"))
459 (define (stem breapth width depth height)
461 (numbers->string (list breapth width depth height))
464 (define (stop-last-system)
467 (define (stop-system)
470 (define (symmetric-x-triangle thick w h)
472 (numbers->string (list h w thick))
473 " draw_symmetric_x_triangle"))
476 ;; (string-append "(" (escape-parentheses s) ") show "))
477 (string-append "(" (ps-encoding s) ") show "))
480 (define (top-of-file)
482 (header (string-append "GNU LilyPond (" (lilypond-version) "), ")
483 (strftime "%c" (localtime (current-time))))
484 LATIN1-ENCODING-COMMANDS
488 (string-append "Engraved by LilyPond (" (lilypond-version) ")"))))
493 (define (zigzag-line centre? zzw zzh thick dx dy)
495 (if centre? "true" "false")
497 (ly:number->string zzw)
499 (ly:number->string zzh)
501 (ly:number->string thick)
503 (ly:number->string dx)
505 (ly:number->string dy)
506 " draw_zigzag_line "))