]> git.donarmstrong.com Git - lilypond.git/blob - scm/output-ps.scm
* scm/lily.scm (get-output-module): New function.
[lilypond.git] / scm / output-ps.scm
1 ;;;; output-ps.scm -- implement Scheme output routines for PostScript
2 ;;;;
3 ;;;;  source file of the GNU LilyPond music typesetter
4 ;;;; 
5 ;;;; (c)  1998--2004 Jan Nieuwenhuizen <janneke@gnu.org>
6 ;;;;                 Han-Wen Nienhuys <hanwen@cs.uu.nl>
7
8 ;;;; Note: currently misused as testbed for titles with markup, see
9 ;;;;       input/test/title-markup.ly
10 ;;;; 
11 ;;;; TODO:
12 ;;;;   * special characters, encoding.
13 ;;;;     + implement encoding switch (switches?  input/output??),
14 ;;;;     + move encoding definitions to ENCODING.ps files, or
15 ;;;;     + find out which program's PS(?) encoding code we can use?
16 ;;;;   * text setting, kerning.
17 ;;;;   * document output-interface
18
19 ;;(if (not safe-mode?)
20 ;;    (debug-enable 'backtrace))
21 (debug-enable 'backtrace)
22
23 (define-module (scm output-ps))
24 (define this-module (current-module))
25
26 (use-modules
27  (guile)
28  (ice-9 regex)
29  (srfi srfi-13)
30  (lily))
31
32 (define (expression->string expr)
33   (eval expr this-module))
34
35 ;;; Output interface entry
36 (define (output-expression expr port)
37   (display (expression->string expr) port))
38
39
40 ;;; Global vars
41 ;; alist containing fontname -> fontcommand assoc (both strings)
42 (define page-count 0)
43 (define page-number 0)
44 (define font-name-alist '())
45
46 ;; /lilypondpaperoutputscale 1.75729901757299 def
47 ;;/lily-output-units 2.83464  def  %% milimeter
48 ;;/output-scale lilypondpaperoutputscale lily-output-units mul def
49 ;;
50 ;; output-scale = 1.75729901757299 * 2.83464 = 4.9813100871731003736
51
52 (define OUTPUT-SCALE 4.98)
53 (define TOP-MARGIN 0)
54
55 ;;; helper functions, not part of output interface
56 (define (escape-parentheses s)
57   (regexp-substitute/global #f "(^|[^\\])([\\(\\)])" s 'pre 1 "\\" 2 'post))
58
59 (define (offset-add a b)
60   (cons (+ (car a) (car b))
61         (+ (cdr a) (cdr b))))
62
63 ;; WIP
64 (define font-encoding-alist
65   '(("ecrm12" . "ISOLatin1Encoding")
66     ("ecmb12" . "ISOLatin1Encoding")))
67                  
68 (define (ps-encoding text)
69   (escape-parentheses text))
70
71 ;; FIXME: lily-def
72 (define (ps-string-def prefix key val)
73   (string-append "/" prefix (symbol->string key) " ("
74                  (escape-parentheses val)
75                  ") def\n"))
76
77 (define (ps-number-def prefix key val)
78   (let ((s (if (integer? val)
79                (number->string val)
80                (number->string (exact->inexact val)))))
81     (string-append "/" prefix (symbol->string key) " " s " def\n")))
82
83 (define (tex-font? fontname)
84   (equal? (substring fontname 0 2) "cm"))
85
86
87 ;;;
88 ;;; Lily output interface, PostScript implementation --- cleanup and docme
89 ;;;
90
91 ;;; Output-interface functions
92 (define (beam width slope thick blot)
93   (string-append
94    (numbers->string (list slope width thick blot)) " draw_beam" ))
95
96 ;; two beziers
97 (define (bezier-sandwich l thick)
98   (string-append 
99    (apply string-append (map number-pair->string l))
100    (ly:number->string thick)
101    " draw_bezier_sandwich "))
102
103 (define (bracket arch_angle arch_width arch_height  height arch_thick thick)
104   (string-append
105    (numbers->string
106     (list arch_angle arch_width arch_height height arch_thick thick))
107    " draw_bracket"))
108
109 (define (char i)
110   (string-append 
111    "(\\" (inexact->string i 8) ") show " ))
112
113 (define (comment s)
114   (string-append "% " s "\n"))
115
116 (define (dashed-line thick on off dx dy)
117   (string-append 
118    (ly:number->string dx)
119    " "
120    (ly:number->string dy)
121    " "
122    (ly:number->string thick)
123    " [ "
124    (ly:number->string on)
125    " "
126    (ly:number->string off)
127    " ] 0 draw_dashed_line"))
128
129 ;; what the heck is this interface ?
130 (define (dashed-slur thick dash l)
131   (string-append 
132    (apply string-append (map number-pair->string l)) 
133    (ly:number->string thick) 
134    " [ "
135    (ly:number->string dash)
136    " "
137    ;;UGH.  10 ?
138    (ly:number->string (* 10 thick))
139    " ] 0 draw_dashed_slur"))
140
141 (define (define-fonts internal-external-name-mag-pairs)
142
143   (define (fontname->designsize fontname)
144     (let ((i (string-index fontname char-numeric?)))
145       (string->number (substring fontname i))))
146   
147   (define (define-font command fontname scaling)
148     (string-append
149      "/" command " { /" fontname " findfont "
150      (ly:number->string scaling) " output-scale div scalefont } bind def\n"))
151
152   (define (reencode-font raw encoding command)
153     (string-append
154      raw " " encoding " /" command " reencode-font\n"
155      "/" command "{ /" command " findfont 1 scalefont } bind def\n"))
156           
157   ;; frobnicate NAME to jibe with external definitions.
158   (define (possibly-mangle-fontname fontname)
159     (cond
160      ((tex-font? fontname)
161       ;; FIXME: we need proper Fontmap for CM fonts, like so:
162       ;; /CMR10 (cmr10.pfb); 
163       ;; (string-upcase fontname)
164       (string-append fontname ".pfb"))
165      ((or (equal? (substring fontname 0 4) "feta")
166           (equal? (substring fontname 0 4) "parm"))
167       (regexp-substitute/global
168        #f "(feta|parmesan)([a-z-]*)([0-9]+)"
169        fontname 'pre "GNU-LilyPond-" 1 2 "-" 3 'post))
170      (else fontname)))
171                          
172   ;;  (define (font-load-command name-mag command)
173   (define (font-load-command lst)
174     (let* ((key-name-size (car lst))
175            (value (cdr lst))
176            (value-name-size (car value))
177            (command (cdr value))
178            (fontname (car value-name-size))
179            (mangled (possibly-mangle-fontname fontname))
180            (encoding (assoc-get fontname font-encoding-alist))
181            (designsize (if (tex-font? fontname)
182                            (/ 12 (fontname->designsize fontname))
183                            ;; This is about 12/20 :-)
184                            (cdr key-name-size)))
185            (fontsize (cdr value-name-size))
186            (scaling (* 12 (/ fontsize designsize)))
187            (scaling (/ fontsize (/ designsize 12))))
188
189       (if
190        #f
191        (begin
192          (newline)
193          (format (current-error-port) "key-name-size ~S\n" key-name-size)
194          (format (current-error-port) "value ~S\n" value)
195          (format (current-error-port) "value-name-size ~S\n" value-name-size)
196          (format (current-error-port) "command ~S\n" command)
197          (format (current-error-port) "designsize ~S\n" designsize)
198          (format (current-error-port) "fontname ~S\n" fontname)
199          (format (current-error-port) "mangled ~S\n" mangled)
200          (format (current-error-port) "fontsize ~S\n" fontsize)
201          (format (current-error-port) "scaling ~S\n" scaling)))
202       
203       (if encoding
204           ;; FIXME: should rather tag encoded font
205           (let ((raw (string-append command "-raw")))
206             (string-append
207              (define-font raw mangled scaling)
208              (reencode-font raw encoding command)))
209           (define-font command mangled scaling))))
210   
211   (define (ps-encoded-fontswitch name-mag-pair)
212     (let* ((key (car name-mag-pair))
213            (value (cdr name-mag-pair))
214            (fontname (car value))
215            (scaling (cdr value)))
216       (cons key (cons value
217                       (string-append
218                        "lilyfont" fontname "-" (number->string scaling))))))
219
220   (set! font-name-alist
221         (map ps-encoded-fontswitch internal-external-name-mag-pairs))
222   (apply string-append (map font-load-command font-name-alist)))
223
224 (define (define-origin file line col) "")
225
226 (define (dot x y radius)
227   (string-append
228    " "
229    (numbers->string
230     (list x y radius)) " draw_dot"))
231
232 (define (draw-line thick x1 y1 x2 y2)
233   (string-append 
234   "     1 setlinecap
235         1 setlinejoin "
236   (ly:number->string thick)
237         " setlinewidth "
238    (ly:number->string x1)
239    " "
240    (ly:number->string y1)
241    " moveto "
242    (ly:number->string x2)
243    " "
244    (ly:number->string y2)
245    " lineto stroke"))
246
247 (define (end-output)
248   "\nend-lilypond-output\n")
249
250 (define (ez-ball ch letter-col ball-col)
251   (string-append
252    " (" ch ") "
253    (numbers->string (list letter-col ball-col))
254    " /Helvetica-Bold " ;; ugh
255    " draw_ez_ball"))
256
257 (define (filledbox breapth width depth height) 
258   (string-append (numbers->string (list breapth width depth height))
259                  " draw_box"))
260
261 (define (fontify name-mag-pair exp)
262
263   (define (select-font name-mag-pair)
264     (let ((c (assoc name-mag-pair font-name-alist)))
265       
266       (if c
267           (string-append " " (cddr c) " setfont ")
268           (begin
269             (ly:warn
270              (format "Programming error: No such font: ~S" name-mag-pair))
271             ""))))
272   
273   (string-append (select-font name-mag-pair) exp))
274
275 (define (header creator time-stamp page-count-)
276   (set! page-count page-count-)
277   (set! page-number 0)
278   (string-append
279    "%!PS-Adobe-3.0\n"
280    "%%Creator: " creator " " time-stamp "\n"
281    "%%Pages: " (number->string page-count) "\n"
282    "%%PageOrder: Ascend\n"
283    ;;(string-append "GNU LilyPond (" (lilypond-version) "), ")
284    ;;      (strftime "%c" (localtime (current-time))))
285    ;; FIXME: duplicated in every backend
286    (ps-string-def
287     "lilypond" 'tagline
288     (string-append "Engraved by LilyPond (version " (lilypond-version) ")"))))
289
290 (define (header-end)
291   (string-append
292    (ly:gulp-file "lilyponddefs.ps")
293    (ly:gulp-file "music-drawing-routines.ps")))
294
295 (define (horizontal-line x1 x2 th)
296   (draw-line th x1  0 x2 0))
297
298 (define (lily-def key val)
299   (let ((prefix "lilypondpaper"))
300     (if (string=?
301          (substring key 0 (min (string-length prefix) (string-length key)))
302          prefix)
303         (string-append "/" key " {" val "} bind def\n")
304         (string-append "/" key " (" val ") def\n"))))
305
306 (define (no-origin) "")
307
308 ;; FIXME: duplictates output-scopes, duplicated in other backends
309 ;; FIXME: silly interface name
310 (define (output-paper-def pd)
311   (let ((prefix "lilypondpaper"))
312     
313     (define (scope-entry->string key var)
314       (let ((val (variable-ref var)))
315         (cond
316          ((string? val) (ps-string-def prefix key val))
317          ((number? val) (ps-number-def prefix key val))
318          (else ""))))
319       
320     (apply
321      string-append
322      (module-map scope-entry->string (ly:output-def-scope pd)))))
323
324 ;; FIXME: duplicated in other output backends
325 ;; FIXME: silly interface name
326 (define (output-scopes paper scopes fields basename)
327   (let ((prefix "lilypond"))
328
329     ;; FIXME: duplicates output-paper's scope-entry->string, mostly
330     (define (scope-entry->string key var)
331       (if (variable-bound? var)
332           (let ((val (variable-ref var)))
333             (if (and (memq key fields) (string? val))
334                 (header-to-file basename key val))
335             (cond
336              ((string? val) (ps-string-def prefix key val))
337              ((number? val) (ps-number-def prefix key val))
338              (else "")))
339           ""))
340     
341     (define (output-scope scope)
342       (apply string-append (module-map scope-entry->string scope)))
343
344     (string-append (apply string-append (map output-scope scopes)))))
345
346 ;; hmm, looks like recursing call is always last statement, does guile
347 ;; think so too?
348 (define (output-stencil port expr offset)
349   (if (pair? expr)
350       (let ((head (car expr)))
351         (cond
352          ((ly:input-location? head)
353           (display (apply define-origin (ly:input-location head)) port)
354           (output-stencil port (cadr expr) offset))
355          ((eq? head 'no-origin)
356           (display (expression->string head) port)
357           (output-stencil port (cadr expr) offset))
358          ((eq? head 'translate-stencil)
359           (output-stencil port (caddr expr) (offset-add offset (cadr expr))))
360          ((eq? head 'combine-stencil)
361           (output-stencil port (cadr expr) offset)
362           (output-stencil port (caddr expr) offset))
363          (else
364           (display (placebox (car offset) (cdr offset)
365                              (expression->string expr)) port))))))
366
367 (define (placebox x y s) 
368   (string-append 
369    (ly:number->string x) " " (ly:number->string y) " {" s "} place-box\n"))
370
371 (define (polygon points blotdiameter)
372   (string-append
373    " "
374    (numbers->string points)
375    (ly:number->string (/ (length points) 2))
376    (ly:number->string blotdiameter)
377    " draw_polygon"))
378
379 (define (repeat-slash wid slope thick)
380   (string-append
381    (numbers->string (list wid slope thick))
382    " draw_repeat_slash"))
383
384 (define (round-filled-box x y width height blotdiam)
385    (string-append
386     " "
387     (numbers->string
388      (list x y width height blotdiam)) " draw_round_box"))
389
390 (define (new-start-system origin dim)
391   (string-append
392    "\n" (number-pair->string origin) " start-system\n"
393    "{\n"
394    "set-ps-scale-to-lily-scale\n"))
395
396 (define (stem breapth width depth height) 
397   (string-append
398    (numbers->string (list breapth width depth height))
399    " draw_box" ))
400
401 (define (stop-system)
402   "}\nstop-system\n")
403
404 (define stop-last-system stop-system)
405
406 (define (symmetric-x-triangle thick w h)
407   (string-append
408    (numbers->string (list h w thick))
409    " draw_symmetric_x_triangle"))
410
411 (define (text s)
412 ;;  (string-append "(" (escape-parentheses s) ") show "))
413   (string-append "(" (ps-encoding s) ") show "))
414
415 (define (unknown) 
416   "\n unknown\n")
417
418 (define (zigzag-line centre? zzw zzh thick dx dy)
419   (string-append
420     (if centre? "true" "false")
421     " "
422     (ly:number->string zzw)
423     " "
424     (ly:number->string zzh)
425     " "
426     (ly:number->string thick)
427     " 0 0 "
428     (ly:number->string dx)
429     " "
430     (ly:number->string dy)
431     " draw_zigzag_line "))
432
433 (define (start-page)
434   (set! page-number (+ page-number 1))
435   (string-append
436    "%%Page: " (number->string page-number) " " (number->string page-count) "\n"
437   "start-page\n"))
438
439 (define (stop-page last?)
440   (if last?
441       "\nstop-last-page\n"
442       "\nstop-page\n"))