source file of the GNU LilyPond music typesetter
- (c) 1997--2005 Han-Wen Nienhuys <hanwen@cs.uu.nl>
+ (c) 1997--2008 Han-Wen Nienhuys <hanwen@xs4all.nl>
*/
#include "dot-column.hh"
#include <cstdio>
-#include <math.h>
+#include <cmath>
#include <map>
+#include <set>
+using namespace std;
+
+#include "axis-group-interface.hh"
+#include "directional-element-interface.hh"
+#include "dot-column.hh"
+#include "dot-configuration.hh"
+#include "dot-formatting-problem.hh"
#include "dots.hh"
+#include "grob.hh"
+#include "note-head.hh"
+#include "pointer-group-interface.hh"
+#include "rest.hh"
#include "rhythmic-head.hh"
-#include "staff-symbol-referencer.hh"
-#include "directional-element-interface.hh"
#include "side-position-interface.hh"
-#include "axis-group-interface.hh"
+#include "staff-symbol-referencer.hh"
#include "stem.hh"
-/*
- TODO: let Dot_column communicate with stem via Note_column.
-*/
-
-MAKE_SCHEME_CALLBACK (Dot_column, force_shift_callback, 2);
-SCM
-Dot_column::force_shift_callback (SCM element_smob, SCM axis)
-{
- Grob *me = unsmob_grob (element_smob);
- Axis a = (Axis) scm_to_int (axis);
- assert (a == Y_AXIS);
- me = me->get_parent (X_AXIS);
-
- if (!to_boolean (me->get_property ("positioning-done")))
- {
- me->set_property ("positioning-done", SCM_BOOL_T);
-
- do_shifts (me);
- }
- return scm_make_real (0.0);
-}
-
-MAKE_SCHEME_CALLBACK (Dot_column, side_position, 2);
+MAKE_SCHEME_CALLBACK (Dot_column, calc_positioning_done, 1);
SCM
-Dot_column::side_position (SCM element_smob, SCM axis)
+Dot_column::calc_positioning_done (SCM smob)
{
- Grob *me = unsmob_grob (element_smob);
- Axis a = (Axis) scm_to_int (axis);
- assert (a == X_AXIS);
-
- Grob *stem = unsmob_grob (me->get_property ("stem"));
- if (stem
- && !Stem::get_beam (stem)
- && Stem::duration_log (stem) > 2
- && !Stem::is_invisible (stem))
- {
- /*
- trigger stem end & direction calculation.
+ Grob *me = unsmob_grob (smob);
- This will add the stem to the support if a flag collision happens.
- */
- Stem::stem_end_position (stem);
- }
- return Side_position_interface::aligned_side (element_smob, axis);
-}
+ /*
+ Trigger note collision resolution first, since that may kill off
+ dots when merging.
+ */
+ if (Grob *collision = unsmob_grob (me->get_object ("note-collision")))
+ (void) collision->get_property ("positioning-done");
-struct Dot_position
-{
- int pos_;
- Direction dir_;
- Grob *dot_;
- bool extremal_head_;
+ me->set_property ("positioning-done", SCM_BOOL_T);
- Dot_position ()
- {
- dot_ = 0;
- pos_ = 0;
- dir_ = CENTER;
- }
-};
+ vector<Grob*> dots
+ = extract_grob_array (me, "dots");
-typedef std::map<int, Dot_position> Dot_configuration;
+ vector<Grob*> main_heads;
+ Real ss = 0;
-/*
- Value CFG according.
-*/
-int
-dot_config_badness (Dot_configuration const &cfg)
-{
- int t = 0;
- for (Dot_configuration::const_iterator i (cfg.begin ());
- i != cfg.end (); i++)
+ Grob *commonx = me;
+ for (vsize i = 0; i < dots.size (); i++)
{
- int p = i->first;
- int demerit = sqr (p - i->second.pos_) * 2;
-
- int dot_move_dir = sign (p - i->second.pos_);
- if (i->second.extremal_head_)
+ Grob *n = dots[i]->get_parent (Y_AXIS);
+ commonx = n->common_refpoint (commonx, X_AXIS);
+
+ if (Grob *stem = unsmob_grob (n->get_object("stem")))
{
- if (i->second.dir_
- && dot_move_dir != i->second.dir_)
- demerit += 3;
- else if (dot_move_dir != UP)
- demerit += 2;
+ commonx = stem->common_refpoint (commonx, X_AXIS);
+
+ if (Stem::first_head (stem) == n)
+ main_heads.push_back (n);
}
- else if (dot_move_dir != UP)
- demerit += 1;
-
- t += demerit;
}
- return t;
-}
+ vector<Box> boxes;
+ set<Grob*> stems;
-void
-print_dot_configuration (Dot_configuration const &cfg)
-{
- printf ("dotconf { ");
- for (Dot_configuration::const_iterator i (cfg.begin ());
- i != cfg.end (); i++)
- printf ("%d, ", i->first);
- printf ("} \n");
-}
+ extract_grob_set(me, "side-support-elements", support);
-/*
- Shift K and following (preceding) entries up (down) as necessary to
- prevent staffline collisions if D is up (down).
-
- If K is in CFG, then do nothing.
-*/
-
-Dot_configuration
-shift_one (Dot_configuration const &cfg,
- int k, Direction d)
-{
- Dot_configuration new_cfg;
- int offset = 0;
-
- if (d > 0)
+ Interval base_x;
+ for (vsize i = 0; i < main_heads.size (); i++)
+ base_x.unite (main_heads[i]->extent (commonx, X_AXIS));
+
+ for (vsize i = 0; i < support.size (); i++)
{
- for (Dot_configuration::const_iterator i (cfg.begin ());
- i != cfg.end (); i++)
+ Grob *s = support[i];
+ if (!ss)
+ ss = Staff_symbol_referencer::staff_space (s);
+
+ /* can't inspect Y extent of rest.
+
+ Rest collisions should wait after line breaking.
+ */
+ Interval y;
+ if (Rest::has_interface (s))
{
- int p = i->first;
- if (p == k)
- {
- if (Staff_symbol_referencer::on_staffline (i->second.dot_, p))
- p += d;
- else
- p += 2* d;
-
- offset = 2*d;
-
- new_cfg[p] = i->second;
- }
- else
- {
- if (new_cfg.find (p) == new_cfg.end ())
- {
- offset = 0;
- }
- new_cfg[p + offset] = i->second;
- }
+ base_x.unite (s->extent (commonx, X_AXIS));
+ continue;
}
- }
- else
- {
- Dot_configuration::const_iterator i (cfg.end ());
- do
+ else if (Stem::has_interface (s))
{
- i--;
-
- int p = i->first;
- if (p == k)
- {
- if (Staff_symbol_referencer::on_staffline (i->second.dot_, p))
- p += d;
- else
- p += 2* d;
-
- offset = 2*d;
-
- new_cfg[p] = i->second;
- }
- else
- {
- if (new_cfg.find (p) == new_cfg.end ())
- {
- offset = 0;
- }
-
- new_cfg[p + offset] = i->second;
- }
+ Real y1 = Stem::head_positions (s)[-get_grob_direction (s)];
+ Real y2 = y1 + get_grob_direction (s) * 7;
+
+ y.add_point (y1);
+ y.add_point (y2);
}
- while (i != cfg.begin ());
- }
+ else
+ y = s->extent (s, Y_AXIS);
- return new_cfg;
-}
+ y *= 2 / ss;
+ y += Staff_symbol_referencer::get_position (s);
+
+ Box b (s->extent (commonx, X_AXIS), y);
+ boxes.push_back (b);
-/*
- Remove the collision in CFG either by shifting up or down, whichever
- is best.
-*/
-void
-remove_collision (Dot_configuration &cfg, int p)
-{
- bool collide = cfg.find (p) != cfg.end ();
+ if (Grob *stem = unsmob_grob (s->get_object ("stem")))
+ stems.insert (stem);
+ }
- if (collide)
+ for (set<Grob*>::const_iterator i(stems.begin());
+ i != stems.end (); i++)
{
- Dot_configuration cfg_up = shift_one (cfg, p, UP);
- Dot_configuration cfg_down = shift_one (cfg, p, DOWN);
-
- int b_up = dot_config_badness (cfg_up);
- int b_down = dot_config_badness (cfg_down);
-
- cfg = (b_up < b_down) ? cfg_up : cfg_down;
+ Grob *stem = (*i);
+ Stencil flag = Stem::flag (stem);
+ if (!flag.is_empty ())
+ {
+ Interval y = flag.extent (Y_AXIS)
+ * (2 / ss)
+ + Stem::stem_end_position (stem);
+
+ Interval x = stem->relative_coordinate (commonx, X_AXIS)
+ + flag.extent (X_AXIS);
+
+ boxes.push_back (Box (x,y));
+ }
}
-}
-
-SCM
-Dot_column::do_shifts (Grob *me)
-{
- Link_array<Grob> dots
- = extract_grob_array (me, ly_symbol2scm ("dots"));
-
- { /*
- Trigger note collision resolution first, since that may kill off
- dots when merging.
- */
- Grob *c = 0;
- for (int i = dots.size (); i--;)
- {
- Grob *n = dots[i]->get_parent (Y_AXIS);
- if (c)
- c = n->common_refpoint (c, X_AXIS);
- else
- c = n;
- }
- for (int i = dots.size (); i--;)
- {
- Grob *n = dots[i]->get_parent (Y_AXIS);
- n->relative_coordinate (c, X_AXIS);
- }
- }
-
- dots.sort (compare_position);
- for (int i = dots.size (); i--;)
+
+ vector_sort (dots, position_less);
+ for (vsize i = dots.size (); i--;)
if (!dots[i]->is_live ())
- dots.del (i);
+ dots.erase (dots.begin () + i);
+
+ Dot_formatting_problem problem (boxes, base_x);
- Dot_configuration cfg;
- for (int i = 0;i < dots.size (); i++)
+ Dot_configuration cfg (problem);
+ for (vsize i = 0; i < dots.size (); i++)
{
Dot_position dp;
dp.dot_ = dots[i];
Grob *note = dots[i]->get_parent (Y_AXIS);
if (note)
{
- Grob *stem = unsmob_grob (note->get_property ("stem"));
+ Grob *stem = unsmob_grob (note->get_object ("stem"));
if (stem)
dp.extremal_head_ = Stem::first_head (stem) == note;
+
+ dp.x_extent_ = note->extent (commonx, X_AXIS);
}
int p = Staff_symbol_referencer::get_rounded_position (dp.dot_);
- dp.pos_ = p;
+ /* icky, since this should go via a Staff_symbol_referencer
+ offset callback but adding a dot overwrites Y-offset. */
+ p += (int) robust_scm2double (dp.dot_->get_property ("staff-position"), 0.0);
+ dp.pos_ = p;
if (dp.extremal_head_)
dp.dir_ = to_dir (dp.dot_->get_property ("direction"));
- remove_collision (cfg, p);
+ cfg.remove_collision (p);
cfg[p] = dp;
- if (Staff_symbol_referencer::on_staffline (dp.dot_, p))
- remove_collision (cfg, p);
+ if (Staff_symbol_referencer::on_line (dp.dot_, p))
+ cfg.remove_collision (p);
}
+ problem.register_configuration (cfg);
+
for (Dot_configuration::const_iterator i (cfg.begin ());
i != cfg.end (); i++)
{
+ /*
+ Junkme?
+ */
Staff_symbol_referencer::set_position (i->second.dot_, i->first);
}
-
- return SCM_UNSPECIFIED;
+
+ me->translate_axis (cfg.x_offset () - me->relative_coordinate (commonx, X_AXIS),
+ X_AXIS);
+ return SCM_BOOL_T;
}
void
-Dot_column::add_head (Grob *me, Grob *rh)
+Dot_column::add_head (Grob *me, Grob *head)
{
- Grob *d = unsmob_grob (rh->get_property ("dot"));
+ Grob *d = unsmob_grob (head->get_object ("dot"));
if (d)
{
- Side_position_interface::add_support (me, rh);
+ Side_position_interface::add_support (me, head);
Pointer_group_interface::add_grob (me, ly_symbol2scm ("dots"), d);
- d->add_offset_callback (Dot_column::force_shift_callback_proc, Y_AXIS);
+ d->set_property ("Y-offset", Grob::x_parent_positioning_proc);
+ // Dot formatting requests the Y-offset, -which- for rests may
+ // trigger post-linebreak callbacks.
+ if (!Rest::has_interface (head))
+ d->set_property ("X-offset", Grob::x_parent_positioning_proc);
Axis_group_interface::add_element (me, d);
}
}
-ADD_INTERFACE (Dot_column, "dot-column-interface",
- "Groups dot objects so they form a column, and position dots so they do not "
- "clash with staff lines ",
- "positioning-done direction stem");
+ADD_INTERFACE (Dot_column,
+ "Group dot objects so they form a column, and position"
+ " dots so they do not clash with staff lines.",
+
+ /* properties */
+ "dots "
+ "positioning-done "
+ "direction "
+ );