]> git.donarmstrong.com Git - lilypond.git/blob - scm/stencil.scm
* scm/markup.scm:
[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 'filledbox (- (car xext)) (cdr xext)
57                        (- (car yext)) (cdr yext))
58       xext yext))
59
60 (define-public (make-circle-stencil radius thickness)
61   "Make a circle of radius @var{radius} and thickness @var{thickness}"
62   (ly:make-stencil
63    (list 'circle radius thickness)
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 thick 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 thick) y-ext))
87          (x-rule (make-filled-box-stencil
88                   (interval-widen x-ext thick) (cons 0 thick))))
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)))
102     (ly:stencil-add
103      (centered-stencil stencil) (make-circle-stencil radius thickness))))
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))))