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