2 stencil-scheme.cc -- implement Stencil scheme accessors
4 source file of the GNU LilyPond music typesetter
6 (c) 1997--2004 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);
50 SCM_ASSERT_TYPE (!isinf (real_amount) && !isnan (real_amount),
51 amount, SCM_ARG2, __FUNCTION__, "finite number");
53 SCM_ASSERT_TYPE (is_axis (axis), axis, SCM_ARG3, __FUNCTION__, "axis");
55 SCM new_s = s->smobbed_copy ();
56 Stencil *q = unsmob_stencil (new_s);
57 q->translate_axis (real_amount, Axis (scm_to_int (axis)));
62 LY_DEFINE (ly_translate_stencil, "ly:stencil-translate",
63 2, 0, 0, (SCM stil, SCM offset),
64 "Return a @var{stil}, "
65 "but translated by @var{offset} (a pair of numbers).")
67 Stencil *s = unsmob_stencil (stil);
68 SCM_ASSERT_TYPE (s, stil, SCM_ARG1, __FUNCTION__, "stencil");
69 SCM_ASSERT_TYPE (is_number_pair (offset), offset, SCM_ARG2, __FUNCTION__, "number pair");
70 Offset o = ly_scm2offset (offset);
72 SCM new_s = s->smobbed_copy ();
73 Stencil *q = unsmob_stencil (new_s);
78 LY_DEFINE (ly_stencil_expr, "ly:stencil-expr",
80 "Return the expression of @var{stil}.")
82 Stencil *s = unsmob_stencil (stil);
83 SCM_ASSERT_TYPE (s, stil, SCM_ARG1, __FUNCTION__, "stencil");
87 LY_DEFINE (ly_stencil_get_extent, "ly:stencil-extent",
88 2, 0, 0, (SCM stil, SCM axis),
89 "Return a pair of numbers signifying the extent of @var{stil} in "
90 "@var{axis} direction (0 or 1 for x and y axis respectively).")
92 Stencil *s = unsmob_stencil (stil);
93 SCM_ASSERT_TYPE (s, stil, SCM_ARG1, __FUNCTION__, "stencil");
94 SCM_ASSERT_TYPE (is_axis (axis), axis, SCM_ARG2, __FUNCTION__, "axis");
96 return ly_interval2scm (s->extent (Axis (scm_to_int (axis))));
100 LY_DEFINE (ly_stencil_empty_p, "ly:stencil-empty?",
102 "Return whether @var{stil} is empty ")
104 Stencil *s = unsmob_stencil (stil);
105 SCM_ASSERT_TYPE (s, stil, SCM_ARG1, __FUNCTION__, "stencil");
106 return scm_from_bool (s->is_empty ());
110 LY_DEFINE (ly_stencil_origin, "ly:stencil-origin",
111 2, 0, 0, (SCM stil, SCM axis),
112 "Return a pair of numbers signifying the origin @var{stil} in "
113 "@var{axis} direction (0 or 1 for x and y axis respectively).")
115 Stencil *s = unsmob_stencil (stil);
116 SCM_ASSERT_TYPE (s, stil, SCM_ARG1, __FUNCTION__, "stencil");
117 SCM_ASSERT_TYPE (is_axis (axis), axis, SCM_ARG2, __FUNCTION__, "axis");
119 return scm_from_double (s->origin()[Axis (scm_to_int (axis))]);
123 LY_DEFINE (ly_stencil_moved_to_edge, "ly:stencil-moved-to-edge",
124 4, 2, 0, (SCM first, SCM axis, SCM direction, SCM second,
125 SCM padding, SCM minimum),
126 "Similar to @code{ly:stencil-combine-edge}, but returns "
127 "@var{second} positioned to be next to @var{first}. ")
130 C&P from combine-at-edge.
132 Stencil *s1 = unsmob_stencil (first);
133 Stencil *s2 = unsmob_stencil (second);
134 Stencil first_stencil;
136 SCM_ASSERT_TYPE (is_axis (axis), axis, SCM_ARG3, __FUNCTION__, "axis");
137 SCM_ASSERT_TYPE (is_direction (direction), direction, SCM_ARG4, __FUNCTION__, "dir");
140 if (padding != SCM_UNDEFINED)
142 SCM_ASSERT_TYPE (scm_is_number (padding), padding, SCM_ARG5, __FUNCTION__, "number");
143 p = scm_to_double (padding);
146 if (minimum != SCM_UNDEFINED)
148 SCM_ASSERT_TYPE (scm_is_number (minimum), minimum, SCM_ARG6, __FUNCTION__, "number");
149 m = scm_to_double (minimum);
156 return first_stencil.moved_to_edge (Axis (scm_to_int (axis)),
157 Direction (scm_to_int (direction)),
158 *s2, p, m).smobbed_copy ();
160 return Stencil().smobbed_copy ();
165 LY_DEFINE (ly_stencil_combine_at_edge, "ly:stencil-combine-at-edge",
166 4, 2, 0, (SCM first, SCM axis, SCM direction,
170 "Construct a stencil by putting @var{second} next to @var{first}. "
171 "@var{axis} can be 0 (x-axis) or 1 (y-axis), "
172 "@var{direction} can be -1 (left or down) or 1 (right or up). "
173 "The stencils are juxtaposed with @var{padding} as extra space. "
174 "If this puts the reference points closer than @var{minimum}, "
175 "they are moved by the latter amount."
176 "@var{first} and @var{second} may also be '() or #f.")
178 Stencil *s1 = unsmob_stencil (first);
179 Stencil *s2 = unsmob_stencil (second);
182 SCM_ASSERT_TYPE (s1 || first == SCM_BOOL_F || first == SCM_EOL,
183 first, SCM_ARG1, __FUNCTION__, "Stencil, #f or ()");
184 SCM_ASSERT_TYPE (s2 || second == SCM_BOOL_F || second == SCM_EOL,
185 second, SCM_ARG4, __FUNCTION__, "Stencil, #f or ()");
186 SCM_ASSERT_TYPE (is_axis (axis), axis, SCM_ARG2, __FUNCTION__, "axis");
187 SCM_ASSERT_TYPE (is_direction (direction), direction, SCM_ARG3, __FUNCTION__, "dir");
190 if (padding != SCM_UNDEFINED)
192 SCM_ASSERT_TYPE (scm_is_number (padding), padding, SCM_ARG5, __FUNCTION__, "number");
193 p = scm_to_double (padding);
196 if (minimum != SCM_UNDEFINED)
198 SCM_ASSERT_TYPE (scm_is_number (minimum), minimum, SCM_ARG6, __FUNCTION__, "number");
199 m = scm_to_double (minimum);
206 result.add_at_edge (Axis (scm_to_int (axis)),
207 Direction (scm_to_int (direction)), *s2, p, m);
209 return result.smobbed_copy ();
212 LY_DEFINE (ly_stencil_add , "ly:stencil-add",
214 "Combine stencils. Takes any number of arguments.")
216 #define FUNC_NAME __FUNCTION__
217 SCM_VALIDATE_REST_ARGUMENT (args);
221 while (!SCM_NULLP (args))
223 Stencil *s = unsmob_stencil (scm_car (args));
225 SCM_ASSERT_TYPE (s, scm_car (args), SCM_ARGn, __FUNCTION__, "Stencil");
227 result.add_stencil (*s);
228 args = scm_cdr (args);
231 return result.smobbed_copy ();
234 LY_DEFINE (ly_make_stencil, "ly:make-stencil",
235 3, 0, 0, (SCM expr, SCM xext, SCM yext),
237 "Stencils are a device independent output expressions."
238 "They carry two pieces of information: \n\n"
239 "1: a specification of how to print this object. "
240 "This specification is processed by the output backends, "
241 " for example @file{scm/output-tex.scm}.\n\n"
242 "2: the vertical and horizontal extents of the object.\n\n")
244 SCM_ASSERT_TYPE (is_number_pair (xext), xext, SCM_ARG2, __FUNCTION__, "number pair");
245 SCM_ASSERT_TYPE (is_number_pair (yext), yext, SCM_ARG3, __FUNCTION__, "number pair");
247 Box b (ly_scm2interval (xext), ly_scm2interval (yext));
249 return s.smobbed_copy ();
253 LY_DEFINE (ly_stencil_align_to_x, "ly:stencil-align-to!",
254 3, 0, 0, (SCM stil, SCM axis, SCM dir),
255 "Align @var{stil} using its own extents. "
256 "@var{dir} is a number -1, 1 are left and right respectively. "
257 "Other values are interpolated (so 0 means the center. ")
259 SCM_ASSERT_TYPE (unsmob_stencil (stil), stil, SCM_ARG1, __FUNCTION__, "stencil");
260 SCM_ASSERT_TYPE (is_axis (axis), axis, SCM_ARG2, __FUNCTION__, "axis");
261 SCM_ASSERT_TYPE (scm_is_number (dir), dir, SCM_ARG3, __FUNCTION__, "number");
263 unsmob_stencil (stil)->align_to ((Axis)scm_to_int (axis),
264 scm_to_double (dir));
269 LY_DEFINE (ly_stencil_fonts, "ly:stencil-fonts",
271 " Analyse @var{s}, and return a list of fonts used in @var{s}.")
273 Stencil *stil = unsmob_stencil (s);
274 SCM_ASSERT_TYPE (stil, s, SCM_ARG1, __FUNCTION__, "Stencil");
275 return find_expression_fonts (stil->expr ());
279 struct Stencil_interpret_arguments
285 void stencil_interpret_in_scm (void *p, SCM expr)
287 Stencil_interpret_arguments *ap = (Stencil_interpret_arguments*) p;
288 scm_call_2 (ap->func, ap->arg1, expr);
291 LY_DEFINE (ly_interpret_stencil_expression, "ly:interpret-stencil-expression",
292 4, 0, 0, (SCM expr, SCM func, SCM arg1, SCM offset),
293 "Parse EXPR, feed bits to FUNC with first arg ARG1")
295 SCM_ASSERT_TYPE (ly_c_procedure_p(func), func, SCM_ARG1, __FUNCTION__,
298 Stencil_interpret_arguments a;
301 Offset o = ly_scm2offset (offset);
303 interpret_stencil_expression (expr, stencil_interpret_in_scm, (void*) &a, o);
305 return SCM_UNSPECIFIED;
310 LY_DEFINE (ly_bracket ,"ly:bracket",
312 (SCM a, SCM iv, SCM t, SCM p),
313 "Make a bracket in direction @var{a}. The extent of the bracket is "
314 "given by @var{iv}. The wings protude by an amount of @var{p}, which "
315 "may be negative. The thickness is given by @var{t}.")
317 SCM_ASSERT_TYPE (is_axis (a), a, SCM_ARG1, __FUNCTION__, "axis") ;
318 SCM_ASSERT_TYPE (is_number_pair (iv), iv, SCM_ARG2, __FUNCTION__, "number pair") ;
319 SCM_ASSERT_TYPE (scm_is_number (t), a, SCM_ARG3, __FUNCTION__, "number") ;
320 SCM_ASSERT_TYPE (scm_is_number (p), a, SCM_ARG4, __FUNCTION__, "number") ;
323 return Lookup::bracket ((Axis)scm_to_int (a), ly_scm2interval (iv),
326 0.95 * scm_to_double (t)).smobbed_copy ();
331 LY_DEFINE (ly_filled_box ,"ly:round-filled-box",
333 (SCM xext, SCM yext, SCM blot),
334 "Make a @code{Stencil} "
335 "that prints a black box of dimensions @var{xext}, "
336 "@var{yext} and roundness @var{blot}."
339 SCM_ASSERT_TYPE (is_number_pair (xext), xext, SCM_ARG1, __FUNCTION__, "number pair") ;
340 SCM_ASSERT_TYPE (is_number_pair (yext), yext, SCM_ARG2, __FUNCTION__, "number pair") ;
341 SCM_ASSERT_TYPE (scm_is_number (blot), blot, SCM_ARG3, __FUNCTION__, "number") ;
343 return Lookup::round_filled_box (Box (ly_scm2interval (xext), ly_scm2interval (yext)),
344 scm_to_double (blot)).smobbed_copy ();