]> git.donarmstrong.com Git - lilypond.git/blobdiff - lily/molecule.cc
release: 1.3.26
[lilypond.git] / lily / molecule.cc
index 94d94cfd138a52357498de72c0e9ba63e5f6d605..45fa78b7ec5a5a019fd9a87d263e799beaf12ecc 100644 (file)
-#include "varray.hh"
+/*
+  molecule.cc -- implement Molecule
+
+  source file of the GNU LilyPond music typesetter
+
+  (c)  1997--2000 Han-Wen Nienhuys <hanwen@cs.uu.nl>
+*/
+#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"
 
-void
-Atom::print() const
-{
-    mtor << "texstring: " <<sym.tex<<"\n";    
-}
 
 Box
-Atom::extent() const
+Molecule::extent() const
 {
-    Box b( sym.dim);
-    b.translate(off);
-    return b;
+  return dim_;
 }
 
-Atom::Atom(Symbol s)
+Interval
+Molecule::extent(Axis a) const
 {
-    sym=s;
+  return dim_[a];
 }
 
-
-String
-Atom::TeXstring() const
+void
+Molecule::translate (Offset o)
 {
-    /* infinity checks. */
-    assert( abs(off.x) < 100 CM);
-    assert( abs(off.y) < 100 CM);
+  if (isinf (o.length ()))
+    {
+      programming_error ("Translating infinitely. Ignore.");
+      return;
+    }
     
-    // whugh.. Hard coded...
-    String s("\\placebox{%}{%}{%}");
-    Array<String> a;
-    a.push(print_dimen(off.y));
-    a.push(print_dimen(off.x));
-    a.push(sym.tex);
-    return substitute_args(s, a);
+  for (SCM ptr = gh_cdr (atom_list_);  ptr != SCM_EOL; ptr = gh_cdr(ptr))
+    {
+      unsmob_atom (gh_car (ptr))->off_ += o;
+    }
+  if (!empty_b ())
+    dim_.translate (o);
 }
 
-/* *************** */
-
-String
-Molecule::TeXstring() const
+void
+Molecule::translate_axis (Real x,Axis a)
 {
-    String s;
-    for(iter_top(ats,c); c.ok(); c++)
-       s+=c->TeXstring();
-    return s;
+  if (isinf (x))
+    {
+      programming_error ("Translating infinitely. Ignore.");
+      return;
+    }
+  for (SCM ptr = gh_cdr (atom_list_);  ptr != SCM_EOL; ptr = gh_cdr(ptr))
+    {
+      unsmob_atom (gh_car (ptr))->off_[a] += x;
+    }
+
+  if (!dim_[a].empty_b ())
+    dim_[a] += x;
 }
 
-Box
-Molecule::extent() const
+void
+Molecule::add_molecule (Molecule const &m)
 {
-    Box b;
-    for(iter_top(ats,c); c.ok(); c++)
-       b.unite(c->extent());
-    return b;
+  for (SCM ptr = gh_cdr (m.atom_list_);  ptr != SCM_EOL; ptr = gh_cdr(ptr))
+    {
+      Atom *a = new Atom (*unsmob_atom (gh_car (ptr)));
+      add_atom (a->self_scm_);
+    }
+  dim_.unite (m.dim_);
 }
 
 void
-Molecule::translate(Offset o)
+Molecule::add_atom (SCM atomsmob)
 {
-    for (iter_top(ats,c); c.ok(); c++)
-       c->translate(o);
+  gh_set_cdr_x (atom_list_,
+               gh_cons  (atomsmob, gh_cdr (atom_list_)));
+
+  scm_unprotect_object (atomsmob);
 }
 
 void
-Molecule::add(Molecule const &m)
+Molecule::operator=(Molecule const & src)
 {
-    for (iter_top(m.ats,c); c.ok(); c++) {
-       add(**c);
-    }
+  if (&src == this)
+    return;
+
+  atom_list_ = gh_cons (SCM_EOL,SCM_EOL);
+  dim_= src.dim_;
+  add_molecule (src);
 }
 
 void
-Molecule::add_right(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 xof=extent().x.right - m.extent().x.left;
-    Molecule toadd(m);
-    toadd.translate(Offset(xof, 0.0));
-    add(toadd);
 }
 
 void
-Molecule::add_left(Molecule const &m)
+Molecule::print () const
 {
-    if (!ats.size()) {
-       add(m);
-       return;
-    }
-    Real xof=extent().x.left - m.extent().x.right;
-    Molecule toadd(m);
-    toadd.translate(Offset(xof, 0.0));
-    add(toadd);
+#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)
+{
+  atom_list_ = gh_cons (SCM_EOL, SCM_EOL);
+  set_empty (true);
+  add_molecule (s);
+}
 
-void
-Molecule::add_top(Molecule const &m)
+Molecule::~Molecule ()
 {
-      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);
 }
 
+
 void
-Molecule::add_bottom(Molecule const &m)
+Molecule::align_to (Axis a, Direction d)
 {
-    if (!ats.size()) {
-       add(m);
-       return;
+  if (d == CENTER)
+    {
+      Interval i (extent (a));
+      translate_axis (-i.center (), a);
+    }
+  else
+    {
+      translate_axis (-extent (a)[d], a);
     }
-    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::Molecule ()
 {
-    assert(false);
+  dim_[X_AXIS].set_empty ();
+  dim_[Y_AXIS].set_empty ();
+  atom_list_ = gh_cons (SCM_EOL, SCM_EOL);
 }
 
-Molecule::Molecule(Molecule const &s)
+
+void
+Molecule::add_at_edge (Axis a, Direction d, Molecule const &m, Real padding)
 {
-    add(s);
+  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);
 }
 
-void
-Molecule::print() const
+bool
+Molecule::empty_b () const
 {
-    for (iter_top(ats,c); c.ok(); c++)
-       c->print();
+  return gh_cdr (atom_list_) == SCM_EOL;
 }