]> git.donarmstrong.com Git - lilypond.git/blob - lily/tie-column.cc
(print): try Tie_column::set_directions () if
[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           Interval x ;
137           if (boxes.size())
138             {
139               Box b = boxes.boundary (updowndir, 0);
140               x = b[X_AXIS];
141               x[-d] =  b[X_AXIS].linear_combination (-d / 2);
142             }
143           
144           if (stem
145               && !Stem::is_invisible (stem)
146               && updowndir == get_grob_direction (stem))
147             x.unite (robust_relative_extent (stem, common, X_AXIS));
148
149           if (!x.is_empty ())
150             (*skyline_drul)[d].boundary (updowndir, 0).height_ = x[-d]; 
151         }
152       while (flip (&updowndir) != DOWN);
153
154       for (int i = 0; i < ties.size (); i++)
155         {
156           Spanner *tie = dynamic_cast<Spanner*> (ties[i]);
157           Grob *head = Tie::head (tie, d);
158           if (!head)
159             continue;
160
161           Grob *dots = unsmob_grob (head->get_object ("dot"));
162           if (dots && d == LEFT)
163             {
164               Interval x = dots->extent (common, X_AXIS);
165               Real p = Staff_symbol_referencer::get_position (dots);
166               
167               Interval y (-1,1);
168               y *= (staff_space /4);
169               y.translate (p * staff_space * .5);
170
171               insert_extent_into_skyline (&skyline_drul->elem_ref (d),
172                                           Box (x,y), Y_AXIS, -d);
173             }
174         }
175       
176     }
177   while (flip (&d) != LEFT);
178 }
179
180
181 void
182 Tie_column::new_directions (Grob *me)
183 {
184   extract_grob_set (me, "ties", ro_ties);
185   Link_array<Grob> ties (ro_ties);
186   if (!ties.size ())
187     return;
188
189   if (ties.size() == 1)
190     {
191       Tie::set_default_control_points (ties[0]);
192       return ;
193     }
194   
195   ties.sort (&Tie::compare);
196
197   Array<Tie_configuration> tie_configs;
198   for (int i = 0; i < ties.size (); i++)
199     {
200       Tie_configuration conf;
201       conf.dir_ = get_grob_direction (ties[i]);
202       conf.position_ = Tie::get_position (ties[i]);
203       tie_configs.push (conf);
204     }
205
206   SCM manual_configs = me->get_property ("tie-configuration");
207   bool manual_override = false;
208   int k = 0;
209   for (SCM s = manual_configs;
210        scm_is_pair (s) && k < tie_configs.size(); s = scm_cdr (s))
211     {
212       SCM entry = scm_car (s);
213       if (!scm_is_pair (entry))
214         continue;
215
216       manual_override = true;
217       tie_configs[k].position_ = robust_scm2double (scm_car (entry), tie_configs[k].position_);
218       tie_configs[k].dir_ = Direction (robust_scm2int (scm_cdr (entry), tie_configs[k].dir_));
219       k ++;
220     }
221
222   if (!tie_configs[0].dir_)
223     tie_configs[0].dir_ = DOWN;
224   if (!tie_configs.top().dir_)
225     tie_configs.top().dir_ = UP;
226
227   /*
228     Seconds
229    */
230   for (int i = 1; i < tie_configs.size(); i++)
231     {
232       if (fabs (tie_configs[i-1].position_ - tie_configs[i].position_) <= 1)
233         {
234           if (!tie_configs[i-1].dir_)
235             tie_configs[i-1].dir_ = DOWN;
236           if (!tie_configs[i].dir_)
237             tie_configs[i].dir_ = UP;
238         }
239     }
240
241   for (int i = 1; i < tie_configs.size() - 1; i++)
242     {
243       if (tie_configs[i].dir_)
244         continue;
245
246       tie_configs[i].dir_ = (Direction) sign (tie_configs[i].position_);
247     }
248
249   Grob *common = me;
250   for (int i = 0; i < ties.size (); i++)
251     {
252       common = dynamic_cast<Spanner*> (ties[i])->get_bound (LEFT)->common_refpoint (common, X_AXIS); 
253       common = dynamic_cast<Spanner*> (ties[i])->get_bound (RIGHT)->common_refpoint (common, X_AXIS); 
254     }
255
256   Drul_array< Array<Skyline_entry> > skylines;
257   set_chord_outlines (&skylines, ties, common);
258   
259   Tie_details details;
260   details.init (ties[0]);
261
262   /*
263     Let the ties flow out, according to our single-tie formatting.
264    */
265   if (!manual_override)
266     {
267       Tie::get_configuration (ties[0], common, &tie_configs.elem_ref (0),
268                               &skylines,
269                               details
270                               );
271       Tie::get_configuration (ties.top (), common,
272                               &tie_configs.elem_ref (tie_configs.size()-1),
273                               &skylines,
274                               details
275                               );
276     }
277
278   /*
279     Calculate final width and shape of the ties.
280    */
281   Real staff_space = Staff_symbol_referencer::staff_space (ties[0]);
282   Real gap = robust_scm2double (ties[0]->get_property ("x-gap"), 0.2);
283   for (int i = 0; i < ties.size(); i++)
284     {
285       if (!manual_override
286           && (i == 0 || i == ties.size () -1))
287         continue;
288       
289       Tie_configuration conf = tie_configs[i];
290       conf = tie_configs[i];
291
292       Real line_dy = 0.0;
293       bool on_line = Staff_symbol_referencer::on_staffline (ties[0],
294                                                             int (rint (conf.position_)));
295       if (on_line)
296         line_dy = - sign (conf.height (details) - 0.6 * staff_space)
297           * 0.2 * staff_space * conf.dir_;
298
299       Real y = conf.position_ * staff_space * 0.5
300         + line_dy;
301       conf.attachment_x_
302         = get_skyline_attachment (skylines, y);
303       conf.attachment_x_.intersect (get_skyline_attachment (skylines,
304                                                             y + conf.dir_ * staff_space * .5));
305
306       conf.delta_y_ += line_dy;
307       conf.attachment_x_.widen (-gap);
308       if (!on_line
309           && Staff_symbol_referencer::staff_radius (ties[0]) * staff_space > y)
310         conf.center_tie_vertically (details);
311               
312       tie_configs[i] = conf;
313     }
314
315   /*
316     Try to shift small ties into available spaces.
317    */
318   if (!manual_override)
319     {
320       set<int> positions_taken; 
321       for (int i = 0; i < tie_configs.size (); i++)
322         positions_taken.insert (int (rint (tie_configs[i].position_)));
323
324       for (int i = 0; i < tie_configs.size (); i++)
325         {
326           Tie_configuration * conf = &tie_configs.elem_ref (i);
327           if (Staff_symbol_referencer::on_staffline (ties[0], int (rint (conf->position_)))
328               && conf->height (details) < 0.4 * staff_space
329               && (positions_taken.find (int (rint (conf->position_ + conf->dir_)))
330                   == positions_taken.end ()))
331             {
332               positions_taken.insert (int (rint (conf->position_ + conf->dir_)));
333               conf->delta_y_ += 0.2 * staff_space * conf->dir_;
334             }
335         }
336     }
337   
338   for (int i = 0; i < ties.size(); i++)
339     {
340       Tie::set_control_points (ties[i], common, tie_configs[i],
341                                details
342                                );
343       set_grob_direction (ties[i], tie_configs[i].dir_);
344     }
345 }
346
347
348 ADD_INTERFACE (Tie_column, "tie-column-interface",
349                "Object that sets directions of multiple ties in a tied chord",
350                "positioning-done "
351                "tie-configuration "
352                );