]> git.donarmstrong.com Git - lilypond.git/blob - scm/output-ps.scm
* scm/fret-diagrams.scm (draw-dots): default values for dot size
[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              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 (define (ps-number-def prefix key val)
84   (let ((s (if (integer? val)
85                (ly:number->string val)
86                (ly:number->string (exact->inexact val)))))
87     (string-append "/" prefix (symbol->string key) " " s " def\n")))
88
89
90 ;;;
91 ;;; Lily output interface, PostScript implementation --- cleanup and docme
92 ;;;
93
94 ;;; Output-interface functions
95 (define (beam width slope thick blot)
96   (string-append
97    (ly:numbers->string (list slope width thick blot)) " draw_beam" ))
98
99 ;; two beziers
100 (define (bezier-sandwich l thick)
101   (string-append 
102    (string-join (map ly:number-pair->string l) " ")
103    " "
104    (ly:number->string thick)
105    " draw_bezier_sandwich"))
106
107 (define (bracket arch_angle arch_width arch_height  height arch_thick thick)
108   (string-append
109    (ly:numbers->string
110     (list arch_angle arch_width arch_height height arch_thick thick))
111    " draw_bracket"))
112
113 (define (char font i)
114   (string-append 
115     (ps-font-command font) " setfont " 
116    "(\\" (ly:inexact->string i 8) ") show" ))
117
118 (define (dashed-line thick on off dx dy)
119   (string-append 
120    (ly:number->string dx) " "
121    (ly:number->string dy) " "
122    (ly:number->string thick)
123    " [ "
124    (ly:number->string on) " "
125    (ly:number->string off)
126    " ] 0 draw_dashed_line"))
127
128 ;; what the heck is this interface ?
129 (define (dashed-slur thick dash l)
130   (string-append 
131    (string-join (map ly:number-pair->string l) " ")
132    " "
133    (ly:number->string thick) 
134    " [ "
135    (ly:number->string dash)
136    " "
137    ;;UGH.  10 ?
138    (ly:number->string (* 10 thick))
139    " ] 0 draw_dashed_slur"))
140
141 ; todo: merge with tex-font-command?
142
143
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 (draw-line thick x1 y1 x2 y2)
158   (string-append 
159    "1 setlinecap 1 setlinejoin "
160    (ly:number->string thick) " setlinewidth "
161    (ly:number->string x1) " "
162    (ly:number->string y1) " moveto "
163    (ly:number->string x2) " "
164    (ly:number->string y2) " lineto stroke"))
165
166 (define (ez-ball ch letter-col ball-col)
167   (string-append
168    " (" ch ") "
169    (ly:numbers->string (list letter-col ball-col))
170    " /Helvetica-Bold " ;; ugh
171    " draw_ez_ball"))
172
173 (define (filledbox breapth width depth height) ; FIXME : use draw_round_box
174   (string-append (ly:numbers->string (list breapth width depth height))
175                  " draw_box"))
176
177 ;; WTF is this in every backend?
178 (define (horizontal-line x1 x2 th)
179   (draw-line th x1 0 x2 0))
180
181 (define (lily-def key val)
182   (let ((prefix "lilypondpaper"))
183     (if (string=?
184          (substring key 0 (min (string-length prefix) (string-length key)))
185          prefix)
186         (string-append "/" key " {" val "} bind def\n")
187         (string-append "/" key " (" val ") def\n"))))
188
189
190 (define (placebox x y s) 
191   (string-append 
192    (ly:number->string x) " " (ly:number->string y) " { " s " } place-box\n"))
193
194 (define (polygon points blotdiameter)
195   (string-append
196    (ly:numbers->string points) " "
197    (ly:number->string (/ (length points) 2)) " "
198    (ly:number->string blotdiameter)
199    " draw_polygon"))
200
201 (define (repeat-slash wid slope thick)
202   (string-append
203    (ly:numbers->string (list wid slope thick))
204    " draw_repeat_slash"))
205
206 (define (round-filled-box x y width height blotdiam)
207    (string-append
208     (ly:numbers->string
209      (list x y width height blotdiam)) " draw_round_box"))
210
211
212 (define (stem breapth width depth height) ; FIXME: use draw_round_box.
213   (string-append
214    (ly:numbers->string (list breapth width depth height))
215    " draw_box" ))
216
217 (define (symmetric-x-triangle thick w h)
218   (string-append
219    (ly:numbers->string (list h w thick))
220    " draw_symmetric_x_triangle"))
221
222 (define (text font s)
223   (let*
224       
225       (
226        ;; ugh, we should find a better way to
227        ;; extract the hsbw for /space from the font.
228        
229        (space-length (cdar (ly:text-dimension font "t"))) 
230        (commands '())
231        (add-command (lambda (x) (set! commands (cons x commands)))) )
232
233     (string-fold
234      (lambda (chr word)
235        "Translate space as into moveto, group the rest in words."
236        (if (and (< 0 (string-length word))
237                 (equal? #\space  chr))
238            (add-command 
239             (string-append "(" (ps-encoding word) ") show\n")))
240
241        (if (equal? #\space  chr)
242            (add-command  (string-append (number->string space-length) " 0.0 rmoveto ")) )
243        
244        (if (equal? #\space  chr)
245            ""
246            (string-append word (make-string 1 chr))))
247      ""
248      (string-append  s " "))
249
250     (string-append
251      (ps-font-command font) " setfont "
252      (string-join (reverse commands)))
253     ))
254   
255
256 (define (white-text s)
257    (let ((mystring (string-append "(" s ")" " /Helvetica-bold" " draw_white_text")))
258   mystring))
259
260 (define (unknown) 
261   "\n unknown\n")
262
263 (define (zigzag-line centre? zzw zzh thick dx dy)
264   (string-append
265     (if centre? "true" "false") " "
266     (ly:number->string zzw) " "
267     (ly:number->string zzh) " "
268     (ly:number->string thick) " "
269     "0 0 "
270     (ly:number->string dx) " "
271     (ly:number->string dy)
272     " draw_zigzag_line"))
273
274
275 (define (grob-cause grob)
276   "")
277
278 (define (no-origin)
279   "")