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