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