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