]> git.donarmstrong.com Git - lilypond.git/blob - scm/output-ps.scm
e9ad248e45e174d0ef8fbb86e81304ba22432ff3
[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 (define (font-command font . override-coding)
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 (null? override-coding)
157                              (if (equal? input-encoding font-encoding)
158                                  #f font-encoding)
159                              (car override-coding))))
160
161     ;; FIXME:  now feta stuff has feta* input-encoding (again?)
162     ;;(format (current-error-port) "FONT: ~S, ~S\n" name font-encoding)
163     ;;(format (current-error-port) "INPUT: ~S\n" input-encoding)
164     (if (and coding-command (equal? (substring coding-command 0 4) "feta"))
165         (set! coding-command #f))
166
167     (string-append
168      "magfont" (string-encode-integer (hashq  name 1000000))
169      "m" (string-encode-integer (inexact->exact (round (* 1000 magnify))))
170      (if (not coding-command) "" (string-append "e" coding-command)))))
171
172 (define (define-fonts bookpaper)
173
174   (define font-list (ly:bookpaper-fonts bookpaper))
175   (define (define-font command fontname scaling)
176     (string-append
177      "/" command " { /" fontname " findfont "
178      (ly:number->string scaling) " output-scale div scalefont } bind def\n"))
179
180   (define (reencode-font plain encoding command)
181     (let ((coding-vector (get-coding-command encoding)))
182       (string-append
183        plain " " coding-vector " /" command " reencode-font\n"
184        "/" command "{ /" command " findfont 1 scalefont } bind def\n")))
185   
186   (define (guess-ps-fontname basename)
187     "We do not have the FontName, try to guess is from basename."
188     (cond
189      ((tex-font? basename)
190       ;; FIXME: we need proper Fontmap for the bluesky CM*, EC* fonts.
191       ;; Only the fonts that we trace in mf/ are in our own FontMap.
192       (string-append basename ".pfb"))
193      (else (string-append basename ".pfa"))
194      ))
195
196   (define (font-load-command font)
197     (let* ((specced-font-name (ly:font-name font))
198            (fontname (if specced-font-name
199                          specced-font-name
200                          (guess-ps-fontname (ly:font-filename font))))
201            
202            (coding-alist (ly:font-encoding-alist font))
203            (input-encoding (assoc-get 'input-name coding-alist))
204            (font-encoding (assoc-get 'output-name coding-alist))
205            (command (font-command font))
206            ;; FIXME -- see (font-command )
207            (plain (font-command font #f))
208            (designsize (ly:font-design-size font))
209            (magnification (* (ly:font-magnification font)))
210            (ops (ly:output-def-lookup bookpaper 'outputscale))
211            (scaling (* ops magnification designsize)))
212
213       (string-append
214        (define-font plain fontname scaling)
215        (if (or (equal? input-encoding font-encoding)
216                ;; guh
217                (equal? font-encoding "fetaBraces")
218                (equal? font-encoding "fetaNumber")
219                (equal? font-encoding "fetaMusic")
220                (equal? font-encoding "parmesanMusic"))
221                ""
222            (reencode-font plain input-encoding command)))))
223   
224   (define (font-load-encoding encoding)
225     (let ((filename (get-coding-filename encoding)))
226       (ly:kpathsea-gulp-file filename)))
227
228   (let* ((encoding-list (map (lambda (x)
229                                (assoc-get 'input-name
230                                           (ly:font-encoding-alist x)))
231                              font-list))
232          (encodings (uniq-list (sort-list (filter string? encoding-list)
233                                           string<?))))
234
235     (string-append
236      (apply string-append (map font-load-encoding encodings))
237      (apply string-append
238             (map (lambda (x) (font-load-command x)) font-list)))))
239
240 (define (define-origin file line col) "")
241
242 (define (dot x y radius)
243   (string-append
244    " "
245    (ly:numbers->string
246     (list x y radius)) " draw_dot"))
247
248 (define (draw-line thick x1 y1 x2 y2)
249   (string-append 
250    "1 setlinecap 1 setlinejoin "
251    (ly:number->string thick) " setlinewidth "
252    (ly:number->string x1) " "
253    (ly:number->string y1) " moveto "
254    (ly:number->string x2) " "
255    (ly:number->string y2) " lineto stroke"))
256
257 (define (end-output)
258   "\nend-lilypond-output\n")
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) ; FIXME : use draw_round_box
268   (string-append (ly:numbers->string (list breapth width depth height))
269                  " draw_box"))
270
271 (define (header creator time-stamp paper page-count- classic?)
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    "%%DocumentPaperSizes: " (ly:output-def-lookup paper 'papersize) "\n"
280    ;;(string-append "GNU LilyPond (" (lilypond-version) "), ")
281    ;;      (strftime "%c" (localtime (current-time))))
282    ;; FIXME: duplicated in every backend
283    (ps-string-def
284     "lilypond" 'tagline
285     (string-append "Engraved by LilyPond (version " (lilypond-version) ")"))
286    ))
287
288 (define (header-end)
289   "")
290
291 ;; WTF is this in every backend?
292 (define (horizontal-line x1 x2 th)
293   (draw-line th x1 0 x2 0))
294
295 (define (lily-def key val)
296   (let ((prefix "lilypondpaper"))
297     (if (string=?
298          (substring key 0 (min (string-length prefix) (string-length key)))
299          prefix)
300         (string-append "/" key " {" val "} bind def\n")
301         (string-append "/" key " (" val ") def\n"))))
302
303 (define (no-origin) "")
304
305 ;; FIXME: duplictates output-scopes, duplicated in other backends
306 ;; FIXME: silly interface name
307 (define (output-paper-def pd)
308   (let ((prefix "lilypondpaper"))
309     
310     (define (scope-entry->string key var)
311       (if (variable-bound? var)
312           (let ((val (variable-ref var)))
313             (cond
314              ((string? val) (ps-string-def prefix key val))
315              ((number? val) (ps-number-def prefix key val))
316              (else "")))
317           ""))
318       
319     (apply
320      string-append
321      (module-map scope-entry->string (ly:output-def-scope pd)))))
322
323 ;; FIXME: duplicated in other output backends
324 ;; FIXME: silly interface name
325 (define (output-scopes paper scopes fields basename)
326   (let ((prefix "lilypond"))
327
328     ;; FIXME: duplicates output-paper's scope-entry->string, mostly
329     (define (value->string  val)
330       (cond
331        ((string? val) (string-append "(" val ")"))
332        ((symbol? val) (symbol->string val))
333        ((number? val) (number->string val))
334        (else "")))
335       
336     (define (output-entry ps-key ly-key)
337       (string-append
338        "/" ps-key " " (value->string (ly:output-def-lookup paper ly-key)) " def \n"))
339
340     
341     (string-append
342      "/lily-output-units 2.83464  def  %% milimeter \n"
343      "% /lily-output-units 0.996264  def  %% true points.\n"
344      (output-entry "staff-line-thickness" 'linethickness)
345      (output-entry "line-width" 'linewidth)
346      (output-entry "paper-size" 'papersize)
347      (output-entry "staff-height" 'staffheight) ;junkme.
348      "/output-scale "
349      (number->string (ly:output-def-lookup paper 'outputscale))
350      " lily-output-units mul def \n"
351      
352      (ly:gulp-file "music-drawing-routines.ps")
353      (ly:gulp-file "lilyponddefs.ps")
354
355     )))
356   
357
358 (define (placebox x y s) 
359   (string-append 
360    (ly:number->string x) " " (ly:number->string y) " { " s " } place-box\n"))
361
362 (define (polygon points blotdiameter)
363   (string-append
364    (ly:numbers->string points) " "
365    (ly:number->string (/ (length points) 2)) " "
366    (ly:number->string blotdiameter)
367    " draw_polygon"))
368
369 (define (repeat-slash wid slope thick)
370   (string-append
371    (ly:numbers->string (list wid slope thick))
372    " draw_repeat_slash"))
373
374 (define (round-filled-box x y width height blotdiam)
375    (string-append
376     (ly:numbers->string
377      (list x y width height blotdiam)) " draw_round_box"))
378
379 (define (start-system origin dim)
380   (string-append
381    "\n" (ly:number-pair->string origin) " start-system\n"
382    "{\n"
383    "set-ps-scale-to-lily-scale\n"))
384
385 (define (stem breapth width depth height) ; FIXME: use draw_round_box.
386   (string-append
387    (ly:numbers->string (list breapth width depth height))
388    " draw_box" ))
389
390 (define (stop-system last?)
391   "} stop-system\n")
392
393 (define (symmetric-x-triangle thick w h)
394   (string-append
395    (ly:numbers->string (list h w thick))
396    " draw_symmetric_x_triangle"))
397
398 (define (text font s)
399   (let*
400       
401       (
402        ;; ugh, we should find a better way to
403        ;; extract the hsbw for /space from the font.
404        
405        (space-length (cdar (ly:text-dimension font "t"))) 
406        (commands '())
407        (add-command (lambda (x) (set! commands (cons x commands)))) )
408
409     (string-fold
410      (lambda (chr word)
411        "Translate space as into moveto, group the rest in words."
412        (if (and (< 0 (string-length word))
413                 (equal? #\space  chr))
414            (add-command 
415             (string-append "(" (ps-encoding word) ") show\n")))
416
417        (if (equal? #\space  chr)
418            (add-command  (string-append (number->string space-length) " 0.0 rmoveto ")) )
419        
420        (if (equal? #\space  chr)
421            ""
422            (string-append word (make-string 1 chr))))
423      ""
424      (string-append  s " "))
425
426     (string-append
427      (font-command font) " setfont "
428      (string-join (reverse commands)))
429     ))
430   
431 (define (unknown) 
432   "\n unknown\n")
433
434 (define (zigzag-line centre? zzw zzh thick dx dy)
435   (string-append
436     (if centre? "true" "false") " "
437     (ly:number->string zzw) " "
438     (ly:number->string zzh) " "
439     (ly:number->string thick) " "
440     "0 0 "
441     (ly:number->string dx) " "
442     (ly:number->string dy)
443     " draw_zigzag_line"))
444
445 (define (start-page)
446   (set! page-number (+ page-number 1))
447   (string-append
448    "%%Page: " (number->string page-number) " " (number->string page-count) "\n"
449   "start-page\n"))
450
451 (define (stop-page last?)
452   (if last?
453       "\nstop-last-page\n"
454       "\nstop-page\n"))