]> git.donarmstrong.com Git - lilypond.git/blobdiff - lily/piano-pedal-engraver.cc
($(outdir)/%.pdf): add DVIPS_FLAGS. This will
[lilypond.git] / lily / piano-pedal-engraver.cc
index 6d0327ddc3f6d906773808c2762aafa03f533b13..77283f3bce6442ec8bcaa617e1b5cd8b50cd12fe 100644 (file)
   
   source file of the GNU LilyPond music typesetter
   
 (c) 2000 Jan Nieuwenhuizen <janneke@gnu.org>
(c) 2000--2004 Jan Nieuwenhuizen <janneke@gnu.org>
   
- */
+  Chris Jackson <chris@fluffhouse.org.uk> - extended to support
+  bracketed pedals.
+*/
 
 #include "engraver.hh"
-#include "musical-request.hh"
-#include "score-element.hh"
+#include "event.hh"
+#include "grob.hh"
 #include "item.hh"
-#include "lookup.hh"
 #include "lily-guile.hh"
-#include "note-head.hh"
-#include "stem.hh"
 #include "side-position-interface.hh"
 #include "staff-symbol-referencer.hh"
-
+#include "item.hh"
+#include "axis-group-interface.hh"
+#include "translator-group.hh"
+#include "directional-element-interface.hh"
+#include "note-column.hh"
+#include "warn.hh"
 
 /*
-  TODO:
-    sostenuto
-    una-chorda / tre-corde
- */
+  Urgh. This engraver is too complex. rewrite. --hwn
 
-
-/*
-  Urg.
-  This is almost text
-  Problem is:
-    * we have no kerning
-    * symbols are at wrong place in font
 */
 
-class Sustain_pedal : public Item
+struct Pedal_info
 {
-public:
-  VIRTUAL_COPY_CONS (Score_element);
+  char const * name_;
 
-protected:
-  virtual Molecule do_brew_molecule () const;
-  virtual void after_line_breaking ();
-};
+  /*
+    Event for currently running pedal.
+  */
+  Music* current_bracket_ev_;
 
