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