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 ;;;; + implement encoding switch (switches? input/output??),
14 ;;;; + move encoding definitions to ENCODING.ps files, or
15 ;;;; + find out which program's PS(?) encoding code we can use?
16 ;;;; * text setting, kerning.
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
35 ;;; Output interface entry
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 ;; /lilypondpaperoutputscale 1.75729901757299 def
44 ;;/lily-output-units 2.83464 def %% milimeter
45 ;;/output-scale lilypondpaperoutputscale lily-output-units mul def
47 ;; output-scale = 1.75729901757299 * 2.83464 = 4.9813100871731003736
49 (define OUTPUT-SCALE 4.98)
52 ;;; helper functions, not part of output interface
53 (define (escape-parentheses s)
54 (regexp-substitute/global #f "(^|[^\\])([\\(\\)])" s 'pre 1 "\\" 2 'post))
56 (define (offset-add a b)
57 (cons (+ (car a) (car b))
61 (define font-encoding-alist
62 '(("ecrm12" . "ISOLatin1Encoding")
63 ("ecmb12" . "ISOLatin1Encoding")))
65 (define (ps-encoding text)
66 (escape-parentheses text))
69 (define (ps-string-def prefix key val)
70 (string-append "/" prefix (symbol->string key) " ("
71 (escape-parentheses val)
74 (define (ps-number-def prefix key val)
75 (let ((s (if (integer? val)
77 (number->string (exact->inexact val)))))
78 (string-append "/" prefix (symbol->string key) " " s " def\n")))
80 (define (tex-font? fontname)
81 (equal? (substring fontname 0 2) "cm"))
83 ;;; Output-interface functions
84 (define (beam width slope thick blot)
86 (numbers->string (list slope width thick blot)) " draw_beam" ))
89 (define (bezier-sandwich l thick)
91 (apply string-append (map number-pair->string l))
92 (ly:number->string thick)
93 " draw_bezier_sandwich "))
95 (define (bracket arch_angle arch_width arch_height height arch_thick thick)
98 (list arch_angle arch_width arch_height height arch_thick thick))
103 "(\\" (inexact->string i 8) ") show " ))
106 (string-append "% " s "\n"))
108 (define (dashed-line thick on off dx dy)
110 (ly:number->string dx)
112 (ly:number->string dy)
114 (ly:number->string thick)
116 (ly:number->string on)
118 (ly:number->string off)
119 " ] 0 draw_dashed_line"))
121 ;; what the heck is this interface ?
122 (define (dashed-slur thick dash l)
124 (apply string-append (map number-pair->string l))
125 (ly:number->string thick)
127 (ly:number->string dash)
130 (ly:number->string (* 10 thick))
131 " ] 0 draw_dashed_slur"))
133 (define (define-fonts internal-external-name-mag-pairs)
135 (define (fontname->designsize fontname)
136 (let ((i (string-index fontname char-numeric?)))
137 (string->number (substring fontname i))))
139 (define (define-font command fontname scaling)
141 "/" command " { /" fontname " findfont "
142 (ly:number->string scaling) " output-scale div scalefont } bind def\n"))
144 (define (reencode-font raw encoding command)
146 raw " " encoding " /" command " reencode-font\n"
147 "/" command "{ /" command " findfont 1 scalefont } bind def\n"))
149 ;; frobnicate NAME to jibe with external definitions.
150 (define (possibly-mangle-fontname fontname)
152 ((tex-font? fontname)
153 ;; FIXME: we need proper Fontmap for CM fonts, like so:
154 ;; /CMR10 (cmr10.pfb);
155 ;; (string-upcase fontname)
156 (string-append fontname ".pfb"))
157 ((or (equal? (substring fontname 0 4) "feta")
158 (equal? (substring fontname 0 4) "parm"))
159 (regexp-substitute/global
160 #f "(feta|parmesan)([a-z-]*)([0-9]+)"
161 fontname 'pre "GNU-LilyPond-" 1 2 "-" 3 'post))
164 ;; (define (font-load-command name-mag command)
165 (define (font-load-command lst)
166 (let* ((key-name-size (car lst))
168 (value-name-size (car value))
169 (command (cdr value))
170 (fontname (car value-name-size))
171 (mangled (possibly-mangle-fontname fontname))
172 (encoding (assoc-get fontname font-encoding-alist))
173 (designsize (if (tex-font? fontname)
174 (/ 12 (fontname->designsize fontname))
175 ;; This is about 12/20 :-)
176 (cdr key-name-size)))
177 (fontsize (cdr value-name-size))
178 (scaling (* 12 (/ fontsize designsize)))
179 (scaling (/ fontsize (/ designsize 12))))
185 (format (current-error-port) "key-name-size ~S\n" key-name-size)
186 (format (current-error-port) "value ~S\n" value)
187 (format (current-error-port) "value-name-size ~S\n" value-name-size)
188 (format (current-error-port) "command ~S\n" command)
189 (format (current-error-port) "designsize ~S\n" designsize)
190 (format (current-error-port) "fontname ~S\n" fontname)
191 (format (current-error-port) "mangled ~S\n" mangled)
192 (format (current-error-port) "fontsize ~S\n" fontsize)
193 (format (current-error-port) "scaling ~S\n" scaling)))
196 ;; FIXME: should rather tag encoded font
197 (let ((raw (string-append command "-raw")))
199 (define-font raw mangled scaling)
200 (reencode-font raw encoding command)))
201 (define-font command mangled scaling))))
203 (define (ps-encoded-fontswitch name-mag-pair)
204 (let* ((key (car name-mag-pair))
205 (value (cdr name-mag-pair))
206 (fontname (car value))
207 (scaling (cdr value)))
208 (cons key (cons value
210 "lilyfont" fontname "-" (number->string scaling))))))
212 (set! font-name-alist
213 (map ps-encoded-fontswitch internal-external-name-mag-pairs))
214 (apply string-append (map font-load-command font-name-alist)))
216 (define (define-origin file line col) "")
218 (define (dot x y radius)
222 (list x y radius)) " draw_dot"))
224 (define (draw-line thick x1 y1 x2 y2)
228 (ly:number->string thick)
230 (ly:number->string x1)
232 (ly:number->string y1)
234 (ly:number->string x2)
236 (ly:number->string y2)
240 "\nend-lilypond-output\n")
242 (define (expression->string expr)
243 (eval expr this-module))
245 (define (ez-ball ch letter-col ball-col)
248 (numbers->string (list letter-col ball-col))
249 " /Helvetica-Bold " ;; ugh
252 (define (filledbox breapth width depth height)
253 (string-append (numbers->string (list breapth width depth height))
256 (define (fontify name-mag-pair exp)
258 (define (select-font name-mag-pair)
259 (let ((c (assoc name-mag-pair font-name-alist)))
262 (string-append " " (cddr c) " setfont ")
265 (format "Programming error: No such font: ~S" name-mag-pair))
268 (string-append (select-font name-mag-pair) exp))
270 (define (header creator time-stamp)
273 "%%Creator: " creator " " time-stamp "\n"
274 ;;(string-append "GNU LilyPond (" (lilypond-version) "), ")
275 ;; (strftime "%c" (localtime (current-time))))
276 ;; FIXME: duplicated in every backend
279 (string-append "Engraved by LilyPond (version " (lilypond-version) ")"))))
283 (ly:gulp-file "lilyponddefs.ps")
284 (ly:gulp-file "music-drawing-routines.ps")))
286 (define (horizontal-line x1 x2 th)
287 (draw-line th x1 0 x2 0))
289 (define (lily-def key val)
290 (let ((prefix "lilypondpaper"))
292 (substring key 0 (min (string-length prefix) (string-length key)))
294 (string-append "/" key " {" val "} bind def\n")
295 (string-append "/" key " (" val ") def\n"))))
297 (define (no-origin) "")
299 ;; FIXME: duplictates output-scopes, duplicated in other backends
300 ;; FIXME: silly interface name
301 (define (output-paper-def pd)
302 (let ((prefix "lilypondpaper"))
304 (define (scope-entry->string key var)
305 (let ((val (variable-ref var)))
307 ((string? val) (ps-string-def prefix key val))
308 ((number? val) (ps-number-def prefix key val))
313 (module-map scope-entry->string (ly:output-def-scope pd)))))
315 ;; FIXME: duplicated in other output backends
316 ;; FIXME: silly interface name
317 (define (output-scopes paper scopes fields basename)
318 (let ((prefix "lilypond"))
320 ;; FIXME: duplicates output-paper's scope-entry->string, mostly
321 (define (scope-entry->string key var)
322 (if (variable-bound? var)
323 (let ((val (variable-ref var)))
324 (if (and (memq key fields) (string? val))
325 (header-to-file basename key val))
327 ((string? val) (ps-string-def prefix key val))
328 ((number? val) (ps-number-def prefix key val))
332 (define (output-scope scope)
333 (apply string-append (module-map scope-entry->string scope)))
335 (string-append (apply string-append (map output-scope scopes)))))
337 ;; hmm, looks like recursing call is always last statement, does guile
339 (define (output-stencil port expr offset)
341 (let ((head (car expr)))
343 ((ly:input-location? head)
344 (display (apply define-origin (ly:input-location head)) port)
345 (output-stencil port (cadr expr) offset))
346 ((eq? head 'no-origin)
347 (display (expression->string head) port)
348 (output-stencil port (cadr expr) offset))
349 ((eq? head 'translate-stencil)
350 (output-stencil port (caddr expr) (offset-add offset (cadr expr))))
351 ((eq? head 'combine-stencil)
352 (output-stencil port (cadr expr) offset)
353 (output-stencil port (caddr expr) offset))
355 (display (placebox (car offset) (cdr offset)
356 (expression->string expr)) port))))))
358 (define (placebox x y s)
360 (ly:number->string x) " " (ly:number->string y) " {" s "} place-box\n"))
362 (define (polygon points blotdiameter)
365 (numbers->string points)
366 (ly:number->string (/ (length points) 2))
367 (ly:number->string blotdiameter)
370 (define (repeat-slash wid slope thick)
372 (numbers->string (list wid slope thick))
373 " draw_repeat_slash"))
375 (define (round-filled-box x y width height blotdiam)
379 (list x y width height blotdiam)) " draw_round_box"))
381 (define (new-start-system origin dim)
383 "\n" (number-pair->string origin) " start-system\n"
385 "set-ps-scale-to-lily-scale\n"))
387 (define (stem breapth width depth height)
389 (numbers->string (list breapth width depth height))
392 (define (stop-system)
395 (define stop-last-system 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 "))
409 (define (zigzag-line centre? zzw zzh thick dx dy)
411 (if centre? "true" "false")
413 (ly:number->string zzw)
415 (ly:number->string zzh)
417 (ly:number->string thick)
419 (ly:number->string dx)
421 (ly:number->string dy)
422 " draw_zigzag_line "))
427 (define (stop-page last?)