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