]> git.donarmstrong.com Git - lilypond.git/blob - scm/output-ps.scm
Fix internalsrefs
[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
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             named-glyph
31             dashed-line
32             zigzag-line
33             ez-ball
34             comment
35             repeat-slash
36             placebox
37             bezier-sandwich
38             horizontal-line
39             embedded-ps
40             filledbox
41             round-filled-box
42             text
43             white-text
44             polygon
45             draw-line
46             no-origin))
47
48
49 (use-modules (guile)
50              (ice-9 regex)
51              (srfi srfi-1)
52              (srfi srfi-13)
53              (scm framework-ps)
54              (lily))
55
56
57 ;;(map export
58 ;;   (append (ly:all-stencil-expressions) (ly:all-output-backend-commands)))
59
60 ;; huh?
61 ;;(write (ly:all-output-backend-commands))
62 ;;(write (ly:all-stencil-expressions))
63
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 (offset-add a b)
70   (cons (+ (car a) (car b))
71         (+ (cdr a) (cdr b))))
72
73 (define (ps-encoding text)
74   (escape-parentheses text))
75
76 ;; FIXME: lily-def
77 (define-public (ps-string-def prefix key val)
78   (string-append "/" prefix (symbol->string key) " ("
79                  (escape-parentheses val)
80                  ") def\n"))
81
82
83 (define (ps-number-def prefix key val)
84   (let ((s (if (integer? val)
85                (ly:number->string val)
86                (ly:number->string (exact->inexact val)))))
87     (string-append "/" prefix (symbol->string key) " " s " def\n")))
88
89
90 ;;;
91 ;;; Lily output interface, PostScript implementation --- cleanup and docme
92 ;;;
93
94 ;;; Output-interface functions
95 (define (beam width slope thick blot)
96   (string-append
97    (ly:numbers->string (list slope width thick blot)) " draw_beam" ))
98
99 ;; two beziers
100 (define (bezier-sandwich lst thick)
101   (string-append 
102    (string-join (map ly:number-pair->string lst) " ")
103    " "
104    (ly:number->string thick)
105    " draw_bezier_sandwich"))
106
107 (define (bracket arch_angle arch_width arch_height  height arch_thick thick)
108   (string-append
109    (ly:numbers->string
110     (list arch_angle arch_width arch_height height arch_thick thick))
111    " draw_bracket"))
112
113 (define (char font i)
114   (string-append 
115    (ps-font-command font) " setfont " 
116    "(\\" (ly:inexact->string i 8) ") show" ))
117
118 (define (named-glyph font glyph)
119   (string-append 
120    (ps-font-command font) " setfont " 
121    "/" glyph " glyphshow "))
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 dash l)
135   (string-append 
136    (string-join (map ly:number-pair->string l) " ")
137    " "
138    (ly:number->string thick) 
139    " [ "
140    (ly:number->string dash)
141    " "
142    ;;UGH.  10 ?
143    (ly:number->string (* 10 thick))
144    " ] 0 draw_dashed_slur"))
145
146                                         ; todo: merge with tex-font-command?
147
148 (define (embedded-ps string)
149   string)
150
151 (define (dot x y radius)
152   (string-append
153    " "
154    (ly:numbers->string
155     (list x y radius)) " draw_dot"))
156
157 (define (white-dot x y radius)
158   (string-append
159    " "
160    (ly:numbers->string
161     (list x y radius)) " draw_white_dot"))
162
163 (define (draw-line thick x1 y1 x2 y2)
164   (string-append 
165    "1 setlinecap 1 setlinejoin "
166    (ly:number->string thick) " setlinewidth "
167    (ly:number->string x1) " "
168    (ly:number->string y1) " moveto "
169    (ly:number->string x2) " "
170    (ly:number->string y2) " lineto stroke"))
171
172 (define (ez-ball ch letter-col ball-col)
173   (string-append
174    " (" ch ") "
175    (ly:numbers->string (list letter-col ball-col))
176    " /Helvetica-Bold " ;; ugh
177    " draw_ez_ball"))
178
179 (define (filledbox breapth width depth height) ; FIXME : use draw_round_box
180   (string-append (ly:numbers->string (list breapth width depth height))
181                  " draw_box"))
182
183 ;; WTF is this in every backend?
184 (define (horizontal-line x1 x2 th)
185   (draw-line th x1 0 x2 0))
186
187 (define (lily-def key val)
188   (let ((prefix "lilypondlayout"))
189     (if (string=?
190          (substring key 0 (min (string-length prefix) (string-length key)))
191          prefix)
192         (string-append "/" key " {" val "} bind def\n")
193         (string-append "/" key " (" val ") def\n"))))
194
195
196 (define (placebox x y s) 
197   (string-append 
198    (ly:number->string x) " " (ly:number->string y) " { " s " } place-box\n"))
199
200 (define (polygon points blotdiameter)
201   (string-append
202    (ly:numbers->string points) " "
203    (ly:number->string (/ (length points) 2)) " "
204    (ly:number->string blotdiameter)
205    " draw_polygon"))
206
207 (define (repeat-slash wid slope thick)
208   (string-append
209    (ly:numbers->string (list wid slope thick))
210    " draw_repeat_slash"))
211
212 (define (round-filled-box x y width height blotdiam)
213   (string-append
214    (ly:numbers->string
215     (list x y width height blotdiam)) " draw_round_box"))
216
217 (define (old-text font s)
218   ;; ugh, we should find a better way to
219   ;; extract the hsbw for /space from the font.
220   (let* ((space-length (cdar (ly:text-dimension font " "))) 
221          (commands '())
222          (add-command (lambda (x) (set! commands (cons x commands)))) )
223
224     (string-fold
225      (lambda (chr word)
226        "Translate space as into moveto, group the rest in words."
227        (if (and (< 0 (string-length word))
228                 (equal? #\space  chr))
229            (add-command 
230             (string-append "(" (ps-encoding word) ") show\n")))
231
232        (if (equal? #\space chr)
233            (add-command  (string-append (number->string space-length) " 0.0 rmoveto ")) )
234        
235        (if (equal? #\space chr)
236            ""
237            (string-append word (make-string 1 chr))))
238      ""
239      (string-append s " "))
240
241     (string-append
242      (ps-font-command font) " setfont "
243      (string-join (reverse commands)))))
244
245 (define (new-text font s)
246   (let* ((space-length (cdar (ly:text-dimension font " ")))
247          (space-move (string-append (number->string space-length) " 0.0 rmoveto "))
248          
249          (input-enc (assoc-get 'input-name
250                                (ly:font-encoding-alist font)
251                                'latin1))
252          (out-vec (decode-byte-string input-enc s)))
253
254
255     (string-append
256      (ps-font-command font) " setfont "
257      (string-join
258       (vector->list
259        (vector-for-each
260         
261         (lambda (sym)
262           (if (eq? sym 'space)
263               space-move
264               (string-append "/" (symbol->string sym) " glyphshow")))
265         out-vec))))))
266
267                                         ;(define text old-text)
268 (define text new-text)
269
270 (define (white-text scale s)
271   (let ((mystring (string-append "(" s  ") " (number->string scale)   " /Helvetica-Bold "
272                                  " draw_white_text")))
273     mystring))
274
275 (define (unknown) 
276   "\n unknown\n")
277
278 (define (zigzag-line centre? zzw zzh thick dx dy)
279   (string-append
280    (if centre? "true" "false") " "
281    (ly:number->string zzw) " "
282    (ly:number->string zzh) " "
283    (ly:number->string thick) " "
284    "0 0 "
285    (ly:number->string dx) " "
286    (ly:number->string dy)
287    " draw_zigzag_line"))
288
289
290 (define (grob-cause grob)
291   "")
292
293 (define (no-origin)
294   "")
295
296 (define-public (glyph-string psname items)
297   (apply
298    string-append
299    (cons
300     (format " /~a findfont setfont " psname)
301     (map (lambda  (item)
302            (format " ~a ~a rmoveto /~a glyphshow "
303                    (car item)
304                    (cadr item)
305                    (caddr item)))
306          items))))