]> 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
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 (use-modules (guile)
50              (ice-9 regex)
51              (srfi srfi-1)
52              (srfi srfi-13)
53              (scm framework-ps)
54              (lily))
55
56
57 ;;(map export
58 ;;   (append (ly:all-stencil-expressions) (ly:all-output-backend-commands)))
59
60 ;; huh?
61 ;;(write (ly:all-output-backend-commands))
62 ;;(write (ly:all-stencil-expressions))
63
64
65 ;;; helper functions, not part of output interface
66 (define (escape-parentheses s)
67   (regexp-substitute/global #f "(^|[^\\])([\\(\\)])" s 'pre 1 "\\" 2 'post))
68
69 (define (offset-add a b)
70   (cons (+ (car a) (car b))
71         (+ (cdr a) (cdr b))))
72
73 (define (ps-encoding text)
74   (escape-parentheses text))
75
76 ;; FIXME: lily-def
77 (define-public (ps-string-def prefix key val)
78   (string-append "/" prefix (symbol->string key) " ("
79                  (escape-parentheses val)
80                  ") def\n"))
81
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 lst thick)
101   (string-append 
102    (string-join (map ly:number-pair->string lst) " ")
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 lst)
130   (string-append 
131    (string-join (map ly:number-pair->string lst) " ")
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 (define (dot x y radius)
142   (string-append
143    " "
144    (ly:numbers->string
145     (list x y radius)) " draw_dot"))
146
147 (define (draw-line thick x1 y1 x2 y2)
148   (string-append 
149    "1 setlinecap 1 setlinejoin "
150    (ly:number->string thick) " setlinewidth "
151    (ly:number->string x1) " "
152    (ly:number->string y1) " moveto "
153    (ly:number->string x2) " "
154    (ly:number->string y2) " lineto stroke"))
155
156 (define (embedded-ps string)
157   string)
158
159 (define (ez-ball ch letter-col ball-col)
160   (string-append
161    " (" ch ") "
162    (ly:numbers->string (list letter-col ball-col))
163    ;; FIXME: barf
164    " /Helvetica-Bold "
165    " draw_ez_ball"))
166
167 ;; FIXME: use draw_round_box
168 (define (filledbox breapth width depth height)
169   (string-append (ly:numbers->string (list breapth width depth height))
170                  " draw_box"))
171
172 (define (glyph-string postscript-font-name x-y-named-glyphs)
173   (apply
174    string-append
175    (cons
176     (format #f " /~a findfont setfont " postscript-font-name)
177     (map (lambda  (item)
178            (format #f " ~a ~a rmoveto /~a glyphshow "
179                    (car item)
180                    (cadr item)
181                    (caddr item)))
182          x-y-named-glyphs))))
183
184 (define (grob-cause grob)
185   "")
186
187 ;; WTF is this in every backend?
188 (define (horizontal-line x1 x2 th)
189   (draw-line th x1 0 x2 0))
190
191 (define (lily-def key val)
192   (let ((prefix "lilypondlayout"))
193     (if (string=?
194          (substring key 0 (min (string-length prefix) (string-length key)))
195          prefix)
196         (string-append "/" key " {" val "} bind def\n")
197         (string-append "/" key " (" val ") def\n"))))
198
199 (define (named-glyph font glyph)
200   (string-append 
201    (ps-font-command font) " setfont " 
202    "/" glyph " glyphshow "))
203
204 (define (no-origin)
205   "")
206
207 (define (placebox x y s) 
208   (string-append 
209    (ly:number->string x) " " (ly:number->string y) " { " s " } place-box\n"))
210
211 (define (polygon points blotdiameter)
212   (string-append
213    (ly:numbers->string points) " "
214    (ly:number->string (/ (length points) 2)) " "
215    (ly:number->string blotdiameter)
216    " draw_polygon"))
217
218 (define (repeat-slash wid slope thick)
219   (string-append
220    (ly:numbers->string (list wid slope thick))
221    " draw_repeat_slash"))
222
223 (define (round-filled-box x y width height blotdiam)
224   (string-append
225    (ly:numbers->string
226     (list x y width height blotdiam)) " draw_round_box"))
227
228 (define (old-text font s)
229   ;; ugh, we should find a better way to
230   ;; extract the hsbw for /space from the font.
231   (let* ((space-length (cdar (ly:text-dimension font " "))) 
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)
245                                         " 0.0 rmoveto ")))
246        
247        (if (equal? #\space chr)
248            ""
249            (string-append word (make-string 1 chr))))
250      ""
251      (string-append s " "))
252
253     (string-append
254      (ps-font-command font) " setfont "
255      (string-join (reverse commands)))))
256
257 (define (new-text font s)
258   (let* ((space-length (cdar (ly:text-dimension font " ")))
259          (space-move (string-append (number->string space-length)
260                                     " 0.0 rmoveto "))
261          (input-enc (assoc-get 'input-name
262                                (ly:font-encoding-alist font)
263                                'latin1))
264          (out-vec (decode-byte-string input-enc s)))
265
266     (string-append
267      (ps-font-command font) " setfont "
268      (string-join
269       (vector->list
270        (vector-for-each
271         
272         (lambda (sym)
273           (if (eq? sym 'space)
274               space-move
275               (string-append "/" (symbol->string sym) " glyphshow")))
276         out-vec))))))
277
278 ;;(define text old-text)
279 (define text new-text)
280
281 ;; FIXME: BARF helvetica?
282 (define (white-text scale s)
283   (let ((mystring (string-append
284                    "(" s  ") " (number->string scale)
285                    " /Helvetica-Bold "
286                    " draw_white_text")))
287     mystring))
288
289 (define (unknown) 
290   "\n unknown\n")
291
292 (define (white-dot x y radius)
293   (string-append
294    " "
295    (ly:numbers->string
296     (list x y radius)) " draw_white_dot"))
297
298 (define (zigzag-line centre? zzw zzh thick dx dy)
299   (string-append
300    (if centre? "true" "false") " "
301    (ly:number->string zzw) " "
302    (ly:number->string zzh) " "
303    (ly:number->string thick) " "
304    "0 0 "
305    (ly:number->string dx) " "
306    (ly:number->string dy)
307    " draw_zigzag_line"))