]> git.donarmstrong.com Git - lilypond.git/blob - scm/output-ps.scm
* lily/paper-book.cc (classic_output):
[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 ;; FIXME -- now that we can have ENCODING == #f, this can be
151 ;; simplified, esp OVERRIDE-CODING-COMMAND
152 (define (font-command font . override-coding-command)
153   (let* ((name (ly:font-filename font))
154          (magnify (ly:font-magnification font))
155          (coding-alist (ly:font-encoding-alist font))
156          (input-encoding (assoc-get 'input-name coding-alist))
157          (font-encoding (assoc-get 'output-name coding-alist))
158          (coding-command (if (not (null? override-coding-command))
159                              (car override-coding-command)
160                              (if font-encoding
161                                  (get-coding-command font-encoding)
162                                  #f))))
163
164     (string-append
165      "magfont" (string-encode-integer (hashq  name 1000000))
166      "m" (string-encode-integer (inexact->exact (round (* 1000 magnify))))
167      (if (or (not font-encoding) (equal? input-encoding font-encoding)) ""
168          (string-append "e" coding-command)))))
169
170 (define (define-fonts paper font-list)
171   
172   (define (define-font command fontname scaling)
173     (string-append
174      "/" command " { /" fontname " findfont "
175      (ly:number->string scaling) " output-scale div scalefont } bind def\n"))
176
177   (define (reencode-font plain encoding command)
178     (let ((coding-vector (get-coding-command encoding)))
179       (string-append
180        plain " " coding-vector " /" command " reencode-font\n"
181        "/" command "{ /" command " findfont 1 scalefont } bind def\n")))
182   
183   (define (guess-ps-fontname basename)
184     "We do not have the FontName, try to guess is from basename."
185     (cond
186      ((tex-font? basename)
187       ;; FIXME: we need proper Fontmap for the bluesky CM*, EC* fonts.
188       ;; Only the fonts that we trace in mf/ are in our own FontMap.
189       (string-append basename ".pfb"))
190      (else (string-append basename ".pfa"))
191      ))
192
193   (define (font-load-command paper font)
194     (let* ((specced-font-name (ly:font-name font))
195            (fontname (if specced-font-name
196                          specced-font-name
197                          (guess-ps-fontname (ly:font-filename font))))
198            
199            (coding-alist (ly:font-encoding-alist font))
200            (input-encoding (assoc-get 'input-name coding-alist))
201            (font-encoding (assoc-get 'output-name coding-alist))
202            (command (font-command font))
203            ;; FIXME -- see (font-command )
204            (plain (if font-encoding (get-coding-command font-encoding)
205                       command))
206            (designsize (ly:font-design-size font))
207            (magnification (* (ly:font-magnification font)))
208            (ops (ly:paper-lookup paper 'outputscale))
209            (scaling (* ops magnification designsize)))
210
211       (string-append
212        (define-font plain fontname scaling)
213        (if (or (equal? input-encoding font-encoding)
214                ;; guh
215                (equal? font-encoding "fetaBraces")
216                (equal? font-encoding "fetaNumber")
217                (equal? font-encoding "fetaMusic")
218                (equal? font-encoding "parmesanMusic"))
219                ""
220            (reencode-font plain input-encoding command)))))
221   
222   (define (font-load-encoding encoding)
223     (let ((filename (get-coding-filename encoding)))
224       (ly:kpathsea-gulp-file filename)))
225
226   (let* ((encoding-list (map (lambda (x)
227                                (assoc-get 'input-name
228                                           (ly:font-encoding-alist x)))
229                              font-list))
230          (encodings (uniq-list (sort-list (filter string? encoding-list)
231                                           string<?))))
232
233     (string-append
234      (apply string-append (map font-load-encoding encodings))
235      (apply string-append
236             (map (lambda (x) (font-load-command paper x)) font-list)))))
237
238 (define (define-origin file line col) "")
239
240 (define (dot x y radius)
241   (string-append
242    " "
243    (ly:numbers->string
244     (list x y radius)) " draw_dot"))
245
246 (define (draw-line thick x1 y1 x2 y2)
247   (string-append 
248    "1 setlinecap 1 setlinejoin "
249    (ly:number->string thick) " setlinewidth "
250    (ly:number->string x1) " "
251    (ly:number->string y1) " moveto "
252    (ly:number->string x2) " "
253    (ly:number->string y2) " lineto stroke"))
254
255 (define (end-output)
256   "\nend-lilypond-output\n")
257
258 (define end-output-classic end-output)
259
260 (define (ez-ball ch letter-col ball-col)
261   (string-append
262    " (" ch ") "
263    (ly:numbers->string (list letter-col ball-col))
264    " /Helvetica-Bold " ;; ugh
265    " draw_ez_ball"))
266
267 (define (filledbox breapth width depth height) 
268   (string-append (ly:numbers->string (list breapth width depth height))
269                  " draw_box"))
270
271 (define (header creator time-stamp page-count-)
272   (set! page-count page-count-)
273   (set! page-number 0)
274   (string-append
275    "%!PS-Adobe-3.0\n"
276    "%%Creator: " creator " " time-stamp "\n"
277    "%%Pages: " (number->string page-count) "\n"
278    "%%PageOrder: Ascend\n"
279    ;; FIXME: TODO get from paper
280    ;; "%%DocumentPaperSizes: a6\n"
281    ;;(string-append "GNU LilyPond (" (lilypond-version) "), ")
282    ;;      (strftime "%c" (localtime (current-time))))
283    ;; FIXME: duplicated in every backend
284    (ps-string-def
285     "lilypond" 'tagline
286     (string-append "Engraved by LilyPond (version " (lilypond-version) ")"))))
287
288 (define (header-end)
289   (string-append
290    (ly:gulp-file "lilyponddefs.ps")
291    (ly:gulp-file "music-drawing-routines.ps")))
292
293 (define (horizontal-line x1 x2 th)
294   (draw-line th x1  0 x2 0))
295
296 (define (lily-def key val)
297   (let ((prefix "lilypondpaper"))
298     (if (string=?
299          (substring key 0 (min (string-length prefix) (string-length key)))
300          prefix)
301         (string-append "/" key " {" val "} bind def\n")
302         (string-append "/" key " (" val ") def\n"))))
303
304 (define (no-origin) "")
305
306 ;; FIXME: duplictates output-scopes, duplicated in other backends
307 ;; FIXME: silly interface name
308 (define (output-paper-def pd)
309   (let ((prefix "lilypondpaper"))
310     
311     (define (scope-entry->string key var)
312       (if (variable-bound? var)
313           (let ((val (variable-ref var)))
314             (cond
315              ((string? val) (ps-string-def prefix key val))
316              ((number? val) (ps-number-def prefix key val))
317              (else "")))
318           ""))
319       
320     (apply
321      string-append
322      (module-map scope-entry->string (ly:output-def-scope pd)))))
323
324 ;; FIXME: duplicated in other output backends
325 ;; FIXME: silly interface name
326 (define (output-scopes paper scopes fields basename)
327   (let ((prefix "lilypond"))
328
329     ;; FIXME: duplicates output-paper's scope-entry->string, mostly
330     (define (scope-entry->string key var)
331       (if (variable-bound? var)
332           (let ((val (variable-ref var)))
333             (if (and (memq key fields) (string? val))
334                 (header-to-file basename key val))
335             (cond
336              ((string? val) (ps-string-def prefix key val))
337              ((number? val) (ps-number-def prefix key val))
338              (else "")))
339           ""))
340     
341     (define (output-scope scope)
342       (apply string-append (module-map scope-entry->string scope)))
343
344     (string-append (apply string-append (map output-scope scopes)))))
345
346 ;; hmm, looks like recursing call is always last statement, does guile
347 ;; think so too?
348 (define (output-stencil port expr offset)
349   (if (pair? expr)
350       (let ((head (car expr)))
351         (cond
352          ((ly:input-location? head)
353           (display (apply define-origin (ly:input-location head)) port)
354           (output-stencil port (cadr expr) offset))
355          ((eq? head 'no-origin)
356           (display (expression->string head) port)
357           (output-stencil port (cadr expr) offset))
358          ((eq? head 'translate-stencil)
359           (output-stencil port (caddr expr) (offset-add offset (cadr expr))))
360          ((eq? head 'combine-stencil)
361           (output-stencil port (cadr expr) offset)
362           (output-stencil port (caddr expr) offset))
363          (else
364           (display (placebox (car offset) (cdr offset)
365                              (expression->string expr)) port))))))
366
367 (define (placebox x y s) 
368   (string-append 
369    (ly:number->string x) " " (ly:number->string y) " { " s " } place-box\n"))
370
371 (define (polygon points blotdiameter)
372   (string-append
373    (ly:numbers->string points) " "
374    (ly:number->string (/ (length points) 2)) " "
375    (ly:number->string blotdiameter)
376    " draw_polygon"))
377
378 (define (repeat-slash wid slope thick)
379   (string-append
380    (ly:numbers->string (list wid slope thick))
381    " draw_repeat_slash"))
382
383 (define (round-filled-box x y width height blotdiam)
384    (string-append
385     (ly:numbers->string
386      (list x y width height blotdiam)) " draw_round_box"))
387
388 (define (start-system origin dim)
389   (string-append
390    "\n" (ly:number-pair->string origin) " start-system\n"
391    "{\n"
392    "set-ps-scale-to-lily-scale\n"))
393
394 (define (stem breapth width depth height) 
395   (string-append
396    (ly:numbers->string (list breapth width depth height))
397    " draw_box" ))
398
399 (define (stop-system last?)
400   "} stop-system\n")
401
402 (define (symmetric-x-triangle thick w h)
403   (string-append
404    (ly:numbers->string (list h w thick))
405    " draw_symmetric_x_triangle"))
406
407 (define (text font s)
408   (string-append (font-command font) " setfont "
409                  "(" (ps-encoding s) ") show"))
410
411 (define (unknown) 
412   "\n unknown\n")
413
414 (define (zigzag-line centre? zzw zzh thick dx dy)
415   (string-append
416     (if centre? "true" "false") " "
417     (ly:number->string zzw) " "
418     (ly:number->string zzh) " "
419     (ly:number->string thick) " "
420     "0 0 "
421     (ly:number->string dx) " "
422     (ly:number->string dy)
423     " draw_zigzag_line"))
424
425 (define (start-page)
426   (set! page-number (+ page-number 1))
427   (string-append
428    "%%Page: " (number->string page-number) " " (number->string page-count) "\n"
429   "start-page\n"))
430
431 (define (stop-page last?)
432   (if last?
433       "\nstop-last-page\n"
434       "\nstop-page\n"))