]> git.donarmstrong.com Git - lilypond.git/blob - lily/tuplet-spanner.cc
release: 1.3.28
[lilypond.git] / lily / tuplet-spanner.cc
1 /*
2   plet-spanner.cc -- implement Tuplet_spanner
3
4   source file of the GNU LilyPond music typesetter
5
6   (c)  1997--2000 Jan Nieuwenhuizen <janneke@gnu.org>
7 */
8
9 #include "atom.hh"
10 #include "beam.hh"
11 #include "box.hh"
12 #include "debug.hh"
13 #include "lookup.hh"
14 #include "molecule.hh"
15 #include "paper-column.hh"
16 #include "paper-def.hh"
17 #include "tuplet-spanner.hh"
18 #include "stem.hh"
19 #include "note-column.hh"
20 #include "dimensions.hh"
21 #include "group-interface.hh"
22 #include "directional-element-interface.hh"
23
24
25 Tuplet_spanner::Tuplet_spanner ()
26 {
27   set_elt_property ("beams", SCM_EOL);
28   set_elt_property ("columns", SCM_EOL);  
29 }
30
31 /*
32   TODO. 
33  */
34 Molecule*
35 Tuplet_spanner::do_brew_molecule_p () const
36 {
37   Molecule* mol_p = new Molecule;
38
39   // Default behaviour: number always, bracket when no beam!
40   bool par_beam = to_boolean (get_elt_property ("parallel-beam"));
41   bool bracket_visibility = !par_beam;
42   bool number_visibility = true;
43
44   SCM bracket = get_elt_property ("tuplet-bracket-visibility");
45   if (gh_boolean_p (bracket))
46     {
47       bracket_visibility = gh_scm2bool (bracket);
48     }
49   else if (bracket == ly_symbol2scm ("if-no-beam"))
50     bracket_visibility = !par_beam;
51
52   SCM numb = get_elt_property ("tuplet-number-visibility");  
53   if (gh_boolean_p (numb))
54     {
55       number_visibility = gh_scm2bool (numb);
56     }
57   else if (bracket == ly_symbol2scm ("if-no-beam"))
58     number_visibility = !par_beam;
59   
60   if (gh_pair_p (get_elt_property ("columns")))
61     {
62       Link_array<Note_column> column_arr=
63         Group_interface__extract_elements (this, (Note_column*)0, "columns");
64         
65       Real ncw = column_arr.top ()->extent(X_AXIS).length ();
66       Real w = spanner_length () + ncw;
67
68
69       Real staff_space = paper_l ()->get_var ("interline");
70       Direction dir = directional_element (this).get ();
71       Real dy = gh_scm2double (get_elt_property ("delta-y"));
72       SCM number = get_elt_property ("text");
73       if (gh_string_p (number) && number_visibility)
74         {
75           Molecule
76             num (lookup_l ()->text ("italic",
77                                     ly_scm2string (number), paper_l ()));
78           num.align_to (X_AXIS, CENTER);
79           num.translate_axis (w/2, X_AXIS);
80           num.align_to (Y_AXIS, CENTER);
81           num.translate_axis (dir * staff_space, Y_AXIS);
82         
83           num.translate_axis (dy/2, Y_AXIS);
84
85           mol_p->add_molecule (num);
86         }
87       
88       Real thick = paper_l ()->get_var ("tuplet_thick");
89       if (bracket_visibility)      
90         {
91           Real gap = paper_l () -> get_var ("tuplet_spanner_gap");
92           Real height = staff_space;
93           SCM at =gh_list(ly_symbol2scm ("tuplet"),
94                                        gh_double2scm (height),
95                                        gh_double2scm (gap),
96                                        gh_double2scm (w),
97                                        gh_double2scm (dy),
98                                        gh_double2scm (thick),
99                                        gh_int2scm (dir),
100                                        SCM_UNDEFINED);
101
102           mol_p->add_atom (at);
103         }
104
105       mol_p->translate_axis (dir * staff_space, Y_AXIS);
106     }
107   return mol_p;
108 }
109   
110 void
111 Tuplet_spanner::do_add_processing ()
112 {
113   if (gh_pair_p (get_elt_property ("columns")))
114     {
115       Link_array<Note_column> column_arr=
116         Group_interface__extract_elements (this, (Note_column*)0, "columns");
117       
118       set_bounds (LEFT, column_arr[0]);
119       set_bounds (RIGHT, column_arr.top ());  
120     }
121 }
122
123
124
125 /*
126   use first -> last note for slope, and then correct for disturbing
127   notes in between.  */
128 void
129 Tuplet_spanner::calc_position_and_height (Real *offset, Real * dy) const
130 {
131   Link_array<Note_column> column_arr=
132     Group_interface__extract_elements (this, (Note_column*)0, "columns");
133
134  
135   Direction d = directional_element (this).get ();
136
137   /*
138     Use outer non-rest columns to determine slope
139    */
140   int l = 0;
141   while (l <column_arr.size() && column_arr[l]->rest_b())
142     l ++;
143
144   int r = column_arr.size ()- 1;
145   while (r >= l && column_arr[r]->rest_b())
146     r--;
147   
148   if (l < r)
149     {
150       *dy = column_arr[r]->extent (Y_AXIS) [d]
151         - column_arr[l]->extent (Y_AXIS) [d];
152     }
153   else
154     * dy = 0;
155
156
157   *offset = - d * infinity_f;
158   
159   Real x0 = column_arr[0]->hpos_f ();
160   Real x1 = column_arr.top ()->hpos_f ();
161   
162   Real factor = column_arr.size () > 1 ? 1/(x1 - x0) : 1.0;
163   
164   for (int i = 0; i < column_arr.size ();  i++)
165     {
166       Real notey = column_arr[i]->extent (Y_AXIS)[d];
167       Real x = column_arr[i]->hpos_f () - x0;
168       Real tuplety =  *dy * x * factor;
169
170       if (notey * d > (*offset + tuplety) * d)
171         *offset = notey - tuplety; 
172     }
173 }
174
175 /*
176   use first -> last note for slope,
177 */
178 void
179 Tuplet_spanner::calc_dy (Real * dy) const
180 {
181   Link_array<Note_column> column_arr=
182     Group_interface__extract_elements (this, (Note_column*)0, "columns");
183
184  
185   Direction d = directional_element (this).get ();
186   *dy = column_arr.top ()->extent (Y_AXIS) [d]
187     - column_arr[0]->extent (Y_AXIS) [d];
188 }
189
190 void
191 Tuplet_spanner::do_post_processing ()
192 {
193   Link_array<Note_column> column_arr=
194     Group_interface__extract_elements (this, (Note_column*)0, "columns");
195
196   if (!column_arr.size ())
197     {
198       set_elt_property ("transparent", SCM_BOOL_T);
199       set_empty (X_AXIS);
200       set_empty (Y_AXIS);
201     }
202
203   Direction d =   directional_element (this).get ();
204   if (!d)
205     {
206       d = get_default_dir ();
207       directional_element (this).set (d);
208
209     }
210   Real dy, offset;
211
212 calc_position_and_height  (&offset,&dy);
213   // calc_position (&offset, dy);
214   
215   set_elt_property ("delta-y", gh_double2scm (dy));
216
217   translate_axis (offset, Y_AXIS);
218   
219   if (scm_ilength (get_elt_property ("beams")) == 1)
220     {
221       SCM bs = get_elt_property ("beams");
222       Score_element *b = unsmob_element (gh_car (bs));
223       Beam * beam_l = dynamic_cast<Beam*> (b);
224       if (!broken_b () 
225           && spanned_drul_[LEFT]->column_l () == beam_l->spanned_drul_[LEFT]->column_l ()
226           && spanned_drul_[RIGHT]->column_l () == beam_l->spanned_drul_[RIGHT]->column_l ())
227         set_elt_property ("parallel-beam", SCM_BOOL_T);
228     }
229 }
230
231
232 Direction
233 Tuplet_spanner::get_default_dir () const
234 {
235   Direction d = UP;
236   SCM dir_sym =get_elt_property ("dir-forced");
237   if (isdir_b (dir_sym))
238     {
239       d= to_dir (dir_sym);
240       if (d != CENTER)
241         return d;
242     }
243
244   d = UP ;
245   for (SCM s = get_elt_property ("columns"); gh_pair_p (s); s = gh_cdr (s))
246     {
247       Score_element * sc = unsmob_element (gh_car (s));
248       Note_column * nc = dynamic_cast<Note_column*> (sc);
249       if (nc->dir () < 0) 
250         {
251           d = DOWN;
252           break;
253         }
254     }
255   
256   return d;
257 }
258
259 void
260 Tuplet_spanner::add_beam (Beam *b)
261 {
262   add_dependency (b);
263   Group_interface gi (this, "beams");
264   gi.add_element (b);
265 }
266
267 void
268 Tuplet_spanner::add_column (Note_column*n)
269 {
270   Group_interface gi (this, "columns");
271   gi.add_element (n);
272
273   add_dependency (n);
274 }
275
276