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