Allow for nested contexts of any depth.
[lilypond.git] / lily / dot-column.cc
blob249eb9b8cdf42c74ec5758806d63c8df6d4140c5
1 /*
2 dot-column.cc -- implement Dot_column
4 source file of the GNU LilyPond music typesetter
6 (c) 1997--2007 Han-Wen Nienhuys <hanwen@xs4all.nl>
7 */
9 #include "dot-column.hh"
11 #include <cstdio>
12 #include <cmath>
13 #include <map>
14 #include <set>
16 using namespace std;
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"
33 MAKE_SCHEME_CALLBACK (Dot_column, calc_positioning_done, 1);
34 SCM
35 Dot_column::calc_positioning_done (SCM smob)
37 Grob *me = unsmob_grob (smob);
40 Trigger note collision resolution first, since that may kill off
41 dots when merging.
43 if (Grob *collision = unsmob_grob (me->get_object ("note-collision")))
44 (void) collision->get_property ("positioning-done");
46 me->set_property ("positioning-done", SCM_BOOL_T);
48 vector<Grob*> dots
49 = extract_grob_array (me, "dots");
51 vector<Grob*> main_heads;
52 Real ss = 0;
54 Grob *commonx = me;
55 for (vsize i = 0; i < dots.size (); i++)
57 Grob *n = dots[i]->get_parent (Y_AXIS);
58 commonx = n->common_refpoint (commonx, X_AXIS);
60 if (Grob *stem = unsmob_grob (n->get_object("stem")))
62 commonx = stem->common_refpoint (commonx, X_AXIS);
64 if (Stem::first_head (stem) == n)
65 main_heads.push_back (n);
69 vector<Box> boxes;
70 set<Grob*> stems;
72 extract_grob_set(me, "side-support-elements", support);
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));
78 for (vsize i = 0; i < support.size (); i++)
80 Grob *s = support[i];
81 if (!ss)
82 ss = Staff_symbol_referencer::staff_space (s);
84 /* can't inspect Y extent of rest.
86 Rest collisions should wait after line breaking.
88 Interval y;
89 if (Rest::has_interface (s))
91 base_x.unite (s->extent (commonx, X_AXIS));
92 continue;
94 else if (Stem::has_interface (s))
96 Real y1 = Stem::head_positions (s)[-get_grob_direction (s)];
97 Real y2 = y1 + get_grob_direction (s) * 7;
99 y.add_point (y1);
100 y.add_point (y2);
102 else
103 y = s->extent (s, Y_AXIS);
105 y *= 2 / ss;
106 y += Staff_symbol_referencer::get_position (s);
108 Box b (s->extent (commonx, X_AXIS), y);
109 boxes.push_back (b);
111 if (Grob *stem = unsmob_grob (s->get_object ("stem")))
112 stems.insert (stem);
115 for (set<Grob*>::const_iterator i(stems.begin());
116 i != stems.end (); i++)
118 Grob *stem = (*i);
119 Stencil flag = Stem::flag (stem);
120 if (!flag.is_empty ())
122 Interval y = flag.extent (Y_AXIS)
123 * (2 / ss)
124 + Stem::stem_end_position (stem);
126 Interval x = stem->relative_coordinate (commonx, X_AXIS)
127 + flag.extent (X_AXIS);
129 boxes.push_back (Box (x,y));
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);
138 Dot_formatting_problem problem (boxes, base_x);
140 Dot_configuration cfg (problem);
141 for (vsize i = 0; i < dots.size (); i++)
143 Dot_position dp;
144 dp.dot_ = dots[i];
146 Grob *note = dots[i]->get_parent (Y_AXIS);
147 if (note)
149 Grob *stem = unsmob_grob (note->get_object ("stem"));
150 if (stem)
151 dp.extremal_head_ = Stem::first_head (stem) == note;
153 dp.x_extent_ = note->extent (commonx, X_AXIS);
156 int p = Staff_symbol_referencer::get_rounded_position (dp.dot_);
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"));
165 cfg.remove_collision (p);
166 cfg[p] = dp;
167 if (Staff_symbol_referencer::on_line (dp.dot_, p))
168 cfg.remove_collision (p);
171 problem.register_configuration (cfg);
173 for (Dot_configuration::const_iterator i (cfg.begin ());
174 i != cfg.end (); i++)
177 Junkme?
179 Staff_symbol_referencer::set_position (i->second.dot_, i->first);
182 me->translate_axis (cfg.x_offset () - me->relative_coordinate (commonx, X_AXIS),
183 X_AXIS);
184 return SCM_BOOL_T;
187 void
188 Dot_column::add_head (Grob *me, Grob *head)
190 Grob *d = unsmob_grob (head->get_object ("dot"));
191 if (d)
193 Side_position_interface::add_support (me, head);
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);
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.",
209 /* properties */
210 "dots "
211 "positioning-done "
212 "direction "