]> git.donarmstrong.com Git - lilypond.git/blob - scm/output-ps.scm
* scm/markup.scm:
[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 (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             circle
25             dot
26             white-dot
27             beam
28             bracket
29             dashed-slur
30             char
31             setcolor
32             resetcolor
33             named-glyph
34             dashed-line
35             zigzag-line
36             ez-ball
37             comment
38             repeat-slash
39             placebox
40             bezier-sandwich
41             horizontal-line
42             embedded-ps
43             filledbox
44             round-filled-box
45             text
46             white-text
47             polygon
48             draw-line
49             no-origin))
50
51
52 (use-modules (guile)
53              (ice-9 regex)
54              (srfi srfi-1)
55              (srfi srfi-13)
56              (scm framework-ps)
57              (lily))
58
59
60 ;;(map export
61 ;;   (append (ly:all-stencil-expressions) (ly:all-output-backend-commands)))
62
63 ;; huh?
64 ;;(write (ly:all-output-backend-commands))
65 ;;(write (ly:all-stencil-expressions))
66
67
68 ;;; helper functions, not part of output interface
69 (define (escape-parentheses s)
70   (regexp-substitute/global #f "(^|[^\\])([\\(\\)])" s 'pre 1 "\\" 2 'post))
71
72 (define (ps-encoding text)
73   (escape-parentheses text))
74
75 ;; FIXME: lily-def
76 (define-public (ps-string-def prefix key val)
77   (string-append "/" prefix (symbol->string key) " ("
78                  (escape-parentheses val)
79                  ") def\n"))
80
81
82 (define (ps-number-def prefix key val)
83   (let ((s (if (integer? val)
84                (ly:number->string val)
85                (ly:number->string (exact->inexact val)))))
86     (string-append "/" prefix (symbol->string key) " " s " def\n")))
87
88
89 ;;;
90 ;;; Lily output interface, PostScript implementation --- cleanup and docme
91 ;;;
92
93 ;;; Output-interface functions
94 (define (beam width slope thick blot)
95   (string-append
96    (ly:numbers->string (list slope width thick blot)) " draw_beam" ))
97
98 ;; two beziers
99 (define (bezier-sandwich lst thick)
100   (string-append 
101    (string-join (map ly:number-pair->string lst) " ")
102    " "
103    (ly:number->string thick)
104    " draw_bezier_sandwich"))
105
106 (define (bracket arch_angle arch_width arch_height height arch_thick thick)
107   (string-append
108    (ly:numbers->string
109     (list arch_angle arch_width arch_height height arch_thick thick))
110    " draw_bracket"))
111
112 (define (circle radius thick)
113   (format
114    "~a ~a draw_circle" radius thick))
115
116 (define (char font i)
117   (string-append 
118    (ps-font-command font) " setfont " 
119    "(\\" (ly:inexact->string i 8) ") show"))
120
121 (define (dashed-line thick on off dx dy)
122   (string-append 
123    (ly:number->string dx) " "
124    (ly:number->string dy) " "
125    (ly:number->string thick)
126    " [ "
127    (ly:number->string on) " "
128    (ly:number->string off)
129    " ] 0 draw_dashed_line"))
130
131 ;; what the heck is this interface ?
132 (define (dashed-slur thick on off l)
133   (string-append 
134    (string-join (map ly:number-pair->string l) " ")
135    " "
136    (ly:number->string thick) 
137    " [ "
138    (ly:number->string on)
139    " "   
140    (ly:number->string off)
141    " ] 0 draw_dashed_slur"))
142
143 (define (dot x y radius)
144   (string-append
145    " "
146    (ly:numbers->string
147     (list x y radius)) " draw_dot"))
148
149 (define (draw-line thick x1 y1 x2 y2)
150   (string-append 
151    "1 setlinecap 1 setlinejoin "
152    (ly:number->string thick) " setlinewidth "
153    (ly:number->string x1) " "
154    (ly:number->string y1) " moveto "
155    (ly:number->string x2) " "
156    (ly:number->string y2) " lineto stroke"))
157
158 (define (embedded-ps string)
159   string)
160
161 ;; FIXME. 
162 (define (ez-ball ch letter-col ball-col)
163   (string-append
164    " (" ch ") "
165    (ly:numbers->string (list letter-col ball-col))
166    ;; FIXME: barf
167    " /Helvetica-Bold "
168    " draw_ez_ball"))
169
170 ;; FIXME: use draw_round_box
171 (define (filledbox breapth width depth height)
172   (string-append (ly:numbers->string (list breapth width depth height))
173                  " draw_box"))
174
175 (define (glyph-string
176          postscript-font-name
177          size
178          x-y-named-glyphs)
179   (format #f "gsave 1 output-scale div 1 output-scale div scale
180   /~a findfont ~a scalefont setfont\n~a grestore" postscript-font-name size
181   (apply
182    string-append
183    (map (lambda  (item)
184           (let
185               ((x (car item))
186                 (y (cadr item))
187                 (g (caddr item)))
188
189             (if (and (= 0.0 x)
190                      (= 0.0 y))
191                 (format #f " /~a glyphshow " g)
192                 (format #f " ~a ~a rmoveto /~a glyphshow "
193                         x y g))))
194         x-y-named-glyphs))
195   ))
196
197 (define (grob-cause offset grob)
198   (let* ((cause (ly:grob-property grob 'cause))
199          (music-origin (if (ly:music? cause)
200                            (ly:music-property cause 'origin)))
201          (location (if (ly:input-location? music-origin)
202                        (ly:input-file-line-column music-origin)
203                        #f
204                        ))
205          (file (if location (string-append (getcwd) "/" (car location))
206                    #f))
207          (x-ext (ly:grob-extent grob grob X)) 
208          (y-ext (ly:grob-extent grob grob Y)) 
209          )
210
211     (if (and location
212              (< 0 (interval-length x-ext))
213              (< 0 (interval-length y-ext)))
214         
215         (format "~a ~a ~a ~a (textedit://~a:~a:~a) mark_URI\n"
216                 (+ (car offset) (car x-ext))
217                 (+ (cdr offset) (car y-ext))
218                 (+ (car offset) (cdr x-ext))
219                 (+ (cdr offset) (cdr y-ext))
220                 file
221                 (cadr location)
222                 (caddr location))
223         "")))
224
225 ;; WTF is this in every backend?
226 (define (horizontal-line x1 x2 th)
227   (draw-line th x1 0 x2 0))
228
229 (define (lily-def key val)
230   (let ((prefix "lilypondlayout"))
231     (if (string=?
232          (substring key 0 (min (string-length prefix) (string-length key)))
233          prefix)
234         (string-append "/" key " {" val "} bind def\n")
235         (string-append "/" key " (" val ") def\n"))))
236
237 (define (named-glyph font glyph)
238   (string-append 
239    (ps-font-command font) " setfont " 
240    "/" glyph " glyphshow "))
241
242 (define (no-origin)
243   "")
244
245 (define (placebox x y s) 
246   (string-append 
247    (ly:number->string x) " " (ly:number->string y) " { " s " } place-box\n"))
248
249 (define (polygon points blotdiameter)
250   (string-append
251    (ly:numbers->string points) " "
252    (ly:number->string (/ (length points) 2)) " "
253    (ly:number->string blotdiameter)
254    " draw_polygon"))
255
256 (define (repeat-slash wid slope thick)
257   (string-append
258    (ly:numbers->string (list wid slope thick))
259    " draw_repeat_slash"))
260
261 ;; restore color from stack
262 (define (resetcolor)
263   (string-append "setrgbcolor\n"))
264
265 (define (round-filled-box x y width height blotdiam)
266   (string-append
267    (ly:numbers->string
268     (list x y width height blotdiam)) " draw_round_box"))
269
270 ;; save current color on stack and set new color
271 (define (setcolor r g b)
272   (string-append "currentrgbcolor "
273   (ly:numbers->string (list r g b))
274   " setrgbcolor\n"))
275
276 (define (text font s)
277 ;  (ly:warn "TEXT backend-command encountered in Pango backend\nargs: ~a ~a" font str)
278   
279   (let* ((space-length (cdar (ly:text-dimension font " ")))
280          (space-move (string-append (number->string space-length)
281                                     " 0.0 rmoveto "))
282          (out-vec (decode-byte-string s)))
283
284     (string-append
285      (ps-font-command font) " setfont "
286      (string-join
287       (vector->list
288        (vector-for-each
289         
290         (lambda (sym)
291           (if (eq? sym 'space)
292               space-move
293               (string-append "/" (symbol->string sym) " glyphshow")))
294         out-vec))))))
295
296 (define (unknown) 
297   "\n unknown\n")
298
299 (define (url-link url x y)
300   (format "~a ~a ~a ~a (~a) mark_URI"
301           (car x)
302           (car y)
303           (cdr x)
304           (cdr y)
305           url))
306
307 (define (utf8-string pango-font-description string)
308   (ly:warn "utf8-string encountered in PS backend"))
309
310 (define (white-dot x y radius)
311   (string-append
312    " "
313    (ly:numbers->string
314     (list x y radius)) " draw_white_dot"))
315
316 (define (white-text scale s)
317   (let ((mystring (string-append
318                    "(" s  ") " (number->string scale)
319                    " /Helvetica-Bold "
320                    " draw_white_text")))
321     mystring
322     ))
323
324 (define (zigzag-line centre? zzw zzh thick dx dy)
325   (string-append
326    (if centre? "true" "false") " "
327    (ly:number->string zzw) " "
328    (ly:number->string zzh) " "
329    (ly:number->string thick) " "
330    "0 0 "
331    (ly:number->string dx) " "
332    (ly:number->string dy)
333    " draw_zigzag_line"))