]> git.donarmstrong.com Git - lilypond.git/blob - scm/output-ps.scm
* Documentation/user/changing-defaults.itely (Page layout): add
[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 (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 lst)
130   (string-append 
131    (string-join (map ly:number-pair->string lst) " ")
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 (define (dot x y radius)
142   (string-append
143    " "
144    (ly:numbers->string
145     (list x y radius)) " draw_dot"))
146
147 (define (draw-line thick x1 y1 x2 y2)
148   (string-append 
149    "1 setlinecap 1 setlinejoin "
150    (ly:number->string thick) " setlinewidth "
151    (ly:number->string x1) " "
152    (ly:number->string y1) " moveto "
153    (ly:number->string x2) " "
154    (ly:number->string y2) " lineto stroke"))
155
156 (define (embedded-ps string)
157   string)
158
159 ;; FIXME. 
160 (define (ez-ball ch letter-col ball-col)
161   (string-append
162    " (" ch ") "
163    (ly:numbers->string (list letter-col ball-col))
164    ;; FIXME: barf
165    " /Helvetica-Bold "
166    " draw_ez_ball"))
167
168 ;; FIXME: use draw_round_box
169 (define (filledbox breapth width depth height)
170   (string-append (ly:numbers->string (list breapth width depth height))
171                  " draw_box"))
172
173
174 (define (utf8-string pango-font-description string)
175   (ly:warn "utf8-string encountered in PS backend"))
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 " g)
194                 (format #f " ~a ~a rmoveto /~a glyphshow "
195                         x y g))))
196         x-y-named-glyphs))
197   ))
198
199 (define (grob-cause grob)
200   "")
201
202 ;; WTF is this in every backend?
203 (define (horizontal-line x1 x2 th)
204   (draw-line th x1 0 x2 0))
205
206 (define (lily-def key val)
207   (let ((prefix "lilypondlayout"))
208     (if (string=?
209          (substring key 0 (min (string-length prefix) (string-length key)))
210          prefix)
211         (string-append "/" key " {" val "} bind def\n")
212         (string-append "/" key " (" val ") def\n"))))
213
214 (define (named-glyph font glyph)
215   (string-append 
216    (ps-font-command font) " setfont " 
217    "/" glyph " glyphshow "))
218
219 (define (no-origin)
220   "")
221
222 (define (placebox x y s) 
223   (string-append 
224    (ly:number->string x) " " (ly:number->string y) " { " s " } place-box\n"))
225
226 (define (polygon points blotdiameter)
227   (string-append
228    (ly:numbers->string points) " "
229    (ly:number->string (/ (length points) 2)) " "
230    (ly:number->string blotdiameter)
231    " draw_polygon"))
232
233 (define (repeat-slash wid slope thick)
234   (string-append
235    (ly:numbers->string (list wid slope thick))
236    " draw_repeat_slash"))
237
238 (define (round-filled-box x y width height blotdiam)
239   (string-append
240    (ly:numbers->string
241     (list x y width height blotdiam)) " draw_round_box"))
242
243 (define (text font s)
244 ;  (ly:warn "TEXT backend-command encountered in Pango backend\nargs: ~a ~a" font str)
245   
246   (let* ((space-length (cdar (ly:text-dimension font " ")))
247          (space-move (string-append (number->string space-length)
248                                     " 0.0 rmoveto "))
249          (input-enc "latin1")
250          (out-vec (decode-byte-string input-enc s)))
251
252     (string-append
253      (ps-font-command font) " setfont "
254      (string-join
255       (vector->list
256        (vector-for-each
257         
258         (lambda (sym)
259           (if (eq? sym 'space)
260               space-move
261               (string-append "/" (symbol->string sym) " glyphshow")))
262         out-vec))))))
263
264 ;; FIXME: BARF helvetica?
265 (define (white-text scale s)
266   (let ((mystring (string-append
267                    "(" s  ") " (number->string scale)
268                    " /Helvetica-Bold "
269                    " draw_white_text")))
270     mystring
271
272
273     ;; FIXME: broken with user install of GS 8.x
274     ""
275     ))
276
277 (define (unknown) 
278   "\n unknown\n")
279
280 (define (white-dot x y radius)
281   (string-append
282    " "
283    (ly:numbers->string
284     (list x y radius)) " draw_white_dot"))
285
286 (define (zigzag-line centre? zzw zzh thick dx dy)
287   (string-append
288    (if centre? "true" "false") " "
289    (ly:number->string zzw) " "
290    (ly:number->string zzh) " "
291    (ly:number->string thick) " "
292    "0 0 "
293    (ly:number->string dx) " "
294    (ly:number->string dy)
295    " draw_zigzag_line"))