]> git.donarmstrong.com Git - lilypond.git/blob - scm/output-ps.scm
* lily/span-bar.cc (print): sort bar line extents. Fixes problem
[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   (format #f "gsave 1 output-scale div 1 output-scale div scale
155   /~a ~a ~a scalefont setfont\n~a grestore"
156           postscript-font-name
157           (if cid?
158               " /CIDFont findresource "
159               " findfont") 
160           
161           size
162           (apply
163            string-append
164            (map (lambda  (item)
165                   (let*
166                       ((x (car item))
167                        (y (cadr item))
168                        (g (caddr item))
169                        (prefix (if  (string? g) "/" "")))
170
171                     (if (and (= 0.0 x)
172                              (= 0.0 y))
173                         (format #f " ~a~a glyphshow\n" prefix g)
174                         (format #f " ~a ~a rmoveto ~a~a glyphshow\n"
175                                 x y
176                                 prefix
177                                 g))))
178                 x-y-named-glyphs))))
179
180 (define (grob-cause offset grob)
181   (let* ((cause (ly:grob-property grob 'cause))
182          (music-origin (if (ly:music? cause)
183                            (ly:music-property cause 'origin))))
184     (if (not (ly:input-location? music-origin))
185         ""
186         (let* ((location (ly:input-file-line-column music-origin))
187                (raw-file (car location))
188                (file (if (is-absolute? raw-file)
189                          raw-file
190                          (string-append (ly-getcwd) "/" raw-file)))
191                (x-ext (ly:grob-extent grob grob X))
192                (y-ext (ly:grob-extent grob grob Y)))
193
194           (if (and (< 0 (interval-length x-ext))
195                    (< 0 (interval-length y-ext)))
196               (format "~a ~a ~a ~a (textedit://~a:~a:~a) mark_URI\n"
197                       (+ (car offset) (car x-ext))
198                       (+ (cdr offset) (car y-ext))
199                       (+ (car offset) (cdr x-ext))
200                       (+ (cdr offset) (cdr y-ext))
201                       file
202                       (cadr location)
203                       (caddr location))
204               "")))))
205
206 (define (lily-def key val)
207   (let ((prefix "lilypondlayout"))
208     (if (string=?
209          (substring key 0 (min (string-length prefix) (string-length key)))
210          prefix)
211         (string-append "/" key " {" val "} bind def\n")
212         (string-append "/" key " (" val ") def\n"))))
213
214 (define (named-glyph font glyph)
215   (string-append 
216    (ps-font-command font) " setfont " 
217    "/" glyph " glyphshow "))
218
219 (define (no-origin)
220   "")
221
222 (define (placebox x y s) 
223   (string-append 
224    (ly:number->string x) " " (ly:number->string y) " { " s " } place-box\n"))
225
226 (define (polygon points blotdiameter filled?)
227   (string-append
228    (ly:numbers->string points) " "
229    (ly:number->string (/ (length points) 2)) " "
230    (ly:number->string blotdiameter)
231    (if filled? " true " " false ")
232    " draw_polygon"))
233
234 (define (repeat-slash wid slope thick)
235   (string-append
236    (ly:numbers->string (list wid slope thick))
237    " draw_repeat_slash"))
238
239 ;; restore color from stack
240 (define (resetcolor)
241   (string-append "setrgbcolor\n"))
242
243 (define (round-filled-box x y width height blotdiam)
244   (string-append
245    (ly:numbers->string
246     (list x y width height blotdiam)) " draw_round_box"))
247
248 ;; save current color on stack and set new color
249 (define (setcolor r g b)
250   (string-append "currentrgbcolor "
251   (ly:numbers->string (list r g b))
252   " setrgbcolor\n"))
253
254 (define (text font s)
255   ;; (ly:warning (_ "TEXT backend-command encountered in Pango backend"))
256   ;; (ly:warning (_ "Arguments: ~a ~a"" font str))
257   
258   (let* ((space-length (cdar (ly:text-dimension font " ")))
259          (space-move (string-append (number->string space-length)
260                                     " 0.0 rmoveto "))
261          (out-vec (decode-byte-string s)))
262
263     (string-append
264      (ps-font-command font) " setfont "
265      (string-join
266       (vector->list
267        (vector-for-each
268         
269         (lambda (sym)
270           (if (eq? sym 'space)
271               space-move
272               (string-append "/" (symbol->string sym) " glyphshow")))
273         out-vec))))))
274
275 (define (unknown) 
276   "\n unknown\n")
277
278 (define (url-link url x y)
279   (format "~a ~a ~a ~a (~a) mark_URI"
280           (car x)
281           (car y)
282           (cdr x)
283           (cdr y)
284           url))
285
286 (define (utf8-string pango-font-description string)
287   (ly:warning (_ "utf8-string encountered in PS backend")))
288
289
290 ;; TODO: FIX THIS.
291 ;;
292 (define (white-dot x y radius)
293   (string-append
294    " "
295    (ly:numbers->string
296     (list x y radius)) " draw_white_dot"))
297
298 (define (white-text scale s)
299   (let ((mystring (string-append
300                    "(" s  ") " (number->string scale)
301                    " /Helvetica-Bold "
302                    " draw_white_text")))
303     mystring
304     ))
305
306 (define (zigzag-line centre? zzw zzh thick dx dy)
307   (string-append
308    (if centre? "true" "false") " "
309    (ly:number->string zzw) " "
310    (ly:number->string zzh) " "
311    (ly:number->string thick) " "
312    "0 0 "
313    (ly:number->string dx) " "
314    (ly:number->string dy)
315    " draw_zigzag_line"))