]> git.donarmstrong.com Git - lilypond.git/blobdiff - lily/molecule.cc
release: 1.3.28
[lilypond.git] / lily / molecule.cc
index c39082ab929cf7d8ddbf4c15ffc0fe594f7bcba7..f447d542d6e44738d8e117eebf5bca1addc56668 100644 (file)
 
   source file of the GNU LilyPond music typesetter
 
-  (c) 1997 Han-Wen Nienhuys <hanwen@stack.nl>
+  (c)  1997--2000 Han-Wen Nienhuys <hanwen@cs.uu.nl>
 */
 
+/*
+  ugh. Rewrite not finished yet. Still must copy atom lists.
+ */
+
+#include <math.h>
+
 #include "interval.hh"
-#include "dimen.hh"
 #include "string.hh"
 #include "molecule.hh"
-#include "symbol.hh"
+#include "atom.hh"
 #include "debug.hh"
-#include "tex.hh"
-
+#include "killing-cons.tcc"
 
-/* *************** */
-
-String
-Molecule::TeX_string() const
-{
-    String s;
-    for(iter_top(ats,c); c.ok(); c++)
-       s+=c->TeX_string();
-    return s;
-}
 
 Box
 Molecule::extent() const
 {
-    Box b;
-    for(iter_top(ats,c); c.ok(); c++)
-       b.unite(c->extent());
-    return b;
+  return dim_;
 }
 
-void
-Molecule::translate(Offset o)
+Interval
+Molecule::extent(Axis a) const
 {
-    for (iter_top(ats,c); c.ok(); c++)
-       c->translate(o);
+  return dim_[a];
 }
 
 void
-Molecule::add(Molecule const &m)
+Molecule::translate (Offset o)
 {
-    for (iter_top(m.ats,c); c.ok(); c++) {
-       add(**c);
+  if (isinf (o.length ()))
+    {
+      programming_error ("Translating infinitely. Ignore.");
+      return;
     }
+    
+  for (SCM ptr = gh_cdr (atom_list_);  ptr != SCM_EOL; ptr = gh_cdr(ptr))
+    {
+      gh_set_car_x (ptr, translate_atom (o, gh_car (ptr)));
+    }
+  if (!empty_b ())
+    dim_.translate (o);
 }
 
 void
-Molecule::add_right(Molecule const &m)
+Molecule::translate_axis (Real x,Axis a)
 {
-     if (!ats.size()) {
-       add(m);
-       return;
+  if (isinf (x))
+    {
+      programming_error ("Translating infinitely. Ignore.");
+      return;
+    }
+  for (SCM ptr = gh_cdr (atom_list_);  ptr != SCM_EOL; ptr = gh_cdr(ptr))
+    {
+      gh_set_car_x (ptr, translate_atom_axis (x, a, gh_car (ptr)));
     }
-   Real xof=extent().x.right - m.extent().x.left;
-    Molecule toadd(m);
-    toadd.translate(Offset(xof, 0.0));
-    add(toadd);
+
+  if (!dim_[a].empty_b ())
+    dim_[a] += x;
 }
 
 void
-Molecule::add_left(Molecule const &m)
+Molecule::add_molecule (Molecule const &m)
 {
-    if (!ats.size()) {
-       add(m);
-       return;
+  for (SCM ptr = gh_cdr (m.atom_list_);  ptr != SCM_EOL; ptr = gh_cdr(ptr))
+    {
+      add_atom (gh_car (ptr));
     }
-    Real xof=extent().x.left - m.extent().x.right;
-    Molecule toadd(m);
-    toadd.translate(Offset(xof, 0.0));
-    add(toadd);
+  dim_.unite (m.dim_);
 }
 
+void
+Molecule::add_atom (SCM atomsmob)
+{
+  gh_set_cdr_x (atom_list_,
+               gh_cons  (atomsmob, gh_cdr (atom_list_)));
+}
 
 void
-Molecule::add_top(Molecule const &m)
+Molecule::operator=(Molecule const & src)
 {
-      if (!ats.size()) {
-       add(m);
-       return;
-    }
-  Real yof=extent().y.right - m.extent().y.left;
-    Molecule toadd(m);
-    toadd.translate(Offset(0,yof));
-    add(toadd);
+  if (&src == this)
+    return;
+
+  atom_list_ = gh_cons (SCM_EOL,scm_list_copy (gh_cdr (src.atom_list_)));
+  dim_= src.dim_;
 }
 
 void
-Molecule::add_bottom(Molecule const &m)
+Molecule::set_empty (bool e)
 {
-    if (!ats.size()) {
-       add(m);
-       return;
+  if (e)
+    {
+      dim_[X_AXIS].set_empty ();
+      dim_[Y_AXIS].set_empty ();
+    }
+  else
+    {
+      dim_[X_AXIS] = Interval(0,0);
+      dim_[Y_AXIS] = Interval (0,0);
     }
-    Real yof=extent().y.left- m.extent().y.right;
-    Molecule toadd(m);
-    toadd.translate(Offset(0,yof));
-    add(toadd);
 }
 
 void
-Molecule::operator = (Molecule const &)
+Molecule::print () const
+{
+#ifndef NPRINT
+  for (SCM ptr = gh_cdr (atom_list_);  ptr != SCM_EOL; ptr = gh_cdr(ptr))
+    gh_display (gh_car (ptr));
+#endif
+}
+
+Molecule::Molecule (Molecule const &s)
 {
-    assert(false);
+  atom_list_ = gh_cons (SCM_EOL, scm_list_copy (gh_cdr (s.atom_list_)));
+  dim_ = s.dim_;
 }
 
-Molecule::Molecule(Molecule const &s)
+Molecule::~Molecule ()
 {
-    add(s);
 }
 
+
 void
-Molecule::print() const
+Molecule::align_to (Axis a, Direction d)
 {
-    for (iter_top(ats,c); c.ok(); c++)
-       c->print();
+  if (d == CENTER)
+    {
+      Interval i (extent (a));
+      translate_axis (-i.center (), a);
+    }
+  else
+    {
+      translate_axis (-extent (a)[d], a);
+    }
 }
 
+Molecule::Molecule ()
+{
+  dim_[X_AXIS].set_empty ();
+  dim_[Y_AXIS].set_empty ();
+  atom_list_ = gh_cons (SCM_EOL, SCM_EOL);
+}
+
+
 void
-Molecule::add(Atom const &a)
+Molecule::add_at_edge (Axis a, Direction d, Molecule const &m, Real padding)
+{
+  Real my_extent= empty_b () ? 0.0 : dim_[a][d];
+  Interval i (m.extent ()[a]);
+  if (i.empty_b ())
+    programming_error ("Molecule::add_at_edge: adding empty molecule.");
+  
+  Real his_extent = i[-d];
+  Real offset = my_extent -  his_extent;
+  Molecule toadd (m);
+  toadd.translate_axis (offset + d * padding, a);
+  add_molecule (toadd);
+}
+
+bool
+Molecule::empty_b () const
 {
-    ats.bottom().add(new Atom(a)); 
+  return gh_cdr (atom_list_) == SCM_EOL;
 }