/*
- cluster-engraver.cc -- implement Cluster_engraver
+ This file is part of LilyPond, the GNU music typesetter.
- (c) 2002 Juergen Reuter <reuter@ipd.uka.de>
+ Copyright (C) 2002--2012 Juergen Reuter <reuter@ipd.uka.de>
+ Han-Wen Nienhuys <hanwen@xs4all.nl>
+
+ LilyPond is free software: you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation, either version 3 of the License, or
+ (at your option) any later version.
+
+ LilyPond is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with LilyPond. If not, see <http://www.gnu.org/licenses/>.
*/
#include "engraver.hh"
-#include "item.hh"
#include "spanner.hh"
#include "note-head.hh"
-#include "protected-scm.hh"
-#include "warn.hh"
+#include "note-column.hh"
+#include "pointer-group-interface.hh"
+#include "pitch.hh"
+#include "stream-event.hh"
+#include "item.hh"
-class Cluster_engraver : public Engraver
+#include "translator.icc"
+
+class Cluster_spanner_engraver : public Engraver
{
protected:
-TRANSLATOR_DECLARATIONS(Cluster_engraver);
- virtual void start_translation_timestep ();
- virtual bool try_music (Music *);
- virtual void process_music ();
- virtual void acknowledge_grob (Grob_info);
- virtual void stop_translation_timestep ();
-
+ TRANSLATOR_DECLARATIONS (Cluster_spanner_engraver);
+ DECLARE_TRANSLATOR_LISTENER (cluster_note);
+ DECLARE_ACKNOWLEDGER (note_column);
+ void stop_translation_timestep ();
+ virtual void process_music ();
+ virtual void finalize ();
private:
- Drul_array<Music*> reqs_drul_;
- Pitch pitch_min_, pitch_max_;
- Spanner *cluster_;
- Protected_scm columns_scm_;
+ vector<Stream_event *> cluster_notes_;
+ Item *beacon_;
+
+ void typeset_grobs ();
+ Spanner *spanner_;
+ Spanner *finished_spanner_;
};
-void reset_min_max (Pitch *pitch_min, Pitch *pitch_max)
+Cluster_spanner_engraver::Cluster_spanner_engraver ()
{
- /*
- * (pitch_min > pitch_max) means that pitches are not yet
- * initialized
- */
- *pitch_min = Pitch (0, 0, +1);
- *pitch_max = Pitch (0, 0, -1);
+ spanner_ = 0;
+ finished_spanner_ = 0;
+ beacon_ = 0;
}
-Cluster_engraver::Cluster_engraver ()
+void
+Cluster_spanner_engraver::finalize ()
{
- cluster_ = 0;
- columns_scm_ = SCM_EOL;
- reqs_drul_[LEFT] = reqs_drul_[RIGHT] = 0;
+ typeset_grobs ();
+ finished_spanner_ = spanner_;
+ spanner_ = 0;
+ typeset_grobs ();
}
-bool
-Cluster_engraver::try_music (Music *m)
+void
+Cluster_spanner_engraver::typeset_grobs ()
{
- if (m->is_mus_type ("abort-event"))
- {
- reqs_drul_[START] = 0;
- reqs_drul_[STOP] = 0;
- if (cluster_)
- {
- cluster_->suicide ();
- cluster_ = 0;
- columns_scm_ = SCM_EOL;
- }
- }
- else if (m->is_mus_type ("cluster-event"))
+ if (finished_spanner_)
{
- Direction d = to_dir (m->get_mus_property ("span-direction"));
- reqs_drul_[d] = m;
- return true;
+ if (!finished_spanner_->get_bound (RIGHT))
+ {
+ finished_spanner_->set_bound (RIGHT,
+ finished_spanner_->get_bound (LEFT));
+
+ }
+
+ finished_spanner_ = 0;
}
- return false;
+ beacon_ = 0;
}
+IMPLEMENT_TRANSLATOR_LISTENER (Cluster_spanner_engraver, cluster_note);
void
-Cluster_engraver::process_music ()
+Cluster_spanner_engraver::listen_cluster_note (Stream_event *ev)
{
- if (reqs_drul_[STOP])
+ cluster_notes_.push_back (ev);
+}
+
+void
+Cluster_spanner_engraver::process_music ()
+{
+ if (cluster_notes_.size ())
{
- if (!cluster_)
- {
- reqs_drul_[STOP]->origin ()->warning ("can't find start of cluster");
- }
- else
- {
- Grob *bound = unsmob_grob (get_property ("currentMusicalColumn"));
- cluster_->set_bound (RIGHT, bound);
- }
+ SCM c0scm = get_property ("middleCPosition");
+
+ int c0 = scm_is_number (c0scm) ? scm_to_int (c0scm) : 0;
+ int pmax = INT_MIN;
+ int pmin = INT_MAX;
+
+ for (vsize i = 0; i < cluster_notes_.size (); i++)
+ {
+ Pitch *pit = unsmob_pitch (cluster_notes_[i]->get_property ("pitch"));
+
+ int p = (pit ? pit->steps () : 0) + c0;
+
+ pmax = max (pmax, p);
+ pmin = min (pmin, p);
+ }
+
+ beacon_ = make_item ("ClusterSpannerBeacon", cluster_notes_[0]->self_scm ());
+ beacon_->set_property ("positions",
+ scm_cons (scm_from_int (pmin),
+ scm_from_int (pmax)));
}
- if (reqs_drul_[START])
+
+ if (beacon_ && !spanner_)
+ spanner_ = make_spanner ("ClusterSpanner", cluster_notes_[0]->self_scm ());
+
+ if (beacon_ && spanner_)
{
- if (cluster_)
- {
- reqs_drul_[START]->origin ()->warning ("may not nest clusters");
- }
- else
- {
- SCM basicProperties = get_property ("Cluster");
- cluster_ = new Spanner (basicProperties);
- columns_scm_ = SCM_EOL;
- Grob *bound = unsmob_grob (get_property ("currentMusicalColumn"));
- cluster_->set_bound (LEFT, bound);
- announce_grob (cluster_, bound->self_scm ());
- }
- reqs_drul_[START] = 0;
+ add_bound_item (spanner_, beacon_);
+ Pointer_group_interface::add_grob (spanner_, ly_symbol2scm ("columns"), beacon_);
}
}
void
-Cluster_engraver::start_translation_timestep ()
+Cluster_spanner_engraver::stop_translation_timestep ()
{
- reset_min_max (&pitch_min_, &pitch_max_);
+ typeset_grobs ();
+ cluster_notes_.clear ();
}
void
-Cluster_engraver::stop_translation_timestep ()
+Cluster_spanner_engraver::acknowledge_note_column (Grob_info info)
{
- if (cluster_)
+ if (!beacon_ && Note_column::has_interface (info.grob ()))
{
- SCM column_scm = get_property ("currentMusicalColumn");
- if (column_scm == SCM_EOL)
- {
- programming_error("failed retrieving current column");
- }
- else
- {
- if (Pitch::compare (pitch_min_, pitch_max_) <= 0)
- {
- Real y_bottom = 0.5 * pitch_min_.steps ();
- SCM c0 = get_property ("centralCPosition");
- if (gh_number_p (c0))
- y_bottom += 0.5 * gh_scm2int (c0);
- else
- programming_error ("Cluster_engraver: failed evaluating c0");
- Real y_top = y_bottom +
- 0.5 * (pitch_max_.steps () - pitch_min_.steps ());
- column_scm = Protected_scm (column_scm);
- SCM segment = scm_list_n (column_scm,
- gh_double2scm (y_bottom),
- gh_double2scm (y_top),
- SCM_UNDEFINED);
- segment = scm_list_n (segment, SCM_UNDEFINED);
- columns_scm_ = (columns_scm_ != SCM_EOL) ?
- gh_append2 (columns_scm_, segment) : segment;
- }
- else
- {
- /* This timestep is caused by a different voice of the
- same staff and hence should be ignored. */
- }
- }
-
- if (reqs_drul_[STOP])
- {
- reqs_drul_[STOP] = 0;
- cluster_->set_grob_property ("segments", columns_scm_);
- typeset_grob (cluster_);
- cluster_ = 0;
- columns_scm_ = SCM_EOL;
- }
+ finished_spanner_ = spanner_;
+ spanner_ = 0;
}
}
-void
-Cluster_engraver::acknowledge_grob (Grob_info info)
-{
- Item *item = dynamic_cast <Item *>(info.grob_);
- if (item)
- {
- if (Note_head::has_interface (info.grob_))
- {
- Music *nr = info.music_cause ();
- if (nr && nr->is_mus_type ("note-event"))
- {
- Pitch pitch = *unsmob_pitch (nr->get_mus_property ("pitch"));
- if (Pitch::compare (pitch_min_, pitch_max_) > 0) // already init'd?
- {
- // not yet init'd; use current pitch to init min/max
- pitch_min_ = pitch;
- pitch_max_ = pitch;
- }
- else if (Pitch::compare (pitch, pitch_max_) > 0) // new max?
- {
- pitch_max_ = pitch;
- }
- else if (Pitch::compare (pitch, pitch_min_) < 0) // new min?
- {
- pitch_min_ = pitch;
- }
- }
- }
- }
-}
+ADD_ACKNOWLEDGER (Cluster_spanner_engraver, note_column);
+ADD_TRANSLATOR (Cluster_spanner_engraver,
+ /* doc */
+ "Engrave a cluster using @code{Spanner} notation.",
+
+ /* create */
+ "ClusterSpanner "
+ "ClusterSpannerBeacon ",
+
+ /* read */
+ "",
+
+ /* write */
+ ""
+ );
-ENTER_DESCRIPTION(Cluster_engraver,
-/* descr */ "engraves a cluster",
-/* creats*/ "Cluster",
-/* accepts */ "cluster-event",
-/* acks */ "note-head-interface",
-/* reads */ "",
-/* write */ "");