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