]> git.donarmstrong.com Git - lilypond.git/blob - lily/dot-column.cc
release: 1.3.86
[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 (Score_element * me, Score_element *rh)
22 {
23   Score_element * d = unsmob_element (rh->get_elt_property ("dot"));
24   if (d)
25     {
26       Side_position::add_support (me,rh);
27
28       Pointer_group_interface gi (me, "dots");
29       gi.add_element (d);
30       
31       d->add_offset_callback (force_shift_callback , Y_AXIS);
32       Axis_group_interface::add_element (me, d);
33     }
34 }
35
36
37
38
39 void
40 Dot_column::set_interface (Score_element* me)
41 {
42
43   Directional_element_interface::set (me, RIGHT);
44   
45   Axis_group_interface::set_interface (me);
46   Axis_group_interface::set_axes (me, X_AXIS,X_AXIS);
47 }
48
49 /*
50   Will fuck up in this case.
51
52   X.  .
53   X.X .
54    |X .
55    |
56    |
57    |X .
58    |
59    |
60
61
62    Should be smarter.
63  */
64
65
66 Real
67 Dot_column::force_shift_callback (Score_element * dot, Axis a)
68 {
69   assert (a == Y_AXIS);
70   Score_element * me = dot->parent_l (X_AXIS);
71   SCM dots = me->get_elt_property ("dots");
72   do_shifts (dots);
73   return 0.0;
74 }
75
76 SCM
77 Dot_column::do_shifts (SCM l)
78 {
79   Link_array<Score_element> dots;
80   while (gh_pair_p (l))
81     {
82       dots.push (unsmob_element (gh_car (l)));
83       l = gh_cdr (l);
84     }
85   
86   dots.sort (compare_position);
87   
88   if (dots.size () < 2)
89     return SCM_UNSPECIFIED;
90   Slice s;
91   s.set_empty ();
92
93   Array<int> taken_posns;
94   int conflicts = 0;
95   for (int i=0; i < dots.size (); i++)
96     {
97       Real p = Staff_symbol_referencer::position_f (dots[i]);
98       for (int j=0; j < taken_posns.size (); j++)
99         {
100           if (taken_posns[j] == (int) p)
101             conflicts++;
102         }
103       taken_posns.push ((int)p);
104       s.unite (Slice ((int)p,
105                       (int)p));      
106     }
107
108   if (!conflicts)
109     return SCM_UNSPECIFIED;
110   
111   int  middle = s.center ();
112   /*
113     +1 -> off by one 
114    */
115   int pos = middle - dots.size () + 1;
116   if (!(pos % 2))
117     pos ++;                     // center () rounds down.
118
119   for (int i=0; i < dots.size (); pos += 2, i++)
120     {
121       Score_element * d = dots[i];
122       Staff_symbol_referencer::set_position (d,pos);
123     }
124
125   return SCM_UNSPECIFIED;
126 }
127
128 bool
129 Dot_column::has_interface (Score_element*m)
130 {
131   return m && m->has_interface (ly_symbol2scm ("dot-column-interface"));
132 }