]> git.donarmstrong.com Git - lilypond.git/blob - lily/stencil.cc
Merge master into nested-bookparts
[lilypond.git] / lily / stencil.cc
1 /*
2   stencil.cc -- implement Stencil
3
4   source file of the GNU LilyPond music typesetter
5
6   (c) 1997--2007 Han-Wen Nienhuys <hanwen@xs4all.nl>
7 */
8
9 #include "stencil.hh"
10
11 #include "main.hh"
12 #include "font-metric.hh"
13 #include "input.hh"
14 #include "string-convert.hh"
15 #include "warn.hh"
16
17 #include "ly-smobs.icc"
18
19 Stencil::Stencil ()
20 {
21   expr_ = SCM_EOL;
22   set_empty (true);
23 }
24
25 Stencil::Stencil (Box b, SCM func)
26 {
27   expr_ = func;
28   dim_ = b;
29 }
30
31 int
32 Stencil::print_smob (SCM, SCM port, scm_print_state *)
33 {
34   scm_puts ("#<Stencil ", port);
35   scm_puts (" >", port);
36   return 1;
37 }
38
39 SCM
40 Stencil::mark_smob (SCM smob)
41 {
42   Stencil *s = (Stencil *) SCM_CELL_WORD_1 (smob);
43   return s->expr_;
44 }
45
46 IMPLEMENT_SIMPLE_SMOBS (Stencil);
47 IMPLEMENT_TYPE_P (Stencil, "ly:stencil?");
48 IMPLEMENT_DEFAULT_EQUAL_P (Stencil);
49
50 Interval
51 Stencil::extent (Axis a) const
52 {
53   return dim_[a];
54 }
55
56 bool
57 Stencil::is_empty () const
58 {
59   return (expr_ == SCM_EOL
60           || dim_[X_AXIS].is_empty ()
61           || dim_[Y_AXIS].is_empty ());
62 }
63
64 SCM
65 Stencil::expr () const
66 {
67   return expr_;
68 }
69
70 Box
71 Stencil::extent_box () const
72 {
73   return dim_;
74 }
75
76 void
77 Stencil::rotate (Real a, Offset off)
78 {
79   rotate_degrees (a * 180/M_PI, off); 
80 }
81
82 /*
83   Rotate this stencil around the point ABSOLUTE_OFF.
84
85  */
86 void
87 Stencil::rotate_degrees_absolute (Real a, Offset absolute_off)
88 {
89   const Real x = absolute_off[X_AXIS];
90   const Real y = absolute_off[Y_AXIS];
91
92   /*
93    * Build scheme expression (processed in stencil-interpret.cc)
94    */
95   /* TODO: by hanwenn 2008/09/10 14:38:56:
96    * in effect, this copies the underlying expression.  It might be a
97    * little bit nicer to mirror this in the api, ie. make a
98    *         Stencil::rotated()
99    * and have Stencil::rotate be an abbrev of
100    *         *this = rotated()
101    */
102
103   expr_ = scm_list_n (ly_symbol2scm ("rotate-stencil"),
104                       scm_list_2 (scm_from_double (a),
105                       scm_cons (scm_from_double (x), scm_from_double (y))),
106                       expr_, SCM_UNDEFINED);
107
108   /*
109    * Calculate the new bounding box
110    */
111   Box shifted_box = extent_box ();
112   shifted_box.translate (-absolute_off);
113
114   vector<Offset> pts;
115   pts.push_back (Offset (shifted_box.x ().at(LEFT), shifted_box.y ().at(DOWN)));
116   pts.push_back (Offset (shifted_box.x ().at(RIGHT), shifted_box.y ().at(DOWN)));
117   pts.push_back (Offset (shifted_box.x ().at(RIGHT), shifted_box.y ().at(UP)));
118   pts.push_back (Offset (shifted_box.x ().at(LEFT), shifted_box.y ().at(UP)));
119
120   const Offset rot = complex_exp (Offset (0, a * M_PI / 180.0));
121   dim_.set_empty ();
122   for (vsize i = 0; i < pts.size (); i++)
123     dim_.add_point (pts[i] * rot + absolute_off);
124 }
125
126 /*
127   Rotate this stencil around the point RELATIVE_OFF.
128
129   RELATIVE_OFF is measured in terms of the extent of the stencil, so
130   -1 = LEFT/DOWN edge, 1 = RIGHT/UP edge.
131  */
132 void
133 Stencil::rotate_degrees (Real a, Offset relative_off)
134 {
135   /*
136    * Calculate the center of rotation
137    */
138   const Real x = extent (X_AXIS).linear_combination (relative_off[X_AXIS]);
139   const Real y = extent (Y_AXIS).linear_combination (relative_off[Y_AXIS]);
140   rotate_degrees_absolute (a, Offset (x, y));
141 }
142
143 void
144 Stencil::translate (Offset o)
145 {
146   Axis a = X_AXIS;
147   while (a < NO_AXES)
148     {
149       if (isinf (o[a])
150           || isnan (o[a])
151
152           // ugh, hardcoded. 
153           || fabs (o[a]) > 1e6)
154         {
155           programming_error (String_convert::form_string ("Improbable offset for stencil: %f staff space", o[a])
156                              + "\n"
157                              + "Setting to zero.");
158           o[a] = 0.0;
159           if (strict_infinity_checking)
160             scm_misc_error (__FUNCTION__, "Improbable offset.", SCM_EOL);
161         }
162       incr (a);
163     }
164
165   expr_ = scm_list_n (ly_symbol2scm ("translate-stencil"),
166                       ly_offset2scm (o),
167                       expr_, SCM_UNDEFINED);
168   if (!is_empty ())
169     dim_.translate (o);
170 }
171
172 void
173 Stencil::translate_axis (Real x, Axis a)
174 {
175   Offset o (0, 0);
176   o[a] = x;
177   translate (o);
178 }
179
180 void
181 Stencil::add_stencil (Stencil const &s)
182 {
183   expr_ = scm_list_3 (ly_symbol2scm ("combine-stencil"), s.expr_, expr_);
184   dim_.unite (s.dim_);
185 }
186
187 void
188 Stencil::set_empty (bool e)
189 {
190   if (e)
191     {
192       dim_[X_AXIS].set_empty ();
193       dim_[Y_AXIS].set_empty ();
194     }
195   else
196     {
197       dim_[X_AXIS] = Interval (0, 0);
198       dim_[Y_AXIS] = Interval (0, 0);
199     }
200 }
201
202 void
203 Stencil::align_to (Axis a, Real x)
204 {
205   if (is_empty ())
206     return;
207
208   Interval i (extent (a));
209   translate_axis (-i.linear_combination (x), a);
210 }
211
212 /*  See scheme Function.  */
213 void
214 Stencil::add_at_edge (Axis a, Direction d, Stencil const &s, Real padding)
215 {
216   Interval my_extent = dim_[a];
217   Interval i (s.extent (a));
218   Real his_extent;
219   if (i.is_empty ())
220     {
221       programming_error ("Stencil::add_at_edge: adding empty stencil.");
222       his_extent = 0.0;
223     }
224   else
225     his_extent = i[-d];
226
227   Real offset = (my_extent.is_empty () ? 0.0 : my_extent[d] - his_extent)
228     + d * padding;
229
230   Stencil toadd (s);
231   toadd.translate_axis (offset, a);
232   add_stencil (toadd);
233 }
234
235 Stencil
236 Stencil::in_color (Real r, Real g, Real b) const
237 {
238   Stencil new_stencil (extent_box (),
239                        scm_list_3 (ly_symbol2scm ("color"),
240                                    scm_list_3 (scm_from_double (r),
241                                                scm_from_double (g),
242                                                scm_from_double (b)),
243                                    expr ()));
244   return new_stencil;
245 }
246
247 /* convenience */
248 Stencil
249 Stencil::translated (Offset z) const
250 {
251   Stencil s (*this);
252   s.translate (z);
253   return s;
254 }