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