]> git.donarmstrong.com Git - lilypond.git/blobdiff - lily/local-key-item.cc
release: 1.3.131
[lilypond.git] / lily / local-key-item.cc
index a8823c59c930f63d1d88f3b0856f8cb5d4e8ff13..e815098effcb3929302390a99c006e6fa0e7715a 100644 (file)
 /*
-  local-key-item.cc -- implement Local_key_item, Musical_pitch
+  local-key-item.cc -- implement Local_key_item, Pitch
 
   source file of the GNU LilyPond music typesetter
 
-  (c)  1997--1999 Han-Wen Nienhuys <hanwen@cs.uu.nl>
+  (c)  1997--2001 Han-Wen Nienhuys <hanwen@cs.uu.nl>
 */
 #include "local-key-item.hh"
 #include "molecule.hh"
-
-#include "lookup.hh"
+#include "staff-symbol-referencer.hh"
+#include "font-interface.hh"
 #include "paper-def.hh"
 #include "musical-request.hh"
-#include "note-head.hh"
+#include "rhythmic-head.hh"
 #include "misc.hh"
+#include "lookup.hh"
 
-Local_key_item::Local_key_item ()
+static SCM
+pitch_less  (SCM p1, SCM p2)
 {
-  c0_position_i_ = 0;
+  return Pitch::less_p (gh_car (p1),  gh_car (p2));
 }
 
+static SCM pitch_less_proc;
+
 void
-Local_key_item::add_pitch (Musical_pitch p, bool cautionary, bool natural)
+init_pitch_funcs ()
 {
-  for (int i=0; i< accidental_arr_.size(); i++)
-    if (!Musical_pitch::compare (p, accidental_arr_[i].pitch_))
-      return;
-         /* maybe natural (and cautionary) should be modif. nonetheless? */
-
-  Local_key_cautionary_tuple t;
-  t.pitch_ = p;
-  t.cautionary_b_ = cautionary;
-  t.natural_b_ = natural;
-  accidental_arr_.push (t);
+  pitch_less_proc = gh_new_procedure2_0 ("pits-less", &pitch_less);
 }
 
+ADD_SCM_INIT_FUNC(lkpitch,init_pitch_funcs);
 
 
 void
-Local_key_item::do_pre_processing()
+Local_key_item::add_pitch (Grob*me, Pitch p, bool cautionary, bool natural)
 {
-  accidental_arr_.sort (Local_key_cautionary_tuple::compare);
-  Note_head_side::do_pre_processing ();
+  SCM acs = me->get_grob_property ("accidentals");
+  SCM pitch = p.smobbed_copy ();
+  SCM opts = SCM_EOL;
+  if (cautionary)
+    opts = gh_cons (ly_symbol2scm ("cautionary"), opts);
+  if (natural)
+    opts = gh_cons (ly_symbol2scm ("natural"), opts);
+
+  pitch = gh_cons (pitch, opts);
+  acs = scm_merge_x (acs, gh_cons (pitch, SCM_EOL), pitch_less_proc);
+
+  me->set_grob_property ("accidentals", acs);
 }
 
 Molecule
-Local_key_item::accidental (int j, bool cautionary, bool natural) const
+Local_key_item::parenthesize (Grob*me, Molecule m)
 {
-  Molecule m (lookup_l ()->afm_find (String ("accidentals-") + to_str (j)));
-  if (natural)
-    {
-         Molecule prefix = lookup_l ()->afm_find (String ("accidentals-0"));
-         m.add_at_edge(X_AXIS, LEFT, Molecule(prefix), 0);
-       }
-  if (cautionary) 
-    {
-      Molecule open = lookup_l ()->afm_find (String ("accidentals-("));
-      Molecule close = lookup_l ()->afm_find (String ("accidentals-)"));
-      m.add_at_edge(X_AXIS, LEFT, Molecule(open), 0);
-      m.add_at_edge(X_AXIS, RIGHT, Molecule(close), 0);
-    }
-  
+  Molecule open = Font_interface::get_default_font (me)->find_by_name (String ("accidentals-("));
+  Molecule close = Font_interface::get_default_font (me)->find_by_name (String ("accidentals-)"));
+  m.add_at_edge(X_AXIS, LEFT, Molecule(open), 0);
+  m.add_at_edge(X_AXIS, RIGHT, Molecule(close), 0);
+
   return m;
 }
 
