3 ;;;; source file of the GNU LilyPOnd music typesetter
6 (define-public (no-flag stem-grob)
7 "No flag: Simply return empty stencil"
11 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
13 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
16 (define-public (add-stroke-straight stencil stem-grob stem-up? log stroke-style offset length thickness stroke-thickness)
17 "Add the stroke for acciaccatura to the given flag stencil."
18 (let* ((udmult (if stem-up? 1 -1))
19 (start (offset-add offset (cons 0 (* (/ length 2) udmult))))
20 (end (offset-add (cons 0 (cdr offset))
21 (cons (- (/ (car offset) 2)) (* (- (+ thickness (car offset))) udmult))))
22 (stroke (make-line-stencil stroke-thickness (car start) (cdr start) (car end) (cdr end))))
23 (ly:stencil-add stencil stroke)))
25 (define (polar->rectangular radius angle-in-degrees)
26 "Convert polar coordinate @code{radius} and @code{angle-in-degrees}
27 to (x-length . y-length)"
28 (let* ((conversion-constant (/ (atan 1 1) 45))
31 (* conversion-constant angle-in-degrees))))
34 (imag-part complex))))
36 (define (buildflag flag-stencil remain curr-stencil spacing)
38 (let* ((translated-stencil (ly:stencil-translate-axis curr-stencil spacing Y))
39 (new-stencil (ly:stencil-add flag-stencil translated-stencil)))
40 (buildflag new-stencil (- remain 1) translated-stencil spacing))
43 (define-public (straight-flag flag-thickness flag-spacing
44 upflag-angle upflag-length
45 downflag-angle downflag-length)
46 "Create a stencil for a straight flag.
47 flag-thickness, -spacing are given in staff spaces,
48 *flag-angle is given in degree,
49 *flag-length is given in staff spaces"
51 (let* ((log (ly:grob-property stem-grob 'duration-log))
52 (layout (ly:grob-layout stem-grob))
53 (stem-up? (eqv? (ly:grob-property stem-grob 'direction) UP))
54 ; scale with the note size (e.g. for grace notes). Default fontsize
55 ; is fs==0, each step is ~12.246% larger / smaller
56 (fs (ly:grob-property stem-grob 'font-size))
57 (factor (if (number? fs) (expt 1.12246 fs) 1))
58 (grob-stem-thickness (ly:grob-property stem-grob 'thickness))
59 (line-thickness (ly:output-def-lookup layout 'line-thickness))
60 (half-stem-thickness (/ (* grob-stem-thickness line-thickness) 2))
61 (up-length (+ (* upflag-length factor) half-stem-thickness))
62 (up-off (polar->rectangular up-length upflag-angle))
63 (down-length (+ (* downflag-length factor) half-stem-thickness))
64 (down-off (polar->rectangular down-length downflag-angle))
65 (thickness (* flag-thickness factor))
66 (offset (cons 0 (if stem-up? (- thickness) thickness)))
67 (spacing (* flag-spacing factor (if stem-up? -1 1)))
68 (start (cons (- half-stem-thickness) (if stem-up? half-stem-thickness (- half-stem-thickness))))
69 (points (if stem-up? (list start up-off
70 (offset-add up-off offset)
71 (offset-add start offset))
73 (offset-add start offset)
74 (offset-add down-off offset)
76 (stencil (ly:round-filled-polygon points half-stem-thickness))
77 ; Log for 1/8 is 3, so we need to subtract 3
78 (flag-stencil (buildflag stencil (- log 3) stencil spacing))
79 (stroke-style (ly:grob-property stem-grob 'stroke-style)))
80 (if (null? stroke-style)
82 (add-stroke-straight flag-stencil stem-grob
85 (if stem-up? up-off down-off)
86 (if stem-up? up-length down-length)
88 (* half-stem-thickness 2))))))
90 ;; Modern straight flags: angles are not as large as in the old style
91 (define-public (modern-straight-flag stem-grob)
92 ((straight-flag 0.55 1 -18 1.1 22 1.2) stem-grob))
94 ;; Old-straight flags (Bach, etc.): quite large flag angles
95 (define-public (old-straight-flag stem-grob)
96 ((straight-flag 0.55 1 -45 1.2 45 1.4) stem-grob))
99 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
100 ;;;; Flags created from feta glyphs (normal and mensural flags)
101 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
104 ; NOTE: By default, lilypond uses the C++ method Stem::calc-flag
105 ; (ly:stem::calc-flag is the corresponding Scheme interface) to generate the
106 ; flag stencil. The following functions are simply a reimplementation in
107 ; Scheme, so that one has that functionality available in Scheme, if one
108 ; wants to write a flag style, which modifies one of the standard flags
109 ; by some stencil operations.
112 (define-public (add-stroke-glyph stencil stem-grob dir stroke-style flag-style)
113 "Load and add a stroke (represented by a glyph in the font) to the given
115 (if (not (string? stroke-style))
117 ; Otherwise: look up the stroke glyph and combine it with the flag
118 (let* ((font-char (string-append "flags." flag-style dir stroke-style))
119 (alt-font-char (string-append "flags." dir stroke-style))
120 (font (ly:grob-default-font stem-grob))
121 (tmpstencil (ly:font-get-glyph font font-char))
122 (stroke-stencil (if (ly:stencil-empty? tmpstencil)
123 (ly:font-get-glyph font alt-font-char)
125 (if (ly:stencil-empty? stroke-stencil)
127 (ly:warning (_ "flag stroke `~a' or `~a' not found") font-char alt-font-char)
129 (ly:stencil-add stencil stroke-stencil)))))
132 (define-public (retrieve-glyph-flag flag-style dir dir-modifier stem-grob)
133 "Load the correct flag glyph from the font"
134 (let* ((log (ly:grob-property stem-grob 'duration-log))
135 (font (ly:grob-default-font stem-grob))
136 (font-char (string-append "flags." flag-style dir dir-modifier (number->string log)))
137 (flag (ly:font-get-glyph font font-char)))
138 (if (ly:stencil-empty? flag)
139 (ly:warning "flag ~a not found" font-char))
143 (define-public (create-glyph-flag flag-style dir-modifier stem-grob)
144 "Create a flag stencil by looking up the glyph from the font"
145 (let* ((dir (if (eqv? (ly:grob-property stem-grob 'direction) UP) "u" "d"))
146 (flag (retrieve-glyph-flag flag-style dir dir-modifier stem-grob))
147 (stroke-style (ly:grob-property stem-grob 'stroke-style)))
148 (if (null? stroke-style)
150 (add-stroke-glyph flag stem-grob dir stroke-style flag-style))))
154 (define-public (mensural-flag stem-grob)
155 "Mensural flags: Create the flag stencil by loading the glyph from the font.
156 Flags are always aligned with staff lines, so we need to check the end point
157 of the stem: For stems ending on staff lines, use different flags than for
158 notes between staff lines. The idea is that flags are always vertically
159 aligned with the staff lines, regardless of whether the note head is on a
160 staff line or between two staff lines. In other words, the inner end of
161 a flag always touches a staff line."
164 (stem-end (inexact->exact (round (ly:grob-property stem-grob 'stem-end-position))))
165 ; For some reason the stem-end is a real instead of an integer...
166 (dir-modifier (if (ly:position-on-line? stem-grob stem-end) "1" "0"))
167 (modifier (if adjust dir-modifier "2")))
168 (create-glyph-flag "mensural" modifier stem-grob)))
172 (define-public ((glyph-flag flag-style) stem-grob)
173 "Simulates the default way of generating flags: look up glyphs
174 flags.style[ud][1234] from the feta font and use it for the flag stencil."
175 (create-glyph-flag flag-style "" stem-grob))
179 (define-public (normal-flag stem-grob)
180 "Create a default flag"
181 (create-glyph-flag "" "" stem-grob))
185 (define-public (default-flag stem-grob)
186 "Create a flag stencil for the stem. Its style will be derived from the
187 @code{'flag-style} Stem property. By default, @code{lilypond} uses a
188 C++ Function (which is slightly faster) to do exactly the same as this
189 function. However, if one wants to modify the default flags, this function
190 can be used to obtain the default flag stencil, which can then be modified
191 at will. The correct way to do this is:
193 \\override Stem #'flag = #default-flag
194 \\override Stem #'flag-style = #'mensural
197 (let* ((flag-style-symbol (ly:grob-property stem-grob 'flag-style))
198 (flag-style (if (symbol? flag-style-symbol)
199 (symbol->string flag-style-symbol)
202 ((equal? flag-style "") (normal-flag stem-grob))
203 ((equal? flag-style "mensural") (mensural-flag stem-grob))
204 ((equal? flag-style "no-flag") (no-flag stem-grob))
205 (else ((glyph-flag flag-style) stem-grob)))))