]> git.donarmstrong.com Git - lilypond.git/blob - lily/stencil.cc
Let Stencil::is_empty use Box::is_empty
[lilypond.git] / lily / stencil.cc
1 /*
2   This file is part of LilyPond, the GNU music typesetter.
3
4   Copyright (C) 1997--2012 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 #include "ly-smobs.icc"
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 int
43 Stencil::print_smob (SCM, SCM port, scm_print_state *)
44 {
45   scm_puts ("#<Stencil ", port);
46   scm_puts (" >", port);
47   return 1;
48 }
49
50 SCM
51 Stencil::mark_smob (SCM smob)
52 {
53   Stencil *s = (Stencil *) SCM_CELL_WORD_1 (smob);
54   return s->expr_;
55 }
56
57 IMPLEMENT_SIMPLE_SMOBS (Stencil);
58 IMPLEMENT_TYPE_P (Stencil, "ly:stencil?");
59 IMPLEMENT_DEFAULT_EQUAL_P (Stencil);
60
61 Interval
62 Stencil::extent (Axis a) const
63 {
64   return dim_[a];
65 }
66
67 bool
68 Stencil::is_empty () const
69 {
70   return (expr_ == SCM_EOL
71           || dim_.is_empty ());
72 }
73
74 SCM
75 Stencil::expr () const
76 {
77   return expr_;
78 }
79
80 Box
81 Stencil::extent_box () const
82 {
83   return dim_;
84 }
85
86 void
87 Stencil::rotate (Real a, Offset off)
88 {
89   rotate_degrees (a * 180 / M_PI, off);
90 }
91
92 /*
93   Rotate this stencil around the point ABSOLUTE_OFF.
94
95  */
96 void
97 Stencil::rotate_degrees_absolute (Real a, Offset absolute_off)
98 {
99   const Real x = absolute_off[X_AXIS];
100   const Real y = absolute_off[Y_AXIS];
101
102   /*
103    * Build scheme expression (processed in stencil-interpret.cc)
104    */
105   /* TODO: by hanwenn 2008/09/10 14:38:56:
106    * in effect, this copies the underlying expression.  It might be a
107    * little bit nicer to mirror this in the api, ie. make a
108    *         Stencil::rotated()
109    * and have Stencil::rotate be an abbrev of
110    *         *this = rotated()
111    */
112
113   expr_ = scm_list_n (ly_symbol2scm ("rotate-stencil"),
114                       scm_list_2 (scm_from_double (a),
115                                   scm_cons (scm_from_double (x), scm_from_double (y))),
116                       expr_, SCM_UNDEFINED);
117
118   /*
119    * Calculate the new bounding box
120    */
121   Box shifted_box = extent_box ();
122   shifted_box.translate (-absolute_off);
123
124   vector<Offset> pts;
125   pts.push_back (Offset (shifted_box.x ().at (LEFT), shifted_box.y ().at (DOWN)));
126   pts.push_back (Offset (shifted_box.x ().at (RIGHT), shifted_box.y ().at (DOWN)));
127   pts.push_back (Offset (shifted_box.x ().at (RIGHT), shifted_box.y ().at (UP)));
128   pts.push_back (Offset (shifted_box.x ().at (LEFT), shifted_box.y ().at (UP)));
129
130   const Offset rot = complex_exp (Offset (0, a * M_PI / 180.0));
131   dim_.set_empty ();
132   for (vsize i = 0; i < pts.size (); i++)
133     dim_.add_point (pts[i] * rot + absolute_off);
134 }
135
136 /*
137   Rotate this stencil around the point RELATIVE_OFF.
138
139   RELATIVE_OFF is measured in terms of the extent of the stencil, so
140   -1 = LEFT/DOWN edge, 1 = RIGHT/UP edge.
141  */
142 void
143 Stencil::rotate_degrees (Real a, Offset relative_off)
144 {
145   /*
146    * Calculate the center of rotation
147    */
148   const Real x = extent (X_AXIS).linear_combination (relative_off[X_AXIS]);
149   const Real y = extent (Y_AXIS).linear_combination (relative_off[Y_AXIS]);
150   rotate_degrees_absolute (a, Offset (x, y));
151 }
152
153 void
154 Stencil::translate (Offset o)
155 {
156   Axis a = X_AXIS;
157   while (a < NO_AXES)
158     {
159       if (isinf (o[a])
160           || isnan (o[a])
161
162           // ugh, hardcoded.
163           || fabs (o[a]) > 1e6)
164         {
165           programming_error (String_convert::form_string ("Improbable offset for stencil: %f staff space", o[a])
166                              + "\n"
167                              + "Setting to zero.");
168           o[a] = 0.0;
169           if (strict_infinity_checking)
170             scm_misc_error (__FUNCTION__, "Improbable offset.", SCM_EOL);
171         }
172       incr (a);
173     }
174
175   if (!scm_is_null (expr_))
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);
181 }
182
183 void
184 Stencil::translate_axis (Real x, Axis a)
185 {
186   Offset o (0, 0);
187   o[a] = x;
188   translate (o);
189 }
190
191 void
192 Stencil::scale (Real x, Real y)
193 {
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;
200 }
201
202 void
203 Stencil::add_stencil (Stencil const &s)
204 {
205   SCM cs = ly_symbol2scm ("combine-stencil");
206   if (scm_is_null (expr_))
207     expr_ = s.expr_;
208   else if (scm_is_null (s.expr_))
209     ;
210   else if (scm_is_pair (expr_)
211       && scm_is_eq (cs, scm_car (expr_)))
212     {
213       if (scm_is_pair (s.expr_)
214           && scm_is_eq (cs, scm_car (s.expr_)))
215         expr_ = scm_append (scm_list_2 (s.expr_, scm_cdr (expr_)));
216       else
217         expr_ = scm_cons2 (cs, s.expr_, scm_cdr (expr_));
218     }
219   else
220     {
221       if (scm_is_pair (s.expr_)
222           && scm_is_eq (cs, scm_car (s.expr_)))
223         expr_ = scm_append (scm_list_2 (s.expr_, scm_list_1 (expr_)));
224       else
225         expr_ = scm_list_3 (cs, s.expr_, expr_);
226     }
227   dim_.unite (s.dim_);
228 }
229
230 void
231 Stencil::set_empty (bool e)
232 {
233   if (e)
234     {
235       dim_[X_AXIS].set_empty ();
236       dim_[Y_AXIS].set_empty ();
237     }
238   else
239     {
240       dim_[X_AXIS] = Interval (0, 0);
241       dim_[Y_AXIS] = Interval (0, 0);
242     }
243 }
244
245 void
246 Stencil::align_to (Axis a, Real x)
247 {
248   if (is_empty ())
249     return;
250
251   Interval i (extent (a));
252   translate_axis (-i.linear_combination (x), a);
253 }
254
255 /*  See scheme Function.  */
256 void
257 Stencil::add_at_edge (Axis a, Direction d, Stencil const &s, Real padding)
258 {
259   Interval my_extent = dim_[a];
260   Interval i (s.extent (a));
261   Real his_extent;
262   if (i.is_empty ())
263     {
264       programming_error ("Stencil::add_at_edge: adding empty stencil.");
265       his_extent = 0.0;
266     }
267   else
268     his_extent = i[-d];
269
270   Real offset = (my_extent.is_empty () ? 0.0 : my_extent[d] - his_extent)
271                 + d * padding;
272
273   Stencil toadd (s);
274   toadd.translate_axis (offset, a);
275   add_stencil (toadd);
276 }
277
278 Stencil
279 Stencil::in_color (Real r, Real g, Real b) const
280 {
281   Stencil new_stencil (extent_box (),
282                        scm_list_3 (ly_symbol2scm ("color"),
283                                    scm_list_3 (scm_from_double (r),
284                                                scm_from_double (g),
285                                                scm_from_double (b)),
286                                    expr ()));
287   return new_stencil;
288 }
289
290 /* convenience */
291 Stencil
292 Stencil::translated (Offset z) const
293 {
294   Stencil s (*this);
295   s.translate (z);
296   return s;
297 }