2 This file is part of LilyPond, the GNU music typesetter.
4 Copyright (C) 1997--2015 Han-Wen Nienhuys <hanwen@xs4all.nl>
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.
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.
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/>.
20 #include "stem-tremolo.hh"
24 #include "directional-element-interface.hh"
27 #include "note-collision.hh"
28 #include "note-column.hh"
29 #include "output-def.hh"
30 #include "staff-symbol-referencer.hh"
34 MAKE_SCHEME_CALLBACK (Stem_tremolo, calc_slope, 1)
36 Stem_tremolo::calc_slope (SCM smob)
38 Grob *me = unsmob<Grob> (smob);
39 Grob *stem = unsmob<Grob> (me->get_object ("stem"));
40 Spanner *beam = Stem::get_beam (stem);
42 SCM style = me->get_property ("style");
44 if (beam && !scm_is_eq (style, ly_symbol2scm ("constant")))
47 SCM s = beam->get_property ("quantized-positions");
48 if (is_number_pair (s))
49 dy = - scm_to_double (scm_car (s)) + scm_to_double (scm_cdr (s));
51 Grob *s2 = Beam::last_normal_stem (beam);
52 Grob *s1 = Beam::first_normal_stem (beam);
54 Grob *common = s1->common_refpoint (s2, X_AXIS);
55 Real dx = s2->relative_coordinate (common, X_AXIS)
56 - s1->relative_coordinate (common, X_AXIS);
58 return scm_from_double (dx ? dy / dx : 0);
61 /* down stems with flags should have more sloped trems (helps avoid
62 flag/stem collisions without making the stem very long) */
63 return scm_from_double ((Stem::duration_log (stem) >= 3
64 && get_grob_direction (me) == DOWN && !beam)
68 MAKE_SCHEME_CALLBACK (Stem_tremolo, calc_width, 1)
70 Stem_tremolo::calc_width (SCM smob)
72 Grob *me = unsmob<Grob> (smob);
73 Grob *stem = unsmob<Grob> (me->get_object ("stem"));
74 Direction dir = get_grob_direction (me);
75 bool beam = Stem::get_beam (stem);
76 bool flag = Stem::duration_log (stem) >= 3 && !beam;
78 /* beamed stems and up-stems with flags have shorter tremolos */
79 return scm_from_double (((dir == UP && flag) || beam) ? 1.0 : 1.5);
82 MAKE_SCHEME_CALLBACK (Stem_tremolo, calc_shape, 1)
84 Stem_tremolo::calc_shape (SCM smob)
86 Grob *me = unsmob<Grob> (smob);
87 Grob *stem = unsmob<Grob> (me->get_object ("stem"));
88 Direction dir = get_grob_direction (me);
89 bool beam = Stem::get_beam (stem);
90 bool flag = Stem::duration_log (stem) >= 3 && !beam;
91 SCM style = me->get_property ("style");
93 return ly_symbol2scm (!scm_is_eq (style, ly_symbol2scm ("constant"))
94 && ((dir == UP && flag) || beam)
95 ? "rectangle" : "beam-like");
99 Stem_tremolo::get_beam_translation (Grob *me)
101 Grob *stem = unsmob<Grob> (me->get_object ("stem"));
102 Spanner *beam = Stem::get_beam (stem);
104 return (beam && beam->is_live ())
105 ? Beam::get_beam_translation (beam)
106 : (Staff_symbol_referencer::staff_space (me)
107 * robust_scm2double (me->get_property ("length-fraction"), 1.0) * 0.81);
111 Stem_tremolo::raw_stencil (Grob *me, Real slope, Direction dir)
113 Real ss = Staff_symbol_referencer::staff_space (me);
114 Real thick = robust_scm2double (me->get_property ("beam-thickness"), 1);
115 Real width = robust_scm2double (me->get_property ("beam-width"), 1);
116 Real blot = me->layout ()->get_dimension (ly_symbol2scm ("blot-diameter"));
117 SCM shape = me->get_property ("shape");
118 if (!scm_is_symbol (shape))
119 shape = ly_symbol2scm ("beam-like");
125 if (scm_is_eq (shape, ly_symbol2scm ("rectangle")))
126 a = Lookup::rotated_box (slope, width, thick, blot);
128 a = Lookup::beam (slope, width, thick, blot);
130 a.align_to (X_AXIS, CENTER);
131 a.align_to (Y_AXIS, CENTER);
133 int tremolo_flags = robust_scm2int (me->get_property ("flag-count"), 0);
136 programming_error ("no tremolo flags");
142 Real beam_translation = get_beam_translation (me);
145 for (int i = 0; i < tremolo_flags; i++)
148 b.translate_axis (beam_translation * i * dir * -1, Y_AXIS);
154 MAKE_SCHEME_CALLBACK (Stem_tremolo, pure_height, 3);
156 Stem_tremolo::pure_height (SCM smob, SCM, SCM)
158 Item *me = unsmob<Item> (smob);
161 Cannot use the real slope, since it looks at the Beam.
163 Stencil s1 (untranslated_stencil (me, 0.35));
164 Item *stem = unsmob<Item> (me->get_object ("stem"));
166 return ly_interval2scm (s1.extent (Y_AXIS));
168 Direction dir = get_grob_direction (me);
170 Spanner *beam = Stem::get_beam (stem);
173 return ly_interval2scm (s1.extent (Y_AXIS));
175 Interval ph = stem->pure_y_extent (stem, 0, INT_MAX);
176 Stem_info si = Stem::get_stem_info (stem);
177 ph[-dir] = si.shortest_y_;
178 int beam_count = Stem::beam_multiplicity (stem).length () + 1;
179 Real beam_translation = get_beam_translation (me);
181 ph = ph - dir * max (beam_count, 1) * beam_translation;
182 ph = ph - ph.center ();
184 return ly_interval2scm (ph);
187 MAKE_SCHEME_CALLBACK (Stem_tremolo, width, 1);
189 Stem_tremolo::width (SCM smob)
191 Grob *me = unsmob<Grob> (smob);
194 Cannot use the real slope, since it looks at the Beam.
196 Stencil s1 (untranslated_stencil (me, 0.35));
198 return ly_interval2scm (s1.extent (X_AXIS));
202 Stem_tremolo::vertical_length (Grob *me)
204 return untranslated_stencil (me, 0.35).extent (Y_AXIS).length ();
208 Stem_tremolo::untranslated_stencil (Grob *me, Real slope)
210 Grob *stem = unsmob<Grob> (me->get_object ("stem"));
213 programming_error ("no stem for stem-tremolo");
217 Direction dir = get_grob_direction (me);
219 bool whole_note = Stem::duration_log (stem) <= 0;
221 /* for a whole note, we position relative to the notehead, so we want the
222 stencil aligned on the flag closest to the head */
223 Direction stencil_dir = whole_note ? -dir : dir;
224 return raw_stencil (me, slope, stencil_dir);
227 MAKE_SCHEME_CALLBACK (Stem_tremolo, calc_y_offset, 1);
229 Stem_tremolo::calc_y_offset (SCM smob)
231 Grob *me = unsmob<Grob> (smob);
232 return scm_from_double (y_offset (me, false));
235 MAKE_SCHEME_CALLBACK (Stem_tremolo, pure_calc_y_offset, 3);
237 Stem_tremolo::pure_calc_y_offset (SCM smob,
241 Grob *me = unsmob<Grob> (smob);
242 return scm_from_double (y_offset (me, true));
245 MAKE_SCHEME_CALLBACK (Stem_tremolo, calc_direction, 1);
247 Stem_tremolo::calc_direction (SCM smob)
249 Item *me = unsmob<Item> (smob);
251 Item *stem = unsmob<Item> (me->get_object ("stem"));
253 return scm_from_int (CENTER);
255 Direction stemdir = get_grob_direction (stem);
257 vector<int> nhp = Stem::note_head_positions (stem);
259 * We re-decide stem-dir if there may be collisions with other
260 * note heads in the staff.
262 Grob *maybe_nc = stem->get_parent (X_AXIS)->get_parent (X_AXIS);
263 bool whole_note = Stem::duration_log (stem) <= 0;
264 if (whole_note && has_interface<Note_collision_interface> (maybe_nc))
266 Drul_array<bool> avoid_me (false, false);
267 vector<int> all_nhps = Note_collision_interface::note_head_positions (maybe_nc);
268 if (all_nhps[0] < nhp[0])
269 avoid_me[DOWN] = true;
270 if (all_nhps.back () > nhp.back ())
272 if (avoid_me[stemdir])
275 if (avoid_me[stemdir])
277 me->warning ("Whole-note tremolo may collide with simultaneous notes.");
282 return scm_from_int (stemdir);
286 Stem_tremolo::y_offset (Grob *me, bool pure)
288 Item *stem = unsmob<Item> (me->get_object ("stem"));
292 Direction dir = get_grob_direction (me);
294 Spanner *beam = Stem::get_beam (stem);
295 Real beam_translation = get_beam_translation (me);
297 int beam_count = beam ? (Stem::beam_multiplicity (stem).length () + 1) : 0;
301 Interval ph = stem->pure_y_extent (stem, 0, INT_MAX);
302 Stem_info si = Stem::get_stem_info (stem);
303 ph[-dir] = si.shortest_y_;
305 return (ph - dir * max (beam_count, 1) * beam_translation)[dir] - dir * 0.5 * me->pure_y_extent (me, 0, INT_MAX).length ();
310 ? stem->pure_y_extent (stem, 0, INT_MAX)[dir]
311 : stem->extent (stem, Y_AXIS)[dir])
312 - dir * max (beam_count, 1) * beam_translation
313 - Stem::beam_end_corrective (stem);
315 if (!beam && Stem::duration_log (stem) >= 3)
317 end_y -= dir * (Stem::duration_log (stem) - 2) * beam_translation;
319 end_y -= dir * beam_translation * 0.5;
322 bool whole_note = Stem::duration_log (stem) <= 0;
323 if (whole_note || isinf(end_y))
325 /* we shouldn't position relative to the end of the stem since the stem
327 Real ss = Staff_symbol_referencer::staff_space (me);
328 vector<int> nhp = Stem::note_head_positions (stem);
329 Real note_head = (dir == UP ? nhp.back () : nhp[0]) * ss / 2;
330 end_y = note_head + dir * 1.5;
336 MAKE_SCHEME_CALLBACK (Stem_tremolo, print, 1);
338 Stem_tremolo::print (SCM grob)
340 Grob *me = unsmob<Grob> (grob);
342 Stencil s = untranslated_stencil (me, robust_scm2double (me->get_property ("slope"), 0.25));
343 return s.smobbed_copy ();
346 ADD_INTERFACE (Stem_tremolo,
347 "A beam slashing a stem to indicate a tremolo. The property"
348 " @code{shape} can be @code{beam-like} or @code{rectangle}.",