]> git.donarmstrong.com Git - lilypond.git/blob - scm/output-ps.scm
(ps-font-command): fix parmesan coding too.
[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
158              (or
159               (equal? (substring coding-command 0 4) "feta")
160               (equal? (substring coding-command 0 8) "parmesan")
161
162              ))
163         (set! coding-command #f))
164
165     (string-append
166      "magfont" (string-encode-integer (hashq  name 1000000))
167      "m" (string-encode-integer (inexact->exact (round (* 1000 magnify))))
168      (if (not coding-command) "" (string-append "e" coding-command)))))
169
170 (define (define-origin file line col) "")
171
172 (define (dot x y radius)
173   (string-append
174    " "
175    (ly:numbers->string
176     (list x y radius)) " draw_dot"))
177
178 (define (draw-line thick x1 y1 x2 y2)
179   (string-append 
180    "1 setlinecap 1 setlinejoin "
181    (ly:number->string thick) " setlinewidth "
182    (ly:number->string x1) " "
183    (ly:number->string y1) " moveto "
184    (ly:number->string x2) " "
185    (ly:number->string y2) " lineto stroke"))
186
187 (define (end-output)
188   "\nend-lilypond-output\n")
189
190 (define (ez-ball ch letter-col ball-col)
191   (string-append
192    " (" ch ") "
193    (ly:numbers->string (list letter-col ball-col))
194    " /Helvetica-Bold " ;; ugh
195    " draw_ez_ball"))
196
197 (define (filledbox breapth width depth height) ; FIXME : use draw_round_box
198   (string-append (ly:numbers->string (list breapth width depth height))
199                  " draw_box"))
200
201 ;; WTF is this in every backend?
202 (define (horizontal-line x1 x2 th)
203   (draw-line th x1 0 x2 0))
204
205 (define (lily-def key val)
206   (let ((prefix "lilypondpaper"))
207     (if (string=?
208          (substring key 0 (min (string-length prefix) (string-length key)))
209          prefix)
210         (string-append "/" key " {" val "} bind def\n")
211         (string-append "/" key " (" val ") def\n"))))
212
213 (define (no-origin) "")
214
215
216
217 (define (placebox x y s) 
218   (string-append 
219    (ly:number->string x) " " (ly:number->string y) " { " s " } place-box\n"))
220
221 (define (polygon points blotdiameter)
222   (string-append
223    (ly:numbers->string points) " "
224    (ly:number->string (/ (length points) 2)) " "
225    (ly:number->string blotdiameter)
226    " draw_polygon"))
227
228 (define (repeat-slash wid slope thick)
229   (string-append
230    (ly:numbers->string (list wid slope thick))
231    " draw_repeat_slash"))
232
233 (define (round-filled-box x y width height blotdiam)
234    (string-append
235     (ly:numbers->string
236      (list x y width height blotdiam)) " draw_round_box"))
237
238
239 (define (stem breapth width depth height) ; FIXME: use draw_round_box.
240   (string-append
241    (ly:numbers->string (list breapth width depth height))
242    " draw_box" ))
243
244 (define (symmetric-x-triangle thick w h)
245   (string-append
246    (ly:numbers->string (list h w thick))
247    " draw_symmetric_x_triangle"))
248
249 (define (text font s)
250   (let*
251       
252       (
253        ;; ugh, we should find a better way to
254        ;; extract the hsbw for /space from the font.
255        
256        (space-length (cdar (ly:text-dimension font "t"))) 
257        (commands '())
258        (add-command (lambda (x) (set! commands (cons x commands)))) )
259
260     (string-fold
261      (lambda (chr word)
262        "Translate space as into moveto, group the rest in words."
263        (if (and (< 0 (string-length word))
264                 (equal? #\space  chr))
265            (add-command 
266             (string-append "(" (ps-encoding word) ") show\n")))
267
268        (if (equal? #\space  chr)
269            (add-command  (string-append (number->string space-length) " 0.0 rmoveto ")) )
270        
271        (if (equal? #\space  chr)
272            ""
273            (string-append word (make-string 1 chr))))
274      ""
275      (string-append  s " "))
276
277     (string-append
278      (ps-font-command font) " setfont "
279      (string-join (reverse commands)))
280     ))
281   
282 (define (unknown) 
283   "\n unknown\n")
284
285 (define (zigzag-line centre? zzw zzh thick dx dy)
286   (string-append
287     (if centre? "true" "false") " "
288     (ly:number->string zzw) " "
289     (ly:number->string zzh) " "
290     (ly:number->string thick) " "
291     "0 0 "
292     (ly:number->string dx) " "
293     (ly:number->string dy)
294     " draw_zigzag_line"))