]> git.donarmstrong.com Git - lilypond.git/blob - scm/flag-styles.scm
Add woodwind fingering diagrams
[lilypond.git] / scm / flag-styles.scm
1 ;;;; This file is part of LilyPond, the GNU music typesetter.
2 ;;;;
3 ;;;; Copyright (C) 2008--2010 Reinhold Kainhofer <reinhold@kainhofer.com>
4 ;;;;
5 ;;;; LilyPond is free software: you can redistribute it and/or modify
6 ;;;; it under the terms of the GNU General Public License as published by
7 ;;;; the Free Software Foundation, either version 3 of the License, or
8 ;;;; (at your option) any later version.
9 ;;;;
10 ;;;; LilyPond is distributed in the hope that it will be useful,
11 ;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
12 ;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 ;;;; GNU General Public License for more details.
14 ;;;;
15 ;;;; You should have received a copy of the GNU General Public License
16 ;;;; along with LilyPond.  If not, see <http://www.gnu.org/licenses/>.
17
18 ;;;;  This file implements different flag styles in Scheme / GUILE, most
19 ;;;;  notably the old-straight-flag and the modern-straight-flag styles.
20
21
22 (define-public (no-flag stem-grob)
23   "No flag: Simply return empty stencil"
24   empty-stencil)
25
26
27 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
28 ;;;;  Straight flags
29 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
30
31
32 (define-public (add-stroke-straight stencil stem-grob dir log stroke-style offset length thickness stroke-thickness)
33   "Add the stroke for acciaccatura to the given flag stencil.
34   The stroke starts for up-flags at upper-end-of-flag+(0,length/2) and
35   ends at (0, vertical-center-of-flag-end) - (flag-x-width/2, flag-x-width + flag-thickness).
36   Here length is the whole length, while flag-x-width is just the
37   x-extent and thus depends on the angle! Other combinations don't look as
38   good... For down-stems the y-coordinates are simply mirrored."
39   (let* ((start (offset-add offset (cons 0  (* (/ length 2) dir))))
40          (end (offset-add (cons 0 (cdr offset))
41                           (cons (- (/ (car offset) 2)) (* (- (+ thickness (car offset))) dir))))
42          (stroke (make-line-stencil stroke-thickness (car start) (cdr start) (car end) (cdr end))))
43   (ly:stencil-add stencil stroke)))
44
45 (define (buildflag flag-stencil remain curr-stencil spacing)
46   "Internal function to recursively create a stencil with @code{remain} flags
47    from the single-flag stencil curr-stencil, which is already translated to
48    the position of the previous flag position."
49   (if (> remain 0)
50       (let* ((translated-stencil (ly:stencil-translate-axis curr-stencil spacing Y))
51              (new-stencil (ly:stencil-add flag-stencil translated-stencil)))
52         (buildflag new-stencil (- remain 1) translated-stencil spacing))
53       flag-stencil))
54
55 (define-public (straight-flag flag-thickness flag-spacing
56                        upflag-angle upflag-length
57                        downflag-angle downflag-length)
58     "Create a stencil for a straight flag.
59      flag-thickness, -spacing are given in staff spaces,
60      *flag-angle is given in degree, *flag-length is given in staff spaces.
61      All lengths will be scaled according to the font size of the note."
62   (lambda (stem-grob)
63     (let* ((log (ly:grob-property stem-grob 'duration-log))
64            (dir (ly:grob-property stem-grob 'direction))
65            (stem-up (eqv? dir UP))
66            (layout (ly:grob-layout stem-grob))
67            ; scale with the note size (e.g. for grace notes)
68            (factor (magstep (ly:grob-property stem-grob 'font-size 0)))
69            (grob-stem-thickness (ly:grob-property stem-grob 'thickness))
70            (line-thickness (ly:output-def-lookup layout 'line-thickness))
71            (half-stem-thickness (/ (* grob-stem-thickness line-thickness) 2))
72            (raw-length (if stem-up upflag-length downflag-length))
73            (angle (if stem-up upflag-angle downflag-angle))
74            (flag-length (+ (* raw-length factor) half-stem-thickness))
75            (flag-end (polar->rectangular flag-length angle))
76            (thickness (* flag-thickness factor))
77            (thickness-offset (cons 0 (* -1 thickness dir)))
78            (spacing (* -1 flag-spacing factor dir ))
79            (start (cons (- half-stem-thickness) (* half-stem-thickness dir)))
80            ; The points of a round-filled-polygon need to be given in clockwise
81            ; order, otherwise the polygon will be enlarged by blot-size*2!
82            (points (if stem-up (list start flag-end
83                                      (offset-add flag-end thickness-offset)
84                                      (offset-add start thickness-offset))
85                                (list start
86                                      (offset-add start thickness-offset)
87                                      (offset-add flag-end thickness-offset)
88                                      flag-end)))
89            (stencil (ly:round-filled-polygon points half-stem-thickness))
90            ; Log for 1/8 is 3, so we need to subtract 3
91            (flag-stencil (buildflag stencil (- log 3) stencil spacing))
92            (stroke-style (ly:grob-property stem-grob 'stroke-style)))
93     (if (equal? stroke-style "grace")
94       (add-stroke-straight flag-stencil stem-grob
95                            dir log
96                            stroke-style
97                            flag-end flag-length
98                            thickness
99                            (* half-stem-thickness 2))
100       flag-stencil))))
101
102 (define-public (modern-straight-flag stem-grob)
103   "Modern straight flag style (for composers like Stockhausen, Boulez, etc.).
104    The angles are 18 and 22 degrees and thus smaller than for the ancient style
105    of Bach etc."
106   ((straight-flag 0.55 1 -18 1.1 22 1.2) stem-grob))
107
108 (define-public (old-straight-flag stem-grob)
109   "Old straight flag style (for composers like Bach). The angles of the flags
110    are both 45 degrees."
111   ((straight-flag 0.55 1 -45 1.2 45 1.4) stem-grob))
112
113
114 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
115 ;;;;  Flags created from feta glyphs (normal and mensural flags)
116 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
117
118
119 ; NOTE: By default, lilypond uses the C++ method Stem::calc-flag
120 ; (ly:stem::calc-flag is the corresponding Scheme interface) to generate the
121 ; flag stencil. The following functions are simply a reimplementation in
122 ; Scheme, so that one has that functionality available in Scheme, if one
123 ; wants to write a flag style, which modifies one of the standard flags
124 ; by some stencil operations.
125
126
127 (define-public (add-stroke-glyph stencil stem-grob dir stroke-style flag-style)
128   "Load and add a stroke (represented by a glyph in the font) to the given
129    flag stencil"
130   (if (not (string? stroke-style))
131     stencil
132     ; Otherwise: look up the stroke glyph and combine it with the flag
133     (let* ((font-char (string-append "flags." flag-style dir stroke-style))
134            (alt-font-char (string-append "flags." dir stroke-style))
135            (font (ly:grob-default-font stem-grob))
136            (tmpstencil (ly:font-get-glyph font font-char))
137            (stroke-stencil (if (ly:stencil-empty? tmpstencil)
138                                (ly:font-get-glyph font alt-font-char)
139                                tmpstencil)))
140       (if (ly:stencil-empty? stroke-stencil)
141         (begin
142           (ly:warning (_ "flag stroke `~a' or `~a' not found") font-char alt-font-char)
143           stencil)
144         (ly:stencil-add stencil stroke-stencil)))))
145
146
147 (define-public (retrieve-glyph-flag flag-style dir dir-modifier stem-grob)
148   "Load the correct flag glyph from the font"
149   (let* ((log (ly:grob-property stem-grob 'duration-log))
150          (font (ly:grob-default-font stem-grob))
151          (font-char (string-append "flags." flag-style dir dir-modifier (number->string log)))
152          (flag (ly:font-get-glyph font font-char)))
153     (if (ly:stencil-empty? flag)
154       (ly:warning "flag ~a not found" font-char))
155     flag))
156
157
158 (define-public (create-glyph-flag flag-style dir-modifier stem-grob)
159   "Create a flag stencil by looking up the glyph from the font"
160   (let* ((dir (if (eqv? (ly:grob-property stem-grob 'direction) UP) "u" "d"))
161          (flag (retrieve-glyph-flag flag-style dir dir-modifier stem-grob))
162          (stroke-style (ly:grob-property stem-grob 'stroke-style)))
163     (if (null? stroke-style)
164       flag
165       (add-stroke-glyph flag stem-grob dir stroke-style flag-style))))
166
167
168
169 (define-public (mensural-flag stem-grob)
170   "Mensural flags: Create the flag stencil by loading the glyph from the font.
171    Flags are always aligned with staff lines, so we need to check the end point
172    of the stem: For stems ending on staff lines, use different flags than for
173    notes between staff lines.  The idea is that flags are always vertically
174    aligned with the staff lines, regardless of whether the note head is on a
175    staff line or between two staff lines.  In other words, the inner end of
176    a flag always touches a staff line."
177
178   (let* ((adjust #t)
179          (stem-end (inexact->exact (round (ly:grob-property stem-grob 'stem-end-position))))
180          ; For some reason the stem-end is a real instead of an integer...
181          (dir-modifier (if (ly:position-on-line? stem-grob stem-end) "1" "0"))
182          (modifier (if adjust dir-modifier "2")))
183     (create-glyph-flag "mensural" modifier stem-grob)))
184
185
186
187 (define-public ((glyph-flag flag-style) stem-grob)
188   "Simulates the default way of generating flags: look up glyphs
189    flags.style[ud][1234] from the feta font and use it for the flag stencil."
190   (create-glyph-flag flag-style "" stem-grob))
191
192
193
194 (define-public (normal-flag stem-grob)
195   "Create a default flag"
196   (create-glyph-flag "" "" stem-grob))
197
198
199
200 (define-public (default-flag stem-grob)
201   "Create a flag stencil for the stem. Its style will be derived from the
202    @code{'flag-style} Stem property. By default, @code{lilypond} uses a
203    C++ Function (which is slightly faster) to do exactly the same as this
204    function. However, if one wants to modify the default flags, this function
205    can be used to obtain the default flag stencil, which can then be modified
206    at will. The correct way to do this is:
207 @example
208 \\override Stem #'flag = #default-flag
209 \\override Stem #'flag-style = #'mensural
210 @end example
211 "
212   (let* ((flag-style-symbol (ly:grob-property stem-grob 'flag-style))
213          (flag-style (if (symbol? flag-style-symbol)
214                          (symbol->string flag-style-symbol)
215                          "")))
216     (cond
217         ((equal? flag-style "") (normal-flag stem-grob))
218         ((equal? flag-style "mensural") (mensural-flag stem-grob))
219         ((equal? flag-style "no-flag") (no-flag stem-grob))
220         (else ((glyph-flag flag-style) stem-grob)))))