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