]> git.donarmstrong.com Git - lilypond.git/blobdiff - lily/dynamic-engraver.cc
lilypond-manuals.css: edit color scheme and some spacing
[lilypond.git] / lily / dynamic-engraver.cc
index f51dfb20fc32a6ed7deac3b356f7e0b29cca179c..686fbc5a6869f4095ccc091c58d748e04a2b8a3d 100644 (file)
 /*
-  dynamic-engraver.cc -- implement Dynamic_engraver
+  This file is part of LilyPond, the GNU music typesetter.
 
-  source file of the GNU LilyPond music typesetter
+  Copyright (C) 2008--2015 Han-Wen Nienhuys <hanwen@lilypond.org>
 
-  (c)  1997--2002 Han-Wen Nienhuys <hanwen@cs.uu.nl>
-*/
-#include "debug.hh"
-#include "dimensions.hh"
-#include "hairpin.hh"
-#include "musical-request.hh"
-#include "paper-column.hh"
-#include "note-column.hh"
-#include "item.hh"
-#include "side-position-interface.hh"
-#include "engraver.hh"
-#include "group-interface.hh"
-#include "directional-element-interface.hh"
-#include "translator-group.hh"
-#include "axis-group-interface.hh"
-#include "script.hh"
-
-/*
-  TODO:
+  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.
 
-  * direction of text-dynamic-request if not equal to direction of
-  line-spanner
+  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.
 
-  - TODO: this engraver is too complicated. We should split it into
-  the handling of the basic grobs and the  linespanner
+  You should have received a copy of the GNU General Public License
+  along with LilyPond.  If not, see <http://www.gnu.org/licenses/>.
+*/
 
-  - TODO: the line-spanner is not killed after the (de)crescs are
-  finished.
+#include "engraver.hh"
+#include "hairpin.hh"
+#include "international.hh"
+#include "item.hh"
+#include "note-column.hh"
+#include "pointer-group-interface.hh"
+#include "self-alignment-interface.hh"
+#include "spanner.hh"
+#include "stream-event.hh"
+#include "text-interface.hh"
 
-*/
+#include "translator.icc"
 
-/**
-   print text & hairpin dynamics.
- */
 class Dynamic_engraver : public Engraver
 {
-  Item * script_p_;
-  Spanner * finished_cresc_p_;
-  Spanner * cresc_p_;
-
-  Text_script_req* script_req_l_;
-  
-  Span_req * current_cresc_req_;
-  Drul_array<Span_req*> accepted_spanreqs_drul_;
-
-  Spanner* line_spanner_;
-  Spanner* finished_line_spanner_;
+  TRANSLATOR_DECLARATIONS (Dynamic_engraver);
+  void acknowledge_note_column (Grob_info);
+  void listen_absolute_dynamic (Stream_event *);
+  void listen_span_dynamic (Stream_event *);
+  void listen_break_span (Stream_event *);
 
-  Link_array<Note_column> pending_column_arr_;
-  Link_array<Grob> pending_element_arr_;
-  
-  void typeset_all ();
-
-TRANSLATOR_DECLARATIONS(Dynamic_engraver );
-  
 protected:
-  virtual void finalize ();
-  virtual void acknowledge_grob (Grob_info);
-  virtual bool try_music (Music *req_l);
+  virtual void process_music ();
   virtual void stop_translation_timestep ();
-  virtual void process_music ();  
-  virtual void start_translation_timestep ();
-};
+  virtual void finalize ();
 
+private:
+  SCM get_property_setting (Stream_event *evt, char const *evprop,
+                            char const *ctxprop);
+  string get_spanner_type (Stream_event *ev);
 
+  Drul_array<Stream_event *> accepted_spanevents_drul_;
+  Spanner *current_spanner_;
+  Spanner *finished_spanner_;
 
+  Item *script_;
+  Stream_event *script_event_;
+  Stream_event *current_span_event_;
+  bool end_new_spanner_;
+};
 
