]> git.donarmstrong.com Git - lilypond.git/blobdiff - lily/chord-name.cc
release: 1.3.75
[lilypond.git] / lily / chord-name.cc
index b190520b74ca5fb41117b5de16e025267ba15509..62562e08ec1a96e57a62a66a171bdbb003211d0c 100644 (file)
 
   source file of the GNU LilyPond music typesetter
 
-  (c)  1999 Jan Nieuwenhuizen <janneke@gnu.org>
+  (c)  1999--2000 Jan Nieuwenhuizen <janneke@gnu.org>
 */
 
 #include "chord-name.hh"
-#include "musical-request.hh"
-#include "warn.hh"
-#include "debug.hh"
 #include "molecule.hh"
 #include "paper-def.hh"
 #include "lookup.hh"
-
-SCM
-pitch2scm (Musical_pitch p)
-{
-  return gh_cons (gh_int2scm (p.notename_i_), gh_int2scm (p.accidental_i_));
-}
-
-Chord_name::Chord_name (Chord const& c)
-{
-  chord_ = c;
-}
+#include "score-element.hh"
 
 /*
-  word is roman text or styled text:
-   "text"
-   ("style" . "text")
+  TODO: move text lookup out of Chord_name
  */
-Molecule
-Chord_name::ly_word2molecule (SCM scm) const
-{
-  String style;
-  if (gh_pair_p (scm))
-    {
-      style = ly_scm2string (gh_car (scm));
-      scm = gh_cdr (scm);
-    }
-  String text = ly_scm2string (scm);
-  return lookup_l ()->text (style, text, paper_l ());
-}
 
 /*
scm is word or list of words:
-   word
-   (word word)
 word is roman text or property-styled text:
+   "text"
+   ("text" . property-alist)
  */
-Molecule
-Chord_name::ly_text2molecule (SCM scm) const
-{
-  Molecule mol;
-  if (gh_list_p (scm))
-    {
-      while (gh_cdr (scm) != SCM_EOL)
-        {
-         mol.add_at_edge (X_AXIS, RIGHT, 
-            ly_word2molecule (gh_car (scm)), 0);
-         scm = gh_cdr (scm);
-       }
-      scm = gh_car (scm);
-    }  
-  mol.add_at_edge (X_AXIS, RIGHT, 
-    ly_word2molecule (scm), 0);
-  return mol;
-}
 
 Molecule
