]> git.donarmstrong.com Git - lilypond.git/blob - Documentation/snippets/new/generating-custom-flags.ly
Rerun scripts/auxiliar/update-with-convert-ly.sh
[lilypond.git] / Documentation / snippets / new / generating-custom-flags.ly
1 \version "2.17.6"
2
3 \header {
4   lsrtags = "rhythms, tweaks-and-overrides"
5
6   texidoc = "
7 The @code{stencil} property of the Flag grob can be set to a custom scheme
8 function to generate the glyph for the flag.
9
10 "
11   doctitle = "Generating custom flags"
12 }
13
14 #(define-public (weight-flag grob)
15    (let* ((stem-grob (ly:grob-parent grob X))
16           (log (- (ly:grob-property stem-grob 'duration-log) 2))
17           (is-up? (eqv? (ly:grob-property stem-grob 'direction) UP))
18           (yext (if is-up? (cons (* log -0.8) 0) (cons 0 (* log 0.8))))
19           (flag-stencil (make-filled-box-stencil '(-0.4 . 0.4) yext))
20           (stroke-style (ly:grob-property grob 'stroke-style))
21           (stroke-stencil (if (equal? stroke-style "grace")
22                               (make-line-stencil 0.2 -0.9 -0.4 0.9 -0.4)
23                               empty-stencil)))
24      (ly:stencil-add flag-stencil stroke-stencil)))
25
26
27 % Create a flag stencil by looking up the glyph from the font
28 #(define (inverted-flag grob)
29    (let* ((stem-grob (ly:grob-parent grob X))
30           (dir (if (eqv? (ly:grob-property stem-grob 'direction) UP) "d" "u"))
31           (flag (retrieve-glyph-flag "" dir "" grob))
32           (line-thickness (ly:staff-symbol-line-thickness grob))
33           (stem-thickness (ly:grob-property stem-grob 'thickness))
34           (stem-width (* line-thickness stem-thickness))
35           (stroke-style (ly:grob-property grob 'stroke-style))
36           (stencil (if (null? stroke-style)
37                        flag
38                        (add-stroke-glyph flag stem-grob dir stroke-style "")))
39           (rotated-flag (ly:stencil-rotate-absolute stencil 180 0 0)))
40      (ly:stencil-translate rotated-flag (cons (- (/ stem-width 2)) 0))))
41
42 snippetexamplenotes = { \autoBeamOff c'8 d'16 c'32 d'64 \acciaccatura {c'8} d'64 }
43
44 {
45   \override Score.RehearsalMark.self-alignment-X = #LEFT
46   \time 1/4
47   \mark "Normal flags"
48   \snippetexamplenotes
49
50   \mark "Custom flag: inverted"
51   \override Flag.stencil = #inverted-flag
52   \snippetexamplenotes
53
54   \mark "Custom flag: weight"
55   \override Flag.stencil = #weight-flag
56   \snippetexamplenotes
57
58   \mark "Revert to normal"
59   \revert Flag.stencil
60   \snippetexamplenotes
61 }
62