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