lilypond-1.3.67
[lilypond.git] / lily / tuplet-spanner.cc
blob3acb6c68f3432e037cbd3e392e453a5265f98595
1 /*
2 plet-spanner.cc -- implement Tuplet_spanner
4 source file of the GNU LilyPond music typesetter
6 (c) 1997--2000 Jan Nieuwenhuizen <janneke@gnu.org>
7 */
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"
25 Tuplet_spanner::Tuplet_spanner (SCM s)
26 : Spanner (s)
28 set_elt_pointer ("beams", SCM_EOL);
29 set_elt_pointer ("columns", SCM_EOL);
31 // ugh.
32 set_elt_property ("delta-y", gh_int2scm (0));
36 TODO.
39 GLUE_SCORE_ELEMENT(Tuplet_spanner,brew_molecule);
40 SCM
41 Tuplet_spanner::member_brew_molecule () const
43 Molecule mol;
45 // Default behaviour: number always, bracket when no beam!
46 bool par_beam = to_boolean (get_elt_property ("parallel-beam"));
47 bool bracket_visibility = !par_beam;
48 bool number_visibility = true;
50 SCM bracket = get_elt_property ("tuplet-bracket-visibility");
51 if (gh_boolean_p (bracket))
53 bracket_visibility = gh_scm2bool (bracket);
55 else if (bracket == ly_symbol2scm ("if-no-beam"))
56 bracket_visibility = !par_beam;
58 SCM numb = get_elt_property ("tuplet-number-visibility");
59 if (gh_boolean_p (numb))
61 number_visibility = gh_scm2bool (numb);
63 else if (bracket == ly_symbol2scm ("if-no-beam"))
64 number_visibility = !par_beam;
66 if (gh_pair_p (get_elt_pointer ("columns")))
68 Link_array<Note_column> column_arr=
69 Pointer_group_interface__extract_elements (this, (Note_column*)0, "columns");
71 Real ncw = column_arr.top ()->extent(X_AXIS).length ();
72 Real w = spanner_length () + ncw;
75 Real staff_space = paper_l ()->get_var ("interline");
76 Direction dir = Directional_element_interface (this).get ();
77 Real dy = gh_scm2double (get_elt_property ("delta-y"));
78 SCM number = get_elt_property ("text");
79 if (gh_string_p (number) && number_visibility)
81 Molecule
82 num (lookup_l ()->text ("italic",
83 ly_scm2string (number), paper_l ()));
84 num.align_to (X_AXIS, CENTER);
85 num.translate_axis (w/2, X_AXIS);
86 num.align_to (Y_AXIS, CENTER);
87 num.translate_axis (dir * staff_space, Y_AXIS);
89 num.translate_axis (dy/2, Y_AXIS);
91 mol.add_molecule (num);
94 if (bracket_visibility)
96 SCM ss = paper_l ()->get_scmvar ("staffspace");
97 SCM lt = paper_l ()->get_scmvar ("stafflinethickness");
99 SCM thick = get_elt_property ("thick");
100 SCM gap = get_elt_property ("number-gap");
102 SCM at =gh_list(ly_symbol2scm ("tuplet"),
104 scm_product (gap, ss),
105 gh_double2scm (w),
106 gh_double2scm (dy),
107 scm_product (thick, lt),
108 gh_int2scm (dir),
109 SCM_UNDEFINED);
111 Box b;
112 mol.add_molecule (Molecule (b, at));
115 return mol.create_scheme();
122 use first -> last note for slope, and then correct for disturbing
123 notes in between. */
124 void
125 Tuplet_spanner::calc_position_and_height (Real *offset, Real * dy) const
127 Link_array<Note_column> column_arr=
128 Pointer_group_interface__extract_elements (this, (Note_column*)0, "columns");
131 Score_element * common = common_refpoint (get_elt_pointer ("columns"), Y_AXIS);
133 Direction d = Directional_element_interface (this).get ();
136 Use outer non-rest columns to determine slope
138 int l = 0;
139 while (l <column_arr.size() && column_arr[l]->rest_b())
140 l ++;
142 int r = column_arr.size ()- 1;
143 while (r >= l && column_arr[r]->rest_b())
144 r--;
146 if (l < r)
148 *dy = column_arr[r]->extent (Y_AXIS) [d] + column_arr[r]->relative_coordinate (common, Y_AXIS)
149 - column_arr[l]->extent (Y_AXIS) [d] - column_arr[l]->relative_coordinate (common, Y_AXIS);
151 else
152 * dy = 0;
155 *offset = - d * infinity_f;
157 if (!column_arr.size ())
158 return;
160 Real x0 = column_arr[0]->relative_coordinate (0, X_AXIS);
161 Real x1 = column_arr.top ()->relative_coordinate (0, X_AXIS);
163 Real factor = column_arr.size () > 1 ? 1/(x1 - x0) : 1.0;
165 for (int i = 0; i < column_arr.size (); i++)
167 Real notey = column_arr[i]->extent (Y_AXIS)[d] +
168 column_arr[i]->relative_coordinate (common, Y_AXIS)
170 Real x = column_arr[i]->relative_coordinate (0, X_AXIS) - x0;
171 Real tuplety = *dy * x * factor;
173 if (notey * d > (*offset + tuplety) * d)
174 *offset = notey - tuplety;
179 use first -> last note for slope,
181 void
182 Tuplet_spanner::calc_dy (Real * dy) const
184 Link_array<Note_column> column_arr=
185 Pointer_group_interface__extract_elements (this, (Note_column*)0, "columns");
188 Direction d = Directional_element_interface (this).get ();
189 *dy = column_arr.top ()->extent (Y_AXIS) [d]
190 - column_arr[0]->extent (Y_AXIS) [d];
193 GLUE_SCORE_ELEMENT(Tuplet_spanner,after_line_breaking);
195 Tuplet_spanner::member_after_line_breaking ()
197 Link_array<Note_column> column_arr=
198 Pointer_group_interface__extract_elements (this, (Note_column*)0, "columns");
200 if (!column_arr.size ())
202 suicide ();
203 return SCM_UNDEFINED;
206 Direction d = Directional_element_interface (this).get ();
207 if (!d)
209 d = get_default_dir ();
210 Directional_element_interface (this).set (d);
213 Real dy, offset;
215 calc_position_and_height (&offset,&dy);
217 set_elt_property ("delta-y", gh_double2scm (dy));
219 translate_axis (offset, Y_AXIS);
221 if (scm_ilength (get_elt_pointer ("beams")) == 1)
223 SCM bs = get_elt_pointer ("beams");
224 Score_element *b = unsmob_element (gh_car (bs));
225 Beam * beam_l = dynamic_cast<Beam*> (b);
226 if (!broken_b ()
227 && get_bound (LEFT)->column_l () == beam_l->get_bound (LEFT)->column_l ()
228 && get_bound (RIGHT)->column_l () == beam_l->get_bound (RIGHT)->column_l ())
229 set_elt_property ("parallel-beam", SCM_BOOL_T);
231 return SCM_UNDEFINED;
235 Direction
236 Tuplet_spanner::get_default_dir () const
238 Direction d = UP;
239 SCM dir_sym =get_elt_property ("dir-forced");
240 if (isdir_b (dir_sym))
242 d= to_dir (dir_sym);
243 if (d != CENTER)
244 return d;
247 d = UP ;
248 for (SCM s = get_elt_pointer ("columns"); gh_pair_p (s); s = gh_cdr (s))
250 Score_element * sc = unsmob_element (gh_car (s));
251 Note_column * nc = dynamic_cast<Note_column*> (sc);
252 if (nc->dir () < 0)
254 d = DOWN;
255 break;
259 return d;
262 void
263 Tuplet_spanner::add_beam (Beam *b)
265 add_dependency (b);
266 Pointer_group_interface gi (this, "beams");
267 gi.add_element (b);
270 void
271 Tuplet_spanner::add_column (Note_column*n)
273 Pointer_group_interface gi (this, "columns");
274 gi.add_element (n);
276 add_dependency (n);
278 add_bound_item (this, n);