]> git.donarmstrong.com Git - lilypond.git/blob - lily/dot-column.cc
a725998bcfc69462404eeaef29d9addd9a95bf88
[lilypond.git] / lily / dot-column.cc
1 /*
2   dot-column.cc -- implement Dot_column
3
4   source file of the GNU LilyPond music typesetter
5
6   (c)  1997--2000 Han-Wen Nienhuys <hanwen@cs.uu.nl>
7 */
8
9 #include "dots.hh"
10 #include "dot-column.hh"
11 #include "rhythmic-head.hh"
12 #include "group-interface.hh"
13 #include "staff-symbol-referencer.hh"
14 #include "directional-element-interface.hh"
15 #include "side-position-interface.hh"
16 #include "axis-group-interface.hh"
17
18
19
20 void
21 Dot_column::add_head (Rhythmic_head *r)
22 {
23   if (!r->dots_l ())
24     return ;
25
26   Side_position_interface (this).add_support (r);
27   Item * d = r->dots_l ();
28   if (d)
29     {
30       Pointer_group_interface gi (this, "dots");
31       gi.add_element (d);
32       
33       d->add_offset_callback (force_shift_callback , Y_AXIS);
34       Axis_group_interface (this).add_element (d);
35     }
36 }
37
38
39 int
40 Dot_column::compare (Score_element * const &d1, Score_element * const &d2)
41 {
42   Staff_symbol_referencer_interface s1(d1);
43   Staff_symbol_referencer_interface s2(d2);  
44   
45   return int (s1.position_f () - s2.position_f ());
46 }
47
48
49 Dot_column::Dot_column (SCM s)
50   : Item (s)
51 {
52   this->set_elt_pointer  ("dots", SCM_EOL);
53   Directional_element_interface (this).set (RIGHT);
54   
55   Axis_group_interface (this).set_interface ();
56   Axis_group_interface (this).set_axes(X_AXIS,X_AXIS);
57 }
58
59 /*
60   Will fuck up in this case.
61
62   X.  .
63   X.X .
64    |X .
65    |
66    |
67    |X .
68    |
69    |
70
71
72    Should be smarter.
73  */
74
75
76 Real
77 Dot_column::force_shift_callback (Score_element const * dot, Axis a)
78 {
79   assert (a == Y_AXIS);
80   Score_element * me = dot->parent_l (X_AXIS);
81   SCM dots = me->get_elt_pointer ("dots");
82   do_shifts (dots);
83   return 0.0;
84 }
85
86 SCM
87 Dot_column::do_shifts (SCM l)
88 {
89   Link_array<Score_element> dots;
90   while (gh_pair_p (l))
91     {
92       dots.push (unsmob_element (gh_car (l)));
93       l = gh_cdr (l);
94     }
95   
96   dots.sort (Dot_column::compare);
97   
98   if (dots.size () < 2)
99     return SCM_UNDEFINED;
100   Slice s;
101   s.set_empty ();
102
103   Array<int> taken_posns;
104   int conflicts = 0;
105   for (int i=0; i < dots.size (); i++)
106     {
107       Real p = Staff_symbol_referencer_interface (dots[i]).position_f ();
108       for (int j=0; j < taken_posns.size (); j++)
109         {
110           if (taken_posns[j] == (int) p)
111             conflicts++;
112         }
113       taken_posns.push ((int)p);
114       s.unite (Slice ((int)p,
115                       (int)p));      
116     }
117
118   if (!conflicts)
119     return SCM_UNDEFINED;
120   
121   int  middle = s.center ();
122   /*
123     +1 -> off by one 
124    */
125   int pos = middle - dots.size () + 1;
126   if (!(pos % 2))
127     pos ++;                     // center () rounds down.
128
129   for (int i=0; i < dots.size (); pos += 2, i++)
130     {
131       Score_element * d = dots[i];
132       Staff_symbol_referencer_interface (d).set_position(pos);
133     }
134
135   return SCM_UNDEFINED;
136 }