]> git.donarmstrong.com Git - lilypond.git/blob - lily/tie-column.cc
* lily/note-column.cc (dir): idem.
[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 #include "paper-column.hh"
11 #include "spanner.hh"
12 #include "pointer-group-interface.hh"
13 #include "tie.hh"
14 #include "directional-element-interface.hh"
15 #include "rhythmic-head.hh"
16
17 /*
18   tie dir depends on what Tie_column does.
19 */
20 /*
21   TODO: this doesn't follow standard pattern. Regularize.
22 */
23 void
24 Tie_column::add_tie (Grob *me, Grob *tie)
25 {
26   if (tie->get_parent (Y_AXIS)
27       && Tie_column::has_interface (tie->get_parent (Y_AXIS)))
28     return;
29
30   if (!Pointer_group_interface::count (me, ly_symbol2scm ("ties")))
31     {
32       dynamic_cast<Spanner *> (me)->set_bound (LEFT, Tie::head (tie, LEFT));
33       dynamic_cast<Spanner *> (me)->set_bound (RIGHT, Tie::head (tie, RIGHT));
34     }
35
36   tie->set_parent (me, Y_AXIS);
37   Pointer_group_interface::add_grob (me, ly_symbol2scm ("ties"), tie);
38   tie->add_dependency (me);
39 }
40
41 void
42 Tie_column::set_directions (Grob *me)
43 {
44   werner_directions (me);
45 }
46
47 int
48 tie_compare (Grob *const &s1,
49              Grob *const &s2)
50 {
51   return sign (Tie::get_position (s1) - Tie::get_position (s2));
52 }
53
54 /*
55   Werner:
56
57   . The algorithm to choose the direction of the ties doesn't work
58   properly.  I suggest the following for applying ties sequentially
59   from top to bottom:
60
61   + The topmost tie is always `up'.
62
63   + If there is a vertical gap to the last note above larger than
64   or equal to a fifth (or sixth?), the tie is `up', otherwise it
65   is `down'.
66
67   + The bottommost tie is always `down'.
68 */
69 void
70 Tie_column::werner_directions (Grob *me)
71 {
72   extract_grob_set (me, "ties", ro_ties);
73   Link_array<Grob> ties (ro_ties);
74   if (!ties.size ())
75     return;
76
77   ties.sort (tie_compare);
78
79   Direction d = get_grob_direction (me);
80   if (d)
81     {
82       for (int i = ties.size (); i--;)
83         {
84           Grob *t = ties[i];
85           if (!get_grob_direction (t))
86             set_grob_direction (t, d);
87         }
88       return;
89     }
90
91   if (ties.size () == 1)
92     {
93       Grob *t = ties[0];
94       if (t->is_live ()
95           && !get_grob_direction (t))
96         set_grob_direction (t, Tie::get_default_dir (t));
97       return;
98     }
99
100   Real last_down_pos = 10000;
101   if (!get_grob_direction (ties[0]))
102     set_grob_direction (ties[0], DOWN);
103
104   /*
105     Go downward.
106   */
107   Grob *last_tie = 0;
108   for (int i = ties.size (); i--;)
109     {
110       Grob *t = ties[i];
111
112       Direction d = get_grob_direction (t);
113       Real p = Tie::get_position (t);
114       if (!d)
115         {
116           if (last_tie
117               && Tie::get_column_rank (t, LEFT)
118               < Tie::get_column_rank (last_tie, LEFT))
119             {
120               d = DOWN;
121             }
122           else if (last_down_pos - p > 5)
123             {
124               d = UP;
125             }
126           else
127             {
128               d = DOWN;
129             }
130
131           set_grob_direction (t, d);
132         }
133
134       if (d == DOWN)
135         last_down_pos = p;
136
137       last_tie = t;
138     }
139
140   return;
141 }
142
143 MAKE_SCHEME_CALLBACK (Tie_column, after_line_breaking, 1);
144 SCM
145 Tie_column::after_line_breaking (SCM smob)
146 {
147   werner_directions (unsmob_grob (smob));
148   return SCM_UNSPECIFIED;
149 }
150
151 /*
152   Extend the spanner over its Tie constituents.
153 */
154 MAKE_SCHEME_CALLBACK (Tie_column, before_line_breaking, 1);
155 SCM
156 Tie_column::before_line_breaking (SCM smob)
157 {
158   Spanner *me = dynamic_cast<Spanner *> (unsmob_grob (smob));
159   for (SCM s = me->get_property ("ties"); scm_is_pair (s); s = scm_cdr (s))
160     {
161       Spanner *tie = dynamic_cast<Spanner *> (unsmob_grob (scm_car (s)));
162       Direction dir = LEFT;
163       do
164         {
165           if (dir * tie->get_bound (dir)->get_column ()->get_rank ()
166               > dir * me->get_bound (dir)->get_column ()->get_rank ())
167             {
168               me->set_bound (dir, Tie::head (tie, dir));
169             }
170         }
171       while (flip (&dir) != LEFT);
172     }
173   return SCM_UNSPECIFIED;
174 }
175
176 ADD_INTERFACE (Tie_column, "tie-column-interface",
177                "Object that sets directions of multiple ties in a tied chord",
178                "direction");
179