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