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 LY_DEFINE (ly_translate_stencil_axis, "ly:stencil-translate-axis",
21 3, 0, 0, (SCM stil, SCM amount, SCM axis),
22 "Return a copy of @var{stil} but translated by @var{amount} in @var{axis} direction.")
24 Stencil *s = unsmob_stencil (stil);
25 SCM_ASSERT_TYPE (s, stil, SCM_ARG1, __FUNCTION__, "stencil");
26 SCM_ASSERT_TYPE (scm_is_number (amount), amount, SCM_ARG2, __FUNCTION__, "number");
28 Real real_amount = scm_to_double (amount);
31 SCM_ASSERT_TYPE (!isinf (real_amount) && !isnan (real_amount),
32 amount, SCM_ARG2, __FUNCTION__, "finite number");
35 SCM_ASSERT_TYPE (is_axis (axis), axis, SCM_ARG3, __FUNCTION__, "axis");
37 SCM new_s = s->smobbed_copy ();
38 Stencil *q = unsmob_stencil (new_s);
39 q->translate_axis (real_amount, Axis (scm_to_int (axis)));
43 LY_DEFINE (ly_translate_stencil, "ly:stencil-translate",
44 2, 0, 0, (SCM stil, SCM offset),
45 "Return a @var{stil}, "
46 "but translated by @var{offset} (a pair of numbers).")
48 Stencil *s = unsmob_stencil (stil);
49 SCM_ASSERT_TYPE (s, stil, SCM_ARG1, __FUNCTION__, "stencil");
50 SCM_ASSERT_TYPE (is_number_pair (offset), offset, SCM_ARG2, __FUNCTION__, "number pair");
51 Offset o = ly_scm2offset (offset);
53 SCM new_s = s->smobbed_copy ();
54 Stencil *q = unsmob_stencil (new_s);
59 LY_DEFINE (ly_stencil_expr, "ly:stencil-expr",
61 "Return the expression of @var{stil}.")
63 Stencil *s = unsmob_stencil (stil);
64 SCM_ASSERT_TYPE (s, stil, SCM_ARG1, __FUNCTION__, "stencil");
68 LY_DEFINE (ly_stencil_get_extent, "ly:stencil-extent",
69 2, 0, 0, (SCM stil, SCM axis),
70 "Return a pair of numbers signifying the extent of @var{stil} in "
71 "@var{axis} direction (0 or 1 for x and y axis respectively).")
73 Stencil *s = unsmob_stencil (stil);
74 SCM_ASSERT_TYPE (s, stil, SCM_ARG1, __FUNCTION__, "stencil");
75 SCM_ASSERT_TYPE (is_axis (axis), axis, SCM_ARG2, __FUNCTION__, "axis");
77 return ly_interval2scm (s->extent (Axis (scm_to_int (axis))));
80 LY_DEFINE (ly_stencil_empty_p, "ly:stencil-empty?",
82 "Return whether @var{stil} is empty ")
84 Stencil *s = unsmob_stencil (stil);
85 SCM_ASSERT_TYPE (s, stil, SCM_ARG1, __FUNCTION__, "stencil");
86 return scm_from_bool (s->is_empty ());
89 LY_DEFINE (ly_stencil_origin, "ly:stencil-origin",
90 2, 0, 0, (SCM stil, SCM axis),
91 "Return a pair of numbers signifying the origin @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 scm_from_double (s->origin ()[Axis (scm_to_int (axis))]);
101 LY_DEFINE (ly_stencil_moved_to_edge, "ly:stencil-moved-to-edge",
102 4, 2, 0, (SCM first, SCM axis, SCM direction, SCM second,
103 SCM padding, SCM minimum),
104 "Similar to @code{ly:stencil-combine-edge}, but returns "
105 "@var{second} positioned to be next to @var{first}. ")
108 C&P from combine-at-edge.
110 Stencil *s1 = unsmob_stencil (first);
111 Stencil *s2 = unsmob_stencil (second);
112 Stencil first_stencil;
114 SCM_ASSERT_TYPE (is_axis (axis), axis, SCM_ARG3, __FUNCTION__, "axis");
115 SCM_ASSERT_TYPE (is_direction (direction), direction, SCM_ARG4, __FUNCTION__, "dir");
118 if (padding != SCM_UNDEFINED)
120 SCM_ASSERT_TYPE (scm_is_number (padding), padding, SCM_ARG5, __FUNCTION__, "number");
121 p = scm_to_double (padding);
124 if (minimum != SCM_UNDEFINED)
126 SCM_ASSERT_TYPE (scm_is_number (minimum), minimum, SCM_ARG6, __FUNCTION__, "number");
127 m = scm_to_double (minimum);
134 return first_stencil.moved_to_edge (Axis (scm_to_int (axis)),
135 Direction (scm_to_int (direction)),
136 *s2, p, m).smobbed_copy ();
138 return Stencil ().smobbed_copy ();
141 LY_DEFINE (ly_stencil_combine_at_edge, "ly:stencil-combine-at-edge",
142 4, 2, 0, (SCM first, SCM axis, SCM direction,
146 "Construct a stencil by putting @var{second} next to @var{first}. "
147 "@var{axis} can be 0 (x-axis) or 1 (y-axis), "
148 "@var{direction} can be -1 (left or down) or 1 (right or up). "
149 "The stencils are juxtaposed with @var{padding} as extra space. "
150 "If this puts the reference points closer than @var{minimum}, "
151 "they are moved by the latter amount."
152 "@var{first} and @var{second} may also be '() or #f.")
154 Stencil *s1 = unsmob_stencil (first);
155 Stencil *s2 = unsmob_stencil (second);
158 SCM_ASSERT_TYPE (s1 || first == SCM_BOOL_F || first == SCM_EOL,
159 first, SCM_ARG1, __FUNCTION__, "Stencil, #f or ()");
160 SCM_ASSERT_TYPE (s2 || second == SCM_BOOL_F || second == SCM_EOL,
161 second, SCM_ARG4, __FUNCTION__, "Stencil, #f or ()");
162 SCM_ASSERT_TYPE (is_axis (axis), axis, SCM_ARG2, __FUNCTION__, "axis");
163 SCM_ASSERT_TYPE (is_direction (direction), direction, SCM_ARG3, __FUNCTION__, "dir");
166 if (padding != SCM_UNDEFINED)
168 SCM_ASSERT_TYPE (scm_is_number (padding), padding, SCM_ARG5, __FUNCTION__, "number");
169 p = scm_to_double (padding);
172 if (minimum != SCM_UNDEFINED)
174 SCM_ASSERT_TYPE (scm_is_number (minimum), minimum, SCM_ARG6, __FUNCTION__, "number");
175 m = scm_to_double (minimum);
182 result.add_at_edge (Axis (scm_to_int (axis)),
183 Direction (scm_to_int (direction)), *s2, p, m);
185 return result.smobbed_copy ();
188 LY_DEFINE (ly_stencil_add, "ly:stencil-add",
190 "Combine stencils. Takes any number of arguments.")
192 #define FUNC_NAME __FUNCTION__
193 SCM_VALIDATE_REST_ARGUMENT (args);
200 while (!SCM_NULLP (args))
202 Stencil *s = unsmob_stencil (scm_car (args));
204 SCM_ASSERT_TYPE (s, scm_car (args), SCM_ARGn, __FUNCTION__, "Stencil");
207 extent.unite (s->extent_box ());
208 *tail = scm_cons (s->expr (), SCM_EOL);
209 tail = SCM_CDRLOC (*tail);
210 args = scm_cdr (args);
213 expr = scm_cons (ly_symbol2scm ("combine-stencil"), expr);
214 return Stencil (extent, expr).smobbed_copy ();
217 LY_DEFINE (ly_make_stencil, "ly:make-stencil",
218 3, 0, 0, (SCM expr, SCM xext, SCM yext),
220 "Stencils are a device independent output expressions."
221 "They carry two pieces of information: \n\n"
222 "1: a specification of how to print this object. "
223 "This specification is processed by the output backends, "
224 " for example @file{scm/output-tex.scm}.\n\n"
225 "2: the vertical and horizontal extents of the object.\n\n")
227 SCM_ASSERT_TYPE (is_number_pair (xext), xext, SCM_ARG2, __FUNCTION__, "number pair");
228 SCM_ASSERT_TYPE (is_number_pair (yext), yext, SCM_ARG3, __FUNCTION__, "number pair");
230 Box b (ly_scm2interval (xext), ly_scm2interval (yext));
232 return s.smobbed_copy ();
235 LY_DEFINE (ly_stencil_aligned_to, "ly:stencil-aligned-to",
236 3, 0, 0, (SCM stil, SCM axis, SCM dir),
237 "Align @var{stil} using its own extents. "
238 "@var{dir} is a number -1, 1 are left and right respectively. "
239 "Other values are interpolated (so 0 means the center).")
241 SCM_ASSERT_TYPE (unsmob_stencil (stil), stil, SCM_ARG1, __FUNCTION__, "stencil");
242 SCM_ASSERT_TYPE (is_axis (axis), axis, SCM_ARG2, __FUNCTION__, "axis");
243 SCM_ASSERT_TYPE (scm_is_number (dir), dir, SCM_ARG3, __FUNCTION__, "number");
245 Stencil target = *unsmob_stencil (stil);
247 target.align_to ((Axis)scm_to_int (axis),
248 scm_to_double (dir));
249 return target.smobbed_copy ();
252 LY_DEFINE (ly_stencil_fonts, "ly:stencil-fonts",
254 " Analyse @var{s}, and return a list of fonts used in @var{s}.")
256 Stencil *stil = unsmob_stencil (s);
257 SCM_ASSERT_TYPE (stil, s, SCM_ARG1, __FUNCTION__, "Stencil");
258 return find_expression_fonts (stil->expr ());
262 LY_DEFINE (ly_stencil_in_color, "ly:stencil-in-color",
263 4, 0, 0, (SCM stc, SCM r, SCM g, SCM b),
264 "Put @var{stc} in a different color.")
266 Stencil *stil = unsmob_stencil (stc);
267 SCM_ASSERT_TYPE (stil, stc, SCM_ARG1, __FUNCTION__, "Stencil");
268 return Stencil (stil->extent_box (),
269 scm_list_3 (ly_symbol2scm ("color"),
270 scm_list_3 (r, g, b),
271 stil->expr ())).smobbed_copy ();
275 struct Stencil_interpret_arguments
281 void stencil_interpret_in_scm (void *p, SCM expr)
283 Stencil_interpret_arguments *ap = (Stencil_interpret_arguments *) p;
284 scm_call_2 (ap->func, ap->arg1, expr);
287 LY_DEFINE (ly_interpret_stencil_expression, "ly:interpret-stencil-expression",
288 4, 0, 0, (SCM expr, SCM func, SCM arg1, SCM offset),
289 "Parse EXPR, feed bits to FUNC with first arg ARG1")
291 SCM_ASSERT_TYPE (ly_c_procedure_p (func), func, SCM_ARG1, __FUNCTION__,
294 Stencil_interpret_arguments a;
297 Offset o = ly_scm2offset (offset);
299 interpret_stencil_expression (expr, stencil_interpret_in_scm, (void *) & a, o);
301 return SCM_UNSPECIFIED;
304 LY_DEFINE (ly_bracket, "ly:bracket",
306 (SCM a, SCM iv, SCM t, SCM p),
307 "Make a bracket in direction @var{a}. The extent of the bracket is "
308 "given by @var{iv}. The wings protude by an amount of @var{p}, which "
309 "may be negative. The thickness is given by @var{t}.")
311 SCM_ASSERT_TYPE (is_axis (a), a, SCM_ARG1, __FUNCTION__, "axis");
312 SCM_ASSERT_TYPE (is_number_pair (iv), iv, SCM_ARG2, __FUNCTION__, "number pair");
313 SCM_ASSERT_TYPE (scm_is_number (t), a, SCM_ARG3, __FUNCTION__, "number");
314 SCM_ASSERT_TYPE (scm_is_number (p), a, SCM_ARG4, __FUNCTION__, "number");
316 return Lookup::bracket ((Axis)scm_to_int (a), ly_scm2interval (iv),
319 0.95 * scm_to_double (t)).smobbed_copy ();
322 LY_DEFINE (ly_filled_box, "ly:round-filled-box",
324 (SCM xext, SCM yext, SCM blot),
325 "Make a @code{Stencil} "
326 "that prints a black box of dimensions @var{xext}, "
327 "@var{yext} and roundness @var{blot}.")
329 SCM_ASSERT_TYPE (is_number_pair (xext), xext, SCM_ARG1, __FUNCTION__, "number pair");
330 SCM_ASSERT_TYPE (is_number_pair (yext), yext, SCM_ARG2, __FUNCTION__, "number pair");
331 SCM_ASSERT_TYPE (scm_is_number (blot), blot, SCM_ARG3, __FUNCTION__, "number");
333 return Lookup::round_filled_box (Box (ly_scm2interval (xext), ly_scm2interval (yext)),
334 scm_to_double (blot)).smobbed_copy ();