.ly version update.
[lilypond.git] / lily / molecule.cc
blobe05e6432638557525241858b14bb5ad08f9e7d78
1 /*
2 molecule.cc -- implement Molecule
4 source file of the GNU LilyPond music typesetter
6 (c) 1997--2003 Han-Wen Nienhuys <hanwen@cs.uu.nl>
7 */
9 #include <math.h>
10 #include <libc-extension.hh> // isinf
12 #include "font-metric.hh"
13 #include "dimensions.hh"
14 #include "interval.hh"
15 #include "string.hh"
16 #include "molecule.hh"
17 #include "warn.hh"
20 #include "ly-smobs.icc"
23 SCM
24 Molecule::smobbed_copy () const
26 Molecule * m = new Molecule (*this);
28 return m->smobbed_self ();
31 Interval
32 Molecule::extent (Axis a) const
34 return dim_[a];
37 Molecule::Molecule (Box b, SCM func)
39 expr_ = func;
40 dim_ = b;
43 Molecule::Molecule ()
45 expr_ = SCM_EOL;
46 set_empty (true);
49 void
50 Molecule::translate (Offset o)
52 Axis a = X_AXIS;
53 while (a < NO_AXES)
55 if (abs (o[a]) > 100 CM
56 || isinf (o[a]) || isnan (o[a]))
58 programming_error ("Improbable offset for translation: setting to zero");
59 o[a] = 0.0;
61 incr (a);
64 expr_ = scm_list_n (ly_symbol2scm ("translate-molecule"),
65 ly_offset2scm (o),
66 expr_, SCM_UNDEFINED);
67 if (!empty_b ())
68 dim_.translate (o);
72 void
73 Molecule::translate_axis (Real x,Axis a)
75 Offset o (0,0);
76 o[a] = x;
77 translate (o);
82 void
83 Molecule::add_molecule (Molecule const &m)
85 expr_ = scm_list_n (ly_symbol2scm ("combine-molecule"),
86 m.expr_,
87 expr_, SCM_UNDEFINED);
88 dim_.unite (m.dim_);
91 void
92 Molecule::set_empty (bool e)
94 if (e)
96 dim_[X_AXIS].set_empty ();
97 dim_[Y_AXIS].set_empty ();
99 else
101 dim_[X_AXIS] = Interval (0,0);
102 dim_[Y_AXIS] = Interval (0,0);
107 void
108 Molecule::align_to (Axis a, Real x)
110 if (empty_b())
111 return ;
113 Interval i (extent (a));
114 translate_axis (-i.linear_combination (x), a);
118 See scheme Function.
120 void
121 Molecule::add_at_edge (Axis a, Direction d, Molecule const &m, Real padding,
122 Real minimum)
124 Real my_extent= empty_b () ? 0.0 : dim_[a][d];
125 Interval i (m.extent (a));
126 Real his_extent;
127 if (i.empty_b ())
129 programming_error ("Molecule::add_at_edge: adding empty molecule.");
130 his_extent = 0.0;
132 else
133 his_extent = i[-d];
135 Real offset = (my_extent - his_extent) + d*padding;
136 if (minimum > 0 && fabs (offset) < minimum)
137 offset = sign (offset) * minimum;
139 Molecule toadd (m);
140 toadd.translate_axis (offset, a);
141 add_molecule (toadd);
147 Hmm... maybe this is not such a good idea ; stuff can be empty,
148 while expr_ == '()
150 bool
151 Molecule::empty_b () const
153 return expr_ == SCM_EOL;
157 Molecule::get_expr () const
159 return expr_;
165 Molecule::extent_box () const
167 return dim_;
169 IMPLEMENT_SIMPLE_SMOBS (Molecule);
173 Molecule::print_smob (SCM , SCM port, scm_print_state *)
175 scm_puts ("#<Molecule ", port);
176 #if 0
177 Molecule *r = (Molecule *) ly_cdr (s);
178 String string (r->to_string ());
179 scm_puts ((char *)str.to_str0 (), port);
180 #endif
181 scm_puts (" >", port);
183 return 1;
188 Molecule::mark_smob (SCM s)
190 Molecule *r = (Molecule *) ly_cdr (s);
192 return r->expr_;
195 IMPLEMENT_TYPE_P (Molecule, "ly:molecule?");
196 IMPLEMENT_DEFAULT_EQUAL_P (Molecule);