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))
91 ;; ("ecmb12" . "ISOLatin1Encoding")))
93 (define (ps-encoding text)
94 (escape-parentheses text))
97 (define (ps-string-def prefix key val)
98 (string-append "/" prefix (symbol->string key) " ("
99 (escape-parentheses val)
102 (define (ps-number-def prefix key val)
103 (let ((s (if (integer? val)
104 (ly:number->string val)
105 (ly:number->string (exact->inexact val)))))
106 (string-append "/" prefix (symbol->string key) " " s " def\n")))
108 (define (tex-font? fontname)
109 (equal? (substring fontname 0 2) "cm"))
113 ;;; Lily output interface, PostScript implementation --- cleanup and docme
116 ;;; Output-interface functions
117 (define (beam width slope thick blot)
119 (ly:numbers->string (list slope width thick blot)) " draw_beam" ))
122 (define (bezier-sandwich l thick)
124 (string-join (map ly:number-pair->string l) " ")
126 (ly:number->string thick)
127 " draw_bezier_sandwich"))
129 (define (bracket arch_angle arch_width arch_height height arch_thick thick)
132 (list arch_angle arch_width arch_height height arch_thick thick))
135 (define (char font i)
137 (font-command font) " setfont "
138 "(\\" (ly:inexact->string i 8) ") show" ))
141 (string-append "% " s "\n"))
143 (define (dashed-line thick on off dx dy)
145 (ly:number->string dx) " "
146 (ly:number->string dy) " "
147 (ly:number->string thick)
149 (ly:number->string on) " "
150 (ly:number->string off)
151 " ] 0 draw_dashed_line"))
153 ;; what the heck is this interface ?
154 (define (dashed-slur thick dash l)
156 (string-join (map ly:number-pair->string l) " ")
158 (ly:number->string thick)
160 (ly:number->string dash)
163 (ly:number->string (* 10 thick))
164 " ] 0 draw_dashed_slur"))
166 (define (font-command font)
169 (string-encode-integer
170 (hashq (ly:font-name font) 1000000))
172 (string-encode-integer
173 (inexact->exact (round (* 1000 (ly:font-magnification font)))))))
175 (define (define-fonts paper font-list)
176 (define (define-font command fontname scaling)
178 "/" command " { /" fontname " findfont "
180 (ly:number->string scaling) " output-scale div scalefont } bind def\n"))
181 ;;(ly:number->string scaling) " scalefont } bind def\n"))
183 (define (reencode-font raw encoding command)
185 raw " " encoding " /" command " reencode-font\n"
186 "/" command "{ /" command " findfont 1 scalefont } bind def\n"))
188 ;; frobnicate NAME to jibe with external definitions.
189 (define (possibly-mangle-fontname fontname)
191 ((tex-font? fontname)
192 ;; FIXME: we need proper Fontmap for CM fonts, like so:
193 ;; /CMR10 (cmr10.pfb);
194 ;; (string-upcase fontname)
195 (string-append fontname ".pfb"))
196 ((or (equal? (substring fontname 0 4) "feta")
197 (equal? (substring fontname 0 4) "parm"))
198 (regexp-substitute/global
199 #f "(feta|parmesan)([a-z-]*)([0-9]+)"
200 fontname 'pre "GNU-LilyPond-" 1 2 "-" 3 'post))
203 (define (font-load-command paper font)
204 (let* ((command (font-command font))
205 (fontname (ly:font-name font))
206 (mangled (possibly-mangle-fontname fontname))
207 (coding-alist (ly:font-encoding-alist font))
208 (encoding (assoc-get 'input-name coding-alist))
209 (designsize (ly:font-design-size font))
210 (magnification (* (ly:font-magnification font)))
211 (ops (ly:paper-lookup paper 'outputscale))
212 (scaling (* ops magnification designsize)))
218 (format (current-error-port) "fontname ~S\n" fontname)
219 (format (current-error-port) "command ~S\n" command)
220 (format (current-error-port) "encoding name ~S\n" encoding)
221 (format (current-error-port) "mangled ~S\n" mangled)
222 (format (current-error-port) "designsize ~S\n" designsize)
223 (format (current-error-port) "magnification ~S\n" magnification)
224 (format (current-error-port) "ops ~S\n" ops)
225 (format (current-error-port) "scaling ~S\n" scaling)))
227 (if (equal? encoding "AdobeStandardEncoding")
228 (define-font command mangled scaling)
229 ;; FIXME: should rather tag encoded font
230 (let ((raw (string-append command "-raw"))
231 (vector (get-coding-command encoding)))
233 (define-font raw mangled scaling)
234 (reencode-font raw vector command))))))
236 (define (font-load-encoding encoding)
237 (let ((filename (get-coding-filename encoding)))
238 (ly:kpathsea-gulp-file filename)))
240 (let* ((encoding-list (map (lambda (x)
241 (assoc-get 'input-name
242 (ly:font-encoding-alist x)))
244 (encodings (uniq-list (sort-list encoding-list string<?))))
247 (apply string-append (map font-load-encoding encodings))
249 (map (lambda (x) (font-load-command paper x)) fonts)))))
251 (define (define-origin file line col) "")
253 (define (dot x y radius)
257 (list x y radius)) " draw_dot"))
259 (define (draw-line thick x1 y1 x2 y2)
261 "1 setlinecap 1 setlinejoin "
262 (ly:number->string thick) " setlinewidth "
263 (ly:number->string x1) " "
264 (ly:number->string y1) " moveto "
265 (ly:number->string x2) " "
266 (ly:number->string y2) " lineto stroke"))
269 "\nend-lilypond-output\n")
271 (define (ez-ball ch letter-col ball-col)
274 (ly:numbers->string (list letter-col ball-col))
275 " /Helvetica-Bold " ;; ugh
278 (define (filledbox breapth width depth height)
279 (string-append (ly:numbers->string (list breapth width depth height))
282 (define (header creator time-stamp page-count-)
283 (set! page-count page-count-)
287 "%%Creator: " creator " " time-stamp "\n"
288 "%%Pages: " (number->string page-count) "\n"
289 "%%PageOrder: Ascend\n"
290 ;; FIXME: TODO get from paper
291 ;; "%%DocumentPaperSizes: a6\n"
292 ;;(string-append "GNU LilyPond (" (lilypond-version) "), ")
293 ;; (strftime "%c" (localtime (current-time))))
294 ;; FIXME: duplicated in every backend
297 (string-append "Engraved by LilyPond (version " (lilypond-version) ")"))))
301 (ly:gulp-file "lilyponddefs.ps")
302 (ly:gulp-file "music-drawing-routines.ps")))
304 (define (horizontal-line x1 x2 th)
305 (draw-line th x1 0 x2 0))
307 (define (lily-def key val)
308 (let ((prefix "lilypondpaper"))
310 (substring key 0 (min (string-length prefix) (string-length key)))
312 (string-append "/" key " {" val "} bind def\n")
313 (string-append "/" key " (" val ") def\n"))))
315 (define (no-origin) "")
317 ;; FIXME: duplictates output-scopes, duplicated in other backends
318 ;; FIXME: silly interface name
319 (define (output-paper-def pd)
320 (let ((prefix "lilypondpaper"))
322 (define (scope-entry->string key var)
323 (if (variable-bound? var)
324 (let ((val (variable-ref var)))
326 ((string? val) (ps-string-def prefix key val))
327 ((number? val) (ps-number-def prefix key val))
333 (module-map scope-entry->string (ly:output-def-scope pd)))))
335 ;; FIXME: duplicated in other output backends
336 ;; FIXME: silly interface name
337 (define (output-scopes paper scopes fields basename)
338 (let ((prefix "lilypond"))
340 ;; FIXME: duplicates output-paper's scope-entry->string, mostly
341 (define (scope-entry->string key var)
342 (if (variable-bound? var)
343 (let ((val (variable-ref var)))
344 (if (and (memq key fields) (string? val))
345 (header-to-file basename key val))
347 ((string? val) (ps-string-def prefix key val))
348 ((number? val) (ps-number-def prefix key val))
352 (define (output-scope scope)
353 (apply string-append (module-map scope-entry->string scope)))
355 (string-append (apply string-append (map output-scope scopes)))))
357 ;; hmm, looks like recursing call is always last statement, does guile
359 (define (output-stencil port expr offset)
361 (let ((head (car expr)))
363 ((ly:input-location? head)
364 (display (apply define-origin (ly:input-location head)) port)
365 (output-stencil port (cadr expr) offset))
366 ((eq? head 'no-origin)
367 (display (expression->string head) port)
368 (output-stencil port (cadr expr) offset))
369 ((eq? head 'translate-stencil)
370 (output-stencil port (caddr expr) (offset-add offset (cadr expr))))
371 ((eq? head 'combine-stencil)
372 (output-stencil port (cadr expr) offset)
373 (output-stencil port (caddr expr) offset))
375 (display (placebox (car offset) (cdr offset)
376 (expression->string expr)) port))))))
378 (define (placebox x y s)
380 (ly:number->string x) " " (ly:number->string y) " { " s " } place-box\n"))
382 (define (polygon points blotdiameter)
384 (ly:numbers->string points) " "
385 (ly:number->string (/ (length points) 2)) " "
386 (ly:number->string blotdiameter)
389 (define (repeat-slash wid slope thick)
391 (ly:numbers->string (list wid slope thick))
392 " draw_repeat_slash"))
394 (define (round-filled-box x y width height blotdiam)
397 (list x y width height blotdiam)) " draw_round_box"))
399 (define (new-start-system origin dim)
401 "\n" (ly:number-pair->string origin) " start-system\n"
403 "set-ps-scale-to-lily-scale\n"))
405 (define (stem breapth width depth height)
407 (ly:numbers->string (list breapth width depth height))
410 (define (stop-system)
413 (define stop-last-system stop-system)
415 (define (symmetric-x-triangle thick w h)
417 (ly:numbers->string (list h w thick))
418 " draw_symmetric_x_triangle"))
420 (define (text font s)
422 ;; (string-append "(" (escape-parentheses s) ") show "))
425 (font-command font) " setfont "
426 "(" (ps-encoding s) ") show"))
431 (define (zigzag-line centre? zzw zzh thick dx dy)
433 (if centre? "true" "false") " "
434 (ly:number->string zzw) " "
435 (ly:number->string zzh) " "
436 (ly:number->string thick) " "
438 (ly:number->string dx) " "
439 (ly:number->string dy)
440 " draw_zigzag_line"))
443 (set! page-number (+ page-number 1))
445 "%%Page: " (number->string page-number) " " (number->string page-count) "\n"
448 (define (stop-page last?)