2 stencil.cc -- implement Stencil
4 source file of the GNU LilyPond music typesetter
6 (c) 1997--2004 Han-Wen Nienhuys <hanwen@cs.uu.nl>
10 #include <libc-extension.hh> // isinf
13 #include "font-metric.hh"
14 #include "dimensions.hh"
15 #include "interval.hh"
20 #include "ly-smobs.icc"
28 Stencil::Stencil (Box b, SCM func)
35 Stencil::print_smob (SCM, SCM port, scm_print_state *)
37 scm_puts ("#<Stencil ", port);
38 scm_puts (" >", port);
43 Stencil::mark_smob (SCM smob)
45 Stencil *s = (Stencil*) ly_cdr (smob);
49 IMPLEMENT_SIMPLE_SMOBS (Stencil);
50 IMPLEMENT_TYPE_P (Stencil, "ly:stencil?");
51 IMPLEMENT_DEFAULT_EQUAL_P (Stencil);
54 Stencil::extent (Axis a) const
59 /* Hmm... maybe this is not such a good idea ; stuff can be empty,
62 Stencil::is_empty () const
64 return expr_ == SCM_EOL;
68 Stencil::expr () const
74 Stencil::extent_box () const
79 Stencil::origin () const
85 Stencil::translate (Offset o)
90 if (abs (o[a]) > 100 CM
91 || isinf (o[a]) || isnan (o[a]))
93 programming_error ("Improbable offset for translation: setting to zero");
99 expr_ = scm_list_n (ly_symbol2scm ("translate-stencil"),
101 expr_, SCM_UNDEFINED);
108 Stencil::translate_axis (Real x, Axis a)
116 Stencil::add_stencil (Stencil const &s)
118 expr_ = scm_list_3 (ly_symbol2scm ("combine-stencil"), s.expr_, expr_);
123 Stencil::set_empty (bool e)
127 dim_[X_AXIS].set_empty ();
128 dim_[Y_AXIS].set_empty ();
132 dim_[X_AXIS] = Interval (0,0);
133 dim_[Y_AXIS] = Interval (0,0);
138 Stencil::align_to (Axis a, Real x)
143 Interval i (extent (a));
144 translate_axis (-i.linear_combination (x), a);
147 /* FIXME: unintuitive naming, you would expect *this to be moved.
148 Kept (keeping?) API for compat with add_at_edge ().
150 What is PADDING, what is MINIMUM, exactly? */
152 Stencil::moved_to_edge (Axis a, Direction d, Stencil const &s,
153 Real padding, Real minimum) const
155 Interval my_extent = dim_[a];
156 Interval i (s.extent (a));
160 programming_error ("Stencil::moved_to_edge: adding empty stencil.");
166 Real offset = (my_extent.is_empty () ? 0.0 : my_extent[d] - his_extent)
170 toadd.translate_axis (offset,a);
172 if (minimum > 0 && d * (-origin ()[a] + toadd.origin ()[a]) < minimum)
173 toadd.translate_axis ( -toadd.origin ()[a]
174 + origin ()[a] + d * minimum, a);
179 /* See scheme Function. */
181 Stencil::add_at_edge (Axis a, Direction d, Stencil const &s, Real padding,
184 add_stencil (moved_to_edge (a, d, s, padding, minimum));
188 /****************************************************************/
191 interpret_stencil_expr (SCM expr,
192 void (*func) (void*, SCM),
198 if (!ly_c_pair_p (expr))
201 SCM head =ly_car (expr);
202 if (unsmob_input (head))
204 Input *ip = unsmob_input (head);
206 scm_list_4 (ly_symbol2scm ("define-origin"),
207 scm_makfrom0str (ip->file_string ()
209 scm_int2num (ip->line_number ()),
210 scm_int2num (ip->column_number ())));
212 expr = ly_cadr (expr);
214 else if (head == ly_symbol2scm ("no-origin"))
216 (*func) (func_arg, scm_list_1 (head));
217 expr = ly_cadr (expr);
219 else if (head == ly_symbol2scm ("translate-stencil"))
221 o += ly_scm2offset (ly_cadr (expr));
222 expr = ly_caddr (expr);
224 else if (head == ly_symbol2scm ("combine-stencil"))
226 interpret_stencil_expr (ly_cadr (expr), func, func_arg, o);
227 expr = ly_caddr (expr);
232 scm_list_4 (ly_symbol2scm ("placebox"),
233 scm_make_real (o[X_AXIS]),
234 scm_make_real (o[Y_AXIS]),
248 find_font_function (void * fs, SCM x)
250 Font_struct * me = (Font_struct*)fs;
252 if (ly_car (x) == ly_symbol2scm ("placebox"))
254 SCM args = ly_cdr (x);
255 SCM what = ly_caddr (x);
257 if (ly_c_pair_p (what))
259 SCM head = ly_car (what);
260 if (ly_symbol2scm ("text") == head)
261 me->fonts_ = scm_cons (ly_cadr (what), me->fonts_);
262 else if (head == ly_symbol2scm ("char"))
263 me->fonts_ = scm_cons (ly_cadr (what), me->fonts_);
268 LY_DEFINE(ly_stencil_fonts, "ly:stencil-fonts",
270 "Analyse @var{s}, and return a list of fonts used in @var{s}."
272 Stencil *stil =unsmob_stencil (s);
273 SCM_ASSERT_TYPE (stil, s, SCM_ARG1, __FUNCTION__, "Stencil");
278 interpret_stencil_expr (stil.expr (), &find_font_function,
279 (void*) &fl, Offset (0,0));