]> git.donarmstrong.com Git - lilypond.git/blob - scm/output-ps.scm
* flower/file-path.cc (find): try to open directly as well, so we
[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              named-glyph
31              dashed-line
32              zigzag-line
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              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 (named-glyph font glyph)
120   (string-append 
121     (ps-font-command font) " setfont " 
122    "/" glyph " glyphshow "))
123
124 (define (dashed-line thick on off dx dy)
125   (string-append 
126    (ly:number->string dx) " "
127    (ly:number->string dy) " "
128    (ly:number->string thick)
129    " [ "
130    (ly:number->string on) " "
131    (ly:number->string off)
132    " ] 0 draw_dashed_line"))
133
134 ;; what the heck is this interface ?
135 (define (dashed-slur thick dash l)
136   (string-append 
137    (string-join (map ly:number-pair->string l) " ")
138    " "
139    (ly:number->string thick) 
140    " [ "
141    (ly:number->string dash)
142    " "
143    ;;UGH.  10 ?
144    (ly:number->string (* 10 thick))
145    " ] 0 draw_dashed_slur"))
146
147 ; todo: merge with tex-font-command?
148
149 (define (embedded-ps string)
150   string)
151
152 (define (dot x y radius)
153   (string-append
154    " "
155    (ly:numbers->string
156     (list x y radius)) " draw_dot"))
157
158 (define (white-dot x y radius)
159   (string-append
160    " "
161    (ly:numbers->string
162     (list x y radius)) " draw_white_dot"))
163
164 (define (draw-line thick x1 y1 x2 y2)
165   (string-append 
166    "1 setlinecap 1 setlinejoin "
167    (ly:number->string thick) " setlinewidth "
168    (ly:number->string x1) " "
169    (ly:number->string y1) " moveto "
170    (ly:number->string x2) " "
171    (ly:number->string y2) " lineto stroke"))
172
173 (define (ez-ball ch letter-col ball-col)
174   (string-append
175    " (" ch ") "
176    (ly:numbers->string (list letter-col ball-col))
177    " /Helvetica-Bold " ;; ugh
178    " draw_ez_ball"))
179
180 (define (filledbox breapth width depth height) ; FIXME : use draw_round_box
181   (string-append (ly:numbers->string (list breapth width depth height))
182                  " draw_box"))
183
184 ;; WTF is this in every backend?
185 (define (horizontal-line x1 x2 th)
186   (draw-line th x1 0 x2 0))
187
188 (define (lily-def key val)
189   (let ((prefix "lilypondlayout"))
190     (if (string=?
191          (substring key 0 (min (string-length prefix) (string-length key)))
192          prefix)
193         (string-append "/" key " {" val "} bind def\n")
194         (string-append "/" key " (" val ") def\n"))))
195
196
197 (define (placebox x y s) 
198   (string-append 
199    (ly:number->string x) " " (ly:number->string y) " { " s " } place-box\n"))
200
201 (define (polygon points blotdiameter)
202   (string-append
203    (ly:numbers->string points) " "
204    (ly:number->string (/ (length points) 2)) " "
205    (ly:number->string blotdiameter)
206    " draw_polygon"))
207
208 (define (repeat-slash wid slope thick)
209   (string-append
210    (ly:numbers->string (list wid slope thick))
211    " draw_repeat_slash"))
212
213 (define (round-filled-box x y width height blotdiam)
214    (string-append
215     (ly:numbers->string
216      (list x y width height blotdiam)) " draw_round_box"))
217
218 (define (old-text font s)
219   (let*
220       
221       ;; ugh, we should find a better way to
222       ;; extract the hsbw for /space from the font.
223       
224       ((space-length (cdar (ly:text-dimension font " "))) 
225        (commands '())
226        (add-command (lambda (x) (set! commands (cons x commands)))) )
227
228     (string-fold
229      (lambda (chr word)
230        "Translate space as into moveto, group the rest in words."
231        (if (and (< 0 (string-length word))
232                 (equal? #\space  chr))
233            (add-command 
234             (string-append "(" (ps-encoding word) ") show\n")))
235
236        (if (equal? #\space chr)
237            (add-command  (string-append (number->string space-length) " 0.0 rmoveto ")) )
238        
239        (if (equal? #\space chr)
240            ""
241            (string-append word (make-string 1 chr))))
242      ""
243      (string-append s " "))
244
245     (string-append
246      (ps-font-command font) " setfont "
247      (string-join (reverse commands)))
248     ))
249
250 (define (new-text font s)
251   (let*
252       ((space-length (cdar (ly:text-dimension font " ")))
253        (space-move (string-append (number->string space-length) " 0.0 rmoveto "))
254        
255        (input-enc (assoc-get 'input-name
256                              (ly:font-encoding-alist font)
257                              'latin1))
258        (out-vec (decode-byte-string input-enc s)))
259
260
261     (string-append
262      (ps-font-command font) " setfont "
263      (string-join
264       (vector->list
265        (vector-for-each
266         
267         (lambda (sym)
268           (if (eq? sym 'space)
269               space-move
270               (string-append "/" (symbol->string sym) " glyphshow")))
271         out-vec)))
272      )))
273
274 ;(define text old-text)
275 (define text new-text)
276
277 (define (white-text scale s)
278    (let ((mystring (string-append "(" s  ") " (number->string scale)   " /Helvetica-Bold "
279           " draw_white_text")))
280   mystring))
281
282 (define (unknown) 
283   "\n unknown\n")
284
285 (define (zigzag-line centre? zzw zzh thick dx dy)
286   (string-append
287     (if centre? "true" "false") " "
288     (ly:number->string zzw) " "
289     (ly:number->string zzh) " "
290     (ly:number->string thick) " "
291     "0 0 "
292     (ly:number->string dx) " "
293     (ly:number->string dy)
294     " draw_zigzag_line"))
295
296
297 (define (grob-cause grob)
298   "")
299
300 (define (no-origin)
301   "")
302
303 (define-public (glyph-string psname items)
304   (apply
305    string-append
306    (cons
307     (format " /~a findfont setfont " psname)
308     (map (lambda  (item)
309            (format " ~a ~a rmoveto /~a glyphshow "
310                    (car item)
311                    (cadr item)
312                    (caddr item)))
313          items))))