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
68 (define page-number 0)
70 ;;; helper functions, not part of output interface
71 (define (escape-parentheses s)
72 (regexp-substitute/global #f "(^|[^\\])([\\(\\)])" s 'pre 1 "\\" 2 'post))
74 (define (offset-add a b)
75 (cons (+ (car a) (car b))
78 (define (ps-encoding text)
79 (escape-parentheses text))
82 (define (ps-string-def prefix key val)
83 (string-append "/" prefix (symbol->string key) " ("
84 (escape-parentheses val)
87 (define (ps-number-def prefix key val)
88 (let ((s (if (integer? val)
89 (ly:number->string val)
90 (ly:number->string (exact->inexact val)))))
91 (string-append "/" prefix (symbol->string key) " " s " def\n")))
93 (define (tex-font? fontname)
94 (equal? (substring fontname 0 2) "cm"))
98 ;;; Lily output interface, PostScript implementation --- cleanup and docme
101 ;;; Output-interface functions
102 (define (beam width slope thick blot)
104 (ly:numbers->string (list slope width thick blot)) " draw_beam" ))
107 (define (bezier-sandwich l thick)
109 (string-join (map ly:number-pair->string l) " ")
111 (ly:number->string thick)
112 " draw_bezier_sandwich"))
114 (define (bracket arch_angle arch_width arch_height height arch_thick thick)
117 (list arch_angle arch_width arch_height height arch_thick thick))
120 (define (char font i)
122 (font-command font) " setfont "
123 "(\\" (ly:inexact->string i 8) ") show" ))
126 (string-append "% " s "\n"))
128 (define (dashed-line thick on off dx dy)
130 (ly:number->string dx) " "
131 (ly:number->string dy) " "
132 (ly:number->string thick)
134 (ly:number->string on) " "
135 (ly:number->string off)
136 " ] 0 draw_dashed_line"))
138 ;; what the heck is this interface ?
139 (define (dashed-slur thick dash l)
141 (string-join (map ly:number-pair->string l) " ")
143 (ly:number->string thick)
145 (ly:number->string dash)
148 (ly:number->string (* 10 thick))
149 " ] 0 draw_dashed_slur"))
151 ;; FIXME -- now that we can have ENCODING == #f, this can be
152 ;; simplified, esp OVERRIDE-CODING-COMMAND
153 (define (font-command font . override-coding-command)
154 (let* ((name (ly:font-filename font))
155 (magnify (ly:font-magnification font))
156 (coding-alist (ly:font-encoding-alist font))
157 (input-encoding (assoc-get 'input-name coding-alist))
158 (font-encoding (assoc-get 'output-name coding-alist))
159 (coding-command (if (not (null? override-coding-command))
160 (car override-coding-command)
162 (get-coding-command font-encoding)
166 "magfont" (string-encode-integer (hashq name 1000000))
167 "m" (string-encode-integer (inexact->exact (round (* 1000 magnify))))
168 (if (or (not font-encoding) (equal? input-encoding font-encoding)) ""
169 (string-append "e" coding-command)))))
171 (define (define-fonts paper font-list)
173 (define (define-font command fontname scaling)
175 "/" command " { /" fontname " findfont "
176 (ly:number->string scaling) " output-scale div scalefont } bind def\n"))
178 (define (reencode-font plain encoding command)
179 (let ((coding-vector (get-coding-command encoding)))
181 plain " " coding-vector " /" command " reencode-font\n"
182 "/" command "{ /" command " findfont 1 scalefont } bind def\n")))
184 (define (guess-ps-fontname basename)
185 "We do not have the FontName, try to guess is from basename."
187 ((tex-font? basename)
188 ;; FIXME: we need proper Fontmap for the bluesky CM*, EC* fonts.
189 ;; Only the fonts that we trace in mf/ are in our own FontMap.
190 (string-append basename ".pfb"))
191 (else (string-append basename ".pfa"))
194 (define (font-load-command paper font)
195 (let* ((specced-font-name (ly:font-name font))
196 (fontname (if specced-font-name
198 (guess-ps-fontname (ly:font-filename font))))
200 (coding-alist (ly:font-encoding-alist font))
201 (input-encoding (assoc-get 'input-name coding-alist))
202 (font-encoding (assoc-get 'output-name coding-alist))
203 (command (font-command font))
204 ;; FIXME -- see (font-command )
205 (plain (if font-encoding (get-coding-command font-encoding)
207 (designsize (ly:font-design-size font))
208 (magnification (* (ly:font-magnification font)))
209 (ops (ly:paper-lookup paper 'outputscale))
210 (scaling (* ops magnification designsize)))
213 (define-font plain fontname scaling)
214 (if (or (equal? input-encoding font-encoding)
216 (equal? font-encoding "fetaBraces")
217 (equal? font-encoding "fetaNumber")
218 (equal? font-encoding "fetaMusic")
219 (equal? font-encoding "parmesanMusic"))
221 (reencode-font plain input-encoding command)))))
223 (define (font-load-encoding encoding)
224 (let ((filename (get-coding-filename encoding)))
225 (ly:kpathsea-gulp-file filename)))
227 (let* ((encoding-list (map (lambda (x)
228 (assoc-get 'input-name
229 (ly:font-encoding-alist x)))
231 (encodings (uniq-list (sort-list (filter string? encoding-list)
235 (apply string-append (map font-load-encoding encodings))
237 (map (lambda (x) (font-load-command paper x)) font-list)))))
239 (define (define-origin file line col) "")
241 (define (dot x y radius)
245 (list x y radius)) " draw_dot"))
247 (define (draw-line thick x1 y1 x2 y2)
249 "1 setlinecap 1 setlinejoin "
250 (ly:number->string thick) " setlinewidth "
251 (ly:number->string x1) " "
252 (ly:number->string y1) " moveto "
253 (ly:number->string x2) " "
254 (ly:number->string y2) " lineto stroke"))
257 "\nend-lilypond-output\n")
259 (define (ez-ball ch letter-col ball-col)
262 (ly:numbers->string (list letter-col ball-col))
263 " /Helvetica-Bold " ;; ugh
266 (define (filledbox breapth width depth height)
267 (string-append (ly:numbers->string (list breapth width depth height))
270 (define (header creator time-stamp page-count-)
271 (set! page-count page-count-)
275 "%%Creator: " creator " " time-stamp "\n"
276 "%%Pages: " (number->string page-count) "\n"
277 "%%PageOrder: Ascend\n"
278 ;; FIXME: TODO get from paper
279 ;; "%%DocumentPaperSizes: a6\n"
280 ;;(string-append "GNU LilyPond (" (lilypond-version) "), ")
281 ;; (strftime "%c" (localtime (current-time))))
282 ;; FIXME: duplicated in every backend
285 (string-append "Engraved by LilyPond (version " (lilypond-version) ")"))))
289 (ly:gulp-file "lilyponddefs.ps")
290 (ly:gulp-file "music-drawing-routines.ps")))
292 (define (horizontal-line x1 x2 th)
293 (draw-line th x1 0 x2 0))
295 (define (lily-def key val)
296 (let ((prefix "lilypondpaper"))
298 (substring key 0 (min (string-length prefix) (string-length key)))
300 (string-append "/" key " {" val "} bind def\n")
301 (string-append "/" key " (" val ") def\n"))))
303 (define (no-origin) "")
305 ;; FIXME: duplictates output-scopes, duplicated in other backends
306 ;; FIXME: silly interface name
307 (define (output-paper-def pd)
308 (let ((prefix "lilypondpaper"))
310 (define (scope-entry->string key var)
311 (if (variable-bound? var)
312 (let ((val (variable-ref var)))
314 ((string? val) (ps-string-def prefix key val))
315 ((number? val) (ps-number-def prefix key val))
321 (module-map scope-entry->string (ly:output-def-scope pd)))))
323 ;; FIXME: duplicated in other output backends
324 ;; FIXME: silly interface name
325 (define (output-scopes paper scopes fields basename)
326 (let ((prefix "lilypond"))
328 ;; FIXME: duplicates output-paper's scope-entry->string, mostly
329 (define (scope-entry->string key var)
330 (if (variable-bound? var)
331 (let ((val (variable-ref var)))
332 (if (and (memq key fields) (string? val))
333 (header-to-file basename key val))
335 ((string? val) (ps-string-def prefix key val))
336 ((number? val) (ps-number-def prefix key val))
340 (define (output-scope scope)
341 (apply string-append (module-map scope-entry->string scope)))
343 (string-append (apply string-append (map output-scope scopes)))))
345 ;; hmm, looks like recursing call is always last statement, does guile
347 (define (output-stencil port expr offset)
349 (let ((head (car expr)))
351 ((ly:input-location? head)
352 (display (apply define-origin (ly:input-location head)) port)
353 (output-stencil port (cadr expr) offset))
354 ((eq? head 'no-origin)
355 (display (expression->string head) port)
356 (output-stencil port (cadr expr) offset))
357 ((eq? head 'translate-stencil)
358 (output-stencil port (caddr expr) (offset-add offset (cadr expr))))
359 ((eq? head 'combine-stencil)
360 (output-stencil port (cadr expr) offset)
361 (output-stencil port (caddr expr) offset))
363 (display (placebox (car offset) (cdr offset)
364 (expression->string expr)) port))))))
366 (define (placebox x y s)
368 (ly:number->string x) " " (ly:number->string y) " { " s " } place-box\n"))
370 (define (polygon points blotdiameter)
372 (ly:numbers->string points) " "
373 (ly:number->string (/ (length points) 2)) " "
374 (ly:number->string blotdiameter)
377 (define (repeat-slash wid slope thick)
379 (ly:numbers->string (list wid slope thick))
380 " draw_repeat_slash"))
382 (define (round-filled-box x y width height blotdiam)
385 (list x y width height blotdiam)) " draw_round_box"))
387 (define (new-start-system origin dim)
389 "\n" (ly:number-pair->string origin) " start-system\n"
391 "set-ps-scale-to-lily-scale\n"))
393 (define (stem breapth width depth height)
395 (ly:numbers->string (list breapth width depth height))
398 (define (stop-system)
401 (define stop-last-system stop-system)
403 (define (symmetric-x-triangle thick w h)
405 (ly:numbers->string (list h w thick))
406 " draw_symmetric_x_triangle"))
408 (define (text font s)
409 (string-append (font-command font) " setfont "
410 "(" (ps-encoding s) ") show"))
415 (define (zigzag-line centre? zzw zzh thick dx dy)
417 (if centre? "true" "false") " "
418 (ly:number->string zzw) " "
419 (ly:number->string zzh) " "
420 (ly:number->string thick) " "
422 (ly:number->string dx) " "
423 (ly:number->string dy)
424 " draw_zigzag_line"))
427 (set! page-number (+ page-number 1))
429 "%%Page: " (number->string page-number) " " (number->string page-count) "\n"
432 (define (stop-page last?)