]> git.donarmstrong.com Git - lilypond.git/blob - lily/stem-tremolo.cc
c093e9f40e3c7e75f44e796bf9789a866709f4ac
[lilypond.git] / lily / stem-tremolo.cc
1 /*
2   stem-tremolo.cc -- implement Stem_tremolo
3
4   source file of the GNU LilyPond music typesetter
5
6   (c) 1997--2007 Han-Wen Nienhuys <hanwen@xs4all.nl>
7 */
8
9 #include "stem-tremolo.hh"
10
11 #include "spanner.hh"
12 #include "beam.hh"
13 #include "directional-element-interface.hh"
14 #include "item.hh"
15 #include "lookup.hh"
16 #include "output-def.hh"
17 #include "staff-symbol-referencer.hh"
18 #include "stem.hh"
19 #include "warn.hh"
20
21 MAKE_SCHEME_CALLBACK (Stem_tremolo, calc_slope, 1)
22 SCM
23 Stem_tremolo::calc_slope (SCM smob)
24 {
25   Grob *me = unsmob_grob (smob);
26   Grob *stem = unsmob_grob (me->get_object ("stem"));
27   Spanner *beam = Stem::get_beam (stem);
28
29   if (beam)
30     {
31       Real dy = 0;
32       SCM s = beam->get_property ("quantized-positions");
33       if (is_number_pair (s))
34         dy = - scm_to_double (scm_car (s)) + scm_to_double (scm_cdr (s));
35
36       Grob *s2 = Beam::last_normal_stem (beam);
37       Grob *s1 = Beam::first_normal_stem (beam);
38       
39       Grob *common = s1->common_refpoint (s2, X_AXIS);
40       Real dx = s2->relative_coordinate (common, X_AXIS) -
41         s1->relative_coordinate (common, X_AXIS);
42
43       return scm_from_double (dx ? dy / dx : 0);
44     }
45   else
46     /* down stems with flags should have more sloped trems (helps avoid
47        flag/stem collisions without making the stem very long) */
48     return scm_from_double (
49         (Stem::duration_log (stem) >= 3 && get_grob_direction (stem) == DOWN) ?
50           0.40 : 0.25);
51 }
52
53 MAKE_SCHEME_CALLBACK (Stem_tremolo, calc_width, 1)
54 SCM
55 Stem_tremolo::calc_width (SCM smob)
56 {
57   Grob *me = unsmob_grob (smob);
58   Grob *stem = unsmob_grob (me->get_object ("stem"));
59   Direction stemdir = get_grob_direction (stem);
60   bool beam = Stem::get_beam (stem);
61   bool flag = Stem::duration_log (stem) >= 3 && !beam;
62
63   /* beamed stems and up-stems with flags have shorter tremolos */
64   return scm_from_double (((stemdir == UP && flag) || beam)? 1.0 : 1.5);
65 }
66
67 MAKE_SCHEME_CALLBACK (Stem_tremolo, calc_style, 1)
68 SCM
69 Stem_tremolo::calc_style (SCM smob)
70 {
71   Grob *me = unsmob_grob (smob);
72   Grob *stem = unsmob_grob (me->get_object ("stem"));
73   Direction stemdir = get_grob_direction (stem);
74   bool beam = Stem::get_beam (stem);
75   bool flag = Stem::duration_log (stem) >= 3 && !beam;
76
77   return ly_symbol2scm (((stemdir == UP && flag) || beam) ? "rectangle" : "default");
78 }
79
80 Real
81 Stem_tremolo::get_beam_translation (Grob *me)
82 {
83   Grob *stem = unsmob_grob (me->get_object ("stem"));
84   Spanner *beam = Stem::get_beam (stem);
85
86   return (beam && beam->is_live ())
87     ? Beam::get_beam_translation (beam)
88     : (Staff_symbol_referencer::staff_space (me)
89        * robust_scm2double (me->get_property ("length-fraction"), 1.0) * 0.81);
90 }
91
92 Stencil
93 Stem_tremolo::raw_stencil (Grob *me, Real slope, Direction stemdir)
94 {
95   Real ss = Staff_symbol_referencer::staff_space (me);
96   Real thick = robust_scm2double (me->get_property ("beam-thickness"), 1);
97   Real width = robust_scm2double (me->get_property ("beam-width"), 1);
98   Real blot = me->layout ()->get_dimension (ly_symbol2scm ("blot-diameter"));
99   SCM style = me->get_property ("style");
100   if (!scm_is_symbol (style))
101     style = ly_symbol2scm ("default");
102
103   width *= ss;
104   thick *= ss;
105
106   Stencil a;
107   if (style == ly_symbol2scm ("rectangle"))
108     a = Lookup::rotated_box (slope, width, thick, blot);
109   else
110     a = Lookup::beam (slope, width, thick, blot);
111
112   a.align_to (X_AXIS, CENTER);
113   a.align_to (Y_AXIS, CENTER);
114
115   int tremolo_flags = robust_scm2int (me->get_property ("flag-count"), 0);
116   if (!tremolo_flags)
117     {
118       programming_error ("no tremolo flags");
119
120       me->suicide ();
121       return Stencil ();
122     }
123
124   Real beam_translation = get_beam_translation (me);
125
126   Stencil mol;
127   for (int i = 0; i < tremolo_flags; i++)
128     {
129       Stencil b (a);
130       b.translate_axis (beam_translation * i * stemdir * -1, Y_AXIS);
131       mol.add_stencil (b);
132     }
133   return mol;
134 }
135
136
137
138 MAKE_SCHEME_CALLBACK (Stem_tremolo, height, 1);
139 SCM
140 Stem_tremolo::height (SCM smob)
141 {
142   Grob *me = unsmob_grob (smob);
143
144   /*
145     Cannot use the real slope, since it looks at the Beam.
146    */
147   Stencil s1 (translated_stencil (me, 0.35));
148
149   return ly_interval2scm (s1.extent (Y_AXIS));
150 }
151
152 MAKE_SCHEME_CALLBACK (Stem_tremolo, width, 1);
153 SCM
154 Stem_tremolo::width (SCM smob)
155 {
156   Grob *me = unsmob_grob (smob);
157
158   /*
159     Cannot use the real slope, since it looks at the Beam.
160    */
161   Stencil s1 (untranslated_stencil (me, 0.35));
162
163   return ly_interval2scm (s1.extent (X_AXIS));
164 }
165
166 Real
167 Stem_tremolo::vertical_length (Grob *me)
168 {
169   return untranslated_stencil (me, 0.35).extent (Y_AXIS).length ();
170 }
171   
172 Stencil
173 Stem_tremolo::untranslated_stencil (Grob *me, Real slope)
174 {
175   Grob *stem = unsmob_grob (me->get_object ("stem"));
176   if (!stem)
177     {
178       programming_error ("no stem for stem-tremolo");
179       return Stencil ();
180     }
181
182   Direction stemdir = get_grob_direction (stem);
183   if (!stemdir)
184     stemdir = UP;
185
186   bool whole_note = Stem::duration_log (stem) <= 0;
187
188   /* for a whole note, we position relative to the notehead, so we want the
189      stencil aligned on the flag closest to the head */
190   Direction stencil_dir = whole_note ? -stemdir : stemdir;
191   return raw_stencil (me, slope, stencil_dir);
192 }
193
194   
195 Stencil
196 Stem_tremolo::translated_stencil (Grob *me, Real slope)
197 {
198   Stencil mol = untranslated_stencil (me, slope);
199
200   Grob *stem = unsmob_grob (me->get_object ("stem"));
201   if (!stem)
202     return Stencil ();
203   
204   Direction stemdir = get_grob_direction (stem);
205   if (stemdir == 0)
206     stemdir = UP;
207
208   Spanner *beam = Stem::get_beam (stem);
209   Real beam_translation = get_beam_translation (me);
210
211   int beam_count = beam ? (Stem::beam_multiplicity (stem).length () + 1) : 0;
212   Real ss = Staff_symbol_referencer::staff_space (me);
213
214   Real end_y
215     = Stem::stem_end_position (stem) * ss / 2
216     - stemdir * max (beam_count, 1) * beam_translation;
217
218   if (!beam && Stem::duration_log (stem) >= 3)
219     {
220       end_y -= stemdir * (Stem::duration_log (stem) - 2) * beam_translation;
221       if (stemdir == UP)
222         end_y -= stemdir * beam_translation * 0.5;
223     }
224
225   bool whole_note = Stem::duration_log (stem) <= 0;
226   if (whole_note)
227     {
228       /* we shouldn't position relative to the end of the stem since the stem
229          is invisible */
230       vector<int> nhp = Stem::note_head_positions (stem);
231       Real note_head = (stemdir == UP ? nhp.back () : nhp[0]) * ss / 2;
232       end_y = note_head + stemdir * 1.5;
233     }
234   mol.translate_axis (end_y, Y_AXIS);
235
236   return mol;
237 }
238
239 MAKE_SCHEME_CALLBACK (Stem_tremolo, print, 1);
240 SCM
241 Stem_tremolo::print (SCM grob)
242 {
243   Grob *me = unsmob_grob (grob);
244   
245   Stencil s = translated_stencil (me, robust_scm2double (me->get_property ("slope"), 0.25));
246   return s.smobbed_copy ();
247 }
248
249 ADD_INTERFACE (Stem_tremolo,
250                "A beam slashing a stem to indicate a tremolo.  The property"
251                " @code{style} can be @code{default} or @code{rectangle}.",
252
253                /* properties */
254                "beam-thickness "
255                "beam-width "
256                "flag-count "
257                "length-fraction "
258                "stem "
259                "style "
260                "slope "
261                );