]> git.donarmstrong.com Git - lilypond.git/blob - lily/stencil.cc
Merge branch 'issue4082'
[lilypond.git] / lily / stencil.cc
1 /*
2   This file is part of LilyPond, the GNU music typesetter.
3
4   Copyright (C) 1997--2014 Han-Wen Nienhuys <hanwen@xs4all.nl>
5
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.
10
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.
15
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/>.
18 */
19
20 #include "stencil.hh"
21
22 #include "main.hh"
23 #include "font-metric.hh"
24 #include "input.hh"
25 #include "string-convert.hh"
26 #include "warn.hh"
27
28
29 Stencil::Stencil ()
30 {
31   expr_ = SCM_EOL;
32   set_empty (true);
33 }
34
35 Stencil::Stencil (Box b, SCM func)
36 {
37   expr_ = func;
38   dim_ = b;
39 }
40
41 int
42 Stencil::print_smob (SCM, SCM port, scm_print_state *)
43 {
44   scm_puts ("#<Stencil ", port);
45   scm_puts (" >", port);
46   return 1;
47 }
48
49 SCM
50 Stencil::mark_smob (SCM smob)
51 {
52   Stencil *s = (Stencil *) SCM_CELL_WORD_1 (smob);
53   return s->expr_;
54 }
55
56 const char Stencil::type_p_name_[] = "ly:stencil?";
57
58 Interval
59 Stencil::extent (Axis a) const
60 {
61   return dim_[a];
62 }
63
64 bool
65 Stencil::is_empty () const
66 {
67   return (expr_ == SCM_EOL
68           || dim_.is_empty ());
69 }
70
71 bool
72 Stencil::is_empty (Axis a) const
73 {
74   return dim_.is_empty (a);
75 }
76
77 SCM
78 Stencil::expr () const
79 {
80   return expr_;
81 }
82
83 Box
84 Stencil::extent_box () const
85 {
86   return dim_;
87 }
88
89 void
90 Stencil::rotate (Real a, Offset off)
91 {
92   rotate_degrees (a * 180 / M_PI, off);
93 }
94
95 /*
96   Rotate this stencil around the point ABSOLUTE_OFF.
97
98  */
99 void
100 Stencil::rotate_degrees_absolute (Real a, Offset absolute_off)
101 {
102   const Real x = absolute_off[X_AXIS];
103   const Real y = absolute_off[Y_AXIS];
104
105   /*
106    * Build scheme expression (processed in stencil-interpret.cc)
107    */
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
111    *         Stencil::rotated()
112    * and have Stencil::rotate be an abbrev of
113    *         *this = rotated()
114    */
115
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);
120
121   /*
122    * Calculate the new bounding box
123    */
124   Box shifted_box = extent_box ();
125   shifted_box.translate (-absolute_off);
126
127   vector<Offset> pts;
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)));
132
133   const Offset rot = complex_exp (Offset (0, a * M_PI / 180.0));
134   dim_.set_empty ();
135   for (vsize i = 0; i < pts.size (); i++)
136     dim_.add_point (pts[i] * rot + absolute_off);
137 }
138
139 /*
140   Rotate this stencil around the point RELATIVE_OFF.
141
142   RELATIVE_OFF is measured in terms of the extent of the stencil, so
143   -1 = LEFT/DOWN edge, 1 = RIGHT/UP edge.
144  */
145 void
146 Stencil::rotate_degrees (Real a, Offset relative_off)
147 {
148   /*
149    * Calculate the center of rotation
150    */
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));
154 }
155
156 void
157 Stencil::translate (Offset o)
158 {
159   Axis a = X_AXIS;
160   while (a < NO_AXES)
161     {
162       if (isinf (o[a])
163           || isnan (o[a])
164
165           // ugh, hardcoded.
166           || fabs (o[a]) > 1e6)
167         {
168           programming_error (String_convert::form_string ("Improbable offset for stencil: %f staff space", o[a])
169                              + "\n"
170                              + "Setting to zero.");
171           o[a] = 0.0;
172           if (strict_infinity_checking)
173             scm_misc_error (__FUNCTION__, "Improbable offset.", SCM_EOL);
174         }
175       incr (a);
176     }
177
178   if (!scm_is_null (expr_))
179     expr_ = scm_list_n (ly_symbol2scm ("translate-stencil"),
180                         ly_offset2scm (o),
181                         expr_, SCM_UNDEFINED);
182   if (!is_empty ())
183     dim_.translate (o);
184 }
185
186 void
187 Stencil::translate_axis (Real x, Axis a)
188 {
189   Offset o (0, 0);
190   o[a] = x;
191   translate (o);
192 }
193
194 void
195 Stencil::scale (Real x, Real y)
196 {
197   expr_ = scm_list_3 (ly_symbol2scm ("scale-stencil"),
198                       scm_list_2 (scm_from_double (x),
199                                   scm_from_double (y)),
200                       expr_);
201   dim_[X_AXIS] *= x;
202   dim_[Y_AXIS] *= y;
203 }
204
205 void
206 Stencil::add_stencil (Stencil const &s)
207 {
208   SCM cs = ly_symbol2scm ("combine-stencil");
209   if (scm_is_null (expr_))
210     expr_ = s.expr_;
211   else if (scm_is_null (s.expr_))
212     ;
213   else if (scm_is_pair (expr_)
214       && scm_is_eq (cs, scm_car (expr_)))
215     {
216       if (scm_is_pair (s.expr_)
217           && scm_is_eq (cs, scm_car (s.expr_)))
218         expr_ = scm_append (scm_list_2 (s.expr_, scm_cdr (expr_)));
219       else
220         expr_ = scm_cons2 (cs, s.expr_, scm_cdr (expr_));
221     }
222   else
223     {
224       if (scm_is_pair (s.expr_)
225           && scm_is_eq (cs, scm_car (s.expr_)))
226         expr_ = scm_append (scm_list_2 (s.expr_, scm_list_1 (expr_)));
227       else
228         expr_ = scm_list_3 (cs, s.expr_, expr_);
229     }
230   dim_.unite (s.dim_);
231 }
232
233 void
234 Stencil::set_empty (bool e)
235 {
236   if (e)
237     {
238       dim_[X_AXIS].set_empty ();
239       dim_[Y_AXIS].set_empty ();
240     }
241   else
242     {
243       dim_[X_AXIS] = Interval (0, 0);
244       dim_[Y_AXIS] = Interval (0, 0);
245     }
246 }
247
248 void
249 Stencil::align_to (Axis a, Real x)
250 {
251   if (is_empty (a))
252     return;
253
254   Interval i (extent (a));
255   translate_axis (-i.linear_combination (x), a);
256 }
257
258 /*  See scheme Function.  */
259
260 // Any stencil that is empty in the orthogonal axis is spacing.
261 // Spacing is not subjected to the max (0) rule and can thus be
262 // negative.
263
264 void
265 Stencil::add_at_edge (Axis a, Direction d, Stencil const &s, Real padding)
266 {
267   // Material that is empty in the axis of reference has only limited
268   // usefulness for combining.  We still retain as much information as
269   // available since there may be uses like setting page links or
270   // background color or watermarks, and off-axis extents.
271
272   if (is_empty (a))
273     {
274       add_stencil (s);
275       return;
276     }
277
278   Interval first_extent = extent (a);
279
280   if (s.is_empty (a))
281     {
282       Stencil toadd (s);
283       // translation does not affect axis-empty extent box.
284       toadd.translate_axis (first_extent[d], a);
285       add_stencil (toadd);
286       return;
287     }
288
289   Interval next_extent = s.extent (a);
290
291   bool first_is_spacing = is_empty (other_axis (a));
292   bool next_is_spacing = s.is_empty (other_axis (a));
293
294   Real offset = first_extent[d] - next_extent[-d];
295
296   if (!(first_is_spacing || next_is_spacing))
297     {
298       offset += d * padding;
299     }
300
301   Stencil toadd (s);
302   toadd.translate_axis (offset, a);
303   add_stencil (toadd);
304 }
305
306 // Stencil::stack is mainly used for assembling lines or columns
307 // of stencils.  For the most common case of adding at the right, the
308 // reference point of the added stencil is usually placed at the right
309 // edge of the current one, unless the added stencil has a negative
310 // left extent in which case its left edge is placed at the right edge
311 // of the current one.
312 //
313 // Spacing is special in that it is applied without padding.  Spacing
314 // at the right edge shifts the right edge accordingly.
315 //
316 // For spacing at the left edge, there are several approaches.  In
317 // order to get to predictable behavior, we want to have at least a
318 // continuous approach.  An obvious idea is to do a "translate" by the
319 // appropriate amount.  Doing that while retaining the nominal left
320 // edge seems like the most straightforward way.
321
322 void
323 Stencil::stack (Axis a, Direction d, Stencil const &s, Real padding, Real mindist)
324 {
325   // Material that is empty in the axis of reference can't be sensibly
326   // stacked.  We just revert to add_at_edge behavior then.
327
328   if (is_empty (a))
329     {
330       Stencil toadd (s);
331       toadd.add_stencil (*this);
332       expr_ = toadd.expr ();
333       dim_ = toadd.extent_box ();
334       return;
335     }
336
337   Interval first_extent = extent (a);
338
339   if (s.is_empty (a))
340     {
341       Stencil toadd (s);
342       toadd.translate_axis (first_extent[d], a);
343       toadd.add_stencil (*this);
344       expr_ = toadd.expr ();
345       dim_ = toadd.extent_box ();
346       return;
347     }
348
349   Interval next_extent = s.extent (a);
350
351   // It is somewhat tedious to special-case all spacing, but it turns
352   // out that not doing so makes it astonishingly hard to make the
353   // code do the correct thing.
354
355   // If first is spacing, we translate second accordingly without
356   // letting this affect its backward edge.
357   if (is_empty (other_axis (a)))
358     {
359       Stencil toadd (s);
360       Real offset = d * first_extent.delta ();
361       toadd.translate_axis (offset, a);
362       toadd.add_stencil (*this);
363       expr_ = toadd.expr ();
364       dim_ = toadd.extent_box ();
365       dim_[a][-d] = next_extent[-d];
366       dim_[a][d] = next_extent[d] + offset;
367       return;
368     }
369
370   // If next is spacing, similar action:
371   if (s.is_empty (other_axis (a)))
372     {
373       Stencil toadd (s);
374       Real offset = first_extent [d];
375       toadd.translate_axis (offset, a);
376       toadd.add_stencil (*this);
377       expr_ = toadd.expr ();
378       dim_ = toadd.extent_box ();
379       dim_[a][-d] = first_extent[-d];
380       dim_[a][d] = first_extent[d] + d * next_extent.delta ();
381       return;
382     }
383
384
385   Real offset = first_extent[d];
386
387   // If the added stencil has a backwardly protruding edge, we make
388   // room for it when combining.
389
390   if (d * next_extent [-d] < 0)
391     offset -= next_extent [-d];
392
393   offset += d * padding;
394
395   if (offset * d < mindist)
396     offset = d * mindist;
397
398   Stencil toadd (s);
399   toadd.translate_axis (offset, a);
400   toadd.add_stencil (*this);
401   expr_ = toadd.expr ();
402   dim_ = toadd.extent_box ();
403   dim_[a][-d] = first_extent [-d];
404   dim_[a][d] = next_extent [d] + offset;
405 }
406
407
408 Stencil
409 Stencil::in_color (Real r, Real g, Real b) const
410 {
411   Stencil new_stencil (extent_box (),
412                        scm_list_3 (ly_symbol2scm ("color"),
413                                    scm_list_3 (scm_from_double (r),
414                                                scm_from_double (g),
415                                                scm_from_double (b)),
416                                    expr ()));
417   return new_stencil;
418 }
419
420 /* convenience */
421 Stencil
422 Stencil::translated (Offset z) const
423 {
424   Stencil s (*this);
425   s.translate (z);
426   return s;
427 }