]> git.donarmstrong.com Git - lilypond.git/blob - lily/tuplet-spanner.cc
release: 1.3.19
[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   SCM visibility_sym =get_elt_property ("tuplet-visibility");
44   if (gh_number_p (visibility_sym))
45     {
46       /*
47         ARG. Fixme.
48        */
49       
50       /* Property values:
51          0       show nothing
52          1       show number
53          2       show (number and bracket)-if-no-beam
54          3       show number, and bracket-if-no-beam
55          4       show number, and bracket
56       */
57       int value = gh_scm2int ((visibility_sym));
58       bracket_visibility = (value == 4 || (value > 1 && !par_beam));
59       number_visibility = (value > 2 || value == 1 || 
60                            (value == 2 && !par_beam));
61     }
62   
63   if (gh_pair_p (get_elt_property ("columns")))
64     {
65       Link_array<Note_column> column_arr=
66         Group_interface__extract_elements (this, (Note_column*)0, "columns");
67         
68       Real ncw = column_arr.top ()->extent(X_AXIS).length ();
69       Real w = spanner_length () + ncw;
70
71
72       Real staff_space = paper_l ()->get_var ("interline");
73       Direction dir = directional_element (this).get ();
74       Real dy = gh_scm2double (get_elt_property ("delta-y"));
75       SCM number = get_elt_property ("text");
76       if (gh_string_p (number))
77         {
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_p->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         
97           mol_p->add_molecule (lookup_l ()->tuplet_bracket (dy, w, thick, gap, staff_space, dir));
98         }
99
100       mol_p->translate_axis (dir * staff_space, Y_AXIS);
101     }
102   return mol_p;
103 }
104   
105 void
106 Tuplet_spanner::do_add_processing ()
107 {
108   if (gh_pair_p (get_elt_property ("columns")))
109     {
110       Link_array<Note_column> column_arr=
111         Group_interface__extract_elements (this, (Note_column*)0, "columns");
112       
113       set_bounds (LEFT, column_arr[0]);
114       set_bounds (RIGHT, column_arr.top ());  
115     }
116 }
117
118 /*
119   use first -> last note for slope, and then correct for disturbing
120   notes in between.  */
121 void
122 Tuplet_spanner::calc_position_and_height (Real *offset, Real * dy) const
123 {
124   Link_array<Note_column> column_arr=
125     Group_interface__extract_elements (this, (Note_column*)0, "columns");
126
127   
128   Direction d = directional_element (this).get ();
129   *dy = column_arr.top ()->extent (Y_AXIS) [d]
130     - column_arr[0]->extent (Y_AXIS) [d];
131
132   *offset = - d * infinity_f;
133   
134   Real x0 = column_arr[0]->hpos_f ();
135   Real x1 = column_arr.top ()->hpos_f ();
136   for (int i = 0; i < column_arr.size ();  i++)
137     {
138       Real notey = column_arr[i]->extent (Y_AXIS)[d];
139       Real x = column_arr[i]->hpos_f () - x0;
140       Real tuplety =  *dy * x / (x1 -x0);
141
142       if (notey * d > (*offset + tuplety) * d)
143         *offset = notey - tuplety; 
144     }
145 }
146
147 void
148 Tuplet_spanner::do_post_processing ()
149 {
150
151   Link_array<Note_column> column_arr=
152     Group_interface__extract_elements (this, (Note_column*)0, "columns");
153
154   if (!column_arr.size ())
155     {
156       set_elt_property ("transparent", SCM_BOOL_T);
157       set_empty (X_AXIS);
158       set_empty (Y_AXIS);
159     }
160
161   Direction d =   directional_element (this).get ();
162   if (!d)
163     {
164       d = UP;
165       directional_element (this).set (d);
166
167     }
168   Real dy, offset;
169
170   calc_position_and_height (&offset, &dy);
171   set_elt_property ("delta-y", gh_double2scm (dy));
172
173   translate_axis (offset, Y_AXIS);
174   
175   if (scm_ilength (get_elt_property ("beams")) == 1)
176     {
177       SCM bs = get_elt_property ("beams");
178       Score_element *b = unsmob_element (gh_car (bs));
179       Beam * beam_l = dynamic_cast<Beam*> (b);
180       if (!broken_b () 
181           && spanned_drul_[LEFT]->column_l () == beam_l->spanned_drul_[LEFT]->column_l ()
182           && spanned_drul_[RIGHT]->column_l () == beam_l->spanned_drul_[RIGHT]->column_l ())
183         set_elt_property ("parallel-beam", SCM_BOOL_T);
184     }
185 }
186
187
188 Direction
189 Tuplet_spanner::get_default_dir () const
190 {
191   Direction d = UP;
192   SCM dir_sym =get_elt_property ("dir-forced");
193   if (gh_number_p (dir_sym))
194     {
195       d= to_dir (dir_sym);
196       if (d != CENTER)
197         return d;
198     }
199
200   for (SCM s = get_elt_property ("columns"); gh_pair_p (s); s = gh_cdr (s))
201     {
202       Score_element * sc = unsmob_element (gh_car (s));
203       Note_column * nc = dynamic_cast<Note_column*> (sc);
204       if (nc->dir () < 0) 
205         {
206           d = DOWN;
207           break;
208         }
209     }
210   
211   return d;
212 }
213
214 void
215 Tuplet_spanner::add_beam (Beam *b)
216 {
217   add_dependency (b);
218   Group_interface gi (this, "beams");
219   gi.add_element (b);
220 }
221
222 void
223 Tuplet_spanner::add_column (Note_column*n)
224 {
225   Group_interface gi (this, "columns");
226   gi.add_element (n);
227
228   add_dependency (n);
229 }
230
231