]> git.donarmstrong.com Git - lilypond.git/blob - scm/flag-styles.scm
c8fea1ae5d8e655ee5d61701f61b7b577072dc47
[lilypond.git] / scm / flag-styles.scm
1 ;;;; This file is part of LilyPond, the GNU music typesetter.
2 ;;;;
3 ;;;; Copyright (C) 2008--2012 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 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 grob dir log stroke-style
33                                     offset length thickness stroke-thickness)
34   "Add the stroke for acciaccatura to the given flag stencil.
35 The stroke starts for up-flags at `upper-end-of-flag + (0,length/2)'
36 and ends at `(0, vertical-center-of-flag-end) - (flag-x-width/2,
37 flag-x-width + flag-thickness)'.  Here `length' is the
38 whole length, while `flag-x-width' is just the x-extent and thus depends on
39 the angle!  Other combinations don't look as good.
40
41 For down-stems the y-coordinates are simply mirrored."
42   (let* ((stem-grob (ly:grob-parent grob X))
43          (start (offset-add offset (cons 0  (* (/ length 2) dir))))
44          (end (offset-add (cons 0 (cdr offset))
45                           (cons (- (/ (car offset) 2)) (* (- (+ thickness (car offset))) dir))))
46          (stroke (make-line-stencil stroke-thickness (car start) (cdr start) (car end) (cdr end))))
47     (ly:stencil-add stencil stroke)))
48
49 (define (buildflag flag-stencil remain curr-stencil spacing)
50   "Internal function to recursively create a stencil with @code{remain} flags
51    from the single-flag stencil curr-stencil, which is already translated to
52    the position of the previous flag position."
53   (if (> remain 0)
54       (let* ((translated-stencil (ly:stencil-translate-axis curr-stencil spacing Y))
55              (new-stencil (ly:stencil-add flag-stencil translated-stencil)))
56         (buildflag new-stencil (- remain 1) translated-stencil spacing))
57       flag-stencil))
58
59 (define-public (straight-flag flag-thickness flag-spacing
60                               upflag-angle upflag-length
61                               downflag-angle downflag-length)
62   "Create a stencil for a straight flag.  @var{flag-thickness} and
63 @var{flag-spacing} are given in staff spaces, @var{upflag-angle} and
64 @var{downflag-angle} are given in degrees, and @var{upflag-length} and
65 @var{downflag-length} are given in staff spaces.
66
67 All lengths are scaled according to the font size of the note."
68
69   (lambda (grob)
70     (let* ((stem-grob (ly:grob-parent grob X))
71            (log (ly:grob-property stem-grob 'duration-log))
72            (dir (ly:grob-property stem-grob 'direction))
73            (stem-up (eqv? dir UP))
74            (layout (ly:grob-layout grob))
75            ;; scale with the note size (e.g. for grace notes)
76            (factor (magstep (ly:grob-property grob 'font-size 0)))
77            (grob-stem-thickness (ly:grob-property stem-grob 'thickness))
78            (line-thickness (ly:output-def-lookup layout 'line-thickness))
79            (half-stem-thickness (/ (* grob-stem-thickness line-thickness) 2))
80            (raw-length (if stem-up upflag-length downflag-length))
81            (angle (if stem-up upflag-angle downflag-angle))
82            (flag-length (+ (* raw-length factor) half-stem-thickness))
83            (flag-end (polar->rectangular flag-length angle))
84            (thickness (* flag-thickness factor))
85            (thickness-offset (cons 0 (* -1 thickness dir)))
86            (spacing (* -1 flag-spacing factor dir ))
87            (start (cons (- half-stem-thickness) (* half-stem-thickness dir)))
88            ;; The points of a round-filled-polygon need to be given in clockwise
89            ;; order, otherwise the polygon will be enlarged by blot-size*2!
90            (points (if stem-up (list start flag-end
91                                      (offset-add flag-end thickness-offset)
92                                      (offset-add start thickness-offset))
93                        (list start
94                              (offset-add start thickness-offset)
95                              (offset-add flag-end thickness-offset)
96                              flag-end)))
97            (stencil (ly:round-filled-polygon points half-stem-thickness))
98            ;; Log for 1/8 is 3, so we need to subtract 3
99            (flag-stencil (buildflag stencil (- log 3) stencil spacing))
100            (stroke-style (ly:grob-property grob 'stroke-style)))
101       (if (equal? stroke-style "grace")
102           (add-stroke-straight flag-stencil grob
103                                dir log
104                                stroke-style
105                                flag-end flag-length
106                                thickness
107                                (* half-stem-thickness 2))
108           flag-stencil))))
109
110 (define-public (modern-straight-flag grob)
111   "Modern straight flag style (for composers like Stockhausen, Boulez, etc.).
112 The angles are 18 and 22 degrees and thus smaller than for the ancient style
113 of Bach, etc."
114   ((straight-flag 0.55 1 -18 1.1 22 1.2) grob))
115
116 (define-public (old-straight-flag grob)
117   "Old straight flag style (for composers like Bach).  The angles of the
118 flags are both 45 degrees."
119   ((straight-flag 0.55 1 -45 1.2 45 1.4) grob))
120
121
122 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
123 ;;;;  Flags created from feta glyphs (normal and mensural flags)
124 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
125
126
127 ;; NOTE: By default, lilypond uses the C++ method Flag::stencil
128 ;; (ly:flag::stencil is the corresponding Scheme interface) to generate the
129 ;; flag stencil. The following functions are simply a reimplementation in
130 ;; Scheme, so that one has that functionality available in Scheme, if one
131 ;; wants to write a flag style, which modifies one of the standard flags
132 ;; by some stencil operations.
133
134
135 (define-public (add-stroke-glyph stencil grob dir stroke-style flag-style)
136   "Load and add a stroke (represented by a glyph in the font) to the given
137 flag stencil."
138   (if (not (string? stroke-style))
139       stencil
140       ;; Otherwise: look up the stroke glyph and combine it with the flag
141       (let* ((stem-grob (ly:grob-parent grob X))
142              (font-char (string-append "flags." flag-style dir stroke-style))
143              (alt-font-char (string-append "flags." dir stroke-style))
144              (font (ly:grob-default-font grob))
145              (tmpstencil (ly:font-get-glyph font font-char))
146              (stroke-stencil (if (ly:stencil-empty? tmpstencil)
147                                  (ly:font-get-glyph font alt-font-char)
148                                  tmpstencil)))
149         (if (ly:stencil-empty? stroke-stencil)
150             (begin
151               (ly:warning (_ "flag stroke `~a' or `~a' not found") font-char alt-font-char)
152               stencil)
153             (ly:stencil-add stencil stroke-stencil)))))
154
155
156 (define-public (retrieve-glyph-flag flag-style dir dir-modifier grob)
157   "Load the correct flag glyph from the font."
158   (let* ((stem-grob (ly:grob-parent grob X))
159          (log (ly:grob-property stem-grob 'duration-log))
160          (font (ly:grob-default-font grob))
161          (font-char (string-append "flags." flag-style dir dir-modifier (number->string log)))
162          (flag (ly:font-get-glyph font font-char)))
163     (if (ly:stencil-empty? flag)
164         (ly:warning "flag ~a not found" font-char))
165     flag))
166
167
168 (define-public (create-glyph-flag flag-style dir-modifier grob)
169   "Create a flag stencil by looking up the glyph from the font."
170   (let* ((stem-grob (ly:grob-parent grob X))
171          (dir (if (eqv? (ly:grob-property stem-grob 'direction) UP) "u" "d"))
172          (flag (retrieve-glyph-flag flag-style dir dir-modifier grob))
173          (stroke-style (ly:grob-property grob 'stroke-style)))
174     (if (null? stroke-style)
175         flag
176         (add-stroke-glyph flag grob dir stroke-style flag-style))))
177
178
179
180 (define-public (mensural-flag grob)
181   "Mensural flags: Create the flag stencil by loading the glyph from the font.
182 Flags are always aligned with staff lines, so we need to check the end point
183 of the stem: For stems ending on staff lines, use different flags than for
184 notes between staff lines.  The idea is that flags are always vertically
185 aligned with the staff lines, regardless of whether the note head is on a
186 staff line or between two staff lines.  In other words, the inner end of
187 a flag always touches a staff line."
188
189   (let* ((stem-grob (ly:grob-parent grob X))
190          (adjust #t)
191          (d (ly:grob-property stem-grob 'direction))
192          (ss (ly:staff-symbol-staff-space stem-grob))
193          (stem-end (inexact->exact (round (* (index-cell
194                                               (ly:grob-extent stem-grob
195                                                               stem-grob
196                                                               Y)
197                                               d)
198                                              (/ 2 ss)))))
199          ;; For some reason the stem-end is a real instead of an integer...
200          (dir-modifier (if (ly:position-on-line? stem-grob stem-end) "1" "0"))
201          (modifier (if adjust dir-modifier "2")))
202     (create-glyph-flag "mensural" modifier grob)))
203
204
205
206 (define-public ((glyph-flag flag-style) grob)
207   "Simulatesthe default way of generating flags: Look up glyphs
208 @code{flags.style[ud][1234]} from the feta font and use it for the flag
209 stencil."
210   (create-glyph-flag flag-style "" grob))
211
212
213
214 (define-public (normal-flag grob)
215   "Create a default flag."
216   (create-glyph-flag "" "" grob))
217
218
219
220 (define-public (default-flag grob)
221   "Create a flag stencil for the stem.  Its style will be derived from the
222 @code{'style} Flag property.  By default, @code{lilypond} uses a
223 C++ Function (which is slightly faster) to do exactly the same as this
224 function.  However, if one wants to modify the default flags, this function
225 can be used to obtain the default flag stencil, which can then be modified
226 at will.  The correct way to do this is:
227
228 @example
229 \\override Flag #'stencil = #default-flag
230 \\override Flag #'style = #'mensural
231 @end example
232 "
233   (let* ((stem-grob (ly:grob-parent grob X))
234          (flag-style-symbol (ly:grob-property grob 'style))
235          (flag-style (if (symbol? flag-style-symbol)
236                          (symbol->string flag-style-symbol)
237                          "")))
238     (cond
239      ((equal? flag-style "") (normal-flag grob))
240      ((equal? flag-style "mensural") (mensural-flag grob))
241      ((equal? flag-style "no-flag") (no-flag grob))
242      (else ((glyph-flag flag-style) grob)))))