]> git.donarmstrong.com Git - lilypond.git/blob - lily/stem-tremolo.cc
* lily/beam.cc (set_stem_lengths): extend stems for gapped tremolo
[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 "warn.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 MAKE_SCHEME_CALLBACK (Stem_tremolo,dim_callback,2);
25
26 /*
27   todo: init with cons. 
28  */
29 SCM
30 Stem_tremolo::dim_callback (SCM e, SCM)
31 {
32   Grob * se = unsmob_grob (e);
33   
34   Real space = Staff_symbol_referencer::staff_space (se);
35   return ly_interval2scm (Interval (-space, space));
36 }
37
38 /*
39   ugh ?  --from Slur
40  */
41 MAKE_SCHEME_CALLBACK (Stem_tremolo, height, 2);
42 SCM
43 Stem_tremolo::height (SCM smob, SCM ax)
44 {
45   Axis a = (Axis)gh_scm2int (ax);
46   Grob * me = unsmob_grob (smob);
47   assert (a == Y_AXIS);
48
49   SCM mol = me->get_uncached_molecule ();
50
51   if (Molecule *m = unsmob_molecule (mol))
52     return ly_interval2scm (m->extent (a));
53   else
54     return ly_interval2scm (Interval());
55 }
56
57
58 MAKE_SCHEME_CALLBACK (Stem_tremolo,brew_molecule,1);
59 SCM
60 Stem_tremolo::brew_molecule (SCM smob)
61 {
62   Grob *me= unsmob_grob (smob);
63   Grob * stem = unsmob_grob (me->get_grob_property ("stem"));
64   Grob * beam = Stem::beam_l (stem);
65   
66   Real dydx;
67   if (beam)
68     {
69       Real dy = 0;
70       SCM s = beam->get_grob_property ("positions");
71       if (gh_pair_p (s))
72         {
73           dy = -gh_scm2double (gh_car (s)) +gh_scm2double (gh_cdr (s));
74         }
75       Real dx = Beam::last_visible_stem (beam)->relative_coordinate (0, X_AXIS)
76         - Beam::first_visible_stem (beam)->relative_coordinate (0, X_AXIS);
77       dydx = dx ? dy/dx : 0;
78     }
79   else
80     // urg
81     dydx = 0.25;
82
83   Real ss = Staff_symbol_referencer::staff_space (stem);
84   Real thick = gh_scm2double (me->get_grob_property ("beam-thickness"));
85   Real width = gh_scm2double (me->get_grob_property ("beam-width"));
86   width *= ss;
87   thick *= ss;
88   
89   Molecule a (Lookup::beam (dydx, width, thick));
90   a.translate (Offset (-width/2, width / 2 * dydx));
91   
92   int tremolo_flags = 0;
93   SCM s = me->get_grob_property ("flag-count");
94   if (gh_number_p (s))
95     tremolo_flags = gh_scm2int (s);
96
97   if (!tremolo_flags)
98     {
99       programming_error ("No tremolo flags?");
100
101       me->suicide();
102       return SCM_EOL;
103     }
104
105   /*
106     Who the fuck is 0.81 ?
107
108     --hwn.
109    */
110   Real beam_translation = beam ? Beam::get_beam_translation (beam) : 0.81;
111
112   Molecule mol; 
113   for (int i = 0; i < tremolo_flags; i++)
114     {
115       Molecule b (a);
116       b.translate_axis (beam_translation * i, Y_AXIS);
117       mol.add_molecule (b);
118     }
119   Direction stemdir = Stem::get_direction (stem);
120   Interval mol_ext = mol.extent (Y_AXIS);
121
122   // ugh, rather calc from Stem_tremolo_req
123   int beams_i = (beam) ? (Stem::beam_multiplicity (stem).length ()+ 1): 0;
124
125   /*
126     TODO.
127    */
128
129
130   Real beamthickness = 0.0;
131   SCM sbt = (beam) ? beam->get_grob_property ("thickness") : SCM_EOL ;
132   if (gh_number_p (sbt))
133     {
134       beamthickness = gh_scm2double (sbt) * ss;
135     }
136
137   Real end_y
138     = Stem::stem_end_position (stem) *ss/2 
139     - stemdir * (beams_i * beamthickness
140                  + ((beams_i -1) >? 0) * beam_translation);
141
142   /*
143     the 0.33 ss is to compensate for the size of the note head
144    */
145   Real chord_start_y = Stem::chord_start_y (stem) +
146     0.33 * ss * stemdir;
147
148   Real padding = beam_translation;
149
150   /*
151     if there is not enough space, center on remaining space,
152     else one beamspace away from stem end.
153    */
154   if (stemdir * (end_y - chord_start_y) - 2*padding - mol_ext.length ()  < 0.0)
155     {
156       mol.translate_axis ((end_y + chord_start_y) /2.0  - mol_ext.center (),Y_AXIS);
157     }
158   else
159     {
160       mol.translate_axis (end_y - stemdir * beam_translation
161                           -mol_ext [stemdir]
162                           , Y_AXIS);
163     }
164   
165   return mol.smobbed_copy ();
166 }
167
168
169 void
170 Stem_tremolo::set_stem (Grob*me,Grob *s)
171 {
172   me->set_grob_property ("stem", s->self_scm ());
173 }
174
175
176 ADD_INTERFACE (Stem_tremolo,"stem-tremolo-interface",
177   "",
178   "stem beam-width beam-thickness flag-count");