]> git.donarmstrong.com Git - lilypond.git/blob - lily/stem-tremolo.cc
tremolo fixes
[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--2002 Han-Wen Nienhuys <hanwen@cs.uu.nl>
7   
8  */
9
10 #include "stem-tremolo.hh"
11 #include "debug.hh"
12 #include "beam.hh"
13 #include "paper-def.hh"
14 #include "lookup.hh"
15 #include "stem.hh"
16 #include "item.hh"
17 #include "staff-symbol-referencer.hh"
18 #include "directional-element-interface.hh"
19
20 /*
21   TODO:
22     lengthen stem if necessary
23  */
24
25
26 MAKE_SCHEME_CALLBACK (Stem_tremolo,dim_callback,2);
27
28 /*
29   todo: init with cons. 
30  */
31 SCM
32 Stem_tremolo::dim_callback (SCM e, SCM)
33 {
34   Grob * se = unsmob_grob (e);
35   
36   Real space = Staff_symbol_referencer::staff_space (se);
37   return ly_interval2scm (Interval (-space, space));
38 }
39
40 /*
41   ugh ?  --from Slur
42  */
43 MAKE_SCHEME_CALLBACK (Stem_tremolo, height, 2);
44 SCM
45 Stem_tremolo::height (SCM smob, SCM ax)
46 {
47   Axis a = (Axis)gh_scm2int (ax);
48   Grob * me = unsmob_grob (smob);
49   assert (a == Y_AXIS);
50
51   SCM mol = me->get_uncached_molecule ();
52   return ly_interval2scm (unsmob_molecule (mol)->extent (a));
53 }
54
55
56 MAKE_SCHEME_CALLBACK (Stem_tremolo,brew_molecule,1);
57 SCM
58 Stem_tremolo::brew_molecule (SCM smob)
59 {
60   Grob *me= unsmob_grob (smob);
61   Grob * stem = unsmob_grob (me->get_grob_property ("stem"));
62   Grob * beam = Stem::beam_l (stem);
63   
64   Real dydx;
65   if (beam)
66     {
67       Real dy = 0;
68       SCM s = beam->get_grob_property ("positions");
69       if (gh_pair_p (s))
70         {
71           dy = -gh_scm2double (gh_car (s)) +gh_scm2double (gh_cdr (s));
72         }
73       Real dx = Beam::last_visible_stem (beam)->relative_coordinate (0, X_AXIS)
74         - Beam::first_visible_stem (beam)->relative_coordinate (0, X_AXIS);
75       dydx = dx ? dy/dx : 0;
76     }
77   else
78     // urg
79     dydx = 0.25;
80
81   Real ss = Staff_symbol_referencer::staff_space (stem);
82   Real thick = gh_scm2double (me->get_grob_property ("beam-thickness"));
83   Real width = gh_scm2double (me->get_grob_property ("beam-width"));
84   width *= ss;
85   thick *= ss;
86   
87   Molecule a (Lookup::beam (dydx, width, thick));
88   a.translate (Offset (-width/2, width / 2 * dydx));
89   
90   int tremolo_flags;
91   SCM s = me->get_grob_property ("flag-count");
92   if (gh_number_p (s))
93     tremolo_flags = gh_scm2int (s);
94   else
95     // huh?
96     tremolo_flags = 1;
97
98   Real interbeam = beam ? Beam::get_interbeam (beam) : 0.81;
99
100   Molecule mol; 
101   for (int i = 0; i < tremolo_flags; i++)
102     {
103       Molecule b (a);
104       b.translate_axis (interbeam * i, Y_AXIS);
105       mol.add_molecule (b);
106     }
107   if (tremolo_flags)
108     mol.translate_axis (-mol.extent (Y_AXIS).center (), Y_AXIS);
109   if (beam)
110     {
111       // ugh, rather calc from Stem_tremolo_req
112       int beams_i = Stem::beam_count (stem, RIGHT)
113         >? Stem::beam_count (stem, LEFT);
114       mol.translate (Offset (0,
115                              Stem::stem_end_position (stem) * ss / 2 - 
116                              Directional_element_interface::get (beam)
117                              * beams_i * interbeam));
118     }
119   else
120     {  
121       /*
122         Beams should intersect one beamthickness below stem end
123       */
124       Real dy = Stem::stem_end_position (stem) * ss / 2;
125       dy -= mol.extent (Y_AXIS).length () / 2 *  Stem::get_direction (stem);
126
127       mol.translate (Offset (0, dy));
128     }
129   
130   return mol.smobbed_copy ();
131 }
132
133
134 void
135 Stem_tremolo::set_stem (Grob*me,Grob *s)
136 {
137   me->set_grob_property ("stem", s->self_scm ());
138 }
139
140
141 ADD_INTERFACE (Stem_tremolo,"stem-tremolo-interface",
142   "",
143   "stem beam-width beam-thickness flag-count");