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"
36 MAKE_SCHEME_CALLBACK (Stem_tremolo, calc_slope, 1)
38 Stem_tremolo::calc_slope (SCM smob)
40 Grob *me = unsmob<Grob> (smob);
41 Grob *stem = unsmob<Grob> (me->get_object ("stem"));
42 Spanner *beam = Stem::get_beam (stem);
44 SCM style = me->get_property ("style");
46 if (beam && !scm_is_eq (style, ly_symbol2scm ("constant")))
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));
53 Grob *s2 = Beam::last_normal_stem (beam);
54 Grob *s1 = Beam::first_normal_stem (beam);
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);
60 return scm_from_double (dx ? dy / dx : 0);
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)
70 MAKE_SCHEME_CALLBACK (Stem_tremolo, calc_width, 1)
72 Stem_tremolo::calc_width (SCM smob)
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;
80 /* beamed stems and up-stems with flags have shorter tremolos */
81 return scm_from_double (((dir == UP && flag) || beam) ? 1.0 : 1.5);
84 MAKE_SCHEME_CALLBACK (Stem_tremolo, calc_shape, 1)
86 Stem_tremolo::calc_shape (SCM smob)
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");
95 return ly_symbol2scm (!scm_is_eq (style, ly_symbol2scm ("constant"))
96 && ((dir == UP && flag) || beam)
97 ? "rectangle" : "beam-like");
101 Stem_tremolo::get_beam_translation (Grob *me)
103 Grob *stem = unsmob<Grob> (me->get_object ("stem"));
104 Spanner *beam = Stem::get_beam (stem);
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);
113 Stem_tremolo::raw_stencil (Grob *me, Real slope, Direction dir)
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");
127 if (scm_is_eq (shape, ly_symbol2scm ("rectangle")))
128 a = Lookup::rotated_box (slope, width, thick, blot);
130 a = Lookup::beam (slope, width, thick, blot);
132 a.align_to (X_AXIS, CENTER);
133 a.align_to (Y_AXIS, CENTER);
135 int tremolo_flags = robust_scm2int (me->get_property ("flag-count"), 0);
138 programming_error ("no tremolo flags");
144 Real beam_translation = get_beam_translation (me);
147 for (int i = 0; i < tremolo_flags; i++)
150 b.translate_axis (beam_translation * i * dir * -1, Y_AXIS);
156 MAKE_SCHEME_CALLBACK (Stem_tremolo, pure_height, 3);
158 Stem_tremolo::pure_height (SCM smob, SCM, SCM)
160 Item *me = unsmob<Item> (smob);
163 Cannot use the real slope, since it looks at the Beam.
165 Stencil s1 (untranslated_stencil (me, 0.35));
166 Item *stem = unsmob<Item> (me->get_object ("stem"));
168 return ly_interval2scm (s1.extent (Y_AXIS));
170 Direction dir = get_grob_direction (me);
172 Spanner *beam = Stem::get_beam (stem);
175 return ly_interval2scm (s1.extent (Y_AXIS));
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);
183 ph = ph - dir * max (beam_count, 1) * beam_translation;
184 ph = ph - ph.center ();
186 return ly_interval2scm (ph);
189 MAKE_SCHEME_CALLBACK (Stem_tremolo, width, 1);
191 Stem_tremolo::width (SCM smob)
193 Grob *me = unsmob<Grob> (smob);
196 Cannot use the real slope, since it looks at the Beam.
198 Stencil s1 (untranslated_stencil (me, 0.35));
200 return ly_interval2scm (s1.extent (X_AXIS));
204 Stem_tremolo::vertical_length (Grob *me)
206 return untranslated_stencil (me, 0.35).extent (Y_AXIS).length ();
210 Stem_tremolo::untranslated_stencil (Grob *me, Real slope)
212 Grob *stem = unsmob<Grob> (me->get_object ("stem"));
215 programming_error ("no stem for stem-tremolo");
219 Direction dir = get_grob_direction (me);
221 bool whole_note = Stem::duration_log (stem) <= 0;
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);
229 MAKE_SCHEME_CALLBACK (Stem_tremolo, calc_y_offset, 1);
231 Stem_tremolo::calc_y_offset (SCM smob)
233 Grob *me = unsmob<Grob> (smob);
234 return scm_from_double (y_offset (me, false));
237 MAKE_SCHEME_CALLBACK (Stem_tremolo, pure_calc_y_offset, 3);
239 Stem_tremolo::pure_calc_y_offset (SCM smob,
243 Grob *me = unsmob<Grob> (smob);
244 return scm_from_double (y_offset (me, true));
247 MAKE_SCHEME_CALLBACK (Stem_tremolo, calc_direction, 1);
249 Stem_tremolo::calc_direction (SCM smob)
251 Item *me = unsmob<Item> (smob);
253 Item *stem = unsmob<Item> (me->get_object ("stem"));
255 return scm_from_int (CENTER);
257 Direction stemdir = get_grob_direction (stem);
259 vector<int> nhp = Stem::note_head_positions (stem);
261 * We re-decide stem-dir if there may be collisions with other
262 * note heads in the staff.
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))
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 ())
274 if (avoid_me[stemdir])
277 if (avoid_me[stemdir])
279 me->warning ("Whole-note tremolo may collide with simultaneous notes.");
284 return scm_from_int (stemdir);
288 Stem_tremolo::y_offset (Grob *me, bool pure)
290 Item *stem = unsmob<Item> (me->get_object ("stem"));
294 Direction dir = get_grob_direction (me);
296 Spanner *beam = Stem::get_beam (stem);
297 Real beam_translation = get_beam_translation (me);
299 int beam_count = beam ? (Stem::beam_multiplicity (stem).length () + 1) : 0;
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_;
307 return (ph - dir * max (beam_count, 1) * beam_translation)[dir] - dir * 0.5 * me->pure_y_extent (me, 0, INT_MAX).length ();
312 ? stem->pure_y_extent (stem, 0, INT_MAX)[dir]
313 : stem->extent (stem, Y_AXIS)[dir])
314 - dir * max (beam_count, 1) * beam_translation
315 - Stem::beam_end_corrective (stem);
317 if (!beam && Stem::duration_log (stem) >= 3)
319 end_y -= dir * (Stem::duration_log (stem) - 2) * beam_translation;
321 end_y -= dir * beam_translation * 0.5;
324 bool whole_note = Stem::duration_log (stem) <= 0;
325 if (whole_note || isinf(end_y))
327 /* we shouldn't position relative to the end of the stem since the stem
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;
338 MAKE_SCHEME_CALLBACK (Stem_tremolo, print, 1);
340 Stem_tremolo::print (SCM grob)
342 Grob *me = unsmob<Grob> (grob);
344 Stencil s = untranslated_stencil (me, robust_scm2double (me->get_property ("slope"), 0.25));
345 return s.smobbed_copy ();
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}.",