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