]> git.donarmstrong.com Git - lilypond.git/blobdiff - lily/molecule.cc
2003 -> 2004
[lilypond.git] / lily / molecule.cc
index ff047f2c771f89a4bbce7469a21b76f3cfc2105a..552e88675c7230e994e6594e88f98a64c2d5dbd7 100644 (file)
-#include "varray.hh"
+/*
+  molecule.cc -- implement Molecule
+
+  source file of the GNU LilyPond music typesetter
+
+  (c) 1997--2004 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"
 
-void
-Atom::print() const
+
+#include "ly-smobs.icc"
+
+
+SCM
+Molecule::smobbed_copy () const
 {
-    mtor << "texstring: " <<sym.tex<<"\n";    
+  Molecule * m = new Molecule (*this);
+
+  return m->smobbed_self ();
 }
 
-Box
-Atom::extent() const
+Interval
+Molecule::extent (Axis a) const
 {
-    Box b( sym.dim);
-    b.translate(off);
-    return b;
+  return dim_[a];
 }
 
-Atom::Atom(Symbol s)
+Molecule::Molecule (Box b, SCM func)
 {
-    sym=s;
+  expr_ = func;
+  dim_ = b;
 }
 
-
-String
-Atom::TeX_string() const
+Molecule::Molecule ()
 {
-    /* infinity checks. */
-    assert( abs(off.x) < 100 CM);
-    assert( abs(off.y) < 100 CM);
-    
-    // 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);
+  expr_ = SCM_EOL;
+  set_empty (true);
 }
 
-/* *************** */
-
-String
-Molecule::TeX_string() const
+void
+Molecule::translate (Offset o)
 {
-    String s;
-    for(iter_top(ats,c); c.ok(); c++)
-       s+=c->TeX_string();
-    return s;
+  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 (!is_empty ())
+    dim_.translate (o);
 }
+  
 
-Box
-Molecule::extent() const
+void
+Molecule::translate_axis (Real x,Axis a)
 {
-    Box b;
-    for(iter_top(ats,c); c.ok(); c++)
-       b.unite(c->extent());
-    return b;
-}
+  Offset o (0,0);
+  o[a] = x;
+  translate (o);
+}  
+
+
 
 void
-Molecule::translate(Offset o)
+Molecule::add_molecule (Molecule const &m)
 {
-    for (iter_top(ats,c); c.ok(); c++)
-       c->translate(o);
+  expr_ = scm_list_n (ly_symbol2scm ("combine-molecule"),
+                  m.expr_,
+                  expr_, SCM_UNDEFINED);
+  dim_.unite (m.dim_);
 }
 
 void
-Molecule::add(Molecule const &m)
+Molecule::set_empty (bool e)
 {
-    for (iter_top(m.ats,c); c.ok(); c++) {
-       add(**c);
+  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);
     }
 }
 
+
 void
-Molecule::add_right(Molecule const &m)
+Molecule::align_to (Axis a, Real x)
 {
-     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);
+  if (is_empty ())
+    return ;
+
+  Interval i (extent (a));
+  translate_axis (-i.linear_combination (x), a);
 }
 
+/*
+  See scheme Function.
+ */
 void
-Molecule::add_left(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= 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;
     }
-    Real xof=extent().x.left - m.extent().x.right;
-    Molecule toadd(m);
-    toadd.translate(Offset(xof, 0.0));
-    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::add_top(Molecule const &m)
+
+/*
+  Hmm... maybe this is not such a good idea ; stuff can be empty,
+  while expr_ == '()
+ */
+bool
+Molecule::is_empty () const
 {
-      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);
+  return expr_ == SCM_EOL;
 }
 
-void
-Molecule::add_bottom(Molecule const &m)
+SCM
+Molecule::get_expr () const
 {
-    if (!ats.size()) {
-       add(m);
-       return;
-    }
-    Real yof=extent().y.left- m.extent().y.right;
-    Molecule toadd(m);
-    toadd.translate(Offset(0,yof));
-    add(toadd);
+  return expr_;
 }
 
-void
-Molecule::operator = (Molecule const &)
+
+
+Box
+Molecule::extent_box () const
 {
-    assert(false);
+  return dim_;
 }
+IMPLEMENT_SIMPLE_SMOBS (Molecule);
 
-Molecule::Molecule(Molecule const &s)
+
+int
+Molecule::print_smob (SCM , SCM port, scm_print_state *)
 {
-    add(s);
+  scm_puts ("#<Molecule ", port);
+  scm_puts (" >", port);
+  
+  return 1;
 }
 
-void
-Molecule::print() const
+  
+SCM
+Molecule::mark_smob (SCM s)
 {
-    for (iter_top(ats,c); c.ok(); c++)
-       c->print();
+  Molecule  *r = (Molecule *) ly_cdr (s);
+  
+  return r->expr_;
 }
+
+IMPLEMENT_TYPE_P (Molecule, "ly:molecule?");
+IMPLEMENT_DEFAULT_EQUAL_P (Molecule);
+