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 ;;;; * move makeTitle (input/test/title-markup.ly) out of user space, but
13 ;;;; * allow customisation of makeTitle markup.
14 ;;;; * makePieceTitle markup
16 ;;;; * special characters, encoding.
17 ;;;; + implement encoding switch (switches? input/output??),
18 ;;;; + move encoding definitions to ENCODING.ps files, or
19 ;;;; + find out which program's PS(?) encoding code we can use?
20 ;;;; * text setting, kerning.
21 ;;;; * document output-interface
23 (debug-enable 'backtrace)
25 (define-module (scm output-ps))
26 (define this-module (current-module))
37 ;;; Lily output interface, PostScript implementation --- cleanup and docme
39 ;;; Output interface entry
40 (define-public (ps-output-expression expr port)
41 (display (expression->string expr) port))
44 ;; alist containing fontname -> fontcommand assoc (both strings)
45 (define font-name-alist '())
47 ;; /lilypondpaperoutputscale 1.75729901757299 def
48 ;;/lily-output-units 2.83464 def %% milimeter
49 ;;/output-scale lilypondpaperoutputscale lily-output-units mul def
51 ;; output-scale = 1.75729901757299 * 2.83464 = 4.9813100871731003736
53 (define OUTPUT-SCALE 4.98)
56 ;;; helper functions, not part of output interface
57 (define (escape-parentheses s)
58 (regexp-substitute/global #f "(^|[^\\])([\\(\\)])" s 'pre 1 "\\" 2 'post))
60 (define (offset-add a b)
61 (cons (+ (car a) (car b))
65 (define font-encoding-alist
66 '(("ecrm12" . "ISOLatin1Encoding")
67 ("ecmb12" . "ISOLatin1Encoding")))
69 (define (ps-encoding text)
70 (escape-parentheses text))
73 (define (ps-string-def prefix key val)
74 (string-append "/" prefix (symbol->string key) " ("
75 (escape-parentheses val)
78 (define (ps-number-def prefix key val)
79 (let ((s (if (integer? val)
81 (number->string (exact->inexact val)))))
82 (string-append "/" prefix (symbol->string key) " " s " def\n")))
84 (define (tex-font? fontname)
85 (equal? (substring fontname 0 2) "cm"))
87 ;;; Output-interface functions
88 (define (beam width slope thick blot)
90 (numbers->string (list slope width thick blot)) " draw_beam" ))
93 (define (bezier-sandwich l thick)
95 (apply string-append (map number-pair->string l))
96 (ly:number->string thick)
97 " draw_bezier_sandwich "))
99 (define (bracket arch_angle arch_width arch_height height arch_thick thick)
102 (list arch_angle arch_width arch_height height arch_thick thick))
107 "(\\" (inexact->string i 8) ") show " ))
110 (string-append "% " s "\n"))
112 (define (dashed-line thick on off dx dy)
114 (ly:number->string dx)
116 (ly:number->string dy)
118 (ly:number->string thick)
120 (ly:number->string on)
122 (ly:number->string off)
123 " ] 0 draw_dashed_line"))
125 ;; what the heck is this interface ?
126 (define (dashed-slur thick dash l)
128 (apply string-append (map number-pair->string l))
129 (ly:number->string thick)
131 (ly:number->string dash)
134 (ly:number->string (* 10 thick))
135 " ] 0 draw_dashed_slur"))
137 (define (define-fonts internal-external-name-mag-pairs)
139 (define (fontname->designsize fontname)
140 (let ((i (string-index fontname char-numeric?)))
141 (string->number (substring fontname i))))
143 (define (define-font command fontname scaling)
145 "/" command " { /" fontname " findfont "
146 (ly:number->string scaling) " output-scale div scalefont } bind def\n"))
148 (define (reencode-font raw encoding command)
150 raw " " encoding " /" command " reencode-font\n"
151 "/" command "{ /" command " findfont 1 scalefont } bind def\n"))
153 ;; frobnicate NAME to jibe with external definitions.
154 (define (possibly-mangle-fontname fontname)
156 ((tex-font? fontname)
157 ;; FIXME: we need proper Fontmap for CM fonts, like so:
158 ;; /CMR10 (cmr10.pfb);
159 ;; (string-upcase fontname)
160 (string-append fontname ".pfb"))
161 ((or (equal? (substring fontname 0 4) "feta")
162 (equal? (substring fontname 0 4) "parm"))
163 (regexp-substitute/global
164 #f "(feta|parmesan)([a-z-]*)([0-9]+)"
165 fontname 'pre "GNU-LilyPond-" 1 2 "-" 3 'post))
168 ;; (define (font-load-command name-mag command)
169 (define (font-load-command lst)
170 (let* ((key-name-size (car lst))
172 (value-name-size (car value))
173 (command (cdr value))
174 (fontname (car value-name-size))
175 (mangled (possibly-mangle-fontname fontname))
176 (encoding (assoc-get fontname font-encoding-alist))
177 (designsize (if (tex-font? fontname)
178 (/ 12 (fontname->designsize fontname))
179 ;; This is about 12/20 :-)
180 (cdr key-name-size)))
181 (fontsize (cdr value-name-size))
182 (scaling (* 12 (/ fontsize designsize)))
183 (scaling (/ fontsize (/ designsize 12))))
189 (format (current-error-port) "key-name-size ~S\n" key-name-size)
190 (format (current-error-port) "value ~S\n" value)
191 (format (current-error-port) "value-name-size ~S\n" value-name-size)
192 (format (current-error-port) "command ~S\n" command)
193 (format (current-error-port) "designsize ~S\n" designsize)
194 (format (current-error-port) "fontname ~S\n" fontname)
195 (format (current-error-port) "mangled ~S\n" mangled)
196 (format (current-error-port) "fontsize ~S\n" fontsize)
197 (format (current-error-port) "scaling ~S\n" scaling)))
200 ;; FIXME: should rather tag encoded font
201 (let ((raw (string-append command "-raw")))
203 (define-font raw mangled scaling)
204 (reencode-font raw encoding command)))
205 (define-font command mangled scaling))))
207 (define (ps-encoded-fontswitch name-mag-pair)
208 (let* ((key (car name-mag-pair))
209 (value (cdr name-mag-pair))
210 (fontname (car value))
211 (scaling (cdr value)))
212 (cons key (cons value
214 "lilyfont" fontname "-" (number->string scaling))))))
216 (set! font-name-alist
217 (map ps-encoded-fontswitch internal-external-name-mag-pairs))
218 (apply string-append (map font-load-command font-name-alist)))
220 (define (define-origin file line col) "")
222 (define (dot x y radius)
226 (list x y radius)) " draw_dot"))
228 (define (draw-line thick x1 y1 x2 y2)
232 (ly:number->string thick)
234 (ly:number->string x1)
236 (ly:number->string y1)
238 (ly:number->string x2)
240 (ly:number->string y2)
244 "\nend-lilypond-output\n")
246 (define (expression->string expr)
247 (eval expr this-module))
249 (define (ez-ball ch letter-col ball-col)
252 (numbers->string (list letter-col ball-col))
253 " /Helvetica-Bold " ;; ugh
256 (define (filledbox breapth width depth height)
257 (string-append (numbers->string (list breapth width depth height))
260 (define (fontify name-mag-pair exp)
262 (define (select-font name-mag-pair)
263 (let ((c (assoc name-mag-pair font-name-alist)))
266 (string-append " " (cddr c) " setfont ")
269 (format "Programming error: No such font: ~S" name-mag-pair))
272 (string-append (select-font name-mag-pair) exp))
274 (define (header creator generate)
277 "%%Creator: " creator generate "\n"))
281 (ly:gulp-file "lilyponddefs.ps")
282 (ly:gulp-file "music-drawing-routines.ps")))
284 (define (horizontal-line x1 x2 th)
285 (draw-line th x1 0 x2 0))
287 (define (lily-def key val)
288 (let ((prefix "lilypondpaper"))
290 (substring key 0 (min (string-length prefix) (string-length key)))
292 (string-append "/" key " {" val "} bind def\n")
293 (string-append "/" key " (" val ") def\n"))))
295 (define (no-origin) "")
297 ;; FIXME: duplictates output-scopes, duplicated in other backends
298 ;; FIXME: silly interface name
299 (define (output-paper-def pd)
300 (let ((prefix "lilypondpaper"))
302 (define (scope-entry->string key var)
303 (let ((val (variable-ref var)))
305 ((string? val) (ps-string-def prefix key val))
306 ((number? val) (ps-number-def prefix key val))
311 (module-map scope-entry->string (ly:output-def-scope pd)))))
313 ;; FIXME: duplicated in other output backends
314 ;; FIXME: silly interface name
315 (define (output-scopes paper scopes fields basename)
316 (let ((prefix "lilypond"))
318 ;; FIXME: duplicates output-paper's scope-entry->string, mostly
319 (define (scope-entry->string key var)
320 (let ((val (variable-ref var)))
321 (if (memq key fields)
322 (header-to-file basename key val))
324 ((string? val) (ps-string-def prefix key val))
325 ((number? val) (ps-number-def prefix key val))
328 (define (output-scope scope)
329 (apply string-append (module-map scope-entry->string scope)))
331 (string-append (apply string-append (map output-scope scopes)))))
333 ;; hmm, looks like recursing call is always last statement, does guile
335 (define (output-stencil port expr offset)
337 (let ((head (car expr)))
339 ((ly:input-location? head)
340 (display (apply define-origin (ly:input-location head)) port)
341 (output-stencil port (cadr expr) offset))
342 ((eq? head 'no-origin)
343 (display (expression->string head) port)
344 (output-stencil port (cadr expr) offset))
345 ((eq? head 'translate-stencil)
346 (output-stencil port (caddr expr) (offset-add offset (cadr expr))))
347 ((eq? head 'combine-stencil)
348 (output-stencil port (cadr expr) offset)
349 (output-stencil port (caddr expr) offset))
351 (display (placebox (car offset) (cdr offset)
352 (expression->string expr)) port))))))
354 (define (placebox x y s)
356 (ly:number->string x) " " (ly:number->string y) " {" s "} place-box\n"))
358 (define (polygon points blotdiameter)
361 (numbers->string points)
362 (ly:number->string (/ (length points) 2))
363 (ly:number->string blotdiameter)
366 (define (repeat-slash wid slope thick)
368 (numbers->string (list wid slope thick))
369 " draw_repeat_slash"))
371 (define (round-filled-box x y width height blotdiam)
375 (list x y width height blotdiam)) " draw_round_box"))
377 (define (start-system width height)
379 "\n" (ly:number->string height)
381 ;;(draw-line 2 0 (* OUTPUT-SCALE height) 100 0)
382 (draw-line 2 0 0 50 (- 0 (* OUTPUT-SCALE height)))
384 "set-ps-scale-to-lily-scale\n"))
386 (define (stem breapth width depth height)
388 (numbers->string (list breapth width depth height))
391 (define (stop-last-system)
394 (define (stop-system)
397 (define (symmetric-x-triangle thick w h)
399 (numbers->string (list h w thick))
400 " draw_symmetric_x_triangle"))
403 ;; (string-append "(" (escape-parentheses s) ") show "))
404 (string-append "(" (ps-encoding s) ") show "))
407 (define (top-of-file)
409 (header (string-append "GNU LilyPond (" (lilypond-version) "), ")
410 (strftime "%c" (localtime (current-time))))
414 (string-append "Engraved by LilyPond (" (lilypond-version) ")"))))
419 (define (zigzag-line centre? zzw zzh thick dx dy)
421 (if centre? "true" "false")
423 (ly:number->string zzw)
425 (ly:number->string zzh)
427 (ly:number->string thick)
429 (ly:number->string dx)
431 (ly:number->string dy)
432 " draw_zigzag_line "))
437 (define (stop-page last?)