]> git.donarmstrong.com Git - lilypond.git/blobdiff - lily/molecule.cc
release: 1.3.141
[lilypond.git] / lily / molecule.cc
index c52fdc7b329b324a974f625bc2fce765b85095bc..5f1ced7f4975288518cf265a6b7d8a51b24909e8 100644 (file)
-#include "varray.hh"
+/*
+  molecule.cc -- implement Molecule
+
+  source file of the GNU LilyPond music typesetter
+
+  (c)  1997--2001 Han-Wen Nienhuys <hanwen@cs.uu.nl>
+*/
+
+#include <math.h>
+#include <libc-extension.hh>
+
+#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 "killing-cons.tcc"
 
-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;
 }
 
+Molecule::Molecule ()
+{
+  expr_ = SCM_EOL;
+  set_empty (true);
+}
 
-String
-Atom::TeXstring() const
+void
+Molecule::translate (Offset o)
 {
-    // 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);
+  Axis a = X_AXIS;
+  while (a < NO_AXES)
+    {
+      if (abs (o[a]) > 30 CM
+         || isinf (o[a]) || isnan (o[a]))
+       {
+         programming_error ("Improbable offset for translation: setting to zero");
+         o[a] =  0.0;
+       }
+      incr (a);
+    }
+
+  expr_ = gh_list (ly_symbol2scm ("translate-molecule"),
+                  ly_offset2scm (o),
+                  expr_, SCM_UNDEFINED);
+  if (!empty_b ())
+    dim_.translate (o);
 }
+  
+
+void
+Molecule::translate_axis (Real x,Axis a)
+{
+  Offset o (0,0);
+  o[a] = x;
+  translate (o);
+}  
 
-/* *************** */
 
-String
-Molecule::TeXstring() const
+
+void
+Molecule::add_molecule (Molecule const &m)
 {
-    String s;
-    for(iter_top(ats,c); c.ok(); c++)
-       s+=c->TeXstring();
-    return s;
+  expr_ = gh_list (ly_symbol2scm ("combine-molecule"),
+                  m.expr_,
+                  expr_, SCM_UNDEFINED);
+  dim_.unite (m.dim_);
 }
 
