]> git.donarmstrong.com Git - lilypond.git/blob - scm/stencil.scm
3fb0df8b2dc3b16a20ececd1eb010971e3447bad
[lilypond.git] / scm / stencil.scm
1 ;;;; stencil.scm -- 
2 ;;;;
3 ;;;;  source file of the GNU LilyPond music typesetter
4 ;;;; 
5 ;;;; (c) 2003--2005 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))