]> git.donarmstrong.com Git - lilypond.git/blob - scm/output-ps.scm
4cc714fdecf34ac05ae58d18c255ebed76b10f1b
[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              output-paper-def
23              output-scopes
24              select-font
25              blank
26              dot
27              beam
28              bracket
29              dashed-slur
30              char
31              dashed-line
32              zigzag-line
33              symmetric-x-triangle
34              ez-ball
35              comment
36              end-output
37              experimental-on
38              repeat-slash
39              header-end
40              header
41              placebox
42              bezier-sandwich
43              start-system
44              stop-system
45              stop-last-system
46              horizontal-line
47              filledbox
48              round-filled-box
49              text
50              tuplet
51              polygon
52              draw-line
53              define-origin
54              no-origin
55              start-page
56              stop-page
57              ))
58
59 (use-modules (guile)
60              (ice-9 regex)
61              (srfi srfi-1)
62              (srfi srfi-13)
63              (lily))
64
65 ;;; Global vars
66 (define page-count 0)
67 (define page-number 0)
68
69 ;;; helper functions, not part of output interface
70 (define (escape-parentheses s)
71   (regexp-substitute/global #f "(^|[^\\])([\\(\\)])" s 'pre 1 "\\" 2 'post))
72
73 (define (offset-add a b)
74   (cons (+ (car a) (car b))
75         (+ (cdr a) (cdr b))))
76
77 (define (ps-encoding text)
78   (escape-parentheses text))
79
80 ;; FIXME: lily-def
81 (define (ps-string-def prefix key val)
82   (string-append "/" prefix (symbol->string key) " ("
83                  (escape-parentheses val)
84                  ") def\n"))
85
86 (define (ps-number-def prefix key val)
87   (let ((s (if (integer? val)
88                (ly:number->string val)
89                (ly:number->string (exact->inexact val)))))
90     (string-append "/" prefix (symbol->string key) " " s " def\n")))
91
92 (define (tex-font? fontname)
93   (equal? (substring fontname 0 2) "cm"))
94
95
96 ;;;
97 ;;; Lily output interface, PostScript implementation --- cleanup and docme
98 ;;;
99
100 ;;; Output-interface functions
101 (define (beam width slope thick blot)
102   (string-append
103    (ly:numbers->string (list slope width thick blot)) " draw_beam" ))
104
105 ;; two beziers
106 (define (bezier-sandwich l thick)
107   (string-append 
108    (string-join (map ly:number-pair->string l) " ")
109    " "
110    (ly:number->string thick)
111    " draw_bezier_sandwich"))
112
113 (define (bracket arch_angle arch_width arch_height  height arch_thick thick)
114   (string-append
115    (ly:numbers->string
116     (list arch_angle arch_width arch_height height arch_thick thick))
117    " draw_bracket"))
118
119 (define (char font i)
120   (string-append 
121     (font-command font) " setfont " 
122    "(\\" (ly:inexact->string i 8) ") show" ))
123
124 (define (comment s)
125   (string-append "% " s "\n"))
126
127 (define (dashed-line thick on off dx dy)
128   (string-append 
129    (ly:number->string dx) " "
130    (ly:number->string dy) " "
131    (ly:number->string thick)
132    " [ "
133    (ly:number->string on) " "
134    (ly:number->string off)
135    " ] 0 draw_dashed_line"))
136
137 ;; what the heck is this interface ?
138 (define (dashed-slur thick dash l)
139   (string-append 
140    (string-join (map ly:number-pair->string l) " ")
141    " "
142    (ly:number->string thick) 
143    " [ "
144    (ly:number->string dash)
145    " "
146    ;;UGH.  10 ?
147    (ly:number->string (* 10 thick))
148    " ] 0 draw_dashed_slur"))
149
150 (define (font-command font . override-coding)
151   (let* ((name (ly:font-filename font))
152          (magnify (ly:font-magnification font))
153          (coding-alist (ly:font-encoding-alist font))
154          (input-encoding (assoc-get 'input-name coding-alist))
155          (font-encoding (assoc-get 'output-name coding-alist))
156          (coding-command (if (null? override-coding)
157                              (if (equal? input-encoding font-encoding)
158                                  #f font-encoding)
159                              (car override-coding))))
160
161     ;; FIXME:  now feta stuff has feta* input-encoding (again?)
162     ;;(format (current-error-port) "FONT: ~S, ~S\n" name font-encoding)
163     ;;(format (current-error-port) "INPUT: ~S\n" input-encoding)
164     (if (and coding-command (equal? (substring coding-command 0 4) "feta"))
165         (set! coding-command #f))
166
167     (string-append
168      "magfont" (string-encode-integer (hashq  name 1000000))
169      "m" (string-encode-integer (inexact->exact (round (* 1000 magnify))))
170      (if (not coding-command) "" (string-append "e" coding-command)))))
171
172 (define (define-fonts paper font-list)
173   
174   (define (define-font command fontname scaling)
175     (string-append
176      "/" command " { /" fontname " findfont "
177      (ly:number->string scaling) " output-scale div scalefont } bind def\n"))
178
179   (define (reencode-font plain encoding command)
180     (let ((coding-vector (get-coding-command encoding)))
181       (string-append
182        plain " " coding-vector " /" command " reencode-font\n"
183        "/" command "{ /" command " findfont 1 scalefont } bind def\n")))
184   
185   (define (guess-ps-fontname basename)
186     "We do not have the FontName, try to guess is from basename."
187     (cond
188      ((tex-font? basename)
189       ;; FIXME: we need proper Fontmap for the bluesky CM*, EC* fonts.
190       ;; Only the fonts that we trace in mf/ are in our own FontMap.
191       (string-append basename ".pfb"))
192      (else (string-append basename ".pfa"))
193      ))
194
195   (define (font-load-command paper font)
196     (let* ((specced-font-name (ly:font-name font))
197            (fontname (if specced-font-name
198                          specced-font-name
199                          (guess-ps-fontname (ly:font-filename font))))
200            
201            (coding-alist (ly:font-encoding-alist font))
202            (input-encoding (assoc-get 'input-name coding-alist))
203            (font-encoding (assoc-get 'output-name coding-alist))
204            (command (font-command font))
205            ;; FIXME -- see (font-command )
206            (plain (font-command font #f))
207            (designsize (ly:font-design-size font))
208            (magnification (* (ly:font-magnification font)))
209            (ops (ly:paper-lookup paper 'outputscale))
210            (scaling (* ops magnification designsize)))
211
212       (string-append
213        (define-font plain fontname scaling)
214        (if (or (equal? input-encoding font-encoding)
215                ;; guh
216                (equal? font-encoding "fetaBraces")
217                (equal? font-encoding "fetaNumber")
218                (equal? font-encoding "fetaMusic")
219                (equal? font-encoding "parmesanMusic"))
220                ""
221            (reencode-font plain input-encoding command)))))
222   
223   (define (font-load-encoding encoding)
224     (let ((filename (get-coding-filename encoding)))
225       (ly:kpathsea-gulp-file filename)))
226
227   (let* ((encoding-list (map (lambda (x)
228                                (assoc-get 'input-name
229                                           (ly:font-encoding-alist x)))
230                              font-list))
231          (encodings (uniq-list (sort-list (filter string? encoding-list)
232                                           string<?))))
233
234     (string-append
235      (apply string-append (map font-load-encoding encodings))
236      (apply string-append
237             (map (lambda (x) (font-load-command paper x)) font-list)))))
238
239 (define (define-origin file line col) "")
240
241 (define (dot x y radius)
242   (string-append
243    " "
244    (ly:numbers->string
245     (list x y radius)) " draw_dot"))
246
247 (define (draw-line thick x1 y1 x2 y2)
248   (string-append 
249    "1 setlinecap 1 setlinejoin "
250    (ly:number->string thick) " setlinewidth "
251    (ly:number->string x1) " "
252    (ly:number->string y1) " moveto "
253    (ly:number->string x2) " "
254    (ly:number->string y2) " lineto stroke"))
255
256 (define (end-output)
257   "\nend-lilypond-output\n")
258
259 (define (ez-ball ch letter-col ball-col)
260   (string-append
261    " (" ch ") "
262    (ly:numbers->string (list letter-col ball-col))
263    " /Helvetica-Bold " ;; ugh
264    " draw_ez_ball"))
265
266 (define (filledbox breapth width depth height) ; FIXME : use draw_round_box
267   (string-append (ly:numbers->string (list breapth width depth height))
268                  " draw_box"))
269
270 (define (header creator time-stamp paper page-count- classic?)
271   (set! page-count page-count-)
272   (set! page-number 0)
273   (string-append
274    "%!PS-Adobe-3.0\n"
275    "%%Creator: " creator " " time-stamp "\n"
276    "%%Pages: " (number->string page-count) "\n"
277    "%%PageOrder: Ascend\n"
278    "%%DocumentPaperSizes: " (ly:paper-lookup paper 'papersize) "\n"
279    ;;(string-append "GNU LilyPond (" (lilypond-version) "), ")
280    ;;      (strftime "%c" (localtime (current-time))))
281    ;; FIXME: duplicated in every backend
282    (ps-string-def
283     "lilypond" 'tagline
284     (string-append "Engraved by LilyPond (version " (lilypond-version) ")"))))
285
286 (define (header-end)
287   (string-append
288    (ly:gulp-file "lilyponddefs.ps")
289    (ly:gulp-file "music-drawing-routines.ps")))
290
291 ;; WTF is this in every backend?
292 (define (horizontal-line x1 x2 th)
293   (draw-line th x1 0 x2 0))
294
295 (define (lily-def key val)
296   (let ((prefix "lilypondpaper"))
297     (if (string=?
298          (substring key 0 (min (string-length prefix) (string-length key)))
299          prefix)
300         (string-append "/" key " {" val "} bind def\n")
301         (string-append "/" key " (" val ") def\n"))))
302
303 (define (no-origin) "")
304
305 ;; FIXME: duplictates output-scopes, duplicated in other backends
306 ;; FIXME: silly interface name
307 (define (output-paper-def pd)
308   (let ((prefix "lilypondpaper"))
309     
310     (define (scope-entry->string key var)
311       (if (variable-bound? var)
312           (let ((val (variable-ref var)))
313             (cond
314              ((string? val) (ps-string-def prefix key val))
315              ((number? val) (ps-number-def prefix key val))
316              (else "")))
317           ""))
318       
319     (apply
320      string-append
321      (module-map scope-entry->string (ly:output-def-scope pd)))))
322
323 ;; FIXME: duplicated in other output backends
324 ;; FIXME: silly interface name
325 (define (output-scopes paper scopes fields basename)
326   (let ((prefix "lilypond"))
327
328     ;; FIXME: duplicates output-paper's scope-entry->string, mostly
329     (define (scope-entry->string key var)
330       (if (variable-bound? var)
331           (let ((val (variable-ref var)))
332             (if (and (memq key fields) (string? val))
333                 (header-to-file basename key val))
334             (cond
335              ((string? val) (ps-string-def prefix key val))
336              ((number? val) (ps-number-def prefix key val))
337              (else "")))
338           ""))
339     
340     (define (output-scope scope)
341       (apply string-append (module-map scope-entry->string scope)))
342
343     (string-append (apply string-append (map output-scope scopes)))))
344
345 (define (placebox x y s) 
346   (string-append 
347    (ly:number->string x) " " (ly:number->string y) " { " s " } place-box\n"))
348
349 (define (polygon points blotdiameter)
350   (string-append
351    (ly:numbers->string points) " "
352    (ly:number->string (/ (length points) 2)) " "
353    (ly:number->string blotdiameter)
354    " draw_polygon"))
355
356 (define (repeat-slash wid slope thick)
357   (string-append
358    (ly:numbers->string (list wid slope thick))
359    " draw_repeat_slash"))
360
361 (define (round-filled-box x y width height blotdiam)
362    (string-append
363     (ly:numbers->string
364      (list x y width height blotdiam)) " draw_round_box"))
365
366 (define (start-system origin dim)
367   (string-append
368    "\n" (ly:number-pair->string origin) " start-system\n"
369    "{\n"
370    "set-ps-scale-to-lily-scale\n"))
371
372 (define (stem breapth width depth height) ; FIXME: use draw_round_box.
373   (string-append
374    (ly:numbers->string (list breapth width depth height))
375    " draw_box" ))
376
377 (define (stop-system last?)
378   "} stop-system\n")
379
380 (define (symmetric-x-triangle thick w h)
381   (string-append
382    (ly:numbers->string (list h w thick))
383    " draw_symmetric_x_triangle"))
384
385 (define (text font s)
386   (let*
387       
388       (
389        ;; ugh, we should find a better way to
390        ;; extract the hsbw for /space from the font.
391        
392        (space-length (cdar (ly:text-dimension font "t"))) 
393        (commands '())
394        (add-command (lambda (x) (set! commands (cons x commands)))) )
395
396     (string-fold
397      (lambda (chr word)
398        "Translate space as into moveto, group the rest in words."
399        (if (and (< 0 (string-length word))
400                 (equal? #\space  chr))
401            (add-command 
402             (string-append "(" (ps-encoding word) ") show\n")))
403
404        (if (equal? #\space  chr)
405            (add-command  (string-append (number->string space-length) " 0.0 rmoveto ")) )
406        
407        (if (equal? #\space  chr)
408            ""
409            (string-append word (make-string 1 chr))))
410      ""
411      (string-append  s " "))
412
413     (string-append
414      (font-command font) " setfont "
415      (string-join (reverse commands)))
416     ))
417   
418 (define (unknown) 
419   "\n unknown\n")
420
421 (define (zigzag-line centre? zzw zzh thick dx dy)
422   (string-append
423     (if centre? "true" "false") " "
424     (ly:number->string zzw) " "
425     (ly:number->string zzh) " "
426     (ly:number->string thick) " "
427     "0 0 "
428     (ly:number->string dx) " "
429     (ly:number->string dy)
430     " draw_zigzag_line"))
431
432 (define (start-page)
433   (set! page-number (+ page-number 1))
434   (string-append
435    "%%Page: " (number->string page-number) " " (number->string page-count) "\n"
436   "start-page\n"))
437
438 (define (stop-page last?)
439   (if last?
440       "\nstop-last-page\n"
441       "\nstop-page\n"))