]> git.donarmstrong.com Git - lilypond.git/blob - lily/stencil.cc
use _degrees to signify functions taking degree angles.
[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 [x, y]
84  */
85 void
86 Stencil::rotate_degrees (Real a, Offset off)
87 {
88   const Real x_cen = extent (X_AXIS).center ();
89   const Real y_cen = extent (Y_AXIS).center ();
90
91   /*
92    * Calculate the center of rotation
93    */
94   const Real x = x_cen + off[X_AXIS] * x_cen;
95   const Real y = y_cen + off[Y_AXIS] * y_cen;
96
97   /*
98    * Build scheme expression (processed in stencil-interpret.cc)
99    */
100   expr_ = scm_list_n (ly_symbol2scm ("rotate-stencil"),
101                       scm_list_2 (scm_from_double (a),
102                       scm_cons (scm_from_double (x), scm_from_double (y))),
103                       expr_, SCM_UNDEFINED);
104
105   /*
106    * Calculate the new bounding box
107    */
108   vector<Offset> pts;
109   pts.push_back (Offset (-x_cen, -y_cen));
110   pts.push_back (Offset (x_cen, -y_cen));
111   pts.push_back (Offset (x_cen, y_cen));
112   pts.push_back (Offset (-x_cen, y_cen));
113
114   const Offset rot = complex_exp (Offset (0, a * M_PI / 180.0));
115   dim_.set_empty ();
116   for (vsize i = 0; i < pts.size (); i++)
117     dim_.add_point (pts[i] * rot + Offset (x_cen, y_cen));
118 }
119
120 void
121 Stencil::translate (Offset o)
122 {
123   Axis a = X_AXIS;
124   while (a < NO_AXES)
125     {
126       if (isinf (o[a])
127           || isnan (o[a])
128
129           // ugh, hardcoded. 
130           || fabs (o[a]) > 1e6)
131         {
132           programming_error (String_convert::form_string ("Improbable offset for stencil: %f staff space", o[a])
133                              + "\n"
134                              + "Setting to zero.");
135           o[a] = 0.0;
136           if (strict_infinity_checking)
137             scm_misc_error (__FUNCTION__, "Improbable offset.", SCM_EOL);
138         }
139       incr (a);
140     }
141
142   expr_ = scm_list_n (ly_symbol2scm ("translate-stencil"),
143                       ly_offset2scm (o),
144                       expr_, SCM_UNDEFINED);
145   if (!is_empty ())
146     dim_.translate (o);
147 }
148
149 void
150 Stencil::translate_axis (Real x, Axis a)
151 {
152   Offset o (0, 0);
153   o[a] = x;
154   translate (o);
155 }
156
157 void
158 Stencil::add_stencil (Stencil const &s)
159 {
160   expr_ = scm_list_3 (ly_symbol2scm ("combine-stencil"), s.expr_, expr_);
161   dim_.unite (s.dim_);
162 }
163
164 void
165 Stencil::set_empty (bool e)
166 {
167   if (e)
168     {
169       dim_[X_AXIS].set_empty ();
170       dim_[Y_AXIS].set_empty ();
171     }
172   else
173     {
174       dim_[X_AXIS] = Interval (0, 0);
175       dim_[Y_AXIS] = Interval (0, 0);
176     }
177 }
178
179 void
180 Stencil::align_to (Axis a, Real x)
181 {
182   if (is_empty ())
183     return;
184
185   Interval i (extent (a));
186   translate_axis (-i.linear_combination (x), a);
187 }
188
189 /*  See scheme Function.  */
190 void
191 Stencil::add_at_edge (Axis a, Direction d, Stencil const &s, Real padding)
192 {
193   Interval my_extent = dim_[a];
194   Interval i (s.extent (a));
195   Real his_extent;
196   if (i.is_empty ())
197     {
198       programming_error ("Stencil::moved_to_edge: adding empty stencil.");
199       his_extent = 0.0;
200     }
201   else
202     his_extent = i[-d];
203
204   Real offset = (my_extent.is_empty () ? 0.0 : my_extent[d] - his_extent)
205     + d * padding;
206
207   Stencil toadd (s);
208   toadd.translate_axis (offset, a);
209   add_stencil (toadd);
210 }
211
212 Stencil
213 Stencil::in_color (Real r, Real g, Real b) const
214 {
215   Stencil new_stencil (extent_box (),
216                        scm_list_3 (ly_symbol2scm ("color"),
217                                    scm_list_3 (scm_from_double (r),
218                                                scm_from_double (g),
219                                                scm_from_double (b)),
220                                    expr ()));
221   return new_stencil;
222 }
223
224 /* convenience */
225 Stencil
226 Stencil::translated (Offset z) const
227 {
228   Stencil s (*this);
229   s.translate (z);
230   return s;
231 }