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