]> 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--2005 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 (define-module (scm output-ps)
17   #:re-export (quote)
18
19   ;; JUNK this -- see lily.scm: ly:all-output-backend-commands
20   #:export (unknown
21             blank
22             circle
23             dot
24             white-dot
25             beam
26             bracket
27             dashed-slur
28             char
29             setcolor
30             resetcolor
31             named-glyph
32             dashed-line
33             zigzag-line
34             ez-ball
35             comment
36             repeat-slash
37             placebox
38             bezier-sandwich
39             horizontal-line
40             embedded-ps
41             filledbox
42             round-filled-box
43             text
44             white-text
45             polygon
46             draw-line
47             no-origin))
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 (ps-encoding text)
71   (escape-parentheses text))
72
73 ;; FIXME: lily-def
74 (define-public (ps-string-def prefix key val)
75   (string-append "/" prefix (symbol->string key) " ("
76                  (escape-parentheses val)
77                  ") def\n"))
78
79
80 (define (ps-number-def prefix key val)
81   (let ((s (if (integer? val)
82                (ly:number->string val)
83                (ly:number->string (exact->inexact val)))))
84     (string-append "/" prefix (symbol->string key) " " s " def\n")))
85
86
87 ;;;
88 ;;; Lily output interface, PostScript implementation --- cleanup and docme
89 ;;;
90
91 ;;; Output-interface functions
92 (define (beam width slope thick blot)
93   (string-append
94    (ly:numbers->string (list slope width thick blot)) " draw_beam" ))
95
96 ;; two beziers
97 (define (bezier-sandwich lst thick)
98   (string-append 
99    (string-join (map ly:number-pair->string lst) " ")
100    " "
101    (ly:number->string thick)
102    " draw_bezier_sandwich"))
103
104 (define (bracket arch_angle arch_width arch_height height arch_thick thick)
105   (string-append
106    (ly:numbers->string
107     (list arch_angle arch_width arch_height height arch_thick thick))
108    " draw_bracket"))
109
110 (define (circle radius thick)
111   (format
112    "~a ~a draw_circle" radius thick))
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 (dashed-line thick on off dx dy)
120   (string-append 
121    (ly:number->string dx) " "
122    (ly:number->string dy) " "
123    (ly:number->string thick)
124    " [ "
125    (ly:number->string on) " "
126    (ly:number->string off)
127    " ] 0 draw_dashed_line"))
128
129 ;; what the heck is this interface ?
130 (define (dashed-slur thick on off l)
131   (string-append 
132    (string-join (map ly:number-pair->string l) " ")
133    " "
134    (ly:number->string thick) 
135    " [ "
136    (ly:number->string on)
137    " "   
138    (ly:number->string off)
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 ;; FIXME. 
160 (define (ez-ball ch letter-col ball-col)
161   (string-append
162    " (" ch ") "
163    (ly:numbers->string (list letter-col ball-col))
164    ;; FIXME: barf
165    " /Helvetica-Bold "
166    " draw_ez_ball"))
167
168 ;; FIXME: use draw_round_box
169 (define (filledbox breapth width depth height)
170   (string-append (ly:numbers->string (list breapth width depth height))
171                  " draw_box"))
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 offset grob)
196   (let* ((cause (ly:grob-property grob 'cause))
197          (music-origin (if (ly:music? cause)
198                            (ly:music-property cause 'origin)))
199          (location (if (ly:input-location? music-origin)
200                        (ly:input-file-line-column music-origin)
201                        #f
202                        ))
203          (file (if location (string-append (getcwd) "/" (car location))
204                    #f))
205          (x-ext (ly:grob-extent grob grob X)) 
206          (y-ext (ly:grob-extent grob grob Y)) 
207          )
208
209     (if (and location
210              (< 0 (interval-length x-ext))
211              (< 0 (interval-length y-ext)))
212         
213         (format "~a ~a ~a ~a (textedit://~a:~a:~a) mark_URI\n"
214                 (+ (car offset) (car x-ext))
215                 (+ (cdr offset) (car y-ext))
216                 (+ (car offset) (cdr x-ext))
217                 (+ (cdr offset) (cdr y-ext))
218                 file
219                 (cadr location)
220                 (caddr location))
221         "")))
222
223 ;; WTF is this in every backend?
224 (define (horizontal-line x1 x2 th)
225   (draw-line th x1 0 x2 0))
226
227 (define (lily-def key val)
228   (let ((prefix "lilypondlayout"))
229     (if (string=?
230          (substring key 0 (min (string-length prefix) (string-length key)))
231          prefix)
232         (string-append "/" key " {" val "} bind def\n")
233         (string-append "/" key " (" val ") def\n"))))
234
235 (define (named-glyph font glyph)
236   (string-append 
237    (ps-font-command font) " setfont " 
238    "/" glyph " glyphshow "))
239
240 (define (no-origin)
241   "")
242
243 (define (placebox x y s) 
244   (string-append 
245    (ly:number->string x) " " (ly:number->string y) " { " s " } place-box\n"))
246
247 (define (polygon points blotdiameter)
248   (string-append
249    (ly:numbers->string points) " "
250    (ly:number->string (/ (length points) 2)) " "
251    (ly:number->string blotdiameter)
252    " draw_polygon"))
253
254 (define (repeat-slash wid slope thick)
255   (string-append
256    (ly:numbers->string (list wid slope thick))
257    " draw_repeat_slash"))
258
259 ;; restore color from stack
260 (define (resetcolor)
261   (string-append "setrgbcolor\n"))
262
263 (define (round-filled-box x y width height blotdiam)
264   (string-append
265    (ly:numbers->string
266     (list x y width height blotdiam)) " draw_round_box"))
267
268 ;; save current color on stack and set new color
269 (define (setcolor r g b)
270   (string-append "currentrgbcolor "
271   (ly:numbers->string (list r g b))
272   " setrgbcolor\n"))
273
274 (define (text font s)
275   ;; (ly:warning (_ "TEXT backend-command encountered in Pango backend"))
276   ;; (ly:warning (_ "Arguments: ~a ~a"" font str))
277   
278   (let* ((space-length (cdar (ly:text-dimension font " ")))
279          (space-move (string-append (number->string space-length)
280                                     " 0.0 rmoveto "))
281          (out-vec (decode-byte-string s)))
282
283     (string-append
284      (ps-font-command font) " setfont "
285      (string-join
286       (vector->list
287        (vector-for-each
288         
289         (lambda (sym)
290           (if (eq? sym 'space)
291               space-move
292               (string-append "/" (symbol->string sym) " glyphshow")))
293         out-vec))))))
294
295 (define (unknown) 
296   "\n unknown\n")
297
298 (define (url-link url x y)
299   (format "~a ~a ~a ~a (~a) mark_URI"
300           (car x)
301           (car y)
302           (cdr x)
303           (cdr y)
304           url))
305
306 (define (utf8-string pango-font-description string)
307   (ly:warning (_ "utf8-string encountered in PS backend")))
308
309 (define (white-dot x y radius)
310   (string-append
311    " "
312    (ly:numbers->string
313     (list x y radius)) " draw_white_dot"))
314
315 (define (white-text scale s)
316   (let ((mystring (string-append
317                    "(" s  ") " (number->string scale)
318                    " /Helvetica-Bold "
319                    " draw_white_text")))
320     mystring
321     ))
322
323 (define (zigzag-line centre? zzw zzh thick dx dy)
324   (string-append
325    (if centre? "true" "false") " "
326    (ly:number->string zzw) " "
327    (ly:number->string zzh) " "
328    (ly:number->string thick) " "
329    "0 0 "
330    (ly:number->string dx) " "
331    (ly:number->string dy)
332    " draw_zigzag_line"))