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