-Dynamic_engraver::Dynamic_engraver ()
+Dynamic_engraver::Dynamic_engraver (Context *c)
+  : Engraver (c)
 {
-  script_p_ = 0;
-  finished_cresc_p_ = 0;
-  line_spanner_ = 0;
-  finished_line_spanner_ = 0;
-  current_cresc_req_ = 0;
-  cresc_p_ =0;
-
-  script_req_l_ = 0;
-  accepted_spanreqs_drul_[START] = 0;
-  accepted_spanreqs_drul_[STOP] = 0;
+  script_event_ = 0;
+  current_span_event_ = 0;
+  script_ = 0;
+  finished_spanner_ = 0;
+  current_spanner_ = 0;
+  accepted_spanevents_drul_.set (0, 0);
+  end_new_spanner_ = false;
 }
 
 void
-Dynamic_engraver::start_translation_timestep ()
+Dynamic_engraver::listen_absolute_dynamic (Stream_event *ev)
 {
-  script_req_l_ = 0;
-  accepted_spanreqs_drul_[START] = 0;
-  accepted_spanreqs_drul_[STOP] = 0;
+  ASSIGN_EVENT_ONCE (script_event_, ev);
 }
 
-bool
-Dynamic_engraver::try_music (Music * m)
+void
+Dynamic_engraver::listen_span_dynamic (Stream_event *ev)
 {
-  if (dynamic_cast <Text_script_req*> (m)
-      && m->get_mus_property ("text-type") == ly_symbol2scm ("dynamic"))
-    {
-      script_req_l_ = dynamic_cast<Text_script_req*> (m);
-      return true;
-    }
-  else if (Span_req* s =  dynamic_cast <Span_req*> (m))
+  Direction d = to_dir (ev->get_property ("span-direction"));
+
+  ASSIGN_EVENT_ONCE (accepted_spanevents_drul_[d], ev);
+}
+
+void
+Dynamic_engraver::listen_break_span (Stream_event *event)
+{
+  if (event->in_event_class ("break-dynamic-span-event"))
     {
-      String t = ly_scm2string (s->get_mus_property ("span-type"));
-      if (t== "abort")
-       {
-         accepted_spanreqs_drul_[LEFT] = 0;
-         accepted_spanreqs_drul_[RIGHT] = 0;
-         /*
-           Let's not kill the line spanner, since that would fuck up
-           earlier, not-to-be-terminated stuff.
-
-           It will disappear by itself when stop_translation_timestep
- () finds that there is nothing to support anymore.  */
-         
-         if (cresc_p_)
-           cresc_p_->suicide ();
-         cresc_p_ = 0;
-       }
-      else if (t == "crescendo"
-          || t == "decrescendo")
-       {
-         accepted_spanreqs_drul_[s->get_span_dir ()] = s;
-         return true;
-       }
+      // Case 1: Already have a start dynamic event -> break applies to new
+      //         spanner (created later) -> set a flag
+      // Case 2: no new spanner, but spanner already active -> break it now
+      if (accepted_spanevents_drul_[START])
+        end_new_spanner_ = true;
+      else if (current_spanner_)
+        current_spanner_->set_property ("spanner-broken", SCM_BOOL_T);
     }
-  return false;
+}
+
+SCM
+Dynamic_engraver::get_property_setting (Stream_event *evt,
+                                        char const *evprop,
+                                        char const *ctxprop)
+{
+  SCM spanner_type = evt->get_property (evprop);
+  if (scm_is_null (spanner_type))
+    spanner_type = get_property (ctxprop);
+  return spanner_type;
 }
 
 void
 Dynamic_engraver::process_music ()
 {
-  if (accepted_spanreqs_drul_[START] || accepted_spanreqs_drul_[STOP] || script_req_l_)
-    {
-      if (!line_spanner_)
-       {
-         line_spanner_ = new Spanner (get_property ("DynamicLineSpanner"));
-
-         Music * rq = accepted_spanreqs_drul_[START];
-         if (script_req_l_)
-           rq =  script_req_l_ ;
-         announce_grob(line_spanner_, rq ? rq->self_scm(): SCM_EOL);
-       }
-    }
-  
-  /*
-    During a (de)crescendo, pending request will not be cleared,
-    and a line-spanner will always be created, as \< \! are already
-    two requests.
-
-    Note: line-spanner must always have at least same duration
-    as (de)crecsendo, b.o. line-breaking.
-  */
-
-  
-
-  /*
-    maybe we should leave dynamic texts to the text-engraver and
-    simply acknowledge them?
-  */
-  if (script_req_l_)
+  if (current_spanner_
+      && (accepted_spanevents_drul_[STOP]
+          || script_event_
+          || accepted_spanevents_drul_[START]))
     {
-      script_p_ = new Item (get_property ("DynamicText"));
-      script_p_->set_grob_property ("text",
-                                  script_req_l_->get_mus_property ("text"));
-      
-      if (Direction d = script_req_l_->get_direction ())
-       Directional_element_interface::set (line_spanner_, d);
+      Stream_event *ender = accepted_spanevents_drul_[STOP];
+      if (!ender)
+        ender = script_event_;
 
-      Axis_group_interface::add_element (line_spanner_, script_p_);
+      if (!ender)
+        ender = accepted_spanevents_drul_[START];
 
-      announce_grob(script_p_, script_req_l_->self_scm());
+      finished_spanner_ = current_spanner_;
+      announce_end_grob (finished_spanner_, ender->self_scm ());
+      current_spanner_ = 0;
+      current_span_event_ = 0;
     }
 
-  if (accepted_spanreqs_drul_[STOP])
+  if (accepted_spanevents_drul_[START])
     {
-      /*
-       finish side position alignment if the (de)cresc ends here, and
-       there are no new dynamics.
-       */
-      if (!cresc_p_)
-       {
-         accepted_spanreqs_drul_[STOP]->origin ()->warning
- (_ ("can't find start of (de)crescendo"));
-         accepted_spanreqs_drul_[STOP] = 0;
-       }
+      current_span_event_ = accepted_spanevents_drul_[START];
+
+      string start_type = get_spanner_type (current_span_event_);
+      SCM cresc_type = get_property_setting (current_span_event_, "span-type",
+                                             (start_type + "Spanner").c_str ());
+
+      if (scm_is_eq (cresc_type, ly_symbol2scm ("text")))
+        {
+          current_spanner_
+            = make_spanner ("DynamicTextSpanner",
+                            accepted_spanevents_drul_[START]->self_scm ());
+
+          SCM text = get_property_setting (current_span_event_, "span-text",
+                                           (start_type + "Text").c_str ());
+          if (Text_interface::is_markup (text))
+            current_spanner_->set_property ("text", text);
+          /*
+            If the line of a text spanner is hidden, end the alignment spanner
+            early: this allows dynamics to be spaced individually instead of
+            being linked together.
+          */
+          if (scm_is_eq (current_spanner_->get_property ("style"),
+                         ly_symbol2scm ("none")))
+            current_spanner_->set_property ("spanner-broken", SCM_BOOL_T);
+        }
       else
-       {
-         assert (!finished_cresc_p_ && cresc_p_);
-
-         cresc_p_->set_bound (RIGHT, script_p_
-                              ? script_p_
-                              : unsmob_grob (get_property ("currentMusicalColumn")));
-         add_bound_item (line_spanner_, cresc_p_->get_bound (RIGHT));
-         
-
-         finished_cresc_p_ = cresc_p_;
-         cresc_p_ = 0;
-         current_cresc_req_ = 0;
-       }
+        {
+          if (!scm_is_eq (cresc_type, ly_symbol2scm ("hairpin")))
+            {
+              string as_string = ly_scm_write_string (cresc_type);
+              current_span_event_
+              ->origin ()->warning (_f ("unknown crescendo style: %s\ndefaulting to hairpin.", as_string.c_str ()));
+            }
+          current_spanner_ = make_spanner ("Hairpin",
+                                           current_span_event_->self_scm ());
+        }
+      // if we have a break-dynamic-span event right after the start dynamic, break the new spanner immediately
+      if (end_new_spanner_)
+        {
+          current_spanner_->set_property ("spanner-broken", SCM_BOOL_T);
+          end_new_spanner_ = false;
+        }
+      if (finished_spanner_)
+        {
+          if (has_interface<Hairpin> (finished_spanner_))
+            Pointer_group_interface::add_grob (finished_spanner_,
+                                               ly_symbol2scm ("adjacent-spanners"),
+                                               current_spanner_);
+          if (has_interface<Hairpin> (current_spanner_))
+            Pointer_group_interface::add_grob (current_spanner_,
+                                               ly_symbol2scm ("adjacent-spanners"),
+                                               finished_spanner_);
+        }
     }
-  
-  if (accepted_spanreqs_drul_[START])
+
+  if (script_event_)
     {
-      if (current_cresc_req_)
-       {
-         String msg = current_cresc_req_->get_span_dir () == 1
-           ? _ ("already have a crescendo")
-           : _ ("already have a decrescendo");
-      
-         accepted_spanreqs_drul_[START]->origin ()->warning (msg);
-         current_cresc_req_->origin ()->warning (_("Cresc started here"));
-       }
-      else
-       {
-         current_cresc_req_ = accepted_spanreqs_drul_[START];
-
-         /*
-           TODO: Use symbols.
-         */
-
-         String start_type = ly_scm2string (accepted_spanreqs_drul_[START]->get_mus_property ("span-type"));
-
-         /*
-           ugh. Use push/pop?
-         */
-         SCM s = get_property ((start_type + "Spanner").ch_C ());
-         if (!gh_symbol_p (s) || s == ly_symbol2scm ("hairpin"))
-           {
-             cresc_p_  = new Spanner (get_property ("Hairpin"));
-             cresc_p_->set_grob_property ("grow-direction",
-                                          gh_int2scm ((start_type == "crescendo")
-                                                      ? BIGGER : SMALLER));
-             
-           }
-         /*
-           This is a convenient (and legacy) interface to TextSpanners
-           for use in (de)crescendi.
-           Hmm.
-         */
-         else
-           {
-             cresc_p_  = new Spanner (get_property ("TextSpanner"));
-             cresc_p_->set_grob_property ("type", s);
-             daddy_trans_l_->set_property ((start_type
-                                           + "Spanner").ch_C(), SCM_UNDEFINED);
-             s = get_property ((start_type + "Text").ch_C ());
-             /*
-               FIXME: use markup_p () to check type.
-             */
-             if (gh_string_p (s) || gh_pair_p (s))
-               {
-                 cresc_p_->set_grob_property ("edge-text",
-                                              gh_cons (s, ly_str02scm ("")));
-                 daddy_trans_l_->set_property ((start_type + "Text").ch_C(),
-                                               SCM_EOL);
-               }
-           }
-
-         cresc_p_->set_bound (LEFT, script_p_
-                              ? script_p_
-                              : unsmob_grob (get_property ("currentMusicalColumn")));
-
-         Axis_group_interface::add_element (line_spanner_, cresc_p_);
-
-         add_bound_item (line_spanner_, cresc_p_->get_bound (LEFT));
-         
-         announce_grob(cresc_p_, accepted_spanreqs_drul_[START]->self_scm());
-       }
+      script_ = make_item ("DynamicText", script_event_->self_scm ());
+      script_->set_property ("text",
+                             script_event_->get_property ("text"));
+
+      if (finished_spanner_)
+        finished_spanner_->set_bound (RIGHT, script_);
+      if (current_spanner_)
+        current_spanner_->set_bound (LEFT, script_);
     }
 }
 
 void
 Dynamic_engraver::stop_translation_timestep ()
 {
-  typeset_all ();
-  if (!current_cresc_req_)
-    {
-      finished_line_spanner_ = line_spanner_;
-      line_spanner_ =0;
-      typeset_all ();
-    }
+  if (finished_spanner_ && !finished_spanner_->get_bound (RIGHT))
+    finished_spanner_
+    ->set_bound (RIGHT,
+                 unsmob<Grob> (get_property ("currentMusicalColumn")));
+
+  if (current_spanner_ && !current_spanner_->get_bound (LEFT))
+    current_spanner_
+    ->set_bound (LEFT,
+                 unsmob<Grob> (get_property ("currentMusicalColumn")));
+  script_ = 0;
+  script_event_ = 0;
+  accepted_spanevents_drul_.set (0, 0);
+  finished_spanner_ = 0;
+  end_new_spanner_ = false;
 }
 
 void
 Dynamic_engraver::finalize ()
 {
-  typeset_all ();
-  
-  if (line_spanner_
-      && !line_spanner_->live())
-    line_spanner_ = 0;
-  if (line_spanner_)
+  if (current_spanner_
+      && !current_spanner_->is_live ())
+    current_spanner_ = 0;
+  if (current_spanner_)
     {
-      finished_line_spanner_ = line_spanner_;
-      typeset_all ();
-    }
-
-  if (cresc_p_
-      && !cresc_p_->live())
-    cresc_p_ = 0;
-  if (cresc_p_)
-    {
-      current_cresc_req_->origin ()->warning (_ ("unterminated (de)crescendo"));
-      cresc_p_->suicide ();
-      cresc_p_ = 0;
+      current_span_event_
+      ->origin ()->warning (_f ("unterminated %s",
+                                get_spanner_type (current_span_event_)
+                                .c_str ()));
+      current_spanner_->suicide ();
+      current_spanner_ = 0;
     }
 }
 
