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