1 ;;;; output-ps.scm -- implement Scheme output interface 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 ;;;; * %% Papersize in (header ...)
13 ;;;; * text setting, kerning.
14 ;;;; * document output-interface
16 (debug-enable 'backtrace)
18 (define-module (scm output-ps)
20 #:export (define-fonts
67 (define page-number 0)
69 ;;; helper functions, not part of output interface
70 (define (escape-parentheses s)
71 (regexp-substitute/global #f "(^|[^\\])([\\(\\)])" s 'pre 1 "\\" 2 'post))
73 (define (offset-add a b)
74 (cons (+ (car a) (car b))
77 (define (ps-encoding text)
78 (escape-parentheses text))
81 (define (ps-string-def prefix key val)
82 (string-append "/" prefix (symbol->string key) " ("
83 (escape-parentheses val)
86 (define (ps-number-def prefix key val)
87 (let ((s (if (integer? val)
88 (ly:number->string val)
89 (ly:number->string (exact->inexact val)))))
90 (string-append "/" prefix (symbol->string key) " " s " def\n")))
92 (define (tex-font? fontname)
93 (equal? (substring fontname 0 2) "cm"))
97 ;;; Lily output interface, PostScript implementation --- cleanup and docme
100 ;;; Output-interface functions
101 (define (beam width slope thick blot)
103 (ly:numbers->string (list slope width thick blot)) " draw_beam" ))
106 (define (bezier-sandwich l thick)
108 (string-join (map ly:number-pair->string l) " ")
110 (ly:number->string thick)
111 " draw_bezier_sandwich"))
113 (define (bracket arch_angle arch_width arch_height height arch_thick thick)
116 (list arch_angle arch_width arch_height height arch_thick thick))
119 (define (char font i)
121 (font-command font) " setfont "
122 "(\\" (ly:inexact->string i 8) ") show" ))
125 (string-append "% " s "\n"))
127 (define (dashed-line thick on off dx dy)
129 (ly:number->string dx) " "
130 (ly:number->string dy) " "
131 (ly:number->string thick)
133 (ly:number->string on) " "
134 (ly:number->string off)
135 " ] 0 draw_dashed_line"))
137 ;; what the heck is this interface ?
138 (define (dashed-slur thick dash l)
140 (string-join (map ly:number-pair->string l) " ")
142 (ly:number->string thick)
144 (ly:number->string dash)
147 (ly:number->string (* 10 thick))
148 " ] 0 draw_dashed_slur"))
150 ;; FIXME -- now that we can have ENCODING == #f, this can be
151 ;; simplified, esp OVERRIDE-CODING-COMMAND
152 (define (font-command font . override-coding-command)
153 (let* ((name (ly:font-filename font))
154 (magnify (ly:font-magnification font))
155 (coding-alist (ly:font-encoding-alist font))
156 (input-encoding (assoc-get 'input-name coding-alist))
157 (font-encoding (assoc-get 'output-name coding-alist))
158 (coding-command (if (not (null? override-coding-command))
159 (car override-coding-command)
161 (get-coding-command font-encoding)
165 "magfont" (string-encode-integer (hashq name 1000000))
166 "m" (string-encode-integer (inexact->exact (round (* 1000 magnify))))
167 (if (or (not font-encoding) (equal? input-encoding font-encoding)) ""
168 (string-append "e" coding-command)))))
170 (define (define-fonts paper font-list)
172 (define (define-font command fontname scaling)
174 "/" command " { /" fontname " findfont "
175 (ly:number->string scaling) " output-scale div scalefont } bind def\n"))
177 (define (reencode-font plain encoding command)
178 (let ((coding-vector (get-coding-command encoding)))
180 plain " " coding-vector " /" command " reencode-font\n"
181 "/" command "{ /" command " findfont 1 scalefont } bind def\n")))
183 (define (guess-ps-fontname basename)
184 "We do not have the FontName, try to guess is from basename."
186 ((tex-font? basename)
187 ;; FIXME: we need proper Fontmap for the bluesky CM*, EC* fonts.
188 ;; Only the fonts that we trace in mf/ are in our own FontMap.
189 (string-append basename ".pfb"))
190 (else (string-append basename ".pfa"))
193 (define (font-load-command paper font)
194 (let* ((specced-font-name (ly:font-name font))
195 (fontname (if specced-font-name
197 (guess-ps-fontname (ly:font-filename font))))
199 (coding-alist (ly:font-encoding-alist font))
200 (input-encoding (assoc-get 'input-name coding-alist))
201 (font-encoding (assoc-get 'output-name coding-alist))
202 (command (font-command font))
203 ;; FIXME -- see (font-command )
204 (plain (if font-encoding (get-coding-command font-encoding)
206 (designsize (ly:font-design-size font))
207 (magnification (* (ly:font-magnification font)))
208 (ops (ly:paper-lookup paper 'outputscale))
209 (scaling (* ops magnification designsize)))
212 (define-font plain fontname scaling)
213 (if (or (equal? input-encoding font-encoding)
215 (equal? font-encoding "fetaBraces")
216 (equal? font-encoding "fetaNumber")
217 (equal? font-encoding "fetaMusic")
218 (equal? font-encoding "parmesanMusic"))
220 (reencode-font plain input-encoding command)))))
222 (define (font-load-encoding encoding)
223 (let ((filename (get-coding-filename encoding)))
224 (ly:kpathsea-gulp-file filename)))
226 (let* ((encoding-list (map (lambda (x)
227 (assoc-get 'input-name
228 (ly:font-encoding-alist x)))
230 (encodings (uniq-list (sort-list (filter string? encoding-list)
234 (apply string-append (map font-load-encoding encodings))
236 (map (lambda (x) (font-load-command paper x)) font-list)))))
238 (define (define-origin file line col) "")
240 (define (dot x y radius)
244 (list x y radius)) " draw_dot"))
246 (define (draw-line thick x1 y1 x2 y2)
248 "1 setlinecap 1 setlinejoin "
249 (ly:number->string thick) " setlinewidth "
250 (ly:number->string x1) " "
251 (ly:number->string y1) " moveto "
252 (ly:number->string x2) " "
253 (ly:number->string y2) " lineto stroke"))
256 "\nend-lilypond-output\n")
258 (define (ez-ball ch letter-col ball-col)
261 (ly:numbers->string (list letter-col ball-col))
262 " /Helvetica-Bold " ;; ugh
265 (define (filledbox breapth width depth height)
266 (string-append (ly:numbers->string (list breapth width depth height))
269 (define (header creator time-stamp page-count-)
270 (set! page-count page-count-)
274 "%%Creator: " creator " " time-stamp "\n"
275 "%%Pages: " (number->string page-count) "\n"
276 "%%PageOrder: Ascend\n"
277 ;; FIXME: TODO get from paper
278 ;; "%%DocumentPaperSizes: a6\n"
279 ;;(string-append "GNU LilyPond (" (lilypond-version) "), ")
280 ;; (strftime "%c" (localtime (current-time))))
281 ;; FIXME: duplicated in every backend
284 (string-append "Engraved by LilyPond (version " (lilypond-version) ")"))))
288 (ly:gulp-file "lilyponddefs.ps")
289 (ly:gulp-file "music-drawing-routines.ps")))
291 (define (horizontal-line x1 x2 th)
292 (draw-line th x1 0 x2 0))
294 (define (lily-def key val)
295 (let ((prefix "lilypondpaper"))
297 (substring key 0 (min (string-length prefix) (string-length key)))
299 (string-append "/" key " {" val "} bind def\n")
300 (string-append "/" key " (" val ") def\n"))))
302 (define (no-origin) "")
304 ;; FIXME: duplictates output-scopes, duplicated in other backends
305 ;; FIXME: silly interface name
306 (define (output-paper-def pd)
307 (let ((prefix "lilypondpaper"))
309 (define (scope-entry->string key var)
310 (if (variable-bound? var)
311 (let ((val (variable-ref var)))
313 ((string? val) (ps-string-def prefix key val))
314 ((number? val) (ps-number-def prefix key val))
320 (module-map scope-entry->string (ly:output-def-scope pd)))))
322 ;; FIXME: duplicated in other output backends
323 ;; FIXME: silly interface name
324 (define (output-scopes paper scopes fields basename)
325 (let ((prefix "lilypond"))
327 ;; FIXME: duplicates output-paper's scope-entry->string, mostly
328 (define (scope-entry->string key var)
329 (if (variable-bound? var)
330 (let ((val (variable-ref var)))
331 (if (and (memq key fields) (string? val))
332 (header-to-file basename key val))
334 ((string? val) (ps-string-def prefix key val))
335 ((number? val) (ps-number-def prefix key val))
339 (define (output-scope scope)
340 (apply string-append (module-map scope-entry->string scope)))
342 (string-append (apply string-append (map output-scope scopes)))))
344 ;; hmm, looks like recursing call is always last statement, does guile
346 (define (output-stencil port expr offset)
348 (let ((head (car expr)))
350 ((ly:input-location? head)
351 (display (apply define-origin (ly:input-location head)) port)
352 (output-stencil port (cadr expr) offset))
353 ((eq? head 'no-origin)
354 (display (expression->string head) port)
355 (output-stencil port (cadr expr) offset))
356 ((eq? head 'translate-stencil)
357 (output-stencil port (caddr expr) (offset-add offset (cadr expr))))
358 ((eq? head 'combine-stencil)
359 (output-stencil port (cadr expr) offset)
360 (output-stencil port (caddr expr) offset))
362 (display (placebox (car offset) (cdr offset)
363 (expression->string expr)) port))))))
365 (define (placebox x y s)
367 (ly:number->string x) " " (ly:number->string y) " { " s " } place-box\n"))
369 (define (polygon points blotdiameter)
371 (ly:numbers->string points) " "
372 (ly:number->string (/ (length points) 2)) " "
373 (ly:number->string blotdiameter)
376 (define (repeat-slash wid slope thick)
378 (ly:numbers->string (list wid slope thick))
379 " draw_repeat_slash"))
381 (define (round-filled-box x y width height blotdiam)
384 (list x y width height blotdiam)) " draw_round_box"))
386 (define (start-system origin dim)
388 "\n" (ly:number-pair->string origin) " start-system\n"
390 "set-ps-scale-to-lily-scale\n"))
392 (define (stem breapth width depth height)
394 (ly:numbers->string (list breapth width depth height))
397 (define (stop-system last?)
400 (define (symmetric-x-triangle thick w h)
402 (ly:numbers->string (list h w thick))
403 " draw_symmetric_x_triangle"))
405 (define (text font s)
409 ;; ugh, we should find a better way to
410 ;; extract the hsbw for /space from the font.
412 (space-length (cdar (ly:text-dimension font "t")))
414 (add-command (lambda (x) (set! commands (cons x commands)))) )
418 "Translate space as into moveto, group the rest in words."
419 (if (and (< 0 (string-length word))
420 (equal? #\space chr))
422 (string-append "(" (ps-encoding word) ") show\n")))
424 (if (equal? #\space chr)
425 (add-command (string-append (number->string space-length) " 0.0 rmoveto ")) )
427 (if (equal? #\space chr)
429 (string-append word (make-string 1 chr))))
431 (string-append s " "))
434 (font-command font) " setfont "
435 (string-join (reverse commands)))
441 (define (zigzag-line centre? zzw zzh thick dx dy)
443 (if centre? "true" "false") " "
444 (ly:number->string zzw) " "
445 (ly:number->string zzh) " "
446 (ly:number->string thick) " "
448 (ly:number->string dx) " "
449 (ly:number->string dy)
450 " draw_zigzag_line"))
453 (set! page-number (+ page-number 1))
455 "%%Page: " (number->string page-number) " " (number->string page-count) "\n"
458 (define (stop-page last?)