]> git.donarmstrong.com Git - lilypond.git/blobdiff - lily/molecule.cc
* Documentation/user/music-glossary.itely: add Finnish author.
[lilypond.git] / lily / molecule.cc
index cd4d0482063cfdfabfd6933b8b6bbe431e4688cd..552e88675c7230e994e6594e88f98a64c2d5dbd7 100644 (file)
@@ -3,33 +3,33 @@
 
   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];
 }
@@ -37,10 +37,10 @@ Molecule::extent(Axis a) const
 Molecule::Molecule (Box b, SCM func)
 {
   expr_ = func;
-  dim_ = b ;
+  dim_ = b;
 }
 
-Molecule::Molecule()
+Molecule::Molecule ()
 {
   expr_ = SCM_EOL;
   set_empty (true);
@@ -52,7 +52,7 @@ Molecule::translate (Offset o)
   Axis a = X_AXIS;
   while (a < NO_AXES)
     {
-      if (abs(o[a]) > 30 CM
+      if (abs (o[a]) > 100 CM
          || isinf (o[a]) || isnan (o[a]))
        {
          programming_error ("Improbable offset for translation: setting to zero");
@@ -61,10 +61,10 @@ Molecule::translate (Offset o)
       incr (a);
     }
 
-  expr_ = gh_list (ly_symbol2scm ("translate-molecule"),
-                  to_scm (o),
+  expr_ = scm_list_n (ly_symbol2scm ("translate-molecule"),
+                  ly_offset2scm (o),
                   expr_, SCM_UNDEFINED);
-  if (!empty_b ())
+  if (!is_empty ())
     dim_.translate (o);
 }
   
@@ -72,7 +72,7 @@ Molecule::translate (Offset o)
 void
 Molecule::translate_axis (Real x,Axis a)
 {
-  Offset o(0,0);
+  Offset o (0,0);
   o[a] = x;
   translate (o);
 }  
@@ -82,7 +82,7 @@ Molecule::translate_axis (Real x,Axis a)
 void
 Molecule::add_molecule (Molecule const &m)
 {
-  expr_ = gh_list (ly_symbol2scm ("combine-molecule"),
+  expr_ = scm_list_n (ly_symbol2scm ("combine-molecule"),
                   m.expr_,
                   expr_, SCM_UNDEFINED);
   dim_.unite (m.dim_);
@@ -98,45 +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
-{
-#ifndef NPRINT
-  gh_display (expr_);
-#endif
-}
 
 void
-Molecule::align_to (Axis a, Direction d)
+Molecule::align_to (Axis a, Real x)
 {
+  if (is_empty ())
+    return ;
+
   Interval i (extent (a));
-  Real r =  (d == CENTER) ? i.center () : i[d];
-  translate_axis (-r, a);
+  translate_axis (-i.linear_combination (x), a);
 }
 
-
+/*
+  See scheme Function.
+ */
 void
-Molecule::add_at_edge (Axis a, Direction d, Molecule const &m, Real padding)
+Molecule::add_at_edge (Axis a, Direction d, Molecule const &m, Real padding,
+                      Real minimum)
 {
-  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 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; 
   
-  Real his_extent = i[-d];
-  Real offset = my_extent -  his_extent;
   Molecule toadd (m);
-  toadd.translate_axis (offset + d * padding, a);
+  toadd.translate_axis (offset, a);
   add_molecule (toadd);
 }
 
+
+
+/*
+  Hmm... maybe this is not such a good idea ; stuff can be empty,
+  while expr_ == '()
+ */
 bool
-Molecule::empty_b () const
+Molecule::is_empty () const
 {
   return expr_ == SCM_EOL;
 }
+
+SCM
+Molecule::get_expr () const
+{
+  return expr_;
+}
+
+
+
+Box
+Molecule::extent_box () const
+{
+  return dim_;
+}
+IMPLEMENT_SIMPLE_SMOBS (Molecule);
+
+
+int
+Molecule::print_smob (SCM , SCM port, scm_print_state *)
+{
+  scm_puts ("#<Molecule ", port);
+  scm_puts (" >", port);
+  
+  return 1;
+}
+
+  
+SCM
+Molecule::mark_smob (SCM s)
+{
+  Molecule  *r = (Molecule *) ly_cdr (s);
+  
+  return r->expr_;
+}
+
+IMPLEMENT_TYPE_P (Molecule, "ly:molecule?");
+IMPLEMENT_DEFAULT_EQUAL_P (Molecule);
+