2 molecule.cc -- implement Molecule
4 source file of the GNU LilyPond music typesetter
6 (c) 1997--2001 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
]) > 30 CM
56 || isinf (o
[a
]) || isnan (o
[a
]))
58 programming_error ("Improbable offset for translation: setting to zero");
64 expr_
= gh_list (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_
= gh_list (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
)
110 Interval
i (extent (a
));
111 Real r
= (d
== CENTER
) ? i
.center () : i
[d
];
112 translate_axis (-r
, a
);
116 Molecule::add_at_edge (Axis a
, Direction d
, Molecule
const &m
, Real padding
)
118 Real my_extent
= empty_b () ? 0.0 : dim_
[a
][d
];
119 Interval
i (m
.extent (a
));
123 programming_error ("Molecule::add_at_edge: adding empty molecule.");
129 Real offset
= my_extent
- his_extent
;
131 toadd
.translate_axis (offset
+ d
* padding
, a
);
132 add_molecule (toadd
);
135 /* ly_? Thought we had the ly_ prefix for wrapping/adding to gh_ */
137 Molecule::ly_set_molecule_extent_x (SCM mol
, SCM axis
, SCM np
)
139 Molecule
* m
= unsmob_molecule (mol
);
140 if (m
&& ly_axis_p (axis
) && ly_number_pair_p (np
))
142 Interval iv
= ly_scm2interval (np
);
143 m
->dim_
[Axis (gh_scm2int (axis
))] = ly_scm2interval (np
);
146 warning ("ly-set-molecule-extent!: invalid arguments");
147 return SCM_UNDEFINED
;
151 Molecule::ly_get_molecule_extent (SCM mol
, SCM axis
)
153 Molecule
*m
= unsmob_molecule (mol
);
155 if (!m
|| !ly_axis_p (axis
))
157 warning ("ly-get-molecule-extent: invalid arguments");
158 return ly_interval2scm (Interval (0,0));
161 return ly_interval2scm (m
->extent (Axis (gh_scm2int (axis
))));
166 Molecule::ly_molecule_combined_at_edge (SCM first
, SCM axis
, SCM direction
,
167 SCM second
, SCM padding
)
170 Molecule
* m1
= unsmob_molecule (first
);
171 Molecule
* m2
= unsmob_molecule (second
);
174 if (!m1
|| !m2
|| !isdir_b (direction
) || !ly_axis_p (axis
) || !gh_number_p (padding
))
176 warning ("ly-combine-molecule-at-edge: invalid arguments");
178 return r
.smobbed_copy ();
183 result
.add_at_edge (Axis (gh_scm2int (axis
)), Direction (gh_scm2int (direction
)),
184 *m2
, gh_scm2double (padding
));
186 return result
.smobbed_copy ();
191 make_molecule (SCM expr
, SCM xext
, SCM yext
)
196 Box
b (ly_scm2interval (xext
), ly_scm2interval(yext
));
197 Molecule
m (b
, expr
);
198 return m
.smobbed_copy ();
205 scm_c_define_gsubr ("ly-make-molecule", 3, 0, 0, (Scheme_function_unknown
) make_molecule
);
206 scm_c_define_gsubr ("ly-combine-molecule-at-edge", 5 , 0, 0, (Scheme_function_unknown
) Molecule::ly_molecule_combined_at_edge
);
207 scm_c_define_gsubr ("ly-set-molecule-extent!", 3 , 0, 0, (Scheme_function_unknown
) Molecule::ly_set_molecule_extent_x
);
208 scm_c_define_gsubr ("ly-get-molecule-extent", 2 , 0, 0, (Scheme_function_unknown
) Molecule::ly_get_molecule_extent
);
210 ADD_SCM_INIT_FUNC (molecule
,molecule_init
);
214 Molecule::empty_b () const
216 return expr_
== SCM_EOL
;
220 fontify_atom (Font_metric
* met
, SCM f
)
225 return gh_list (ly_symbol2scm ("fontify"),
226 ly_quote_scm (met
->description_
), f
, SCM_UNDEFINED
);
230 Molecule::get_expr () const
238 Molecule::extent_box () const
242 IMPLEMENT_SIMPLE_SMOBS (Molecule
);
246 Molecule::print_smob (SCM s
, SCM port
, scm_print_state
*)
248 Molecule
*r
= (Molecule
*) gh_cdr (s
);
250 scm_puts ("#<Molecule ", port
);
251 /* String str (r->str ());
252 scm_puts ((char *)str.ch_C (), port);*/
253 scm_puts (" >", port
);
260 Molecule::mark_smob (SCM s
)
262 Molecule
*r
= (Molecule
*) gh_cdr (s
);
267 IMPLEMENT_TYPE_P (Molecule
, "molecule?");
268 IMPLEMENT_DEFAULT_EQUAL_P (Molecule
);
269 IMPLEMENT_UNSMOB (Molecule
, molecule
);