]> git.donarmstrong.com Git - lilypond.git/blob - scm/output-ps.scm
* scm/output-tex.scm: remove fontify; text and char take a font
[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 font i)
139   (string-append 
140     (font-command font) " setfont " 
141    "(\\" (ly:inexact->string i 8) ") show" ))
142
143 (define (comment s)
144   (string-append "% " s "\n"))
145
146 (define (dashed-line thick on off dx dy)
147   (string-append 
148    (ly:number->string dx) " "
149    (ly:number->string dy) " "
150    (ly:number->string thick)
151    " [ "
152    (ly:number->string on) " "
153    (ly:number->string off)
154    " ] 0 draw_dashed_line"))
155
156 ;; what the heck is this interface ?
157 (define (dashed-slur thick dash l)
158   (string-append 
159    (string-join (map ly:number-pair->string l) " ")
160    " "
161    (ly:number->string thick) 
162    " [ "
163    (ly:number->string dash)
164    " "
165    ;;UGH.  10 ?
166    (ly:number->string (* 10 thick))
167    " ] 0 draw_dashed_slur"))
168
169 (define (font-command font)
170   (string-append
171    "magfont"
172    (string-encode-integer
173     (hashq (ly:font-name font) 1000000))
174    "m"
175    (string-encode-integer
176     (inexact->exact (round (* 1000 (ly:font-magnification font)))))))
177
178 (define (define-fonts paper font-list)
179   (define (define-font command fontname scaling)
180     (string-append
181      "/" command " { /" fontname " findfont "
182      ;; FIXME
183      (ly:number->string scaling) " output-scale div scalefont } bind def\n"))
184      ;;(ly:number->string scaling) " scalefont } bind def\n"))
185
186   (define (reencode-font raw encoding command)
187     (string-append
188      raw " " encoding " /" command " reencode-font\n"
189      "/" command "{ /" command " findfont 1 scalefont } bind def\n"))
190           
191   ;; frobnicate NAME to jibe with external definitions.
192   (define (possibly-mangle-fontname fontname)
193     (cond
194      ((tex-font? fontname)
195       ;; FIXME: we need proper Fontmap for CM fonts, like so:
196       ;; /CMR10 (cmr10.pfb); 
197       ;; (string-upcase fontname)
198       (string-append fontname ".pfb"))
199      ((or (equal? (substring fontname 0 4) "feta")
200           (equal? (substring fontname 0 4) "parm"))
201       (regexp-substitute/global
202        #f "(feta|parmesan)([a-z-]*)([0-9]+)"
203        fontname 'pre "GNU-LilyPond-" 1 2 "-" 3 'post))
204      (else fontname)))
205                          
206   (define (font-load-command paper font)
207     (let* ((command (font-command font))
208            (fontname (ly:font-name font))
209            (mangled (possibly-mangle-fontname fontname))
210            (encoding (assoc-get fontname font-encoding-alist))
211            (designsize (ly:font-design-size font))
212            (magnification (* (ly:font-magnification font)))
213            (ops (ly:paper-lookup paper 'outputscale))
214            (scaling (* ops magnification designsize)) )
215
216       (if
217        #f
218        (begin
219          (newline)
220          (format (current-error-port) "fontname ~S\n" fontname)
221          (format (current-error-port) "command ~S\n" command)
222          (format (current-error-port) "mangled ~S\n" mangled)
223          (format (current-error-port) "designsize ~S\n" designsize)
224          (format (current-error-port) "foo-design ~S\n" foo-design)
225          (format (current-error-port) "magnification ~S\n" magnification)
226          (format (current-error-port) "ops ~S\n" ops)
227          (format (current-error-port) "scaling ~S\n" scaling)))
228       
229       (if encoding
230           ;; FIXME: should rather tag encoded font
231           (let ((raw (string-append command "-raw")))
232             (string-append
233              (define-font raw mangled scaling)
234              (reencode-font raw encoding command)))
235           (define-font command mangled scaling))))
236   
237   (apply string-append
238          (map (lambda (x) (font-load-command paper 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) 
268   (string-append (ly:numbers->string (list breapth width depth height))
269                  " draw_box"))
270
271 (define (header creator time-stamp page-count-)
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    ;; FIXME: TODO get from paper
280    ;; "%%DocumentPaperSizes: a6\n"
281    ;;(string-append "GNU LilyPond (" (lilypond-version) "), ")
282    ;;      (strftime "%c" (localtime (current-time))))
283    ;; FIXME: duplicated in every backend
284    (ps-string-def
285     "lilypond" 'tagline
286     (string-append "Engraved by LilyPond (version " (lilypond-version) ")"))))
287
288 (define (header-end)
289   (string-append
290    (ly:gulp-file "lilyponddefs.ps")
291    (ly:gulp-file "music-drawing-routines.ps")))
292
293 (define (horizontal-line x1 x2 th)
294   (draw-line th x1  0 x2 0))
295
296 (define (lily-def key val)
297   (let ((prefix "lilypondpaper"))
298     (if (string=?
299          (substring key 0 (min (string-length prefix) (string-length key)))
300          prefix)
301         (string-append "/" key " {" val "} bind def\n")
302         (string-append "/" key " (" val ") def\n"))))
303
304 (define (no-origin) "")
305
306 ;; FIXME: duplictates output-scopes, duplicated in other backends
307 ;; FIXME: silly interface name
308 (define (output-paper-def pd)
309   (let ((prefix "lilypondpaper"))
310     
311     (define (scope-entry->string key var)
312       (if (variable-bound? var)
313           (let ((val (variable-ref var)))
314             (cond
315              ((string? val) (ps-string-def prefix key val))
316              ((number? val) (ps-number-def prefix key val))
317              (else "")))
318           ""))
319       
320     (apply
321      string-append
322      (module-map scope-entry->string (ly:output-def-scope pd)))))
323
324 ;; FIXME: duplicated in other output backends
325 ;; FIXME: silly interface name
326 (define (output-scopes paper scopes fields basename)
327   (let ((prefix "lilypond"))
328
329     ;; FIXME: duplicates output-paper's scope-entry->string, mostly
330     (define (scope-entry->string key var)
331       (if (variable-bound? var)
332           (let ((val (variable-ref var)))
333             (if (and (memq key fields) (string? val))
334                 (header-to-file basename key val))
335             (cond
336              ((string? val) (ps-string-def prefix key val))
337              ((number? val) (ps-number-def prefix key val))
338              (else "")))
339           ""))
340     
341     (define (output-scope scope)
342       (apply string-append (module-map scope-entry->string scope)))
343
344     (string-append (apply string-append (map output-scope scopes)))))
345
346 ;; hmm, looks like recursing call is always last statement, does guile
347 ;; think so too?
348 (define (output-stencil port expr offset)
349   (if (pair? expr)
350       (let ((head (car expr)))
351         (cond
352          ((ly:input-location? head)
353           (display (apply define-origin (ly:input-location head)) port)
354           (output-stencil port (cadr expr) offset))
355          ((eq? head 'no-origin)
356           (display (expression->string head) port)
357           (output-stencil port (cadr expr) offset))
358          ((eq? head 'translate-stencil)
359           (output-stencil port (caddr expr) (offset-add offset (cadr expr))))
360          ((eq? head 'combine-stencil)
361           (output-stencil port (cadr expr) offset)
362           (output-stencil port (caddr expr) offset))
363          (else
364           (display (placebox (car offset) (cdr offset)
365                              (expression->string expr)) port))))))
366
367 (define (placebox x y s) 
368   (string-append 
369    (ly:number->string x) " " (ly:number->string y) " { " s " } place-box\n"))
370
371 (define (polygon points blotdiameter)
372   (string-append
373    (ly:numbers->string points) " "
374    (ly:number->string (/ (length points) 2)) " "
375    (ly:number->string blotdiameter)
376    " draw_polygon"))
377
378 (define (repeat-slash wid slope thick)
379   (string-append
380    (ly:numbers->string (list wid slope thick))
381    " draw_repeat_slash"))
382
383 (define (round-filled-box x y width height blotdiam)
384    (string-append
385     (ly:numbers->string
386      (list x y width height blotdiam)) " draw_round_box"))
387
388 (define (new-start-system origin dim)
389   (string-append
390    "\n" (ly:number-pair->string origin) " start-system\n"
391    "{\n"
392    "set-ps-scale-to-lily-scale\n"))
393
394 (define (stem breapth width depth height) 
395   (string-append
396    (ly:numbers->string (list breapth width depth height))
397    " draw_box" ))
398
399 (define (stop-system)
400   "} stop-system\n")
401
402 (define stop-last-system stop-system)
403
404 (define (symmetric-x-triangle thick w h)
405   (string-append
406    (ly:numbers->string (list h w thick))
407    " draw_symmetric_x_triangle"))
408
409 (define (text font s)
410   
411 ;;  (string-append "(" (escape-parentheses s) ") show "))
412   (string-append
413
414    (font-command font) " setfont " 
415    "(" (ps-encoding s) ") show"))
416
417 (define (unknown) 
418   "\n unknown\n")
419
420 (define (zigzag-line centre? zzw zzh thick dx dy)
421   (string-append
422     (if centre? "true" "false") " "
423     (ly:number->string zzw) " "
424     (ly:number->string zzh) " "
425     (ly:number->string thick) " "
426     "0 0 "
427     (ly:number->string dx) " "
428     (ly:number->string dy)
429     " draw_zigzag_line"))
430
431 (define (start-page)
432   (set! page-number (+ page-number 1))
433   (string-append
434    "%%Page: " (number->string page-number) " " (number->string page-count) "\n"
435   "start-page\n"))
436
437 (define (stop-page last?)
438   (if last?
439       "\nstop-last-page\n"
440       "\nstop-page\n"))