]> git.donarmstrong.com Git - lilypond.git/blob - lily/stem-tremolo.cc
b3ea13d30d1f0fac3992b4ae9391e10b76e9f99f
[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--2006 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_visible_stem (beam);
37       Grob *s1 = Beam::first_visible_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::get_beam_translation (beam) : 0.81;
87 }
88
89 /* FIXME: move to Lookup? */
90 Stencil
91 Stem_tremolo::rotated_box (Real slope, Real width, Real thick, Real blot)
92 {
93   vector<Offset> pts;
94   Offset rot (1, slope);
95
96   thick -= 2*blot;
97   width -= 2*blot;
98   rot /= sqrt (1 + slope*slope);
99   pts.push_back (Offset (0, -thick / 2) * rot);
100   pts.push_back (Offset (width, -thick / 2) * rot);
101   pts.push_back (Offset (width, thick / 2) * rot);
102   pts.push_back (Offset (0, thick / 2) * rot);
103   return Lookup::round_filled_polygon (pts, blot);
104 }
105
106 Stencil
107 Stem_tremolo::raw_stencil (Grob *me, Real slope, Direction stemdir)
108 {
109   Real ss = Staff_symbol_referencer::staff_space (me);
110   Real thick = robust_scm2double (me->get_property ("beam-thickness"), 1);
111   Real width = robust_scm2double (me->get_property ("beam-width"), 1);
112   Real blot = me->layout ()->get_dimension (ly_symbol2scm ("blot-diameter"));
113   SCM style = me->get_property ("style");
114   if (!scm_is_symbol (style))
115     style = ly_symbol2scm ("default");
116
117   width *= ss;
118   thick *= ss;
119
120   Stencil a = style == ly_symbol2scm ("rectangle") ?
121     rotated_box (slope, width, thick, blot) :
122     Lookup::beam (slope, width, thick, blot);
123   a.align_to (X_AXIS, CENTER);
124   a.align_to (Y_AXIS, CENTER);
125
126   int tremolo_flags = robust_scm2int (me->get_property ("flag-count"), 0);
127   if (!tremolo_flags)
128     {
129       programming_error ("no tremolo flags");
130
131       me->suicide ();
132       return Stencil ();
133     }
134
135   Real beam_translation = get_beam_translation(me);
136
137   Stencil mol;
138   for (int i = 0; i < tremolo_flags; i++)
139     {
140       Stencil b (a);
141       b.translate_axis (beam_translation * i * stemdir * -1, Y_AXIS);
142       mol.add_stencil (b);
143     }
144   return mol;
145 }
146
147
148
149 MAKE_SCHEME_CALLBACK (Stem_tremolo, height, 1);
150 SCM
151 Stem_tremolo::height (SCM smob)
152 {
153   Grob *me = unsmob_grob (smob);
154
155   /*
156     Cannot use the real slope, since it looks at the Beam.
157    */
158   Stencil s1 (raw_stencil (me, 0.35, UP));
159
160   return ly_interval2scm (s1.extent (Y_AXIS));
161 }
162
163
164 MAKE_SCHEME_CALLBACK (Stem_tremolo, print, 1);
165 SCM
166 Stem_tremolo::print (SCM grob)
167 {
168   Grob *me = unsmob_grob (grob);
169   Grob *stem = unsmob_grob (me->get_object ("stem"));
170   if (!stem)
171     {
172       programming_error ("no stem for stem-tremolo");
173       return SCM_EOL;
174     }
175
176   Spanner *beam = Stem::get_beam (stem);
177   Direction stemdir = get_grob_direction (stem);
178   bool whole_note = Stem::duration_log (stem) <= 0;
179   if (stemdir == 0)
180     stemdir = UP;
181
182   Real beam_translation
183     = (beam && beam->is_live ())
184     ? Beam::get_beam_translation (beam)
185     : 0.81;
186
187   /* for a whole note, we position relative to the notehead, so we want the
188      stencil aligned on the flag closest to the head */
189   Direction stencil_dir = whole_note ? -stemdir : stemdir;
190   Stencil mol = raw_stencil (me, robust_scm2double (me->get_property ("slope"),
191                                                     0.25), stencil_dir);
192
193   Interval mol_ext = mol.extent (Y_AXIS);
194   Real ss = Staff_symbol_referencer::staff_space (me);
195
196   // ugh, rather calc from Stem_tremolo_req
197   int beam_count = beam ? (Stem::beam_multiplicity (stem).length () + 1) : 0;
198
199   Real beamthickness = 0.0;
200   SCM sbt = (beam) ? beam->get_property ("thickness") : SCM_EOL;
201   if (scm_is_number (sbt))
202     beamthickness = scm_to_double (sbt) * ss;
203
204   Real end_y
205     = Stem::stem_end_position (stem) * ss / 2
206     - stemdir * max (beam_count, 1) * beam_translation;
207
208   if (!beam && Stem::duration_log (stem) >= 3)
209     {
210       end_y -= stemdir * (Stem::duration_log (stem) - 2) * beam_translation;
211       if (stemdir == UP)
212         end_y -= stemdir * beam_translation * 0.5;
213     }
214   if (whole_note)
215     {
216       /* we shouldn't position relative to the end of the stem since the stem
217          is invisible */
218       vector<int> nhp = Stem::note_head_positions (stem);
219       Real note_head = (stemdir == UP ? nhp.back () : nhp[0]);
220       end_y = note_head + stemdir * 2.0;
221     }
222   mol.translate_axis (end_y, Y_AXIS);
223
224   return mol.smobbed_copy ();
225 }
226
227 ADD_INTERFACE (Stem_tremolo, "stem-tremolo-interface",
228                "A beam slashing a stem to indicate a tremolo.",
229
230                "beam-thickness "
231                "beam-width "
232                "flag-count "
233                "stem "
234                "style "
235                "slope "
236                );