* scm/beam.scm (check-slope-callbacks): check sign of slope.
[lilypond.git] / lily / stencil.cc
blob14d7961b8a0783c944a60eaf30ca526953084018
1 /*
2 stencil.cc -- implement Stencil
4 source file of the GNU LilyPond music typesetter
6 (c) 1997--2004 Han-Wen Nienhuys <hanwen@cs.uu.nl>
7 */
9 #include <math.h>
10 #include <libc-extension.hh> // isinf
12 #include "input-smob.hh"
13 #include "font-metric.hh"
14 #include "dimensions.hh"
15 #include "interval.hh"
16 #include "string.hh"
17 #include "stencil.hh"
18 #include "warn.hh"
20 #include "ly-smobs.icc"
22 Stencil::Stencil ()
24 expr_ = SCM_EOL;
25 set_empty (true);
28 Stencil::Stencil (Box b, SCM func)
30 expr_ = func;
31 dim_ = b;
34 int
35 Stencil::print_smob (SCM, SCM port, scm_print_state *)
37 scm_puts ("#<Stencil ", port);
38 scm_puts (" >", port);
39 return 1;
42 SCM
43 Stencil::mark_smob (SCM smob)
45 Stencil *s = (Stencil*) ly_cdr (smob);
46 return s->expr_;
49 IMPLEMENT_SIMPLE_SMOBS (Stencil);
50 IMPLEMENT_TYPE_P (Stencil, "ly:stencil?");
51 IMPLEMENT_DEFAULT_EQUAL_P (Stencil);
53 Interval
54 Stencil::extent (Axis a) const
56 return dim_[a];
59 /* Hmm... maybe this is not such a good idea ; stuff can be empty,
60 while expr_ == '() */
61 bool
62 Stencil::is_empty () const
64 return expr_ == SCM_EOL;
67 SCM
68 Stencil::expr () const
70 return expr_;
73 Box
74 Stencil::extent_box () const
76 return dim_;
78 Offset
79 Stencil::origin () const
81 return origin_;
84 void
85 Stencil::translate (Offset o)
87 Axis a = X_AXIS;
88 while (a < NO_AXES)
90 if (abs (o[a]) > 100 CM
91 || isinf (o[a]) || isnan (o[a]))
93 programming_error ("Improbable offset for translation: setting to zero");
94 o[a] = 0.0;
96 incr (a);
99 expr_ = scm_list_n (ly_symbol2scm ("translate-stencil"),
100 ly_offset2scm (o),
101 expr_, SCM_UNDEFINED);
102 if (!is_empty ())
103 dim_.translate (o);
104 origin_ += o;
107 void
108 Stencil::translate_axis (Real x, Axis a)
110 Offset o (0,0);
111 o[a] = x;
112 translate (o);
115 void
116 Stencil::add_stencil (Stencil const &s)
118 expr_ = scm_list_3 (ly_symbol2scm ("combine-stencil"), s.expr_, expr_);
119 dim_.unite (s.dim_);
124 void
125 Stencil::set_empty (bool e)
127 if (e)
129 dim_[X_AXIS].set_empty ();
130 dim_[Y_AXIS].set_empty ();
132 else
134 dim_[X_AXIS] = Interval (0,0);
135 dim_[Y_AXIS] = Interval (0,0);
139 void
140 Stencil::align_to (Axis a, Real x)
142 if (is_empty ())
143 return;
145 Interval i (extent (a));
146 translate_axis (-i.linear_combination (x), a);
149 /* FIXME: unintuitive naming, you would expect *this to be moved.
150 Kept (keeping?) API for compat with add_at_edge ().
152 What is PADDING, what is MINIMUM, exactly? */
153 Stencil
154 Stencil::moved_to_edge (Axis a, Direction d, Stencil const &s,
155 Real padding, Real minimum) const
157 Interval my_extent = dim_[a];
158 Interval i (s.extent (a));
159 Real his_extent;
160 if (i.is_empty ())
162 programming_error ("Stencil::moved_to_edge: adding empty stencil.");
163 his_extent = 0.0;
165 else
166 his_extent = i[-d];
168 Real offset = (my_extent.is_empty () ? 0.0 : my_extent[d] - his_extent)
169 + d * padding;
171 Stencil toadd (s);
172 toadd.translate_axis (offset,a);
174 if (minimum > 0 && d * (-origin ()[a] + toadd.origin ()[a]) < minimum)
175 toadd.translate_axis ( -toadd.origin ()[a]
176 + origin ()[a] + d * minimum, a);
178 return toadd;
181 /* See scheme Function. */
182 void
183 Stencil::add_at_edge (Axis a, Direction d, Stencil const &s, Real padding,
184 Real minimum)
186 add_stencil (moved_to_edge (a, d, s, padding, minimum));
190 /****************************************************************/
193 void
194 interpret_stencil_expression (SCM expr,
195 void (*func) (void*, SCM),
196 void *func_arg,
197 Offset o)
199 while (1)
201 if (!ly_c_pair_p (expr))
202 return;
204 SCM head = ly_car (expr);
206 if (head == ly_symbol2scm ("translate-stencil"))
208 o += ly_scm2offset (ly_cadr (expr));
209 expr = ly_caddr (expr);
211 else if (head == ly_symbol2scm ("combine-stencil"))
213 for (SCM x = ly_cdr (expr); ly_c_pair_p (x); x = ly_cdr (x))
214 interpret_stencil_expression (ly_car (x), func, func_arg, o);
215 return;
217 else if (head == ly_symbol2scm ("grob-cause"))
219 SCM grob = ly_cadr (expr);
221 (*func) (func_arg, scm_list_2 (head, grob));
222 interpret_stencil_expression (ly_caddr (expr), func, func_arg, o);
223 (*func) (func_arg, scm_list_1 (ly_symbol2scm ("no-origin")));
225 return ;
227 else
229 (*func) (func_arg,
230 scm_list_4 (ly_symbol2scm ("placebox"),
231 scm_make_real (o[X_AXIS]),
232 scm_make_real (o[Y_AXIS]),
233 expr));
234 return;
240 struct Font_list
242 SCM fonts_;
245 static void
246 find_font_function (void *fs, SCM x)
248 Font_list *me = (Font_list*) fs;
250 if (ly_car (x) == ly_symbol2scm ("placebox"))
252 SCM args = ly_cdr (x);
253 SCM what = ly_caddr (args);
255 if (ly_c_pair_p (what))
257 SCM head = ly_car (what);
258 if (ly_symbol2scm ("text") == head)
259 me->fonts_ = scm_cons (ly_cadr (what), me->fonts_);
260 else if (head == ly_symbol2scm ("char"))
261 me->fonts_ = scm_cons (ly_cadr (what), me->fonts_);
267 find_expression_fonts (SCM expr)
269 Font_list fl;
271 fl.fonts_ = SCM_EOL;
273 interpret_stencil_expression (expr, &find_font_function,
274 (void*) &fl, Offset (0,0));
276 return fl.fonts_;
280 LY_DEFINE (ly_stencil_fonts, "ly:stencil-fonts",
281 1, 0, 0, (SCM s),
282 " Analyse @var{s}, and return a list of fonts used in @var{s}.")
284 Stencil *stil = unsmob_stencil (s);
285 SCM_ASSERT_TYPE (stil, s, SCM_ARG1, __FUNCTION__, "Stencil");
286 return find_expression_fonts (stil->expr ());
289 struct Stencil_interpret_arguments
291 SCM func;
292 SCM arg1;
295 void stencil_interpret_in_scm (void *p, SCM expr)
297 Stencil_interpret_arguments *ap = (Stencil_interpret_arguments*) p;
298 scm_call_2 (ap->func, ap->arg1, expr);
303 LY_DEFINE (ly_interpret_stencil_expression, "ly:interpret-stencil-expression",
304 4, 0, 0, (SCM expr, SCM func, SCM arg1, SCM offset),
305 "Parse EXPR, feed bits to FUNC with first arg ARG1")
307 SCM_ASSERT_TYPE (ly_c_procedure_p(func), func, SCM_ARG1, __FUNCTION__,
308 "procedure");
310 Stencil_interpret_arguments a;
311 a.func = func;
312 a.arg1 = arg1;
313 Offset o = ly_scm2offset (offset);
315 interpret_stencil_expression (expr, stencil_interpret_in_scm, (void*) &a, o);
317 return SCM_UNSPECIFIED;