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)
74 ;; /lilypondpaperoutputscale 1.75729901757299 def
75 ;;/lily-output-units 2.83464 def %% milimeter
76 ;;/output-scale lilypondpaperoutputscale lily-output-units mul def
78 ;; output-scale = 1.75729901757299 * 2.83464 = 4.9813100871731003736
80 (define OUTPUT-SCALE 4.98)
83 ;;; helper functions, not part of output interface
84 (define (escape-parentheses s)
85 (regexp-substitute/global #f "(^|[^\\])([\\(\\)])" s 'pre 1 "\\" 2 'post))
87 (define (offset-add a b)
88 (cons (+ (car a) (car b))
92 (define font-encoding-alist
93 '(("ecrm12" . "ISOLatin1Encoding")
94 ("ecmb12" . "ISOLatin1Encoding")))
96 (define (ps-encoding text)
97 (escape-parentheses text))
100 (define (ps-string-def prefix key val)
101 (string-append "/" prefix (symbol->string key) " ("
102 (escape-parentheses val)
105 (define (ps-number-def prefix key val)
106 (let ((s (if (integer? val)
107 (ly:number->string val)
108 (ly:number->string (exact->inexact val)))))
109 (string-append "/" prefix (symbol->string key) " " s " def\n")))
111 (define (tex-font? fontname)
112 (equal? (substring fontname 0 2) "cm"))
116 ;;; Lily output interface, PostScript implementation --- cleanup and docme
119 ;;; Output-interface functions
120 (define (beam width slope thick blot)
122 (ly:numbers->string (list slope width thick blot)) " draw_beam" ))
125 (define (bezier-sandwich l thick)
127 (string-join (map ly:number-pair->string l) " ")
129 (ly:number->string thick)
130 " draw_bezier_sandwich"))
132 (define (bracket arch_angle arch_width arch_height height arch_thick thick)
135 (list arch_angle arch_width arch_height height arch_thick thick))
140 "(\\" (ly:inexact->string i 8) ") show" ))
143 (string-append "% " s "\n"))
145 (define (dashed-line thick on off dx dy)
147 (ly:number->string dx) " "
148 (ly:number->string dy) " "
149 (ly:number->string thick)
151 (ly:number->string on) " "
152 (ly:number->string off)
153 " ] 0 draw_dashed_line"))
155 ;; what the heck is this interface ?
156 (define (dashed-slur thick dash l)
158 (string-join (map ly:number-pair->string l) " ")
160 (ly:number->string thick)
162 (ly:number->string dash)
165 (ly:number->string (* 10 thick))
166 " ] 0 draw_dashed_slur"))
168 (define (font-command font)
171 (string-encode-integer
172 (hashq (ly:font-name font) 1000000))
174 (string-encode-integer
175 (inexact->exact (round (* 1000 (ly:font-magnification font)))))))
177 (define (define-fonts paper font-list)
179 (define (fontname->designsize fontname)
180 (let ((i (string-index fontname char-numeric?)))
181 (string->number (substring fontname i))))
183 (define (define-font command fontname scaling)
185 "/" command " { /" fontname " findfont "
187 (ly:number->string scaling) " output-scale div scalefont } bind def\n"))
188 ;;(ly:number->string scaling) " scalefont } bind def\n"))
190 (define (reencode-font raw encoding command)
192 raw " " encoding " /" command " reencode-font\n"
193 "/" command "{ /" command " findfont 1 scalefont } bind def\n"))
195 ;; frobnicate NAME to jibe with external definitions.
196 (define (possibly-mangle-fontname fontname)
198 ((tex-font? fontname)
199 ;; FIXME: we need proper Fontmap for CM fonts, like so:
200 ;; /CMR10 (cmr10.pfb);
201 ;; (string-upcase fontname)
202 (string-append fontname ".pfb"))
203 ((or (equal? (substring fontname 0 4) "feta")
204 (equal? (substring fontname 0 4) "parm"))
205 (regexp-substitute/global
206 #f "(feta|parmesan)([a-z-]*)([0-9]+)"
207 fontname 'pre "GNU-LilyPond-" 1 2 "-" 3 'post))
210 (define (font-load-command paper font)
211 (let* ((command (font-command font))
212 (fontname (ly:font-name font))
213 (mangled (possibly-mangle-fontname fontname))
214 (encoding (assoc-get fontname font-encoding-alist))
215 (designsize (ly:font-design-size font))
216 (magnification (* (ly:font-magnification font)))
217 (ops (ly:paper-lookup paper 'outputscale))
218 (scaling (* ops magnification designsize)) )
224 (format (current-error-port) "fontname ~S\n" fontname)
225 (format (current-error-port) "command ~S\n" command)
226 (format (current-error-port) "mangled ~S\n" mangled)
227 (format (current-error-port) "designsize ~S\n" designsize)
228 (format (current-error-port) "foo-design ~S\n" foo-design)
229 (format (current-error-port) "magnification ~S\n" magnification)
230 (format (current-error-port) "ops ~S\n" ops)
231 (format (current-error-port) "scaling ~S\n" scaling)))
234 ;; FIXME: should rather tag encoded font
235 (let ((raw (string-append command "-raw")))
237 (define-font raw mangled scaling)
238 (reencode-font raw encoding command)))
239 (define-font command mangled scaling))))
242 (map (lambda (x) (font-load-command paper x)) font-list)))
244 (define (define-origin file line col) "")
246 (define (dot x y radius)
250 (list x y radius)) " draw_dot"))
252 (define (draw-line thick x1 y1 x2 y2)
254 "1 setlinecap 1 setlinejoin "
255 (ly:number->string thick) " setlinewidth "
256 (ly:number->string x1) " "
257 (ly:number->string y1) " moveto "
258 (ly:number->string x2) " "
259 (ly:number->string y2) " lineto stroke"))
262 "\nend-lilypond-output\n")
264 (define (ez-ball ch letter-col ball-col)
267 (ly:numbers->string (list letter-col ball-col))
268 " /Helvetica-Bold " ;; ugh
271 (define (filledbox breapth width depth height)
272 (string-append (ly:numbers->string (list breapth width depth height))
275 (define (fontify font exp)
276 (string-append (font-command font) " setfont " exp))
278 (define (header creator time-stamp page-count-)
279 (set! page-count page-count-)
283 "%%Creator: " creator " " time-stamp "\n"
284 "%%Pages: " (number->string page-count) "\n"
285 "%%PageOrder: Ascend\n"
286 ;; FIXME: TODO get from paper
287 ;; "%%DocumentPaperSizes: a6\n"
288 ;;(string-append "GNU LilyPond (" (lilypond-version) "), ")
289 ;; (strftime "%c" (localtime (current-time))))
290 ;; FIXME: duplicated in every backend
293 (string-append "Engraved by LilyPond (version " (lilypond-version) ")"))))
297 (ly:gulp-file "lilyponddefs.ps")
298 (ly:gulp-file "music-drawing-routines.ps")))
300 (define (horizontal-line x1 x2 th)
301 (draw-line th x1 0 x2 0))
303 (define (lily-def key val)
304 (let ((prefix "lilypondpaper"))
306 (substring key 0 (min (string-length prefix) (string-length key)))
308 (string-append "/" key " {" val "} bind def\n")
309 (string-append "/" key " (" val ") def\n"))))
311 (define (no-origin) "")
313 ;; FIXME: duplictates output-scopes, duplicated in other backends
314 ;; FIXME: silly interface name
315 (define (output-paper-def pd)
316 (let ((prefix "lilypondpaper"))
318 (define (scope-entry->string key var)
319 (if (variable-bound? var)
320 (let ((val (variable-ref var)))
322 ((string? val) (ps-string-def prefix key val))
323 ((number? val) (ps-number-def prefix key val))
329 (module-map scope-entry->string (ly:output-def-scope pd)))))
331 ;; FIXME: duplicated in other output backends
332 ;; FIXME: silly interface name
333 (define (output-scopes paper scopes fields basename)
334 (let ((prefix "lilypond"))
336 ;; FIXME: duplicates output-paper's scope-entry->string, mostly
337 (define (scope-entry->string key var)
338 (if (variable-bound? var)
339 (let ((val (variable-ref var)))
340 (if (and (memq key fields) (string? val))
341 (header-to-file basename key val))
343 ((string? val) (ps-string-def prefix key val))
344 ((number? val) (ps-number-def prefix key val))
348 (define (output-scope scope)
349 (apply string-append (module-map scope-entry->string scope)))
351 (string-append (apply string-append (map output-scope scopes)))))
353 ;; hmm, looks like recursing call is always last statement, does guile
355 (define (output-stencil port expr offset)
357 (let ((head (car expr)))
359 ((ly:input-location? head)
360 (display (apply define-origin (ly:input-location head)) port)
361 (output-stencil port (cadr expr) offset))
362 ((eq? head 'no-origin)
363 (display (expression->string head) port)
364 (output-stencil port (cadr expr) offset))
365 ((eq? head 'translate-stencil)
366 (output-stencil port (caddr expr) (offset-add offset (cadr expr))))
367 ((eq? head 'combine-stencil)
368 (output-stencil port (cadr expr) offset)
369 (output-stencil port (caddr expr) offset))
371 (display (placebox (car offset) (cdr offset)
372 (expression->string expr)) port))))))
374 (define (placebox x y s)
376 (ly:number->string x) " " (ly:number->string y) " { " s " } place-box\n"))
378 (define (polygon points blotdiameter)
380 (ly:numbers->string points) " "
381 (ly:number->string (/ (length points) 2)) " "
382 (ly:number->string blotdiameter)
385 (define (repeat-slash wid slope thick)
387 (ly:numbers->string (list wid slope thick))
388 " draw_repeat_slash"))
390 (define (round-filled-box x y width height blotdiam)
393 (list x y width height blotdiam)) " draw_round_box"))
395 (define (new-start-system origin dim)
397 "\n" (ly:number-pair->string origin) " start-system\n"
399 "set-ps-scale-to-lily-scale\n"))
401 (define (stem breapth width depth height)
403 (ly:numbers->string (list breapth width depth height))
406 (define (stop-system)
409 (define stop-last-system stop-system)
411 (define (symmetric-x-triangle thick w h)
413 (ly:numbers->string (list h w thick))
414 " draw_symmetric_x_triangle"))
417 ;; (string-append "(" (escape-parentheses s) ") show "))
418 (string-append "(" (ps-encoding s) ") show"))
423 (define (zigzag-line centre? zzw zzh thick dx dy)
425 (if centre? "true" "false") " "
426 (ly:number->string zzw) " "
427 (ly:number->string zzh) " "
428 (ly:number->string thick) " "
430 (ly:number->string dx) " "
431 (ly:number->string dy)
432 " draw_zigzag_line"))
435 (set! page-number (+ page-number 1))
437 "%%Page: " (number->string page-number) " " (number->string page-count) "\n"
440 (define (stop-page last?)