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 "font-metric.hh"
12 #include "libc-extension.hh"
17 TODO: naming add/combine.
20 UGH. Junk all mutators.
22 LY_DEFINE (ly_stencil_set_extent_x, "ly:stencil-set-extent!",
23 3, 0, 0, (SCM stil, SCM axis, SCM np),
24 "Set the extent of @var{stil} "
25 "(@var{extent} must be a pair of numbers) "
26 "in @var{axis} direction (0 or 1 for x- and y-axis respectively).")
28 Stencil *s = unsmob_stencil (stil);
29 SCM_ASSERT_TYPE (s, stil, SCM_ARG1, __FUNCTION__, "stencil");
30 SCM_ASSERT_TYPE (is_axis (axis), axis, SCM_ARG2, __FUNCTION__, "axis");
31 SCM_ASSERT_TYPE (is_number_pair (np), np, SCM_ARG3, __FUNCTION__,
34 Interval iv = ly_scm2interval (np);
35 s->dim_[Axis (scm_to_int (axis))] = iv;
37 return SCM_UNSPECIFIED;
40 LY_DEFINE (ly_translate_stencil_axis, "ly:stencil-translate-axis",
41 3, 0, 0, (SCM stil, SCM amount, SCM axis),
42 "Return a copy of @var{stil} but translated by @var{amount} in @var{axis} direction.")
44 Stencil *s = unsmob_stencil (stil);
45 SCM_ASSERT_TYPE (s, stil, SCM_ARG1, __FUNCTION__, "stencil");
46 SCM_ASSERT_TYPE (scm_is_number (amount), amount, SCM_ARG2, __FUNCTION__, "number");
48 Real real_amount = scm_to_double (amount);
50 SCM_ASSERT_TYPE (!isinf (real_amount) && !isnan (real_amount),
51 amount, SCM_ARG2, __FUNCTION__, "finite number");
54 SCM_ASSERT_TYPE (is_axis (axis), axis, SCM_ARG3, __FUNCTION__, "axis");
56 SCM new_s = s->smobbed_copy ();
57 Stencil *q = unsmob_stencil (new_s);
58 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))));
99 LY_DEFINE (ly_stencil_empty_p, "ly:stencil-empty?",
101 "Return whether @var{stil} is empty ")
103 Stencil *s = unsmob_stencil (stil);
104 SCM_ASSERT_TYPE (s, stil, SCM_ARG1, __FUNCTION__, "stencil");
105 return scm_from_bool (s->is_empty ());
108 LY_DEFINE (ly_stencil_origin, "ly:stencil-origin",
109 2, 0, 0, (SCM stil, SCM axis),
110 "Return a pair of numbers signifying the origin @var{stil} in "
111 "@var{axis} direction (0 or 1 for x and y axis respectively).")
113 Stencil *s = unsmob_stencil (stil);
114 SCM_ASSERT_TYPE (s, stil, SCM_ARG1, __FUNCTION__, "stencil");
115 SCM_ASSERT_TYPE (is_axis (axis), axis, SCM_ARG2, __FUNCTION__, "axis");
117 return scm_from_double (s->origin ()[Axis (scm_to_int (axis))]);
120 LY_DEFINE (ly_stencil_moved_to_edge, "ly:stencil-moved-to-edge",
121 4, 2, 0, (SCM first, SCM axis, SCM direction, SCM second,
122 SCM padding, SCM minimum),
123 "Similar to @code{ly:stencil-combine-edge}, but returns "
124 "@var{second} positioned to be next to @var{first}. ")
127 C&P from combine-at-edge.
129 Stencil *s1 = unsmob_stencil (first);
130 Stencil *s2 = unsmob_stencil (second);
131 Stencil first_stencil;
133 SCM_ASSERT_TYPE (is_axis (axis), axis, SCM_ARG3, __FUNCTION__, "axis");
134 SCM_ASSERT_TYPE (is_direction (direction), direction, SCM_ARG4, __FUNCTION__, "dir");
137 if (padding != SCM_UNDEFINED)
139 SCM_ASSERT_TYPE (scm_is_number (padding), padding, SCM_ARG5, __FUNCTION__, "number");
140 p = scm_to_double (padding);
143 if (minimum != SCM_UNDEFINED)
145 SCM_ASSERT_TYPE (scm_is_number (minimum), minimum, SCM_ARG6, __FUNCTION__, "number");
146 m = scm_to_double (minimum);
153 return first_stencil.moved_to_edge (Axis (scm_to_int (axis)),
154 Direction (scm_to_int (direction)),
155 *s2, p, m).smobbed_copy ();
157 return Stencil ().smobbed_copy ();
160 LY_DEFINE (ly_stencil_combine_at_edge, "ly:stencil-combine-at-edge",
161 4, 2, 0, (SCM first, SCM axis, SCM direction,
165 "Construct a stencil by putting @var{second} next to @var{first}. "
166 "@var{axis} can be 0 (x-axis) or 1 (y-axis), "
167 "@var{direction} can be -1 (left or down) or 1 (right or up). "
168 "The stencils are juxtaposed with @var{padding} as extra space. "
169 "If this puts the reference points closer than @var{minimum}, "
170 "they are moved by the latter amount."
171 "@var{first} and @var{second} may also be '() or #f.")
173 Stencil *s1 = unsmob_stencil (first);
174 Stencil *s2 = unsmob_stencil (second);
177 SCM_ASSERT_TYPE (s1 || first == SCM_BOOL_F || first == SCM_EOL,
178 first, SCM_ARG1, __FUNCTION__, "Stencil, #f or ()");
179 SCM_ASSERT_TYPE (s2 || second == SCM_BOOL_F || second == SCM_EOL,
180 second, SCM_ARG4, __FUNCTION__, "Stencil, #f or ()");
181 SCM_ASSERT_TYPE (is_axis (axis), axis, SCM_ARG2, __FUNCTION__, "axis");
182 SCM_ASSERT_TYPE (is_direction (direction), direction, SCM_ARG3, __FUNCTION__, "dir");
185 if (padding != SCM_UNDEFINED)
187 SCM_ASSERT_TYPE (scm_is_number (padding), padding, SCM_ARG5, __FUNCTION__, "number");
188 p = scm_to_double (padding);
191 if (minimum != SCM_UNDEFINED)
193 SCM_ASSERT_TYPE (scm_is_number (minimum), minimum, SCM_ARG6, __FUNCTION__, "number");
194 m = scm_to_double (minimum);
201 result.add_at_edge (Axis (scm_to_int (axis)),
202 Direction (scm_to_int (direction)), *s2, p, m);
204 return result.smobbed_copy ();
207 LY_DEFINE (ly_stencil_add, "ly:stencil-add",
209 "Combine stencils. Takes any number of arguments.")
211 #define FUNC_NAME __FUNCTION__
212 SCM_VALIDATE_REST_ARGUMENT (args);
216 while (!SCM_NULLP (args))
218 Stencil *s = unsmob_stencil (scm_car (args));
220 SCM_ASSERT_TYPE (s, scm_car (args), SCM_ARGn, __FUNCTION__, "Stencil");
222 result.add_stencil (*s);
223 args = scm_cdr (args);
226 return result.smobbed_copy ();
229 LY_DEFINE (ly_make_stencil, "ly:make-stencil",
230 3, 0, 0, (SCM expr, SCM xext, SCM yext),
232 "Stencils are a device independent output expressions."
233 "They carry two pieces of information: \n\n"
234 "1: a specification of how to print this object. "
235 "This specification is processed by the output backends, "
236 " for example @file{scm/output-tex.scm}.\n\n"
237 "2: the vertical and horizontal extents of the object.\n\n")
239 SCM_ASSERT_TYPE (is_number_pair (xext), xext, SCM_ARG2, __FUNCTION__, "number pair");
240 SCM_ASSERT_TYPE (is_number_pair (yext), yext, SCM_ARG3, __FUNCTION__, "number pair");
242 Box b (ly_scm2interval (xext), ly_scm2interval (yext));
244 return s.smobbed_copy ();
247 LY_DEFINE (ly_stencil_align_to_x, "ly:stencil-align-to!",
248 3, 0, 0, (SCM stil, SCM axis, SCM dir),
249 "Align @var{stil} using its own extents. "
250 "@var{dir} is a number -1, 1 are left and right respectively. "
251 "Other values are interpolated (so 0 means the center. ")
253 SCM_ASSERT_TYPE (unsmob_stencil (stil), stil, SCM_ARG1, __FUNCTION__, "stencil");
254 SCM_ASSERT_TYPE (is_axis (axis), axis, SCM_ARG2, __FUNCTION__, "axis");
255 SCM_ASSERT_TYPE (scm_is_number (dir), dir, SCM_ARG3, __FUNCTION__, "number");
257 unsmob_stencil (stil)->align_to ((Axis)scm_to_int (axis),
258 scm_to_double (dir));
262 LY_DEFINE (ly_stencil_fonts, "ly:stencil-fonts",
264 " Analyse @var{s}, and return a list of fonts used in @var{s}.")
266 Stencil *stil = unsmob_stencil (s);
267 SCM_ASSERT_TYPE (stil, s, SCM_ARG1, __FUNCTION__, "Stencil");
268 return find_expression_fonts (stil->expr ());
271 struct Stencil_interpret_arguments
277 void stencil_interpret_in_scm (void *p, SCM expr)
279 Stencil_interpret_arguments *ap = (Stencil_interpret_arguments *) p;
280 scm_call_2 (ap->func, ap->arg1, expr);
283 LY_DEFINE (ly_interpret_stencil_expression, "ly:interpret-stencil-expression",
284 4, 0, 0, (SCM expr, SCM func, SCM arg1, SCM offset),
285 "Parse EXPR, feed bits to FUNC with first arg ARG1")
287 SCM_ASSERT_TYPE (ly_c_procedure_p (func), func, SCM_ARG1, __FUNCTION__,
290 Stencil_interpret_arguments a;
293 Offset o = ly_scm2offset (offset);
295 interpret_stencil_expression (expr, stencil_interpret_in_scm, (void *) & a, o);
297 return SCM_UNSPECIFIED;
300 LY_DEFINE (ly_bracket, "ly:bracket",
302 (SCM a, SCM iv, SCM t, SCM p),
303 "Make a bracket in direction @var{a}. The extent of the bracket is "
304 "given by @var{iv}. The wings protude by an amount of @var{p}, which "
305 "may be negative. The thickness is given by @var{t}.")
307 SCM_ASSERT_TYPE (is_axis (a), a, SCM_ARG1, __FUNCTION__, "axis");
308 SCM_ASSERT_TYPE (is_number_pair (iv), iv, SCM_ARG2, __FUNCTION__, "number pair");
309 SCM_ASSERT_TYPE (scm_is_number (t), a, SCM_ARG3, __FUNCTION__, "number");
310 SCM_ASSERT_TYPE (scm_is_number (p), a, SCM_ARG4, __FUNCTION__, "number");
312 return Lookup::bracket ((Axis)scm_to_int (a), ly_scm2interval (iv),
315 0.95 * scm_to_double (t)).smobbed_copy ();
318 LY_DEFINE (ly_filled_box, "ly:round-filled-box",
320 (SCM xext, SCM yext, SCM blot),
321 "Make a @code{Stencil} "
322 "that prints a black box of dimensions @var{xext}, "
323 "@var{yext} and roundness @var{blot}.")
325 SCM_ASSERT_TYPE (is_number_pair (xext), xext, SCM_ARG1, __FUNCTION__, "number pair");
326 SCM_ASSERT_TYPE (is_number_pair (yext), yext, SCM_ARG2, __FUNCTION__, "number pair");
327 SCM_ASSERT_TYPE (scm_is_number (blot), blot, SCM_ARG3, __FUNCTION__, "number");
329 return Lookup::round_filled_box (Box (ly_scm2interval (xext), ly_scm2interval (yext)),
330 scm_to_double (blot)).smobbed_copy ();