-/*
- piano-pedal-engraver.cc -- implement Piano_pedal_engraver
-
+/*
+ piano-pedal-engraver.cc -- implement Piano_pedal_engraver
+
source file of the GNU LilyPond music typesetter
-
- (c) 2000--2001 Jan Nieuwenhuizen <janneke@gnu.org>
- */
+
+ (c) 2000--2006 Jan Nieuwenhuizen <janneke@gnu.org>
+
+ Chris Jackson <chris@fluffhouse.org.uk> - extended to support
+ bracketed pedals.
+*/
#include "engraver.hh"
-#include "musical-request.hh"
-#include "grob.hh"
-#include "item.hh"
#include "lily-guile.hh"
-#include "rhythmic-head.hh"
-#include "stem.hh"
#include "side-position-interface.hh"
#include "staff-symbol-referencer.hh"
-#include "item.hh"
+#include "axis-group-interface.hh"
+#include "context.hh"
+#include "directional-element-interface.hh"
+#include "note-column.hh"
+#include "warn.hh"
+
+/*
+ Urgh. This engraver is too complex. rewrite. --hwn
+*/
+
+struct Pedal_info
+{
+ char const *name_;
+
+ /*
+ Event for currently running pedal.
+ */
+ Music *current_bracket_ev_;
+
+ /*
+ Event for currently starting pedal, (necessary?
+
+ distinct from current_bracket_ev_, since current_bracket_ev_ only
+ necessary for brackets, not for text style.
+ */
+ Music *start_ev_;
+
+ /*
+ Events that were found in this timestep.
+ */
+ Drul_array<Music *> event_drul_;
+ Item *item_;
+ Spanner *bracket_; // A single portion of a pedal bracket
+ Spanner *finished_bracket_;
+
+ /*
+ This grob contains all the pedals of the same type on the same staff
+ */
+ Spanner *line_spanner_;
+ Spanner *finished_line_spanner_;
+};
class Piano_pedal_engraver : public Engraver
{
public:
- VIRTUAL_COPY_CONS (Translator);
- Piano_pedal_engraver ();
+ TRANSLATOR_DECLARATIONS (Piano_pedal_engraver);
~Piano_pedal_engraver ();
protected:
virtual void initialize ();
- virtual bool try_music (Music*);
- virtual void stop_translation_timestep ();
- virtual void start_translation_timestep ();
- virtual void acknowledge_grob (Grob_info);
- virtual void create_grobs ();
+ virtual void finalize ();
+ virtual bool try_music (Music *);
+ void stop_translation_timestep ();
+ DECLARE_ACKNOWLEDGER (note_column);
+ void process_music ();
private:
- struct Pedal_info
- {
- char const * name_;
- Span_req* start_req_l_;
- Drul_array<Span_req*> req_l_drul_;
- Item* item_p_;
- };
-
Pedal_info *info_list_;
-};
-ADD_THIS_TRANSLATOR (Piano_pedal_engraver);
+ /*
+ Record a stack of the current pedal spanners, so if more than one pedal
+ occurs simultaneously then extra space can be added between them.
+ */
+
+ Link_array<Spanner> previous_;
+ void del_linespanner (Spanner *);
+
+ void create_text_grobs (Pedal_info *p, bool);
+ void create_bracket_grobs (Pedal_info *p, bool);
+ void typeset_all (Pedal_info *p);
+};
Piano_pedal_engraver::Piano_pedal_engraver ()
{
info_list_ = 0;
}
+
void
-Piano_pedal_engraver::initialize()
+Piano_pedal_engraver::initialize ()
{
- info_list_ = new Pedal_info[4];
- Pedal_info *p = info_list_;
+ char *names [] = { "Sostenuto", "Sustain", "UnaCorda", 0 };
+ info_list_ = new Pedal_info[sizeof (names) / sizeof (char const *)];
+ Pedal_info *p = info_list_;
- char * names [] = { "Sostenuto", "Sustain", "UnaChorda", 0 };
- char **np = names ;
+ char **np = names;
do
{
p->name_ = *np;
- p->item_p_ = 0;
- p->req_l_drul_[START] = 0;
- p->req_l_drul_[STOP] = 0;
- p->start_req_l_ = 0;
+ p->item_ = 0;
+ p->bracket_ = 0;
+ p->finished_bracket_ = 0;
+ p->line_spanner_ = 0;
+ p->finished_line_spanner_ = 0;
+ p->current_bracket_ev_ = 0;
+ p->event_drul_[START] = 0;
+ p->event_drul_[STOP] = 0;
+ p->start_ev_ = 0;
p++;
}
- while (*(np ++));
+ while (* (np++));
}
-Piano_pedal_engraver::~Piano_pedal_engraver()
+Piano_pedal_engraver::~Piano_pedal_engraver ()
{
delete[] info_list_;
}
/*
- Urg: Code dup
- I'm a script
- */
+ Urg: Code dup
+ I'm a script
+*/
void
-Piano_pedal_engraver::acknowledge_grob (Grob_info info)
+Piano_pedal_engraver::acknowledge_note_column (Grob_info info)
{
- for (Pedal_info*p = info_list_; p && p->name_; p ++)
+ for (Pedal_info *p = info_list_; p && p->name_; p++)
{
- if (p->item_p_)
+ if (p->line_spanner_)
{
- if (Rhythmic_head::has_interface (info.elem_l_))
- {
- Side_position::add_support (p->item_p_, info.elem_l_);
-
- if (Side_position::get_axis(p->item_p_) == X_AXIS
- && !p->item_p_->parent_l (Y_AXIS))
- p->item_p_->set_parent (info.elem_l_, Y_AXIS);
- }
- if (Stem::has_interface (info.elem_l_))
- {
- Side_position::add_support (p->item_p_,info.elem_l_);
- }
+ Side_position_interface::add_support (p->line_spanner_, info.grob ());
+ add_bound_item (p->line_spanner_, info.grob ());
}
+ if (p->bracket_)
+ add_bound_item (p->bracket_, info.grob ());
+ if (p->finished_bracket_)
+ add_bound_item (p->finished_bracket_, info.grob ());
}
}
bool
Piano_pedal_engraver::try_music (Music *m)
{
- if (Span_req * s = dynamic_cast<Span_req*>(m))
+ if (m->is_mus_type ("pedal-event"))
{
- for (Pedal_info*p = info_list_; p->name_; p ++)
+ for (Pedal_info *p = info_list_; p->name_; p++)
{
- if (scm_equal_p (s->get_mus_property ("span-type"),
- ly_str02scm (p->name_))==SCM_BOOL_T)
+ String nm = p->name_ + String ("Event");
+ if (ly_is_equal (m->get_property ("name"),
+ scm_str2symbol (nm.to_str0 ())))
{
- p->req_l_drul_[s->get_span_dir()] = s;
+ Direction d = to_dir (m->get_property ("span-direction"));
+ p->event_drul_[d] = m;
return true;
}
}
}
void
-Piano_pedal_engraver::create_grobs ()
+Piano_pedal_engraver::process_music ()
{
- for (Pedal_info*p = info_list_; p && p->name_; p ++)
- {
- if (p->item_p_ || ! (p->req_l_drul_[STOP] || p->req_l_drul_[START]))
- continue;
-
- SCM s = SCM_EOL;
- SCM strings = get_property( ("pedal" + String (p->name_) + "Strings").ch_C());
- if( scm_ilength (strings) < 3)
- continue;
-
- if (p->req_l_drul_[STOP] && p->req_l_drul_[START])
+ for (Pedal_info *p = info_list_; p && p->name_; p++)
+ {
+ if (p->event_drul_[STOP] || p->event_drul_[START])
{
- if (!p->start_req_l_)
- {
- p->req_l_drul_[STOP]->origin ()->warning (_f ("can't find start of piano pedal: %s", p->name_ ));
- }
- else
+ if (!p->line_spanner_)
{
- s = gh_cadr (strings);
+ String name = String (p->name_) + "PedalLineSpanner";
+ Music *rq = (p->event_drul_[START] ? p->event_drul_[START] : p->event_drul_[STOP]);
+ p->line_spanner_ = make_spanner (name.to_str0 (), rq->self_scm ());
}
- p->start_req_l_ = p->req_l_drul_[START];
+
+ /* Choose the appropriate grobs to add to the line spanner
+ These can be text items or text-spanners
+ */
+
+ /*
+ ugh, code dup, should read grob to create from other
+ property.
+
+ bracket: |_________/\____|
+ text: Ped. *Ped. *
+ mixed: Ped. _____/\____|
+ */
+
+ String prop = String ("pedal") + p->name_ + "Style";
+ SCM style = get_property (prop.to_str0 ());
+
+ bool mixed = style == ly_symbol2scm ("mixed");
+ bool bracket = (mixed
+ || style == ly_symbol2scm ("bracket"));
+ bool text = (style == ly_symbol2scm ("text")
+ || mixed);
+
+ if (text && !p->item_)
+ create_text_grobs (p, mixed);
+ if (bracket)
+ create_bracket_grobs (p, mixed);
}
- else if (p->req_l_drul_[STOP])
+ }
+}
+
+void
+Piano_pedal_engraver::create_text_grobs (Pedal_info *p, bool mixed)
+{
+ SCM s = SCM_EOL;
+ SCM strings = get_property (("pedal" + String (p->name_) + "Strings").to_str0 ());
+
+ if (scm_ilength (strings) < 3)
+ {
+ Music *m = p->event_drul_[START];
+ if (!m) m = p->event_drul_ [STOP];
+
+ String msg = _f ("expect 3 strings for piano pedals, found: %d",
+ scm_ilength (strings));
+ if (m)
+ m->origin ()->warning (msg);
+ else
+ warning (msg);
+
+ return;
+ }
+
+ if (p->event_drul_[STOP] && p->event_drul_[START])
+ {
+ if (!mixed)
{
- if (!p->start_req_l_)
- {
- p->req_l_drul_[STOP]->origin ()->warning (_f ("can't find start of piano pedal: %s", p->name_ ));
- }
+ if (!p->start_ev_)
+ p->event_drul_[STOP]->origin ()->warning (_f ("can't find start of piano pedal: `%s'", p->name_));
else
- {
- s = gh_car (strings);
- }
- p->start_req_l_ = 0;
+ s = scm_cadr (strings);
+ p->start_ev_ = p->event_drul_[START];
}
- else if (p->req_l_drul_[START])
+ }
+ else if (p->event_drul_[STOP])
+ {
+ if (!mixed)
{
- p->start_req_l_ = p->req_l_drul_[START];
- s = gh_caddr (strings);
+ if (!p->start_ev_)
+ p->event_drul_[STOP]->origin ()->warning (_f ("can't find start of piano pedal: `%s'", p->name_));
+ else
+ s = scm_caddr (strings);
+ p->start_ev_ = 0;
}
-
- if (gh_string_p (s))
+ }
+ else if (p->event_drul_[START])
+ {
+ p->start_ev_ = p->event_drul_[START];
+ s = scm_car (strings);
+ if (!mixed)
{
- String propname = String (p->name_) + "Pedal";
- p->item_p_ = new Item (get_property (propname.ch_C()));
- p->item_p_->set_grob_property ("text", s);
-
- announce_grob (p->item_p_,
- p->req_l_drul_[START]
- ? p->req_l_drul_[START]
- : p->req_l_drul_[STOP]);
+ /*
+ Code dup?! see below.
+ */
+ if (previous_.size ())
+ // add extra space below the previous already-occuring pedal
+ Side_position_interface::add_support (p->line_spanner_,
+ previous_.top ());
+ previous_.push (p->line_spanner_);
}
- p->req_l_drul_[START] = 0;
- p->req_l_drul_[STOP] = 0;
+ }
+
+ if (scm_is_string (s))
+ {
+ String propname = String (p->name_) + "Pedal";
+
+ p->item_ = make_item (propname.to_str0 (), (p->event_drul_[START]
+ ? p->event_drul_[START]
+ : p->event_drul_[STOP])->self_scm ());
+
+ p->item_->set_property ("text", s);
+ Axis_group_interface::add_element (p->line_spanner_, p->item_);
+ }
+
+ if (!mixed)
+ {
+ p->event_drul_[START] = 0;
+ p->event_drul_[STOP] = 0;
}
}
void
-Piano_pedal_engraver::stop_translation_timestep ()
+Piano_pedal_engraver::create_bracket_grobs (Pedal_info *p, bool mixed)
{
- Item * sustain = 0;
- for (Pedal_info*p = info_list_; p->name_; p ++)
+ if (!p->bracket_ && p->event_drul_[STOP])
{
- if (p->name_ == String ("Sustain"))
- sustain = p->item_p_;
+ String msg = _f ("can't find start of piano pedal bracket: `%s'", p->name_);
+ p->event_drul_[STOP]->origin ()->warning (msg);
+ p->event_drul_[STOP] = 0;
}
- for (Pedal_info*p = info_list_; p->name_; p ++)
+ if (p->event_drul_[STOP])
{
- if (p->item_p_)
+ assert (!p->finished_bracket_);
+
+ Grob *cmc = unsmob_grob (get_property ("currentMusicalColumn"));
+
+ if (!p->bracket_->get_bound (RIGHT))
+ p->bracket_->set_bound (RIGHT, cmc);
+
+ /*
+ Set properties so that the stencil-creating function will
+ know whether the right edge should be flared ___/
+ */
+
+ if (!p->event_drul_[START])
+ {
+ SCM flare = p->bracket_->get_property ("bracket-flare");
+ p->bracket_->set_property ("bracket-flare", scm_cons (scm_car (flare),
+ scm_from_double (0)));
+ }
+
+ p->finished_bracket_ = p->bracket_;
+ p->bracket_ = 0;
+ p->current_bracket_ev_ = 0;
+ }
+
+ if (p->event_drul_[START])
+ {
+ p->start_ev_ = p->event_drul_[START];
+ p->current_bracket_ev_ = p->event_drul_[START];
+
+ p->bracket_ = make_spanner ("PianoPedalBracket", p->event_drul_[START]->self_scm ());
+
+ /*
+ Set properties so that the stencil-creating function will
+ know whether the left edge should be flared \___
+ */
+
+ if (!p->finished_bracket_)
+ {
+ SCM flare = p->bracket_->get_property ("bracket-flare");
+ p->bracket_->set_property ("bracket-flare", scm_cons (scm_from_double (0), scm_cdr (flare)));
+ }
+
+ /* Set this property for 'mixed style' pedals, Ped._______/\ ,
+ so the stencil function will shorten the ____ line by the length of the Ped. text.
+ */
+
+ if (mixed)
{
- Side_position::add_staff_support (p->item_p_);
-
/*
- Hmm.
+ Mixed style: Store a pointer to the preceding text for use in
+ calculating the length of the line
+
+
+ TODO:
+
+ WTF is pedal-text not the bound of the object? --hwn
*/
- if (p->name_ != String ("Sustain"))
- {
- if (sustain)
- {
- Side_position::add_support (p->item_p_,sustain);
- }
- }
- typeset_grob (p->item_p_);
+ if (p->item_)
+ p->bracket_->set_object ("pedal-text", p->item_->self_scm ());
+ }
+
+ /*
+ We do not use currentMusicalColumn for the left span-point.
+ If the column as accidentals (eg on a different stave), the
+ currentMusicalColumn is too wide, making the bracket too big.
+
+ TODO:
+
+ Hmm. What do we do when there are no notes when the spanner starts?
+
+ TODO:
+
+ what about the right span point?
+
+ */
+ Axis_group_interface::add_element (p->line_spanner_, p->bracket_);
+
+ if (!p->event_drul_[STOP])
+ {
+
+ /*
+ code dup. --hwn.
+
+ // position new pedal spanner below the current one
+ */
+ if (previous_.size ())
+ Side_position_interface::add_support (p->line_spanner_, previous_.top ());
+
+ previous_.push (p->line_spanner_);
+ }
+ }
+
+ p->event_drul_[START] = 0;
+ p->event_drul_[STOP] = 0;
+}
+
+void
+Piano_pedal_engraver::finalize ()
+{
+ for (Pedal_info *p = info_list_; p && p->name_; p++)
+ {
+ /*
+ suicide?
+ */
+ if (p->line_spanner_
+ && !p->line_spanner_->is_live ())
+ p->line_spanner_ = 0;
+
+ if (p->bracket_
+ && !p->bracket_->is_live ())
+ p->bracket_ = 0;
+
+ if (p->bracket_)
+ {
+ SCM cc = get_property ("currentCommandColumn");
+ Item *c = unsmob_item (cc);
+ if (p->line_spanner_)
+ p->line_spanner_->set_bound (RIGHT, c);
+ p->bracket_->set_bound (RIGHT, c);
+
+ p->finished_bracket_ = p->bracket_;
+ p->bracket_ = 0;
+ p->finished_line_spanner_ = p->line_spanner_;
+ p->line_spanner_ = 0;
+ typeset_all (p);
+ }
+
+ if (p->line_spanner_)
+ {
+ p->finished_line_spanner_ = p->line_spanner_;
+ typeset_all (p);
}
- p->item_p_ = 0;
}
}
void
-Piano_pedal_engraver::start_translation_timestep ()
+Piano_pedal_engraver::del_linespanner (Spanner *g)
+{
+ int idx = previous_.find_index (g);
+ if (idx >= 0)
+ previous_.del (idx);
+}
+
+void
+Piano_pedal_engraver::stop_translation_timestep ()
{
- for (Pedal_info*p = info_list_; p->name_; p ++)
+ for (Pedal_info *p = info_list_; p && p->name_; p++)
{
- p->req_l_drul_[STOP] = 0;
- p->req_l_drul_[START] = 0;
+ if (!p->bracket_)
+ {
+ p->finished_line_spanner_ = p->line_spanner_;
+ p->line_spanner_ = 0;
+ del_linespanner (p->finished_line_spanner_);
+ }
+
+ typeset_all (p);
+ }
+
+ for (Pedal_info *p = info_list_; p->name_; p++)
+ {
+ p->event_drul_[STOP] = 0;
+ p->event_drul_[START] = 0;
}
}
+
+void
+Piano_pedal_engraver::typeset_all (Pedal_info *p)
+{
+ /*
+ Handle suicide.
+ */
+ if (p->finished_line_spanner_
+ && !p->finished_line_spanner_->is_live ())
+ p->finished_line_spanner_ = 0;
+ if (p->finished_bracket_
+ && !p->finished_bracket_->is_live ())
+ p->finished_bracket_ = 0;
+
+ if (p->item_)
+ p->item_ = 0;
+
+ if (p->finished_bracket_)
+ {
+ Grob *r = p->finished_bracket_->get_bound (RIGHT);
+ if (!r)
+ p->finished_bracket_->set_bound (RIGHT, unsmob_grob (get_property ("currentMusicalColumn")));
+
+ p->finished_bracket_ = 0;
+ }
+
+ if (p->finished_line_spanner_)
+ {
+ Grob *l = p->finished_line_spanner_->get_bound (LEFT);
+ Grob *r = p->finished_line_spanner_->get_bound (RIGHT);
+ if (!r && l)
+ p->finished_line_spanner_->set_bound (RIGHT, l);
+ else if (!l && r)
+ p->finished_line_spanner_->set_bound (LEFT, r);
+ else if (!r && !l)
+ {
+ Grob *cc = unsmob_grob (get_property ("currentMusicalColumn"));
+ Item *ci = dynamic_cast<Item *> (cc);
+ p->finished_line_spanner_->set_bound (RIGHT, ci);
+ p->finished_line_spanner_->set_bound (LEFT, ci);
+ }
+
+ p->finished_line_spanner_ = 0;
+ }
+}
+
+#include "translator.icc"
+ADD_ACKNOWLEDGER (Piano_pedal_engraver, note_column);
+ADD_TRANSLATOR (Piano_pedal_engraver,
+ /* doc */ "Engrave piano pedal symbols and brackets.",
+ /* create */ "SostenutoPedal SustainPedal UnaCordaPedal SostenutoPedalLineSpanner SustainPedalLineSpanner UnaCordaPedalLineSpanner",
+ /* accept */ "pedal-event",
+ /* read */ "currentCommandColumn "
+ "pedalSostenutoStrings "
+ "pedalSustainStrings "
+ "pedalUnaCordaStrings "
+ "pedalSostenutoStyle "
+ "pedalSustainStyle "
+ "pedalUnaCordaStyle",
+ /* write */ "");