]> git.donarmstrong.com Git - lilypond.git/blob - scm/stencil.scm
Fix some bugs in the dynamic engraver and PostScript backend
[lilypond.git] / scm / stencil.scm
1 ;;;; stencil.scm -- 
2 ;;;;
3 ;;;;  source file of the GNU LilyPond music typesetter
4 ;;;; 
5 ;;;; (c) 2003--2006 Han-Wen Nienhuys <hanwen@cs.uu.nl>
6
7 (define-public (stack-stencils axis dir padding stils)
8   "Stack stencils STILS in direction AXIS, DIR, using PADDING."
9   (cond
10    ((null? stils) empty-stencil)
11    ((null? (cdr stils)) (car stils))
12    (else (ly:stencil-combine-at-edge
13           (car stils) axis dir (stack-stencils axis dir padding (cdr stils))
14           padding))))
15
16 (define-public (stack-stencils-padding-list axis dir padding stils)
17   "Stack stencils STILS in direction AXIS, DIR, using a list of PADDING."
18   (cond
19    ((null? stils) empty-stencil)
20    ((null? (cdr stils)) (car stils))
21    (else (ly:stencil-combine-at-edge
22           (car stils)
23           axis dir
24           (stack-stencils-padding-list axis dir (cdr padding) (cdr stils))
25           (car padding)))))
26
27 (define-public (centered-stencil stencil)
28   "Center stencil @var{stencil} in both the X and Y directions"
29   (ly:stencil-aligned-to (ly:stencil-aligned-to stencil X CENTER) Y CENTER))
30
31 (define-public (stack-lines dir padding baseline stils)
32   "Stack vertically with a baseline-skip."
33   (if (null? stils)
34       empty-stencil
35       (if (null? (cdr stils))
36           (car stils)
37           (ly:stencil-combine-at-edge
38            (car stils) Y dir 
39            (stack-lines dir padding baseline (cdr stils))
40            padding baseline))))
41
42 (define-public (bracketify-stencil stil axis thick protusion padding)
43   "Add brackets around STIL, producing a new stencil."
44
45   (let* ((ext (ly:stencil-extent stil axis))
46          (lb (ly:bracket axis ext thick (- protusion)))
47          (rb (ly:bracket axis ext thick protusion)))
48     (set! stil
49           (ly:stencil-combine-at-edge stil (other-axis axis) 1 lb padding))
50     (set! stil
51           (ly:stencil-combine-at-edge stil (other-axis axis) -1 rb padding))
52     stil))
53
54 (define-public (make-filled-box-stencil xext yext)
55   "Make a filled box."
56   
57   (ly:make-stencil
58       (list 'round-filled-box (- (car xext)) (cdr xext)
59                        (- (car yext)) (cdr yext) 0.0)
60       xext yext))
61
62 (define-public (make-circle-stencil radius thickness fill)
63   "Make a circle of radius @var{radius} and thickness @var{thickness}"
64   (ly:make-stencil
65    (list 'circle radius thickness fill) 
66    (cons (- radius) radius)
67    (cons (- radius) radius)))
68
69 (define-public (box-grob-stencil grob)
70   "Make a box of exactly the extents of the grob.  The box precisely
71 encloses the contents.
72 "
73   (let* ((xext (ly:grob-extent grob grob 0))
74          (yext (ly:grob-extent grob grob 1))
75          (thick 0.1))
76     
77     (ly:stencil-add
78      (make-filled-box-stencil xext (cons (- (car yext) thick) (car yext)))
79      (make-filled-box-stencil xext (cons (cdr yext) (+ (cdr yext) thick)))
80      (make-filled-box-stencil (cons (cdr xext) (+ (cdr xext) thick)) yext)
81      (make-filled-box-stencil (cons (- (car xext) thick) (car xext)) yext))))
82
83 ;; TODO merge this and prev function. 
84 (define-public (box-stencil stencil thickness padding)
85   "Add a box around STENCIL, producing a new stencil."
86   (let* ((x-ext (interval-widen (ly:stencil-extent stencil 0) padding))
87          (y-ext (interval-widen (ly:stencil-extent stencil 1) padding))
88          (y-rule (make-filled-box-stencil (cons 0 thickness) y-ext))
89          (x-rule (make-filled-box-stencil
90                   (interval-widen x-ext thickness) (cons 0 thickness))))
91     (set! stencil (ly:stencil-combine-at-edge stencil X 1 y-rule padding))
92     (set! stencil (ly:stencil-combine-at-edge stencil X -1 y-rule padding))
93     (set! stencil (ly:stencil-combine-at-edge stencil Y 1 x-rule 0.0))  
94     (set! stencil (ly:stencil-combine-at-edge stencil Y -1 x-rule 0.0))
95     stencil))
96
97 (define-public (circle-stencil stencil thickness padding)
98   "Add a circle around STENCIL, producing a new stencil."
99   (let* ((x-ext (ly:stencil-extent stencil 0))
100          (y-ext (ly:stencil-extent stencil 1))
101          (diameter (max (- (cdr x-ext) (car x-ext))
102                         (- (cdr y-ext) (car y-ext))))
103          (radius (+ (/ diameter 2) padding thickness)))
104     (ly:stencil-add
105      (centered-stencil stencil) (make-circle-stencil radius thickness #f))))
106
107 (define-public (fontify-text font-metric text)
108   "Set TEXT with font FONT-METRIC, returning a stencil."
109   (let* ((b (ly:text-dimension font-metric text)))
110     (ly:make-stencil
111      `(text ,font-metric ,text) (car b) (cdr b))))
112      
113 (define-public (fontify-text-white scale font-metric text)
114   "Set TEXT with scale factor s"
115   (let* ((b (ly:text-dimension font-metric text))
116          ;;urg -- workaround for using ps font
117          (c `(white-text ,(* 2 scale) ,text)))
118     ;;urg -- extent is not from ps font, but we hope it's close
119     (ly:make-stencil c (car b) (cdr b))))
120
121 (define-public (dimension-arrows destination) 
122   "Draw twosided arrow from here to @var{destination}"
123   
124   (let*
125       ((e_x 1+0i)
126        (e_y 0+1i)
127        (rotate (lambda (z ang)
128                  (* (make-polar 1 ang)
129                     z)))
130        (complex-to-offset (lambda (z)
131                             (list (real-part z) (imag-part z))))
132        
133        (z-dest (+ (* e_x (car destination)) (* e_y (cdr destination))))
134        (e_z (/ z-dest (magnitude z-dest)))
135        (triangle-points '(-1+0.25i
136                           0
137                           -1-0.25i))
138        (p1s (map (lambda (z)
139                    (+ z-dest (rotate z (angle z-dest))))
140                  triangle-points))
141        (p2s (map (lambda (z)
142                    (rotate z (angle (- z-dest))))
143                    triangle-points))
144        (null (cons 0 0)) 
145        (arrow-1  
146         (ly:make-stencil
147          `(polygon (quote ,(concatenate (map complex-to-offset p1s)))
148                    0.0
149                    #t) null null))
150        (arrow-2
151         (ly:make-stencil
152          `(polygon (quote ,(concatenate (map complex-to-offset p2s)))
153                    0.0
154                    #t) null null ) )
155        (thickness 0.1)
156        (shorten-line 0.5)
157        (start (complex-to-offset (/ (* e_z shorten-line) 2)))
158        (end (complex-to-offset (- z-dest (/ (* e_z shorten-line) 2))))
159        
160        (line (ly:make-stencil
161               `(draw-line ,thickness
162                           ,(car start) ,(cadr start)
163                           ,(car end) ,(cadr end)
164                           )
165               (cons (min 0 (car destination))
166                     (min 0 (cdr destination)))
167               (cons (max 0 (car destination))
168                     (max 0 (cdr destination)))))
169                     
170        (result (ly:stencil-add arrow-2 arrow-1 line)))
171
172
173     result))
174
175 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
176 ;; ANNOTATIONS
177 ;;
178 ;; annotations are arrows indicating the numerical value of
179 ;; spacing variables 
180 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
181
182 (define-public (annotate-y-interval layout name extent is-length)
183   (let*
184       ((text-props (cons
185                     '((font-size . -3)
186                       (font-family . typewriter))
187                     (layout-extract-page-properties layout)))
188        (annotation #f)
189        )
190
191     ;; do something sensible for 0,0 intervals. 
192     (set! extent (interval-widen extent 0.001))
193     (if (not (interval-sane? extent))
194         (set! annotation (interpret-markup layout text-props
195                                            (make-simple-markup (format "~a: NaN/inf" name))))
196         (let*
197             ((text-stencil (interpret-markup
198                             layout text-props
199                             (make-column-markup
200                              (list
201                               (make-whiteout-markup (make-simple-markup name))
202                               (make-whiteout-markup
203                                (make-simple-markup
204                                 (cond
205                                  ((interval-empty? extent) "empty")
206                                  (is-length (format "~$" (interval-length extent)))
207                                  (else
208                                   (format "(~$,~$)" (car extent)
209                                           (cdr extent))))))))))
210              (arrows
211               (ly:stencil-translate-axis 
212                (dimension-arrows (cons 0 (interval-length extent)))
213                (interval-start extent) Y)))
214           
215           (set! annotation
216                 (ly:stencil-aligned-to text-stencil Y CENTER))
217           
218           (set! annotation (ly:stencil-translate
219                             annotation
220                             (cons 0 (interval-center extent))))
221           
222
223           (set! annotation
224                 (ly:stencil-combine-at-edge arrows X RIGHT annotation 0.5 0))
225
226           (set! annotation
227                 (ly:make-stencil (ly:stencil-expr annotation)
228                                  (ly:stencil-extent annotation X)
229                                  (cons 10000 -10000)))))
230     annotation))
231
232
233 (define-public (eps-file->stencil axis size file-name)
234   (let*
235       ((contents (ly:gulp-file file-name))
236        (bbox (get-postscript-bbox contents))
237        (bbox-size (if (= axis X)
238                       (- (list-ref bbox 2) (list-ref bbox 0))
239                       (- (list-ref bbox 3) (list-ref bbox 1))
240                       ))
241        (factor (exact->inexact (/ size bbox-size)))
242        (scaled-bbox
243         (map (lambda (x) (* factor x)) bbox)))
244
245     (if bbox
246         (ly:make-stencil
247          (list
248           'embedded-ps
249           (string-append
250            (format
251            "
252 gsave
253 currentpoint translate
254 BeginEPSF
255 ~a ~a scale
256 %%BeginDocument: ~a
257 "          factor factor
258            file-name
259            )
260            contents
261            "%%EndDocument
262 EndEPSF
263 grestore
264 "))
265         
266          (cons (list-ref scaled-bbox 0) (list-ref scaled-bbox 2))
267          (cons (list-ref scaled-bbox 1) (list-ref scaled-bbox 3)))
268         
269         (ly:make-stencil "" '(0 . 0) '(0 . 0)))
270     ))
271
272 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
273 ;; output signatures.
274
275 (define-public (write-system-signatures basename paper-systems count)
276   (if (pair? paper-systems)
277       (begin
278         (let*
279             ((outname (format "~a-~a.signature" basename count)) )
280              
281           (ly:message "Writing ~a" outname)
282           (write-system-signature outname (car paper-systems))
283           (write-system-signatures basename (cdr paper-systems) (1+ count))))))
284
285
286 (define-public (write-system-signature filename paper-system)
287   (define (float? x)
288     (and (number? x) (inexact? x)))
289
290   (define system-grob
291     (paper-system-system-grob paper-system))
292   
293   (define output (open-output-file filename))
294   
295   (define (strip-floats expr)
296     "Replace floats by #f"
297     (cond
298      ((float? expr) #f)
299      ((ly:font-metric? expr) (ly:font-name expr))
300      ((pair? expr) (cons (strip-floats (car expr))
301                          (strip-floats (cdr expr))))
302      (else expr)))
303
304   (define (fold-false-pairs expr)
305     "Try to remove lists of #f as much as possible."
306     (if (pair? expr)
307         (let*
308             ((first (car expr))
309              (rest (fold-false-pairs (cdr expr))))
310
311           (if first
312               (cons (fold-false-pairs first) rest)
313               rest))
314         expr))
315   
316   (define (music-cause grob)
317     (let*
318         ((cause (ly:grob-property  grob 'cause)))
319       
320       (cond
321        ((ly:music? cause) cause)
322        ((ly:grob? cause) (music-cause cause))
323        (else #f))))
324
325   (define (pythonic-string expr)
326     "escape quotes and slashes for python consumption"
327     (regexp-substitute/global #f "([\\\\'\"])" (format "~a" expr) 'pre "\\" 1 'post))
328
329   (define (pythonic-pair expr)
330     (format "(~a,~a)"
331             (car expr) (cdr expr)))
332                     
333   (define (found-grob expr)
334     (let*
335         ((grob (car expr))
336          (rest (cdr expr))
337          (collected '())
338          (cause (music-cause grob))
339          (input (if (ly:music? cause) (ly:music-property cause 'origin) #f))
340          (location (if (ly:input-location? input) (ly:input-file-line-char-column input) '()))
341
342          (x-ext (ly:grob-extent grob system-grob X))
343          (y-ext (ly:grob-extent grob system-grob Y))
344          )
345
346       (interpret-for-signature #f (lambda (e)
347                                     (set! collected (cons e collected)))
348                                rest)
349
350       (format output
351               "['~a', '~a', ~a, ~a, '~a'],\n"
352               (cdr (assq 'name (ly:grob-property grob 'meta) ))
353               (pythonic-string location)
354               (pythonic-pair (if (interval-empty? x-ext) '(0 . 0) x-ext))
355               (pythonic-pair (if (interval-empty? y-ext) '(0 . 0) y-ext))
356               (pythonic-string collected))
357       ))
358
359   (define (interpret-for-signature escape collect expr)
360     (define (interpret expr)
361       (let*
362           ((head (if (pair? expr)
363                      (car expr)
364                      #f)))
365
366         (cond
367          ((eq? head 'grob-cause) (escape (cdr expr)))
368          ((eq? head 'color) (interpret (caddr expr)))
369          ((eq? head 'rotate-stencil) (interpret (caddr expr)))
370          ((eq? head 'translate-stencil) (interpret (caddr expr)))
371          ((eq? head 'combine-stencil)
372           (for-each (lambda (e) (interpret e))  (cdr expr)))
373          (else
374           (collect (fold-false-pairs (strip-floats expr))))
375          
376          )))
377
378     (interpret expr))
379
380   (if (ly:grob? system-grob)
381       (begin
382         (display (format "# Output signature\n# Generated by LilyPond ~a\n" (lilypond-version))
383                  output)
384         (interpret-for-signature found-grob (lambda (x) #f)
385                                  (ly:stencil-expr
386                                   (paper-system-stencil paper-system))))))
387