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