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