]> 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
173 (define (glyph-string
174          postscript-font-name
175          size
176          x-y-named-glyphs)
177   (format #f "gsave 1 output-scale div 1 output-scale div scale
178   /~a findfont ~a scalefont setfont\n~a grestore" postscript-font-name size
179   (apply
180    string-append
181    (map (lambda  (item)
182           (let
183               ((x (car item))
184                 (y (cadr item))
185                 (g (caddr item)))
186
187             (if (and (= 0.0 x)
188                      (= 0.0 y))
189                 (format #f " /~a glyphshow " g)
190                 (format #f " ~a ~a rmoveto /~a glyphshow "
191                         x y g))))
192         x-y-named-glyphs))
193   ))
194
195 (define (grob-cause grob)
196   "")
197
198 ;; WTF is this in every backend?
199 (define (horizontal-line x1 x2 th)
200   (draw-line th x1 0 x2 0))
201
202 (define (lily-def key val)
203   (let ((prefix "lilypondlayout"))
204     (if (string=?
205          (substring key 0 (min (string-length prefix) (string-length key)))
206          prefix)
207         (string-append "/" key " {" val "} bind def\n")
208         (string-append "/" key " (" val ") def\n"))))
209
210 (define (named-glyph font glyph)
211   (string-append 
212    (ps-font-command font) " setfont " 
213    "/" glyph " glyphshow "))
214
215 (define (no-origin)
216   "")
217
218 (define (placebox x y s) 
219   (string-append 
220    (ly:number->string x) " " (ly:number->string y) " { " s " } place-box\n"))
221
222 (define (polygon points blotdiameter)
223   (string-append
224    (ly:numbers->string points) " "
225    (ly:number->string (/ (length points) 2)) " "
226    (ly:number->string blotdiameter)
227    " draw_polygon"))
228
229 (define (repeat-slash wid slope thick)
230   (string-append
231    (ly:numbers->string (list wid slope thick))
232    " draw_repeat_slash"))
233
234 (define (round-filled-box x y width height blotdiam)
235   (string-append
236    (ly:numbers->string
237     (list x y width height blotdiam)) " draw_round_box"))
238
239 (define (old-text font s)
240   ;; ugh, we should find a better way to
241   ;; extract the hsbw for /space from the font.
242   (let* ((space-length (cdar (ly:text-dimension font " "))) 
243          (commands '())
244          (add-command (lambda (x) (set! commands (cons x commands)))))
245
246     (string-fold
247      (lambda (chr word)
248        "Translate space as into moveto, group the rest in words."
249        (if (and (< 0 (string-length word))
250                 (equal? #\space  chr))
251            (add-command 
252             (string-append "(" (ps-encoding word) ") show\n")))
253
254        (if (equal? #\space chr)
255            (add-command  (string-append (number->string space-length)
256                                         " 0.0 rmoveto ")))
257        
258        (if (equal? #\space chr)
259            ""
260            (string-append word (make-string 1 chr))))
261      ""
262      (string-append s " "))
263
264     (string-append
265      (ps-font-command font) " setfont "
266      (string-join (reverse commands)))))
267
268 (define (new-text font s)
269   (let* ((space-length (cdar (ly:text-dimension font " ")))
270          (space-move (string-append (number->string space-length)
271                                     " 0.0 rmoveto "))
272          (input-enc 'latin1)
273          (out-vec (decode-byte-string input-enc s)))
274
275     (string-append
276      (ps-font-command font) " setfont "
277      (string-join
278       (vector->list
279        (vector-for-each
280         
281         (lambda (sym)
282           (if (eq? sym 'space)
283               space-move
284               (string-append "/" (symbol->string sym) " glyphshow")))
285         out-vec))))))
286
287 ;;(define text old-text)
288 (define (text font str)
289   (ly:warn "TEXT backend-command encountered in Pango backend\nargs: ~a ~a" font str)
290   (new-text font str))
291
292 ;; FIXME: BARF helvetica?
293 (define (white-text scale s)
294   (let ((mystring (string-append
295                    "(" s  ") " (number->string scale)
296                    " /Helvetica-Bold "
297                    " draw_white_text")))
298     mystring))
299
300 (define (unknown) 
301   "\n unknown\n")
302
303 (define (white-dot x y radius)
304   (string-append
305    " "
306    (ly:numbers->string
307     (list x y radius)) " draw_white_dot"))
308
309 (define (zigzag-line centre? zzw zzh thick dx dy)
310   (string-append
311    (if centre? "true" "false") " "
312    (ly:number->string zzw) " "
313    (ly:number->string zzh) " "
314    (ly:number->string thick) " "
315    "0 0 "
316    (ly:number->string dx) " "
317    (ly:number->string dy)
318    " draw_zigzag_line"))