2 stencil.cc -- implement Stencil
4 source file of the GNU LilyPond music typesetter
6 (c) 1997--2007 Han-Wen Nienhuys <hanwen@xs4all.nl>
12 #include "font-metric.hh"
14 #include "string-convert.hh"
17 #include "ly-smobs.icc"
25 Stencil::Stencil (Box b, SCM func)
32 Stencil::print_smob (SCM, SCM port, scm_print_state *)
34 scm_puts ("#<Stencil ", port);
35 scm_puts (" >", port);
40 Stencil::mark_smob (SCM smob)
42 Stencil *s = (Stencil *) SCM_CELL_WORD_1 (smob);
46 IMPLEMENT_SIMPLE_SMOBS (Stencil);
47 IMPLEMENT_TYPE_P (Stencil, "ly:stencil?");
48 IMPLEMENT_DEFAULT_EQUAL_P (Stencil);
51 Stencil::extent (Axis a) const
57 Stencil::is_empty () const
59 return (expr_ == SCM_EOL
60 || dim_[X_AXIS].is_empty ()
61 || dim_[Y_AXIS].is_empty ());
65 Stencil::expr () const
71 Stencil::extent_box () const
77 Stencil::rotate (Real a, Offset off)
79 rotate_degrees (a *180/M_PI, off);
83 Rotate this stencil around the point [x, y]
86 Stencil::rotate_degrees (Real a, Offset off)
88 const Real x_cen = extent (X_AXIS).center ();
89 const Real y_cen = extent (Y_AXIS).center ();
92 * Calculate the center of rotation
94 const Real x = x_cen + off[X_AXIS] * x_cen;
95 const Real y = y_cen + off[Y_AXIS] * y_cen;
98 * Build scheme expression (processed in stencil-interpret.cc)
100 expr_ = scm_list_n (ly_symbol2scm ("rotate-stencil"),
101 scm_list_2 (scm_from_double (a),
102 scm_cons (scm_from_double (x), scm_from_double (y))),
103 expr_, SCM_UNDEFINED);
106 * Calculate the new bounding box
109 pts.push_back (Offset (-x_cen, -y_cen));
110 pts.push_back (Offset (x_cen, -y_cen));
111 pts.push_back (Offset (x_cen, y_cen));
112 pts.push_back (Offset (-x_cen, y_cen));
114 const Offset rot = complex_exp (Offset (0, a * M_PI / 180.0));
116 for (vsize i = 0; i < pts.size (); i++)
117 dim_.add_point (pts[i] * rot + Offset (x_cen, y_cen));
121 Stencil::translate (Offset o)
130 || fabs (o[a]) > 1e6)
132 programming_error (String_convert::form_string ("Improbable offset for stencil: %f staff space", o[a])
134 + "Setting to zero.");
136 if (strict_infinity_checking)
137 scm_misc_error (__FUNCTION__, "Improbable offset.", SCM_EOL);
142 expr_ = scm_list_n (ly_symbol2scm ("translate-stencil"),
144 expr_, SCM_UNDEFINED);
150 Stencil::translate_axis (Real x, Axis a)
158 Stencil::add_stencil (Stencil const &s)
160 expr_ = scm_list_3 (ly_symbol2scm ("combine-stencil"), s.expr_, expr_);
165 Stencil::set_empty (bool e)
169 dim_[X_AXIS].set_empty ();
170 dim_[Y_AXIS].set_empty ();
174 dim_[X_AXIS] = Interval (0, 0);
175 dim_[Y_AXIS] = Interval (0, 0);
180 Stencil::align_to (Axis a, Real x)
185 Interval i (extent (a));
186 translate_axis (-i.linear_combination (x), a);
189 /* See scheme Function. */
191 Stencil::add_at_edge (Axis a, Direction d, Stencil const &s, Real padding)
193 Interval my_extent = dim_[a];
194 Interval i (s.extent (a));
198 programming_error ("Stencil::moved_to_edge: adding empty stencil.");
204 Real offset = (my_extent.is_empty () ? 0.0 : my_extent[d] - his_extent)
208 toadd.translate_axis (offset, a);
213 Stencil::in_color (Real r, Real g, Real b) const
215 Stencil new_stencil (extent_box (),
216 scm_list_3 (ly_symbol2scm ("color"),
217 scm_list_3 (scm_from_double (r),
219 scm_from_double (b)),
226 Stencil::translated (Offset z) const