/*
note-column.cc -- implement Note_column
- source file of the LilyPond music typesetter
+ source file of the GNU LilyPond music typesetter
- (c) 1997 Han-Wen Nienhuys <hanwen@stack.nl>
+ (c) 1997--2000 Han-Wen Nienhuys <hanwen@cs.uu.nl>
*/
+#include <math.h> // ceil
+#include "axis-group-interface.hh"
+#include "dot-column.hh"
#include "note-column.hh"
-#include "debug.hh"
-#include "script.hh"
-#include "notehead.hh"
+#include "beam.hh"
+#include "note-head.hh"
#include "stem.hh"
+#include "rest.hh"
+#include "debug.hh"
+#include "paper-def.hh"
+#include "group-interface.hh"
+#include "staff-symbol-referencer.hh"
-IMPLEMENT_STATIC_NAME(Note_column);
-
-
-void
-Note_column::add(Stem*stem_l)
+bool
+Note_column::rest_b () const
{
- assert(!stem_l_);
- stem_l_ = stem_l;
- add_dependency(stem_l);
+ SCM r = get_elt_pointer ("rests");
+
+ return gh_pair_p (r);
}
-void
-Note_column::add(Notehead* n_l)
+int
+Note_column::shift_compare (Note_column *const &p1, Note_column*const&p2)
{
- if (head_l_arr_.size()){
- if (n_l->rest_b_ != rest_b_) return; // ugly fix. Should think about integrating rests into colunms.
- } else
- rest_b_ = n_l->rest_b_;
-
- head_l_arr_.push(n_l);
- add_dependency(n_l);
+ SCM s1 = p1->get_elt_property ("horizontal-shift");
+ SCM s2 = p2->get_elt_property ("horizontal-shift");
+
+ int h1 = (gh_number_p (s1))? gh_scm2int (s1) :0;
+ int h2 = (gh_number_p (s2)) ? gh_scm2int (s2):0;
+ return h1 - h2;
}
-void
-Note_column::add(Script*s_l)
+Note_column::Note_column( SCM s)
+ : Item (s)
{
- script_l_arr_.push(s_l);
- add_dependency(s_l);
+ set_elt_pointer ("rests", SCM_EOL);
+ set_elt_pointer ("note-heads", SCM_EOL);
+ Axis_group_interface (this).set_interface ();
+ Axis_group_interface (this).set_axes (X_AXIS, Y_AXIS);
+ Group_interface (this, "interfaces").add_thing (ly_symbol2scm ("Note_column"));
}
-void
-Note_column::translate(Offset o)
+Stem *
+Note_column::stem_l () const
{
- for (int i=0; i < head_l_arr_.size(); i++)
- head_l_arr_[i]->translate(o);
- for (int i=0; i < script_l_arr_.size(); i++)
- script_l_arr_[i]->translate(o);
- if (stem_l_)
- stem_l_->translate(o);
-}
+ SCM s = get_elt_pointer ("stem");
+ return dynamic_cast<Stem*> (unsmob_element (s));
+}
-void
-Note_column::do_print()const
+
+Slice
+Note_column::head_positions_interval() const
{
- mtor << "heads: " << head_l_arr_.size() << '\n';
- mtor << "scripts: " << script_l_arr_.size() << '\n';
+ Slice iv;
+
+ iv.set_empty ();
+
+ SCM h = get_elt_pointer ("note-heads");
+ for (; gh_pair_p (h); h = gh_cdr (h))
+ {
+ Score_element *se = unsmob_element (gh_car (h));
+ Staff_symbol_referencer_interface si (se);
+
+ int j = int (si.position_f ());
+ iv.unite (Slice (j,j));
+ }
+ return iv;
}
-Interval
-Note_column::do_height()const return r
+Direction
+Note_column::dir () const
{
- if (stem_l_)
- r.unite(stem_l_->height());
- for (int i=0; i < head_l_arr_.size(); i++)
- r.unite(head_l_arr_[i]->height());
- for (int i=0; i < script_l_arr_.size(); i++)
- r.unite(script_l_arr_[i]->height());
+ if (stem_l ())
+ return stem_l ()->get_direction ();
+ else if (gh_pair_p (get_elt_pointer ("note-heads")))
+ return (Direction)sign (head_positions_interval().center ());
+
+ programming_error ("Note column without heads and stem!");
+ return CENTER;
}
-Interval
-Note_column::do_width()const return r;
+
+void
+Note_column::set_stem (Stem * stem_l)
{
- if (stem_l_)
- r.unite(stem_l_->width());
- for (int i=0; i < head_l_arr_.size(); i++)
- r.unite(head_l_arr_[i]->width());
- for (int i=0; i < script_l_arr_.size(); i++)
- r.unite(script_l_arr_[i]->width());
+ set_elt_pointer ("stem", stem_l->self_scm_);
+
+ add_dependency (stem_l);
+ Axis_group_interface (this).add_element (stem_l);
}
+
+
void
-Note_column::do_pre_processing()
+Note_column::add_head (Rhythmic_head *h)
{
- if (stem_l_ && !dir_i_)
- dir_i_ = stem_l_->dir_i_;
-
- if (!script_l_arr_.size())
- return;
-
- Array<Script*> placed_l_arr_a[4];
- for (int i=0; i < script_l_arr_.size(); i++) {
- Script*s_l = script_l_arr_[i];
- int j = (s_l->dir_i_ >0) ? 0 : 2;
- if (!s_l->inside_staff_b_)
- j ++;
-
- placed_l_arr_a[j].push(s_l);
+ if (Rest*r=dynamic_cast<Rest *> (h))
+ {
+ Pointer_group_interface gi (this, "rests");
+ gi.add_element (h);
}
- for (int j =0; j <4; j++) {
- placed_l_arr_a[j].sort( Script::compare);
- }
-
- Notehead *top_head_l=0;
- Notehead *bot_head_l=0;
- for (int i=0; i< head_l_arr_.size(); i++) {
- if (head_l_arr_[i]->extremal == -1)
- bot_head_l = head_l_arr_[i];
- else if (head_l_arr_[i]->extremal == 1)
- top_head_l = head_l_arr_[i];
- }
- /* argh. This sux. */
- if (!top_head_l)
- top_head_l = bot_head_l;
- if (!bot_head_l)
- bot_head_l = top_head_l;
- assert(bot_head_l && top_head_l);
- Item *support_l=top_head_l;
- int j;
- for (j = 0; j < 2; j++ ) {
- for (int i=0; i < placed_l_arr_a[j].size(); i++) {
- placed_l_arr_a[j][i]->add_support(support_l);
- support_l = placed_l_arr_a[j][i];
- }
- }
-
- support_l=bot_head_l;
- for (; j < 4; j++ ) {
- for (int i=0; i < placed_l_arr_a[j].size(); i++) {
- placed_l_arr_a[j][i]->add_support(support_l);
- support_l = placed_l_arr_a[j][i];
- }
+ if (Note_head *nh=dynamic_cast<Note_head *> (h))
+ {
+ Pointer_group_interface gi (this, "note-heads");
+ gi.add_element (nh);
}
+ Axis_group_interface (this).add_element (h);
}
-Note_column::Note_column()
+/**
+ translate the rest symbols vertically by amount DY_I.
+ */
+void
+Note_column::translate_rests (int dy_i)
{
- h_shift_b_ =false;
- stem_l_ =0;
- rest_b_ = false;
- dir_i_ =0;
+ SCM s = get_elt_pointer ("rests");
+ for (; gh_pair_p (s); s = gh_cdr (s))
+ {
+ Score_element * se = unsmob_element (gh_car (s));
+ Staff_symbol_referencer_interface si (se);
+
+ se->translate_axis (dy_i * si.staff_space ()/2.0, Y_AXIS);
+ }
}
+
+
void
-Note_column::sort()
+Note_column::set_dotcol (Dot_column *d)
{
- head_l_arr_.sort( Notehead::compare);
+ Axis_group_interface (this).add_element (d);
}
+
+/*
+ [TODO]
+ handle rest under beam (do_post: beams are calculated now)
+ what about combination of collisions and rest under beam.
+
+ Should lookup
-Interval_t<int>
-Note_column::head_positions_interval()const
+ rest -> stem -> beam -> interpolate_y_position ()
+*/
+
+void
+Note_column::after_line_breaking ()
+{
+ if (!stem_l () || !rest_b ())
+ return;
+
+ Beam * b = stem_l ()->beam_l ();
+ if (!b || !b->visible_stem_count ())
+ return;
+
+ /* ugh. Should be done by beam.
+ (what? should be done --jcn)
+ scary too?: height is calculated during post_processing
+ */
+ Real beam_dy = 0;
+ Real beam_y = 0;
+
+ SCM s = b->get_elt_property ("height");
+ if (gh_number_p (s))
+ beam_dy = gh_scm2double (s);
+
+ s = b->get_elt_property ("y-position");
+ if (gh_number_p (s))
+ beam_y = gh_scm2double (s);
+
+
+ Real x0 = b->first_visible_stem ()->relative_coordinate (0, X_AXIS);
+ Real dx = b->last_visible_stem ()->relative_coordinate (0, X_AXIS) - x0;
+ Real dydx = beam_dy && dx ? beam_dy/dx : 0;
+
+ Direction d = stem_l ()->get_direction ();
+ Real beamy = (stem_l ()->relative_coordinate (0, X_AXIS) - x0) * dydx + beam_y;
+
+ s = get_elt_pointer ("rests");
+ Score_element * se = unsmob_element (gh_car (s));
+ Staff_symbol_referencer_interface si (se);
+
+ Real staff_space = si.staff_space ();
+ Real rest_dim = extent (Y_AXIS)[d]*2.0 /staff_space ;
+
+ Real minimum_dist
+ = paper_l ()->get_var ("restcollision_minimum_beamdist") ;
+ Real dist =
+ minimum_dist + -d * (beamy - rest_dim) >? 0;
+
+ int stafflines = si.line_count ();
+
+ // move discretely by half spaces.
+ int discrete_dist = int (ceil (dist ));
+
+ // move by whole spaces inside the staff.
+ if (discrete_dist < stafflines+1)
+ discrete_dist = int (ceil (discrete_dist / 2.0)* 2.0);
+
+ translate_rests (-d * discrete_dist);
+}
+
+
+Interval
+Note_column::rest_dim () const
{
- ( (Note_column*)this)->sort();
- return Interval_t<int> ( head_l_arr_[0]->position,
- head_l_arr_.top()->position);
+ Interval restdim;
+ SCM s = get_elt_pointer ("rests");
+ for (; gh_pair_p (s); s = gh_cdr (s))
+ {
+ Score_element * sc = unsmob_element ( gh_car (s));
+ restdim.unite (sc->extent (Y_AXIS));
+ }
+
+ return restdim;
+}
+Note_head*
+Note_column::first_head () const
+{
+ Stem * st = stem_l ();
+ return st? st->first_head (): 0;
}