lilypond-1.3.28
[lilypond.git] / lily / molecule.cc
blobf447d542d6e44738d8e117eebf5bca1addc56668
1 /*
2 molecule.cc -- implement Molecule
4 source file of the GNU LilyPond music typesetter
6 (c) 1997--2000 Han-Wen Nienhuys <hanwen@cs.uu.nl>
7 */
9 /*
10 ugh. Rewrite not finished yet. Still must copy atom lists.
13 #include <math.h>
15 #include "interval.hh"
16 #include "string.hh"
17 #include "molecule.hh"
18 #include "atom.hh"
19 #include "debug.hh"
20 #include "killing-cons.tcc"
23 Box
24 Molecule::extent() const
26 return dim_;
29 Interval
30 Molecule::extent(Axis a) const
32 return dim_[a];
35 void
36 Molecule::translate (Offset o)
38 if (isinf (o.length ()))
40 programming_error ("Translating infinitely. Ignore.");
41 return;
44 for (SCM ptr = gh_cdr (atom_list_); ptr != SCM_EOL; ptr = gh_cdr(ptr))
46 gh_set_car_x (ptr, translate_atom (o, gh_car (ptr)));
48 if (!empty_b ())
49 dim_.translate (o);
52 void
53 Molecule::translate_axis (Real x,Axis a)
55 if (isinf (x))
57 programming_error ("Translating infinitely. Ignore.");
58 return;
60 for (SCM ptr = gh_cdr (atom_list_); ptr != SCM_EOL; ptr = gh_cdr(ptr))
62 gh_set_car_x (ptr, translate_atom_axis (x, a, gh_car (ptr)));
65 if (!dim_[a].empty_b ())
66 dim_[a] += x;
69 void
70 Molecule::add_molecule (Molecule const &m)
72 for (SCM ptr = gh_cdr (m.atom_list_); ptr != SCM_EOL; ptr = gh_cdr(ptr))
74 add_atom (gh_car (ptr));
76 dim_.unite (m.dim_);
79 void
80 Molecule::add_atom (SCM atomsmob)
82 gh_set_cdr_x (atom_list_,
83 gh_cons (atomsmob, gh_cdr (atom_list_)));
86 void
87 Molecule::operator=(Molecule const & src)
89 if (&src == this)
90 return;
92 atom_list_ = gh_cons (SCM_EOL,scm_list_copy (gh_cdr (src.atom_list_)));
93 dim_= src.dim_;
96 void
97 Molecule::set_empty (bool e)
99 if (e)
101 dim_[X_AXIS].set_empty ();
102 dim_[Y_AXIS].set_empty ();
104 else
106 dim_[X_AXIS] = Interval(0,0);
107 dim_[Y_AXIS] = Interval (0,0);
111 void
112 Molecule::print () const
114 #ifndef NPRINT
115 for (SCM ptr = gh_cdr (atom_list_); ptr != SCM_EOL; ptr = gh_cdr(ptr))
116 gh_display (gh_car (ptr));
117 #endif
120 Molecule::Molecule (Molecule const &s)
122 atom_list_ = gh_cons (SCM_EOL, scm_list_copy (gh_cdr (s.atom_list_)));
123 dim_ = s.dim_;
126 Molecule::~Molecule ()
131 void
132 Molecule::align_to (Axis a, Direction d)
134 if (d == CENTER)
136 Interval i (extent (a));
137 translate_axis (-i.center (), a);
139 else
141 translate_axis (-extent (a)[d], a);
145 Molecule::Molecule ()
147 dim_[X_AXIS].set_empty ();
148 dim_[Y_AXIS].set_empty ();
149 atom_list_ = gh_cons (SCM_EOL, SCM_EOL);
153 void
154 Molecule::add_at_edge (Axis a, Direction d, Molecule const &m, Real padding)
156 Real my_extent= empty_b () ? 0.0 : dim_[a][d];
157 Interval i (m.extent ()[a]);
158 if (i.empty_b ())
159 programming_error ("Molecule::add_at_edge: adding empty molecule.");
161 Real his_extent = i[-d];
162 Real offset = my_extent - his_extent;
163 Molecule toadd (m);
164 toadd.translate_axis (offset + d * padding, a);
165 add_molecule (toadd);
168 bool
169 Molecule::empty_b () const
171 return gh_cdr (atom_list_) == SCM_EOL;