2 This file is part of LilyPond, the GNU music typesetter.
4 Copyright (C) 1997--2011 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 "output-def.hh"
28 #include "staff-symbol-referencer.hh"
32 MAKE_SCHEME_CALLBACK (Stem_tremolo, calc_slope, 1)
34 Stem_tremolo::calc_slope (SCM smob)
36 Grob *me = unsmob_grob (smob);
37 Grob *stem = unsmob_grob (me->get_object ("stem"));
38 Spanner *beam = Stem::get_beam (stem);
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));
47 Grob *s2 = Beam::last_normal_stem (beam);
48 Grob *s1 = Beam::first_normal_stem (beam);
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);
54 return scm_from_double (dx ? dy / dx : 0);
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)
63 MAKE_SCHEME_CALLBACK (Stem_tremolo, calc_width, 1)
65 Stem_tremolo::calc_width (SCM smob)
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;
73 /* beamed stems and up-stems with flags have shorter tremolos */
74 return scm_from_double (((stemdir == UP && flag) || beam) ? 1.0 : 1.5);
77 MAKE_SCHEME_CALLBACK (Stem_tremolo, calc_style, 1)
79 Stem_tremolo::calc_style (SCM smob)
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;
87 return ly_symbol2scm (((stemdir == UP && flag) || beam) ? "rectangle" : "default");
91 Stem_tremolo::get_beam_translation (Grob *me)
93 Grob *stem = unsmob_grob (me->get_object ("stem"));
94 Spanner *beam = Stem::get_beam (stem);
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);
103 Stem_tremolo::raw_stencil (Grob *me, Real slope, Direction stemdir)
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");
117 if (style == ly_symbol2scm ("rectangle"))
118 a = Lookup::rotated_box (slope, width, thick, blot);
120 a = Lookup::beam (slope, width, thick, blot);
122 a.align_to (X_AXIS, CENTER);
123 a.align_to (Y_AXIS, CENTER);
125 int tremolo_flags = robust_scm2int (me->get_property ("flag-count"), 0);
128 programming_error ("no tremolo flags");
134 Real beam_translation = get_beam_translation (me);
137 for (int i = 0; i < tremolo_flags; i++)
140 b.translate_axis (beam_translation * i * stemdir * -1, Y_AXIS);
146 MAKE_SCHEME_CALLBACK (Stem_tremolo, pure_height, 3);
148 Stem_tremolo::pure_height (SCM smob, SCM, SCM)
150 Item *me = unsmob_item (smob);
153 Cannot use the real slope, since it looks at the Beam.
155 Stencil s1 (untranslated_stencil (me, 0.35));
156 Item *stem = unsmob_item (me->get_object ("stem"));
158 return ly_interval2scm (s1.extent (Y_AXIS));
160 Direction stemdir = get_grob_direction (stem);
164 Spanner *beam = Stem::get_beam (stem);
167 return ly_interval2scm (s1.extent (Y_AXIS));
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);
175 ph = ph - stemdir * max (beam_count, 1) * beam_translation;
176 ph = ph - ph.center ();
178 return ly_interval2scm (ph);
181 MAKE_SCHEME_CALLBACK (Stem_tremolo, width, 1);
183 Stem_tremolo::width (SCM smob)
185 Grob *me = unsmob_grob (smob);
188 Cannot use the real slope, since it looks at the Beam.
190 Stencil s1 (untranslated_stencil (me, 0.35));
192 return ly_interval2scm (s1.extent (X_AXIS));
196 Stem_tremolo::vertical_length (Grob *me)
198 return untranslated_stencil (me, 0.35).extent (Y_AXIS).length ();
202 Stem_tremolo::untranslated_stencil (Grob *me, Real slope)
204 Grob *stem = unsmob_grob (me->get_object ("stem"));
207 programming_error ("no stem for stem-tremolo");
211 Direction stemdir = get_grob_direction (stem);
215 bool whole_note = Stem::duration_log (stem) <= 0;
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);
223 MAKE_SCHEME_CALLBACK (Stem_tremolo, calc_y_offset, 1);
225 Stem_tremolo::calc_y_offset (SCM smob)
227 Grob *me = unsmob_grob (smob);
228 return scm_from_double (y_offset (me, false));
231 MAKE_SCHEME_CALLBACK (Stem_tremolo, pure_calc_y_offset, 3);
233 Stem_tremolo::pure_calc_y_offset (SCM smob,
237 Grob *me = unsmob_grob (smob);
238 return scm_from_double (y_offset (me, true));
242 Stem_tremolo::y_offset (Grob *me, bool pure)
244 Item *stem = unsmob_item (me->get_object ("stem"));
248 Direction stemdir = get_grob_direction (stem);
252 Spanner *beam = Stem::get_beam (stem);
253 Real beam_translation = get_beam_translation (me);
255 int beam_count = beam ? (Stem::beam_multiplicity (stem).length () + 1) : 0;
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_;
263 return (ph - stemdir * max (beam_count, 1) * beam_translation)[stemdir] - stemdir * 0.5 * me->pure_height (me, 0, INT_MAX).length ();
267 = stem->extent (stem, Y_AXIS)[stemdir]
268 - stemdir * max (beam_count, 1) * beam_translation
269 - Stem::beam_end_corrective (stem);
271 if (!beam && Stem::duration_log (stem) >= 3)
273 end_y -= stemdir * (Stem::duration_log (stem) - 2) * beam_translation;
275 end_y -= stemdir * beam_translation * 0.5;
278 bool whole_note = Stem::duration_log (stem) <= 0;
281 /* we shouldn't position relative to the end of the stem since the stem
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;
292 MAKE_SCHEME_CALLBACK (Stem_tremolo, print, 1);
294 Stem_tremolo::print (SCM grob)
296 Grob *me = unsmob_grob (grob);
298 Stencil s = untranslated_stencil (me, robust_scm2double (me->get_property ("slope"), 0.25));
299 return s.smobbed_copy ();
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}.",