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