]> git.donarmstrong.com Git - lilypond.git/blobdiff - lily/molecule.cc
* lily/parser.yy (relative_music): whoops. All \relative were off
[lilypond.git] / lily / molecule.cc
index f9cc7bb12a9da38d87b4e51a55c4616fda3d913d..af60d58ab4ccf50948e39be0bd9ba0dfee17fbdc 100644 (file)
 
   source file of the GNU LilyPond music typesetter
 
-  (c) 1997 Han-Wen Nienhuys <hanwen@stack.nl>
+  (c)  1997--2003 Han-Wen Nienhuys <hanwen@cs.uu.nl>
 */
 
+#include <math.h>
+#include <libc-extension.hh>   // isinf
+
+#include "font-metric.hh" 
+#include "dimensions.hh"
 #include "interval.hh"
-#include "dimen.hh"
 #include "string.hh"
 #include "molecule.hh"
-#include "symbol.hh"
-#include "debug.hh"
-#include "tex.hh"
+#include "warn.hh"
+
+
+#include "ly-smobs.icc"
 
-String
-Molecule::TeX_string() const
+
+SCM
+Molecule::smobbed_copy () const
 {
-    String s;
-    for(iter_top(ats,c); c.ok(); c++)
-       s+=c->TeX_string();
-    return s;
+  Molecule * m = new Molecule (*this);
+
+  return m->smobbed_self ();
 }
 
-Box
-Molecule::extent() const
+Interval
+Molecule::extent (Axis a) const
 {
-    Box b;
-    for(iter_top(ats,c); c.ok(); c++)
-       b.unite(c->extent());
-    return b;
+  return dim_[a];
 }
 
-void
-Molecule::translate(Offset o)
+Molecule::Molecule (Box b, SCM func)
 {
-    for (iter_top(ats,c); c.ok(); c++)
-       c->translate(o);
+  expr_ = func;
+  dim_ = b;
 }
 
-void
-Molecule::translate(Real x,Axis a)
+Molecule::Molecule ()
 {
-    for (iter_top(ats,c); c.ok(); c++)
-       c->translate(x,a);
+  expr_ = SCM_EOL;
+  set_empty (true);
 }
 
 void
-Molecule::add(Molecule const &m)
+Molecule::translate (Offset o)
 {
-    for (iter_top(m.ats,c); c.ok(); c++) {
-       add(**c);
+  Axis a = X_AXIS;
+  while (a < NO_AXES)
+    {
+      if (abs (o[a]) > 100 CM
+         || isinf (o[a]) || isnan (o[a]))
+       {
+         programming_error ("Improbable offset for translation: setting to zero");
+         o[a] =  0.0;
+       }
+      incr (a);
     }
+
+  expr_ = scm_list_n (ly_symbol2scm ("translate-molecule"),
+                  ly_offset2scm (o),
+                  expr_, SCM_UNDEFINED);
+  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;
-    }
-     Real xof=extent().x().right - m.extent().x().left;
-     
-    Molecule toadd(m);
-    toadd.translate(Offset(xof, 0.0));
-    add(toadd);
+  Offset o (0,0);
+  o[a] = x;
+  translate (o);
+}  
+
+
+
+void
+Molecule::add_molecule (Molecule const &m)
+{
+  expr_ = scm_list_n (ly_symbol2scm ("combine-molecule"),
+                  m.expr_,
+                  expr_, SCM_UNDEFINED);
+  dim_.unite (m.dim_);
 }
 
 void
-Molecule::add_left(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().left - m.extent().x().right;
-   
-    Molecule toadd(m);
-    toadd.translate(Offset(xof, 0.0));
-    add(toadd);
 }
 
 
 void
-Molecule::add_top(Molecule const &m)
+Molecule::align_to (Axis a, Direction d)
 {
-      if (!ats.size()) {
-       add(m);
-       return;
-    }
-   Real yof=extent().y().right - m.extent().y().left;
+  if (empty_b())
+    return ;
 
-    Molecule toadd(m);
-    toadd.translate(yof, Y_AXIS);
-    add(toadd);
+  Interval i (extent (a));
+  Real r = (d == CENTER) ? i.center () : i[d];
+  translate_axis (-r, a);
 }
 
+/*
+  See scheme Function.
+ */
 void
-Molecule::add_bottom(Molecule const &m)
+Molecule::add_at_edge (Axis a, Direction d, Molecule const &m, Real padding,
+                      Real minimum)
 {
-    if (!ats.size()) {
-       add(m);
-       return;
+  Real my_extent= empty_b () ? 0.0 : dim_[a][d];
+  Interval i (m.extent (a));
+  Real his_extent;
+  if (i.empty_b ())
+    {
+      programming_error ("Molecule::add_at_edge: adding empty molecule.");
+      his_extent = 0.0;
     }
-     Real yof=extent().y().left- m.extent().y().right;
-    Molecule toadd(m);
-    toadd.translate(yof, Y_AXIS);
-    add(toadd);
+  else
+    his_extent = i[-d];      
+
+  Real offset = (my_extent -  his_extent)  + d*padding;
+  if (minimum > 0  && fabs (offset) <  minimum)
+    offset = sign (offset) * minimum; 
+  
+  Molecule toadd (m);
+  toadd.translate_axis (offset, a);
+  add_molecule (toadd);
 }
 
-void
-Molecule::operator = (Molecule const &)
+
+
+/*
+  Hmm... maybe this is not such a good idea ; stuff can be empty,
+  while expr_ == '()
+ */
+bool
+Molecule::empty_b () const
 {
-    assert(false);
+  return expr_ == SCM_EOL;
 }
 
-Molecule::Molecule(Molecule const &s)
+SCM
+Molecule::get_expr () const
 {
-    add(s);
+  return expr_;
 }
 
-void
-Molecule::print() const
+
+
+Box
+Molecule::extent_box () const
+{
+  return dim_;
+}
+IMPLEMENT_SIMPLE_SMOBS (Molecule);
+
+
+int
+Molecule::print_smob (SCM , SCM port, scm_print_state *)
 {
-#ifndef NPRINT
-    if (! check_debug)
-       return;
-    for (iter_top(ats,c); c.ok(); c++)
-       c->print();
+  scm_puts ("#<Molecule ", port);
+#if 0
+  Molecule  *r = (Molecule *) ly_cdr (s);
+  String string (r->to_string ());
+  scm_puts ((char *)str.to_str0 (), port);
 #endif
+  scm_puts (" >", port);
+  
+  return 1;
 }
 
-void
-Molecule::add(Atom const &a)
+  
+SCM
+Molecule::mark_smob (SCM s)
 {
-    ats.bottom().add(new Atom(a)); 
+  Molecule  *r = (Molecule *) ly_cdr (s);
+  
+  return r->expr_;
 }
+
+IMPLEMENT_TYPE_P (Molecule, "ly:molecule?");
+IMPLEMENT_DEFAULT_EQUAL_P (Molecule);
+