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