source file of the GNU LilyPond music typesetter
- (c) 1997--1999 Han-Wen Nienhuys <hanwen@cs.uu.nl>
+ (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 "string.hh"
#include "molecule.hh"
-#include "atom.hh"
-#include "debug.hh"
+#include "warn.hh"
-#include "killing-cons.tcc"
-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];
}
+Molecule::Molecule (Box b, SCM func)
+{
+ expr_ = func;
+ dim_ = b;
+}
+
+Molecule::Molecule ()
+{
+ expr_ = SCM_EOL;
+ set_empty (true);
+}
+
void
Molecule::translate (Offset o)
{
- for (Cons<Atom> * ptr = atom_list_; ptr; ptr = ptr->next_)
+ Axis a = X_AXIS;
+ while (a < NO_AXES)
{
- ptr->car_->off_ += o;
+ 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_.translate (o);
+
+ expr_ = scm_list_n (ly_symbol2scm ("translate-molecule"),
+ ly_offset2scm (o),
+ expr_, SCM_UNDEFINED);
+ if (!is_empty ())
+ dim_.translate (o);
}
+
void
Molecule::translate_axis (Real x,Axis a)
{
- for (Cons<Atom> * ptr = atom_list_; ptr; ptr = ptr->next_)
- ptr->car_->off_[a] += x;
+ Offset o (0,0);
+ o[a] = x;
+ translate (o);
+}
+
- dim_[a] += x;
-}
void
Molecule::add_molecule (Molecule const &m)
{
- Cons_list<Atom> al (copy_killing_cons_list (m.atom_list_));
+ expr_ = scm_list_n (ly_symbol2scm ("combine-molecule"),
+ m.expr_,
+ expr_, SCM_UNDEFINED);
+ dim_.unite (m.dim_);
+}
- if (al.head_)
+void
+Molecule::set_empty (bool e)
+{
+ if (e)
{
- *al.tail_ = atom_list_;
- atom_list_ = al.head_;
+ dim_[X_AXIS].set_empty ();
+ dim_[Y_AXIS].set_empty ();
+ }
+ else
+ {
+ dim_[X_AXIS] = Interval (0,0);
+ dim_[Y_AXIS] = Interval (0,0);
}
- dim_.unite (m.dim_);
}
+
void
-Molecule::add_atom (Atom const *al)
+Molecule::align_to (Axis a, Real x)
{
- Atom *a = new Atom(*al);
+ if (is_empty ())
+ return ;
- atom_list_ = new Killing_cons<Atom> (a, atom_list_);
+ Interval i (extent (a));
+ translate_axis (-i.linear_combination (x), a);
}
+/*
+ See scheme Function.
+ */
void
-Molecule::operator=(Molecule const & src)
+Molecule::add_at_edge (Axis a, Direction d, Molecule const &m, Real padding,
+ Real minimum)
{
- if (&src == this) return;
- delete atom_list_;
- atom_list_ = 0;
- dim_= src.dim_;
- add_molecule (src);
-}
+ 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];
-Molecule::Molecule (Molecule const &s)
-{
- atom_list_ = 0;
- add_molecule (s);
+ 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
{
- delete atom_list_;
+ return expr_ == SCM_EOL;
}
-void
-Molecule::print() const
+SCM
+Molecule::get_expr () const
{
-#ifndef NPRINT
- if (! check_debug)
- return;
- DOUT << "dim:";
- for (Axis i=X_AXIS; i < NO_AXES; incr (i))
- DOUT << axis_name_str (i) << " = " << dim_[i].str ();
-#endif
+ return expr_;
}
-void
-Molecule::do_center (Axis a)
+
+
+Box
+Molecule::extent_box () const
{
- Interval i (extent (a));
- translate_axis (-i.center (), a);
+ return dim_;
}
+IMPLEMENT_SIMPLE_SMOBS (Molecule);
-Molecule::Molecule ()
+
+int
+Molecule::print_smob (SCM , SCM port, scm_print_state *)
{
- dim_ = Box (Interval(0,0),Interval( 0,0 ));
- atom_list_ = 0;
+ scm_puts ("#<Molecule ", port);
+ scm_puts (" >", port);
+
+ return 1;
}
-
-void
-Molecule::add_at_edge (Axis a, Direction d, Molecule const &m, Real padding)
+
+SCM
+Molecule::mark_smob (SCM s)
{
- Real my_extent= dim_[a][d];
+ Molecule *r = (Molecule *) ly_cdr (s);
- Real offset = my_extent - m.extent ()[a][-d];
- Molecule toadd (m);
- toadd.translate_axis (offset + d * padding, a);
- add_molecule (toadd);
+ return r->expr_;
}
+
+IMPLEMENT_TYPE_P (Molecule, "ly:molecule?");
+IMPLEMENT_DEFAULT_EQUAL_P (Molecule);
+