-Box
-Molecule::extent() const
+void
+Molecule::set_empty (bool e)
 {
-    Box b;
-    for(iter_top(ats,c); c.ok(); c++)
-       b.unite(c->extent());
-    return b;
+  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::translate(Offset o)
+Molecule::align_to (Axis a, Direction d)
 {
-    for (iter_top(ats,c); c.ok(); c++)
-       c->translate(o);
+  Interval i (extent (a));
+  Real r = (d == CENTER) ? i.center () : i[d];
+  translate_axis (-r, a);
 }
 
 void
-Molecule::add(const Molecule &m)
+Molecule::add_at_edge (Axis a, Direction d, Molecule const &m, Real padding)
 {
-    for (iter_top(m.ats,c); c.ok(); c++) {
-       add(**c);
+  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;
     }
+  else
+    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::add_right(const Molecule &m)
+
+
+
+bool
+number_pair_p (SCM p)
+{
+  return gh_pair_p (p) && gh_number_p (gh_car (p)) && gh_number_p (gh_cdr (p));
+}
+
+bool
+axis_p (SCM a)
+{
+  return gh_number_p (a) && (gh_scm2int (a) == 0 || gh_scm2int (a) == 1); 
+}
+
+SCM
+Molecule::ly_set_molecule_extent_x (SCM mol, SCM axis, SCM np)
 {
-     if (!ats.size()) {
-       add(m);
-       return;
+  Molecule* m = unsmob_molecule (mol);
+  if (m && axis_p (axis) && number_pair_p (np))
+    {
+      Interval iv = ly_scm2interval (np);
+      m->dim_[Axis (gh_scm2int (axis))] = ly_scm2interval (np);
     }
-   Real xof=extent().x.right - m.extent().x.left;
-    Molecule toadd(m);
-    toadd.translate(Offset(xof, 0.0));
-    add(toadd);
+  else
+    warning ("ly-set-molecule-extent!: invalid arguments");
+  return SCM_UNDEFINED;
 }
 
-void
-Molecule::add_left(const Molecule &m)
+SCM
+Molecule::ly_get_molecule_extent (SCM mol, SCM axis)
 {
-    if (!ats.size()) {
-       add(m);
-       return;
+  Molecule *m = unsmob_molecule (mol);
+  if (!m || !axis_p (axis))
+    {
+      warning ("ly-get-molecule-extent: invalid arguments");
+      return ly_interval2scm (Interval (0,0));
     }
-    Real xof=extent().x.left - m.extent().x.right;
-    Molecule toadd(m);
-    toadd.translate(Offset(xof, 0.0));
-    add(toadd);
+
+  return ly_interval2scm (m->extent (Axis (gh_scm2int (axis))));
 }
 
 
-void
-Molecule::add_top(const Molecule &m)
+SCM
+Molecule::ly_molecule_combined_at_edge (SCM first, SCM axis, SCM direction,
+                          SCM second, SCM padding)
+
 {
-      if (!ats.size()) {
-       add(m);
-       return;
+  Molecule * m1 = unsmob_molecule (first);
+  Molecule * m2 = unsmob_molecule (second);
+  Molecule result;
+  
+  if (!m1 || !m2 || !isdir_b (direction) || !axis_p (axis) || !gh_number_p (padding))
+    {
+      warning ("ly-combine-molecule-at-edge: invalid arguments");
+      Molecule r;
+      return  r.smobbed_copy ();
     }
-  Real yof=extent().y.right - m.extent().y.left;
-    Molecule toadd(m);
-    toadd.translate(Offset(0,yof));
-    add(toadd);
+
+  result = *m1;
+
+  result.add_at_edge (Axis (gh_scm2int (axis)), Direction (gh_scm2int (direction)),
+                     *m2, gh_scm2double (padding));
+
+  return result.smobbed_copy ();
 }
 
-void
-Molecule::add_bottom(const Molecule &m)
+
+SCM
+make_molecule (SCM expr, SCM xext, SCM yext)
 {
-    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);
+  /*
+    TODO: typechecking. 
+   */
+  Box b (ly_scm2interval (xext), ly_scm2interval(yext));
+  Molecule m (b, expr);
+  return m.smobbed_copy ();
 }
 
-void
-Molecule::operator = (const Molecule&)
+
+static void
+molecule_init ()
 {
-    assert(false);
+  scm_make_gsubr ("ly-make-molecule", 3, 0, 0, (Scheme_function_unknown) make_molecule);
+  scm_make_gsubr ("ly-combine-molecule-at-edge", 5 , 0, 0, (Scheme_function_unknown) Molecule::ly_molecule_combined_at_edge);
+  scm_make_gsubr ("ly-set-molecule-extent!", 3 , 0, 0, (Scheme_function_unknown) Molecule::ly_set_molecule_extent_x);
+  scm_make_gsubr ("ly-get-molecule-extent", 2 , 0, 0, (Scheme_function_unknown) Molecule::ly_get_molecule_extent);
 }
+ADD_SCM_INIT_FUNC (molecule,molecule_init);
+
 
-Molecule::Molecule(const Molecule&s)
+bool
+Molecule::empty_b () const
 {
-    add(s);
+  return expr_ == SCM_EOL;
 }
 
-void
-Molecule::print() const
+SCM
+fontify_atom (Font_metric * met, SCM f)
 {
-    for (iter_top(ats,c); c.ok(); c++)
-       c->print();
+  if (f == SCM_EOL)
+    return f;
+  else
+    return  gh_list (ly_symbol2scm ("fontify"),
+                    ly_quote_scm (met->description_), f, SCM_UNDEFINED);
 }
+
+SCM
+Molecule::get_expr () const
+{
+  return expr_;
+}
+
+
+
+Box
+Molecule::extent_box () const
+{
+  return dim_;
+}
+IMPLEMENT_SIMPLE_SMOBS (Molecule);
+
+
+int
+Molecule::print_smob (SCM s, SCM port, scm_print_state *)
+{
+  Molecule  *r = (Molecule *) gh_cdr (s);
+     
+  scm_puts ("#<Molecule ", port);
+  /*  String str (r->str ());
+  scm_puts ((char *)str.ch_C (), port);*/
+  scm_puts (" >", port);
+  
+  return 1;
+}
+
+  
+SCM
+Molecule::mark_smob (SCM s)
+{
+  Molecule  *r = (Molecule *) gh_cdr (s);
+  
+  return r->expr_;
+}
+
+IMPLEMENT_TYPE_P (Molecule, "molecule?");
+IMPLEMENT_DEFAULT_EQUAL_P (Molecule);
+IMPLEMENT_UNSMOB (Molecule, molecule);