2 stencil.cc -- implement Stencil
4 source file of the GNU LilyPond music typesetter
6 (c) 1997--2005 Han-Wen Nienhuys <hanwen@cs.uu.nl>
13 #include <libc-extension.hh> // isinf
15 #include "input-smob.hh"
16 #include "font-metric.hh"
17 #include "dimensions.hh"
18 #include "string-convert.hh"
21 #include "ly-smobs.icc"
29 Stencil::Stencil (Box b, SCM func)
36 Stencil::print_smob (SCM, SCM port, scm_print_state *)
38 scm_puts ("#<Stencil ", port);
39 scm_puts (" >", port);
44 Stencil::mark_smob (SCM smob)
46 Stencil *s = (Stencil*) SCM_CELL_WORD_1 (smob);
50 IMPLEMENT_SIMPLE_SMOBS (Stencil);
51 IMPLEMENT_TYPE_P (Stencil, "ly:stencil?");
52 IMPLEMENT_DEFAULT_EQUAL_P (Stencil);
55 Stencil::extent (Axis a) const
60 /* Hmm... maybe this is not such a good idea ; stuff can be empty,
63 Stencil::is_empty () const
65 return expr_ == SCM_EOL;
69 Stencil::expr () const
75 Stencil::extent_box () const
80 Stencil::origin () const
86 Stencil::translate (Offset o)
91 if (isinf (o[a]) || isnan (o[a]))
93 programming_error (String_convert::form_string ("Improbable offset for stencil: %f staff space", o[a])
95 + "Setting to zero.");
101 expr_ = scm_list_n (ly_symbol2scm ("translate-stencil"),
103 expr_, SCM_UNDEFINED);
110 Stencil::translate_axis (Real x, Axis a)
118 Stencil::add_stencil (Stencil const &s)
120 expr_ = scm_list_3 (ly_symbol2scm ("combine-stencil"), s.expr_, expr_);
127 Stencil::set_empty (bool e)
131 dim_[X_AXIS].set_empty ();
132 dim_[Y_AXIS].set_empty ();
136 dim_[X_AXIS] = Interval (0, 0);
137 dim_[Y_AXIS] = Interval (0, 0);
142 Stencil::align_to (Axis a, Real x)
147 Interval i (extent (a));
148 translate_axis (-i.linear_combination (x), a);
151 /* FIXME: unintuitive naming, you would expect *this to be moved.
152 Kept (keeping?) API for compat with add_at_edge ().
154 What is PADDING, what is MINIMUM, exactly? */
156 Stencil::moved_to_edge (Axis a, Direction d, Stencil const &s,
157 Real padding, Real minimum) const
159 Interval my_extent = dim_[a];
160 Interval i (s.extent (a));
164 programming_error ("Stencil::moved_to_edge: adding empty stencil.");
166 // SCM_ASSERT_TYPE (0, s.expr (), SCM_ARG1, __FUNCTION__, "non-empty stencil");
171 Real offset = (my_extent.is_empty () ? 0.0 : my_extent[d] - his_extent)
175 toadd.translate_axis (offset, a);
177 if (minimum > 0 && d * (-origin ()[a] + toadd.origin ()[a]) < minimum)
178 toadd.translate_axis ( -toadd.origin ()[a]
179 + origin ()[a] + d * minimum, a);
184 /* See scheme Function. */
186 Stencil::add_at_edge (Axis a, Direction d, Stencil const &s, Real padding,
189 add_stencil (moved_to_edge (a, d, s, padding, minimum));
193 /****************************************************************/
197 interpret_stencil_expression (SCM expr,
198 void (*func) (void*, SCM),
204 if (!scm_is_pair (expr))
207 SCM head = scm_car (expr);
209 if (head == ly_symbol2scm ("translate-stencil"))
211 o += ly_scm2offset (scm_cadr (expr));
212 expr = scm_caddr (expr);
214 else if (head == ly_symbol2scm ("combine-stencil"))
216 for (SCM x = scm_cdr (expr); scm_is_pair (x); x = scm_cdr (x))
217 interpret_stencil_expression (scm_car (x), func, func_arg, o);
220 else if (head == ly_symbol2scm ("grob-cause"))
222 SCM grob = scm_cadr (expr);
224 (*func) (func_arg, scm_list_3 (head,
225 ly_quote_scm (ly_offset2scm (o)), grob));
226 interpret_stencil_expression (scm_caddr (expr), func, func_arg, o);
227 (*func) (func_arg, scm_list_1 (ly_symbol2scm ("no-origin")));
230 else if (head == ly_symbol2scm ("color"))
232 SCM color = scm_cadr (expr);
233 SCM r = scm_car (color);
234 SCM g = scm_cadr (color);
235 SCM b = scm_caddr (color);
237 (*func) (func_arg, scm_list_4 (ly_symbol2scm ("setcolor"), r, g, b));
238 interpret_stencil_expression (scm_caddr (expr), func, func_arg, o);
239 (*func) (func_arg, scm_list_1 (ly_symbol2scm ("resetcolor")));
246 scm_list_4 (ly_symbol2scm ("placebox"),
247 scm_make_real (o[X_AXIS]),
248 scm_make_real (o[Y_AXIS]),
262 find_font_function (void *fs, SCM x)
264 Font_list *me = (Font_list*) fs;
266 if (scm_car (x) == ly_symbol2scm ("placebox"))
268 SCM args = scm_cdr (x);
269 SCM what = scm_caddr (args);
271 if (scm_is_pair (what))
273 SCM head = scm_car (what);
274 if (ly_symbol2scm ("text") == head)
275 me->fonts_ = scm_cons (scm_cadr (what), me->fonts_);
276 else if (head == ly_symbol2scm ("char"))
277 me->fonts_ = scm_cons (scm_cadr (what), me->fonts_);
283 find_expression_fonts (SCM expr)
289 interpret_stencil_expression (expr, &find_font_function,
290 (void*) &fl, Offset (0, 0));