2 This file is part of LilyPond, the GNU music typesetter.
4 Copyright (C) 1997--2012 Han-Wen Nienhuys <hanwen@xs4all.nl>
6 LilyPond is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation, either version 3 of the License, or
9 (at your option) any later version.
11 LilyPond is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with LilyPond. If not, see <http://www.gnu.org/licenses/>.
23 #include "font-metric.hh"
25 #include "string-convert.hh"
28 #include "ly-smobs.icc"
36 Stencil::Stencil (Box b, SCM func)
43 Stencil::print_smob (SCM, SCM port, scm_print_state *)
45 scm_puts ("#<Stencil ", port);
46 scm_puts (" >", port);
51 Stencil::mark_smob (SCM smob)
53 Stencil *s = (Stencil *) SCM_CELL_WORD_1 (smob);
57 IMPLEMENT_SIMPLE_SMOBS (Stencil);
58 IMPLEMENT_TYPE_P (Stencil, "ly:stencil?");
59 IMPLEMENT_DEFAULT_EQUAL_P (Stencil);
62 Stencil::extent (Axis a) const
68 Stencil::is_empty () const
69 /* If only one of X- or Y-extent is empty; such a stencil can be useful
70 * for backspacing, as with \hspace #-2, so we do not consider it empty.
73 return (expr_ == SCM_EOL
74 || (dim_[X_AXIS].is_empty ()
75 && dim_[Y_AXIS].is_empty ()));
79 Stencil::expr () const
85 Stencil::extent_box () const
91 Stencil::rotate (Real a, Offset off)
93 rotate_degrees (a * 180 / M_PI, off);
97 Rotate this stencil around the point ABSOLUTE_OFF.
101 Stencil::rotate_degrees_absolute (Real a, Offset absolute_off)
103 const Real x = absolute_off[X_AXIS];
104 const Real y = absolute_off[Y_AXIS];
107 * Build scheme expression (processed in stencil-interpret.cc)
109 /* TODO: by hanwenn 2008/09/10 14:38:56:
110 * in effect, this copies the underlying expression. It might be a
111 * little bit nicer to mirror this in the api, ie. make a
113 * and have Stencil::rotate be an abbrev of
117 expr_ = scm_list_n (ly_symbol2scm ("rotate-stencil"),
118 scm_list_2 (scm_from_double (a),
119 scm_cons (scm_from_double (x), scm_from_double (y))),
120 expr_, SCM_UNDEFINED);
123 * Calculate the new bounding box
125 Box shifted_box = extent_box ();
126 shifted_box.translate (-absolute_off);
129 pts.push_back (Offset (shifted_box.x ().at (LEFT), shifted_box.y ().at (DOWN)));
130 pts.push_back (Offset (shifted_box.x ().at (RIGHT), shifted_box.y ().at (DOWN)));
131 pts.push_back (Offset (shifted_box.x ().at (RIGHT), shifted_box.y ().at (UP)));
132 pts.push_back (Offset (shifted_box.x ().at (LEFT), shifted_box.y ().at (UP)));
134 const Offset rot = complex_exp (Offset (0, a * M_PI / 180.0));
136 for (vsize i = 0; i < pts.size (); i++)
137 dim_.add_point (pts[i] * rot + absolute_off);
141 Rotate this stencil around the point RELATIVE_OFF.
143 RELATIVE_OFF is measured in terms of the extent of the stencil, so
144 -1 = LEFT/DOWN edge, 1 = RIGHT/UP edge.
147 Stencil::rotate_degrees (Real a, Offset relative_off)
150 * Calculate the center of rotation
152 const Real x = extent (X_AXIS).linear_combination (relative_off[X_AXIS]);
153 const Real y = extent (Y_AXIS).linear_combination (relative_off[Y_AXIS]);
154 rotate_degrees_absolute (a, Offset (x, y));
158 Stencil::translate (Offset o)
167 || fabs (o[a]) > 1e6)
169 programming_error (String_convert::form_string ("Improbable offset for stencil: %f staff space", o[a])
171 + "Setting to zero.");
173 if (strict_infinity_checking)
174 scm_misc_error (__FUNCTION__, "Improbable offset.", SCM_EOL);
179 expr_ = scm_list_n (ly_symbol2scm ("translate-stencil"),
181 expr_, SCM_UNDEFINED);
187 Stencil::translate_axis (Real x, Axis a)
195 Stencil::scale (Real x, Real y)
197 expr_ = scm_list_3 (ly_symbol2scm ("scale-stencil"),
198 scm_list_2 (scm_from_double (x),
199 scm_from_double (y)),
206 Stencil::add_stencil (Stencil const &s)
208 expr_ = scm_list_3 (ly_symbol2scm ("combine-stencil"), s.expr_, expr_);
213 Stencil::set_empty (bool e)
217 dim_[X_AXIS].set_empty ();
218 dim_[Y_AXIS].set_empty ();
222 dim_[X_AXIS] = Interval (0, 0);
223 dim_[Y_AXIS] = Interval (0, 0);
228 Stencil::align_to (Axis a, Real x)
233 Interval i (extent (a));
234 translate_axis (-i.linear_combination (x), a);
237 /* See scheme Function. */
239 Stencil::add_at_edge (Axis a, Direction d, Stencil const &s, Real padding)
241 Interval my_extent = dim_[a];
242 Interval i (s.extent (a));
246 programming_error ("Stencil::add_at_edge: adding empty stencil.");
252 Real offset = (my_extent.is_empty () ? 0.0 : my_extent[d] - his_extent)
256 toadd.translate_axis (offset, a);
261 Stencil::in_color (Real r, Real g, Real b) const
263 Stencil new_stencil (extent_box (),
264 scm_list_3 (ly_symbol2scm ("color"),
265 scm_list_3 (scm_from_double (r),
267 scm_from_double (b)),
274 Stencil::translated (Offset z) const