-void
-Dynamic_engraver::typeset_all ()
-{  
-  /*
-    remove suicided spanners,
-    ugh: we'll need this for every spanner, beam, slur
-    Hmm, how to do this, cleanly?
-    Maybe just check at typeset_grob ()?
-  */
-  if (finished_cresc_p_
-      && !finished_cresc_p_->live())
-    finished_cresc_p_ = 0;
-  if (finished_line_spanner_
-      && !finished_line_spanner_->live())
-    finished_line_spanner_ = 0;
-
-  if (finished_cresc_p_)
-    {
-      if (!finished_cresc_p_->get_bound (RIGHT))
-       {
-         finished_cresc_p_->set_bound (RIGHT, script_p_
-                                       ? script_p_
-                                       : unsmob_grob (get_property ("currentMusicalColumn")));
-
-         if (finished_line_spanner_)
-           add_bound_item (finished_line_spanner_,
-                           finished_cresc_p_->get_bound (RIGHT));
-       }
-      typeset_grob (finished_cresc_p_);
-      finished_cresc_p_ =0;
-    }
-  
-  if (script_p_)
-    {
-      typeset_grob (script_p_);
-      script_p_ = 0;
-    }
-  if (finished_line_spanner_)
-    {
-      /* To make sure that this works */
-      Side_position_interface::add_staff_support (finished_line_spanner_);
-      
-      /*
-       We used to have
-       
-            extend-spanner-over-elements (finished_line_spanner_);
-
-       but this is rather kludgy, since finished_line_spanner_
-       typically has a staff-symbol field set , extending it over the
-       entire staff.
+string
+Dynamic_engraver::get_spanner_type (Stream_event *ev)
+{
+  string type;
+  SCM start_sym = scm_car (ev->get_property ("class"));
 
-      */
+  if (scm_is_eq (start_sym, ly_symbol2scm ("decrescendo-event")))
+    type = "decrescendo";
+  else if (scm_is_eq (start_sym, ly_symbol2scm ("crescendo-event")))
+    type = "crescendo";
+  else
+    programming_error ("unknown dynamic spanner type");
 
-      Grob * l = finished_line_spanner_->get_bound (LEFT );
-      Grob * r = finished_line_spanner_->get_bound (RIGHT);      
-      if (!r && l)
-       finished_line_spanner_->set_bound (RIGHT, l);
-      else if (!l && r)
-       finished_line_spanner_->set_bound (LEFT, r);
-      else if (!r && !l)
-       {
-         /*
-           This is a isolated dynamic apparently, and does not even have
-           any interesting support item.
-          */
-         Grob * cc = unsmob_grob (get_property ("currentMusicalColumn"));
-         Item * ci = dynamic_cast<Item*>(cc);
-         finished_line_spanner_->set_bound (RIGHT, ci);
-         finished_line_spanner_->set_bound (LEFT, ci);   
-       }
-       
-      typeset_grob (finished_line_spanner_);
-      finished_line_spanner_ = 0;
-    }
+  return type;
 }
 
 void
