]> git.donarmstrong.com Git - lilypond.git/blob - scm/output-ps.scm
* lily/ly-module.cc (LY_DEFINE): bugfix.
[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--2004 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   #:export (define-fonts
21              unknown
22              select-font
23              blank
24              dot
25              beam
26              bracket
27              dashed-slur
28              char
29              dashed-line
30              zigzag-line
31              symmetric-x-triangle
32              ez-ball
33              comment
34              end-output
35              experimental-on
36              repeat-slash
37              header-end
38              header
39              placebox
40              bezier-sandwich
41              start-system
42              stop-system
43              stop-last-system
44              horizontal-line
45              filledbox
46              round-filled-box
47              text
48              tuplet
49              polygon
50              draw-line
51              define-origin
52              no-origin
53              start-page
54              stop-page
55              ))
56
57 (use-modules (guile)
58              (ice-9 regex)
59              (srfi srfi-1)
60              (srfi srfi-13)
61              (lily))
62
63 ;;; helper functions, not part of output interface
64 (define (escape-parentheses s)
65   (regexp-substitute/global #f "(^|[^\\])([\\(\\)])" s 'pre 1 "\\" 2 'post))
66
67 (define (offset-add a b)
68   (cons (+ (car a) (car b))
69         (+ (cdr a) (cdr b))))
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 (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 l thick)
98   (string-append 
99    (string-join (map ly:number-pair->string l) " ")
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 (char font i)
111   (string-append 
112     (ps-font-command font) " setfont " 
113    "(\\" (ly:inexact->string i 8) ") show" ))
114
115 (define (comment s)
116   (string-append "% " s "\n"))
117
118 (define (dashed-line thick on off dx dy)
119   (string-append 
120    (ly:number->string dx) " "
121    (ly:number->string dy) " "
122    (ly:number->string thick)
123    " [ "
124    (ly:number->string on) " "
125    (ly:number->string off)
126    " ] 0 draw_dashed_line"))
127
128 ;; what the heck is this interface ?
129 (define (dashed-slur thick dash l)
130   (string-append 
131    (string-join (map ly:number-pair->string l) " ")
132    " "
133    (ly:number->string thick) 
134    " [ "
135    (ly:number->string dash)
136    " "
137    ;;UGH.  10 ?
138    (ly:number->string (* 10 thick))
139    " ] 0 draw_dashed_slur"))
140
141 ; todo: merge with tex-font-command?
142
143 (define-public (ps-font-command font . override-coding)
144   (let* ((name (ly:font-filename font))
145          (magnify (ly:font-magnification font))
146          (coding-alist (ly:font-encoding-alist font))
147          (input-encoding (assoc-get 'input-name coding-alist))
148          (font-encoding (assoc-get 'output-name coding-alist))
149          (coding-command (if (null? override-coding)
150                              (if (equal? input-encoding font-encoding)
151                                  #f font-encoding)
152                              (car override-coding))))
153
154     ;; FIXME:  now feta stuff has feta* input-encoding (again?)
155     ;;(format (current-error-port) "FONT: ~S, ~S\n" name font-encoding)
156     ;;(format (current-error-port) "INPUT: ~S\n" input-encoding)
157     (if (and coding-command (equal? (substring coding-command 0 4) "feta"))
158         (set! coding-command #f))
159
160     (string-append
161      "magfont" (string-encode-integer (hashq  name 1000000))
162      "m" (string-encode-integer (inexact->exact (round (* 1000 magnify))))
163      (if (not coding-command) "" (string-append "e" coding-command)))))
164
165 (define (define-origin file line col) "")
166
167 (define (dot x y radius)
168   (string-append
169    " "
170    (ly:numbers->string
171     (list x y radius)) " draw_dot"))
172
173 (define (draw-line thick x1 y1 x2 y2)
174   (string-append 
175    "1 setlinecap 1 setlinejoin "
176    (ly:number->string thick) " setlinewidth "
177    (ly:number->string x1) " "
178    (ly:number->string y1) " moveto "
179    (ly:number->string x2) " "
180    (ly:number->string y2) " lineto stroke"))
181
182 (define (end-output)
183   "\nend-lilypond-output\n")
184
185 (define (ez-ball ch letter-col ball-col)
186   (string-append
187    " (" ch ") "
188    (ly:numbers->string (list letter-col ball-col))
189    " /Helvetica-Bold " ;; ugh
190    " draw_ez_ball"))
191
192 (define (filledbox breapth width depth height) ; FIXME : use draw_round_box
193   (string-append (ly:numbers->string (list breapth width depth height))
194                  " draw_box"))
195
196 ;; WTF is this in every backend?
197 (define (horizontal-line x1 x2 th)
198   (draw-line th x1 0 x2 0))
199
200 (define (lily-def key val)
201   (let ((prefix "lilypondpaper"))
202     (if (string=?
203          (substring key 0 (min (string-length prefix) (string-length key)))
204          prefix)
205         (string-append "/" key " {" val "} bind def\n")
206         (string-append "/" key " (" val ") def\n"))))
207
208 (define (no-origin) "")
209
210
211
212 (define (placebox x y s) 
213   (string-append 
214    (ly:number->string x) " " (ly:number->string y) " { " s " } place-box\n"))
215
216 (define (polygon points blotdiameter)
217   (string-append
218    (ly:numbers->string points) " "
219    (ly:number->string (/ (length points) 2)) " "
220    (ly:number->string blotdiameter)
221    " draw_polygon"))
222
223 (define (repeat-slash wid slope thick)
224   (string-append
225    (ly:numbers->string (list wid slope thick))
226    " draw_repeat_slash"))
227
228 (define (round-filled-box x y width height blotdiam)
229    (string-append
230     (ly:numbers->string
231      (list x y width height blotdiam)) " draw_round_box"))
232
233
234 (define (stem breapth width depth height) ; FIXME: use draw_round_box.
235   (string-append
236    (ly:numbers->string (list breapth width depth height))
237    " draw_box" ))
238
239 (define (symmetric-x-triangle thick w h)
240   (string-append
241    (ly:numbers->string (list h w thick))
242    " draw_symmetric_x_triangle"))
243
244 (define (text font s)
245   (let*
246       
247       (
248        ;; ugh, we should find a better way to
249        ;; extract the hsbw for /space from the font.
250        
251        (space-length (cdar (ly:text-dimension font "t"))) 
252        (commands '())
253        (add-command (lambda (x) (set! commands (cons x commands)))) )
254
255     (string-fold
256      (lambda (chr word)
257        "Translate space as into moveto, group the rest in words."
258        (if (and (< 0 (string-length word))
259                 (equal? #\space  chr))
260            (add-command 
261             (string-append "(" (ps-encoding word) ") show\n")))
262
263        (if (equal? #\space  chr)
264            (add-command  (string-append (number->string space-length) " 0.0 rmoveto ")) )
265        
266        (if (equal? #\space  chr)
267            ""
268            (string-append word (make-string 1 chr))))
269      ""
270      (string-append  s " "))
271
272     (string-append
273      (ps-font-command font) " setfont "
274      (string-join (reverse commands)))
275     ))
276   
277 (define (unknown) 
278   "\n unknown\n")
279
280 (define (zigzag-line centre? zzw zzh thick dx dy)
281   (string-append
282     (if centre? "true" "false") " "
283     (ly:number->string zzw) " "
284     (ly:number->string zzh) " "
285     (ly:number->string thick) " "
286     "0 0 "
287     (ly:number->string dx) " "
288     (ly:number->string dy)
289     " draw_zigzag_line"))