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