2 stencil-scheme.cc -- implement Stencil scheme accessors
4 source file of the GNU LilyPond music typesetter
6 (c) 1997--2005 Han-Wen Nienhuys <hanwen@cs.uu.nl>
11 #include <libc-extension.hh> // isinf
13 #include "font-metric.hh"
18 TODO: naming add/combine.
21 UGH. Junk all mutators.
23 LY_DEFINE (ly_stencil_set_extent_x, "ly:stencil-set-extent!",
24 3, 0, 0, (SCM stil, SCM axis, SCM np),
25 "Set the extent of @var{stil} "
26 "(@var{extent} must be a pair of numbers) "
27 "in @var{axis} direction (0 or 1 for x- and y-axis respectively).")
29 Stencil *s = unsmob_stencil (stil);
30 SCM_ASSERT_TYPE (s, stil, SCM_ARG1, __FUNCTION__, "stencil");
31 SCM_ASSERT_TYPE (is_axis (axis), axis, SCM_ARG2, __FUNCTION__, "axis");
32 SCM_ASSERT_TYPE (is_number_pair (np), np, SCM_ARG3, __FUNCTION__,
35 Interval iv = ly_scm2interval (np);
36 s->dim_[Axis (scm_to_int (axis))] = iv;
38 return SCM_UNSPECIFIED;
41 LY_DEFINE (ly_translate_stencil_axis, "ly:stencil-translate-axis",
42 3, 0, 0, (SCM stil, SCM amount, SCM axis),
43 "Return a copy of @var{stil} but translated by @var{amount} in @var{axis} direction.")
45 Stencil *s = unsmob_stencil (stil);
46 SCM_ASSERT_TYPE (s, stil, SCM_ARG1, __FUNCTION__, "stencil");
47 SCM_ASSERT_TYPE (scm_is_number (amount), amount, SCM_ARG2, __FUNCTION__, "number");
49 Real real_amount = scm_to_double (amount);
51 SCM_ASSERT_TYPE (!isinf (real_amount) && !isnan (real_amount),
52 amount, SCM_ARG2, __FUNCTION__, "finite number");
55 SCM_ASSERT_TYPE (is_axis (axis), axis, SCM_ARG3, __FUNCTION__, "axis");
57 SCM new_s = s->smobbed_copy ();
58 Stencil *q = unsmob_stencil (new_s);
59 q->translate_axis (real_amount, Axis (scm_to_int (axis)));
64 LY_DEFINE (ly_translate_stencil, "ly:stencil-translate",
65 2, 0, 0, (SCM stil, SCM offset),
66 "Return a @var{stil}, "
67 "but translated by @var{offset} (a pair of numbers).")
69 Stencil *s = unsmob_stencil (stil);
70 SCM_ASSERT_TYPE (s, stil, SCM_ARG1, __FUNCTION__, "stencil");
71 SCM_ASSERT_TYPE (is_number_pair (offset), offset, SCM_ARG2, __FUNCTION__, "number pair");
72 Offset o = ly_scm2offset (offset);
74 SCM new_s = s->smobbed_copy ();
75 Stencil *q = unsmob_stencil (new_s);
80 LY_DEFINE (ly_stencil_expr, "ly:stencil-expr",
82 "Return the expression of @var{stil}.")
84 Stencil *s = unsmob_stencil (stil);
85 SCM_ASSERT_TYPE (s, stil, SCM_ARG1, __FUNCTION__, "stencil");
89 LY_DEFINE (ly_stencil_get_extent, "ly:stencil-extent",
90 2, 0, 0, (SCM stil, SCM axis),
91 "Return a pair of numbers signifying the extent of @var{stil} in "
92 "@var{axis} direction (0 or 1 for x and y axis respectively).")
94 Stencil *s = unsmob_stencil (stil);
95 SCM_ASSERT_TYPE (s, stil, SCM_ARG1, __FUNCTION__, "stencil");
96 SCM_ASSERT_TYPE (is_axis (axis), axis, SCM_ARG2, __FUNCTION__, "axis");
98 return ly_interval2scm (s->extent (Axis (scm_to_int (axis))));
102 LY_DEFINE (ly_stencil_empty_p, "ly:stencil-empty?",
104 "Return whether @var{stil} is empty ")
106 Stencil *s = unsmob_stencil (stil);
107 SCM_ASSERT_TYPE (s, stil, SCM_ARG1, __FUNCTION__, "stencil");
108 return scm_from_bool (s->is_empty ());
112 LY_DEFINE (ly_stencil_origin, "ly:stencil-origin",
113 2, 0, 0, (SCM stil, SCM axis),
114 "Return a pair of numbers signifying the origin @var{stil} in "
115 "@var{axis} direction (0 or 1 for x and y axis respectively).")
117 Stencil *s = unsmob_stencil (stil);
118 SCM_ASSERT_TYPE (s, stil, SCM_ARG1, __FUNCTION__, "stencil");
119 SCM_ASSERT_TYPE (is_axis (axis), axis, SCM_ARG2, __FUNCTION__, "axis");
121 return scm_from_double (s->origin()[Axis (scm_to_int (axis))]);
125 LY_DEFINE (ly_stencil_moved_to_edge, "ly:stencil-moved-to-edge",
126 4, 2, 0, (SCM first, SCM axis, SCM direction, SCM second,
127 SCM padding, SCM minimum),
128 "Similar to @code{ly:stencil-combine-edge}, but returns "
129 "@var{second} positioned to be next to @var{first}. ")
132 C&P from combine-at-edge.
134 Stencil *s1 = unsmob_stencil (first);
135 Stencil *s2 = unsmob_stencil (second);
136 Stencil first_stencil;
138 SCM_ASSERT_TYPE (is_axis (axis), axis, SCM_ARG3, __FUNCTION__, "axis");
139 SCM_ASSERT_TYPE (is_direction (direction), direction, SCM_ARG4, __FUNCTION__, "dir");
142 if (padding != SCM_UNDEFINED)
144 SCM_ASSERT_TYPE (scm_is_number (padding), padding, SCM_ARG5, __FUNCTION__, "number");
145 p = scm_to_double (padding);
148 if (minimum != SCM_UNDEFINED)
150 SCM_ASSERT_TYPE (scm_is_number (minimum), minimum, SCM_ARG6, __FUNCTION__, "number");
151 m = scm_to_double (minimum);
158 return first_stencil.moved_to_edge (Axis (scm_to_int (axis)),
159 Direction (scm_to_int (direction)),
160 *s2, p, m).smobbed_copy ();
162 return Stencil().smobbed_copy ();
167 LY_DEFINE (ly_stencil_combine_at_edge, "ly:stencil-combine-at-edge",
168 4, 2, 0, (SCM first, SCM axis, SCM direction,
172 "Construct a stencil by putting @var{second} next to @var{first}. "
173 "@var{axis} can be 0 (x-axis) or 1 (y-axis), "
174 "@var{direction} can be -1 (left or down) or 1 (right or up). "
175 "The stencils are juxtaposed with @var{padding} as extra space. "
176 "If this puts the reference points closer than @var{minimum}, "
177 "they are moved by the latter amount."
178 "@var{first} and @var{second} may also be '() or #f.")
180 Stencil *s1 = unsmob_stencil (first);
181 Stencil *s2 = unsmob_stencil (second);
184 SCM_ASSERT_TYPE (s1 || first == SCM_BOOL_F || first == SCM_EOL,
185 first, SCM_ARG1, __FUNCTION__, "Stencil, #f or ()");
186 SCM_ASSERT_TYPE (s2 || second == SCM_BOOL_F || second == SCM_EOL,
187 second, SCM_ARG4, __FUNCTION__, "Stencil, #f or ()");
188 SCM_ASSERT_TYPE (is_axis (axis), axis, SCM_ARG2, __FUNCTION__, "axis");
189 SCM_ASSERT_TYPE (is_direction (direction), direction, SCM_ARG3, __FUNCTION__, "dir");
192 if (padding != SCM_UNDEFINED)
194 SCM_ASSERT_TYPE (scm_is_number (padding), padding, SCM_ARG5, __FUNCTION__, "number");
195 p = scm_to_double (padding);
198 if (minimum != SCM_UNDEFINED)
200 SCM_ASSERT_TYPE (scm_is_number (minimum), minimum, SCM_ARG6, __FUNCTION__, "number");
201 m = scm_to_double (minimum);
208 result.add_at_edge (Axis (scm_to_int (axis)),
209 Direction (scm_to_int (direction)), *s2, p, m);
211 return result.smobbed_copy ();
214 LY_DEFINE (ly_stencil_add , "ly:stencil-add",
216 "Combine stencils. Takes any number of arguments.")
218 #define FUNC_NAME __FUNCTION__
219 SCM_VALIDATE_REST_ARGUMENT (args);
223 while (!SCM_NULLP (args))
225 Stencil *s = unsmob_stencil (scm_car (args));
227 SCM_ASSERT_TYPE (s, scm_car (args), SCM_ARGn, __FUNCTION__, "Stencil");
229 result.add_stencil (*s);
230 args = scm_cdr (args);
233 return result.smobbed_copy ();
236 LY_DEFINE (ly_make_stencil, "ly:make-stencil",
237 3, 0, 0, (SCM expr, SCM xext, SCM yext),
239 "Stencils are a device independent output expressions."
240 "They carry two pieces of information: \n\n"
241 "1: a specification of how to print this object. "
242 "This specification is processed by the output backends, "
243 " for example @file{scm/output-tex.scm}.\n\n"
244 "2: the vertical and horizontal extents of the object.\n\n")
246 SCM_ASSERT_TYPE (is_number_pair (xext), xext, SCM_ARG2, __FUNCTION__, "number pair");
247 SCM_ASSERT_TYPE (is_number_pair (yext), yext, SCM_ARG3, __FUNCTION__, "number pair");
249 Box b (ly_scm2interval (xext), ly_scm2interval (yext));
251 return s.smobbed_copy ();
255 LY_DEFINE (ly_stencil_align_to_x, "ly:stencil-align-to!",
256 3, 0, 0, (SCM stil, SCM axis, SCM dir),
257 "Align @var{stil} using its own extents. "
258 "@var{dir} is a number -1, 1 are left and right respectively. "
259 "Other values are interpolated (so 0 means the center. ")
261 SCM_ASSERT_TYPE (unsmob_stencil (stil), stil, SCM_ARG1, __FUNCTION__, "stencil");
262 SCM_ASSERT_TYPE (is_axis (axis), axis, SCM_ARG2, __FUNCTION__, "axis");
263 SCM_ASSERT_TYPE (scm_is_number (dir), dir, SCM_ARG3, __FUNCTION__, "number");
265 unsmob_stencil (stil)->align_to ((Axis)scm_to_int (axis),
266 scm_to_double (dir));
271 LY_DEFINE (ly_stencil_fonts, "ly:stencil-fonts",
273 " Analyse @var{s}, and return a list of fonts used in @var{s}.")
275 Stencil *stil = unsmob_stencil (s);
276 SCM_ASSERT_TYPE (stil, s, SCM_ARG1, __FUNCTION__, "Stencil");
277 return find_expression_fonts (stil->expr ());
281 struct Stencil_interpret_arguments
287 void stencil_interpret_in_scm (void *p, SCM expr)
289 Stencil_interpret_arguments *ap = (Stencil_interpret_arguments*) p;
290 scm_call_2 (ap->func, ap->arg1, expr);
293 LY_DEFINE (ly_interpret_stencil_expression, "ly:interpret-stencil-expression",
294 4, 0, 0, (SCM expr, SCM func, SCM arg1, SCM offset),
295 "Parse EXPR, feed bits to FUNC with first arg ARG1")
297 SCM_ASSERT_TYPE (ly_c_procedure_p(func), func, SCM_ARG1, __FUNCTION__,
300 Stencil_interpret_arguments a;
303 Offset o = ly_scm2offset (offset);
305 interpret_stencil_expression (expr, stencil_interpret_in_scm, (void*) &a, o);
307 return SCM_UNSPECIFIED;
312 LY_DEFINE (ly_bracket , "ly:bracket",
314 (SCM a, SCM iv, SCM t, SCM p),
315 "Make a bracket in direction @var{a}. The extent of the bracket is "
316 "given by @var{iv}. The wings protude by an amount of @var{p}, which "
317 "may be negative. The thickness is given by @var{t}.")
319 SCM_ASSERT_TYPE (is_axis (a), a, SCM_ARG1, __FUNCTION__, "axis") ;
320 SCM_ASSERT_TYPE (is_number_pair (iv), iv, SCM_ARG2, __FUNCTION__, "number pair") ;
321 SCM_ASSERT_TYPE (scm_is_number (t), a, SCM_ARG3, __FUNCTION__, "number") ;
322 SCM_ASSERT_TYPE (scm_is_number (p), a, SCM_ARG4, __FUNCTION__, "number") ;
325 return Lookup::bracket ((Axis)scm_to_int (a), ly_scm2interval (iv),
328 0.95 * scm_to_double (t)).smobbed_copy ();
333 LY_DEFINE (ly_filled_box , "ly:round-filled-box",
335 (SCM xext, SCM yext, SCM blot),
336 "Make a @code{Stencil} "
337 "that prints a black box of dimensions @var{xext}, "
338 "@var{yext} and roundness @var{blot}."
341 SCM_ASSERT_TYPE (is_number_pair (xext), xext, SCM_ARG1, __FUNCTION__, "number pair") ;
342 SCM_ASSERT_TYPE (is_number_pair (yext), yext, SCM_ARG2, __FUNCTION__, "number pair") ;
343 SCM_ASSERT_TYPE (scm_is_number (blot), blot, SCM_ARG3, __FUNCTION__, "number") ;
345 return Lookup::round_filled_box (Box (ly_scm2interval (xext), ly_scm2interval (yext)),
346 scm_to_double (blot)).smobbed_copy ();