-Dynamic_engraver::acknowledge_grob (Grob_info i)
+Dynamic_engraver::acknowledge_note_column (Grob_info info)
 {
-  if (!line_spanner_)
-    return ;
-  
-  if (Note_column::has_interface (i.grob_l_))
+  if (script_ && !script_->get_parent (X_AXIS))
     {
-      if (line_spanner_
-         /* Don't refill killed spanner */
-         && line_spanner_->live())
-       {
-         Side_position_interface::add_support (line_spanner_,i.grob_l_);
-         add_bound_item (line_spanner_,dynamic_cast<Item*> (i.grob_l_));
-       }
-
-      if (script_p_ && !script_p_->get_parent (X_AXIS))
-       {
-         script_p_->set_parent (i.grob_l_,  X_AXIS);
-       }
-      
+      extract_grob_set (info.grob (), "note-heads", heads);
+      /*
+        Spacing constraints may require dynamics to be attached to rests,
+        so check for a rest if this note column has no note heads.
+      */
+      Grob *x_parent = (heads.size ()
+                        ? info.grob ()
+                        : unsmob<Grob> (info.grob ()->get_object ("rest")));
+      if (x_parent)
+        script_->set_parent (x_parent, X_AXIS);
     }
-  else if (Script_interface::has_interface (i.grob_l_) && script_p_)
-    {
-      SCM p = i.grob_l_->get_grob_property ("script-priority");
 
-      if (gh_number_p (p)
-         && gh_scm2int (p) < gh_scm2int (script_p_->get_grob_property ("script-priority")))
-       {
-         Side_position_interface::add_support (line_spanner_, i.grob_l_);
+  if (current_spanner_ && !current_spanner_->get_bound (LEFT))
+    current_spanner_->set_bound (LEFT, info.grob ());
+  if (finished_spanner_ && !finished_spanner_->get_bound (RIGHT))
+    finished_spanner_->set_bound (RIGHT, info.grob ());
+}
 
-       }         
-    }
+void
+Dynamic_engraver::boot ()
+{
+  ADD_LISTENER (Dynamic_engraver, absolute_dynamic);
+  ADD_LISTENER (Dynamic_engraver, span_dynamic);
+  ADD_LISTENER (Dynamic_engraver, break_span);
+  ADD_ACKNOWLEDGER (Dynamic_engraver, note_column);
 }
-ENTER_DESCRIPTION(Dynamic_engraver,
-/* descr */       "
-This engraver creates hairpins, dynamic texts, and their vertical
-alignments.  The symbols are collected onto a DynamicLineSpanner grob
-which takes care of vertical positioning.  
-",
-                 
-/* creats*/       "DynamicLineSpanner DynamicText Hairpin TextSpanner",
-/* acks  */       "note-column-interface script-interface",
-/* reads */       "",
-/* write */       "");
+
+ADD_TRANSLATOR (Dynamic_engraver,
+                /* doc */
+                "Create hairpins, dynamic texts and dynamic text spanners.",
+
+                /* create */
+                "DynamicTextSpanner "
+                "DynamicText "
+                "Hairpin ",
+
+                /* read */
+                "crescendoSpanner "
+                "crescendoText "
+                "currentMusicalColumn "
+                "decrescendoSpanner "
+                "decrescendoText ",
+
+                /* write */
+                ""
+               );