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