]> 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 interface 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 ;;;;   * %% Papersize in (header ...)
13 ;;;;   * text setting, kerning.
14 ;;;;   * document output-interface
15
16 (debug-enable 'backtrace)
17
18 (define-module (scm output-ps)
19   #:re-export (quote)
20   #:export (unknown
21              blank
22              dot
23              white-dot
24              beam
25              bracket
26              dashed-slur
27              char
28              dashed-line
29              zigzag-line
30              symmetric-x-triangle
31              ez-ball
32              comment
33              repeat-slash
34              placebox
35              bezier-sandwich
36              horizontal-line
37              filledbox
38              round-filled-box
39              text
40              tuplet
41              polygon
42              draw-line
43              define-origin
44              no-origin
45              ))
46
47
48 (use-modules (guile)
49              (ice-9 regex)
50              (srfi srfi-1)
51              (srfi srfi-13)
52              (scm framework-ps)
53              (lily))
54
55
56 ;;(map export
57 ;;   (append (ly:all-stencil-expressions) (ly:all-output-backend-commands)))
58
59 ;; huh?
60 ;;(write (ly:all-output-backend-commands))
61 ;;(write (ly:all-stencil-expressions))
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 (ps-encoding text)
73   (escape-parentheses text))
74
75 ;; FIXME: lily-def
76 (define-public (ps-string-def prefix key val)
77   (string-append "/" prefix (symbol->string key) " ("
78                  (escape-parentheses val)
79                  ") def\n"))
80
81 (define (ps-number-def prefix key val)
82   (let ((s (if (integer? val)
83                (ly:number->string val)
84                (ly:number->string (exact->inexact val)))))
85     (string-append "/" prefix (symbol->string key) " " s " def\n")))
86
87
88 ;;;
89 ;;; Lily output interface, PostScript implementation --- cleanup and docme
90 ;;;
91
92 ;;; Output-interface functions
93 (define (beam width slope thick blot)
94   (string-append
95    (ly:numbers->string (list slope width thick blot)) " draw_beam" ))
96
97 ;; two beziers
98 (define (bezier-sandwich l thick)
99   (string-append 
100    (string-join (map ly:number-pair->string l) " ")
101    " "
102    (ly:number->string thick)
103    " draw_bezier_sandwich"))
104
105 (define (bracket arch_angle arch_width arch_height  height arch_thick thick)
106   (string-append
107    (ly:numbers->string
108     (list arch_angle arch_width arch_height height arch_thick thick))
109    " draw_bracket"))
110
111 (define (char font i)
112   (string-append 
113     (ps-font-command font) " setfont " 
114    "(\\" (ly:inexact->string i 8) ") show" ))
115
116 (define (dashed-line thick on off dx dy)
117   (string-append 
118    (ly:number->string dx) " "
119    (ly:number->string dy) " "
120    (ly:number->string thick)
121    " [ "
122    (ly:number->string on) " "
123    (ly:number->string off)
124    " ] 0 draw_dashed_line"))
125
126 ;; what the heck is this interface ?
127 (define (dashed-slur thick dash l)
128   (string-append 
129    (string-join (map ly:number-pair->string l) " ")
130    " "
131    (ly:number->string thick) 
132    " [ "
133    (ly:number->string dash)
134    " "
135    ;;UGH.  10 ?
136    (ly:number->string (* 10 thick))
137    " ] 0 draw_dashed_slur"))
138
139 ; todo: merge with tex-font-command?
140
141
142
143 (define (define-origin file line col) "")
144
145 (define (dot x y radius)
146   (string-append
147    " "
148    (ly:numbers->string
149     (list x y radius)) " draw_dot"))
150
151 (define (white-dot x y radius)
152   (string-append
153    " "
154    (ly:numbers->string
155     (list x y radius)) " draw_white_dot"))
156
157 (define (white-dot x y radius)
158   (string-append
159    " "
160    (ly:numbers->string
161     (list x y radius)) " draw_white_dot"))
162
163 (define (draw-line thick x1 y1 x2 y2)
164   (string-append 
165    "1 setlinecap 1 setlinejoin "
166    (ly:number->string thick) " setlinewidth "
167    (ly:number->string x1) " "
168    (ly:number->string y1) " moveto "
169    (ly:number->string x2) " "
170    (ly:number->string y2) " lineto stroke"))
171
172 (define (ez-ball ch letter-col ball-col)
173   (string-append
174    " (" ch ") "
175    (ly:numbers->string (list letter-col ball-col))
176    " /Helvetica-Bold " ;; ugh
177    " draw_ez_ball"))
178
179 (define (filledbox breapth width depth height) ; FIXME : use draw_round_box
180   (string-append (ly:numbers->string (list breapth width depth height))
181                  " draw_box"))
182
183 ;; WTF is this in every backend?
184 (define (horizontal-line x1 x2 th)
185   (draw-line th x1 0 x2 0))
186
187 (define (lily-def key val)
188   (let ((prefix "lilypondpaper"))
189     (if (string=?
190          (substring key 0 (min (string-length prefix) (string-length key)))
191          prefix)
192         (string-append "/" key " {" val "} bind def\n")
193         (string-append "/" key " (" val ") def\n"))))
194
195 (define (no-origin) "")
196
197
198
199 (define (placebox x y s) 
200   (string-append 
201    (ly:number->string x) " " (ly:number->string y) " { " s " } place-box\n"))
202
203 (define (polygon points blotdiameter)
204   (string-append
205    (ly:numbers->string points) " "
206    (ly:number->string (/ (length points) 2)) " "
207    (ly:number->string blotdiameter)
208    " draw_polygon"))
209
210 (define (repeat-slash wid slope thick)
211   (string-append
212    (ly:numbers->string (list wid slope thick))
213    " draw_repeat_slash"))
214
215 (define (round-filled-box x y width height blotdiam)
216    (string-append
217     (ly:numbers->string
218      (list x y width height blotdiam)) " draw_round_box"))
219
220
221 (define (stem breapth width depth height) ; FIXME: use draw_round_box.
222   (string-append
223    (ly:numbers->string (list breapth width depth height))
224    " draw_box" ))
225
226 (define (symmetric-x-triangle thick w h)
227   (string-append
228    (ly:numbers->string (list h w thick))
229    " draw_symmetric_x_triangle"))
230
231 (define (text font s)
232   (let*
233       
234       (
235        ;; ugh, we should find a better way to
236        ;; extract the hsbw for /space from the font.
237        
238        (space-length (cdar (ly:text-dimension font "t"))) 
239        (commands '())
240        (add-command (lambda (x) (set! commands (cons x commands)))) )
241
242     (string-fold
243      (lambda (chr word)
244        "Translate space as into moveto, group the rest in words."
245        (if (and (< 0 (string-length word))
246                 (equal? #\space  chr))
247            (add-command 
248             (string-append "(" (ps-encoding word) ") show\n")))
249
250        (if (equal? #\space  chr)
251            (add-command  (string-append (number->string space-length) " 0.0 rmoveto ")) )
252        
253        (if (equal? #\space  chr)
254            ""
255            (string-append word (make-string 1 chr))))
256      ""
257      (string-append  s " "))
258
259     (string-append
260      (ps-font-command font) " setfont "
261      (string-join (reverse commands)))
262     ))
263   
264 (define (unknown) 
265   "\n unknown\n")
266
267 (define (zigzag-line centre? zzw zzh thick dx dy)
268   (string-append
269     (if centre? "true" "false") " "
270     (ly:number->string zzw) " "
271     (ly:number->string zzh) " "
272     (ly:number->string thick) " "
273     "0 0 "
274     (ly:number->string dx) " "
275     (ly:number->string dy)
276     " draw_zigzag_line"))