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 . override-coding-command)
167 (let* ((name (ly:font-name font))
168 (magnify (ly:font-magnification font))
169 (coding-alist (ly:font-encoding-alist font))
170 (input-encoding (assoc-get 'input-name coding-alist))
171 (font-encoding (assoc-get 'output-name coding-alist))
172 (coding-command (if (not (null? override-coding-command))
173 (car override-coding-command)
174 (get-coding-command font-encoding))))
177 "magfont" (string-encode-integer (hashq name 1000000))
178 "m" (string-encode-integer (inexact->exact (round (* 1000 magnify))))
179 (if (equal? input-encoding font-encoding) ""
180 (string-append "e" coding-command)))))
182 (define (define-fonts paper font-list)
184 (define (define-font command fontname scaling)
186 "/" command " { /" fontname " findfont "
187 (ly:number->string scaling) " output-scale div scalefont } bind def\n"))
189 (define (reencode-font plain encoding command)
190 (let ((coding-vector (get-coding-command encoding)))
192 plain " " coding-vector " /" command " reencode-font\n"
193 "/" command "{ /" command " findfont 1 scalefont } bind def\n")))
195 (define (guess-ps-fontname basename)
196 "We do not have the FontName, try to guess is from basename."
198 ((tex-font? basename)
199 ;; FIXME: we need proper Fontmap for the bluesky CM*, EC* fonts.
200 ;; Only the fonts that we trace in mf/ are in our own FontMap.
201 (string-append basename ".pfb"))
202 ((or (equal? (substring basename 0 4) "feta")
203 (equal? (substring basename 0 4) "parm"))
204 (regexp-substitute/global
205 #f "(feta|parmesan)([a-z-]*)([0-9]+)"
206 basename 'pre "GNU-LilyPond-" 1 2 "-" 3 'post))
209 (define (font-load-command paper font)
210 (let* ((basename (ly:font-name font))
211 (fontname (guess-ps-fontname basename))
212 (coding-alist (ly:font-encoding-alist font))
213 (input-encoding (assoc-get 'input-name coding-alist))
214 (font-encoding (assoc-get 'output-name coding-alist))
215 (plain (font-command font (get-coding-command font-encoding)))
216 (command (font-command font))
217 (designsize (ly:font-design-size font))
218 (magnification (* (ly:font-magnification font)))
219 (ops (ly:paper-lookup paper 'outputscale))
220 (scaling (* ops magnification designsize)))
223 (define-font plain fontname scaling)
224 (if (or (equal? input-encoding font-encoding)
226 (equal? font-encoding "fetaBraces")
227 (equal? font-encoding "fetaNumber")
228 (equal? font-encoding "fetaMusic")
229 (equal? font-encoding "parmesanMusic"))
231 (reencode-font plain input-encoding command)))))
233 (define (font-load-encoding encoding)
234 (let ((filename (get-coding-filename encoding)))
235 (ly:kpathsea-gulp-file filename)))
237 (let* ((encoding-list (map (lambda (x)
238 (assoc-get 'input-name
239 (ly:font-encoding-alist x)))
241 (encodings (uniq-list (sort-list encoding-list string<?))))
244 (apply string-append (map font-load-encoding encodings))
246 (map (lambda (x) (font-load-command paper x)) font-list)))))
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 (header creator time-stamp page-count-)
280 (set! page-count page-count-)
284 "%%Creator: " creator " " time-stamp "\n"
285 "%%Pages: " (number->string page-count) "\n"
286 "%%PageOrder: Ascend\n"
287 ;; FIXME: TODO get from paper
288 ;; "%%DocumentPaperSizes: a6\n"
289 ;;(string-append "GNU LilyPond (" (lilypond-version) "), ")
290 ;; (strftime "%c" (localtime (current-time))))
291 ;; FIXME: duplicated in every backend
294 (string-append "Engraved by LilyPond (version " (lilypond-version) ")"))))
298 (ly:gulp-file "lilyponddefs.ps")
299 (ly:gulp-file "music-drawing-routines.ps")))
301 (define (horizontal-line x1 x2 th)
302 (draw-line th x1 0 x2 0))
304 (define (lily-def key val)
305 (let ((prefix "lilypondpaper"))
307 (substring key 0 (min (string-length prefix) (string-length key)))
309 (string-append "/" key " {" val "} bind def\n")
310 (string-append "/" key " (" val ") def\n"))))
312 (define (no-origin) "")
314 ;; FIXME: duplictates output-scopes, duplicated in other backends
315 ;; FIXME: silly interface name
316 (define (output-paper-def pd)
317 (let ((prefix "lilypondpaper"))
319 (define (scope-entry->string key var)
320 (if (variable-bound? var)
321 (let ((val (variable-ref var)))
323 ((string? val) (ps-string-def prefix key val))
324 ((number? val) (ps-number-def prefix key val))
330 (module-map scope-entry->string (ly:output-def-scope pd)))))
332 ;; FIXME: duplicated in other output backends
333 ;; FIXME: silly interface name
334 (define (output-scopes paper scopes fields basename)
335 (let ((prefix "lilypond"))
337 ;; FIXME: duplicates output-paper's scope-entry->string, mostly
338 (define (scope-entry->string key var)
339 (if (variable-bound? var)
340 (let ((val (variable-ref var)))
341 (if (and (memq key fields) (string? val))
342 (header-to-file basename key val))
344 ((string? val) (ps-string-def prefix key val))
345 ((number? val) (ps-number-def prefix key val))
349 (define (output-scope scope)
350 (apply string-append (module-map scope-entry->string scope)))
352 (string-append (apply string-append (map output-scope scopes)))))
354 ;; hmm, looks like recursing call is always last statement, does guile
356 (define (output-stencil port expr offset)
358 (let ((head (car expr)))
360 ((ly:input-location? head)
361 (display (apply define-origin (ly:input-location head)) port)
362 (output-stencil port (cadr expr) offset))
363 ((eq? head 'no-origin)
364 (display (expression->string head) port)
365 (output-stencil port (cadr expr) offset))
366 ((eq? head 'translate-stencil)
367 (output-stencil port (caddr expr) (offset-add offset (cadr expr))))
368 ((eq? head 'combine-stencil)
369 (output-stencil port (cadr expr) offset)
370 (output-stencil port (caddr expr) offset))
372 (display (placebox (car offset) (cdr offset)
373 (expression->string expr)) port))))))
375 (define (placebox x y s)
377 (ly:number->string x) " " (ly:number->string y) " { " s " } place-box\n"))
379 (define (polygon points blotdiameter)
381 (ly:numbers->string points) " "
382 (ly:number->string (/ (length points) 2)) " "
383 (ly:number->string blotdiameter)
386 (define (repeat-slash wid slope thick)
388 (ly:numbers->string (list wid slope thick))
389 " draw_repeat_slash"))
391 (define (round-filled-box x y width height blotdiam)
394 (list x y width height blotdiam)) " draw_round_box"))
396 (define (new-start-system origin dim)
398 "\n" (ly:number-pair->string origin) " start-system\n"
400 "set-ps-scale-to-lily-scale\n"))
402 (define (stem breapth width depth height)
404 (ly:numbers->string (list breapth width depth height))
407 (define (stop-system)
410 (define stop-last-system stop-system)
412 (define (symmetric-x-triangle thick w h)
414 (ly:numbers->string (list h w thick))
415 " draw_symmetric_x_triangle"))
417 (define (text font s)
419 ;; (string-append "(" (escape-parentheses s) ") show "))
422 (font-command font) " setfont "
423 "(" (ps-encoding s) ") show"))
428 (define (zigzag-line centre? zzw zzh thick dx dy)
430 (if centre? "true" "false") " "
431 (ly:number->string zzw) " "
432 (ly:number->string zzh) " "
433 (ly:number->string thick) " "
435 (ly:number->string dx) " "
436 (ly:number->string dy)
437 " draw_zigzag_line"))
440 (set! page-number (+ page-number 1))
442 "%%Page: " (number->string page-number) " " (number->string page-count) "\n"
445 (define (stop-page last?)