]> git.donarmstrong.com Git - lilypond.git/blob - scm/output-ps.scm
(grob-cause): escape spaces in URLs.
[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             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
136 (define (glyph-string
137          postscript-font-name
138          size cid?
139          x-y-named-glyphs)
140
141   (define (encoding-vector-hack glyphs)
142     
143     ;; GS fucks up with glyphs that are not in the
144     ;; encoding vector.
145     (define (inner j glyphs)
146       (if (or (null? glyphs) (> j 256))
147           '()
148           (cons (format "dup ~a /~a put\n"
149                         j (car glyphs))
150                 (inner (1+ j) (cdr glyphs)))))
151           
152     (format "256 array 0 1 255 { 1 index exch /.notdef put} for\n ~a
153 /EncHack reencode-font /EncHack findfont"
154             (apply string-append (inner 32 glyphs))))
155     ;; END HACK.
156   
157   (format #f "gsave 1 output-scale div 1 output-scale div scale
158   /~a ~a ~a scalefont setfont\n~a grestore"
159           postscript-font-name
160           (if cid?
161               " /CIDFont findresource "
162               " findfont")
163         
164           size
165           (apply
166            string-append
167            (map (lambda  (item)
168                   (let*
169                       ((x (car item))
170                        (y (cadr item))
171                        (g (caddr item))
172                        (prefix (if  (string? g) "/" "")))
173
174                     (if (and (= 0.0 x)
175                              (= 0.0 y))
176                         (format #f " ~a~a glyphshow\n" prefix g)
177                         (format #f " ~a ~a rmoveto ~a~a glyphshow\n"
178                                 x y
179                                 prefix
180                                 g))))
181                 x-y-named-glyphs))))
182
183 (define (grob-cause offset grob)
184   (let* ((cause (ly:grob-property grob 'cause))
185          (music-origin (if (ly:music? cause)
186                            (ly:music-property cause 'origin))))
187     (if (not (ly:input-location? music-origin))
188         ""
189         (let* ((location (ly:input-file-line-char-column music-origin))
190                (raw-file (car location))
191                (file (if (is-absolute? raw-file)
192                          raw-file
193                          (string-append (ly-getcwd) "/" raw-file)))
194                (x-ext (ly:grob-extent grob grob X))
195                (y-ext (ly:grob-extent grob grob Y)))
196
197           (if (and (< 0 (interval-length x-ext))
198                    (< 0 (interval-length y-ext)))
199               (format "~a ~a ~a ~a (textedit://~a:~a:~a:~a) mark_URI\n"
200                       (+ (car offset) (car x-ext))
201                       (+ (cdr offset) (car y-ext))
202                       (+ (car offset) (cdr x-ext))
203                       (+ (cdr offset) (cdr y-ext))
204
205                       ;; TODO
206                       ;; full escaping.
207                       (string-regexp-substitute " " "%20" file)
208                       (cadr location)
209                       (caddr location)
210                       (cadddr location))
211               "")))))
212
213 (define (lily-def key val)
214   (let ((prefix "lilypondlayout"))
215     (if (string=?
216          (substring key 0 (min (string-length prefix) (string-length key)))
217          prefix)
218         (string-append "/" key " {" val "} bind def\n")
219         (string-append "/" key " (" val ") def\n"))))
220
221 (define (named-glyph font glyph)
222   (string-append 
223    (ps-font-command font) " setfont " 
224    "/" glyph " glyphshow "))
225
226 (define (no-origin)
227   "")
228
229 (define (placebox x y s) 
230   (string-append 
231    (ly:number->string x) " " (ly:number->string y) " { " s " } place-box\n"))
232
233 (define (polygon points blotdiameter filled?)
234   (string-append
235    (ly:numbers->string points) " "
236    (ly:number->string (/ (length points) 2)) " "
237    (ly:number->string blotdiameter)
238    (if filled? " true " " false ")
239    " draw_polygon"))
240
241 (define (repeat-slash wid slope thick)
242   (string-append
243    (ly:numbers->string (list wid slope thick))
244    " draw_repeat_slash"))
245
246 ;; restore color from stack
247 (define (resetcolor)
248   (string-append "setrgbcolor\n"))
249
250 (define (round-filled-box x y width height blotdiam)
251   (string-append
252    (ly:numbers->string
253     (list x y width height blotdiam)) " draw_round_box"))
254
255 ;; save current color on stack and set new color
256 (define (setcolor r g b)
257   (string-append "currentrgbcolor "
258   (ly:numbers->string (list r g b))
259   " setrgbcolor\n"))
260
261 (define (text font s)
262   ;; (ly:warning (_ "TEXT backend-command encountered in Pango backend"))
263   ;; (ly:warning (_ "Arguments: ~a ~a"" font str))
264   
265   (let* ((space-length (cdar (ly:text-dimension font " ")))
266          (space-move (string-append (number->string space-length)
267                                     " 0.0 rmoveto "))
268          (out-vec (decode-byte-string s)))
269
270     (string-append
271      (ps-font-command font) " setfont "
272      (string-join
273       (vector->list
274        (vector-for-each
275         
276         (lambda (sym)
277           (if (eq? sym 'space)
278               space-move
279               (string-append "/" (symbol->string sym) " glyphshow")))
280         out-vec))))))
281
282 (define (unknown) 
283   "\n unknown\n")
284
285 (define (url-link url x y)
286   (format "~a ~a ~a ~a (~a) mark_URI"
287           (car x)
288           (car y)
289           (cdr x)
290           (cdr y)
291           url))
292
293 (define (utf-8-string pango-font-description string)
294   (ly:warning (_ "utf-8-string encountered in PS backend")))
295
296
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"))