]> git.donarmstrong.com Git - lilypond.git/blob - lily/dot-column.cc
release: 1.3.10
[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--1999 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
14 void
15 Dot_column::add_dots (Dots *d)
16 {
17   Group_interface gi (this, "dots");
18   gi.add_element (d);
19
20   add_dependency (d);
21   add_element (d);
22 }
23
24 void
25 Dot_column::add_head (Rhythmic_head *r)
26 {
27   if (!r->dots_l ())
28     return ;
29
30   add_support (r);
31   add_dots (r->dots_l ());
32 }
33
34
35 int
36 Dot_column::compare (Dots * const &d1, Dots * const &d2)
37 {
38   return int (d1->position_f () - d2->position_f ());
39 }
40
41
42 Dot_column::Dot_column ()
43 {
44   Group_interface gi (this, "dots");
45   gi.set_interface ();
46   
47   set_direction (RIGHT);
48   set_axes(X_AXIS,X_AXIS);
49 }
50
51 /*
52   Will fuck up in this case.
53
54   X.  .
55   X.X .
56    |X .
57    |
58    |
59    |X .
60    |
61    |
62
63
64    Should be smarter.
65  */
66 void
67 Dot_column::do_post_processing ()
68 {
69   Link_array<Dots> dots = Group_interface__extract_elements (this, (Dots*)0 , "dots"); 
70   dots.sort (Dot_column::compare);
71   
72   if (dots.size () < 2)
73     return;
74   Slice s;
75   s.set_empty ();
76
77   Array<int> taken_posns;
78   int conflicts = 0;
79   for (int i=0; i < dots.size (); i++)
80     {
81       for (int j=0; j < taken_posns.size (); j++)
82         if (taken_posns[j] == (int) dots[i]->position_f ())
83           conflicts++;
84       taken_posns.push ((int)dots[i]->position_f ());
85       s.unite (Slice ((int)dots[i]->position_f (),
86                       (int)dots[i]->position_f ()));      
87     }
88
89   if (!conflicts)
90     return;
91   
92   int  middle = s.center ();
93   /*
94     +1 -> off by one 
95    */
96   int pos = middle - dots.size () + 1;
97   if (!(pos % 2))
98     pos ++;                     // center () rounds down.
99
100   for (int i=0; i  <dots.size (); pos += 2, i++)
101     {
102       dots[i]->set_position(pos);
103     }
104 }