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