]> git.donarmstrong.com Git - lilypond.git/blobdiff - lily/dynamic-engraver.cc
release: 1.3.74
[lilypond.git] / lily / dynamic-engraver.cc
index 38362df35118cc27fc447ea2428888174257187d..cfd5eae9a234c79b72bc0525074bda483c825112 100644 (file)
 #include "dimensions.hh"
 #include "crescendo.hh"
 #include "musical-request.hh"
-#include "lookup.hh"
-#include "paper-def.hh"
 #include "paper-column.hh"
-#include "staff-symbol.hh"
 #include "note-column.hh"
-#include "text-item.hh"
+#include "item.hh"
 #include "side-position-interface.hh"
 #include "engraver.hh"
-#include "stem.hh"
-#include "note-head.hh"
 #include "group-interface.hh"
 #include "directional-element-interface.hh"
-#include "staff-symbol-referencer.hh"
 #include "translator-group.hh"
+#include "axis-group-interface.hh"
 
-class Dynamic_line_spanner : public Spanner
-{
-public:
-  Dynamic_line_spanner ();
-  
-  void add_column (Note_column*);
-  Direction get_default_dir () const;
-protected:
-  virtual void do_post_processing ();
-};
-
-Dynamic_line_spanner::Dynamic_line_spanner ()
-{
-  set_elt_property ("transparent", SCM_BOOL_T);
-  side_position (this).set_axis (Y_AXIS);
-}
-
-void
-Dynamic_line_spanner::add_column (Note_column* n)
-{
-  if (!spanned_drul_[LEFT])
-    set_bounds (LEFT, n);
-  set_bounds (RIGHT, n);
 
-  add_dependency (n);
-}
-
-Direction
-Dynamic_line_spanner::get_default_dir () const
-{
-  return DOWN;
-}
-
-void
-Dynamic_line_spanner::do_post_processing ()
-{
-  Spanner::do_post_processing ();
-  Direction dir = directional_element (this).get ();
-  if (!dir)
-    dir = get_default_dir ();
-
-  /*
-    Hier is ook vast iets voor?
-   */
-  Staff_symbol_referencer_interface si (this);
-  Real above_staff = si.line_count () + 2;
-
-#if 0
-  // Aargh, nu snap ik waarom ik het niet snap
-  // zie Staff_symbol_referencer_interface::set_position 
-
-  if (si.position_f () * dir < above_staff)
-    si.set_position (above_staff * (int)dir);
+/*
+  TODO:
 
-  SCM s = get_elt_property ("padding");
-  if (gh_number_p (s))
-    {
-      si.set_position (si.position_f () + gh_scm2double (s) * (int) dir);
-    }
-#else
-  Real dy = 0;
-  Real pos = si.position_f () * dir;
-  if (pos * dir < above_staff)
-    dy = above_staff;
-
-  SCM s = get_elt_property ("padding");
-  if (gh_number_p (s))
-    dy += gh_scm2double (s);
-  
-  Real half_space = si.staff_space () / 2;
-  translate_axis (dy*half_space*dir, Y_AXIS);
-#endif
-  
-}
+  * direction of text-dynamic-request if not equal to direction of
+  line-spanner
+*/
 
 /**
    print text & hairpin dynamics.
  */
 class Dynamic_engraver : public Engraver
 {
-  Text_item * text_p_;
-  Crescendo * finished_cresc_p_;
-  Crescendo * cresc_p_;
+  Item * text_p_;
+  Spanner * finished_cresc_p_;
+  Spanner * cresc_p_;
 
   Text_script_req* text_req_l_;
-  Span_req * span_start_req_l_;
-  Drul_array<Span_req*> span_req_l_drul_;
+  
+  Span_req * current_cresc_req_;
+  Drul_array<Span_req*> accepted_spanreqs_drul_;
 
-  Dynamic_line_spanner* line_spanner_;
-  Moment last_request_mom_;
+  Spanner* line_spanner_;
+  Spanner* finished_line_spanner_;
+
+  Link_array<Note_column> pending_column_arr_;
+  Link_array<Score_element> pending_element_arr_;
   
-  void  typeset_all ();
+  void typeset_all ();
 
 public:
   VIRTUAL_COPY_CONS(Translator);
   Dynamic_engraver ();
   
 protected:
-  void announce_element (Score_element_info);
-  
   virtual void do_removal_processing ();
   virtual void acknowledge_element (Score_element_info);
   virtual bool do_try_music (Music *req_l);
-  virtual void do_process_requests ();
+  virtual void do_process_music ();
   virtual void do_pre_move_processing ();
   virtual void do_post_move_processing ();
 };
 
 ADD_THIS_TRANSLATOR (Dynamic_engraver);
 
-void
-Dynamic_engraver::announce_element (Score_element_info i)
-{
-  group (i.elem_l_, "interfaces").add_thing (ly_symbol2scm ("dynamic"));
-  
-  Engraver::announce_element (i);
-}
-
 
 Dynamic_engraver::Dynamic_engraver ()
 {
   text_p_ = 0;
   finished_cresc_p_ = 0;
   line_spanner_ = 0;
-  span_start_req_l_ = 0;
+  finished_line_spanner_ = 0;
+  current_cresc_req_ = 0;
   cresc_p_ =0;
 
   text_req_l_ = 0;
-  span_req_l_drul_[START] = 0;
-  span_req_l_drul_[STOP] = 0;
+  accepted_spanreqs_drul_[START] = 0;
+  accepted_spanreqs_drul_[STOP] = 0;
 }
 
 void
 Dynamic_engraver::do_post_move_processing ()
 {
   text_req_l_ = 0;
-  span_req_l_drul_[START] = 0;
-  span_req_l_drul_[STOP] = 0;
+  accepted_spanreqs_drul_[START] = 0;
+  accepted_spanreqs_drul_[STOP] = 0;
 }
 
 bool
@@ -180,7 +103,7 @@ Dynamic_engraver::do_try_music (Music * m)
       if ((s->span_type_str_ == "crescendo"
           || s->span_type_str_ == "decrescendo"))
        {
-         span_req_l_drul_[s->span_dir_] = s;
+         accepted_spanreqs_drul_[s->span_dir_] = s;
          return true;
        }
     }
