]> git.donarmstrong.com Git - lilypond.git/blob - scm/output-ps.scm
handle CID CFFs.
[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--2005 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 (define-module (scm output-ps)
17   #:re-export (quote)
18
19   ;; JUNK this -- see lily.scm: ly:all-output-backend-commands
20   #:export (unknown
21             blank
22             circle
23             dot
24             white-dot
25             beam
26             bracket
27             dashed-slur
28             char
29             setcolor
30             resetcolor
31             named-glyph
32             dashed-line
33             zigzag-line
34             ez-ball
35             comment
36             repeat-slash
37             placebox
38             bezier-sandwich
39             horizontal-line
40             embedded-ps
41             filledbox
42             round-filled-box
43             text
44             white-text
45             polygon
46             draw-line
47             no-origin))
48
49
50 (use-modules (guile)
51              (ice-9 regex)
52              (srfi srfi-1)
53              (srfi srfi-13)
54              (scm framework-ps)
55              (lily))
56
57
58 ;;(map export
59 ;;   (append (ly:all-stencil-expressions) (ly:all-output-backend-commands)))
60
61 ;; huh?
62 ;;(write (ly:all-output-backend-commands))
63 ;;(write (ly:all-stencil-expressions))
64
65 ;;; helper functions, not part of output interface
66 (define (escape-parentheses s)
67   (regexp-substitute/global #f "(^|[^\\])([\\(\\)])" s 'pre 1 "\\" 2 'post))
68
69 (define (ps-encoding text)
70   (escape-parentheses text))
71
72 ;; FIXME: lily-def
73 (define-public (ps-string-def prefix key val)
74   (string-append "/" prefix (symbol->string key) " ("
75                  (escape-parentheses val)
76                  ") def\n"))
77
78
79 (define (ps-number-def prefix key val)
80   (let ((s (if (integer? val)
81                (ly:number->string val)
82                (ly:number->string (exact->inexact val)))))
83     (string-append "/" prefix (symbol->string key) " " s " def\n")))
84
85
86 ;;;
87 ;;; Lily output interface, PostScript implementation --- cleanup and docme
88 ;;;
89
90 ;;; Output-interface functions
91 (define (beam width slope thick blot)
92   (string-append
93    (ly:numbers->string (list slope width thick blot)) " draw_beam" ))
94
95 ;; two beziers
96 (define (bezier-sandwich lst thick)
97   (string-append 
98    (string-join (map ly:number-pair->string lst) " ")
99    " "
100    (ly:number->string thick)
101    " draw_bezier_sandwich"))
102
103 (define (bracket arch_angle arch_width arch_height height arch_thick thick)
104   (string-append
105    (ly:numbers->string
106     (list arch_angle arch_width arch_height height arch_thick thick))
107    " draw_bracket"))
108
109 (define (char font i)
110   (string-append 
111    (ps-font-command font) " setfont " 
112    "(\\" (ly:inexact->string i 8) ") show"))
113
114 (define (circle radius thick fill)
115   (format
116    "~a ~a ~a draw_circle" radius thick
117    (if fill
118        "true "
119        "false ")))
120
121 (define (dashed-line thick on off dx dy)
122   (string-append 
123    (ly:number->string dx) " "
124    (ly:number->string dy) " "
125    (ly:number->string thick)
126    " [ "
127    (ly:number->string on) " "
128    (ly:number->string off)
129    " ] 0 draw_dashed_line"))
130
131 ;; what the heck is this interface ?
132 (define (dashed-slur thick on off l)
133   (string-append 
134    (string-join (map ly:number-pair->string l) " ")
135    " "
136    (ly:number->string thick) 
137    " [ "
138    (ly:number->string on)
139    " "   
140    (ly:number->string off)
141    " ] 0 draw_dashed_slur"))
142
143 (define (dot x y radius)
144   (string-append
145    " "
146    (ly:numbers->string
147     (list x y radius)) " draw_dot"))
148
149 (define (draw-line thick x1 y1 x2 y2)
150   (string-append 
151    "1 setlinecap 1 setlinejoin "
152    (ly:number->string thick) " setlinewidth "
153    (ly:number->string x1) " "
154    (ly:number->string y1) " moveto "
155    (ly:number->string x2) " "
156    (ly:number->string y2) " lineto stroke"))
157
158 (define (embedded-ps string)
159   string)
160
161 ;; FIXME. 
162 (define (ez-ball ch letter-col ball-col)
163   (string-append
164    " (" ch ") "
165    (ly:numbers->string (list letter-col ball-col))
166    ;; FIXME: barf
167    " /Helvetica-Bold "
168    " draw_ez_ball"))
169
170 ;; FIXME: use draw_round_box
171 (define (filledbox breapth width depth height)
172   (string-append (ly:numbers->string (list breapth width depth height))
173                  " draw_box"))
174
175 (define (glyph-string
176          postscript-font-name
177          size cid?
178          x-y-named-glyphs)
179   
180   (format #f "gsave 1 output-scale div 1 output-scale div scale
181   /~a ~a ~a scalefont setfont\n~a grestore"
182           postscript-font-name
183           (if cid?
184               " /CIDFont findresource "
185               " findfont") 
186           
187           size
188           (apply
189            string-append
190            (map (lambda  (item)
191                   (let
192                       ((x (car item))
193                        (y (cadr item))
194                        (g (caddr item)))
195
196                     (if (and (= 0.0 x)
197                              (= 0.0 y))
198                         (format #f " /~a glyphshow\n" g)
199                         (format #f " ~a ~a rmoveto ~a~a glyphshow\n"
200                                 x y
201                                 (if  (string? g) "/" "")
202                                 g))))
203                 x-y-named-glyphs))))
204
205 (define (grob-cause offset grob)
206   (let* ((cause (ly:grob-property grob 'cause))
207          (music-origin (if (ly:music? cause)
208                            (ly:music-property cause 'origin))))
209     (if (not (ly:input-location? music-origin))
210         ""
211         (let* ((location (ly:input-file-line-column music-origin))
212                (raw-file (car location))
213                (file (if (is-absolute? raw-file)
214                          raw-file
215                          (string-append (ly-getcwd) "/" raw-file)))
216                (x-ext (ly:grob-extent grob grob X))
217                (y-ext (ly:grob-extent grob grob Y)))
218
219           (if (and (< 0 (interval-length x-ext))
220                    (< 0 (interval-length y-ext)))
221               (format "~a ~a ~a ~a (textedit://~a:~a:~a) mark_URI\n"
222                       (+ (car offset) (car x-ext))
223                       (+ (cdr offset) (car y-ext))
224                       (+ (car offset) (cdr x-ext))
225                       (+ (cdr offset) (cdr y-ext))
226                       file
227                       (cadr location)
228                       (caddr location))
229               "")))))
230
231 ;; WTF is this in every backend?
232 (define (horizontal-line x1 x2 th)
233   (draw-line th x1 0 x2 0))
234
235 (define (lily-def key val)
236   (let ((prefix "lilypondlayout"))
237     (if (string=?
238          (substring key 0 (min (string-length prefix) (string-length key)))
239          prefix)
240         (string-append "/" key " {" val "} bind def\n")
241         (string-append "/" key " (" val ") def\n"))))
242
243 (define (named-glyph font glyph)
244   (string-append 
245    (ps-font-command font) " setfont " 
246    "/" glyph " glyphshow "))
247
248 (define (no-origin)
249   "")
250
251 (define (placebox x y s) 
252   (string-append 
253    (ly:number->string x) " " (ly:number->string y) " { " s " } place-box\n"))
254
255 (define (polygon points blotdiameter filled?)
256   (string-append
257    (ly:numbers->string points) " "
258    (ly:number->string (/ (length points) 2)) " "
259    (ly:number->string blotdiameter)
260    (if filled? " true " " false ")
261    " draw_polygon"))
262
263 (define (repeat-slash wid slope thick)
264   (string-append
265    (ly:numbers->string (list wid slope thick))
266    " draw_repeat_slash"))
267
268 ;; restore color from stack
269 (define (resetcolor)
270   (string-append "setrgbcolor\n"))
271
272 (define (round-filled-box x y width height blotdiam)
273   (string-append
274    (ly:numbers->string
275     (list x y width height blotdiam)) " draw_round_box"))
276
277 ;; save current color on stack and set new color
278 (define (setcolor r g b)
279   (string-append "currentrgbcolor "
280   (ly:numbers->string (list r g b))
281   " setrgbcolor\n"))
282
283 (define (text font s)
284   ;; (ly:warning (_ "TEXT backend-command encountered in Pango backend"))
285   ;; (ly:warning (_ "Arguments: ~a ~a"" font str))
286   
287   (let* ((space-length (cdar (ly:text-dimension font " ")))
288          (space-move (string-append (number->string space-length)
289                                     " 0.0 rmoveto "))
290          (out-vec (decode-byte-string s)))
291
292     (string-append
293      (ps-font-command font) " setfont "
294      (string-join
295       (vector->list
296        (vector-for-each
297         
298         (lambda (sym)
299           (if (eq? sym 'space)
300               space-move
301               (string-append "/" (symbol->string sym) " glyphshow")))
302         out-vec))))))
303
304 (define (unknown) 
305   "\n unknown\n")
306
307 (define (url-link url x y)
308   (format "~a ~a ~a ~a (~a) mark_URI"
309           (car x)
310           (car y)
311           (cdr x)
312           (cdr y)
313           url))
314
315 (define (utf8-string pango-font-description string)
316   (ly:warning (_ "utf8-string encountered in PS backend")))
317
318 (define (white-dot x y radius)
319   (string-append
320    " "
321    (ly:numbers->string
322     (list x y radius)) " draw_white_dot"))
323
324 (define (white-text scale s)
325   (let ((mystring (string-append
326                    "(" s  ") " (number->string scale)
327                    " /Helvetica-Bold "
328                    " draw_white_text")))
329     mystring
330     ))
331
332 (define (zigzag-line centre? zzw zzh thick dx dy)
333   (string-append
334    (if centre? "true" "false") " "
335    (ly:number->string zzw) " "
336    (ly:number->string zzh) " "
337    (ly:number->string thick) " "
338    "0 0 "
339    (ly:number->string dx) " "
340    (ly:number->string dy)
341    " draw_zigzag_line"))