]> git.donarmstrong.com Git - lilypond.git/blob - lily/dot-column.cc
* lily/tie.cc: change 'heads to 'head-pair.
[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--2003 Han-Wen Nienhuys <hanwen@cs.uu.nl>
7 */
8
9 #include <set>
10
11 #include "dots.hh"
12 #include "dot-column.hh"
13 #include "rhythmic-head.hh"
14 #include "group-interface.hh"
15 #include "staff-symbol-referencer.hh"
16 #include "directional-element-interface.hh"
17 #include "side-position-interface.hh"
18 #include "axis-group-interface.hh"
19 #include "stem.hh"
20
21 using std::set;
22
23 /*
24   TODO: let Dot_column communicate with stem via Note_column.
25  */
26
27 MAKE_SCHEME_CALLBACK (Dot_column,force_shift_callback,2);
28 SCM
29 Dot_column::force_shift_callback (SCM element_smob, SCM axis)
30 {
31   Grob *me = unsmob_grob (element_smob);
32   Axis a = (Axis) gh_scm2int (axis);
33   assert (a == Y_AXIS);
34   me = me->get_parent (X_AXIS);
35
36   if (!to_boolean (me->get_grob_property ("collision-done")))
37     {
38       SCM l = me->get_grob_property ("dots");
39       me->set_grob_property ("collision-done", SCM_BOOL_T);
40   
41       do_shifts (me, l);
42     }
43   return gh_double2scm (0.0);
44 }
45
46 MAKE_SCHEME_CALLBACK(Dot_column,side_position, 2);
47 SCM
48 Dot_column::side_position (SCM element_smob, SCM axis)
49 {
50   Grob *me = unsmob_grob (element_smob);
51   Axis a = (Axis) gh_scm2int (axis);
52   assert (a == X_AXIS);
53
54   Grob * stem = unsmob_grob (me->get_grob_property ("stem"));
55   if (stem
56       && !Stem::get_beam (stem)
57       && Stem::duration_log (stem) > 2
58       && !Stem::invisible_b (stem)
59       )
60     {
61       /*
62         trigger stem end & direction calculation.
63
64         This will add the stem to the support if a flag collision happens.
65        */
66       Stem::stem_end_position (stem); 
67     }
68   return Side_position_interface::aligned_side (element_smob, axis);
69 }
70
71
72 /*
73   Put the dots in the spaces, close to the heads.
74
75   This is somewhat gruesome; the problem really is
76
77   minimize (sum_j dist (head_j, dot_j))
78
79   over all configurations. This sounds like a messy optimization
80   problem to solve.
81   
82 */
83 SCM
84 Dot_column::do_shifts (Grob*me, SCM l)
85 {
86   Link_array<Grob> dots;
87   while (gh_pair_p (l))
88     {
89       dots.push (unsmob_grob (ly_car (l)));
90       l = ly_cdr (l);
91     }
92   
93   dots.sort (compare_position);
94   
95
96   set<int> taken_posns;
97   for (int i=0; i < dots.size (); i++)
98     {
99       Grob * d = dots[i];
100       int p = int (Staff_symbol_referencer::get_position (d));
101
102       if (Staff_symbol_referencer::on_staffline (d, p)
103           || taken_posns.find (p) != taken_posns.end ())
104         {
105           int pd = p;
106           int pu = p;
107           if (Staff_symbol_referencer::on_staffline (d, p))
108             {
109               pu ++;
110               pd --;
111             }
112           
113           Direction dir =  to_dir (d->get_grob_property  ("direction"));
114           if (dir != DOWN)
115               
116           while (1)
117             {
118               if (dir != DOWN)
119                 {
120                   if (taken_posns.find (pu) == taken_posns.end ())
121                     {
122                       p = pu;
123                       break;
124                     }
125                   pu += 2;
126                 }
127               if (dir != UP)
128                 {
129                   if (taken_posns.find (pd) == taken_posns.end ())
130                     {
131                       p = pd;
132                       break;
133                     }
134                   pd -= 2;
135                 }
136             }
137           Staff_symbol_referencer::set_position (d, p);  
138         }
139       
140       taken_posns.insert (p);
141     }
142   
143   return SCM_UNSPECIFIED;
144 }
145
146
147
148 void
149 Dot_column::add_head (Grob * me, Grob *rh)
150 {
151   Grob * d = unsmob_grob (rh->get_grob_property ("dot"));
152   if (d)
153     {
154       Side_position_interface::add_support (me,rh);
155
156       Pointer_group_interface::add_grob (me, ly_symbol2scm ("dots"), d);
157       d->add_offset_callback (Dot_column::force_shift_callback_proc , Y_AXIS);
158       Axis_group_interface::add_element (me, d);
159     }
160 }
161
162
163
164
165 ADD_INTERFACE (Dot_column, "dot-column-interface",
166   "Interface that groups dots so they form a column",
167   "direction stem");
168