-void
-Sustain_pedal::after_line_breaking ()
-{
-  Side_position_interface i (this);
-  Direction d =  i.get_direction ();
-  i.set_direction (d);
-}
-
-Molecule
-Sustain_pedal::do_brew_molecule () const
-{
-  Molecule mol;
-  SCM glyph = get_elt_property ("glyph");
-  if (glyph == SCM_UNDEFINED)
-    return mol;
-  String text = ly_scm2string (glyph);
-
-  for (int i = 0; i < text.length_i (); i++)
-    {
-      // leuke koor dump door tiepo, snapnie helemaal:
-      //String idx = ("pedal-") + text[i];
-      // urg, Byte* ??
-      // braak: waarom vindt String het zo moeilijk om
-      // String + char te doen?
-      //String idx = "pedal-" + String (&text.byte_C ()[i], 1);
-      String idx = String ("pedal-") + String (&text.byte_C ()[i], 1);
-      Molecule m = lookup_l ()->afm_find (idx);
-      if (m.empty_b ())
-       continue;
-      Real kern = 0;
-      if (i)
-       {
-         SCM s = scm_eval (gh_list (ly_symbol2scm ("pedal-kerning"),
-                                    ly_str02scm (String (&text.byte_C ()[i - 1], 1).ch_C ()),
-                                    ly_str02scm (String (&text.byte_C ()[i], 1).ch_C ()),
-                                    SCM_UNDEFINED));
-         if (gh_number_p (s))
-           {
-             Staff_symbol_referencer_interface st (this);
-             Real staff_space = st.staff_space ();
-             kern = gh_scm2double (s) * staff_space;
-           }
-       }
-      mol.add_at_edge (X_AXIS, RIGHT, m, kern);
-    }
+  /*
+    Event for currently starting pedal, (necessary?
     
-  return mol;
-}
+    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_;
+};
+
 
-/**
-   engrave Piano pedals
- */
 class Piano_pedal_engraver : public Engraver
 {
 public:
-  VIRTUAL_COPY_CONS (Translator);
-  Piano_pedal_engraver ();
-
+  TRANSLATOR_DECLARATIONS (Piano_pedal_engraver);
+  ~Piano_pedal_engraver ();
 protected:
-  virtual bool do_try_music (Music*);
-  virtual void do_process_music ();
-  virtual void do_pre_move_processing ();
-  virtual void do_post_move_processing ();
-  virtual void acknowledge_element (Score_element_info);
+  virtual void initialize ();
+  virtual void finalize ();
+  virtual bool try_music (Music*);
+  virtual void stop_translation_timestep ();
+  virtual void acknowledge_grob (Grob_info);
+  virtual void process_music ();
 
 private:
-  Span_req* sustain_start_req_l_;
-  Drul_array<Span_req*> sustain_req_l_drul_;
-  Sustain_pedal* sustain_p_;
+
+  Pedal_info *info_list_;
+
+  /*
+    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 create_text_grobs (Pedal_info *p, bool);
+  void create_bracket_grobs (Pedal_info *p, bool);
+  void typeset_all ();
 };
 
-ADD_THIS_TRANSLATOR(Piano_pedal_engraver);
 
 Piano_pedal_engraver::Piano_pedal_engraver ()
 {
-  sustain_p_ = 0;
-  sustain_req_l_drul_[START] = 0;
-  sustain_req_l_drul_[STOP] = 0;
-  sustain_start_req_l_ = 0;
+  info_list_ = 0;
+}
+
+void
+Piano_pedal_engraver::initialize ()
+{
+  previous_.clear ();
+
+  char * names [] = { "Sostenuto", "Sustain", "UnaCorda", 0  };
+
+  info_list_ = new Pedal_info[sizeof (names)/ sizeof (const char*)]; 
+  Pedal_info *p = info_list_;
+
+  char **np = names ;
+  do
+    {
+      p->name_ = *np;
+      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 ++));
+}
+
+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_element (Score_element_info i)
+Piano_pedal_engraver::acknowledge_grob (Grob_info info)
 {
-  if (sustain_p_)
+  for (Pedal_info*p = info_list_; p && p->name_; p ++)
     {
-      if (Note_head* n = dynamic_cast<Note_head*> (i.elem_l_))
-       {
-         Side_position_interface st (sustain_p_);
-         st.add_support (n);
-         if (st.get_axis( ) == X_AXIS
-             && !sustain_p_->parent_l (Y_AXIS))
-           sustain_p_->set_parent (n, Y_AXIS);
-       }
-      if (Stem* s = dynamic_cast<Stem*> (i.elem_l_))
+      if (Note_column::has_interface (info.grob_))
        {
-         Side_position_interface st (sustain_p_);
-         st.add_support (s);
+         if (p->line_spanner_)
+           {
+             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::do_try_music (Music *m)
+Piano_pedal_engraver::try_music (Music *m)
 {
 if (Span_req * s = dynamic_cast<Span_req*>(m))
if  (m->is_mus_type ("pedal-event"))
     {
-      if (s->span_type_str_ == "sustain")
+      for (Pedal_info*p = info_list_; p->name_; p ++)
        {
-         sustain_req_l_drul_[s->span_dir_] = s;
-         return true;
+         String nm = p->name_ + String ("Event");
+         if (gh_equal_p (m->get_mus_property ("name") ,
+                         gh_symbol2scm (nm.to_str0())))
+           {
+             Direction d = to_dir (m->get_mus_property ("span-direction"));
+             p->event_drul_[d] = m;
+             return true;
+           }
        }
     }
   return false;
 }
 
 void
-Piano_pedal_engraver::do_process_music ()
+Piano_pedal_engraver::process_music ()
 {
-  SCM s = SCM_UNDEFINED;
-  if (sustain_req_l_drul_[STOP] && sustain_req_l_drul_[START])
+  for (Pedal_info*p = info_list_; p && p->name_; p ++)
     {
-      if (!sustain_start_req_l_)
+      if (p->event_drul_[STOP] || p->event_drul_[START])
        {
-         sustain_req_l_drul_[STOP]->warning (_ ("can't find start of piano_pedal"));
+         if (!p->line_spanner_)
+           {
+             String name  = String (p->name_) + "PedalLineSpanner";
+             p->line_spanner_ = make_spanner (name.to_str0 ());
+
+             Music * rq = (p->event_drul_[START]  ?  p->event_drul_[START]  :  p->event_drul_[STOP]);
+             announce_grob (p->line_spanner_, rq->self_scm ());
+           }
+      
+         /* 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");
+         if (style == ly_symbol2scm ("text") ||
+             mixed)    
+           {
+             if (! p->item_)
+               create_text_grobs (p, mixed);
+           }
+         if (style == ly_symbol2scm ("bracket") ||
+             mixed)
+          {
+            create_bracket_grobs (p, mixed);
+          }
        }
+    }
+}
+
+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 = _ ("Need 3 strings for piano pedals. No pedal made. ");
+      if (m)
+       m->origin()->warning (msg);
       else
+       warning (msg);
+      
+      return ;
+    }
+  
+  if (p->event_drul_[STOP] && p->event_drul_[START]) 
+    {
+      if (!mixed)
        {
-         s = get_property ("stopStartSustain");
-         if (!gh_string_p (s))
-           s = ly_str02scm ("*Ped.");
+         if (!p->start_ev_)
+           {
+             p->event_drul_[STOP]->origin ()->warning (_f ("can't find start of piano pedal: `%s'",  p->name_));
+           }
+         else
+           {
+             s = ly_cadr (strings);
+           }
+         p->start_ev_ = p->event_drul_[START];
        }
-      sustain_start_req_l_ = sustain_req_l_drul_[START];
     }
-  else if (sustain_req_l_drul_[STOP])
-    {
-      if (!sustain_start_req_l_)
+  else if (p->event_drul_[STOP])
+    { 
+      if (!mixed)
        {
-         sustain_req_l_drul_[STOP]->warning (_ ("can't find start of piano_pedal"));
+         if (!p->start_ev_)
+           {
+             p->event_drul_[STOP]->origin ()->warning (_f ("can't find start of piano pedal: `%s'", p->name_));
+           }
+         else
+           {
+             s = ly_caddr (strings);
+             if (previous_.size ())
+               previous_.pop();
+           }
+         p->start_ev_ = 0;
        }
-      else
+    }
+  else if (p->event_drul_[START])
+    {
+      p->start_ev_ = p->event_drul_[START];
+      s = ly_car (strings);
+      if (!mixed)
        {
-         s = get_property ("stopSustain");
-         if (!gh_string_p (s))
-           s = ly_str02scm ("*");
+         /*
+           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_);
        }
-      sustain_start_req_l_ = 0;
     }
-  else if (sustain_req_l_drul_[START])
+      
+  if (gh_string_p (s))
     {
-      sustain_start_req_l_ = sustain_req_l_drul_[START];
-      s = get_property ("startSustain");
-      if (!gh_string_p (s))
-       s = ly_str02scm ("Ped.");
+      String propname = String (p->name_) + "Pedal";
+
+      p->item_ = make_item (propname.to_str0 ());
+      p->item_->set_grob_property ("text", s);
+      Axis_group_interface::add_element (p->line_spanner_, p->item_);
+         
+      announce_grob (p->item_,
+                    (p->event_drul_[START]
+                     ? p->event_drul_[START]
+                     : p->event_drul_[STOP])->self_scm ());
     }
-  if (s != SCM_UNDEFINED)
+
+  if (!mixed)
     {
-      sustain_p_ = new Sustain_pedal;
-      sustain_p_->set_elt_property ("glyph", s);
+      p->event_drul_[START] = 0;
+      p->event_drul_[STOP] = 0;
+    }
+}
+
+void
+Piano_pedal_engraver::create_bracket_grobs (Pedal_info *p, bool mixed)
+{
+  if (!p->bracket_ && p->event_drul_[STOP])
+    {
+      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;
+    }
+
+  if (p->event_drul_[STOP])
+    {
+      if (!p->event_drul_[START])
+       {
+         if (previous_.size())
+           previous_.pop();
+       }
+      
+      assert (!p->finished_bracket_); 
+
+      Grob *cmc = unsmob_grob (get_property ("currentMusicalColumn"));
 
+      if (!p->bracket_->get_bound (RIGHT))
+       p->bracket_->set_bound (RIGHT, cmc);
 
-      Side_position_interface si (sustain_p_);
-      si.set_axis (Y_AXIS);
+      /*
+       Set properties so that the stencil-creating function will
+       know whether the right edge should be flared ___/
+       */
 
-      /* Hmm,
-        If set to empty, it can't be centred
-        Howto centre non-fat text?
-        sustain_p_->set_empty (X_AXIS);
+      if (!p->event_drul_[START])
+       {
+         SCM flare = p->bracket_->get_grob_property ("bracket-flare");
+         p->bracket_->set_grob_property ("bracket-flare", scm_cons (gh_car (flare),
+                                                                    gh_double2scm (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");
+
+      /*
+       Set properties so that the stencil-creating function will
+       know whether the left edge should be flared \___
       */
-      sustain_p_->set_elt_property ("self-alignment-X", gh_int2scm (0));
-      sustain_p_->add_offset_callback (Side_position_interface::aligned_on_self, X_AXIS);
-      sustain_p_->add_offset_callback (Side_position_interface::centered_on_parent, X_AXIS);
-      announce_element (Score_element_info (sustain_p_,
-                                           sustain_req_l_drul_[START]
-                                           ? sustain_req_l_drul_[START]
-                                           : sustain_req_l_drul_[STOP]));
+
+      if (!p->finished_bracket_)
+       {
+         SCM flare = p->bracket_->get_grob_property ("bracket-flare");
+         p->bracket_->set_grob_property ("bracket-flare", scm_cons (gh_double2scm (0),gh_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)
+       {
+         /*
+           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->item_)
+           p->bracket_->set_grob_property ("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_);             
+      announce_grob (p->bracket_, p->event_drul_[START]->self_scm ());
+
+      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_->live())
+       p->line_spanner_ = 0;
+      
+      if (p->line_spanner_)
+       {
+         p->finished_line_spanner_ = p->line_spanner_;
+         typeset_all ();
+       }
+      if (p->bracket_
+         && !p->bracket_->live())
+       p->bracket_ = 0;
+      
+      if (p->bracket_)
+       {
+         p->current_bracket_ev_->origin ()->warning (_ ("unterminated pedal bracket"));
+         p->bracket_->suicide ();
+         p->bracket_ = 0;
+       }
     }
 }
 
+  
 void
-Piano_pedal_engraver::do_pre_move_processing ()
+Piano_pedal_engraver::stop_translation_timestep ()
 {
-  if (sustain_p_)
+  for (Pedal_info*p = info_list_; p && p->name_; p ++)
+    {
+      if (!p->bracket_)
+       {
+         p->finished_line_spanner_ = p->line_spanner_;
+         p->line_spanner_ = 0;
+       }
+    }
+  
+  typeset_all ();
+
+  for (Pedal_info*p = info_list_; p->name_; p ++)
     {
-      side_position (sustain_p_).add_staff_support ();
-      typeset_element (sustain_p_);
+      p->event_drul_[STOP] = 0;
+      p->event_drul_[START] = 0;
     }
-  sustain_p_ = 0;
 }
 
+
 void
-Piano_pedal_engraver::do_post_move_processing ()
+Piano_pedal_engraver::typeset_all ()
 {
-  sustain_req_l_drul_[STOP] = 0;
-  sustain_req_l_drul_[START] = 0;
+  Item * sustain = 0;
+  for (Pedal_info*p = info_list_; p->name_; p ++)
+    {
+      /*
+       Handle suicide. 
+       */
+      if (p->finished_line_spanner_
+         && !p->finished_line_spanner_->live ())
+       p->finished_line_spanner_ = 0;
+      if (p->finished_bracket_
+         && !p->finished_bracket_->live())
+       p->finished_bracket_ = 0;
+
+
+      if (p->name_ == String ("Sustain"))
+       sustain = p->item_;
+
+      if (p->item_)
+       {
+         /*
+           Hmm.
+         */
+         if (p->name_ != String ("Sustain") && sustain)
+           {
+             Side_position_interface::add_support (p->item_,sustain);
+           }
+         typeset_grob (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")));
+           }
+
+         typeset_grob (p->finished_bracket_);
+         
+         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);    
+           }
+         typeset_grob (p->finished_line_spanner_);
+         p->finished_line_spanner_ = 0;
+       }
+    }
 }
+
+ENTER_DESCRIPTION (Piano_pedal_engraver,
+/* descr */       "Engrave piano pedal symbols and brackets.",
+/* creats*/       "SostenutoPedal SustainPedal UnaCordaPedal SostenutoPedalLineSpanner SustainPedalLineSpanner UnaCordaPedalLineSpanner",
+/* accepts */     "pedal-event",
+/* acks  */       "note-column-interface",
+/* reads */       "pedalSostenutoStrings pedalSustainStrings pedalUnaCordaStrings pedalSostenutoStyle pedalSustainStyle pedalUnaCordaStyle",
+/* write */       "");