]> git.donarmstrong.com Git - lilypond.git/blob - lily/tuplet-spanner.cc
release: 1.3.22
[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 "beam.hh"
10 #include "box.hh"
11 #include "debug.hh"
12 #include "lookup.hh"
13 #include "molecule.hh"
14 #include "paper-column.hh"
15 #include "paper-def.hh"
16 #include "tuplet-spanner.hh"
17 #include "stem.hh"
18 #include "note-column.hh"
19 #include "dimensions.hh"
20 #include "group-interface.hh"
21 #include "directional-element-interface.hh"
22
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         
93           mol_p->add_molecule (lookup_l ()->tuplet_bracket (dy, w, thick, gap, staff_space, dir));
94         }
95
96       mol_p->translate_axis (dir * staff_space, Y_AXIS);
97     }
98   return mol_p;
99 }
100   
101 void
102 Tuplet_spanner::do_add_processing ()
103 {
104   if (gh_pair_p (get_elt_property ("columns")))
105     {
106       Link_array<Note_column> column_arr=
107         Group_interface__extract_elements (this, (Note_column*)0, "columns");
108       
109       set_bounds (LEFT, column_arr[0]);
110       set_bounds (RIGHT, column_arr.top ());  
111     }
112 }
113
114 /*
115   use first -> last note for slope, and then correct for disturbing
116   notes in between.  */
117 void
118 Tuplet_spanner::calc_position_and_height (Real *offset, Real * dy) const
119 {
120   Link_array<Note_column> column_arr=
121     Group_interface__extract_elements (this, (Note_column*)0, "columns");
122
123  
124   Direction d = directional_element (this).get ();
125   *dy = column_arr.top ()->extent (Y_AXIS) [d]
126     - column_arr[0]->extent (Y_AXIS) [d];
127
128   *offset = - d * infinity_f;
129   
130   Real x0 = column_arr[0]->hpos_f ();
131   Real x1 = column_arr.top ()->hpos_f ();
132   
133   Real factor = column_arr.size () > 1 ? 1/(x1 - x0) : 1.0;
134   
135   for (int i = 0; i < column_arr.size ();  i++)
136     {
137       Real notey = column_arr[i]->extent (Y_AXIS)[d];
138       Real x = column_arr[i]->hpos_f () - x0;
139       Real tuplety =  *dy * x * factor;
140
141       if (notey * d > (*offset + tuplety) * d)
142         *offset = notey - tuplety; 
143     }
144 }
145
146 void
147 Tuplet_spanner::do_post_processing ()
148 {
149
150   Link_array<Note_column> column_arr=
151     Group_interface__extract_elements (this, (Note_column*)0, "columns");
152
153   if (!column_arr.size ())
154     {
155       set_elt_property ("transparent", SCM_BOOL_T);
156       set_empty (X_AXIS);
157       set_empty (Y_AXIS);
158     }
159
160   Direction d =   directional_element (this).get ();
161   if (!d)
162     {
163       d = UP;
164       directional_element (this).set (d);
165
166     }
167   Real dy, offset;
168
169   calc_position_and_height (&offset, &dy);
170   set_elt_property ("delta-y", gh_double2scm (dy));
171
172   translate_axis (offset, Y_AXIS);
173   
174   if (scm_ilength (get_elt_property ("beams")) == 1)
175     {
176       SCM bs = get_elt_property ("beams");
177       Score_element *b = unsmob_element (gh_car (bs));
178       Beam * beam_l = dynamic_cast<Beam*> (b);
179       if (!broken_b () 
180           && spanned_drul_[LEFT]->column_l () == beam_l->spanned_drul_[LEFT]->column_l ()
181           && spanned_drul_[RIGHT]->column_l () == beam_l->spanned_drul_[RIGHT]->column_l ())
182         set_elt_property ("parallel-beam", SCM_BOOL_T);
183     }
184 }
185
186
187 Direction
188 Tuplet_spanner::get_default_dir () const
189 {
190   assert (false);
191   
192   Direction d = UP;
193   SCM dir_sym =get_elt_property ("dir-forced");
194   if (gh_number_p (dir_sym))
195     {
196       d= to_dir (dir_sym);
197       if (d != CENTER)
198         return d;
199     }
200
201   for (SCM s = get_elt_property ("columns"); gh_pair_p (s); s = gh_cdr (s))
202     {
203       Score_element * sc = unsmob_element (gh_car (s));
204       Note_column * nc = dynamic_cast<Note_column*> (sc);
205       if (nc->dir () < 0) 
206         {
207           d = DOWN;
208           break;
209         }
210     }
211   
212   return d;
213 }
214
215 void
216 Tuplet_spanner::add_beam (Beam *b)
217 {
218   add_dependency (b);
219   Group_interface gi (this, "beams");
220   gi.add_element (b);
221 }
222
223 void
224 Tuplet_spanner::add_column (Note_column*n)
225 {
226   Group_interface gi (this, "columns");
227   gi.add_element (n);
228
229   add_dependency (n);
230 }
231
232