]> git.donarmstrong.com Git - lilypond.git/blob - lily/stem-tremolo.cc
9f21c466c3e5932764fc2bc29b67988fa5e9694b
[lilypond.git] / lily / stem-tremolo.cc
1 /*
2   This file is part of LilyPond, the GNU music typesetter.
3
4   Copyright (C) 1997--2011 Han-Wen Nienhuys <hanwen@xs4all.nl>
5
6   LilyPond is free software: you can redistribute it and/or modify
7   it under the terms of the GNU General Public License as published by
8   the Free Software Foundation, either version 3 of the License, or
9   (at your option) any later version.
10
11   LilyPond is distributed in the hope that it will be useful,
12   but WITHOUT ANY WARRANTY; without even the implied warranty of
13   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14   GNU General Public License for more details.
15
16   You should have received a copy of the GNU General Public License
17   along with LilyPond.  If not, see <http://www.gnu.org/licenses/>.
18 */
19
20 #include "stem-tremolo.hh"
21
22 #include "spanner.hh"
23 #include "beam.hh"
24 #include "directional-element-interface.hh"
25 #include "item.hh"
26 #include "lookup.hh"
27 #include "output-def.hh"
28 #include "staff-symbol-referencer.hh"
29 #include "stem.hh"
30 #include "warn.hh"
31
32 MAKE_SCHEME_CALLBACK (Stem_tremolo, calc_slope, 1)
33 SCM
34 Stem_tremolo::calc_slope (SCM smob)
35 {
36   Grob *me = unsmob_grob (smob);
37   Grob *stem = unsmob_grob (me->get_object ("stem"));
38   Spanner *beam = Stem::get_beam (stem);
39
40   if (beam)
41     {
42       Real dy = 0;
43       SCM s = beam->get_property ("quantized-positions");
44       if (is_number_pair (s))
45         dy = - scm_to_double (scm_car (s)) + scm_to_double (scm_cdr (s));
46
47       Grob *s2 = Beam::last_normal_stem (beam);
48       Grob *s1 = Beam::first_normal_stem (beam);
49
50       Grob *common = s1->common_refpoint (s2, X_AXIS);
51       Real dx = s2->relative_coordinate (common, X_AXIS)
52                 - s1->relative_coordinate (common, X_AXIS);
53
54       return scm_from_double (dx ? dy / dx : 0);
55     }
56   else
57     /* down stems with flags should have more sloped trems (helps avoid
58        flag/stem collisions without making the stem very long) */
59     return scm_from_double ((Stem::duration_log (stem) >= 3 && get_grob_direction (stem) == DOWN)
60                             ? 0.40 : 0.25);
61 }
62
63 MAKE_SCHEME_CALLBACK (Stem_tremolo, calc_width, 1)
64 SCM
65 Stem_tremolo::calc_width (SCM smob)
66 {
67   Grob *me = unsmob_grob (smob);
68   Grob *stem = unsmob_grob (me->get_object ("stem"));
69   Direction stemdir = get_grob_direction (stem);
70   bool beam = Stem::get_beam (stem);
71   bool flag = Stem::duration_log (stem) >= 3 && !beam;
72
73   /* beamed stems and up-stems with flags have shorter tremolos */
74   return scm_from_double (((stemdir == UP && flag) || beam) ? 1.0 : 1.5);
75 }
76
77 MAKE_SCHEME_CALLBACK (Stem_tremolo, calc_style, 1)
78 SCM
79 Stem_tremolo::calc_style (SCM smob)
80 {
81   Grob *me = unsmob_grob (smob);
82   Grob *stem = unsmob_grob (me->get_object ("stem"));
83   Direction stemdir = get_grob_direction (stem);
84   bool beam = Stem::get_beam (stem);
85   bool flag = Stem::duration_log (stem) >= 3 && !beam;
86
87   return ly_symbol2scm (((stemdir == UP && flag) || beam) ? "rectangle" : "default");
88 }
89
90 Real
91 Stem_tremolo::get_beam_translation (Grob *me)
92 {
93   Grob *stem = unsmob_grob (me->get_object ("stem"));
94   Spanner *beam = Stem::get_beam (stem);
95
96   return (beam && beam->is_live ())
97          ? Beam::get_beam_translation (beam)
98          : (Staff_symbol_referencer::staff_space (me)
99             * robust_scm2double (me->get_property ("length-fraction"), 1.0) * 0.81);
100 }
101
102 Stencil
103 Stem_tremolo::raw_stencil (Grob *me, Real slope, Direction stemdir)
104 {
105   Real ss = Staff_symbol_referencer::staff_space (me);
106   Real thick = robust_scm2double (me->get_property ("beam-thickness"), 1);
107   Real width = robust_scm2double (me->get_property ("beam-width"), 1);
108   Real blot = me->layout ()->get_dimension (ly_symbol2scm ("blot-diameter"));
109   SCM style = me->get_property ("style");
110   if (!scm_is_symbol (style))
111     style = ly_symbol2scm ("default");
112
113   width *= ss;
114   thick *= ss;
115
116   Stencil a;
117   if (style == ly_symbol2scm ("rectangle"))
118     a = Lookup::rotated_box (slope, width, thick, blot);
119   else
120     a = Lookup::beam (slope, width, thick, blot);
121
122   a.align_to (X_AXIS, CENTER);
123   a.align_to (Y_AXIS, CENTER);
124
125   int tremolo_flags = robust_scm2int (me->get_property ("flag-count"), 0);
126   if (!tremolo_flags)
127     {
128       programming_error ("no tremolo flags");
129
130       me->suicide ();
131       return Stencil ();
132     }
133
134   Real beam_translation = get_beam_translation (me);
135
136   Stencil mol;
137   for (int i = 0; i < tremolo_flags; i++)
138     {
139       Stencil b (a);
140       b.translate_axis (beam_translation * i * stemdir * -1, Y_AXIS);
141       mol.add_stencil (b);
142     }
143   return mol;
144 }
145
146 MAKE_SCHEME_CALLBACK (Stem_tremolo, pure_height, 3);
147 SCM
148 Stem_tremolo::pure_height (SCM smob, SCM, SCM)
149 {
150   Item *me = unsmob_item (smob);
151
152   /*
153     Cannot use the real slope, since it looks at the Beam.
154    */
155   Stencil s1 (untranslated_stencil (me, 0.35));
156   Item *stem = unsmob_item (me->get_object ("stem"));
157   if (!stem)
158     return ly_interval2scm (s1.extent (Y_AXIS));
159
160   Direction stemdir = get_grob_direction (stem);
161   if (stemdir == 0)
162     stemdir = UP;
163
164   Spanner *beam = Stem::get_beam (stem);
165
166   if (!beam)
167     return ly_interval2scm (s1.extent (Y_AXIS));
168
169   Interval ph = stem->pure_height (stem, 0, INT_MAX);
170   Stem_info si = Stem::get_stem_info (stem);
171   ph[-stemdir] = si.shortest_y_;
172   int beam_count = Stem::beam_multiplicity (stem).length () + 1;
173   Real beam_translation = get_beam_translation (me);
174
175   ph = ph - stemdir * max (beam_count, 1) * beam_translation;
176   ph = ph - ph.center ();
177
178   return ly_interval2scm (ph);
179 }
180
181 MAKE_SCHEME_CALLBACK (Stem_tremolo, width, 1);
182 SCM
183 Stem_tremolo::width (SCM smob)
184 {
185   Grob *me = unsmob_grob (smob);
186
187   /*
188     Cannot use the real slope, since it looks at the Beam.
189    */
190   Stencil s1 (untranslated_stencil (me, 0.35));
191
192   return ly_interval2scm (s1.extent (X_AXIS));
193 }
194
195 Real
196 Stem_tremolo::vertical_length (Grob *me)
197 {
198   return untranslated_stencil (me, 0.35).extent (Y_AXIS).length ();
199 }
200
201 Stencil
202 Stem_tremolo::untranslated_stencil (Grob *me, Real slope)
203 {
204   Grob *stem = unsmob_grob (me->get_object ("stem"));
205   if (!stem)
206     {
207       programming_error ("no stem for stem-tremolo");
208       return Stencil ();
209     }
210
211   Direction stemdir = get_grob_direction (stem);
212   if (!stemdir)
213     stemdir = UP;
214
215   bool whole_note = Stem::duration_log (stem) <= 0;
216
217   /* for a whole note, we position relative to the notehead, so we want the
218      stencil aligned on the flag closest to the head */
219   Direction stencil_dir = whole_note ? -stemdir : stemdir;
220   return raw_stencil (me, slope, stencil_dir);
221 }
222
223 MAKE_SCHEME_CALLBACK (Stem_tremolo, calc_y_offset, 1);
224 SCM
225 Stem_tremolo::calc_y_offset (SCM smob)
226 {
227   Grob *me = unsmob_grob (smob);
228   return scm_from_double (y_offset (me, false));
229 }
230
231 MAKE_SCHEME_CALLBACK (Stem_tremolo, pure_calc_y_offset, 3);
232 SCM
233 Stem_tremolo::pure_calc_y_offset (SCM smob,
234                                   SCM, /* start */
235                                   SCM /* end */)
236 {
237   Grob *me = unsmob_grob (smob);
238   return scm_from_double (y_offset (me, true));
239 }
240
241 Real
242 Stem_tremolo::y_offset (Grob *me, bool pure)
243 {
244   Item *stem = unsmob_item (me->get_object ("stem"));
245   if (!stem)
246     return 0.0;
247
248   Direction stemdir = get_grob_direction (stem);
249   if (stemdir == 0)
250     stemdir = UP;
251
252   Spanner *beam = Stem::get_beam (stem);
253   Real beam_translation = get_beam_translation (me);
254
255   int beam_count = beam ? (Stem::beam_multiplicity (stem).length () + 1) : 0;
256
257   if (pure && beam)
258     {
259       Interval ph = stem->pure_height (stem, 0, INT_MAX);
260       Stem_info si = Stem::get_stem_info (stem);
261       ph[-stemdir] = si.shortest_y_;
262
263       return (ph - stemdir * max (beam_count, 1) * beam_translation)[stemdir] - stemdir * 0.5 * me->pure_height (me, 0, INT_MAX).length ();
264     }
265
266   Real end_y
267     = stem->extent (stem, Y_AXIS)[stemdir]
268       - stemdir * max (beam_count, 1) * beam_translation
269       - Stem::beam_end_corrective (stem);
270
271   if (!beam && Stem::duration_log (stem) >= 3)
272     {
273       end_y -= stemdir * (Stem::duration_log (stem) - 2) * beam_translation;
274       if (stemdir == UP)
275         end_y -= stemdir * beam_translation * 0.5;
276     }
277
278   bool whole_note = Stem::duration_log (stem) <= 0;
279   if (whole_note)
280     {
281       /* we shouldn't position relative to the end of the stem since the stem
282          is invisible */
283       Real ss = Staff_symbol_referencer::staff_space (me);
284       vector<int> nhp = Stem::note_head_positions (stem);
285       Real note_head = (stemdir == UP ? nhp.back () : nhp[0]) * ss / 2;
286       end_y = note_head + stemdir * 1.5;
287     }
288
289   return end_y;
290 }
291
292 MAKE_SCHEME_CALLBACK (Stem_tremolo, print, 1);
293 SCM
294 Stem_tremolo::print (SCM grob)
295 {
296   Grob *me = unsmob_grob (grob);
297
298   Stencil s = untranslated_stencil (me, robust_scm2double (me->get_property ("slope"), 0.25));
299   return s.smobbed_copy ();
300 }
301
302 ADD_INTERFACE (Stem_tremolo,
303                "A beam slashing a stem to indicate a tremolo.  The property"
304                " @code{style} can be @code{default} or @code{rectangle}.",
305
306                /* properties */
307                "beam-thickness "
308                "beam-width "
309                "flag-count "
310                "length-fraction "
311                "stem "
312                "style "
313                "slope "
314               );