]> git.donarmstrong.com Git - lilypond.git/blob - lily/stem-tremolo.cc
release: 1.5.47
[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 bool
26 Stem_tremolo::has_interface (Grob *me)
27 {
28   return me->has_interface (ly_symbol2scm ("stem-tremolo-interface"));
29 }
30
31 MAKE_SCHEME_CALLBACK (Stem_tremolo,dim_callback,2);
32
33 /*
34   todo: init with cons. 
35  */
36 SCM
37 Stem_tremolo::dim_callback (SCM e, SCM)
38 {
39   Grob * se = unsmob_grob (e);
40   
41   Real space = Staff_symbol_referencer::staff_space (se);
42   return ly_interval2scm (Interval (-space, space));
43 }
44
45 /*
46   ugh ?  --from Slur
47  */
48 MAKE_SCHEME_CALLBACK (Stem_tremolo, height, 2);
49 SCM
50 Stem_tremolo::height (SCM smob, SCM ax)
51 {
52   Axis a = (Axis)gh_scm2int (ax);
53   Grob * me = unsmob_grob (smob);
54   assert (a == Y_AXIS);
55
56   SCM mol = me->get_uncached_molecule ();
57   return ly_interval2scm (unsmob_molecule (mol)->extent (a));
58 }
59
60
61 MAKE_SCHEME_CALLBACK (Stem_tremolo,brew_molecule,1);
62 SCM
63 Stem_tremolo::brew_molecule (SCM smob)
64 {
65   Grob *me= unsmob_grob (smob);
66   Grob * stem = unsmob_grob (me->get_grob_property ("stem"));
67   Grob * beam = Stem::beam_l (stem);
68   
69   Real dydx;
70   if (beam)
71     {
72       Real dy = 0;
73       SCM s = beam->get_grob_property ("positions");
74       if (gh_pair_p (s))
75         {
76           dy = -gh_scm2double (gh_car (s)) +gh_scm2double (gh_cdr (s));
77         }
78       Real dx = Beam::last_visible_stem (beam)->relative_coordinate (0, X_AXIS)
79         - Beam::first_visible_stem (beam)->relative_coordinate (0, X_AXIS);
80       dydx = dx ? dy/dx : 0;
81     }
82   else
83     // urg
84     dydx = 0.25;
85
86   Real ss = Staff_symbol_referencer::staff_space (stem);
87   Real thick = gh_scm2double (me->get_grob_property ("beam-thickness"));
88   Real width = gh_scm2double (me->get_grob_property ("beam-width"));
89   width *= ss;
90   thick *= ss;
91   
92   Molecule a (Lookup::beam (dydx, width, thick));
93   a.translate (Offset (-width/2, width / 2 * dydx));
94   
95   int tremolo_flags;
96   SCM s = me->get_grob_property ("flag-count");
97   if (gh_number_p (s))
98     tremolo_flags = gh_scm2int (s);
99   else
100     // huh?
101     tremolo_flags = 1;
102
103   Real interbeam = beam ? Beam::get_interbeam (beam) : 0.81;
104
105   Molecule mol; 
106   for (int i = 0; i < tremolo_flags; i++)
107     {
108       Molecule b (a);
109       b.translate_axis (interbeam * i, Y_AXIS);
110       mol.add_molecule (b);
111     }
112   if (tremolo_flags)
113     mol.translate_axis (-mol.extent (Y_AXIS).center (), Y_AXIS);
114   if (beam)
115     {
116       // ugh, rather calc from Stem_tremolo_req
117       int beams_i = Stem::beam_count (stem, RIGHT)
118         >? Stem::beam_count (stem, LEFT);
119       mol.translate (Offset (stem->relative_coordinate (0, X_AXIS)
120                              - me->relative_coordinate (0, X_AXIS),
121                              Stem::stem_end_position (stem) * ss / 2 - 
122                              Directional_element_interface::get (beam)
123                              * beams_i * interbeam));
124     }
125   else
126     {  
127       /*
128         Beams should intersect one beamthickness below stem end
129       */
130       Real dy = Stem::stem_end_position (stem) * ss / 2;
131       dy -= mol.extent (Y_AXIS).length () / 2 *  Stem::get_direction (stem);
132
133       /*
134         uhg.  Should use relative coords and placement
135       */
136       Real whole_note_correction;
137       if (Stem::invisible_b (stem))
138         {
139           Grob *hed = Stem::support_head (stem);
140           whole_note_correction = -Stem::get_direction (stem)
141             *hed->extent (hed, X_AXIS).length () / 2;
142         }
143       else
144         whole_note_correction = 0;
145          
146       mol.translate (Offset (stem->relative_coordinate (0, X_AXIS)
147                              - me->relative_coordinate (0, X_AXIS) +
148                              whole_note_correction, dy));
149     }
150   
151   return mol.smobbed_copy ();
152 }
153
154
155 void
156 Stem_tremolo::set_stem (Grob*me,Grob *s)
157 {
158   me->set_grob_property ("stem", s->self_scm ());
159 }
160
161
162 ADD_INTERFACE (Stem_tremolo,"stem-tremolo-interface",
163   "",
164   "stem beam-width beam-thickness flag-count");