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