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