-Chord_name::pitch2molecule (Musical_pitch p) const
+Chord_name::ly_word2molecule (Score_element * me, SCM word, Real* x) 
 {
-  SCM name = scm_eval (gh_list (ly_symbol2scm ("user-pitch-name"), ly_quote_scm (pitch2scm (p)), SCM_UNDEFINED));
+  *x = 0;
 
-  if (name != SCM_UNSPECIFIED)
+  SCM options_alist = SCM_EOL;
+  if (gh_pair_p (word))
     {
-      return ly_text2molecule (name);
+      options_alist = gh_cdr (word);
+      word = gh_car (word);
     }
 
-  Molecule mol = lookup_l ()->text ("", p.str ().left_str (1).upper_str (), paper_l ());
-
-  /*
-    We want the smaller size, even if we're big ourselves.
-   */
-  if (p.accidental_i_)
-    mol.add_at_edge (X_AXIS, RIGHT, 
-                    
-                    paper_l ()->lookup_l (-2)->afm_find (String ("accidentals-") + to_str (p.accidental_i_)), 0.0);
-  return mol;
-}
-
-Musical_pitch
-diff_pitch (Musical_pitch tonic, Musical_pitch  p)
-{
-  Musical_pitch diff (p.notename_i_ - tonic.notename_i_, 
-    p.accidental_i_ - tonic.accidental_i_, 
-    p.octave_i_ - tonic.octave_i_);
-
-  while  (diff.notename_i_ >= 7)
+  if (gh_string_p (word))
     {
-      diff.notename_i_ -= 7;
-      diff.octave_i_ ++;
-    }
-  while  (diff.notename_i_ < 0)
-    {
-      diff.notename_i_ += 7;
-      diff.octave_i_ --;
-    }
-
-  diff.accidental_i_ -= (tonic.semitone_pitch () + diff.semitone_pitch ())
-    - p.semitone_pitch ();
-
-  return diff;
-}
-
-bool
-Chord_name::user_chord_name (Array<Musical_pitch> pitch_arr, Chord_mol* name_p) const
-{
-  SCM chord = SCM_EOL;
-  Array<Musical_pitch> chord_type = pitch_arr;
-  Chord::rebuild_transpose (&chord_type, diff_pitch (pitch_arr[0], Musical_pitch (0)), false);
-
-  for (int i= chord_type.size (); i--; )
-    chord = gh_cons (pitch2scm (chord_type[i]), chord);
-
-  SCM name = scm_eval (gh_list (ly_symbol2scm ("user-chord-name"), ly_quote_scm (chord), SCM_UNDEFINED));
-  if (gh_pair_p (name))
-    {
-      name_p->modifier_mol = ly_text2molecule (gh_car (name));
-      name_p->addition_mol = ly_text2molecule (gh_cdr (name));
-      return true;
-    }
-  return false;
-}
-
-void
-Chord_name::banter (Array<Musical_pitch> pitch_arr, Chord_mol* name_p) const
-{
-  Array<Musical_pitch> add_arr;
-  Array<Musical_pitch> sub_arr;
-  Chord::find_additions_and_subtractions (pitch_arr, &add_arr, &sub_arr);
-                          
-  Array<Musical_pitch> scale;
-  for (int i=0; i < 7; i++)
-    scale.push (Musical_pitch (i));
+      /*
+       UGH. Should read from font metric structure.
+      */
+      Real ex = me->lookup_l ()->text ("", "x",
+                                  me->paper_l ()).extent (Y_AXIS).length ();
+      Real em = me->lookup_l ()->text ("", "m",
+                                  me->paper_l ()).extent (X_AXIS).length ();
 
-  Musical_pitch tonic = pitch_arr[0];
-  chord_.rebuild_transpose (&scale, tonic, true);
-  
-  /*
-    Does chord include this step?  -1 if flat
-   */
-  int has[16];
-  for (int i=0; i<16; i++)
-    has[i] = 0;
+      String w = ly_scm2string (word);
 
-  String mod_str;
-  String add_str;
-  String sep_str;
-  for (int i = 0; i < add_arr.size (); i++)
-    {
-      Musical_pitch p = add_arr[i];
-      int step = Chord::step_i (tonic, p);
-      int accidental = p.accidental_i_ - scale[(step - 1) % 7].accidental_i_;
-      if ((step < 16) && (has[step] != -1))
-        has[step] = accidental == -1 ? -1 : 1;
-      // only from guile table ?
-      if ((step == 3) && (accidental == -1))
+      String style;
+      SCM s = scm_assoc (ly_symbol2scm ("style"), options_alist);
+      if (s != SCM_BOOL_F)
        {
-         mod_str = "m";
+         style = ly_scm2string (gh_cdr (s));
        }
-      else if (accidental
-              || (!(step % 2) 
-              || ((i == add_arr.size () - 1) && (step > 5))))
-        {
-         add_str += sep_str;
-         sep_str = "/";
-          if ((step == 7) && (accidental == 1))
-           {
-              add_str += "maj7";
-           }
-         else
-           {
-             add_str += to_str (step);
-             if (accidental)
-               add_str += accidental < 0 ? "-" : "+";
-           }
-       }
-    }
 
-  for (int i = 0; i < sub_arr.size (); i++)
-    {
-      Musical_pitch p = sub_arr[i];
-      int step = Chord::step_i (tonic, p);
+      Offset offset;
+      int size = 0;
       /*
-       if additions include 2 or 4, assume sus2/4 and don't display 'no3'
+       urg, `type'
       */
-      if (!((step == 3) && (has[2] || has[4])))
+      s = scm_assoc (ly_symbol2scm ("type"), options_alist);
+      if (s != SCM_BOOL_F && ly_scm2string (gh_cdr (s)) == "super")
        {
-         add_str += sep_str + "no" + to_str (step);
-         sep_str = "/";
+         Real super_y = ex / 2;
+         offset = Offset (0, super_y);
+         if (!size)
+           size = -2;
        }
-    }
 
-  if (mod_str.length_i ())
-    name_p->modifier_mol.add_at_edge (X_AXIS, RIGHT, 
-      lookup_l ()->text ("roman", mod_str, paper_l ()), 0);
-  if (add_str.length_i ())
-    {
-      if (!name_p->addition_mol.empty_b ())
-        add_str = "/" + add_str;
-      name_p->addition_mol.add_at_edge (X_AXIS, RIGHT,
-       lookup_l ()->text ("script", add_str, paper_l ()), 0);
+      s = scm_assoc (ly_symbol2scm ("size"), options_alist);
+      if (s != SCM_BOOL_F)
+       {
+         size = gh_scm2int (gh_cdr (s));
+       }
+
+      s = scm_assoc (ly_symbol2scm ("offset"), options_alist);
+      if (s != SCM_BOOL_F)
+       {
+         // hmm
+         SCM o = gh_cdr (s);
+         if (gh_pair_p (o))
+           offset = Offset (0, gh_scm2double (gh_cdr (o))) * ex;
+         *x = gh_scm2double (gh_car (o)) * em;
+       }
+
+      Molecule mol;
+      s = scm_assoc (ly_symbol2scm ("font"), options_alist);
+      if (s != SCM_BOOL_F && ly_scm2string (gh_cdr (s)) == "feta")
+        mol = me->paper_l ()->lookup_l (size)->afm_find (w);
+      else
+       mol = me->paper_l ()->lookup_l (size)->text (style, w, me->paper_l ());
+
+      mol.translate (offset);
+      return mol;
     }
+  return Molecule ();
 }
 
