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