-#include "varray.hh"
+/*
+ molecule.cc -- implement Molecule
+
+ source file of the GNU LilyPond music typesetter
+
+ (c) 1997--2000 Han-Wen Nienhuys <hanwen@cs.uu.nl>
+*/
+
+/*
+ ugh. Rewrite not finished yet. Still must copy atom lists.
+ */
+
+#include <math.h>
+
#include "interval.hh"
-#include "dimen.hh"
#include "string.hh"
#include "molecule.hh"
-#include "symbol.hh"
+#include "atom.hh"
#include "debug.hh"
-#include "tex.hh"
+#include "killing-cons.tcc"
-void
-Atom::print() const
-{
- mtor << "texstring: " <<sym.tex<<"\n";
-}
Box
-Atom::extent() const
+Molecule::extent() const
{
- Box b( sym.dim);
- b.translate(off);
- return b;
+ return dim_;
}
-Atom::Atom(Symbol s)
+Interval
+Molecule::extent(Axis a) const
{
- sym=s;
+ return dim_[a];
}
-
-String
-Atom::TeX_string() const
+void
+Molecule::translate (Offset o)
{
- /* infinity checks. */
- assert( abs(off.x) < 100 CM);
- assert( abs(off.y) < 100 CM);
+ if (isinf (o.length ()))
+ {
+ programming_error ("Translating infinitely. Ignore.");
+ return;
+ }
- // 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);
+ 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);
}
-/* *************** */
-
-String
-Molecule::TeX_string() const
+void
+Molecule::translate_axis (Real x,Axis a)
{
- String s;
- for(iter_top(ats,c); c.ok(); c++)
- s+=c->TeX_string();
- return s;
+ 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;
}
-Box
-Molecule::extent() const
+void
+Molecule::add_molecule (Molecule const &m)
{
- Box b;
- for(iter_top(ats,c); c.ok(); c++)
- b.unite(c->extent());
- return b;
+ for (SCM ptr = gh_cdr (m.atom_list_); ptr != SCM_EOL; ptr = gh_cdr(ptr))
+ {
+ add_atom (gh_car (ptr));
+ }
+ dim_.unite (m.dim_);
}
void
-Molecule::translate(Offset o)
+Molecule::add_atom (SCM atomsmob)
{
- for (iter_top(ats,c); c.ok(); c++)
- c->translate(o);
+ gh_set_cdr_x (atom_list_,
+ gh_cons (atomsmob, gh_cdr (atom_list_)));
}
void
-Molecule::add(Molecule const &m)
+Molecule::operator=(Molecule const & src)
{
- for (iter_top(m.ats,c); c.ok(); c++) {
- add(**c);
- }
+ if (&src == this)
+ return;
+
+ atom_list_ = gh_cons (SCM_EOL,scm_list_copy (gh_cdr (src.atom_list_)));
+ dim_= src.dim_;
}
void
-Molecule::add_right(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.right - m.extent().x.left;
- Molecule toadd(m);
- toadd.translate(Offset(xof, 0.0));
- add(toadd);
}
void
-Molecule::add_left(Molecule const &m)
+Molecule::print () const
{
- if (!ats.size()) {
- add(m);
- return;
- }
- Real xof=extent().x.left - m.extent().x.right;
- Molecule toadd(m);
- toadd.translate(Offset(xof, 0.0));
- add(toadd);
+#ifndef NPRINT
+ for (SCM ptr = gh_cdr (atom_list_); ptr != SCM_EOL; ptr = gh_cdr(ptr))
+ gh_display (gh_car (ptr));
+#endif
}
+Molecule::Molecule (Molecule const &s)
+{
+ atom_list_ = gh_cons (SCM_EOL, scm_list_copy (gh_cdr (s.atom_list_)));
+ dim_ = s.dim_;
+}
-void
-Molecule::add_top(Molecule const &m)
+Molecule::~Molecule ()
{
- 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);
}
+
void
-Molecule::add_bottom(Molecule const &m)
+Molecule::align_to (Axis a, Direction d)
{
- if (!ats.size()) {
- add(m);
- return;
+ if (d == CENTER)
+ {
+ Interval i (extent (a));
+ translate_axis (-i.center (), a);
+ }
+ else
+ {
+ translate_axis (-extent (a)[d], a);
}
- Real yof=extent().y.left- m.extent().y.right;
- Molecule toadd(m);
- toadd.translate(Offset(0,yof));
- add(toadd);
}
-void
-Molecule::operator = (Molecule const &)
+Molecule::Molecule ()
{
- assert(false);
+ dim_[X_AXIS].set_empty ();
+ dim_[Y_AXIS].set_empty ();
+ atom_list_ = gh_cons (SCM_EOL, SCM_EOL);
}
-Molecule::Molecule(Molecule const &s)
+
+void
+Molecule::add_at_edge (Axis a, Direction d, Molecule const &m, Real padding)
{
- add(s);
+ 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 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::print() const
+bool
+Molecule::empty_b () const
{
- for (iter_top(ats,c); c.ok(); c++)
- c->print();
+ return gh_cdr (atom_list_) == SCM_EOL;
}