2 tie-column.cc -- implement Tie_column
4 source file of the GNU LilyPond music typesetter
6 (c) 2000--2005 Han-Wen Nienhuys <hanwen@cs.uu.nl>
13 #include "staff-symbol-referencer.hh"
15 #include "tie-column.hh"
16 #include "paper-column.hh"
18 #include "pointer-group-interface.hh"
20 #include "directional-element-interface.hh"
21 #include "rhythmic-head.hh"
24 Tie_column::add_tie (Grob *me, Grob *tie)
26 if (tie->get_parent (Y_AXIS)
27 && Tie_column::has_interface (tie->get_parent (Y_AXIS)))
30 if (!Pointer_group_interface::count (me, ly_symbol2scm ("ties")))
32 dynamic_cast<Spanner *> (me)->set_bound (LEFT, Tie::head (tie, LEFT));
33 dynamic_cast<Spanner *> (me)->set_bound (RIGHT, Tie::head (tie, RIGHT));
36 tie->set_parent (me, Y_AXIS);
37 Pointer_group_interface::add_grob (me, ly_symbol2scm ("ties"), tie);
38 tie->add_dependency (me);
42 Tie_column::set_directions (Grob *me)
44 if (!to_boolean (me->get_property ("positioning-done")))
46 me->set_property ("positioning-done", SCM_BOOL_T);
52 Tie::compare (Grob *const &s1,
55 return sign (Tie::get_position (s1) - Tie::get_position (s2));
61 . The algorithm to choose the direction of the ties doesn't work
62 properly. I suggest the following for applying ties sequentially
65 + The topmost tie is always `up'.
67 + If there is a vertical gap to the last note above larger than
68 or equal to a fifth (or sixth?), the tie is `up', otherwise it
71 + The bottommost tie is always `down'.
74 Tie_column::werner_directions (Grob *me)
76 extract_grob_set (me, "ties", ro_ties);
77 Link_array<Grob> ties (ro_ties);
81 ties.sort (&Tie::compare);
83 Direction d = get_grob_direction (me);
86 for (int i = ties.size (); i--;)
89 if (!get_grob_direction (t))
90 set_grob_direction (t, d);
95 if (ties.size () == 1)
99 && !get_grob_direction (t))
100 set_grob_direction (t, Tie::get_default_dir (t));
104 Real last_down_pos = 10000;
105 if (!get_grob_direction (ties[0]))
106 set_grob_direction (ties[0], DOWN);
112 for (int i = ties.size (); i--;)
116 Direction d = get_grob_direction (t);
117 Real p = Tie::get_position (t);
121 && Tie::get_column_rank (t, LEFT)
122 < Tie::get_column_rank (last_tie, LEFT))
124 else if (last_down_pos - p > 5)
129 set_grob_direction (t, d);
141 MAKE_SCHEME_CALLBACK (Tie_column, after_line_breaking, 1);
143 Tie_column::after_line_breaking (SCM smob)
145 set_directions (unsmob_grob (smob));
146 return SCM_UNSPECIFIED;
150 Extend the spanner over its Tie constituents.
152 MAKE_SCHEME_CALLBACK (Tie_column, before_line_breaking, 1);
154 Tie_column::before_line_breaking (SCM smob)
156 Spanner *me = dynamic_cast<Spanner *> (unsmob_grob (smob));
157 for (SCM s = me->get_property ("ties"); scm_is_pair (s); s = scm_cdr (s))
159 Spanner *tie = dynamic_cast<Spanner *> (unsmob_grob (scm_car (s)));
160 Direction dir = LEFT;
163 if (dir * tie->get_bound (dir)->get_column ()->get_rank ()
164 > dir * me->get_bound (dir)->get_column ()->get_rank ())
165 me->set_bound (dir, Tie::head (tie, dir));
167 while (flip (&dir) != LEFT);
169 return SCM_UNSPECIFIED;
172 ADD_INTERFACE (Tie_column, "tie-column-interface",
173 "Object that sets directions of multiple ties in a tied chord",
182 config_allowed (map<Tie_configuration, bool> const &allowed,
183 Tie_configuration conf)
185 return allowed.find (conf) == allowed.end ();
189 add_configuration (map<Tie_configuration, bool> *allowed,
191 Tie_configuration new_conf)
193 bool on_line = Staff_symbol_referencer::on_staffline (tie_column, new_conf.position_);
195 if (allowed->find (new_conf) != allowed->end ()
196 && !(*allowed)[new_conf])
198 programming_error ("Tie configuration not allowed");
204 Tie_configuration forbidden;
206 forbidden.dir_ = -new_conf.dir_ ;
207 forbidden.position_ = new_conf.position_;
208 (*allowed)[forbidden] = false;
210 forbidden.position_ += new_conf.dir_;
211 (*allowed)[forbidden] = false;
212 forbidden.position_ += new_conf.dir_;
213 (*allowed)[forbidden] = false;
215 forbidden.dir_ = new_conf.dir_;
216 forbidden.position_ = new_conf.position_ + new_conf.dir_;
217 (*allowed)[forbidden] = false;
221 Tie_configuration forbidden;
222 forbidden.dir_ = - new_conf.dir_;
223 forbidden.position_ = new_conf.position_;
226 (*allowed)[forbidden] = false;
227 forbidden.position_ -= new_conf.dir_;
228 forbidden.dir_ = new_conf.dir_;
229 (*allowed)[forbidden] = false;
231 forbidden.position_ += 2* new_conf.dir_;
232 (*allowed)[forbidden] = false;
238 Tie_column::new_directions (Grob *me)
240 extract_grob_set (me, "ties", ro_ties);
241 Link_array<Grob> ties (ro_ties);
245 if (ties.size() == 1)
247 Tie::set_default_control_points (ties[0]);
251 ties.sort (&Tie::compare);
252 Array<Tie_configuration> tie_configs;
253 for (int i = 0; i < ties.size (); i++)
255 Tie_configuration conf;
256 conf.dir_ = get_grob_direction (ties[i]);
257 conf.position_ = (int) rint (Tie::get_position (ties[i]));
258 tie_configs.push (conf);
262 if (!tie_configs[0].dir_)
263 tie_configs[0].dir_ = DOWN;
264 if (!tie_configs.top().dir_)
265 tie_configs.top().dir_ = UP;
271 for (int i = 1; i < tie_configs.size(); i++)
273 if (fabs (tie_configs[i-1].position_ - tie_configs[i].position_) <= 1)
275 if (!tie_configs[i-1].dir_)
276 tie_configs[i-1].dir_ = DOWN;
277 if (!tie_configs[i].dir_)
278 tie_configs[i].dir_ = UP;
282 for (int i = 1; i < tie_configs.size() - 1; i++)
284 if (tie_configs[i].dir_)
287 tie_configs[i].dir_ = (Direction) sign (tie_configs[i].position_);
290 Grob *common[NO_AXES] = {
293 for (int i = 0; i < ties.size (); i++)
294 for (int a = X_AXIS; a < NO_AXES; a++)
298 common[ax] = dynamic_cast<Spanner*> (ties[i])->get_bound (LEFT)->common_refpoint (common[a], ax);
299 common[ax] = dynamic_cast<Spanner*> (ties[i])->get_bound (RIGHT)->common_refpoint (common[a], ax);
302 map<Tie_configuration, bool> allowed;
304 Tie::get_configuration (ties[0], common, &tie_configs.elem_ref (0));
305 Tie::get_configuration (ties.top (), common,
306 &tie_configs.elem_ref (tie_configs.size()-1));
308 add_configuration (&allowed, me, tie_configs[0]);
309 add_configuration (&allowed, me, tie_configs.top());
311 for (int i = 1; i < ties.size(); i++)
313 Tie_configuration conf = tie_configs[i];
314 Tie::get_configuration (ties[i], common, &conf);
315 if (!config_allowed (allowed, conf))
317 conf = tie_configs[i];
322 conf.attachment_x_[d] = d * 1e6; // infty
323 for (int j = i - 1; j < i + 2; j++)
325 if (j >= 0 && j < ties.size())
327 Spanner *t = dynamic_cast<Spanner*> (ties[j]);
329 = robust_relative_extent (t->get_bound (d),
330 common[X_AXIS], X_AXIS);
331 conf.attachment_x_[d]
332 = d * min (d * conf.attachment_x_[d], d * ext[-d]);
336 while (flip (&d) != LEFT);
337 tie_configs[i] = conf;
340 tie_configs[i] = conf;
342 add_configuration (&allowed, me, conf);
345 for (int i = 0; i < ties.size(); i++)
347 Tie::set_control_points (ties[i], common, tie_configs[i]);
348 set_grob_direction (ties[i], tie_configs[i].dir_);