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