]> git.donarmstrong.com Git - lilypond.git/blob - lily/tie.cc
* lily/beam.cc (get_default_dir): take extreme note head as input
[lilypond.git] / lily / tie.cc
1 /*
2   tie.cc -- implement Tie
3
4   source file of the GNU LilyPond music typesetter
5
6   (c) 1997--2006 Han-Wen Nienhuys <hanwen@xs4all.nl>
7 */
8
9 #include "tie.hh"
10 #include "spanner.hh"
11 #include "lookup.hh"
12 #include "output-def.hh"
13 #include "rhythmic-head.hh"
14 #include "bezier.hh"
15 #include "paper-column.hh"
16 #include "warn.hh"
17 #include "staff-symbol-referencer.hh"
18 #include "directional-element-interface.hh"
19 #include "bezier.hh"
20 #include "stem.hh"
21 #include "note-head.hh"
22 #include "tie-column.hh"
23 #include "grob-array.hh"
24 #include "tie-formatting-problem.hh"
25 #include "tie-configuration.hh"
26
27
28 int
29 Tie::compare (Grob *const &s1,
30               Grob *const &s2)
31 {
32   return sign (Tie::get_position (s1) - Tie::get_position (s2));
33 }
34
35 void
36 Tie::set_head (Grob *me, Direction d, Grob *h)
37 {
38   dynamic_cast<Spanner *> (me)->set_bound (d, h);
39 }
40
41 Grob *
42 Tie::head (Grob *me, Direction d)
43 {
44   Item *it = dynamic_cast<Spanner*> (me)->get_bound (d);
45   if (Note_head::has_interface (it))
46     return it;
47   else
48     return 0;
49 }
50
51 int
52 Tie::get_column_rank (Grob *me, Direction d)
53 {
54   Spanner *span = dynamic_cast<Spanner *> (me);
55   Grob *h = head (me, d);
56   if (!h)
57     h = span->get_bound (d);
58
59   Grob *col = dynamic_cast<Item *> (h)->get_column ();
60   return Paper_column::get_rank (col);
61 }
62
63 int
64 Tie::get_position (Grob *me)
65 {
66   Direction d = LEFT;
67   do
68     {
69       Grob *h = head (me, d);
70       if (h)
71         return (int) Staff_symbol_referencer::get_position (h);
72     }
73   while (flip (&d) != LEFT);
74
75   /*
76
77   TODO: this is theoretically possible for ties across more than 2
78   systems.. We should look at the first broken copy.
79   
80   */
81   programming_error ("Tie without heads. Suicide");
82   me->suicide ();
83   return 0;
84 }
85
86 /*
87   Default:  Put the tie oppositie of the stem [Wanske p231]
88
89   In case of chords: Tie_column takes over
90
91   The direction of the Tie is more complicated (See [Ross] p136 and
92   further).
93
94   (what about linebreaks? )
95 */
96 Direction
97 Tie::get_default_dir (Grob *me)
98 {
99   Drul_array<Grob*> stems;
100   Direction d = LEFT;
101   do
102     {
103       Grob *stem = head (me, d) ? Rhythmic_head::get_stem (head (me, d)) : 0;
104       if (stem)
105         stem = Stem::is_invisible (stem) ? 0 : stem;
106
107       stems[d] = stem;
108     }
109   while (flip (&d)!= LEFT);
110   
111   if (stems[LEFT] && stems[RIGHT])
112     {
113       if (get_grob_direction (stems[LEFT]) == UP
114           && get_grob_direction (stems[RIGHT]) == UP)
115         return DOWN;
116     }
117   else if (stems[LEFT] || stems[RIGHT])
118     {
119       Grob *s = stems[LEFT] ? stems[LEFT] : stems[RIGHT];
120       return -get_grob_direction (s);
121     }
122
123   return UP;
124 }
125
126
127 MAKE_SCHEME_CALLBACK(Tie, calc_direction, 1);
128 SCM
129 Tie::calc_direction (SCM smob)
130 {
131   Grob *me = unsmob_grob (smob);
132   Grob *yparent = me->get_parent (Y_AXIS);
133   if (Tie_column::has_interface (yparent)
134       && unsmob_grob_array (yparent->get_object ("ties"))
135       && unsmob_grob_array (yparent->get_object ("ties"))->size () > 1)
136     {
137       /* trigger positioning. */
138       (void) yparent->get_property ("positioning-done");
139     }
140   else
141     set_grob_direction (me, Tie::get_default_dir (me));
142
143   return SCM_UNSPECIFIED;
144 }
145
146
147 void
148 Tie::set_default_control_points (Grob *me_grob)
149 {
150   Spanner *me = dynamic_cast<Spanner*> (me_grob);
151   Grob *common  = me;
152   common = me->get_bound (LEFT)->common_refpoint (common, X_AXIS); 
153   common = me->get_bound (RIGHT)->common_refpoint (common, X_AXIS); 
154   
155   Tie_formatting_problem problem;
156   problem.from_tie (me);
157   Tie_specification spec = problem.get_tie_specification (0);
158   spec.has_manual_dir_ = true;
159   spec.manual_dir_ = get_grob_direction (me);
160   
161   Tie_configuration conf
162     = problem.find_optimal_tie_configuration (spec);
163   
164   set_control_points (me, problem.common_x_refpoint (),
165                       conf, problem.details_);
166 }
167
168 void
169 Tie::set_control_points (Grob *me,
170                          Grob *common,
171                          Tie_configuration const &conf,
172                          Tie_details const &details
173                          )
174 {
175   Bezier b = conf.get_transformed_bezier (details);
176   b.translate (Offset (- me->relative_coordinate (common, X_AXIS), 0));
177
178   SCM controls = SCM_EOL;
179   for (int i = 4; i--;)
180     {
181       if (!b.control_[i].is_sane ())
182         programming_error ("Insane offset");
183       controls = scm_cons (ly_offset2scm (b.control_[i]), controls);
184     }
185   me->set_property ("control-points", controls);
186 }
187
188 MAKE_SCHEME_CALLBACK(Tie, calc_control_points, 1);
189 SCM
190 Tie::calc_control_points (SCM smob)
191 {
192   Grob *me = unsmob_grob (smob);
193
194   // trigger Tie-column
195   (void)  get_grob_direction (me);
196
197   Grob *yparent = me->get_parent (Y_AXIS);
198   if (Tie_column::has_interface (yparent)
199       && unsmob_grob_array (yparent->get_object ("ties"))
200       && unsmob_grob_array (yparent->get_object ("ties"))->size () > 1)
201     {
202       /* trigger positioning. */
203       (void) yparent->get_property ("positioning-done");
204     }
205
206   if (!scm_is_pair (me->get_property ("control-points")))
207     {
208       set_default_control_points (me);
209     }
210
211   return SCM_UNSPECIFIED;
212 }
213
214 /*
215   TODO: merge witnh Slur::print.
216  */
217 MAKE_SCHEME_CALLBACK (Tie, print, 1);
218 SCM
219 Tie::print (SCM smob)
220 {
221   Grob *me = unsmob_grob (smob);
222   
223   SCM cp = me->get_property ("control-points");
224
225   Real staff_thick = Staff_symbol_referencer::line_thickness (me);
226   Real base_thick = staff_thick * robust_scm2double (me->get_property ("thickness"), 1);
227   Real line_thick = staff_thick * robust_scm2double (me->get_property ("line-thickness"), 1);
228
229   Bezier b;
230   int i = 0;
231   for (SCM s = cp; s != SCM_EOL; s = scm_cdr (s))
232     {
233       b.control_[i] = ly_scm2offset (scm_car (s));
234       i++;
235     }
236
237   Stencil a;
238
239   SCM p = me->get_property ("dash-period");
240   SCM f = me->get_property ("dash-fraction");
241   if (scm_is_number (p) && scm_is_number (f))
242     a = Lookup::dashed_slur (b,
243                              line_thick,
244                              robust_scm2double (p, 1.0),
245                              robust_scm2double (f, 0));
246   else
247     a = Lookup::slur (b,
248                       get_grob_direction (me) * base_thick,
249                       line_thick);
250
251   return a.smobbed_copy ();
252 }
253
254 ADD_INTERFACE (Tie,
255                "tie-interface",
256                
257                "A horizontal curve connecting two noteheads. \n\n"
258                ,
259                
260
261                /* properties */
262                "avoid-slur "    //  UGH.
263                "control-points "
264                "dash-fraction "
265                "dash-period "
266                "details "
267                "direction "
268                "line-thickness " 
269                "thickness "
270                );
271
272
273
274