]> git.donarmstrong.com Git - lilypond.git/blob - lily/stencil.cc
Fix some bugs in the dynamic engraver and PostScript backend
[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--2006 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-smob.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 Offset
76 Stencil::origin () const
77 {
78   return origin_;
79 }
80
81 /*
82  * Rotate this stencil around the point [x, y]
83  */
84 void
85 Stencil::rotate (Real a, Offset off)
86 {
87   const Real x_cen = extent (X_AXIS).center ();
88   const Real y_cen = extent (Y_AXIS).center ();
89
90   /*
91    * Calculate the center of rotation
92    */
93   const Real x = x_cen + off[X_AXIS] * x_cen;
94   const Real y = y_cen + off[Y_AXIS] * y_cen;
95
96   /*
97    * Build scheme expression (processed in stencil-interpret.cc)
98    */
99   expr_ = scm_list_n (ly_symbol2scm ("rotate-stencil"),
100                       scm_list_2 (scm_from_double (a),
101                       scm_cons (scm_from_double (x), scm_from_double (y))),
102                       expr_, SCM_UNDEFINED);
103
104   /*
105    * Calculate the new bounding box
106    */
107   vector<Offset> pts;
108   pts.push_back (Offset (-x_cen, -y_cen));
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
113   const Offset rot = complex_exp (Offset (0, a * M_PI / 180.0));
114   dim_.set_empty ();
115   for (vsize i = 0; i < pts.size (); i++)
116     dim_.add_point (pts[i] * rot + Offset (x_cen, y_cen));
117 }
118
119 void
120 Stencil::translate (Offset o)
121 {
122   Axis a = X_AXIS;
123   while (a < NO_AXES)
124     {
125       if (isinf (o[a])
126           || isnan (o[a])
127
128           // ugh, hardcoded. 
129           || fabs (o[a]) > 1e6)
130         {
131           programming_error (String_convert::form_string ("Improbable offset for stencil: %f staff space", o[a])
132                              + "\n"
133                              + "Setting to zero.");
134           o[a] = 0.0;
135           if (strict_infinity_checking)
136             scm_misc_error (__FUNCTION__, "Improbable offset.", SCM_EOL);
137         }
138       incr (a);
139     }
140
141   expr_ = scm_list_n (ly_symbol2scm ("translate-stencil"),
142                       ly_offset2scm (o),
143                       expr_, SCM_UNDEFINED);
144   if (!is_empty ())
145     dim_.translate (o);
146   origin_ += 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 /* FIXME: unintuitive naming, you would expect *this to be moved.
190    Kept (keeping?) API for compat with add_at_edge ().
191
192    What is PADDING, what is MINIMUM, exactly?  */
193 Stencil
194 Stencil::moved_to_edge (Axis a, Direction d, Stencil const &s,
195                         Real padding, Real minimum) const
196 {
197   Interval my_extent = dim_[a];
198   Interval i (s.extent (a));
199   Real his_extent;
200   if (i.is_empty ())
201     {
202       programming_error ("Stencil::moved_to_edge: adding empty stencil.");
203       his_extent = 0.0;
204       //      SCM_ASSERT_TYPE (0, s.expr (), SCM_ARG1, __FUNCTION__, "non-empty stencil");
205     }
206   else
207     his_extent = i[-d];
208
209   Real offset = (my_extent.is_empty () ? 0.0 : my_extent[d] - his_extent)
210     + d * padding;
211
212   Stencil toadd (s);
213   toadd.translate_axis (offset, a);
214
215   if (minimum > 0 && d * (-origin ()[a] + toadd.origin ()[a]) < minimum)
216     toadd.translate_axis (-toadd.origin ()[a]
217                           + origin ()[a] + d * minimum, a);
218
219   return toadd;
220 }
221
222 /*  See scheme Function.  */
223 void
224 Stencil::add_at_edge (Axis a, Direction d, Stencil const &s, Real padding,
225                       Real minimum)
226 {
227   add_stencil (moved_to_edge (a, d, s, padding, minimum));
228 }
229
230 Stencil
231 Stencil::in_color (Real r, Real g, Real b) const
232 {
233   Stencil new_stencil (extent_box (),
234                        scm_list_3 (ly_symbol2scm ("color"),
235                                    scm_list_3 (scm_from_double (r),
236                                                scm_from_double (g),
237                                                scm_from_double (b)),
238                                    expr ()));
239   return new_stencil;
240 }