]> git.donarmstrong.com Git - lilypond.git/blob - lily/stem-tremolo.cc
1c44a67924b2e13319ac58a443aa3e6322201de0
[lilypond.git] / lily / stem-tremolo.cc
1 /*
2   This file is part of LilyPond, the GNU music typesetter.
3
4   Copyright (C) 1997--2015 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 "note-collision.hh"
28 #include "note-column.hh"
29 #include "output-def.hh"
30 #include "staff-symbol-referencer.hh"
31 #include "stem.hh"
32 #include "warn.hh"
33
34 using std::vector;
35
36 MAKE_SCHEME_CALLBACK (Stem_tremolo, calc_slope, 1)
37 SCM
38 Stem_tremolo::calc_slope (SCM smob)
39 {
40   Grob *me = unsmob<Grob> (smob);
41   Grob *stem = unsmob<Grob> (me->get_object ("stem"));
42   Spanner *beam = Stem::get_beam (stem);
43
44   SCM style = me->get_property ("style");
45
46   if (beam && !scm_is_eq (style, ly_symbol2scm ("constant")))
47     {
48       Real dy = 0;
49       SCM s = beam->get_property ("quantized-positions");
50       if (is_number_pair (s))
51         dy = - scm_to_double (scm_car (s)) + scm_to_double (scm_cdr (s));
52
53       Grob *s2 = Beam::last_normal_stem (beam);
54       Grob *s1 = Beam::first_normal_stem (beam);
55
56       Grob *common = s1->common_refpoint (s2, X_AXIS);
57       Real dx = s2->relative_coordinate (common, X_AXIS)
58                 - s1->relative_coordinate (common, X_AXIS);
59
60       return scm_from_double (dx ? dy / dx : 0);
61     }
62   else
63     /* down stems with flags should have more sloped trems (helps avoid
64        flag/stem collisions without making the stem very long) */
65     return scm_from_double ((Stem::duration_log (stem) >= 3
66                              && get_grob_direction (me) == DOWN && !beam)
67                             ? 0.40 : 0.25);
68 }
69
70 MAKE_SCHEME_CALLBACK (Stem_tremolo, calc_width, 1)
71 SCM
72 Stem_tremolo::calc_width (SCM smob)
73 {
74   Grob *me = unsmob<Grob> (smob);
75   Grob *stem = unsmob<Grob> (me->get_object ("stem"));
76   Direction dir = get_grob_direction (me);
77   bool beam = Stem::get_beam (stem);
78   bool flag = Stem::duration_log (stem) >= 3 && !beam;
79
80   /* beamed stems and up-stems with flags have shorter tremolos */
81   return scm_from_double (((dir == UP && flag) || beam) ? 1.0 : 1.5);
82 }
83
84 MAKE_SCHEME_CALLBACK (Stem_tremolo, calc_shape, 1)
85 SCM
86 Stem_tremolo::calc_shape (SCM smob)
87 {
88   Grob *me = unsmob<Grob> (smob);
89   Grob *stem = unsmob<Grob> (me->get_object ("stem"));
90   Direction dir = get_grob_direction (me);
91   bool beam = Stem::get_beam (stem);
92   bool flag = Stem::duration_log (stem) >= 3 && !beam;
93   SCM style = me->get_property ("style");
94
95   return ly_symbol2scm (!scm_is_eq (style, ly_symbol2scm ("constant"))
96                         && ((dir == UP && flag) || beam)
97                         ? "rectangle" : "beam-like");
98 }
99
100 Real
101 Stem_tremolo::get_beam_translation (Grob *me)
102 {
103   Grob *stem = unsmob<Grob> (me->get_object ("stem"));
104   Spanner *beam = Stem::get_beam (stem);
105
106   return (beam && beam->is_live ())
107          ? Beam::get_beam_translation (beam)
108          : (Staff_symbol_referencer::staff_space (me)
109             * robust_scm2double (me->get_property ("length-fraction"), 1.0) * 0.81);
110 }
111
112 Stencil
113 Stem_tremolo::raw_stencil (Grob *me, Real slope, Direction dir)
114 {
115   Real ss = Staff_symbol_referencer::staff_space (me);
116   Real thick = robust_scm2double (me->get_property ("beam-thickness"), 1);
117   Real width = robust_scm2double (me->get_property ("beam-width"), 1);
118   Real blot = me->layout ()->get_dimension (ly_symbol2scm ("blot-diameter"));
119   SCM shape = me->get_property ("shape");
120   if (!scm_is_symbol (shape))
121     shape = ly_symbol2scm ("beam-like");
122
123   width *= ss;
124   thick *= ss;
125
126   Stencil a;
127   if (scm_is_eq (shape, ly_symbol2scm ("rectangle")))
128     a = Lookup::rotated_box (slope, width, thick, blot);
129   else
130     a = Lookup::beam (slope, width, thick, blot);
131
132   a.align_to (X_AXIS, CENTER);
133   a.align_to (Y_AXIS, CENTER);
134
135   int tremolo_flags = robust_scm2int (me->get_property ("flag-count"), 0);
136   if (!tremolo_flags)
137     {
138       programming_error ("no tremolo flags");
139
140       me->suicide ();
141       return Stencil ();
142     }
143
144   Real beam_translation = get_beam_translation (me);
145
146   Stencil mol;
147   for (int i = 0; i < tremolo_flags; i++)
148     {
149       Stencil b (a);
150       b.translate_axis (beam_translation * i * dir * -1, Y_AXIS);
151       mol.add_stencil (b);
152     }
153   return mol;
154 }
155
156 MAKE_SCHEME_CALLBACK (Stem_tremolo, pure_height, 3);
157 SCM
158 Stem_tremolo::pure_height (SCM smob, SCM, SCM)
159 {
160   Item *me = unsmob<Item> (smob);
161
162   /*
163     Cannot use the real slope, since it looks at the Beam.
164    */
165   Stencil s1 (untranslated_stencil (me, 0.35));
166   Item *stem = unsmob<Item> (me->get_object ("stem"));
167   if (!stem)
168     return ly_interval2scm (s1.extent (Y_AXIS));
169
170   Direction dir = get_grob_direction (me);
171
172   Spanner *beam = Stem::get_beam (stem);
173
174   if (!beam)
175     return ly_interval2scm (s1.extent (Y_AXIS));
176
177   Interval ph = stem->pure_y_extent (stem, 0, INT_MAX);
178   Stem_info si = Stem::get_stem_info (stem);
179   ph[-dir] = si.shortest_y_;
180   int beam_count = Stem::beam_multiplicity (stem).length () + 1;
181   Real beam_translation = get_beam_translation (me);
182
183   ph = ph - dir * std::max (beam_count, 1) * beam_translation;
184   ph = ph - ph.center ();
185
186   return ly_interval2scm (ph);
187 }
188
189 MAKE_SCHEME_CALLBACK (Stem_tremolo, width, 1);
190 SCM
191 Stem_tremolo::width (SCM smob)
192 {
193   Grob *me = unsmob<Grob> (smob);
194
195   /*
196     Cannot use the real slope, since it looks at the Beam.
197    */
198   Stencil s1 (untranslated_stencil (me, 0.35));
199
200   return ly_interval2scm (s1.extent (X_AXIS));
201 }
202
203 Real
204 Stem_tremolo::vertical_length (Grob *me)
205 {
206   return untranslated_stencil (me, 0.35).extent (Y_AXIS).length ();
207 }
208
209 Stencil
210 Stem_tremolo::untranslated_stencil (Grob *me, Real slope)
211 {
212   Grob *stem = unsmob<Grob> (me->get_object ("stem"));
213   if (!stem)
214     {
215       programming_error ("no stem for stem-tremolo");
216       return Stencil ();
217     }
218
219   Direction dir = get_grob_direction (me);
220
221   bool whole_note = Stem::duration_log (stem) <= 0;
222
223   /* for a whole note, we position relative to the notehead, so we want the
224      stencil aligned on the flag closest to the head */
225   Direction stencil_dir = whole_note ? -dir : dir;
226   return raw_stencil (me, slope, stencil_dir);
227 }
228
229 MAKE_SCHEME_CALLBACK (Stem_tremolo, calc_y_offset, 1);
230 SCM
231 Stem_tremolo::calc_y_offset (SCM smob)
232 {
233   Grob *me = unsmob<Grob> (smob);
234   return scm_from_double (y_offset (me, false));
235 }
236
237 MAKE_SCHEME_CALLBACK (Stem_tremolo, pure_calc_y_offset, 3);
238 SCM
239 Stem_tremolo::pure_calc_y_offset (SCM smob,
240                                   SCM, /* start */
241                                   SCM /* end */)
242 {
243   Grob *me = unsmob<Grob> (smob);
244   return scm_from_double (y_offset (me, true));
245 }
246
247 MAKE_SCHEME_CALLBACK (Stem_tremolo, calc_direction, 1);
248 SCM
249 Stem_tremolo::calc_direction (SCM smob)
250 {
251   Item *me = unsmob<Item> (smob);
252
253   Item *stem = unsmob<Item> (me->get_object ("stem"));
254   if (!stem)
255     return scm_from_int (CENTER);
256
257   Direction stemdir = get_grob_direction (stem);
258
259   vector<int> nhp = Stem::note_head_positions (stem);
260   /*
261    * We re-decide stem-dir if there may be collisions with other
262    * note heads in the staff.
263    */
264   Grob *maybe_nc = stem->get_parent (X_AXIS)->get_parent (X_AXIS);
265   bool whole_note = Stem::duration_log (stem) <= 0;
266   if (whole_note && has_interface<Note_collision_interface> (maybe_nc))
267     {
268       Drul_array<bool> avoid_me (false, false);
269       vector<int> all_nhps = Note_collision_interface::note_head_positions (maybe_nc);
270       if (all_nhps[0] < nhp[0])
271         avoid_me[DOWN] = true;
272       if (all_nhps.back () > nhp.back ())
273         avoid_me[UP] = true;
274       if (avoid_me[stemdir])
275         {
276           stemdir = -stemdir;
277           if (avoid_me[stemdir])
278             {
279               me->warning ("Whole-note tremolo may collide with simultaneous notes.");
280               stemdir = -stemdir;
281             }
282         }
283     }
284   return scm_from_int (stemdir);
285 }
286
287 Real
288 Stem_tremolo::y_offset (Grob *me, bool pure)
289 {
290   Item *stem = unsmob<Item> (me->get_object ("stem"));
291   if (!stem)
292     return 0.0;
293
294   Direction dir = get_grob_direction (me);
295
296   Spanner *beam = Stem::get_beam (stem);
297   Real beam_translation = get_beam_translation (me);
298
299   int beam_count = beam ? (Stem::beam_multiplicity (stem).length () + 1) : 0;
300
301   if (pure && beam)
302     {
303       Interval ph = stem->pure_y_extent (stem, 0, INT_MAX);
304       Stem_info si = Stem::get_stem_info (stem);
305       ph[-dir] = si.shortest_y_;
306
307       return (ph - dir * std::max (beam_count, 1) * beam_translation)[dir] - dir * 0.5 * me->pure_y_extent (me, 0, INT_MAX).length ();
308     }
309
310   Real end_y
311     = (pure
312        ? stem->pure_y_extent (stem, 0, INT_MAX)[dir]
313        : stem->extent (stem, Y_AXIS)[dir])
314       - dir * std::max (beam_count, 1) * beam_translation
315       - Stem::beam_end_corrective (stem);
316
317   if (!beam && Stem::duration_log (stem) >= 3)
318     {
319       end_y -= dir * (Stem::duration_log (stem) - 2) * beam_translation;
320       if (dir == UP)
321         end_y -= dir * beam_translation * 0.5;
322     }
323
324   bool whole_note = Stem::duration_log (stem) <= 0;
325   if (whole_note || isinf(end_y))
326     {
327       /* we shouldn't position relative to the end of the stem since the stem
328          is invisible */
329       Real ss = Staff_symbol_referencer::staff_space (me);
330       vector<int> nhp = Stem::note_head_positions (stem);
331       Real note_head = (dir == UP ? nhp.back () : nhp[0]) * ss / 2;
332       end_y = note_head + dir * 1.5;
333     }
334
335   return end_y;
336 }
337
338 MAKE_SCHEME_CALLBACK (Stem_tremolo, print, 1);
339 SCM
340 Stem_tremolo::print (SCM grob)
341 {
342   Grob *me = unsmob<Grob> (grob);
343
344   Stencil s = untranslated_stencil (me, robust_scm2double (me->get_property ("slope"), 0.25));
345   return s.smobbed_copy ();
346 }
347
348 ADD_INTERFACE (Stem_tremolo,
349                "A beam slashing a stem to indicate a tremolo.  The property"
350                " @code{shape} can be @code{beam-like} or @code{rectangle}.",
351
352                /* properties */
353                "beam-thickness "
354                "beam-width "
355                "direction "
356                "flag-count "
357                "length-fraction "
358                "stem "
359                "shape "
360                "slope "
361               );