]> git.donarmstrong.com Git - lilypond.git/blob - lily/tie-column.cc
* input/regression/tie-chord.ly (testLong): add a chord in 4ths
[lilypond.git] / lily / tie-column.cc
1 /*
2   tie-column.cc -- implement Tie_column
3
4   source file of the GNU LilyPond music typesetter
5
6   (c) 2000--2005 Han-Wen Nienhuys <hanwen@cs.uu.nl>
7 */
8
9 #include "tie-column.hh"
10
11 #include <math.h>
12
13 #include <map>
14 #include <set>
15
16
17 #include "stencil.hh"
18 #include "stem.hh"
19 #include "skyline.hh"
20 #include "staff-symbol-referencer.hh"
21 #include "warn.hh"
22 #include "paper-column.hh"
23 #include "spanner.hh"
24 #include "pointer-group-interface.hh"
25 #include "tie.hh"
26 #include "directional-element-interface.hh"
27 #include "rhythmic-head.hh"
28
29 void
30 Tie_column::add_tie (Grob *me, Grob *tie)
31 {
32   if (tie->get_parent (Y_AXIS)
33       && Tie_column::has_interface (tie->get_parent (Y_AXIS)))
34     return;
35
36   if (!Pointer_group_interface::count (me, ly_symbol2scm ("ties")))
37     {
38       dynamic_cast<Spanner *> (me)->set_bound (LEFT, Tie::head (tie, LEFT));
39       dynamic_cast<Spanner *> (me)->set_bound (RIGHT, Tie::head (tie, RIGHT));
40     }
41
42   tie->set_parent (me, Y_AXIS);
43   Pointer_group_interface::add_grob (me, ly_symbol2scm ("ties"), tie);
44   tie->add_dependency (me);
45 }
46
47 void
48 Tie_column::set_directions (Grob *me)
49 {
50   if (!to_boolean (me->get_property ("positioning-done")))
51     {
52       me->set_property ("positioning-done", SCM_BOOL_T); 
53       new_directions (me);
54     }
55 }
56
57 int
58 Tie::compare (Grob *const &s1,
59               Grob *const &s2)
60 {
61   return sign (Tie::get_position (s1) - Tie::get_position (s2));
62 }
63
64 MAKE_SCHEME_CALLBACK (Tie_column, after_line_breaking, 1);
65 SCM
66 Tie_column::after_line_breaking (SCM smob)
67 {
68   set_directions (unsmob_grob (smob));
69   return SCM_UNSPECIFIED;
70 }
71
72 /*
73   Extend the spanner over its Tie constituents.
74 */
75 MAKE_SCHEME_CALLBACK (Tie_column, before_line_breaking, 1);
76 SCM
77 Tie_column::before_line_breaking (SCM smob)
78 {
79   Spanner *me = dynamic_cast<Spanner *> (unsmob_grob (smob));
80   for (SCM s = me->get_property ("ties"); scm_is_pair (s); s = scm_cdr (s))
81     {
82       Spanner *tie = dynamic_cast<Spanner *> (unsmob_grob (scm_car (s)));
83       Direction dir = LEFT;
84       do
85         {
86           if (dir * tie->get_bound (dir)->get_column ()->get_rank ()
87               > dir * me->get_bound (dir)->get_column ()->get_rank ())
88             me->set_bound (dir, Tie::head (tie, dir));
89         }
90       while (flip (&dir) != LEFT);
91     }
92   return SCM_UNSPECIFIED;
93 }
94
95
96
97 void
98 set_chord_outlines (Drul_array< Array<Skyline_entry> > *skyline_drul,
99                     Link_array<Grob> ties,
100                     Grob *common)
101 {
102   Direction d = LEFT;
103
104   Real staff_space = Staff_symbol_referencer::staff_space (ties[0]);
105   do
106     {
107       Array<Box> boxes;
108       Interval x_union;
109
110       Grob *stem = 0; 
111       for (int i = 0; i < ties.size (); i++)
112         {
113           Spanner *tie = dynamic_cast<Spanner*> (ties[i]);
114
115           Grob *head = Tie::head (tie, d);
116           if (!head)
117             continue;
118
119           if (!stem)
120             stem = unsmob_grob (head->get_object ("stem"));
121           
122           Real p = Tie::get_position (tie);
123           Interval y ((p-1) * 0.5 * staff_space,
124                       (p+1) * 0.5 * staff_space);
125
126           Interval x = head->extent (common, X_AXIS);
127           boxes.push (Box (x, y));
128           x_union.unite (x);
129         }
130
131       (*skyline_drul)[d] = empty_skyline (-d);
132       
133       Spanner *tie = dynamic_cast<Spanner*> (ties[0]);
134       if (tie->get_bound (d)->break_status_dir ())
135         {
136           Real x = robust_relative_extent (tie->get_bound (d),
137                                            common,
138                                            X_AXIS)[-d];
139
140           (*skyline_drul)[d].elem_ref (0).height_ = x; 
141         }
142           
143       for (int i = 0; i < boxes.size (); i++)
144         insert_extent_into_skyline (&skyline_drul->elem_ref (d),
145                                     boxes[i], Y_AXIS, -d);
146       if (stem
147           && !Stem::is_invisible (stem))
148         {
149           Interval x;
150           x.add_point (stem->relative_coordinate (common, X_AXIS));
151           x.widen (staff_space / 20); // ugh.
152           Interval y;
153           y.add_point (Stem::stem_end_position (stem) * staff_space * .5);
154
155           Direction stemdir = Stem::get_direction (stem);
156           y.add_point (Stem::head_positions (stem)[-stemdir]
157                        * staff_space * .5);
158           
159           insert_extent_into_skyline (&skyline_drul->elem_ref (d),
160                                       Box (x,y), Y_AXIS, -d);
161
162
163
164           if (d == LEFT)
165             {
166               Box flag_box = Stem::get_translated_flag (stem).extent_box ();
167               flag_box.translate( Offset (x[RIGHT], X_AXIS));
168               insert_extent_into_skyline (&skyline_drul->elem_ref (d),
169                                           flag_box,
170                                           Y_AXIS, -d);
171             }
172         }
173
174       
175
176
177       Direction updowndir = DOWN;
178       do
179         {
180           Interval x ;
181           if (boxes.size())
182             {
183               Box b = boxes.boundary (updowndir, 0);
184               x = b[X_AXIS];
185               x[-d] =  b[X_AXIS].linear_combination (-d / 2);
186             }
187           
188           if (!x.is_empty ())
189             (*skyline_drul)[d].boundary (updowndir, 0).height_ = x[-d]; 
190         }
191       while (flip (&updowndir) != DOWN);
192
193       for (int i = 0; i < ties.size (); i++)
194         {
195           Spanner *tie = dynamic_cast<Spanner*> (ties[i]);
196           Grob *head = Tie::head (tie, d);
197           if (!head)
198             continue;
199
200           Grob *dots = unsmob_grob (head->get_object ("dot"));
201           if (dots && d == LEFT)
202             {
203               Interval x = dots->extent (common, X_AXIS);
204               Real p = Staff_symbol_referencer::get_position (dots);
205               
206               Interval y (-1,1);
207               y *= (staff_space /4);
208               y.translate (p * staff_space * .5);
209
210               insert_extent_into_skyline (&skyline_drul->elem_ref (d),
211                                           Box (x,y), Y_AXIS, -d);
212             }
213         }
214       
215     }
216   while (flip (&d) != LEFT);
217 }
218
219
220 void
221 Tie_column::new_directions (Grob *me)
222 {
223   extract_grob_set (me, "ties", ro_ties);
224   Link_array<Grob> ties (ro_ties);
225   if (!ties.size ())
226     return;
227
228   if (ties.size() == 1)
229     {
230       Tie::set_default_control_points (ties[0]);
231       return ;
232     }
233   
234   ties.sort (&Tie::compare);
235
236   Array<Tie_configuration> tie_configs;
237   for (int i = 0; i < ties.size (); i++)
238     {
239       Tie_configuration conf;
240       conf.dir_ = get_grob_direction (ties[i]);
241       conf.position_ = Tie::get_position (ties[i]);
242       tie_configs.push (conf);
243     }
244
245   SCM manual_configs = me->get_property ("tie-configuration");
246   bool manual_override = false;
247   int k = 0;
248   for (SCM s = manual_configs;
249        scm_is_pair (s) && k < tie_configs.size(); s = scm_cdr (s))
250     {
251       SCM entry = scm_car (s);
252       if (!scm_is_pair (entry))
253         continue;
254
255       manual_override = true;
256       tie_configs[k].position_ = robust_scm2double (scm_car (entry), tie_configs[k].position_);
257       tie_configs[k].dir_ = Direction (robust_scm2int (scm_cdr (entry), tie_configs[k].dir_));
258       k ++;
259     }
260
261   if (!tie_configs[0].dir_)
262     tie_configs[0].dir_ = DOWN;
263   if (!tie_configs.top().dir_)
264     tie_configs.top().dir_ = UP;
265
266   /*
267     Seconds
268    */
269   for (int i = 1; i < tie_configs.size(); i++)
270     {
271       if (fabs (tie_configs[i-1].position_ - tie_configs[i].position_) <= 1)
272         {
273           if (!tie_configs[i-1].dir_)
274             tie_configs[i-1].dir_ = DOWN;
275           if (!tie_configs[i].dir_)
276             tie_configs[i].dir_ = UP;
277         }
278     }
279
280   for (int i = 1; i < tie_configs.size() - 1; i++)
281     {
282       if (tie_configs[i].dir_)
283         continue;
284
285       Direction position_dir = (Direction) sign (tie_configs[i].position_);
286       if (!position_dir)
287         position_dir = DOWN;
288       
289       tie_configs[i].dir_ = position_dir;
290     }
291
292   Grob *common = me;
293   for (int i = 0; i < ties.size (); i++)
294     {
295       common = dynamic_cast<Spanner*> (ties[i])->get_bound (LEFT)->common_refpoint (common, X_AXIS); 
296       common = dynamic_cast<Spanner*> (ties[i])->get_bound (RIGHT)->common_refpoint (common, X_AXIS); 
297     }
298
299   Drul_array< Array<Skyline_entry> > skylines;
300   set_chord_outlines (&skylines, ties, common);
301   
302   Tie_details details;
303   details.init (ties[0]);
304
305   /*
306     Let the ties flow out, according to our single-tie formatting.
307    */
308   if (!manual_override)
309     {
310       Tie::get_configuration (ties[0], common, &tie_configs.elem_ref (0),
311                               &skylines,
312                               details
313                               );
314       Tie::get_configuration (ties.top (), common,
315                               &tie_configs.elem_ref (tie_configs.size()-1),
316                               &skylines,
317                               details
318                               );
319     }
320
321   /*
322     Calculate final width and shape of the ties.
323    */
324   Real staff_space = Staff_symbol_referencer::staff_space (ties[0]);
325   Real gap = robust_scm2double (ties[0]->get_property ("x-gap"), 0.2);
326   for (int i = 0; i < ties.size(); i++)
327     {
328       if (!manual_override
329           && (i == 0 || i == ties.size () -1))
330         continue;
331       
332       Tie_configuration conf = tie_configs[i];
333       conf = tie_configs[i];
334
335       Real line_dy = 0.0;
336       bool on_line = Staff_symbol_referencer::on_staffline (ties[0],
337                                                             int (rint (conf.position_)));
338       if (on_line)
339         line_dy = - sign (conf.height (details) - 0.6 * staff_space)
340           * 0.2 * staff_space * conf.dir_;
341
342       Real y = conf.position_ * staff_space * 0.5
343         + line_dy;
344       conf.attachment_x_
345         = get_skyline_attachment (skylines, y);
346       conf.attachment_x_.intersect (get_skyline_attachment (skylines,
347                                                             y + conf.dir_ * staff_space * .5));
348
349
350       conf.delta_y_ += line_dy;
351       conf.attachment_x_.widen (-gap);
352       if (!on_line
353           && Staff_symbol_referencer::staff_radius (ties[0]) * staff_space > y)
354         conf.center_tie_vertically (details);
355               
356       tie_configs[i] = conf;
357     }
358
359   /*
360     Try to shift small ties into available spaces.
361    */
362   if (!manual_override)
363     {
364       set<int> positions_taken; 
365       for (int i = 0; i < tie_configs.size (); i++)
366         positions_taken.insert (int (rint (tie_configs[i].position_)));
367
368       for (int i = 0; i < tie_configs.size (); i++)
369         {
370           Tie_configuration * conf = &tie_configs.elem_ref (i);
371
372           /*
373             on staff line and small enough, translate a little further 
374            */
375           Real h = conf->height (details);
376           bool next_free = positions_taken.find (int (rint (conf->position_ + conf->dir_)))
377             == positions_taken.end ();
378           bool on_line = Staff_symbol_referencer::on_staffline (ties[0],
379                                                                 int (rint (conf->position_ + conf->delta_y_)));
380           if (next_free)
381             if (on_line && h < 0.4 * staff_space)
382               {
383                 positions_taken.insert (int (rint (conf->position_ + conf->dir_)));
384                 conf->delta_y_ += 0.2 * staff_space * conf->dir_;
385               }
386             else if (!on_line && h > 0.6 * staff_space)
387               {
388                 positions_taken.insert (int (rint (conf->position_ + conf->dir_)));
389                 conf->delta_y_ += 0.5 * staff_space * conf->dir_;
390               }
391         }
392     }
393   
394   for (int i = 0; i < ties.size(); i++)
395     {
396       Tie::set_control_points (ties[i], common, tie_configs[i],
397                                details
398                                );
399       set_grob_direction (ties[i], tie_configs[i].dir_);
400     }
401 }
402
403
404 ADD_INTERFACE (Tie_column, "tie-column-interface",
405                "Object that sets directions of multiple ties in a tied chord",
406                "positioning-done "
407                "tie-configuration "
408                );