]> git.donarmstrong.com Git - lilypond.git/blob - lily/dot-column.cc
Run `make grand-replace'.
[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--2008 Han-Wen Nienhuys <hanwen@xs4all.nl>
7 */
8
9 #include "dot-column.hh"
10
11 #include <cstdio>
12 #include <cmath>
13 #include <map>
14 #include <set>
15
16 using namespace std;
17
18 #include "axis-group-interface.hh"
19 #include "directional-element-interface.hh"
20 #include "dot-column.hh"
21 #include "dot-configuration.hh"
22 #include "dot-formatting-problem.hh"
23 #include "dots.hh"
24 #include "grob.hh"
25 #include "note-head.hh"
26 #include "pointer-group-interface.hh"
27 #include "rest.hh"
28 #include "rhythmic-head.hh"
29 #include "side-position-interface.hh"
30 #include "staff-symbol-referencer.hh"
31 #include "stem.hh"
32
33 MAKE_SCHEME_CALLBACK (Dot_column, calc_positioning_done, 1);
34 SCM
35 Dot_column::calc_positioning_done (SCM smob)
36 {
37   Grob *me = unsmob_grob (smob);  
38
39   /*
40     Trigger note collision resolution first, since that may kill off
41     dots when merging.
42   */
43   if (Grob *collision = unsmob_grob (me->get_object ("note-collision")))
44     (void) collision->get_property ("positioning-done");
45
46   me->set_property ("positioning-done", SCM_BOOL_T);
47
48   vector<Grob*> dots
49     = extract_grob_array (me, "dots");
50
51   vector<Grob*> main_heads;
52   Real ss = 0;
53
54   Grob *commonx = me;
55   for (vsize i = 0; i < dots.size (); i++)
56     {
57       Grob *n = dots[i]->get_parent (Y_AXIS);
58       commonx = n->common_refpoint (commonx, X_AXIS);
59       
60       if (Grob *stem = unsmob_grob (n->get_object("stem")))
61         {
62           commonx = stem->common_refpoint (commonx, X_AXIS);
63           
64           if (Stem::first_head (stem) == n)
65             main_heads.push_back (n);
66         }
67     }
68
69   vector<Box> boxes;
70   set<Grob*> stems;
71
72   extract_grob_set(me, "side-support-elements", support);
73
74   Interval base_x;
75   for (vsize i = 0; i < main_heads.size (); i++)
76     base_x.unite (main_heads[i]->extent (commonx, X_AXIS));
77  
78   for (vsize i = 0; i < support.size (); i++)
79     {
80       Grob *s = support[i];
81       if (!ss)
82         ss = Staff_symbol_referencer::staff_space (s);
83
84       /* can't inspect Y extent of rest.
85          
86          Rest collisions should wait after line breaking.
87       */
88       Interval y;
89       if (Rest::has_interface (s))
90         {
91           base_x.unite (s->extent (commonx, X_AXIS));
92           continue;
93         }
94       else if (Stem::has_interface (s))
95         {
96           Real y1 = Stem::head_positions (s)[-get_grob_direction (s)];
97           Real y2 = y1 + get_grob_direction (s) * 7;
98
99           y.add_point (y1);
100           y.add_point (y2);
101         }
102       else
103         y = s->extent (s, Y_AXIS);
104
105       y *= 2 / ss;
106       y += Staff_symbol_referencer::get_position (s);
107           
108       Box b (s->extent (commonx, X_AXIS), y);
109       boxes.push_back (b);
110
111       if (Grob *stem = unsmob_grob (s->get_object ("stem")))
112         stems.insert (stem);
113     }
114
115   for (set<Grob*>::const_iterator i(stems.begin());
116        i != stems.end (); i++)
117     {
118       Grob *stem = (*i);
119       Stencil flag = Stem::flag (stem);
120       if (!flag.is_empty ())
121         {
122           Interval y = flag.extent (Y_AXIS)
123             * (2 / ss)
124             + Stem::stem_end_position (stem);
125                   
126           Interval x = stem->relative_coordinate (commonx, X_AXIS)
127             + flag.extent (X_AXIS);
128
129           boxes.push_back (Box (x,y));
130         }
131     }
132               
133   vector_sort (dots, position_less);
134   for (vsize i = dots.size (); i--;)
135     if (!dots[i]->is_live ())
136       dots.erase (dots.begin () + i);
137
138   Dot_formatting_problem problem (boxes, base_x);
139
140   Dot_configuration cfg (problem);
141   for (vsize i = 0; i < dots.size (); i++)
142     {
143       Dot_position dp;
144       dp.dot_ = dots[i];
145
146       Grob *note = dots[i]->get_parent (Y_AXIS);
147       if (note)
148         {
149           Grob *stem = unsmob_grob (note->get_object ("stem"));
150           if (stem)
151             dp.extremal_head_ = Stem::first_head (stem) == note;
152           
153           dp.x_extent_ = note->extent (commonx, X_AXIS);
154         }
155
156       int p = Staff_symbol_referencer::get_rounded_position (dp.dot_);
157
158       /* icky, since this should go via a Staff_symbol_referencer
159          offset callback but adding a dot overwrites Y-offset. */
160       p += (int) robust_scm2double (dp.dot_->get_property ("staff-position"), 0.0);
161       dp.pos_ = p;
162       if (dp.extremal_head_)
163         dp.dir_ = to_dir (dp.dot_->get_property ("direction"));
164
165       cfg.remove_collision (p);
166       cfg[p] = dp;
167       if (Staff_symbol_referencer::on_line (dp.dot_, p))
168         cfg.remove_collision (p);
169     }
170
171   problem.register_configuration (cfg);
172
173   for (Dot_configuration::const_iterator i (cfg.begin ());
174        i != cfg.end (); i++)
175     {
176       /*
177         Junkme?
178        */
179       Staff_symbol_referencer::set_position (i->second.dot_, i->first);
180     }
181   
182   me->translate_axis (cfg.x_offset () - me->relative_coordinate (commonx, X_AXIS),
183                       X_AXIS);
184   return SCM_BOOL_T;
185 }
186
187 void
188 Dot_column::add_head (Grob *me, Grob *head)
189 {
190   Grob *d = unsmob_grob (head->get_object ("dot"));
191   if (d)
192     {
193       Side_position_interface::add_support (me, head);
194
195       Pointer_group_interface::add_grob (me, ly_symbol2scm ("dots"), d);
196       d->set_property ("Y-offset", Grob::x_parent_positioning_proc);
197       // Dot formatting requests the Y-offset, -which- for rests may
198       // trigger post-linebreak callbacks.
199       if (!Rest::has_interface (head))
200         d->set_property ("X-offset", Grob::x_parent_positioning_proc);
201       Axis_group_interface::add_element (me, d);
202     }
203 }
204
205 ADD_INTERFACE (Dot_column,
206                "Group dot objects so they form a column, and position"
207                " dots so they do not clash with staff lines.",
208
209                /* properties */
210                "dots "
211                "positioning-done "
212                "direction "
213                );
214