]> git.donarmstrong.com Git - lilypond.git/blobdiff - lily/molecule.cc
2003 -> 2004
[lilypond.git] / lily / molecule.cc
index f447d542d6e44738d8e117eebf5bca1addc56668..552e88675c7230e994e6594e88f98a64c2d5dbd7 100644 (file)
@@ -3,94 +3,89 @@
 
   source file of the GNU LilyPond music typesetter
 
-  (c)  1997--2000 Han-Wen Nienhuys <hanwen@cs.uu.nl>
+  (c) 1997--2004 Han-Wen Nienhuys <hanwen@cs.uu.nl>
 */
 
-/*
-  ugh. Rewrite not finished yet. Still must copy atom lists.
- */
-
 #include <math.h>
+#include <libc-extension.hh>   // isinf
 
+#include "font-metric.hh" 
+#include "dimensions.hh"
 #include "interval.hh"
 #include "string.hh"
 #include "molecule.hh"
-#include "atom.hh"
-#include "debug.hh"
-#include "killing-cons.tcc"
+#include "warn.hh"
 
 
-Box
-Molecule::extent() const
+#include "ly-smobs.icc"
+
+
+SCM
+Molecule::smobbed_copy () const
 {
-  return dim_;
+  Molecule * m = new Molecule (*this);
+
+  return m->smobbed_self ();
 }
 
 Interval
-Molecule::extent(Axis a) const
+Molecule::extent (Axis a) const
 {
   return dim_[a];
 }
 
-void
-Molecule::translate (Offset o)
+Molecule::Molecule (Box b, SCM func)
 {
-  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);
+  expr_ = func;
+  dim_ = b;
 }
 
-void
-Molecule::translate_axis (Real x,Axis a)
+Molecule::Molecule ()
 {
-  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)));
-    }
-
-  if (!dim_[a].empty_b ())
-    dim_[a] += x;
+  expr_ = SCM_EOL;
+  set_empty (true);
 }
 
 void
-Molecule::add_molecule (Molecule const &m)
+Molecule::translate (Offset o)
 {
-  for (SCM ptr = gh_cdr (m.atom_list_);  ptr != SCM_EOL; ptr = gh_cdr(ptr))
+  Axis a = X_AXIS;
+  while (a < NO_AXES)
     {
-      add_atom (gh_car (ptr));
+      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);
     }
-  dim_.unite (m.dim_);
+
+  expr_ = scm_list_n (ly_symbol2scm ("translate-molecule"),
+                  ly_offset2scm (o),
+                  expr_, SCM_UNDEFINED);
+  if (!is_empty ())
+    dim_.translate (o);
 }
+  
 
 void
-Molecule::add_atom (SCM atomsmob)
+Molecule::translate_axis (Real x,Axis a)
 {
-  gh_set_cdr_x (atom_list_,
-               gh_cons  (atomsmob, gh_cdr (atom_list_)));
-}
+  Offset o (0,0);
+  o[a] = x;
+  translate (o);
+}  
+
+
 
 void
-Molecule::operator=(Molecule const & src)
+Molecule::add_molecule (Molecule const &m)
 {
-  if (&src == this)
-    return;
-
-  atom_list_ = gh_cons (SCM_EOL,scm_list_copy (gh_cdr (src.atom_list_)));
-  dim_= src.dim_;
+  expr_ = scm_list_n (ly_symbol2scm ("combine-molecule"),
+                  m.expr_,
+                  expr_, SCM_UNDEFINED);
+  dim_.unite (m.dim_);
 }
 
 void
@@ -103,70 +98,95 @@ Molecule::set_empty (bool e)
     }
   else
     {
-      dim_[X_AXIS] = Interval(0,0);
+      dim_[X_AXIS] = Interval (0,0);
       dim_[Y_AXIS] = Interval (0,0);
     }
 }
 
+
 void
-Molecule::print () const
+Molecule::align_to (Axis a, Real x)
 {
-#ifndef NPRINT
-  for (SCM ptr = gh_cdr (atom_list_);  ptr != SCM_EOL; ptr = gh_cdr(ptr))
-    gh_display (gh_car (ptr));
-#endif
+  if (is_empty ())
+    return ;
+
+  Interval i (extent (a));
+  translate_axis (-i.linear_combination (x), a);
 }
 
-Molecule::Molecule (Molecule const &s)
+/*
+  See scheme Function.
+ */
+void
+Molecule::add_at_edge (Axis a, Direction d, Molecule const &m, Real padding,
+                      Real minimum)
 {
-  atom_list_ = gh_cons (SCM_EOL, scm_list_copy (gh_cdr (s.atom_list_)));
-  dim_ = s.dim_;
+  Real my_extent= is_empty () ? 0.0 : dim_[a][d];
+  Interval i (m.extent (a));
+  Real his_extent;
+  if (i.is_empty ())
+    {
+      programming_error ("Molecule::add_at_edge: adding empty molecule.");
+      his_extent = 0.0;
+    }
+  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);
 }
 
-Molecule::~Molecule ()
+
+
+/*
+  Hmm... maybe this is not such a good idea ; stuff can be empty,
+  while expr_ == '()
+ */
+bool
+Molecule::is_empty () const
 {
+  return expr_ == SCM_EOL;
 }
 
-
-void
-Molecule::align_to (Axis a, Direction d)
+SCM
+Molecule::get_expr () const
 {
-  if (d == CENTER)
-    {
-      Interval i (extent (a));
-      translate_axis (-i.center (), a);
-    }
-  else
-    {
-      translate_axis (-extent (a)[d], a);
-    }
+  return expr_;
 }
 
-Molecule::Molecule ()
+
+
+Box
+Molecule::extent_box () const
 {
-  dim_[X_AXIS].set_empty ();
-  dim_[Y_AXIS].set_empty ();
-  atom_list_ = gh_cons (SCM_EOL, SCM_EOL);
+  return dim_;
 }
+IMPLEMENT_SIMPLE_SMOBS (Molecule);
 
 
-void
-Molecule::add_at_edge (Axis a, Direction d, Molecule const &m, Real padding)
+int
+Molecule::print_smob (SCM , SCM port, scm_print_state *)
 {
-  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.");
+  scm_puts ("#<Molecule ", port);
+  scm_puts (" >", port);
   
-  Real his_extent = i[-d];
-  Real offset = my_extent -  his_extent;
-  Molecule toadd (m);
-  toadd.translate_axis (offset + d * padding, a);
-  add_molecule (toadd);
+  return 1;
 }
 
-bool
-Molecule::empty_b () const
+  
+SCM
+Molecule::mark_smob (SCM s)
 {
-  return gh_cdr (atom_list_) == SCM_EOL;
+  Molecule  *r = (Molecule *) ly_cdr (s);
+  
+  return r->expr_;
 }
+
+IMPLEMENT_TYPE_P (Molecule, "ly:molecule?");
+IMPLEMENT_DEFAULT_EQUAL_P (Molecule);
+