]> git.donarmstrong.com Git - lilypond.git/blob - lily/tie.cc
rename from laissez-vibrer-tie.cc. Rename
[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 int
32 Tie::compare (Grob *const &s1,
33               Grob *const &s2)
34 {
35   return sign (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) 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
126   return UP;
127 }
128
129
130 MAKE_SCHEME_CALLBACK(Tie, calc_direction, 1);
131 SCM
132 Tie::calc_direction (SCM smob)
133 {
134   Grob *me = unsmob_grob (smob);
135   Grob *yparent = me->get_parent (Y_AXIS);
136   if (Tie_column::has_interface (yparent)
137       && unsmob_grob_array (yparent->get_object ("ties"))
138       && unsmob_grob_array (yparent->get_object ("ties"))->size () > 1)
139     {
140       /* trigger positioning. */
141       (void) yparent->get_property ("positioning-done");
142     }
143   else
144     set_grob_direction (me, Tie::get_default_dir (me));
145
146   return SCM_UNSPECIFIED;
147 }
148
149
150 void
151 Tie::set_default_control_points (Grob *me_grob)
152 {
153   Spanner *me = dynamic_cast<Spanner*> (me_grob);
154   Grob *common  = me;
155   common = me->get_bound (LEFT)->common_refpoint (common, X_AXIS); 
156   common = me->get_bound (RIGHT)->common_refpoint (common, X_AXIS); 
157   
158   Tie_formatting_problem problem;
159   problem.from_tie (me);
160   Tie_specification spec = problem.get_tie_specification (0);
161   spec.has_manual_dir_ = true;
162   spec.manual_dir_ = get_grob_direction (me);
163   
164   Tie_configuration conf
165     = problem.find_optimal_tie_configuration (spec);
166   
167   set_control_points (me, problem.common_x_refpoint (),
168                       conf, problem.details_);
169 }
170
171 void
172 Tie::set_control_points (Grob *me,
173                          Grob *common,
174                          Tie_configuration const &conf,
175                          Tie_details const &details
176                          )
177 {
178   Bezier b = conf.get_transformed_bezier (details);
179   b.translate (Offset (- me->relative_coordinate (common, X_AXIS), 0));
180
181   SCM controls = SCM_EOL;
182   for (int i = 4; i--;)
183     {
184       if (!b.control_[i].is_sane ())
185         programming_error ("Insane offset");
186       controls = scm_cons (ly_offset2scm (b.control_[i]), controls);
187     }
188   me->set_property ("control-points", controls);
189 }
190
191 MAKE_SCHEME_CALLBACK(Tie, calc_control_points, 1);
192 SCM
193 Tie::calc_control_points (SCM smob)
194 {
195   Grob *me = unsmob_grob (smob);
196
197   // trigger Tie-column
198   (void)  get_grob_direction (me);
199
200   Grob *yparent = me->get_parent (Y_AXIS);
201   if (Tie_column::has_interface (yparent)
202       && unsmob_grob_array (yparent->get_object ("ties"))
203       && unsmob_grob_array (yparent->get_object ("ties"))->size () > 1)
204     {
205       /* trigger positioning. */
206       (void) yparent->get_property ("positioning-done");
207     }
208
209   if (!scm_is_pair (me->get_property ("control-points")))
210     {
211       set_default_control_points (me);
212     }
213
214   return SCM_UNSPECIFIED;
215 }
216
217 /*
218   TODO: merge witnh Slur::print.
219  */
220 MAKE_SCHEME_CALLBACK (Tie, print, 1);
221 SCM
222 Tie::print (SCM smob)
223 {
224   Grob *me = unsmob_grob (smob);
225   
226   SCM cp = me->get_property ("control-points");
227
228   Real staff_thick = Staff_symbol_referencer::line_thickness (me);
229   Real base_thick = staff_thick * robust_scm2double (me->get_property ("thickness"), 1);
230   Real line_thick = staff_thick * robust_scm2double (me->get_property ("line-thickness"), 1);
231
232   Bezier b;
233   int i = 0;
234   for (SCM s = cp; s != SCM_EOL; s = scm_cdr (s))
235     {
236       b.control_[i] = ly_scm2offset (scm_car (s));
237       i++;
238     }
239
240   Stencil a;
241
242   SCM p = me->get_property ("dash-period");
243   SCM f = me->get_property ("dash-fraction");
244   if (scm_is_number (p) && scm_is_number (f))
245     a = Lookup::dashed_slur (b,
246                              line_thick,
247                              robust_scm2double (p, 1.0),
248                              robust_scm2double (f, 0));
249   else
250     a = Lookup::slur (b,
251                       get_grob_direction (me) * base_thick,
252                       line_thick);
253
254 #if DEBUG_TIE_SCORING
255   SCM quant_score = me->get_property ("quant-score");
256
257   if (to_boolean (me->layout ()
258                   ->lookup_variable (ly_symbol2scm ("debug-tie-scoring")))
259       && scm_is_string (quant_score))
260     {
261       string str;
262       SCM properties = Font_interface::text_font_alist_chain (me);
263
264       Stencil tm = *unsmob_stencil (Text_interface::interpret_markup
265                                     (me->layout ()->self_scm (), properties,
266                                      quant_score));
267       tm.translate_axis (b.control_[0][Y_AXIS]*2, Y_AXIS);
268       a.add_at_edge (X_AXIS, RIGHT, tm, 1.0, 0);
269     }
270 #endif
271
272   return a.smobbed_copy ();
273 }
274
275 ADD_INTERFACE (Tie,
276                "tie-interface",
277                
278                "A horizontal curve connecting two noteheads. \n\n"
279                ,
280                
281
282                /* properties */
283                "avoid-slur "    //  UGH.
284                "control-points "
285                "dash-fraction "
286                "dash-period "
287                "details "
288                "direction "
289                "line-thickness " 
290                "quant-score "
291                "thickness "
292                );
293
294
295
296