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