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 ;;;; * papersize in header
13 ;;;; * special characters, encoding.
14 ;;;; + implement encoding switch (switches? input/output??),
15 ;;;; + move encoding definitions to ENCODING.ps files, or
16 ;;;; + find out which program's PS(?) encoding code we can use?
17 ;;;; * text setting, kerning.
18 ;;;; * document output-interface
20 (debug-enable 'backtrace)
22 (define-module (scm output-ps)
24 #:export (define-fonts
70 ;; alist containing fontname -> fontcommand assoc (both strings)
72 (define page-number 0)
73 (define font-name-alist '())
75 ;; /lilypondpaperoutputscale 1.75729901757299 def
76 ;;/lily-output-units 2.83464 def %% milimeter
77 ;;/output-scale lilypondpaperoutputscale lily-output-units mul def
79 ;; output-scale = 1.75729901757299 * 2.83464 = 4.9813100871731003736
81 (define OUTPUT-SCALE 4.98)
84 ;;; helper functions, not part of output interface
85 (define (escape-parentheses s)
86 (regexp-substitute/global #f "(^|[^\\])([\\(\\)])" s 'pre 1 "\\" 2 'post))
88 (define (offset-add a b)
89 (cons (+ (car a) (car b))
93 (define font-encoding-alist
94 '(("ecrm12" . "ISOLatin1Encoding")
95 ("ecmb12" . "ISOLatin1Encoding")))
97 (define (ps-encoding text)
98 (escape-parentheses text))
101 (define (ps-string-def prefix key val)
102 (string-append "/" prefix (symbol->string key) " ("
103 (escape-parentheses val)
106 (define (ps-number-def prefix key val)
107 (let ((s (if (integer? val)
108 (ly:number->string val)
109 (ly:number->string (exact->inexact val)))))
110 (string-append "/" prefix (symbol->string key) " " s " def\n")))
112 (define (tex-font? fontname)
113 (equal? (substring fontname 0 2) "cm"))
117 ;;; Lily output interface, PostScript implementation --- cleanup and docme
120 ;;; Output-interface functions
121 (define (beam width slope thick blot)
123 (ly:numbers->string (list slope width thick blot)) " draw_beam" ))
126 (define (bezier-sandwich l thick)
128 (string-join (map ly:number-pair->string l) " ")
130 (ly:number->string thick)
131 " draw_bezier_sandwich"))
133 (define (bracket arch_angle arch_width arch_height height arch_thick thick)
136 (list arch_angle arch_width arch_height height arch_thick thick))
141 "(\\" (ly:inexact->string i 8) ") show" ))
144 (string-append "% " s "\n"))
146 (define (dashed-line thick on off dx dy)
148 (ly:number->string dx) " "
149 (ly:number->string dy) " "
150 (ly:number->string thick)
152 (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 (string-join (map ly:number-pair->string l) " ")
161 (ly:number->string thick)
163 (ly:number->string dash)
166 (ly:number->string (* 10 thick))
167 " ] 0 draw_dashed_slur"))
169 (define (define-fonts internal-external-name-mag-pairs)
171 (define (fontname->designsize fontname)
172 (let ((i (string-index fontname char-numeric?)))
173 (string->number (substring fontname i))))
175 (define (define-font command fontname scaling)
177 "/" command " { /" fontname " findfont "
178 (ly:number->string scaling) " output-scale div scalefont } bind def\n"))
180 (define (reencode-font raw encoding command)
182 raw " " encoding " /" command " reencode-font\n"
183 "/" command "{ /" command " findfont 1 scalefont } bind def\n"))
185 ;; frobnicate NAME to jibe with external definitions.
186 (define (possibly-mangle-fontname fontname)
188 ((tex-font? fontname)
189 ;; FIXME: we need proper Fontmap for CM fonts, like so:
190 ;; /CMR10 (cmr10.pfb);
191 ;; (string-upcase fontname)
192 (string-append fontname ".pfb"))
193 ((or (equal? (substring fontname 0 4) "feta")
194 (equal? (substring fontname 0 4) "parm"))
195 (regexp-substitute/global
196 #f "(feta|parmesan)([a-z-]*)([0-9]+)"
197 fontname 'pre "GNU-LilyPond-" 1 2 "-" 3 'post))
200 ;; (define (font-load-command name-mag command)
201 (define (font-load-command fonts)
202 (let* ((key-name-size (car lst))
204 (value-name-size (car value))
205 (command (font-command font))
206 (fontname (ly:font-name font))
207 (mangled (possibly-mangle-fontname fontname))
208 (encoding (assoc-get fontname font-encoding-alist))
209 (designsize (ly:font-design-size font))
210 (fontsize (cdr value-name-size))
211 (scaling (* fontsize designsize)))
217 (format (current-error-port) "key-name-size ~S\n" key-name-size)
218 (format (current-error-port) "value ~S\n" value)
219 (format (current-error-port) "value-name-size ~S\n" value-name-size)
220 (format (current-error-port) "command ~S\n" command)
221 (format (current-error-port) "designsize ~S\n" designsize)
222 (format (current-error-port) "fontname ~S\n" fontname)
223 (format (current-error-port) "mangled ~S\n" mangled)
224 (format (current-error-port) "fontsize ~S\n" fontsize)
225 (format (current-error-port) "scaling ~S\n" scaling)))
228 ;; FIXME: should rather tag encoded font
229 (let ((raw (string-append command "-raw")))
231 (define-font raw mangled scaling)
232 (reencode-font raw encoding command)))
233 (define-font command mangled scaling))))
235 (define (ps-encoded-fontswitch name-mag-pair)
236 (let* ((key (car name-mag-pair))
237 (value (cdr name-mag-pair))
238 (fontname (car value))
239 (scaling (cdr value)))
240 (cons key (cons value
242 "lilyfont" fontname "-" (ly:number->string scaling))))))
244 (set! font-name-alist
245 (map ps-encoded-fontswitch internal-external-name-mag-pairs))
246 (apply string-append (map font-load-command font-name-alist)))
248 (define (define-origin file line col) "")
250 (define (dot x y radius)
254 (list x y radius)) " draw_dot"))
256 (define (draw-line thick x1 y1 x2 y2)
258 "1 setlinecap 1 setlinejoin "
259 (ly:number->string thick) " setlinewidth "
260 (ly:number->string x1) " "
261 (ly:number->string y1) " moveto "
262 (ly:number->string x2) " "
263 (ly:number->string y2) " lineto stroke"))
266 "\nend-lilypond-output\n")
268 (define (ez-ball ch letter-col ball-col)
271 (ly:numbers->string (list letter-col ball-col))
272 " /Helvetica-Bold " ;; ugh
275 (define (filledbox breapth width depth height)
276 (string-append (ly:numbers->string (list breapth width depth height))
279 (define (fontify name-mag-pair exp)
281 (define (select-font name-mag-pair)
282 (let ((c (assoc name-mag-pair font-name-alist)))
285 (string-append (cddr c) " setfont ")
288 (format "Programming error: No such font: ~S" name-mag-pair))
291 (string-append (select-font name-mag-pair) exp))
293 (define (header creator time-stamp page-count-)
294 (set! page-count page-count-)
298 "%%Creator: " creator " " time-stamp "\n"
299 "%%Pages: " (number->string page-count) "\n"
300 "%%PageOrder: Ascend\n"
301 ;; FIXME: TODO get from paper
302 ;; "%%DocumentPaperSizes: a6\n"
303 ;;(string-append "GNU LilyPond (" (lilypond-version) "), ")
304 ;; (strftime "%c" (localtime (current-time))))
305 ;; FIXME: duplicated in every backend
308 (string-append "Engraved by LilyPond (version " (lilypond-version) ")"))))
312 (ly:gulp-file "lilyponddefs.ps")
313 (ly:gulp-file "music-drawing-routines.ps")))
315 (define (horizontal-line x1 x2 th)
316 (draw-line th x1 0 x2 0))
318 (define (lily-def key val)
319 (let ((prefix "lilypondpaper"))
321 (substring key 0 (min (string-length prefix) (string-length key)))
323 (string-append "/" key " {" val "} bind def\n")
324 (string-append "/" key " (" val ") def\n"))))
326 (define (no-origin) "")
328 ;; FIXME: duplictates output-scopes, duplicated in other backends
329 ;; FIXME: silly interface name
330 (define (output-paper-def pd)
331 (let ((prefix "lilypondpaper"))
333 (define (scope-entry->string key var)
334 (let ((val (variable-ref var)))
336 ((string? val) (ps-string-def prefix key val))
337 ((number? val) (ps-number-def prefix key val))
342 (module-map scope-entry->string (ly:output-def-scope pd)))))
344 ;; FIXME: duplicated in other output backends
345 ;; FIXME: silly interface name
346 (define (output-scopes paper scopes fields basename)
347 (let ((prefix "lilypond"))
349 ;; FIXME: duplicates output-paper's scope-entry->string, mostly
350 (define (scope-entry->string key var)
351 (if (variable-bound? var)
352 (let ((val (variable-ref var)))
353 (if (and (memq key fields) (string? val))
354 (header-to-file basename key val))
356 ((string? val) (ps-string-def prefix key val))
357 ((number? val) (ps-number-def prefix key val))
361 (define (output-scope scope)
362 (apply string-append (module-map scope-entry->string scope)))
364 (string-append (apply string-append (map output-scope scopes)))))
366 ;; hmm, looks like recursing call is always last statement, does guile
368 (define (output-stencil port expr offset)
370 (let ((head (car expr)))
372 ((ly:input-location? head)
373 (display (apply define-origin (ly:input-location head)) port)
374 (output-stencil port (cadr expr) offset))
375 ((eq? head 'no-origin)
376 (display (expression->string head) port)
377 (output-stencil port (cadr expr) offset))
378 ((eq? head 'translate-stencil)
379 (output-stencil port (caddr expr) (offset-add offset (cadr expr))))
380 ((eq? head 'combine-stencil)
381 (output-stencil port (cadr expr) offset)
382 (output-stencil port (caddr expr) offset))
384 (display (placebox (car offset) (cdr offset)
385 (expression->string expr)) port))))))
387 (define (placebox x y s)
389 (ly:number->string x) " " (ly:number->string y) " { " s " } place-box\n"))
391 (define (polygon points blotdiameter)
393 (ly:numbers->string points) " "
394 (ly:number->string (/ (length points) 2)) " "
395 (ly:number->string blotdiameter)
398 (define (repeat-slash wid slope thick)
400 (ly:numbers->string (list wid slope thick))
401 " draw_repeat_slash"))
403 (define (round-filled-box x y width height blotdiam)
406 (list x y width height blotdiam)) " draw_round_box"))
408 (define (new-start-system origin dim)
410 "\n" (ly:number-pair->string origin) " start-system\n"
412 "set-ps-scale-to-lily-scale\n"))
414 (define (stem breapth width depth height)
416 (ly:numbers->string (list breapth width depth height))
419 (define (stop-system)
422 (define stop-last-system stop-system)
424 (define (symmetric-x-triangle thick w h)
426 (ly:numbers->string (list h w thick))
427 " draw_symmetric_x_triangle"))
430 ;; (string-append "(" (escape-parentheses s) ") show "))
431 (string-append "(" (ps-encoding s) ") show"))
436 (define (zigzag-line centre? zzw zzh thick dx dy)
438 (if centre? "true" "false") " "
439 (ly:number->string zzw) " "
440 (ly:number->string zzh) " "
441 (ly:number->string thick) " "
443 (ly:number->string dx) " "
444 (ly:number->string dy)
445 " draw_zigzag_line"))
448 (set! page-number (+ page-number 1))
450 "%%Page: " (number->string page-number) " " (number->string page-count) "\n"
453 (define (stop-page last?)