2 stencil-scheme.cc -- implement Stencil scheme accessors
4 source file of the GNU LilyPond music typesetter
6 (c) 1997--2006 Han-Wen Nienhuys <hanwen@xs4all.nl>
10 #include "font-metric.hh"
11 #include "libc-extension.hh"
16 TODO: naming add/combine.
19 LY_DEFINE (ly_translate_stencil_axis, "ly:stencil-translate-axis",
20 3, 0, 0, (SCM stil, SCM amount, SCM axis),
21 "Return a copy of @var{stil} but translated by @var{amount} in @var{axis} direction.")
23 Stencil *s = unsmob_stencil (stil);
24 SCM_ASSERT_TYPE (s, stil, SCM_ARG1, __FUNCTION__, "stencil");
25 SCM_ASSERT_TYPE (scm_is_number (amount), amount, SCM_ARG2, __FUNCTION__, "number");
27 Real real_amount = scm_to_double (amount);
30 SCM_ASSERT_TYPE (!isinf (real_amount) && !isnan (real_amount),
31 amount, SCM_ARG2, __FUNCTION__, "finite number");
34 SCM_ASSERT_TYPE (is_axis (axis), axis, SCM_ARG3, __FUNCTION__, "axis");
36 SCM new_s = s->smobbed_copy ();
37 Stencil *q = unsmob_stencil (new_s);
38 q->translate_axis (real_amount, Axis (scm_to_int (axis)));
42 LY_DEFINE (ly_translate_stencil, "ly:stencil-translate",
43 2, 0, 0, (SCM stil, SCM offset),
44 "Return a @var{stil}, "
45 "but translated by @var{offset} (a pair of numbers).")
47 Stencil *s = unsmob_stencil (stil);
48 SCM_ASSERT_TYPE (s, stil, SCM_ARG1, __FUNCTION__, "stencil");
49 SCM_ASSERT_TYPE (is_number_pair (offset), offset, SCM_ARG2, __FUNCTION__, "number pair");
50 Offset o = ly_scm2offset (offset);
52 SCM new_s = s->smobbed_copy ();
53 Stencil *q = unsmob_stencil (new_s);
58 LY_DEFINE (ly_stencil_expr, "ly:stencil-expr",
60 "Return the expression of @var{stil}.")
62 Stencil *s = unsmob_stencil (stil);
63 SCM_ASSERT_TYPE (s, stil, SCM_ARG1, __FUNCTION__, "stencil");
67 LY_DEFINE (ly_stencil_extent, "ly:stencil-extent",
68 2, 0, 0, (SCM stil, SCM axis),
69 "Return a pair of numbers signifying the extent of @var{stil} in "
70 "@var{axis} direction (0 or 1 for x and y axis respectively).")
72 Stencil *s = unsmob_stencil (stil);
73 SCM_ASSERT_TYPE (s, stil, SCM_ARG1, __FUNCTION__, "stencil");
74 SCM_ASSERT_TYPE (is_axis (axis), axis, SCM_ARG2, __FUNCTION__, "axis");
76 return ly_interval2scm (s->extent (Axis (scm_to_int (axis))));
79 LY_DEFINE (ly_stencil_empty_p, "ly:stencil-empty?",
81 "Return whether @var{stil} is empty ")
83 Stencil *s = unsmob_stencil (stil);
84 SCM_ASSERT_TYPE (s, stil, SCM_ARG1, __FUNCTION__, "stencil");
85 return scm_from_bool (s->is_empty ());
88 LY_DEFINE (ly_stencil_origin, "ly:stencil-origin",
89 2, 0, 0, (SCM stil, SCM axis),
90 "Return a pair of numbers signifying the origin @var{stil} in "
91 "@var{axis} direction (0 or 1 for x and y axis respectively).")
93 Stencil *s = unsmob_stencil (stil);
94 SCM_ASSERT_TYPE (s, stil, SCM_ARG1, __FUNCTION__, "stencil");
95 SCM_ASSERT_TYPE (is_axis (axis), axis, SCM_ARG2, __FUNCTION__, "axis");
97 return scm_from_double (s->origin ()[Axis (scm_to_int (axis))]);
100 LY_DEFINE (ly_stencil_moved_to_edge, "ly:stencil-moved-to-edge",
101 4, 2, 0, (SCM first, SCM axis, SCM direction, SCM second,
102 SCM padding, SCM minimum),
103 "Similar to @code{ly:stencil-combine-edge}, but returns "
104 "@var{second} positioned to be next to @var{first}. ")
107 C&P from combine-at-edge.
109 Stencil *s1 = unsmob_stencil (first);
110 Stencil *s2 = unsmob_stencil (second);
111 Stencil first_stencil;
113 SCM_ASSERT_TYPE (is_axis (axis), axis, SCM_ARG3, __FUNCTION__, "axis");
114 SCM_ASSERT_TYPE (is_direction (direction), direction, SCM_ARG4, __FUNCTION__, "dir");
117 if (padding != SCM_UNDEFINED)
119 SCM_ASSERT_TYPE (scm_is_number (padding), padding, SCM_ARG5, __FUNCTION__, "number");
120 p = scm_to_double (padding);
123 if (minimum != SCM_UNDEFINED)
125 SCM_ASSERT_TYPE (scm_is_number (minimum), minimum, SCM_ARG6, __FUNCTION__, "number");
126 m = scm_to_double (minimum);
133 return first_stencil.moved_to_edge (Axis (scm_to_int (axis)),
134 Direction (scm_to_int (direction)),
135 *s2, p, m).smobbed_copy ();
137 return Stencil ().smobbed_copy ();
140 LY_DEFINE (ly_stencil_combine_at_edge, "ly:stencil-combine-at-edge",
141 4, 2, 0, (SCM first, SCM axis, SCM direction,
145 "Construct a stencil by putting @var{second} next to @var{first}. "
146 "@var{axis} can be 0 (x-axis) or 1 (y-axis), "
147 "@var{direction} can be -1 (left or down) or 1 (right or up). "
148 "The stencils are juxtaposed with @var{padding} as extra space. "
149 "If this puts the reference points closer than @var{minimum}, "
150 "they are moved by the latter amount."
151 "@var{first} and @var{second} may also be '() or #f.")
153 Stencil *s1 = unsmob_stencil (first);
154 Stencil *s2 = unsmob_stencil (second);
157 SCM_ASSERT_TYPE (s1 || first == SCM_BOOL_F || first == SCM_EOL,
158 first, SCM_ARG1, __FUNCTION__, "Stencil, #f or ()");
159 SCM_ASSERT_TYPE (s2 || second == SCM_BOOL_F || second == SCM_EOL,
160 second, SCM_ARG4, __FUNCTION__, "Stencil, #f or ()");
161 SCM_ASSERT_TYPE (is_axis (axis), axis, SCM_ARG2, __FUNCTION__, "axis");
162 SCM_ASSERT_TYPE (is_direction (direction), direction, SCM_ARG3, __FUNCTION__, "dir");
165 if (padding != SCM_UNDEFINED)
167 SCM_ASSERT_TYPE (scm_is_number (padding), padding, SCM_ARG5, __FUNCTION__, "number");
168 p = scm_to_double (padding);
171 if (minimum != SCM_UNDEFINED)
173 SCM_ASSERT_TYPE (scm_is_number (minimum), minimum, SCM_ARG6, __FUNCTION__, "number");
174 m = scm_to_double (minimum);
181 result.add_at_edge (Axis (scm_to_int (axis)),
182 Direction (scm_to_int (direction)), *s2, p, m);
184 return result.smobbed_copy ();
187 LY_DEFINE (ly_stencil_add, "ly:stencil-add",
189 "Combine stencils. Takes any number of arguments.")
191 #define FUNC_NAME __FUNCTION__
192 SCM_VALIDATE_REST_ARGUMENT (args);
199 while (!SCM_NULLP (args))
201 Stencil *s = unsmob_stencil (scm_car (args));
203 SCM_ASSERT_TYPE (s, scm_car (args), SCM_ARGn, __FUNCTION__, "Stencil");
205 extent.unite (s->extent_box ());
206 *tail = scm_cons (s->expr (), SCM_EOL);
207 tail = SCM_CDRLOC (*tail);
208 args = scm_cdr (args);
211 expr = scm_cons (ly_symbol2scm ("combine-stencil"), expr);
212 return Stencil (extent, expr).smobbed_copy ();
215 LY_DEFINE (ly_make_stencil, "ly:make-stencil",
216 1, 2, 0, (SCM expr, SCM xext, SCM yext),
218 "Stencils are a device independent output expressions."
219 "They carry two pieces of information: \n\n"
220 "1: a specification of how to print this object. "
221 "This specification is processed by the output backends, "
222 " for example @file{scm/output-ps.scm}.\n\n"
223 "2: the vertical and horizontal extents of the object.\n\n"
224 "If the extents are unspecified, they are taken to be empty."
227 SCM_ASSERT_TYPE (!scm_is_pair (expr)
228 || is_stencil_head (scm_car (expr)),
229 expr, SCM_ARG1, __FUNCTION__, "registered stencil expression");
233 if (xext != SCM_UNDEFINED)
235 SCM_ASSERT_TYPE (is_number_pair (xext), xext, SCM_ARG2, __FUNCTION__, "number pair");
236 x = ly_scm2interval (xext);
240 if (yext != SCM_UNDEFINED)
242 SCM_ASSERT_TYPE (is_number_pair (yext), yext, SCM_ARG3, __FUNCTION__, "number pair");
243 y = ly_scm2interval (yext);
248 return s.smobbed_copy ();
251 LY_DEFINE (ly_stencil_aligned_to, "ly:stencil-aligned-to",
252 3, 0, 0, (SCM stil, SCM axis, SCM dir),
253 "Align @var{stil} using its own extents. "
254 "@var{dir} is a number -1, 1 are left and right respectively. "
255 "Other values are interpolated (so 0 means the center).")
257 SCM_ASSERT_TYPE (unsmob_stencil (stil), stil, SCM_ARG1, __FUNCTION__, "stencil");
258 SCM_ASSERT_TYPE (is_axis (axis), axis, SCM_ARG2, __FUNCTION__, "axis");
259 SCM_ASSERT_TYPE (scm_is_number (dir), dir, SCM_ARG3, __FUNCTION__, "number");
261 Stencil target = *unsmob_stencil (stil);
263 target.align_to ((Axis)scm_to_int (axis),
264 scm_to_double (dir));
265 return target.smobbed_copy ();
268 LY_DEFINE (ly_stencil_fonts, "ly:stencil-fonts",
270 " Analyse @var{s}, and return a list of fonts used in @var{s}.")
272 Stencil *stil = unsmob_stencil (s);
273 SCM_ASSERT_TYPE (stil, s, SCM_ARG1, __FUNCTION__, "Stencil");
274 return find_expression_fonts (stil->expr ());
277 LY_DEFINE (ly_stencil_in_color, "ly:stencil-in-color",
278 4, 0, 0, (SCM stc, SCM r, SCM g, SCM b),
279 "Put @var{stc} in a different color.")
281 Stencil *stil = unsmob_stencil (stc);
282 SCM_ASSERT_TYPE (stil, stc, SCM_ARG1, __FUNCTION__, "Stencil");
283 return Stencil (stil->extent_box (),
284 scm_list_3 (ly_symbol2scm ("color"),
285 scm_list_3 (r, g, b),
286 stil->expr ())).smobbed_copy ();
289 struct Stencil_interpret_arguments
295 void stencil_interpret_in_scm (void *p, SCM expr)
297 Stencil_interpret_arguments *ap = (Stencil_interpret_arguments *) p;
298 scm_call_2 (ap->func, ap->arg1, expr);
301 LY_DEFINE (ly_interpret_stencil_expression, "ly:interpret-stencil-expression",
302 4, 0, 0, (SCM expr, SCM func, SCM arg1, SCM offset),
303 "Parse EXPR, feed bits to FUNC with first arg ARG1")
305 SCM_ASSERT_TYPE (ly_is_procedure (func), func, SCM_ARG1, __FUNCTION__,
308 Stencil_interpret_arguments a;
311 Offset o = ly_scm2offset (offset);
313 interpret_stencil_expression (expr, stencil_interpret_in_scm, (void *) & a, o);
315 return SCM_UNSPECIFIED;
318 LY_DEFINE (ly_bracket, "ly:bracket",
320 (SCM a, SCM iv, SCM t, SCM p),
321 "Make a bracket in direction @var{a}. The extent of the bracket is "
322 "given by @var{iv}. The wings protude by an amount of @var{p}, which "
323 "may be negative. The thickness is given by @var{t}.")
325 SCM_ASSERT_TYPE (is_axis (a), a, SCM_ARG1, __FUNCTION__, "axis");
326 SCM_ASSERT_TYPE (is_number_pair (iv), iv, SCM_ARG2, __FUNCTION__, "number pair");
327 SCM_ASSERT_TYPE (scm_is_number (t), a, SCM_ARG3, __FUNCTION__, "number");
328 SCM_ASSERT_TYPE (scm_is_number (p), a, SCM_ARG4, __FUNCTION__, "number");
330 return Lookup::bracket ((Axis)scm_to_int (a), ly_scm2interval (iv),
333 0.95 * scm_to_double (t)).smobbed_copy ();
336 LY_DEFINE (ly_rotate_stencil, "ly:stencil-rotate",
337 4, 0, 0, (SCM stil, SCM angle, SCM x, SCM y),
338 "Return a @var{stil} rotated @var{angle} degrees around point (@var{x}, @var{y}).")
340 Stencil *s = unsmob_stencil (stil);
341 SCM_ASSERT_TYPE (s, stil, SCM_ARG1, __FUNCTION__, "stencil");
342 SCM_ASSERT_TYPE (scm_is_number (angle), angle, SCM_ARG2, __FUNCTION__, "number");
343 SCM_ASSERT_TYPE (scm_is_number (x), x, SCM_ARG3, __FUNCTION__, "number");
344 SCM_ASSERT_TYPE (scm_is_number (y), y, SCM_ARG4, __FUNCTION__, "number");
345 Real a = scm_to_double (angle);
346 Real x_off = scm_to_double (x);
347 Real y_off = scm_to_double (y);
349 SCM new_s = s->smobbed_copy ();
350 Stencil *q = unsmob_stencil (new_s);
351 q->rotate (a, Offset (x_off, y_off));
355 LY_DEFINE (ly_filled_box, "ly:round-filled-box",
357 (SCM xext, SCM yext, SCM blot),
358 "Make a @code{Stencil} "
359 "that prints a black box of dimensions @var{xext}, "
360 "@var{yext} and roundness @var{blot}.")
362 SCM_ASSERT_TYPE (is_number_pair (xext), xext, SCM_ARG1, __FUNCTION__, "number pair");
363 SCM_ASSERT_TYPE (is_number_pair (yext), yext, SCM_ARG2, __FUNCTION__, "number pair");
364 SCM_ASSERT_TYPE (scm_is_number (blot), blot, SCM_ARG3, __FUNCTION__, "number");
366 return Lookup::round_filled_box (Box (ly_scm2interval (xext), ly_scm2interval (yext)),
367 scm_to_double (blot)).smobbed_copy ();
370 LY_DEFINE (ly_register_stencil_expression, "ly:register-stencil-expression",
373 "Add @var{symbol} as head of a stencil expression")
375 SCM_ASSERT_TYPE (scm_is_symbol (symbol), symbol,
376 SCM_ARG1, __FUNCTION__, "Symbol");
377 register_stencil_head (symbol);
378 return SCM_UNSPECIFIED;
381 LY_DEFINE (ly_all_stencil_expressions, "ly:all-stencil-expressions",
384 "Return all symbols recognized as stencil expressions.")
386 return all_stencil_heads ();