]> git.donarmstrong.com Git - lilypond.git/blob - scm/output-ps.scm
* scm/output-ps.scm (output-scopes): use ly:paper-lookup for font
[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 ;;;;   * text setting, kerning.
14 ;;;;   * font properties
15 ;;;;   * customisation of title markup, piece title markup?
16 ;;;;   * page layout
17 ;;;;   * document output-interface
18
19 (debug-enable 'backtrace)
20
21 (define-module (scm output-ps))
22 (define this-module (current-module))
23
24 (use-modules
25  (guile)
26  (ice-9 regex)
27  (srfi srfi-13)
28  (lily))
29
30
31
32
33 ;;; Lily output interface, PostScript implementation --- cleanup and docme
34
35 ;;; Module entry
36 (define-public (ps-output-expression expr port)
37   (display (expression->string expr) port))
38
39 ;;; Global vars
40 ;; alist containing fontname -> fontcommand assoc (both strings)
41 (define font-name-alist '())
42
43 ;; WIP -- stencils from markup? values of output-scopes
44 (define header-stencil #f)
45
46 (define BASELINE-SKIP 3)
47
48 ;; /lilypondpaperoutputscale 1.75729901757299 def
49 ;;/lily-output-units 2.83464  def  %% milimeter
50 ;;/output-scale lilypondpaperoutputscale lily-output-units mul def
51 ;;
52 ;; output-scale = 1.75729901757299 * 2.83464 = 4.9813100871731003736
53
54 (define OUTPUT-SCALE 4.98)
55 (define TOP-MARGIN 0)
56
57 ;;; helper functions, not part of output interface
58 (define (escape-parentheses s)
59   (regexp-substitute/global #f "(^|[^\\])([\\(\\)])" s 'pre 1 "\\" 2 'post))
60
61 (define (offset-add a b)
62   (cons (+ (car a) (car b))
63         (+ (cdr a) (cdr b))))
64
65 (define LATIN1-ENCODING-ALIST
66   '(("ö" . "oumlaut")
67     ("ò" . "ograve")
68     ("ó" . "oacute")
69     ("ô" . "ocircumflex")
70     ("õ" . "otilde")
71     ("ø" . "oslash")))
72
73 (define LATIN1-ENCODING-COMMANDS
74   "/oumlaut { (o) show gsave -1 0 rmoveto (\\177) show grestore } bind def
75 /ograve { (o) show gsave -1 0 rmoveto (\\022) show grestore } def
76 /oacute { (o) show gsave -1 0 rmoveto (\\023) show grestore } def
77 /ocircumflex { (o) show gsave -1 0 rmoveto (^) show grestore } def
78 /otilde { (o) show gsave -1 0 rmoveto (~) show grestore } def
79 /oslash { (o) show gsave -1 0 rmoveto (\\034) show grestore } def
80 ")
81
82 (define (ps-encoding text)
83   (let ((s (escape-parentheses text)))
84     (define (helper alist-list s)
85       (if (not (pair? alist-list))
86           s
87           (helper (cdr alist-list)
88                   (regexp-substitute/global
89                    #f (caar alist-list) s
90                    'pre (string-append ") show " (cdar alist-list) " (")
91                    'post))))
92     (helper LATIN1-ENCODING-ALIST s)))
93
94 ;; FIXME: lily-def
95 (define (ps-string-def prefix key val)
96   (string-append "/" prefix (symbol->string key) " ("
97                  (escape-parentheses val)
98                  ") def\n"))
99
100 (define (ps-number-def prefix key val)
101   (let ((s (if (integer? val)
102                (number->string val)
103                (number->string (exact->inexact val)))))
104     (string-append "/" prefix (symbol->string key) " " s " def\n")))
105
106 (define (tex-font? fontname)
107   (equal? (substring fontname 0 2) "cm"))
108
109
110
111 ;;; Output-interface functions
112 (define (beam width slope thick blot)
113   (string-append
114    (numbers->string (list slope width thick blot)) " draw_beam" ))
115
116 ;; two beziers
117 (define (bezier-sandwich l thick)
118   (string-append 
119    (apply string-append (map number-pair->string l))
120    (ly:number->string thick)
121    " draw_bezier_sandwich "))
122
123 (define (bracket arch_angle arch_width arch_height  height arch_thick thick)
124   (string-append
125    (numbers->string
126     (list arch_angle arch_width arch_height height arch_thick thick))
127    " draw_bracket"))
128
129 (define (char i)
130   (string-append 
131    "(\\" (inexact->string i 8) ") show " ))
132
133 (define (comment s)
134   (string-append "% " s "\n"))
135
136 (define (dashed-line thick on off dx dy)
137   (string-append 
138    (ly:number->string dx)
139    " "
140    (ly:number->string dy)
141    " "
142    (ly:number->string thick)
143    " [ "
144    (ly:number->string on)
145    " "
146    (ly:number->string off)
147    " ] 0 draw_dashed_line"))
148
149 ;; what the heck is this interface ?
150 (define (dashed-slur thick dash l)
151   (string-append 
152    (apply string-append (map number-pair->string l)) 
153    (ly:number->string thick) 
154    " [ "
155    (ly:number->string dash)
156    " "
157    ;;UGH.  10 ?
158    (ly:number->string (* 10 thick))
159    " ] 0 draw_dashed_slur"))
160
161 (define (define-fonts internal-external-name-mag-pairs)
162
163   (define (fontname->designsize fontname)
164     (let ((i (string-index fontname char-numeric?)))
165       (string->number (substring fontname i))))
166                          
167   ;;  (define (font-load-command name-mag command)
168   (define (font-load-command lst)
169     (let* ((key-name-size (car lst))
170            (value (cdr lst))
171            (value-name-size (car value))
172            (command (cdr value))
173            (fontname (car value-name-size))
174            (designsize (if (tex-font? fontname)
175                            (/ 12 (fontname->designsize fontname))
176                            ;; This is about 12/20 :-)
177                            (cdr key-name-size)))
178            (fontsize (cdr value-name-size))
179            (scaling (* 12 (/ fontsize designsize)))
180            (scaling (/ fontsize (/ designsize 12))))
181
182       ;; frobnicate NAME to jibe with external definitions.
183       (define (possibly-mangle-fontname fontname)
184         (cond
185          ((tex-font? fontname)
186           ;; FIXME: we need proper Fontmap for CM fonts, like so:
187           ;; /CMR10 (cmr10.pfb); 
188           ;; (string-upcase fontname)
189           (string-append fontname ".pfb"))
190          ((or (equal? (substring fontname 0 4) "feta")
191               (equal? (substring fontname 0 8) "parmesan"))
192           (regexp-substitute/global
193            #f "(feta|parmesan)([a-z-]*)([0-9]+)"
194            fontname 'pre "GNU-LilyPond-" 1 2 "-" 3 'post))
195          (else fontname)))
196       (if
197        #f
198        (begin
199          (newline)
200          (format (current-error-port) "key-name-size ~S\n" key-name-size)
201          (format (current-error-port) "value ~S\n" value)
202          (format (current-error-port) "value-name-size ~S\n" value-name-size)
203          (format (current-error-port) "command ~S\n" command)
204          (format (current-error-port) "designsize ~S\n" designsize)
205          (format (current-error-port) "fontname ~S\n" fontname)
206          (format (current-error-port) "fontsize ~S\n" fontsize)
207          (format (current-error-port) "scaling ~S\n" scaling)))
208          
209       (string-append
210        "/" command
211        " { /" (possibly-mangle-fontname fontname) " findfont "
212        (ly:number->string scaling)
213        "output-scale div scalefont setfont } bind def \n")))
214     
215   (define (ps-encoded-fontswitch name-mag-pair)
216     (let* ((key (car name-mag-pair))
217            (value (cdr name-mag-pair))
218            (fontname (car value))
219            (scaling (cdr value)))
220       (cons key (cons value
221                       (string-append
222                        "lilyfont" fontname "-" (number->string scaling))))))
223
224   (set! font-name-alist
225         (map ps-encoded-fontswitch internal-external-name-mag-pairs))
226   (apply string-append (map font-load-command font-name-alist)))
227
228 (define (define-origin file line col) "")
229
230 (define (dot x y radius)
231   (string-append
232    " "
233    (numbers->string
234     (list x y radius)) " draw_dot"))
235
236 (define (draw-line thick x1 y1 x2 y2)
237   (string-append 
238   "     1 setlinecap
239         1 setlinejoin "
240   (ly:number->string thick)
241         " setlinewidth "
242    (ly:number->string x1)
243    " "
244    (ly:number->string y1)
245    " moveto "
246    (ly:number->string x2)
247    " "
248    (ly:number->string y2)
249    " lineto stroke"))
250
251 (define (end-output)
252   "\nend-lilypond-output\n")
253
254 (define (expression->string expr)
255   (eval expr this-module))
256
257 (define (ez-ball ch letter-col ball-col)
258   (string-append
259    " (" ch ") "
260    (numbers->string (list letter-col ball-col))
261    " /Helvetica-Bold " ;; ugh
262    " draw_ez_ball"))
263
264 (define (filledbox breapth width depth height) 
265   (string-append (numbers->string (list breapth width depth height))
266                  " draw_box"))
267
268 (define (fontify name-mag-pair exp)
269
270   (define (select-font name-mag-pair)
271     (let ((c (assoc name-mag-pair font-name-alist)))
272       
273       (if c
274           (string-append " " (cddr c) " ")
275           (begin
276             (ly:warn
277              (format "Programming error: No such font: ~S" name-mag-pair))
278             
279             (display "FAILED\n" (current-error-port))
280             (if #f ;(pair? name-mag-pair))
281                 (display (object-type (car name-mag-pair)) (current-error-port))
282                 (write name-mag-pair (current-error-port)))
283             (if #f ;  (pair? font-name-alist)
284                 (display
285                  (object-type (caaar font-name-alist)) (current-error-port))
286                 (write font-name-alist (current-error-port)))
287
288             ;; (format #f "\n%FAILED: (select-font ~S)\n" name-mag-pair))
289             ""))))
290   
291   (string-append (select-font name-mag-pair) exp))
292
293 (define (header creator generate) 
294   (string-append
295    "%!PS-Adobe-3.0\n"
296    "%%Creator: " creator generate "\n"))
297
298 (define (header-end)
299   (string-append
300    ;; URG: now we can't use scm output without Lily
301    (ly:gulp-file "lilyponddefs.ps")
302    "{exch pop //systemdict /run get exec}\n\n"
303    (ly:gulp-file "music-drawing-routines.ps")
304    "{ exch pop //systemdict /run get exec }\n\n"
305    ;; ps-testing wreaks havoc when used with lilypond-book.
306    ;;  -- is this still true with new modules system?
307 ;;   (if (defined? 'ps-testing) "\n /testing true def" "")
308   ;   "\n /testing true def"
309    ))
310
311 (define (horizontal-line x1 x2 th)
312   (draw-line th x1  0 x2 0))
313
314 (define (lily-def key val)
315   (let ((prefix "lilypondpaper"))
316     (if (string=?
317          (substring key 0 (min (string-length prefix) (string-length key)))
318          prefix)
319         (string-append "/" key " {" val "} bind def\n")
320         (string-append "/" key " (" val ") def\n"))))
321
322 (define (make-title port)
323   (if header-stencil
324       (let ((x-ext (ly:stencil-get-extent header-stencil Y))
325             (y-ext (ly:stencil-get-extent header-stencil X)))
326         (display (start-system
327                   (/ (interval-length x-ext) OUTPUT-SCALE)
328                   (/ (interval-length y-ext) OUTPUT-SCALE))
329                  port)
330         (output-stencil port (ly:stencil-get-expr header-stencil) '(0 . 0))
331         (display (stop-system) port)))
332   "")
333
334 (define (no-origin) "")
335
336 ;; FIXME: duplictates output-scopes, duplicated in other backends
337 ;; FIXME: silly interface name
338 (define (output-paper-def pd)
339   (let ((prefix "lilypondpaper"))
340     
341     (define (scope-entry->string key var)
342       (let ((val (variable-ref var)))
343         (cond
344          ((string? val) (ps-string-def prefix key val))
345          ((number? val) (ps-number-def prefix key val))
346          (else ""))))
347       
348     (apply
349      string-append
350      (module-map scope-entry->string (ly:output-def-scope pd)))))
351
352 ;; FIXME: duplicated in other output backends
353 ;; FIXME: silly interface name
354 (define (output-scopes paper scopes fields basename)
355
356   ;; FIXME: customise/generate these
357   (let*
358       ((props
359         (list
360          (append
361           `((linewidth . ,(ly:paper-get-number paper 'linewidth))
362             (font-family . roman))
363           (ly:paper-lookup paper 'font-defaults))))
364        (prefix "lilypond")
365        (stencils '()))
366
367     (define (scope-entry->string key var)
368       (let ((val (variable-ref var)))
369         
370         (if (memq key fields)
371             (header-to-file basename key val))
372         
373         (cond
374          ((eq? key 'font)
375           BARF
376           (format (current-error-port) "PROPS:~S\n" val)
377           (set! props (cons val props))
378           "")
379          
380          ;; define strings, for /make-lilypond-title to pick up
381          ((string? val) (ps-string-def prefix key val))
382          
383          ;; generate stencil from markup
384          ((markup? val) (set! stencils
385                               (append
386                                stencils
387                                (list
388                                 (interpret-markup paper props val))))
389           "")
390          ((number? val) (ps-number-def prefix key val))
391          (else ""))))
392     
393     (define (output-scope scope)
394       (apply string-append (module-map scope-entry->string scope)))
395
396     (let ((s (string-append (apply string-append (map output-scope scopes)))))
397       (set! header-stencil (stack-lines DOWN 0 BASELINE-SKIP stencils))
398
399       ;; match systems, which are also aligned to center
400       (ly:stencil-align-to! header-stencil Y CENTER)
401       
402       ;; trigger font load
403       (ly:stencil-get-expr header-stencil)
404       s)))
405
406 ;; hmm, looks like recursing call is always last statement, does guile
407 ;; think so too?
408 (define (output-stencil port expr offset)
409   (if (pair? expr)
410       (let ((head (car expr)))
411         (cond
412          ((ly:input-location? head)
413           (display (apply define-origin (ly:input-location head)) port)
414           (output-stencil port (cadr expr) offset))
415          ((eq? head 'no-origin)
416           (display (expression->string head) port)
417           (output-stencil port (cadr expr) offset))
418          ((eq? head 'translate-stencil)
419           (output-stencil port (caddr expr) (offset-add offset (cadr expr))))
420          ((eq? head 'combine-stencil)
421           (output-stencil port (cadr expr) offset)
422           (output-stencil port (caddr expr) offset))
423          (else
424           (display (placebox (car offset) (cdr offset)
425                              (expression->string expr)) port))))))
426
427 (define (placebox x y s) 
428   (string-append 
429    (ly:number->string x) " " (ly:number->string y) " {" s "} place-box\n"))
430
431 (define (polygon points blotdiameter)
432   (string-append
433    " "
434    (numbers->string points)
435    (ly:number->string (/ (length points) 2))
436    (ly:number->string blotdiameter)
437    " draw_polygon"))
438
439 (define (repeat-slash wid slope thick)
440   (string-append
441    (numbers->string (list wid slope thick))
442    " draw_repeat_slash"))
443
444 (define (round-filled-box x y width height blotdiam)
445    (string-append
446     " "
447     (numbers->string
448      (list x y width height blotdiam)) " draw_round_box"))
449
450 (define (start-system width height)
451   (string-append
452    "\n" (ly:number->string height)
453    " start-system\n"
454    "{\n"
455    "set-ps-scale-to-lily-scale\n"))
456
457 (define (stem breapth width depth height) 
458   (string-append
459    (numbers->string (list breapth width depth height))
460    " draw_box" ))
461
462 (define (stop-last-system)
463   (stop-system))
464
465 (define (stop-system)
466   "}\nstop-system\n")
467
468 (define (symmetric-x-triangle thick w h)
469   (string-append
470    (numbers->string (list h w thick))
471    " draw_symmetric_x_triangle"))
472
473 (define (text s)
474 ;;  (string-append "(" (escape-parentheses s) ") show "))
475   (string-append "(" (ps-encoding s) ") show "))
476
477 ;; top-of-file, wtf?
478 (define (top-of-file)
479   (string-append
480    (header (string-append "GNU LilyPond (" (lilypond-version) "), ")
481            (strftime "%c" (localtime (current-time))))
482    LATIN1-ENCODING-COMMANDS
483   ;;; ugh
484    (ps-string-def
485     "lilypond" 'tagline
486     (string-append "Engraved by LilyPond (" (lilypond-version) ")"))))
487
488 (define (unknown) 
489   "\n unknown\n")
490
491 (define (zigzag-line centre? zzw zzh thick dx dy)
492   (string-append
493     (if centre? "true" "false")
494     " "
495     (ly:number->string zzw)
496     " "
497     (ly:number->string zzh)
498     " "
499     (ly:number->string thick)
500     " 0 0 "
501     (ly:number->string dx)
502     " "
503     (ly:number->string dy)
504     " draw_zigzag_line "))