]> git.donarmstrong.com Git - lilypond.git/blob - lily/stencil.cc
c8985105ea44f0216ca58a3153a31cb89f3b55f2
[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 (a))
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, and off-axis extents.
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       // translation does not affect axis-empty extent box.
287       toadd.translate_axis (first_extent[d], a);
288       add_stencil (toadd);
289       return;
290     }
291
292   Interval next_extent = s.extent (a);
293
294   bool first_is_spacing = is_empty (other_axis (a));
295   bool next_is_spacing = s.is_empty (other_axis (a));
296
297   Real offset = first_extent[d] - next_extent[-d];
298
299   if (!(first_is_spacing || next_is_spacing))
300     {
301       offset += d * padding;
302     }
303
304   Stencil toadd (s);
305   toadd.translate_axis (offset, a);
306   add_stencil (toadd);
307 }
308
309 // Stencil::stack is mainly used for assembling lines or columns
310 // of stencils.  For the most common case of adding at the right, the
311 // reference point of the added stencil is usually placed at the right
312 // edge of the current one, unless the added stencil has a negative
313 // left extent in which case its left edge is placed at the right edge
314 // of the current one.
315 //
316 // Spacing is special in that it is applied without padding.  Spacing
317 // at the right edge shifts the right edge accordingly.
318 //
319 // For spacing at the left edge, there are several approaches.  In
320 // order to get to predictable behavior, we want to have at least a
321 // continuous approach.  An obvious idea is to do a "translate" by the
322 // appropriate amount.  Doing that while retaining the nominal left
323 // edge seems like the most straightforward way.
324
325 void
326 Stencil::stack (Axis a, Direction d, Stencil const &s, Real padding, Real mindist)
327 {
328   // Material that is empty in the axis of reference can't be sensibly
329   // stacked.  We just revert to add_at_edge behavior then.
330
331   if (is_empty (a))
332     {
333       Stencil toadd (s);
334       toadd.add_stencil (*this);
335       expr_ = toadd.expr ();
336       dim_ = toadd.extent_box ();
337       return;
338     }
339
340   Interval first_extent = extent (a);
341
342   if (s.is_empty (a))
343     {
344       Stencil toadd (s);
345       toadd.translate_axis (first_extent[d], a);
346       toadd.add_stencil (*this);
347       expr_ = toadd.expr ();
348       dim_ = toadd.extent_box ();
349       return;
350     }
351
352   Interval next_extent = s.extent (a);
353
354   // It is somewhat tedious to special-case all spacing, but it turns
355   // out that not doing so makes it astonishingly hard to make the
356   // code do the correct thing.
357
358   // If first is spacing, we translate second accordingly without
359   // letting this affect its backward edge.
360   if (is_empty (other_axis (a)))
361     {
362       Stencil toadd (s);
363       Real offset = d * first_extent.delta ();
364       toadd.translate_axis (offset, a);
365       toadd.add_stencil (*this);
366       expr_ = toadd.expr ();
367       dim_ = toadd.extent_box ();
368       dim_[a][-d] = next_extent[-d];
369       dim_[a][d] = next_extent[d] + offset;
370       return;
371     }
372
373   // If next is spacing, similar action:
374   if (s.is_empty (other_axis (a)))
375     {
376       Stencil toadd (s);
377       Real offset = first_extent [d];
378       toadd.translate_axis (offset, a);
379       toadd.add_stencil (*this);
380       expr_ = toadd.expr ();
381       dim_ = toadd.extent_box ();
382       dim_[a][-d] = first_extent[-d];
383       dim_[a][d] = first_extent[d] + d * next_extent.delta ();
384       return;
385     }
386
387
388   Real offset = first_extent[d];
389
390   // If the added stencil has a backwardly protruding edge, we make
391   // room for it when combining.
392
393   if (d * next_extent [-d] < 0)
394     offset -= next_extent [-d];
395
396   offset += d * padding;
397
398   if (offset * d < mindist)
399     offset = d * mindist;
400
401   Stencil toadd (s);
402   toadd.translate_axis (offset, a);
403   toadd.add_stencil (*this);
404   expr_ = toadd.expr ();
405   dim_ = toadd.extent_box ();
406   dim_[a][-d] = first_extent [-d];
407   dim_[a][d] = next_extent [d] + offset;
408 }
409
410
411 Stencil
412 Stencil::in_color (Real r, Real g, Real b) const
413 {
414   Stencil new_stencil (extent_box (),
415                        scm_list_3 (ly_symbol2scm ("color"),
416                                    scm_list_3 (scm_from_double (r),
417                                                scm_from_double (g),
418                                                scm_from_double (b)),
419                                    expr ()));
420   return new_stencil;
421 }
422
423 /* convenience */
424 Stencil
425 Stencil::translated (Offset z) const
426 {
427   Stencil s (*this);
428   s.translate (z);
429   return s;
430 }