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