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