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