]> git.donarmstrong.com Git - lilypond.git/blob - lily/dot-column.cc
b37a995b1de29c92db248e99302e2948b0f3e28e
[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--2002 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 #include "stem.hh"
18
19 /*
20   TODO: let Dot_column communicate with stem via Note_column.
21  */
22
23 MAKE_SCHEME_CALLBACK (Dot_column,force_shift_callback,2);
24 SCM
25 Dot_column::force_shift_callback (SCM element_smob, SCM axis)
26 {
27   Grob *me = unsmob_grob (element_smob);
28   Axis a = (Axis) gh_scm2int (axis);
29   assert (a == Y_AXIS);
30   me = me->get_parent (X_AXIS);
31   SCM l = me->get_grob_property ("dots");
32   do_shifts (l);
33   return gh_double2scm (0.0);
34 }
35
36 MAKE_SCHEME_CALLBACK(Dot_column,side_position, 2);
37 SCM
38 Dot_column::side_position (SCM element_smob, SCM axis)
39 {
40   Grob *me = unsmob_grob (element_smob);
41   Axis a = (Axis) gh_scm2int (axis);
42   assert (a == X_AXIS);
43
44   Grob * stem = unsmob_grob (me->get_grob_property ("stem"));
45   if (stem
46       && !Stem::beam_l (stem)
47       && Stem::duration_log (stem) > 2)
48     {
49       /*
50         trigger stem end & direction calculation.
51
52         This will add the stem to the support if a flag collision happens.
53        */
54       Stem::stem_end_position (stem); 
55     }
56   return Side_position_interface::aligned_side (element_smob, axis);
57 }
58
59
60 /*
61   Will fuck up in this case.
62
63   X.  .
64   X.X .
65    |X .
66    |
67    |
68    |X .
69    |
70    |
71
72
73    Should be smarter.
74  */
75 SCM
76 Dot_column::do_shifts (SCM l)
77 {
78   Link_array<Grob> dots;
79   while (gh_pair_p (l))
80     {
81       dots.push (unsmob_grob (ly_car (l)));
82       l = ly_cdr (l);
83     }
84   
85   dots.sort (compare_position);
86   
87   if (dots.size () < 2)
88     return SCM_UNSPECIFIED;
89   Slice s;
90   s.set_empty ();
91
92   Array<int> taken_posns;
93   int conflicts = 0;
94   for (int i=0; i < dots.size (); i++)
95     {
96       Real p = Staff_symbol_referencer::position_f (dots[i]);
97       for (int j=0; j < taken_posns.size (); j++)
98         {
99           if (taken_posns[j] == (int) p)
100             conflicts++;
101         }
102       taken_posns.push ((int)p);
103       s.unite (Slice ((int)p,
104  (int)p));      
105     }
106
107   if (!conflicts)
108     return SCM_UNSPECIFIED;
109   
110   int  middle = s.center ();
111   /*
112     +1 -> off by one 
113    */
114   int pos = middle - dots.size () + 1;
115   if (! (pos % 2))
116     pos ++;                     // center () rounds down.
117
118   for (int i=0; i < dots.size (); pos += 2, i++)
119     {
120       Grob * d = dots[i];
121       Staff_symbol_referencer::set_position (d,pos);
122     }
123
124   return SCM_UNSPECIFIED;
125 }
126
127 bool
128 Dot_column::has_interface (Grob*m)
129 {
130   return m && m->has_interface (ly_symbol2scm ("dot-column-interface"));
131 }
132
133
134 void
135 Dot_column::add_head (Grob * me, Grob *rh)
136 {
137   Grob * d = unsmob_grob (rh->get_grob_property ("dot"));
138   if (d)
139     {
140       Side_position_interface::add_support (me,rh);
141
142       Pointer_group_interface::add_grob (me, ly_symbol2scm ("dots"), d);
143       d->add_offset_callback (Dot_column::force_shift_callback_proc , Y_AXIS);
144       Axis_group_interface::add_element (me, d);
145     }
146 }
147
148
149
150
151 ADD_INTERFACE (Dot_column, "dot-column-interface",
152   "Interface that groups dots so they form a column",
153   "stem");
154