-Molecule*
-Local_key_item::do_brew_molecule_p() const
+/*
+  UGH. clean me, revise placement routine (See Ross & Wanske;
+  accidental placement is more complicated than this.
+ */
+
+MAKE_SCHEME_CALLBACK(Local_key_item,brew_molecule,1);
+SCM
+Local_key_item::brew_molecule (SCM smob)
 {
-  Molecule*output = new Molecule;
-  Real note_distance = staff_line_leading_f ()/2;
-  Molecule *octave_mol_p = 0;
-  int lastoct = -100;
+  Grob* me = unsmob_grob (smob);
   
-  for  (int i = 0; i <  accidental_arr_.size(); i++) 
+  Molecule mol;
+
+  Real note_distance = Staff_symbol_referencer::staff_space (me)/2;
+  Molecule octave_mol;
+  bool oct_b = false;
+  int lastoct = -100;
+
+  SCM accs = me->get_grob_property ("accidentals");
+  for  (SCM s = accs;
+       gh_pair_p (s); s = gh_cdr (s))
     {
-      Musical_pitch p (accidental_arr_[i].pitch_);
+      Pitch p (*unsmob_pitch (gh_caar (s)));
+      SCM opts = gh_cdar (s);
+      
       // do one octave
-      if (p.octave_i_ != lastoct) 
+      if (p.octave_i ()  != lastoct) 
        {
-         if (octave_mol_p)
+         if (oct_b)
            {
              Real dy =lastoct*7* note_distance;
-             octave_mol_p->translate_axis (dy, Y_AXIS);
-             output->add_molecule (*octave_mol_p);
-             delete octave_mol_p;
+             octave_mol.translate_axis (dy, Y_AXIS);
+             mol.add_molecule (octave_mol);
+             octave_mol = Molecule ();
            }
-         octave_mol_p= new Molecule;
+         oct_b = true; 
        }
       
-      lastoct = p.octave_i_;
-      Real dy =
-       (c0_position_i_ + p.notename_i_)
+      lastoct = p.octave_i () ;
+
+      SCM c0 =  me->get_grob_property ("c0-position");
+      Real dy = (gh_number_p (c0) ? gh_scm2int (c0) : 0 + p.notename_i_)
        * note_distance;
       
-      Molecule m (accidental (p.accidental_i_,
-                                 accidental_arr_[i].cautionary_b_,
-                                 accidental_arr_[i].natural_b_));
+      Molecule acc (Font_interface::get_default_font (me)->find_by_name (String ("accidentals-")
+                                              + to_str (p.alteration_i_)));
+      
+      if (scm_memq (ly_symbol2scm ("natural"), opts) != SCM_BOOL_F)
+       {
+         Molecule prefix = Font_interface::get_default_font (me)->find_by_name (String ("accidentals-0"));
+         acc.add_at_edge(X_AXIS, LEFT, Molecule(prefix), 0);
+       }
+
+      if (scm_memq (ly_symbol2scm ("cautionary"), opts) != SCM_BOOL_F)
+       acc = parenthesize (me, acc);
 
-      m.translate_axis (dy, Y_AXIS);
-      octave_mol_p->add_at_edge (X_AXIS, RIGHT, m, 0);
+      acc.translate_axis (dy, Y_AXIS);
+      octave_mol.add_at_edge (X_AXIS, RIGHT, acc, 0);
     }
 
-  if (octave_mol_p)
+  if (oct_b)
     {
       Real dy =lastoct*7*note_distance;
-      octave_mol_p->translate_axis (dy, Y_AXIS);
-      output->add_molecule (*octave_mol_p);
-      delete octave_mol_p;
+      octave_mol.translate_axis (dy, Y_AXIS);
+      mol.add_molecule (octave_mol);
+      octave_mol = Molecule ();
     }
   
- if (accidental_arr_.size()) 
+ if (gh_pair_p (accs))
     {
       Drul_array<SCM> pads;
 
       /*
        Use a cons?
        */
-      pads[RIGHT] = get_elt_property ("right-padding");
-      pads[LEFT] = get_elt_property ("left-padding");
+      pads[RIGHT] = me->get_grob_property ("right-padding");
+      pads[LEFT] = me->get_grob_property ("left-padding");
 
+
+      // unused ?
       Direction d = LEFT;
       do {
        if (!gh_number_p (pads[d]))
@@ -125,11 +147,21 @@ Local_key_item::do_brew_molecule_p() const
 
        Box b(Interval (0, gh_scm2double (pads[d]) * note_distance),
              Interval (0,0));
-       Molecule m (lookup_l ()->fill (b));
-       output->add_at_edge (X_AXIS, d, m, 0);
+       Molecule m (Lookup::blank (b));
+       mol.add_at_edge (X_AXIS, d, m, 0);
       } while ( flip (&d)!= LEFT);
     }
 
-  return output;
+  return mol.smobbed_copy();
 }
 
+bool
+Local_key_item::has_interface (Grob*m)
+{
+  return m && m->has_interface (ly_symbol2scm ("accidentals-interface"));
+}
+void
+Local_key_item::set_interface (Grob*m)
+{
+  m->set_interface (ly_symbol2scm ("accidentals-interface"));
+}