]> git.donarmstrong.com Git - lilypond.git/blob - lily/stem-tremolo.cc
release: 1.3.109
[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--2000 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_element (e);
46   
47   Real space = Staff_symbol_referencer::staff_space (se);
48   return ly_interval2scm ( Interval (-space, space));
49 }
50
51
52
53 MAKE_SCHEME_CALLBACK(Stem_tremolo,brew_molecule,1);
54 SCM
55 Stem_tremolo::brew_molecule (SCM smob)
56 {
57   Grob *me= unsmob_element (smob);
58   Grob * stem = unsmob_element (me->get_grob_property ("stem"));
59   Grob * beam = Stem::beam_l (stem);
60   
61   Real dydx;
62   if (beam)
63     {
64       Real dy = 0;
65       SCM s = beam->get_grob_property ("height");
66       if (gh_number_p (s))
67         dy = gh_scm2double (s);
68       Real dx = Beam::last_visible_stem (beam)->relative_coordinate (0, X_AXIS)
69         - Beam::first_visible_stem (beam)->relative_coordinate (0, X_AXIS);
70       dydx = dx ? dy/dx : 0;
71     }
72   else
73     // urg
74     dydx = 0.25;
75
76   Real ss = Staff_symbol_referencer::staff_space (stem);
77   Real thick = gh_scm2double (me->get_grob_property ("beam-thickness"));
78   Real width = gh_scm2double (me->get_grob_property ("beam-width"));
79   width *= ss;
80   thick *= ss;
81   
82   Molecule a (Lookup::beam (dydx, width, thick));
83   a.translate (Offset (-width/2, width / 2 * dydx));
84   
85   int tremolo_flags;
86   SCM s = me->get_grob_property ("tremolo-flags");
87   if (gh_number_p (s))
88     tremolo_flags = gh_scm2int (s);
89   else
90     // huh?
91     tremolo_flags = 1;
92
93   int mult = beam ? Beam::get_multiplicity (beam) : 0;
94   SCM space_proc = me->get_grob_property ("beam-space-function");
95   SCM space = gh_call1 (space_proc, gh_int2scm (mult));
96   Real interbeam_f = gh_scm2double (space) * ss;
97
98
99   Molecule mol; 
100   for (int i = 0; i < tremolo_flags; i++)
101     {
102       Molecule b (a);
103       b.translate_axis (interbeam_f * i, Y_AXIS);
104       mol.add_molecule (b);
105     }
106   if (tremolo_flags)
107     mol.translate_axis (-mol.extent (Y_AXIS).center (), Y_AXIS);
108   if (beam)
109     {
110       // ugh, rather calc from Stem_tremolo_req
111       int beams_i = Stem::beam_count(stem, RIGHT) >? Stem::beam_count (stem, LEFT);
112       mol.translate (Offset(stem->relative_coordinate (0, X_AXIS) - me->relative_coordinate (0, X_AXIS),
113                             Stem::stem_end_position (stem ) * ss / 2 - 
114                             Directional_element_interface::get (beam) * beams_i * interbeam_f));
115     }
116   else
117     {  
118       /*
119         Beams should intersect one beamthickness below stem end
120       */
121       Real dy = Stem::stem_end_position (stem ) * ss / 2;
122       dy -= mol.extent (Y_AXIS).length () / 2 *  Stem::get_direction (stem );
123
124       /*
125         uhg.  Should use relative coords and placement
126       */
127       Real whole_note_correction;
128       if (Stem::invisible_b (stem ))
129         {
130           Grob *hed = Stem::support_head (stem );
131           whole_note_correction = -Stem::get_direction (stem )
132             *hed->extent (hed, X_AXIS).length () / 2;
133         }
134       else
135         whole_note_correction = 0;
136          
137       mol.translate (Offset (stem->relative_coordinate (0, X_AXIS) - me->relative_coordinate (0, X_AXIS) +
138                              whole_note_correction, dy));
139     }
140   
141   return mol.smobbed_copy ();
142 }
143
144
145 void
146 Stem_tremolo::set_stem (Grob*me,Grob *s)
147 {
148   me->set_grob_property ("stem", s->self_scm ());
149 }
150