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
70 return (expr_ == SCM_EOL
75 Stencil::is_empty (Axis a) const
77 return dim_.is_empty (a);
81 Stencil::expr () const
87 Stencil::extent_box () const
93 Stencil::rotate (Real a, Offset off)
95 rotate_degrees (a * 180 / M_PI, off);
99 Rotate this stencil around the point ABSOLUTE_OFF.
103 Stencil::rotate_degrees_absolute (Real a, Offset absolute_off)
105 const Real x = absolute_off[X_AXIS];
106 const Real y = absolute_off[Y_AXIS];
109 * Build scheme expression (processed in stencil-interpret.cc)
111 /* TODO: by hanwenn 2008/09/10 14:38:56:
112 * in effect, this copies the underlying expression. It might be a
113 * little bit nicer to mirror this in the api, ie. make a
115 * and have Stencil::rotate be an abbrev of
119 expr_ = scm_list_n (ly_symbol2scm ("rotate-stencil"),
120 scm_list_2 (scm_from_double (a),
121 scm_cons (scm_from_double (x), scm_from_double (y))),
122 expr_, SCM_UNDEFINED);
125 * Calculate the new bounding box
127 Box shifted_box = extent_box ();
128 shifted_box.translate (-absolute_off);
131 pts.push_back (Offset (shifted_box.x ().at (LEFT), shifted_box.y ().at (DOWN)));
132 pts.push_back (Offset (shifted_box.x ().at (RIGHT), shifted_box.y ().at (DOWN)));
133 pts.push_back (Offset (shifted_box.x ().at (RIGHT), shifted_box.y ().at (UP)));
134 pts.push_back (Offset (shifted_box.x ().at (LEFT), shifted_box.y ().at (UP)));
136 const Offset rot = complex_exp (Offset (0, a * M_PI / 180.0));
138 for (vsize i = 0; i < pts.size (); i++)
139 dim_.add_point (pts[i] * rot + absolute_off);
143 Rotate this stencil around the point RELATIVE_OFF.
145 RELATIVE_OFF is measured in terms of the extent of the stencil, so
146 -1 = LEFT/DOWN edge, 1 = RIGHT/UP edge.
149 Stencil::rotate_degrees (Real a, Offset relative_off)
152 * Calculate the center of rotation
154 const Real x = extent (X_AXIS).linear_combination (relative_off[X_AXIS]);
155 const Real y = extent (Y_AXIS).linear_combination (relative_off[Y_AXIS]);
156 rotate_degrees_absolute (a, Offset (x, y));
160 Stencil::translate (Offset o)
169 || fabs (o[a]) > 1e6)
171 programming_error (String_convert::form_string ("Improbable offset for stencil: %f staff space", o[a])
173 + "Setting to zero.");
175 if (strict_infinity_checking)
176 scm_misc_error (__FUNCTION__, "Improbable offset.", SCM_EOL);
181 if (!scm_is_null (expr_))
182 expr_ = scm_list_n (ly_symbol2scm ("translate-stencil"),
184 expr_, SCM_UNDEFINED);
190 Stencil::translate_axis (Real x, Axis a)
198 Stencil::scale (Real x, Real y)
200 expr_ = scm_list_3 (ly_symbol2scm ("scale-stencil"),
201 scm_list_2 (scm_from_double (x),
202 scm_from_double (y)),
209 Stencil::add_stencil (Stencil const &s)
211 SCM cs = ly_symbol2scm ("combine-stencil");
212 if (scm_is_null (expr_))
214 else if (scm_is_null (s.expr_))
216 else if (scm_is_pair (expr_)
217 && scm_is_eq (cs, scm_car (expr_)))
219 if (scm_is_pair (s.expr_)
220 && scm_is_eq (cs, scm_car (s.expr_)))
221 expr_ = scm_append (scm_list_2 (s.expr_, scm_cdr (expr_)));
223 expr_ = scm_cons2 (cs, s.expr_, scm_cdr (expr_));
227 if (scm_is_pair (s.expr_)
228 && scm_is_eq (cs, scm_car (s.expr_)))
229 expr_ = scm_append (scm_list_2 (s.expr_, scm_list_1 (expr_)));
231 expr_ = scm_list_3 (cs, s.expr_, expr_);
237 Stencil::set_empty (bool e)
241 dim_[X_AXIS].set_empty ();
242 dim_[Y_AXIS].set_empty ();
246 dim_[X_AXIS] = Interval (0, 0);
247 dim_[Y_AXIS] = Interval (0, 0);
252 Stencil::align_to (Axis a, Real x)
257 Interval i (extent (a));
258 translate_axis (-i.linear_combination (x), a);
261 /* See scheme Function. */
263 // Any stencil that is empty in the orthogonal axis is spacing.
264 // Spacing is not subjected to the max (0) rule and can thus be
268 Stencil::add_at_edge (Axis a, Direction d, Stencil const &s, Real padding)
270 // Material that is empty in the axis of reference has only limited
271 // usefulness for combining. We still retain as much information as
272 // available since there may be uses like setting page links or
273 // background color or watermarks.
281 Interval first_extent = extent (a);
286 toadd.translate_axis (first_extent[d], a);
291 Interval next_extent = s.extent (a);
293 bool first_is_spacing = is_empty (other_axis (a));
294 bool next_is_spacing = s.is_empty (other_axis (a));
296 Real offset = first_extent[d] - next_extent[-d];
298 if (!(first_is_spacing || next_is_spacing))
300 offset += d * padding;
304 toadd.translate_axis (offset, a);
309 Stencil::in_color (Real r, Real g, Real b) const
311 Stencil new_stencil (extent_box (),
312 scm_list_3 (ly_symbol2scm ("color"),
313 scm_list_3 (scm_from_double (r),
315 scm_from_double (b)),
322 Stencil::translated (Offset z) const