]> git.donarmstrong.com Git - lilypond.git/blob - lily/stencil.cc
* lily/general-scheme.cc: remove my_{isinf,isnan}.
[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--2005 Han-Wen Nienhuys <hanwen@cs.uu.nl>
7 */
8
9 #include "stencil.hh"
10
11 #include <math.h>
12
13 #include "dimensions.hh"
14 #include "font-metric.hh"
15 #include "input-smob.hh"
16 #include "string-convert.hh"
17 #include "warn.hh"
18
19 #include "ly-smobs.icc"
20
21 Stencil::Stencil ()
22 {
23   expr_ = SCM_EOL;
24   set_empty (true);
25 }
26
27 Stencil::Stencil (Box b, SCM func)
28 {
29   expr_ = func;
30   dim_ = b;
31 }
32
33 int
34 Stencil::print_smob (SCM, SCM port, scm_print_state *)
35 {
36   scm_puts ("#<Stencil ", port);
37   scm_puts (" >", port);
38   return 1;
39 }
40
41 SCM
42 Stencil::mark_smob (SCM smob)
43 {
44   Stencil *s = (Stencil *) SCM_CELL_WORD_1 (smob);
45   return s->expr_;
46 }
47
48 IMPLEMENT_SIMPLE_SMOBS (Stencil);
49 IMPLEMENT_TYPE_P (Stencil, "ly:stencil?");
50 IMPLEMENT_DEFAULT_EQUAL_P (Stencil);
51
52 Interval
53 Stencil::extent (Axis a) const
54 {
55   return dim_[a];
56 }
57
58 bool
59 Stencil::is_empty () const
60 {
61   return (expr_ == SCM_EOL
62           || dim_[X_AXIS].is_empty ()
63           || dim_[Y_AXIS].is_empty ());
64 }
65
66 SCM
67 Stencil::expr () const
68 {
69   return expr_;
70 }
71
72 Box
73 Stencil::extent_box () const
74 {
75   return dim_;
76 }
77 Offset
78 Stencil::origin () const
79 {
80   return origin_;
81 }
82
83 void
84 Stencil::translate (Offset o)
85 {
86   Axis a = X_AXIS;
87   while (a < NO_AXES)
88     {
89       if (isinf (o[a])
90           || isnan (o[a])
91
92           // ugh, hardcoded. 
93           || fabs (o[a]) > 1e6)
94         {
95           programming_error (String_convert::form_string ("Improbable offset for stencil: %f staff space", o[a])
96                              + "\n"
97                              + "Setting to zero.");
98           o[a] = 0.0;
99         }
100       incr (a);
101     }
102
103   expr_ = scm_list_n (ly_symbol2scm ("translate-stencil"),
104                       ly_offset2scm (o),
105                       expr_, SCM_UNDEFINED);
106   if (!is_empty ())
107     dim_.translate (o);
108   origin_ += o;
109 }
110
111 void
112 Stencil::translate_axis (Real x, Axis a)
113 {
114   Offset o (0, 0);
115   o[a] = x;
116   translate (o);
117 }
118
119 void
120 Stencil::add_stencil (Stencil const &s)
121 {
122   expr_ = scm_list_3 (ly_symbol2scm ("combine-stencil"), s.expr_, expr_);
123   dim_.unite (s.dim_);
124 }
125
126 void
127 Stencil::set_empty (bool e)
128 {
129   if (e)
130     {
131       dim_[X_AXIS].set_empty ();
132       dim_[Y_AXIS].set_empty ();
133     }
134   else
135     {
136       dim_[X_AXIS] = Interval (0, 0);
137       dim_[Y_AXIS] = Interval (0, 0);
138     }
139 }
140
141 void
142 Stencil::align_to (Axis a, Real x)
143 {
144   if (is_empty ())
145     return;
146
147   Interval i (extent (a));
148   translate_axis (-i.linear_combination (x), a);
149 }
150
151 /* FIXME: unintuitive naming, you would expect *this to be moved.
152    Kept (keeping?) API for compat with add_at_edge ().
153
154    What is PADDING, what is MINIMUM, exactly?  */
155 Stencil
156 Stencil::moved_to_edge (Axis a, Direction d, Stencil const &s,
157                         Real padding, Real minimum) const
158 {
159   Interval my_extent = dim_[a];
160   Interval i (s.extent (a));
161   Real his_extent;
162   if (i.is_empty ())
163     {
164       programming_error ("Stencil::moved_to_edge: adding empty stencil.");
165       his_extent = 0.0;
166       //      SCM_ASSERT_TYPE (0, s.expr (), SCM_ARG1, __FUNCTION__, "non-empty stencil");
167     }
168   else
169     his_extent = i[-d];
170
171   Real offset = (my_extent.is_empty () ? 0.0 : my_extent[d] - his_extent)
172     + d * padding;
173
174   Stencil toadd (s);
175   toadd.translate_axis (offset, a);
176
177   if (minimum > 0 && d * (-origin ()[a] + toadd.origin ()[a]) < minimum)
178     toadd.translate_axis (-toadd.origin ()[a]
179                           + origin ()[a] + d * minimum, a);
180
181   return toadd;
182 }
183
184 /*  See scheme Function.  */
185 void
186 Stencil::add_at_edge (Axis a, Direction d, Stencil const &s, Real padding,
187                       Real minimum)
188 {
189   add_stencil (moved_to_edge (a, d, s, padding, minimum));
190 }
191
192 Stencil
193 Stencil::in_color (Real r, Real g, Real b) const
194 {
195   Stencil new_stencil (extent_box (),
196                        scm_list_3 (ly_symbol2scm ("color"),
197                                    scm_list_3 (scm_from_double (r),
198                                                scm_from_double (g),
199                                                scm_from_double (b)),
200                                    expr ()));
201   return new_stencil;
202 }