2 This file is part of LilyPond, the GNU music typesetter.
4 Copyright (C) 1997--2014 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"
35 Stencil::Stencil (Box b, SCM func)
42 Stencil::print_smob (SCM, SCM port, scm_print_state *)
44 scm_puts ("#<Stencil ", port);
45 scm_puts (" >", port);
50 Stencil::mark_smob (SCM smob)
52 Stencil *s = (Stencil *) SCM_CELL_WORD_1 (smob);
56 const char Stencil::type_p_name_[] = "ly:stencil?";
59 Stencil::extent (Axis a) const
65 Stencil::is_empty () const
67 return (expr_ == SCM_EOL
72 Stencil::is_empty (Axis a) const
74 return dim_.is_empty (a);
78 Stencil::expr () const
84 Stencil::extent_box () const
90 Stencil::rotate (Real a, Offset off)
92 rotate_degrees (a * 180 / M_PI, off);
96 Rotate this stencil around the point ABSOLUTE_OFF.
100 Stencil::rotate_degrees_absolute (Real a, Offset absolute_off)
102 const Real x = absolute_off[X_AXIS];
103 const Real y = absolute_off[Y_AXIS];
106 * Build scheme expression (processed in stencil-interpret.cc)
108 /* TODO: by hanwenn 2008/09/10 14:38:56:
109 * in effect, this copies the underlying expression. It might be a
110 * little bit nicer to mirror this in the api, ie. make a
112 * and have Stencil::rotate be an abbrev of
116 expr_ = scm_list_n (ly_symbol2scm ("rotate-stencil"),
117 scm_list_2 (scm_from_double (a),
118 scm_cons (scm_from_double (x), scm_from_double (y))),
119 expr_, SCM_UNDEFINED);
122 * Calculate the new bounding box
124 Box shifted_box = extent_box ();
125 shifted_box.translate (-absolute_off);
128 pts.push_back (Offset (shifted_box.x ().at (LEFT), shifted_box.y ().at (DOWN)));
129 pts.push_back (Offset (shifted_box.x ().at (RIGHT), shifted_box.y ().at (DOWN)));
130 pts.push_back (Offset (shifted_box.x ().at (RIGHT), shifted_box.y ().at (UP)));
131 pts.push_back (Offset (shifted_box.x ().at (LEFT), shifted_box.y ().at (UP)));
133 const Offset rot = complex_exp (Offset (0, a * M_PI / 180.0));
135 for (vsize i = 0; i < pts.size (); i++)
136 dim_.add_point (pts[i] * rot + absolute_off);
140 Rotate this stencil around the point RELATIVE_OFF.
142 RELATIVE_OFF is measured in terms of the extent of the stencil, so
143 -1 = LEFT/DOWN edge, 1 = RIGHT/UP edge.
146 Stencil::rotate_degrees (Real a, Offset relative_off)
149 * Calculate the center of rotation
151 const Real x = extent (X_AXIS).linear_combination (relative_off[X_AXIS]);
152 const Real y = extent (Y_AXIS).linear_combination (relative_off[Y_AXIS]);
153 rotate_degrees_absolute (a, Offset (x, y));
157 Stencil::translate (Offset o)
166 || fabs (o[a]) > 1e6)
168 programming_error (String_convert::form_string ("Improbable offset for stencil: %f staff space", o[a])
170 + "Setting to zero.");
172 if (strict_infinity_checking)
173 scm_misc_error (__FUNCTION__, "Improbable offset.", SCM_EOL);
178 if (!scm_is_null (expr_))
179 expr_ = scm_list_n (ly_symbol2scm ("translate-stencil"),
181 expr_, SCM_UNDEFINED);
186 Stencil::translate_axis (Real x, Axis a)
194 Stencil::scale (Real x, Real y)
196 expr_ = scm_list_3 (ly_symbol2scm ("scale-stencil"),
197 scm_list_2 (scm_from_double (x),
198 scm_from_double (y)),
205 Stencil::add_stencil (Stencil const &s)
207 SCM cs = ly_symbol2scm ("combine-stencil");
208 if (scm_is_null (expr_))
210 else if (scm_is_null (s.expr_))
212 else if (scm_is_pair (expr_)
213 && scm_is_eq (cs, scm_car (expr_)))
215 if (scm_is_pair (s.expr_)
216 && scm_is_eq (cs, scm_car (s.expr_)))
217 expr_ = scm_append (scm_list_2 (s.expr_, scm_cdr (expr_)));
219 expr_ = scm_cons2 (cs, s.expr_, scm_cdr (expr_));
223 if (scm_is_pair (s.expr_)
224 && scm_is_eq (cs, scm_car (s.expr_)))
225 expr_ = scm_append (scm_list_2 (s.expr_, scm_list_1 (expr_)));
227 expr_ = scm_list_3 (cs, s.expr_, expr_);
233 Stencil::set_empty (bool e)
237 dim_[X_AXIS].set_empty ();
238 dim_[Y_AXIS].set_empty ();
242 dim_[X_AXIS] = Interval (0, 0);
243 dim_[Y_AXIS] = Interval (0, 0);
248 Stencil::align_to (Axis a, Real x)
253 Interval i (extent (a));
254 translate_axis (-i.linear_combination (x), a);
257 /* See scheme Function. */
259 // Any stencil that is empty in the orthogonal axis is spacing.
260 // Spacing is not subjected to the max (0) rule and can thus be
264 Stencil::add_at_edge (Axis a, Direction d, Stencil const &s, Real padding)
266 // Material that is empty in the axis of reference has only limited
267 // usefulness for combining. We still retain as much information as
268 // available since there may be uses like setting page links or
269 // background color or watermarks, and off-axis extents.
277 Interval first_extent = extent (a);
282 // translation does not affect axis-empty extent box.
283 toadd.translate_axis (first_extent[d], a);
288 Interval next_extent = s.extent (a);
290 bool first_is_spacing = is_empty (other_axis (a));
291 bool next_is_spacing = s.is_empty (other_axis (a));
293 Real offset = first_extent[d] - next_extent[-d];
295 if (!(first_is_spacing || next_is_spacing))
297 offset += d * padding;
301 toadd.translate_axis (offset, a);
305 // Stencil::stack is mainly used for assembling lines or columns
306 // of stencils. For the most common case of adding at the right, the
307 // reference point of the added stencil is usually placed at the right
308 // edge of the current one, unless the added stencil has a negative
309 // left extent in which case its left edge is placed at the right edge
310 // of the current one.
312 // Spacing is special in that it is applied without padding. Spacing
313 // at the right edge shifts the right edge accordingly.
315 // For spacing at the left edge, there are several approaches. In
316 // order to get to predictable behavior, we want to have at least a
317 // continuous approach. An obvious idea is to do a "translate" by the
318 // appropriate amount. Doing that while retaining the nominal left
319 // edge seems like the most straightforward way.
322 Stencil::stack (Axis a, Direction d, Stencil const &s, Real padding, Real mindist)
324 // Material that is empty in the axis of reference can't be sensibly
325 // stacked. We just revert to add_at_edge behavior then.
330 toadd.add_stencil (*this);
331 expr_ = toadd.expr ();
332 dim_ = toadd.extent_box ();
336 Interval first_extent = extent (a);
341 toadd.translate_axis (first_extent[d], a);
342 toadd.add_stencil (*this);
343 expr_ = toadd.expr ();
344 dim_ = toadd.extent_box ();
348 Interval next_extent = s.extent (a);
350 // It is somewhat tedious to special-case all spacing, but it turns
351 // out that not doing so makes it astonishingly hard to make the
352 // code do the correct thing.
354 // If first is spacing, we translate second accordingly without
355 // letting this affect its backward edge.
356 if (is_empty (other_axis (a)))
359 Real offset = d * first_extent.delta ();
360 toadd.translate_axis (offset, a);
361 toadd.add_stencil (*this);
362 expr_ = toadd.expr ();
363 dim_ = toadd.extent_box ();
364 dim_[a][-d] = next_extent[-d];
365 dim_[a][d] = next_extent[d] + offset;
369 // If next is spacing, similar action:
370 if (s.is_empty (other_axis (a)))
373 Real offset = first_extent [d];
374 toadd.translate_axis (offset, a);
375 toadd.add_stencil (*this);
376 expr_ = toadd.expr ();
377 dim_ = toadd.extent_box ();
378 dim_[a][-d] = first_extent[-d];
379 dim_[a][d] = first_extent[d] + d * next_extent.delta ();
384 Real offset = first_extent[d];
386 // If the added stencil has a backwardly protruding edge, we make
387 // room for it when combining.
389 if (d * next_extent [-d] < 0)
390 offset -= next_extent [-d];
392 offset += d * padding;
394 if (offset * d < mindist)
395 offset = d * mindist;
398 toadd.translate_axis (offset, a);
399 toadd.add_stencil (*this);
400 expr_ = toadd.expr ();
401 dim_ = toadd.extent_box ();
402 dim_[a][-d] = first_extent [-d];
403 dim_[a][d] = next_extent [d] + offset;
408 Stencil::in_color (Real r, Real g, Real b) const
410 Stencil new_stencil (extent_box (),
411 scm_list_3 (ly_symbol2scm ("color"),
412 scm_list_3 (scm_from_double (r),
414 scm_from_double (b)),
421 Stencil::translated (Offset z) const