@@ -188,108 +111,162 @@ Dynamic_engraver::do_try_music (Music * m)
 }
 
 void
-Dynamic_engraver::do_process_requests ()
+Dynamic_engraver::do_process_music ()
 {
-  if ((span_req_l_drul_[START] || text_req_l_) && !line_spanner_)
+  if (accepted_spanreqs_drul_[START] || accepted_spanreqs_drul_[STOP] || text_req_l_)
+    
     {
-      line_spanner_ = new Dynamic_line_spanner;
-      side_position (line_spanner_).set_axis (Y_AXIS);
-      announce_element (Score_element_info
-                       (line_spanner_,
-                        text_req_l_ ? text_req_l_ : span_req_l_drul_[START]));
+      if (!line_spanner_)
+       {
+         line_spanner_ = new Spanner (get_property ("basicDynamicLineSpannerProperties"));
 
+         Side_position::set_axis (line_spanner_, Y_AXIS);
+         Axis_group_interface::set_interface (line_spanner_);
+         Axis_group_interface::set_axes (line_spanner_, Y_AXIS, Y_AXIS);
+         announce_element (line_spanner_,
+                            text_req_l_ ? text_req_l_ : accepted_spanreqs_drul_[START]);
+
+       }
     }
-         
-  if (span_req_l_drul_[START] || text_req_l_)
-    last_request_mom_ = now_mom ();
-  
+
+  /*
+    TODO: should finish and create new spanner if vertical dyn-direction is changed.
+   */
+  else if (!accepted_spanreqs_drul_[START] && !text_req_l_)
+    {
+      finished_line_spanner_ = line_spanner_;
+      line_spanner_ = 0;
+    }
+
+       /*
+       todo: resurrect  dynamic{direction, padding,minimumspace}
+       */
+       /*
+       During a (de)crescendo, pending request will not be cleared,
+       and a line-spanner will always be created, as \< \! are already
+       two requests.
+
+       Maybe always creating a line-spanner for a (de)crescendo (see
+       below) is not a good idea:
+
+           a\< b\p \!c
+
+       the \p will be centred on the line-spanner, and thus clash
+       with the hairpin.  When axis-group code is in place, the \p
+       should move below the hairpin, which is probably better?
+
+       Urg, but line-spanner must always have at least same duration
+       as (de)crecsendo, b.o. line-breaking.
+       */
+
+
   if (text_req_l_)
     {
       String loud = text_req_l_->text_str_;
 
-      text_p_ = new Text_item;
-      text_p_->set_elt_property ("text",
-                                         ly_str02scm (loud.ch_C ()));
-      text_p_->set_elt_property ("style", gh_str02scm ("dynamic"));
-      text_p_->set_elt_property ("script-priority",
-                                         gh_int2scm (100));
-         
-      assert (line_spanner_);
-      text_p_->set_parent (line_spanner_, Y_AXIS);
-      announce_element (Score_element_info (text_p_, text_req_l_));
+      text_p_ = new Item (get_property ("basicDynamicTextProperties"));
+      text_p_->set_elt_property ("text", ly_str02scm (loud.ch_C ()));
+      if (Direction d=text_req_l_->get_direction ())
+       Directional_element_interface (line_spanner_).set (d);
+
+      Axis_group_interface::add_element (line_spanner_, text_p_);
+
+      text_p_->add_offset_callback (Side_position::aligned_on_self,
+                                   Y_AXIS);
+      announce_element (text_p_, text_req_l_);
     }
 
-  if (span_req_l_drul_[STOP])
+  if (accepted_spanreqs_drul_[STOP])
     {
       if (!cresc_p_)
        {
-         span_req_l_drul_[STOP]->warning
+         accepted_spanreqs_drul_[STOP]->origin ()->warning
            (_ ("can't find start of (de)crescendo"));
        }
       else
        {
          assert (!finished_cresc_p_);
-         cresc_p_->set_bounds(RIGHT, get_staff_info ().musical_pcol_l ());
-         cresc_p_->add_dependency (get_staff_info ().musical_pcol_l ());
+         cresc_p_->set_bound (RIGHT, unsmob_element (get_property ("currentMusicalColumn")));
          finished_cresc_p_ = cresc_p_;
          cresc_p_ = 0;
-         span_start_req_l_ = 0;
+         current_cresc_req_ = 0;
        }
     }
 
-  if (span_req_l_drul_[START])
+  if (accepted_spanreqs_drul_[START])
     {
-      if (span_start_req_l_)
+      if (current_cresc_req_)
        {
-         span_req_l_drul_[START]->warning
-           (span_start_req_l_->span_dir_ == 1
+         accepted_spanreqs_drul_[START]->origin ()->warning
+           (current_cresc_req_->span_dir_ == 1
             ?
             _ ("already have a crescendo")
             : _ ("already have a decrescendo"));
        }
       else
        {
-         span_start_req_l_ = span_req_l_drul_[START];
-         cresc_p_  = new Crescendo;
+         current_cresc_req_ = accepted_spanreqs_drul_[START];
+         cresc_p_  = new Spanner (get_property ("basicCrescendoProperties"));
+         Crescendo::set_interface (cresc_p_);
          cresc_p_->set_elt_property
            ("grow-direction",
-            gh_int2scm ((span_req_l_drul_[START]->span_type_str_ == "crescendo")
+            gh_int2scm ((accepted_spanreqs_drul_[START]->span_type_str_ == "crescendo")
                         ? BIGGER : SMALLER));
              
-         SCM s = get_property (span_req_l_drul_[START]->span_type_str_ + "Text");
+         SCM s = get_property ((accepted_spanreqs_drul_[START]->span_type_str_ + "Text").ch_C());
          if (gh_string_p (s))
            {
              cresc_p_->set_elt_property ("start-text", s);
-             daddy_trans_l_->set_property (span_req_l_drul_[START]->span_type_str_
+             daddy_trans_l_->set_property (accepted_spanreqs_drul_[START]->span_type_str_
                                            + "Text", SCM_UNDEFINED);
            }
 
-         s = get_property (span_req_l_drul_[START]->span_type_str_ + "Spanner");
+         s = get_property ((accepted_spanreqs_drul_[START]->span_type_str_ + "Spanner").ch_C());
+
+
+         /*
+           TODO: Use symbols.
+          */
          if (gh_string_p (s)) //&& ly_scm2string (s) != "hairpin")
            {
              cresc_p_->set_elt_property ("spanner", s);
-             daddy_trans_l_->set_property (span_req_l_drul_[START]->span_type_str_
+             daddy_trans_l_->set_property (accepted_spanreqs_drul_[START]->span_type_str_
                                            + "Spanner", SCM_UNDEFINED);
            }
 
-         cresc_p_->set_bounds(LEFT, get_staff_info ().musical_pcol_l ());
-         cresc_p_->set_bounds(RIGHT, get_staff_info ().musical_pcol_l ());
-         cresc_p_->add_dependency (get_staff_info ().musical_pcol_l ());
+         cresc_p_->set_bound (LEFT, unsmob_element (get_property ("currentMusicalColumn")));
 
-         // arrragh, brr, urg: we know how wide text is, no?
+
+         /* 
+             We know how wide the text is, if we can be sure that the
+             text already has relevant pointers into the paperdef,
+             and it has its font-size property set.
+
+             Since font-size may be set by a context higher up, we
+             can not be sure of the size.
+
+
+             We shouldn't try to do this stuff here, the Item should
+             do it when the score is finished.  We could maybe
+             set a callback to have the Item do the alignment if
+             it is not a special symbol, like Crescendo.
+         */
+
+         
          if (text_p_)
            {
              index_set_cell (cresc_p_->get_elt_property ("dynamic-drul"),
-                             LEFT, SCM_BOOL_T);
+                             LEFT, text_p_->self_scm ());
              if (finished_cresc_p_)
                index_set_cell (finished_cresc_p_->get_elt_property ("dynamic-drul"),
-                               RIGHT, SCM_BOOL_T);
+                               RIGHT, text_p_->self_scm ());
            }
 
-         assert (line_spanner_);
-         cresc_p_->set_parent (line_spanner_, Y_AXIS);
-         cresc_p_->add_dependency (line_spanner_);
-         announce_element (Score_element_info (cresc_p_, span_req_l_drul_[START]));
+         Axis_group_interface::add_element (line_spanner_, cresc_p_);
+         cresc_p_->set_elt_property ("self-alignment-Y", gh_int2scm (0));
+         cresc_p_->add_offset_callback
+           (Side_position::aligned_on_self, Y_AXIS);
+         announce_element (cresc_p_, accepted_spanreqs_drul_[START]);
        }
     }
 }
@@ -303,27 +280,26 @@ Dynamic_engraver::do_pre_move_processing ()
 void
 Dynamic_engraver::do_removal_processing ()
 {
+  typeset_all ();
+  
   if (cresc_p_)
     {
       typeset_element (cresc_p_ );
-      span_start_req_l_->warning (_ ("unterminated (de)crescendo"));
-      cresc_p_ =0;
+      finished_cresc_p_ = cresc_p_;
+      current_cresc_req_->origin ()->warning (_ ("unterminated (de)crescendo"));
     }
-  typeset_all ();
   if (line_spanner_)
     {
-      typeset_element (line_spanner_);
-      line_spanner_ = 0;
+      finished_line_spanner_ = line_spanner_;
     }
+  typeset_all ();
 }
 
-
 void
 Dynamic_engraver::typeset_all ()
 {  
   if (finished_cresc_p_)
     {
-      //finished_cresc_p_->set_bounds (RIGHT, get_staff_info ().musical_pcol_l ());
       typeset_element (finished_cresc_p_);
       finished_cresc_p_ =0;
     }
@@ -333,29 +309,35 @@ Dynamic_engraver::typeset_all ()
       typeset_element (text_p_);
       text_p_ = 0;
     }
-
-  /*
-    TODO: This should be optionised:
-      * break when group of dynamic requests ends
-      * break now 
-      * continue through piece
-   */
-  if (line_spanner_ && last_request_mom_ < now_mom ())
+  if (finished_line_spanner_)
     {
-      typeset_element (line_spanner_);
-      line_spanner_ = 0;
+      Side_position::add_staff_support (finished_line_spanner_);
+
+      if (!finished_line_spanner_->get_bound (LEFT))
+       {
+         Score_element * cmc
+           = unsmob_element (get_property ("currentMusicalColumn"));
+         finished_line_spanner_->set_bound (LEFT, cmc);
+       }
+      if (!finished_line_spanner_->get_bound (RIGHT))
+       finished_line_spanner_->set_bound (RIGHT,
+                                          finished_line_spanner_->get_bound (LEFT));
+      
+      
+      typeset_element (finished_line_spanner_);
+      finished_line_spanner_ = 0;
     }
 }
 
 void
 Dynamic_engraver::acknowledge_element (Score_element_info i)
 {
-  if (line_spanner_)
+  if (Note_column::has_interface (i.elem_l_))
     {
-      if (Note_column* n = dynamic_cast<Note_column*> (i.elem_l_))
+      if (line_spanner_)
        {
-         side_position (line_spanner_).add_support (n);
-         line_spanner_->add_column (n);
+         Side_position::add_support (line_spanner_,i.elem_l_);
+         add_bound_item (line_spanner_,dynamic_cast<Item*>(i.elem_l_));
        }
     }
 }