]> git.donarmstrong.com Git - lilypond.git/blob - scm/output-ps.scm
* lily/scaled-font-metric.cc (derived_mark): is a const method.
[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)
167   (string-append
168    "magfont"
169    (string-encode-integer
170     (hashq (ly:font-name font) 1000000))
171    "m"
172    (string-encode-integer
173     (inexact->exact (round (* 1000 (ly:font-magnification font)))))))
174
175 (define (define-fonts paper font-list)
176   (define (define-font command fontname scaling)
177     (string-append
178      "/" command " { /" fontname " findfont "
179      ;; FIXME
180      (ly:number->string scaling) " output-scale div scalefont } bind def\n"))
181      ;;(ly:number->string scaling) " scalefont } bind def\n"))
182
183   (define (reencode-font raw encoding command)
184     (string-append
185      raw " " encoding " /" command " reencode-font\n"
186      "/" command "{ /" command " findfont 1 scalefont } bind def\n"))
187           
188   ;; frobnicate NAME to jibe with external definitions.
189   (define (possibly-mangle-fontname fontname)
190     (cond
191      ((tex-font? fontname)
192       ;; FIXME: we need proper Fontmap for CM fonts, like so:
193       ;; /CMR10 (cmr10.pfb); 
194       ;; (string-upcase fontname)
195       (string-append fontname ".pfb"))
196      ((or (equal? (substring fontname 0 4) "feta")
197           (equal? (substring fontname 0 4) "parm"))
198       (regexp-substitute/global
199        #f "(feta|parmesan)([a-z-]*)([0-9]+)"
200        fontname 'pre "GNU-LilyPond-" 1 2 "-" 3 'post))
201      (else fontname)))
202                          
203   (define (font-load-command paper font)
204     (let* ((command (font-command font))
205            (fontname (ly:font-name font))
206            (mangled (possibly-mangle-fontname fontname))
207            (coding (ly:font-encoding font))
208            (encoding (vector-ref coding 2))
209            (designsize (ly:font-design-size font))
210            (magnification (* (ly:font-magnification font)))
211            (ops (ly:paper-lookup paper 'outputscale))
212            (scaling (* ops magnification designsize)))
213
214       (if
215        #t
216        (begin
217          (newline)
218          (format (current-error-port) "fontname ~S\n" fontname)
219          (format (current-error-port) "command ~S\n" command)
220          (format (current-error-port) "encoding ~S\n" coding)
221          (format (current-error-port) "encoding name ~S\n" encoding)
222          (format (current-error-port) "mangled ~S\n" mangled)
223          (format (current-error-port) "designsize ~S\n" designsize)
224          (format (current-error-port) "magnification ~S\n" magnification)
225          (format (current-error-port) "ops ~S\n" ops)
226          (format (current-error-port) "scaling ~S\n" scaling)))
227       
228       (if (or (equal? encoding "ISOLatin1Encoding")
229               ;; UGH, uhg 
230               (equal? fontname "feta20")
231               (equal? fontname "parmesan20"))
232           (define-font command mangled scaling)
233           ;; FIXME: should rather tag encoded font
234           (let ((raw (string-append command "-raw"))
235                 (vector (get-coding-command encoding))
236                 (filename (get-coding-filename encoding)))
237             (string-append
238              (ly:kpathsea-gulp-file filename)
239              (define-font raw mangled scaling)
240              (reencode-font raw vector command))))))
241   
242   (apply string-append
243          (map (lambda (x) (font-load-command paper x)) font-list)))
244
245 (define (define-origin file line col) "")
246
247 (define (dot x y radius)
248   (string-append
249    " "
250    (ly:numbers->string
251     (list x y radius)) " draw_dot"))
252
253 (define (draw-line thick x1 y1 x2 y2)
254   (string-append 
255    "1 setlinecap 1 setlinejoin "
256    (ly:number->string thick) " setlinewidth "
257    (ly:number->string x1) " "
258    (ly:number->string y1) " moveto "
259    (ly:number->string x2) " "
260    (ly:number->string y2) " lineto stroke"))
261
262 (define (end-output)
263   "\nend-lilypond-output\n")
264
265 (define (ez-ball ch letter-col ball-col)
266   (string-append
267    " (" ch ") "
268    (ly:numbers->string (list letter-col ball-col))
269    " /Helvetica-Bold " ;; ugh
270    " draw_ez_ball"))
271
272 (define (filledbox breapth width depth height) 
273   (string-append (ly:numbers->string (list breapth width depth height))
274                  " draw_box"))
275
276 (define (header creator time-stamp page-count-)
277   (set! page-count page-count-)
278   (set! page-number 0)
279   (string-append
280    "%!PS-Adobe-3.0\n"
281    "%%Creator: " creator " " time-stamp "\n"
282    "%%Pages: " (number->string page-count) "\n"
283    "%%PageOrder: Ascend\n"
284    ;; FIXME: TODO get from paper
285    ;; "%%DocumentPaperSizes: a6\n"
286    ;;(string-append "GNU LilyPond (" (lilypond-version) "), ")
287    ;;      (strftime "%c" (localtime (current-time))))
288    ;; FIXME: duplicated in every backend
289    (ps-string-def
290     "lilypond" 'tagline
291     (string-append "Engraved by LilyPond (version " (lilypond-version) ")"))))
292
293 (define (header-end)
294   (string-append
295    (ly:gulp-file "lilyponddefs.ps")
296    (ly:gulp-file "music-drawing-routines.ps")))
297
298 (define (horizontal-line x1 x2 th)
299   (draw-line th x1  0 x2 0))
300
301 (define (lily-def key val)
302   (let ((prefix "lilypondpaper"))
303     (if (string=?
304          (substring key 0 (min (string-length prefix) (string-length key)))
305          prefix)
306         (string-append "/" key " {" val "} bind def\n")
307         (string-append "/" key " (" val ") def\n"))))
308
309 (define (no-origin) "")
310
311 ;; FIXME: duplictates output-scopes, duplicated in other backends
312 ;; FIXME: silly interface name
313 (define (output-paper-def pd)
314   (let ((prefix "lilypondpaper"))
315     
316     (define (scope-entry->string key var)
317       (if (variable-bound? var)
318           (let ((val (variable-ref var)))
319             (cond
320              ((string? val) (ps-string-def prefix key val))
321              ((number? val) (ps-number-def prefix key val))
322              (else "")))
323           ""))
324       
325     (apply
326      string-append
327      (module-map scope-entry->string (ly:output-def-scope pd)))))
328
329 ;; FIXME: duplicated in other output backends
330 ;; FIXME: silly interface name
331 (define (output-scopes paper scopes fields basename)
332   (let ((prefix "lilypond"))
333
334     ;; FIXME: duplicates output-paper's scope-entry->string, mostly
335     (define (scope-entry->string key var)
336       (if (variable-bound? var)
337           (let ((val (variable-ref var)))
338             (if (and (memq key fields) (string? val))
339                 (header-to-file basename key val))
340             (cond
341              ((string? val) (ps-string-def prefix key val))
342              ((number? val) (ps-number-def prefix key val))
343              (else "")))
344           ""))
345     
346     (define (output-scope scope)
347       (apply string-append (module-map scope-entry->string scope)))
348
349     (string-append (apply string-append (map output-scope scopes)))))
350
351 ;; hmm, looks like recursing call is always last statement, does guile
352 ;; think so too?
353 (define (output-stencil port expr offset)
354   (if (pair? expr)
355       (let ((head (car expr)))
356         (cond
357          ((ly:input-location? head)
358           (display (apply define-origin (ly:input-location head)) port)
359           (output-stencil port (cadr expr) offset))
360          ((eq? head 'no-origin)
361           (display (expression->string head) port)
362           (output-stencil port (cadr expr) offset))
363          ((eq? head 'translate-stencil)
364           (output-stencil port (caddr expr) (offset-add offset (cadr expr))))
365          ((eq? head 'combine-stencil)
366           (output-stencil port (cadr expr) offset)
367           (output-stencil port (caddr expr) offset))
368          (else
369           (display (placebox (car offset) (cdr offset)
370                              (expression->string expr)) port))))))
371
372 (define (placebox x y s) 
373   (string-append 
374    (ly:number->string x) " " (ly:number->string y) " { " s " } place-box\n"))
375
376 (define (polygon points blotdiameter)
377   (string-append
378    (ly:numbers->string points) " "
379    (ly:number->string (/ (length points) 2)) " "
380    (ly:number->string blotdiameter)
381    " draw_polygon"))
382
383 (define (repeat-slash wid slope thick)
384   (string-append
385    (ly:numbers->string (list wid slope thick))
386    " draw_repeat_slash"))
387
388 (define (round-filled-box x y width height blotdiam)
389    (string-append
390     (ly:numbers->string
391      (list x y width height blotdiam)) " draw_round_box"))
392
393 (define (new-start-system origin dim)
394   (string-append
395    "\n" (ly:number-pair->string origin) " start-system\n"
396    "{\n"
397    "set-ps-scale-to-lily-scale\n"))
398
399 (define (stem breapth width depth height) 
400   (string-append
401    (ly:numbers->string (list breapth width depth height))
402    " draw_box" ))
403
404 (define (stop-system)
405   "} stop-system\n")
406
407 (define stop-last-system stop-system)
408
409 (define (symmetric-x-triangle thick w h)
410   (string-append
411    (ly:numbers->string (list h w thick))
412    " draw_symmetric_x_triangle"))
413
414 (define (text font s)
415   
416 ;;  (string-append "(" (escape-parentheses s) ") show "))
417   (string-append
418
419    (font-command font) " setfont " 
420    "(" (ps-encoding s) ") show"))
421
422 (define (unknown) 
423   "\n unknown\n")
424
425 (define (zigzag-line centre? zzw zzh thick dx dy)
426   (string-append
427     (if centre? "true" "false") " "
428     (ly:number->string zzw) " "
429     (ly:number->string zzh) " "
430     (ly:number->string thick) " "
431     "0 0 "
432     (ly:number->string dx) " "
433     (ly:number->string dy)
434     " draw_zigzag_line"))
435
436 (define (start-page)
437   (set! page-number (+ page-number 1))
438   (string-append
439    "%%Page: " (number->string page-number) " " (number->string page-count) "\n"
440   "start-page\n"))
441
442 (define (stop-page last?)
443   (if last?
444       "\nstop-last-page\n"
445       "\nstop-page\n"))