3 ;;;; source file of the GNU LilyPOnd music typesetter
5 ;;;; This file implements different flag styles in Scheme / GUILE, most
6 ;;;; notably the old-straight-flag and the modern-straight-flag styles.
10 (define-public (no-flag stem-grob)
11 "No flag: Simply return empty stencil"
15 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
17 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
20 (define-public (add-stroke-straight stencil stem-grob dir log stroke-style offset length thickness stroke-thickness)
21 "Add the stroke for acciaccatura to the given flag stencil.
22 The stroke starts for up-flags at upper-end-of-flag+(0,length/2) and
23 ends at (0, vertical-center-of-flag-end) - (flag-x-width/2, flag-x-width + flag-thickness).
24 Here length is the whole length, while flag-x-width is just the
25 x-extent and thus depends on the angle! Other combinations don't look as
26 good... For down-stems the y-coordinates are simply mirrored."
27 (let* ((start (offset-add offset (cons 0 (* (/ length 2) dir))))
28 (end (offset-add (cons 0 (cdr offset))
29 (cons (- (/ (car offset) 2)) (* (- (+ thickness (car offset))) dir))))
30 (stroke (make-line-stencil stroke-thickness (car start) (cdr start) (car end) (cdr end))))
31 (ly:stencil-add stencil stroke)))
33 (define PI-OVER-180 (/ (atan 1 1) 45))
34 (define (degrees->radians angle-degrees)
35 "Convert the given angle from degrees to radians"
36 (* angle-degrees PI-OVER-180))
38 (define (polar->rectangular radius angle-in-degrees)
39 "Convert polar coordinate @code{radius} and @code{angle-in-degrees}
40 to (x-length . y-length)"
41 (let* ((complex (make-polar
43 (degrees->radians angle-in-degrees))))
46 (imag-part complex))))
48 (define (buildflag flag-stencil remain curr-stencil spacing)
49 "Internal function to recursively create a stencil with @code{remain} flags
50 from the single-flag stencil curr-stencil, which is already translated to
51 the position of the previous flag position."
53 (let* ((translated-stencil (ly:stencil-translate-axis curr-stencil spacing Y))
54 (new-stencil (ly:stencil-add flag-stencil translated-stencil)))
55 (buildflag new-stencil (- remain 1) translated-stencil spacing))
58 (define-public (straight-flag flag-thickness flag-spacing
59 upflag-angle upflag-length
60 downflag-angle downflag-length)
61 "Create a stencil for a straight flag.
62 flag-thickness, -spacing are given in staff spaces,
63 *flag-angle is given in degree, *flag-length is given in staff spaces.
64 All lengths will be scaled according to the font size of the note."
66 (let* ((log (ly:grob-property stem-grob 'duration-log))
67 (dir (ly:grob-property stem-grob 'direction))
68 (stem-up (eqv? dir UP))
69 (layout (ly:grob-layout stem-grob))
70 ; scale with the note size (e.g. for grace notes)
71 (factor (magstep (ly:grob-property stem-grob 'font-size 0)))
72 (grob-stem-thickness (ly:grob-property stem-grob 'thickness))
73 (line-thickness (ly:output-def-lookup layout 'line-thickness))
74 (half-stem-thickness (/ (* grob-stem-thickness line-thickness) 2))
75 (raw-length (if stem-up upflag-length downflag-length))
76 (angle (if stem-up upflag-angle downflag-angle))
77 (flag-length (+ (* raw-length factor) half-stem-thickness))
78 (flag-end (polar->rectangular flag-length angle))
79 (thickness (* flag-thickness factor))
80 (thickness-offset (cons 0 (* -1 thickness dir)))
81 (spacing (* -1 flag-spacing factor dir ))
82 (start (cons (- half-stem-thickness) (* half-stem-thickness dir)))
83 ; The points of a round-filled-polygon need to be given in clockwise
84 ; order, otherwise the polygon will be enlarged by blot-size*2!
85 (points (if stem-up (list start flag-end
86 (offset-add flag-end thickness-offset)
87 (offset-add start thickness-offset))
89 (offset-add start thickness-offset)
90 (offset-add flag-end thickness-offset)
92 (stencil (ly:round-filled-polygon points half-stem-thickness))
93 ; Log for 1/8 is 3, so we need to subtract 3
94 (flag-stencil (buildflag stencil (- log 3) stencil spacing))
95 (stroke-style (ly:grob-property stem-grob 'stroke-style)))
96 (if (equal? stroke-style "grace")
97 (add-stroke-straight flag-stencil stem-grob
102 (* half-stem-thickness 2))
105 (define-public (modern-straight-flag stem-grob)
106 "Modern straight flag style (for composers like Stockhausen, Boulez, etc.).
107 The angles are 18 and 22 degrees and thus smaller than for the ancient style
109 ((straight-flag 0.55 1 -18 1.1 22 1.2) stem-grob))
111 (define-public (old-straight-flag stem-grob)
112 "Old straight flag style (for composers like Bach). The angles of the flags
113 are both 45 degrees."
114 ((straight-flag 0.55 1 -45 1.2 45 1.4) stem-grob))
117 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
118 ;;;; Flags created from feta glyphs (normal and mensural flags)
119 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
122 ; NOTE: By default, lilypond uses the C++ method Stem::calc-flag
123 ; (ly:stem::calc-flag is the corresponding Scheme interface) to generate the
124 ; flag stencil. The following functions are simply a reimplementation in
125 ; Scheme, so that one has that functionality available in Scheme, if one
126 ; wants to write a flag style, which modifies one of the standard flags
127 ; by some stencil operations.
130 (define-public (add-stroke-glyph stencil stem-grob dir stroke-style flag-style)
131 "Load and add a stroke (represented by a glyph in the font) to the given
133 (if (not (string? stroke-style))
135 ; Otherwise: look up the stroke glyph and combine it with the flag
136 (let* ((font-char (string-append "flags." flag-style dir stroke-style))
137 (alt-font-char (string-append "flags." dir stroke-style))
138 (font (ly:grob-default-font stem-grob))
139 (tmpstencil (ly:font-get-glyph font font-char))
140 (stroke-stencil (if (ly:stencil-empty? tmpstencil)
141 (ly:font-get-glyph font alt-font-char)
143 (if (ly:stencil-empty? stroke-stencil)
145 (ly:warning (_ "flag stroke `~a' or `~a' not found") font-char alt-font-char)
147 (ly:stencil-add stencil stroke-stencil)))))
150 (define-public (retrieve-glyph-flag flag-style dir dir-modifier stem-grob)
151 "Load the correct flag glyph from the font"
152 (let* ((log (ly:grob-property stem-grob 'duration-log))
153 (font (ly:grob-default-font stem-grob))
154 (font-char (string-append "flags." flag-style dir dir-modifier (number->string log)))
155 (flag (ly:font-get-glyph font font-char)))
156 (if (ly:stencil-empty? flag)
157 (ly:warning "flag ~a not found" font-char))
161 (define-public (create-glyph-flag flag-style dir-modifier stem-grob)
162 "Create a flag stencil by looking up the glyph from the font"
163 (let* ((dir (if (eqv? (ly:grob-property stem-grob 'direction) UP) "u" "d"))
164 (flag (retrieve-glyph-flag flag-style dir dir-modifier stem-grob))
165 (stroke-style (ly:grob-property stem-grob 'stroke-style)))
166 (if (null? stroke-style)
168 (add-stroke-glyph flag stem-grob dir stroke-style flag-style))))
172 (define-public (mensural-flag stem-grob)
173 "Mensural flags: Create the flag stencil by loading the glyph from the font.
174 Flags are always aligned with staff lines, so we need to check the end point
175 of the stem: For stems ending on staff lines, use different flags than for
176 notes between staff lines. The idea is that flags are always vertically
177 aligned with the staff lines, regardless of whether the note head is on a
178 staff line or between two staff lines. In other words, the inner end of
179 a flag always touches a staff line."
182 (stem-end (inexact->exact (round (ly:grob-property stem-grob 'stem-end-position))))
183 ; For some reason the stem-end is a real instead of an integer...
184 (dir-modifier (if (ly:position-on-line? stem-grob stem-end) "1" "0"))
185 (modifier (if adjust dir-modifier "2")))
186 (create-glyph-flag "mensural" modifier stem-grob)))
190 (define-public ((glyph-flag flag-style) stem-grob)
191 "Simulates the default way of generating flags: look up glyphs
192 flags.style[ud][1234] from the feta font and use it for the flag stencil."
193 (create-glyph-flag flag-style "" stem-grob))
197 (define-public (normal-flag stem-grob)
198 "Create a default flag"
199 (create-glyph-flag "" "" stem-grob))
203 (define-public (default-flag stem-grob)
204 "Create a flag stencil for the stem. Its style will be derived from the
205 @code{'flag-style} Stem property. By default, @code{lilypond} uses a
206 C++ Function (which is slightly faster) to do exactly the same as this
207 function. However, if one wants to modify the default flags, this function
208 can be used to obtain the default flag stencil, which can then be modified
209 at will. The correct way to do this is:
211 \\override Stem #'flag = #default-flag
212 \\override Stem #'flag-style = #'mensural
215 (let* ((flag-style-symbol (ly:grob-property stem-grob 'flag-style))
216 (flag-style (if (symbol? flag-style-symbol)
217 (symbol->string flag-style-symbol)
220 ((equal? flag-style "") (normal-flag stem-grob))
221 ((equal? flag-style "mensural") (mensural-flag stem-grob))
222 ((equal? flag-style "no-flag") (no-flag stem-grob))
223 (else ((glyph-flag flag-style) stem-grob)))))