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))));
101 LY_DEFINE (ly_stencil_empty_p, "ly:stencil-empty?",
103 "Return whether @var{stil} is empty ")
105 Stencil *s = unsmob_stencil (stil);
106 SCM_ASSERT_TYPE (s, stil, SCM_ARG1, __FUNCTION__, "stencil");
107 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))]);
122 LY_DEFINE (ly_stencil_moved_to_edge, "ly:stencil-moved-to-edge",
123 4, 2, 0, (SCM first, SCM axis, SCM direction, SCM second,
124 SCM padding, SCM minimum),
125 "Similar to @code{ly:stencil-combine-edge}, but returns "
126 "@var{second} positioned to be next to @var{first}. ")
129 C&P from combine-at-edge.
131 Stencil *s1 = unsmob_stencil (first);
132 Stencil *s2 = unsmob_stencil (second);
133 Stencil first_stencil;
135 SCM_ASSERT_TYPE (is_axis (axis), axis, SCM_ARG3, __FUNCTION__, "axis");
136 SCM_ASSERT_TYPE (is_direction (direction), direction, SCM_ARG4, __FUNCTION__, "dir");
139 if (padding != SCM_UNDEFINED)
141 SCM_ASSERT_TYPE (scm_is_number (padding), padding, SCM_ARG5, __FUNCTION__, "number");
142 p = scm_to_double (padding);
145 if (minimum != SCM_UNDEFINED)
147 SCM_ASSERT_TYPE (scm_is_number (minimum), minimum, SCM_ARG6, __FUNCTION__, "number");
148 m = scm_to_double (minimum);
155 return first_stencil.moved_to_edge (Axis (scm_to_int (axis)),
156 Direction (scm_to_int (direction)),
157 *s2, p, m).smobbed_copy ();
159 return Stencil ().smobbed_copy ();
162 LY_DEFINE (ly_stencil_combine_at_edge, "ly:stencil-combine-at-edge",
163 4, 2, 0, (SCM first, SCM axis, SCM direction,
167 "Construct a stencil by putting @var{second} next to @var{first}. "
168 "@var{axis} can be 0 (x-axis) or 1 (y-axis), "
169 "@var{direction} can be -1 (left or down) or 1 (right or up). "
170 "The stencils are juxtaposed with @var{padding} as extra space. "
171 "If this puts the reference points closer than @var{minimum}, "
172 "they are moved by the latter amount."
173 "@var{first} and @var{second} may also be '() or #f.")
175 Stencil *s1 = unsmob_stencil (first);
176 Stencil *s2 = unsmob_stencil (second);
179 SCM_ASSERT_TYPE (s1 || first == SCM_BOOL_F || first == SCM_EOL,
180 first, SCM_ARG1, __FUNCTION__, "Stencil, #f or ()");
181 SCM_ASSERT_TYPE (s2 || second == SCM_BOOL_F || second == SCM_EOL,
182 second, SCM_ARG4, __FUNCTION__, "Stencil, #f or ()");
183 SCM_ASSERT_TYPE (is_axis (axis), axis, SCM_ARG2, __FUNCTION__, "axis");
184 SCM_ASSERT_TYPE (is_direction (direction), direction, SCM_ARG3, __FUNCTION__, "dir");
187 if (padding != SCM_UNDEFINED)
189 SCM_ASSERT_TYPE (scm_is_number (padding), padding, SCM_ARG5, __FUNCTION__, "number");
190 p = scm_to_double (padding);
193 if (minimum != SCM_UNDEFINED)
195 SCM_ASSERT_TYPE (scm_is_number (minimum), minimum, SCM_ARG6, __FUNCTION__, "number");
196 m = scm_to_double (minimum);
203 result.add_at_edge (Axis (scm_to_int (axis)),
204 Direction (scm_to_int (direction)), *s2, p, m);
206 return result.smobbed_copy ();
209 LY_DEFINE (ly_stencil_add, "ly:stencil-add",
211 "Combine stencils. Takes any number of arguments.")
213 #define FUNC_NAME __FUNCTION__
214 SCM_VALIDATE_REST_ARGUMENT (args);
218 while (!SCM_NULLP (args))
220 Stencil *s = unsmob_stencil (scm_car (args));
222 SCM_ASSERT_TYPE (s, scm_car (args), SCM_ARGn, __FUNCTION__, "Stencil");
224 result.add_stencil (*s);
225 args = scm_cdr (args);
228 return result.smobbed_copy ();
231 LY_DEFINE (ly_make_stencil, "ly:make-stencil",
232 3, 0, 0, (SCM expr, SCM xext, SCM yext),
234 "Stencils are a device independent output expressions."
235 "They carry two pieces of information: \n\n"
236 "1: a specification of how to print this object. "
237 "This specification is processed by the output backends, "
238 " for example @file{scm/output-tex.scm}.\n\n"
239 "2: the vertical and horizontal extents of the object.\n\n")
241 SCM_ASSERT_TYPE (is_number_pair (xext), xext, SCM_ARG2, __FUNCTION__, "number pair");
242 SCM_ASSERT_TYPE (is_number_pair (yext), yext, SCM_ARG3, __FUNCTION__, "number pair");
244 Box b (ly_scm2interval (xext), ly_scm2interval (yext));
246 return s.smobbed_copy ();
249 LY_DEFINE (ly_stencil_align_to_x, "ly:stencil-align-to!",
250 3, 0, 0, (SCM stil, SCM axis, SCM dir),
251 "Align @var{stil} using its own extents. "
252 "@var{dir} is a number -1, 1 are left and right respectively. "
253 "Other values are interpolated (so 0 means the center. ")
255 SCM_ASSERT_TYPE (unsmob_stencil (stil), stil, SCM_ARG1, __FUNCTION__, "stencil");
256 SCM_ASSERT_TYPE (is_axis (axis), axis, SCM_ARG2, __FUNCTION__, "axis");
257 SCM_ASSERT_TYPE (scm_is_number (dir), dir, SCM_ARG3, __FUNCTION__, "number");
259 unsmob_stencil (stil)->align_to ((Axis)scm_to_int (axis),
260 scm_to_double (dir));
264 LY_DEFINE (ly_stencil_fonts, "ly:stencil-fonts",
266 " Analyse @var{s}, and return a list of fonts used in @var{s}.")
268 Stencil *stil = unsmob_stencil (s);
269 SCM_ASSERT_TYPE (stil, s, SCM_ARG1, __FUNCTION__, "Stencil");
270 return find_expression_fonts (stil->expr ());
273 struct Stencil_interpret_arguments
279 void stencil_interpret_in_scm (void *p, SCM expr)
281 Stencil_interpret_arguments *ap = (Stencil_interpret_arguments *) p;
282 scm_call_2 (ap->func, ap->arg1, expr);
285 LY_DEFINE (ly_interpret_stencil_expression, "ly:interpret-stencil-expression",
286 4, 0, 0, (SCM expr, SCM func, SCM arg1, SCM offset),
287 "Parse EXPR, feed bits to FUNC with first arg ARG1")
289 SCM_ASSERT_TYPE (ly_c_procedure_p (func), func, SCM_ARG1, __FUNCTION__,
292 Stencil_interpret_arguments a;
295 Offset o = ly_scm2offset (offset);
297 interpret_stencil_expression (expr, stencil_interpret_in_scm, (void *) &a, o);
299 return SCM_UNSPECIFIED;
302 LY_DEFINE (ly_bracket, "ly:bracket",
304 (SCM a, SCM iv, SCM t, SCM p),
305 "Make a bracket in direction @var{a}. The extent of the bracket is "
306 "given by @var{iv}. The wings protude by an amount of @var{p}, which "
307 "may be negative. The thickness is given by @var{t}.")
309 SCM_ASSERT_TYPE (is_axis (a), a, SCM_ARG1, __FUNCTION__, "axis");
310 SCM_ASSERT_TYPE (is_number_pair (iv), iv, SCM_ARG2, __FUNCTION__, "number pair");
311 SCM_ASSERT_TYPE (scm_is_number (t), a, SCM_ARG3, __FUNCTION__, "number");
312 SCM_ASSERT_TYPE (scm_is_number (p), a, SCM_ARG4, __FUNCTION__, "number");
314 return Lookup::bracket ((Axis)scm_to_int (a), ly_scm2interval (iv),
317 0.95 * scm_to_double (t)).smobbed_copy ();
320 LY_DEFINE (ly_filled_box, "ly:round-filled-box",
322 (SCM xext, SCM yext, SCM blot),
323 "Make a @code{Stencil} "
324 "that prints a black box of dimensions @var{xext}, "
325 "@var{yext} and roundness @var{blot}.")
327 SCM_ASSERT_TYPE (is_number_pair (xext), xext, SCM_ARG1, __FUNCTION__, "number pair");
328 SCM_ASSERT_TYPE (is_number_pair (yext), yext, SCM_ARG2, __FUNCTION__, "number pair");
329 SCM_ASSERT_TYPE (scm_is_number (blot), blot, SCM_ARG3, __FUNCTION__, "number");
331 return Lookup::round_filled_box (Box (ly_scm2interval (xext), ly_scm2interval (yext)),
332 scm_to_double (blot)).smobbed_copy ();