Fixes Issue 1504, allowing feather beam line breaking.
[lilypond/patrick.git] / lily / stencil.cc
blob6a094d28905af39a9cbb831faeb772676f838a7d
1 /*
2 This file is part of LilyPond, the GNU music typesetter.
4 Copyright (C) 1997--2011 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/>.
20 #include "stencil.hh"
22 #include "main.hh"
23 #include "font-metric.hh"
24 #include "input.hh"
25 #include "string-convert.hh"
26 #include "warn.hh"
28 #include "ly-smobs.icc"
30 Stencil::Stencil ()
32 expr_ = SCM_EOL;
33 set_empty (true);
36 Stencil::Stencil (Box b, SCM func)
38 expr_ = func;
39 dim_ = b;
42 int
43 Stencil::print_smob (SCM, SCM port, scm_print_state *)
45 scm_puts ("#<Stencil ", port);
46 scm_puts (" >", port);
47 return 1;
50 SCM
51 Stencil::mark_smob (SCM smob)
53 Stencil *s = (Stencil *) SCM_CELL_WORD_1 (smob);
54 return s->expr_;
57 IMPLEMENT_SIMPLE_SMOBS (Stencil);
58 IMPLEMENT_TYPE_P (Stencil, "ly:stencil?");
59 IMPLEMENT_DEFAULT_EQUAL_P (Stencil);
61 Interval
62 Stencil::extent (Axis a) const
64 return dim_[a];
67 bool
68 Stencil::is_empty () const
70 return (expr_ == SCM_EOL
71 || dim_[X_AXIS].is_empty ()
72 || dim_[Y_AXIS].is_empty ());
75 SCM
76 Stencil::expr () const
78 return expr_;
81 Box
82 Stencil::extent_box () const
84 return dim_;
87 void
88 Stencil::rotate (Real a, Offset off)
90 rotate_degrees (a * 180/M_PI, off);
94 Rotate this stencil around the point ABSOLUTE_OFF.
97 void
98 Stencil::rotate_degrees_absolute (Real a, Offset absolute_off)
100 const Real x = absolute_off[X_AXIS];
101 const Real y = absolute_off[Y_AXIS];
104 * Build scheme expression (processed in stencil-interpret.cc)
106 /* TODO: by hanwenn 2008/09/10 14:38:56:
107 * in effect, this copies the underlying expression. It might be a
108 * little bit nicer to mirror this in the api, ie. make a
109 * Stencil::rotated()
110 * and have Stencil::rotate be an abbrev of
111 * *this = rotated()
114 expr_ = scm_list_n (ly_symbol2scm ("rotate-stencil"),
115 scm_list_2 (scm_from_double (a),
116 scm_cons (scm_from_double (x), scm_from_double (y))),
117 expr_, SCM_UNDEFINED);
120 * Calculate the new bounding box
122 Box shifted_box = extent_box ();
123 shifted_box.translate (-absolute_off);
125 vector<Offset> pts;
126 pts.push_back (Offset (shifted_box.x ().at(LEFT), shifted_box.y ().at(DOWN)));
127 pts.push_back (Offset (shifted_box.x ().at(RIGHT), shifted_box.y ().at(DOWN)));
128 pts.push_back (Offset (shifted_box.x ().at(RIGHT), shifted_box.y ().at(UP)));
129 pts.push_back (Offset (shifted_box.x ().at(LEFT), shifted_box.y ().at(UP)));
131 const Offset rot = complex_exp (Offset (0, a * M_PI / 180.0));
132 dim_.set_empty ();
133 for (vsize i = 0; i < pts.size (); i++)
134 dim_.add_point (pts[i] * rot + absolute_off);
138 Rotate this stencil around the point RELATIVE_OFF.
140 RELATIVE_OFF is measured in terms of the extent of the stencil, so
141 -1 = LEFT/DOWN edge, 1 = RIGHT/UP edge.
143 void
144 Stencil::rotate_degrees (Real a, Offset relative_off)
147 * Calculate the center of rotation
149 const Real x = extent (X_AXIS).linear_combination (relative_off[X_AXIS]);
150 const Real y = extent (Y_AXIS).linear_combination (relative_off[Y_AXIS]);
151 rotate_degrees_absolute (a, Offset (x, y));
154 void
155 Stencil::translate (Offset o)
157 Axis a = X_AXIS;
158 while (a < NO_AXES)
160 if (isinf (o[a])
161 || isnan (o[a])
163 // ugh, hardcoded.
164 || fabs (o[a]) > 1e6)
166 programming_error (String_convert::form_string ("Improbable offset for stencil: %f staff space", o[a])
167 + "\n"
168 + "Setting to zero.");
169 o[a] = 0.0;
170 if (strict_infinity_checking)
171 scm_misc_error (__FUNCTION__, "Improbable offset.", SCM_EOL);
173 incr (a);
176 expr_ = scm_list_n (ly_symbol2scm ("translate-stencil"),
177 ly_offset2scm (o),
178 expr_, SCM_UNDEFINED);
179 if (!is_empty ())
180 dim_.translate (o);
183 void
184 Stencil::translate_axis (Real x, Axis a)
186 Offset o (0, 0);
187 o[a] = x;
188 translate (o);
191 void
192 Stencil::scale (Real x, Real y)
194 expr_ = scm_list_3 (ly_symbol2scm ("scale-stencil"),
195 scm_list_2 (scm_from_double (x),
196 scm_from_double (y)),
197 expr_);
198 dim_[X_AXIS] *= x;
199 dim_[Y_AXIS] *= y;
202 void
203 Stencil::add_stencil (Stencil const &s)
205 expr_ = scm_list_3 (ly_symbol2scm ("combine-stencil"), s.expr_, expr_);
206 dim_.unite (s.dim_);
209 void
210 Stencil::set_empty (bool e)
212 if (e)
214 dim_[X_AXIS].set_empty ();
215 dim_[Y_AXIS].set_empty ();
217 else
219 dim_[X_AXIS] = Interval (0, 0);
220 dim_[Y_AXIS] = Interval (0, 0);
224 void
225 Stencil::align_to (Axis a, Real x)
227 if (is_empty ())
228 return;
230 Interval i (extent (a));
231 translate_axis (-i.linear_combination (x), a);
234 /* See scheme Function. */
235 void
236 Stencil::add_at_edge (Axis a, Direction d, Stencil const &s, Real padding)
238 Interval my_extent = dim_[a];
239 Interval i (s.extent (a));
240 Real his_extent;
241 if (i.is_empty ())
243 programming_error ("Stencil::add_at_edge: adding empty stencil.");
244 his_extent = 0.0;
246 else
247 his_extent = i[-d];
249 Real offset = (my_extent.is_empty () ? 0.0 : my_extent[d] - his_extent)
250 + d * padding;
252 Stencil toadd (s);
253 toadd.translate_axis (offset, a);
254 add_stencil (toadd);
257 Stencil
258 Stencil::in_color (Real r, Real g, Real b) const
260 Stencil new_stencil (extent_box (),
261 scm_list_3 (ly_symbol2scm ("color"),
262 scm_list_3 (scm_from_double (r),
263 scm_from_double (g),
264 scm_from_double (b)),
265 expr ()));
266 return new_stencil;
269 /* convenience */
270 Stencil
271 Stencil::translated (Offset z) const
273 Stencil s (*this);
274 s.translate (z);
275 return s;