]> git.donarmstrong.com Git - lilypond.git/blob - scm/output-ps.scm
(define-fonts):
[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 ;; WIP
92 (define font-encoding-alist
93   '(("ecrm12" . "ISOLatin1Encoding")
94     ("ecmb12" . "ISOLatin1Encoding")))
95                  
96 (define (ps-encoding text)
97   (escape-parentheses text))
98
99 ;; FIXME: lily-def
100 (define (ps-string-def prefix key val)
101   (string-append "/" prefix (symbol->string key) " ("
102                  (escape-parentheses val)
103                  ") def\n"))
104
105 (define (ps-number-def prefix key val)
106   (let ((s (if (integer? val)
107                (ly:number->string val)
108                (ly:number->string (exact->inexact val)))))
109     (string-append "/" prefix (symbol->string key) " " s " def\n")))
110
111 (define (tex-font? fontname)
112   (equal? (substring fontname 0 2) "cm"))
113
114
115 ;;;
116 ;;; Lily output interface, PostScript implementation --- cleanup and docme
117 ;;;
118
119 ;;; Output-interface functions
120 (define (beam width slope thick blot)
121   (string-append
122    (ly:numbers->string (list slope width thick blot)) " draw_beam" ))
123
124 ;; two beziers
125 (define (bezier-sandwich l thick)
126   (string-append 
127    (string-join (map ly:number-pair->string l) " ")
128    " "
129    (ly:number->string thick)
130    " draw_bezier_sandwich"))
131
132 (define (bracket arch_angle arch_width arch_height  height arch_thick thick)
133   (string-append
134    (ly:numbers->string
135     (list arch_angle arch_width arch_height height arch_thick thick))
136    " draw_bracket"))
137
138 (define (char i)
139   (string-append 
140    "(\\" (ly:inexact->string i 8) ") show" ))
141
142 (define (comment s)
143   (string-append "% " s "\n"))
144
145 (define (dashed-line thick on off dx dy)
146   (string-append 
147    (ly:number->string dx) " "
148    (ly:number->string dy) " "
149    (ly:number->string thick)
150    " [ "
151    (ly:number->string on) " "
152    (ly:number->string off)
153    " ] 0 draw_dashed_line"))
154
155 ;; what the heck is this interface ?
156 (define (dashed-slur thick dash l)
157   (string-append 
158    (string-join (map ly:number-pair->string l) " ")
159    " "
160    (ly:number->string thick) 
161    " [ "
162    (ly:number->string dash)
163    " "
164    ;;UGH.  10 ?
165    (ly:number->string (* 10 thick))
166    " ] 0 draw_dashed_slur"))
167
168 (define (font-command font)
169   (string-append
170    "magfont"
171    (string-encode-integer
172     (hashq (ly:font-name font) 1000000))
173    "m"
174    (string-encode-integer
175     (inexact->exact (round (* 1000 (ly:font-magnification font)))))))
176
177 (define (define-fonts paper font-list)
178
179   (define (fontname->designsize fontname)
180     (let ((i (string-index fontname char-numeric?)))
181       (string->number (substring fontname i))))
182   
183   (define (define-font command fontname scaling)
184     (string-append
185      "/" command " { /" fontname " findfont "
186      ;; FIXME
187      (ly:number->string scaling) " output-scale div scalefont } bind def\n"))
188      ;;(ly:number->string scaling) " scalefont } bind def\n"))
189
190   (define (reencode-font raw encoding command)
191     (string-append
192      raw " " encoding " /" command " reencode-font\n"
193      "/" command "{ /" command " findfont 1 scalefont } bind def\n"))
194           
195   ;; frobnicate NAME to jibe with external definitions.
196   (define (possibly-mangle-fontname fontname)
197     (cond
198      ((tex-font? fontname)
199       ;; FIXME: we need proper Fontmap for CM fonts, like so:
200       ;; /CMR10 (cmr10.pfb); 
201       ;; (string-upcase fontname)
202       (string-append fontname ".pfb"))
203      ((or (equal? (substring fontname 0 4) "feta")
204           (equal? (substring fontname 0 4) "parm"))
205       (regexp-substitute/global
206        #f "(feta|parmesan)([a-z-]*)([0-9]+)"
207        fontname 'pre "GNU-LilyPond-" 1 2 "-" 3 'post))
208      (else fontname)))
209                          
210   (define (font-load-command paper font)
211
212 ;; fontname "feta20"
213 ;; command "magfontGNMWomXVo"
214 ;; mangled "GNU-LilyPond-feta-20"
215 ;; designsize 0.569055118110236
216 ;; foo-design 20
217 ;; magnification 0.569055118110236
218 ;; ops 1.75729901757299
219 ;; scaling 20.0
220
221 ;; fontname "cmr8"
222 ;; command "magfontUIJQomTVo"
223 ;; mangled "cmr8.pfb"
224 ;; designsize 0.564574183197548
225 ;; foo-design 8
226 ;; magnification 0.564574183197548
227 ;; ops 1.75729901757299
228 ;; scaling 7.87450656184296
229     
230     (let* ((command (font-command font))
231            (fontname (ly:font-name font))
232            (mangled (possibly-mangle-fontname fontname))
233            (encoding (assoc-get fontname font-encoding-alist))
234            (designsize (ly:font-design-size font))
235            (magnification (* (ly:font-magnification font)))
236            (foo-design (fontname->designsize fontname))
237            (ops (ly:paper-lookup paper 'outputscale))
238            ;; FIXME this magic is about right ...
239            (scaling (* ops ops magnification designsize foo-design)))
240
241       (if
242        #f
243        (begin
244          (newline)
245          (format (current-error-port) "fontname ~S\n" fontname)
246          (format (current-error-port) "command ~S\n" command)
247          (format (current-error-port) "mangled ~S\n" mangled)
248          (format (current-error-port) "designsize ~S\n" designsize)
249          (format (current-error-port) "foo-design ~S\n" foo-design)
250          (format (current-error-port) "magnification ~S\n" magnification)
251          (format (current-error-port) "ops ~S\n" ops)
252          (format (current-error-port) "scaling ~S\n" scaling)))
253       
254       (if encoding
255           ;; FIXME: should rather tag encoded font
256           (let ((raw (string-append command "-raw")))
257             (string-append
258              (define-font raw mangled scaling)
259              (reencode-font raw encoding command)))
260           (define-font command mangled scaling))))
261   
262   (apply string-append
263          (map (lambda (x) (font-load-command paper x)) font-list)))
264
265 (define (define-origin file line col) "")
266
267 (define (dot x y radius)
268   (string-append
269    " "
270    (ly:numbers->string
271     (list x y radius)) " draw_dot"))
272
273 (define (draw-line thick x1 y1 x2 y2)
274   (string-append 
275    "1 setlinecap 1 setlinejoin "
276    (ly:number->string thick) " setlinewidth "
277    (ly:number->string x1) " "
278    (ly:number->string y1) " moveto "
279    (ly:number->string x2) " "
280    (ly:number->string y2) " lineto stroke"))
281
282 (define (end-output)
283   "\nend-lilypond-output\n")
284
285 (define (ez-ball ch letter-col ball-col)
286   (string-append
287    " (" ch ") "
288    (ly:numbers->string (list letter-col ball-col))
289    " /Helvetica-Bold " ;; ugh
290    " draw_ez_ball"))
291
292 (define (filledbox breapth width depth height) 
293   (string-append (ly:numbers->string (list breapth width depth height))
294                  " draw_box"))
295
296 (define (fontify font exp)
297   (string-append (font-command font) " setfont " exp))
298
299 (define (header creator time-stamp page-count-)
300   (set! page-count page-count-)
301   (set! page-number 0)
302   (string-append
303    "%!PS-Adobe-3.0\n"
304    "%%Creator: " creator " " time-stamp "\n"
305    "%%Pages: " (number->string page-count) "\n"
306    "%%PageOrder: Ascend\n"
307    ;; FIXME: TODO get from paper
308    ;; "%%DocumentPaperSizes: a6\n"
309    ;;(string-append "GNU LilyPond (" (lilypond-version) "), ")
310    ;;      (strftime "%c" (localtime (current-time))))
311    ;; FIXME: duplicated in every backend
312    (ps-string-def
313     "lilypond" 'tagline
314     (string-append "Engraved by LilyPond (version " (lilypond-version) ")"))))
315
316 (define (header-end)
317   (string-append
318    (ly:gulp-file "lilyponddefs.ps")
319    (ly:gulp-file "music-drawing-routines.ps")))
320
321 (define (horizontal-line x1 x2 th)
322   (draw-line th x1  0 x2 0))
323
324 (define (lily-def key val)
325   (let ((prefix "lilypondpaper"))
326     (if (string=?
327          (substring key 0 (min (string-length prefix) (string-length key)))
328          prefix)
329         (string-append "/" key " {" val "} bind def\n")
330         (string-append "/" key " (" val ") def\n"))))
331
332 (define (no-origin) "")
333
334 ;; FIXME: duplictates output-scopes, duplicated in other backends
335 ;; FIXME: silly interface name
336 (define (output-paper-def pd)
337   (let ((prefix "lilypondpaper"))
338     
339     (define (scope-entry->string key var)
340       (if (variable-bound? var)
341           (let ((val (variable-ref var)))
342             (cond
343              ((string? val) (ps-string-def prefix key val))
344              ((number? val) (ps-number-def prefix key val))
345              (else "")))
346           ""))
347       
348     (apply
349      string-append
350      (module-map scope-entry->string (ly:output-def-scope pd)))))
351
352 ;; FIXME: duplicated in other output backends
353 ;; FIXME: silly interface name
354 (define (output-scopes paper scopes fields basename)
355   (let ((prefix "lilypond"))
356
357     ;; FIXME: duplicates output-paper's scope-entry->string, mostly
358     (define (scope-entry->string key var)
359       (if (variable-bound? var)
360           (let ((val (variable-ref var)))
361             (if (and (memq key fields) (string? val))
362                 (header-to-file basename key val))
363             (cond
364              ((string? val) (ps-string-def prefix key val))
365              ((number? val) (ps-number-def prefix key val))
366              (else "")))
367           ""))
368     
369     (define (output-scope scope)
370       (apply string-append (module-map scope-entry->string scope)))
371
372     (string-append (apply string-append (map output-scope scopes)))))
373
374 ;; hmm, looks like recursing call is always last statement, does guile
375 ;; think so too?
376 (define (output-stencil port expr offset)
377   (if (pair? expr)
378       (let ((head (car expr)))
379         (cond
380          ((ly:input-location? head)
381           (display (apply define-origin (ly:input-location head)) port)
382           (output-stencil port (cadr expr) offset))
383          ((eq? head 'no-origin)
384           (display (expression->string head) port)
385           (output-stencil port (cadr expr) offset))
386          ((eq? head 'translate-stencil)
387           (output-stencil port (caddr expr) (offset-add offset (cadr expr))))
388          ((eq? head 'combine-stencil)
389           (output-stencil port (cadr expr) offset)
390           (output-stencil port (caddr expr) offset))
391          (else
392           (display (placebox (car offset) (cdr offset)
393                              (expression->string expr)) port))))))
394
395 (define (placebox x y s) 
396   (string-append 
397    (ly:number->string x) " " (ly:number->string y) " { " s " } place-box\n"))
398
399 (define (polygon points blotdiameter)
400   (string-append
401    (ly:numbers->string points) " "
402    (ly:number->string (/ (length points) 2)) " "
403    (ly:number->string blotdiameter)
404    " draw_polygon"))
405
406 (define (repeat-slash wid slope thick)
407   (string-append
408    (ly:numbers->string (list wid slope thick))
409    " draw_repeat_slash"))
410
411 (define (round-filled-box x y width height blotdiam)
412    (string-append
413     (ly:numbers->string
414      (list x y width height blotdiam)) " draw_round_box"))
415
416 (define (new-start-system origin dim)
417   (string-append
418    "\n" (ly:number-pair->string origin) " start-system\n"
419    "{\n"
420    "set-ps-scale-to-lily-scale\n"))
421
422 (define (stem breapth width depth height) 
423   (string-append
424    (ly:numbers->string (list breapth width depth height))
425    " draw_box" ))
426
427 (define (stop-system)
428   "} stop-system\n")
429
430 (define stop-last-system stop-system)
431
432 (define (symmetric-x-triangle thick w h)
433   (string-append
434    (ly:numbers->string (list h w thick))
435    " draw_symmetric_x_triangle"))
436
437 (define (text s)
438 ;;  (string-append "(" (escape-parentheses s) ") show "))
439   (string-append "(" (ps-encoding s) ") show"))
440
441 (define (unknown) 
442   "\n unknown\n")
443
444 (define (zigzag-line centre? zzw zzh thick dx dy)
445   (string-append
446     (if centre? "true" "false") " "
447     (ly:number->string zzw) " "
448     (ly:number->string zzh) " "
449     (ly:number->string thick) " "
450     "0 0 "
451     (ly:number->string dx) " "
452     (ly:number->string dy)
453     " draw_zigzag_line"))
454
455 (define (start-page)
456   (set! page-number (+ page-number 1))
457   (string-append
458    "%%Page: " (number->string page-number) " " (number->string page-count) "\n"
459   "start-page\n"))
460
461 (define (stop-page last?)
462   (if last?
463       "\nstop-last-page\n"
464       "\nstop-page\n"))