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