]> git.donarmstrong.com Git - lilypond.git/blob - scm/flag-styles.scm
Merge branch 'master' into lilypond/translation
[lilypond.git] / scm / flag-styles.scm
1 ;;;;  flag-styles.scm
2 ;;;;
3 ;;;;  source file of the GNU LilyPOnd music typesetter
4 ;;;;
5 ;;;;  This file implements different flag styles in Scheme / GUILE, most
6 ;;;;  notably the old-straight-flag and the modern-straight-flag styles.
7 ;;;;
8
9
10 (define-public (no-flag stem-grob)
11   "No flag: Simply return empty stencil"
12   empty-stencil)
13
14
15 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
16 ;;;;  Straight flags
17 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
18
19
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)))
32
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))
37
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
42                     radius
43                     (degrees->radians angle-in-degrees))))
44      (cons
45        (real-part complex)
46        (imag-part complex))))
47
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."
52   (if (> remain 0)
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))
56       flag-stencil))
57
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."
65   (lambda (stem-grob)
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))
88                                (list start
89                                      (offset-add start thickness-offset)
90                                      (offset-add flag-end thickness-offset)
91                                      flag-end)))
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
98                            dir log
99                            stroke-style
100                            flag-end flag-length
101                            thickness
102                            (* half-stem-thickness 2))
103       flag-stencil))))
104
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
108    of Bach etc."
109   ((straight-flag 0.55 1 -18 1.1 22 1.2) stem-grob))
110
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))
115
116
117 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
118 ;;;;  Flags created from feta glyphs (normal and mensural flags)
119 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
120
121
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.
128
129
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
132    flag stencil"
133   (if (not (string? stroke-style))
134     stencil
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)
142                                tmpstencil)))
143       (if (ly:stencil-empty? stroke-stencil)
144         (begin
145           (ly:warning (_ "flag stroke `~a' or `~a' not found") font-char alt-font-char)
146           stencil)
147         (ly:stencil-add stencil stroke-stencil)))))
148
149
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))
158     flag))
159
160
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)
167       flag
168       (add-stroke-glyph flag stem-grob dir stroke-style flag-style))))
169
170
171
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."
180
181   (let* ((adjust #t)
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)))
187
188
189
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))
194
195
196
197 (define-public (normal-flag stem-grob)
198   "Create a default flag"
199   (create-glyph-flag "" "" stem-grob))
200
201
202
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:
210 @example
211 \\override Stem #'flag = #default-flag
212 \\override Stem #'flag-style = #'mensural
213 @end example
214 "
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)
218                          "")))
219     (cond
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)))))