2 molecule.cc -- implement Molecule
4 source file of the GNU LilyPond music typesetter
6 (c) 1997--2002 Han-Wen Nienhuys <hanwen@cs.uu.nl>
10 #include <libc-extension.hh> // isinf
12 #include "font-metric.hh"
13 #include "dimensions.hh"
14 #include "interval.hh"
16 #include "molecule.hh"
20 #include "ly-smobs.icc"
24 Molecule::smobbed_copy () const
26 Molecule * m = new Molecule (*this);
28 return m->smobbed_self ();
32 Molecule::extent (Axis a) const
37 Molecule::Molecule (Box b, SCM func)
50 Molecule::translate (Offset o)
55 if (abs (o[a]) > 100 CM
56 || isinf (o[a]) || isnan (o[a]))
58 programming_error ("Improbable offset for translation: setting to zero");
64 expr_ = scm_list_n (ly_symbol2scm ("translate-molecule"),
66 expr_, SCM_UNDEFINED);
73 Molecule::translate_axis (Real x,Axis a)
83 Molecule::add_molecule (Molecule const &m)
85 expr_ = scm_list_n (ly_symbol2scm ("combine-molecule"),
87 expr_, SCM_UNDEFINED);
92 Molecule::set_empty (bool e)
96 dim_[X_AXIS].set_empty ();
97 dim_[Y_AXIS].set_empty ();
101 dim_[X_AXIS] = Interval (0,0);
102 dim_[Y_AXIS] = Interval (0,0);
108 Molecule::align_to (Axis a, Direction d)
113 Interval i (extent (a));
114 Real r = (d == CENTER) ? i.center () : i[d];
115 translate_axis (-r, a);
119 Molecule::add_at_edge (Axis a, Direction d, Molecule const &m, Real padding)
121 Real my_extent= empty_b () ? 0.0 : dim_[a][d];
122 Interval i (m.extent (a));
126 programming_error ("Molecule::add_at_edge: adding empty molecule.");
132 Real offset = my_extent - his_extent;
134 toadd.translate_axis (offset + d * padding, a);
135 add_molecule (toadd);
138 LY_DEFINE(ly_set_molecule_extent_x,"ly:set-molecule-extent!", 3 , 0, 0,
139 (SCM mol, SCM axis, SCM np),
140 "Set the extent (@var{extent} must be a pair of numbers) of @var{mol} in \n"
141 "@var{axis} direction (0 or 1 for x- and y-axis respectively).\n"
143 "Note that an extent @code{(A . B)} is an interval and hence @code{A} is\n"
144 "smaller than @code{B}, and is often negative.\n"
147 Molecule* m = unsmob_molecule (mol);
148 SCM_ASSERT_TYPE (m, mol, SCM_ARG1, __FUNCTION__, "molecule");
149 SCM_ASSERT_TYPE (ly_axis_p (axis), axis, SCM_ARG2, __FUNCTION__, "axis");
150 SCM_ASSERT_TYPE (ly_number_pair_p (np), np, SCM_ARG3, __FUNCTION__, "number pair");
152 Interval iv = ly_scm2interval (np);
153 m->dim_[Axis (gh_scm2int (axis))] = iv;
155 return SCM_UNDEFINED;
158 LY_DEFINE(ly_get_molecule_extent,
159 "ly:get-molecule-extent", 2 , 0, 0, (SCM mol, SCM axis),
160 "Return a pair of numbers signifying the extent of @var{mol} in "
161 "@var{axis} direction (0 or 1 for x and y axis respectively)."
164 Molecule *m = unsmob_molecule (mol);
165 SCM_ASSERT_TYPE (m, mol, SCM_ARG1, __FUNCTION__, "molecule");
166 SCM_ASSERT_TYPE (ly_axis_p (axis), axis, SCM_ARG2, __FUNCTION__, "axis");
168 return ly_interval2scm (m->extent (Axis (gh_scm2int (axis))));
172 LY_DEFINE(ly_molecule_combined_at_edge,
173 "ly:combine-molecule-at-edge",
174 5 , 0, 0, (SCM first, SCM axis, SCM direction,
175 SCM second, SCM padding),
176 "Construct a molecule by putting @var{second} next to "
177 "@var{first}. @var{axis} can be 0 (x-axis) or 1 (y-axis), @var{direction} can be "
178 "-1 (left or down) or 1 (right or up). @var{padding} specifies extra "
179 "space to add in between measured in global staff space.")
182 Molecule * m1 = unsmob_molecule (first);
183 Molecule * m2 = unsmob_molecule (second);
187 SCM_ASSERT_TYPE(ly_axis_p (axis), axis, SCM_ARG2, __FUNCTION__, "axis");
188 SCM_ASSERT_TYPE(ly_dir_p (direction), direction, SCM_ARG3, __FUNCTION__, "dir");
189 SCM_ASSERT_TYPE(gh_number_p (padding), padding, SCM_ARG4, __FUNCTION__, "number");
194 result.add_at_edge (Axis (gh_scm2int (axis)), Direction (gh_scm2int (direction)),
195 *m2, gh_scm2double (padding));
197 return result.smobbed_copy ();
201 FIXME: support variable number of arguments "
203 LY_DEFINE(ly_add_molecule ,
204 "ly:add-molecule", 2, 0,0,(SCM first, SCM second),
205 "Combine two molecules."
208 Molecule * m1 = unsmob_molecule (first);
209 Molecule * m2 = unsmob_molecule (second);
216 result.add_molecule (*m2);
218 return result.smobbed_copy ();
221 LY_DEFINE(ly_make_molecule,
222 "ly:make-molecule", 3, 0, 0, (SCM expr, SCM xext, SCM yext),
224 "The objective of any typesetting system is to put ink on paper in the \n"
225 "right places. For LilyPond, this final stage is left to the @TeX{} and \n"
226 "the printer subsystem. For lily, the last stage in processing a score is \n"
227 "outputting a description of what to put where. This description roughly \n"
230 " PUT glyph AT (x,y) \n"
231 " PUT glyph AT (x,y) \n"
232 " PUT glyph AT (x,y) \n"
234 "you merely have to look at the tex output of lily to see this. \n"
235 "Internally these instructions are encoded in Molecules.@footnote{At some \n"
236 "point LilyPond also contained Atom-objects, but they have been replaced \n"
237 "by Scheme expressions, making the name outdated.} A molecule is \n"
238 "what-to-print-where information that also contains dimension information \n"
239 "(how large is this glyph?). \n"
241 "Conceptually, Molecules can be constructed from Scheme code, by \n"
242 "translating a Molecule and by combining two molecules. In BNF \n"
246 "Molecule :: COMBINE Molecule Molecule \n"
247 " | TRANSLATE Offset Molecule \n"
248 " | GLYPH-DESCRIPTION \n"
252 "If you are interested in seeing how this information is stored, you \n"
253 "can run with the @code{-f scm} option. The scheme expressions are then \n"
254 "dumped in the output file.")
256 SCM_ASSERT_TYPE (ly_number_pair_p (xext), xext, SCM_ARG2, __FUNCTION__, "number pair");
257 SCM_ASSERT_TYPE (ly_number_pair_p (yext), yext, SCM_ARG3, __FUNCTION__, "number pair");
259 Box b (ly_scm2interval (xext), ly_scm2interval(yext));
260 Molecule m (b, expr);
261 return m.smobbed_copy ();
265 fontify_atom (Font_metric const * met, SCM f)
270 return scm_list_n (ly_symbol2scm ("fontify"),
271 ly_quote_scm (met->description_), f, SCM_UNDEFINED);
274 LY_DEFINE(ly_fontify_atom,"ly:fontify-atom", 2, 0, 0,
276 "Add a font selection command for the font metric @var{met} to @var{f}.")
278 SCM_ASSERT_TYPE(unsmob_metrics (met), met, SCM_ARG1, __FUNCTION__, "font metric");
280 return fontify_atom (unsmob_metrics (met), f);
282 LY_DEFINE(ly_align_to_x,"ly:align-to!", 3, 0, 0, (SCM mol, SCM axis, SCM dir),
283 "Align @var{mol} using its own extents.")
285 SCM_ASSERT_TYPE(unsmob_molecule (mol), mol, SCM_ARG1, __FUNCTION__, "molecule");
286 SCM_ASSERT_TYPE(ly_axis_p (axis), axis, SCM_ARG2, __FUNCTION__, "axis");
287 SCM_ASSERT_TYPE(ly_dir_p (dir), dir, SCM_ARG3, __FUNCTION__, "dir");
289 unsmob_molecule (mol)->align_to ((Axis)gh_scm2int (axis), Direction (gh_scm2int (dir)));
291 return SCM_UNDEFINED;
297 Hmm... maybe this is not such a good idea ; stuff can be empty,
301 Molecule::empty_b () const
303 return expr_ == SCM_EOL;
307 Molecule::get_expr () const
315 Molecule::extent_box () const
319 IMPLEMENT_SIMPLE_SMOBS (Molecule);
323 Molecule::print_smob (SCM , SCM port, scm_print_state *)
325 scm_puts ("#<Molecule ", port);
327 Molecule *r = (Molecule *) ly_cdr (s);
328 String string (r->string ());
329 scm_puts ((char *)str.to_str0 (), port);
331 scm_puts (" >", port);
338 Molecule::mark_smob (SCM s)
340 Molecule *r = (Molecule *) ly_cdr (s);
345 IMPLEMENT_TYPE_P (Molecule, "ly:molecule?");
346 IMPLEMENT_DEFAULT_EQUAL_P (Molecule);