-Molecule*
-Chord_name::do_brew_molecule_p () const
+/*
+  ;; text: list of word
+  ;; word: string + optional list of property
+  ;; property: align, kern, font (?), size
+ */
+Molecule
+Chord_name::ly_text2molecule (Score_element * me, SCM text) 
 {
-  Musical_pitch tonic = chord_.pitch_arr_[0];
-  
-  Chord_mol name;
-  name.tonic_mol = pitch2molecule (tonic);
-
-  /*
-    if user has explicitely listed chord name, use that
-    
-    TODO
-    urg
-    maybe we should check all sub-lists of pitches, not
-    just full list and base triad?
-   */
-  if (!user_chord_name (chord_.pitch_arr_, &name))
+  Molecule mol;
+  if (gh_list_p (text))
     {
-      /*
-        else, check if user has listed base triad
-       use user base name and add banter for remaining part
-       */
-      if ((chord_.pitch_arr_.size () > 2)
-         && user_chord_name (chord_.pitch_arr_.slice (0, 3), &name))
+      while (gh_cdr (text) != SCM_EOL)
         {
-         Array<Musical_pitch> base = Chord::base_arr (tonic);
-         base.concat (chord_.pitch_arr_.slice (3, chord_.pitch_arr_.size ()));
-         banter (base, &name);
+         Real x;
+         Molecule m = ly_word2molecule (me, gh_car (text), &x);
+         if (!m.empty_b ())
+           mol.add_at_edge (X_AXIS, RIGHT, m, x);
+         text = gh_cdr (text);
        }
-      /*
-        else, use pure banter
-       */
-      else
-       {
-         banter (chord_.pitch_arr_, &name);
-       }
-    }
+      text = gh_car (text);
+    }  
+  Real x;
+  Molecule m = ly_word2molecule (me,text, &x);
+  if (!m.empty_b ())
+    mol.add_at_edge (X_AXIS, RIGHT, m, x);
+  return mol;
+}
 
-  if (chord_.inversion_b_)
-    {
-      name.inversion_mol = lookup_l ()->text ("", "/", paper_l ());
-      // zucht  const&
-      Molecule mol = pitch2molecule (chord_.inversion_pitch_);
-      name.inversion_mol.add_at_edge (X_AXIS, RIGHT, mol, 0);
-    }
+MAKE_SCHEME_CALLBACK(Chord_name,brew_molecule);
 
-  if (chord_.bass_b_)
-    {
-      name.bass_mol = lookup_l ()->text ("", "/", paper_l ());
-      Molecule mol = pitch2molecule (chord_.bass_pitch_);
-      name.bass_mol.add_at_edge (X_AXIS, RIGHT, mol, 0);
-    }
+SCM
+Chord_name::brew_molecule (SCM smob) 
+{
+  Score_element *sc = unsmob_element (smob);
+  SCM style = sc->get_elt_property ("style");
+
+  if (!gh_string_p (style))
+    style = ly_str02scm ("banter");
+
+  SCM inversion = sc-> get_elt_property ("inversion");
+  if (inversion == SCM_EOL)
+    inversion = SCM_BOOL_F;
+
+  SCM bass =  sc->get_elt_property ("bass");
+  if (bass == SCM_EOL)
+    bass = SCM_BOOL_F;
 
-  // urg, howto get a good superscript_y?
-  Real super_y = lookup_l ()->text ("", "x", paper_l ()).dim_.y ().length ()/2;
-  if (!name.addition_mol.empty_b ())
-    name.addition_mol.translate (Offset (0, super_y));
+  SCM pitches =  sc->get_elt_property ("pitches");
+  SCM text = scm_eval (gh_list (ly_symbol2scm ("chord::user-name"),
+                               style,
+                               ly_quote_scm (pitches),
+                               ly_quote_scm (gh_cons (inversion, bass)),
+                               SCM_UNDEFINED));
 
-  Molecule* mol_p = new Molecule;
-  mol_p->add_at_edge (X_AXIS, RIGHT, name.tonic_mol, 0);
-  // huh?
-  if (!name.modifier_mol.empty_b ())
-    mol_p->add_at_edge (X_AXIS, RIGHT, name.modifier_mol, 0);
-  if (!name.addition_mol.empty_b ())
-    mol_p->add_at_edge (X_AXIS, RIGHT, name.addition_mol, 0);
-  if (!name.inversion_mol.empty_b ())
-    mol_p->add_at_edge (X_AXIS, RIGHT, name.inversion_mol, 0);
-  if (!name.bass_mol.empty_b ())
-    mol_p->add_at_edge (X_AXIS, RIGHT, name.bass_mol, 0);
-  return mol_p;
+  return ly_text2molecule (sc, text).create_scheme ();
 }