lilypond-1.3.31
[lilypond.git] / lily / molecule.cc
blob5620a92d180e07aeccd5580b4a54439c76d56ccd
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.
14 #include <math.h>
16 #include "font-metric.hh"
17 #include "dimensions.hh"
18 #include "interval.hh"
19 #include "string.hh"
20 #include "molecule.hh"
22 #include "debug.hh"
23 #include "killing-cons.tcc"
26 Box
27 Molecule::extent() const
29 return dim_;
32 Interval
33 Molecule::extent(Axis a) const
35 return dim_[a];
38 Molecule::Molecule (Box b, SCM func)
40 expr_ = func;
41 dim_ = b ;
44 Molecule::Molecule()
46 expr_ = SCM_EOL;
47 set_empty (true);
50 void
51 Molecule::translate (Offset o)
53 Axis a = X_AXIS;
54 while (a < NO_AXES)
56 if (abs(o[a]) > 30 CM
57 || isinf (o[a]) || isnan (o[a]))
59 programming_error ("Improbable offset for translation: setting to zero");
60 o[a] = 0.0;
62 incr (a);
65 expr_ = gh_list (ly_symbol2scm ("translate-molecule"),
66 to_scm (o),
67 expr_, SCM_UNDEFINED);
68 if (!empty_b ())
69 dim_.translate (o);
73 void
74 Molecule::translate_axis (Real x,Axis a)
76 Offset o(0,0);
77 o[a] = x;
78 translate (o);
83 void
84 Molecule::add_molecule (Molecule const &m)
86 expr_ = gh_list (ly_symbol2scm ("combine-molecule"),
87 m.expr_,
88 expr_, SCM_UNDEFINED);
89 dim_.unite (m.dim_);
92 void
93 Molecule::set_empty (bool e)
95 if (e)
97 dim_[X_AXIS].set_empty ();
98 dim_[Y_AXIS].set_empty ();
100 else
102 dim_[X_AXIS] = Interval(0,0);
103 dim_[Y_AXIS] = Interval (0,0);
107 void
108 Molecule::print () const
110 #ifndef NPRINT
111 gh_display (expr_);
112 #endif
115 void
116 Molecule::align_to (Axis a, Direction d)
118 Interval i (extent (a));
119 Real r = (d == CENTER) ? i.center () : i[d];
120 translate_axis (-r, a);
124 void
125 Molecule::add_at_edge (Axis a, Direction d, Molecule const &m, Real padding)
127 Real my_extent= empty_b () ? 0.0 : dim_[a][d];
128 Interval i (m.extent ()[a]);
129 if (i.empty_b ())
130 programming_error ("Molecule::add_at_edge: adding empty molecule.");
132 Real his_extent = i[-d];
133 Real offset = my_extent - his_extent;
134 Molecule toadd (m);
135 toadd.translate_axis (offset + d * padding, a);
136 add_molecule (toadd);
139 bool
140 Molecule::empty_b () const
142 return expr_ == SCM_EOL;
147 fontify_atom(Font_metric * met, SCM f)
149 return gh_list (ly_symbol2scm ("fontify"),
150 ly_quote_scm (met->description ()), f, SCM_UNDEFINED);