]> git.donarmstrong.com Git - lilypond.git/blob - lily/stem-tremolo.cc
* lily/lily-guile.cc (robust_scm2double): new function. Use throughout.
[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--2003 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 Molecule
59 Stem_tremolo::raw_molecule (Grob *me)
60 {
61   Grob *stem = unsmob_grob (me->get_grob_property ("stem"));
62   Grob *beam = Stem::get_beam (stem);
63   
64   Real dydx;
65   if (beam)
66     {
67       Real dy = 0;
68       SCM s = beam->get_grob_property ("positions");
69       if (is_number_pair (s))
70         {
71           dy = -gh_scm2double (gh_car (s)) +gh_scm2double (gh_cdr (s));
72         }
73       
74       Real dx = Beam::last_visible_stem (beam)->relative_coordinate (0, X_AXIS)
75         - Beam::first_visible_stem (beam)->relative_coordinate (0, X_AXIS);
76       dydx = dx ? dy/dx : 0;
77     }
78   else
79     // urg
80     dydx = 0.25;
81
82   Real ss = Staff_symbol_referencer::staff_space (me);
83   Real thick = robust_scm2double (me->get_grob_property ("beam-thickness"),1);
84   Real width = robust_scm2double (me->get_grob_property ("beam-width"),1);
85   Real blot = me->get_paper ()->get_realvar (ly_symbol2scm ("blotdiameter"));
86
87   width *= ss;
88   thick *= ss;
89   
90   Molecule a (Lookup::beam (dydx, width, thick, blot));
91   a.translate (Offset (-width/2, width / 2 * dydx));
92   
93   int tremolo_flags = 0;
94   SCM s = me->get_grob_property ("flag-count");
95   if (gh_number_p (s))
96     tremolo_flags = gh_scm2int (s);
97
98   if (!tremolo_flags)
99     {
100       programming_error ("No tremolo flags?");
101
102       me->suicide();
103       return Molecule ();
104     }
105
106   /*
107     Who the fuck is 0.81 ?
108
109     --hwn.
110    */
111   Real beam_translation = beam ? Beam::get_beam_translation (beam) : 0.81;
112
113   Molecule mol; 
114   for (int i = 0; i < tremolo_flags; i++)
115     {
116       Molecule b (a);
117       b.translate_axis (beam_translation * i, Y_AXIS);
118       mol.add_molecule (b);
119     }
120   return mol;
121 }
122
123
124 MAKE_SCHEME_CALLBACK (Stem_tremolo,brew_molecule,1);
125 SCM
126 Stem_tremolo::brew_molecule (SCM grob) 
127 {
128   Grob *me = unsmob_grob (grob);
129   Grob *stem = unsmob_grob (me->get_grob_property ("stem"));
130   Grob *beam = Stem::get_beam (stem);
131   Direction stemdir = Stem::get_direction (stem);
132   Real beam_translation = beam ? Beam::get_beam_translation (beam) : 0.81;
133
134   Molecule mol = raw_molecule (me);
135   Interval mol_ext = mol.extent (Y_AXIS);
136   Real ss = Staff_symbol_referencer::staff_space (me);
137
138   // ugh, rather calc from Stem_tremolo_req
139   int beam_count = (beam) ? (Stem::beam_multiplicity (stem).length ()+ 1): 0;
140
141   /*
142     TODO.
143    */
144
145
146   Real beamthickness = 0.0;
147   SCM sbt = (beam) ? beam->get_grob_property ("thickness") : SCM_EOL ;
148   if (gh_number_p (sbt))
149     {
150       beamthickness = gh_scm2double (sbt) * ss;
151     }
152
153   Real end_y
154     = Stem::stem_end_position (stem) *ss/2 
155     - stemdir * (beam_count * beamthickness
156                  + ((beam_count -1) >? 0) * beam_translation);
157
158   /*
159     the 0.33 ss is to compensate for the size of the note head
160    */
161   Real chord_start_y = Stem::chord_start_y (stem) +
162     0.33 * ss * stemdir;
163
164   Real padding = beam_translation;
165
166   /*
167     if there is a flag, just above/below the notehead.
168     if there is not enough space, center on remaining space,
169     else one beamspace away from stem end.
170    */
171   if (!beam && Stem::duration_log (stem) >= 3)
172     {
173       mol.align_to (Y_AXIS, -stemdir);
174       mol.translate_axis (chord_start_y + .5 * stemdir, Y_AXIS);
175     }
176   else if (stemdir * (end_y - chord_start_y) - 2*padding - mol_ext.length ()  < 0.0)
177     {
178       mol.translate_axis (0.5 * (end_y + chord_start_y)  - mol_ext.center (),Y_AXIS);
179     }
180   else
181     {
182       mol.translate_axis (end_y - stemdir * beam_translation
183                           -mol_ext [stemdir]
184                           , Y_AXIS);
185     }
186   
187   return mol.smobbed_copy ();
188 }
189
190
191 void
192 Stem_tremolo::set_stem (Grob*me,Grob *s)
193 {
194   me->set_grob_property ("stem", s->self_scm ());
195 }
196
197
198 ADD_INTERFACE (Stem_tremolo,"stem-tremolo-interface",
199   "",
200   "stem beam-width beam-thickness flag-count");