2 This file is part of LilyPond, the GNU music typesetter.
4 Copyright (C) 1997--2012 Han-Wen Nienhuys <hanwen@xs4all.nl>
6 LilyPond is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation, either version 3 of the License, or
9 (at your option) any later version.
11 LilyPond is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with LilyPond. If not, see <http://www.gnu.org/licenses/>.
20 #include "font-metric.hh"
21 #include "libc-extension.hh"
26 TODO: naming add/combine.
29 LY_DEFINE (ly_stencil_translate_axis, "ly:stencil-translate-axis",
30 3, 0, 0, (SCM stil, SCM amount, SCM axis),
31 "Return a copy of @var{stil} but translated by @var{amount}"
32 " in @var{axis} direction.")
34 Stencil *s = unsmob_stencil (stil);
35 LY_ASSERT_SMOB (Stencil, stil, 1);
36 LY_ASSERT_TYPE (scm_is_number, amount, 2);
38 LY_ASSERT_TYPE (is_axis, axis, 3);
40 Real real_amount = scm_to_double (amount);
42 SCM new_s = s->smobbed_copy ();
43 Stencil *q = unsmob_stencil (new_s);
44 q->translate_axis (real_amount, Axis (scm_to_int (axis)));
48 LY_DEFINE (ly_stencil_translate, "ly:stencil-translate",
49 2, 0, 0, (SCM stil, SCM offset),
50 "Return a @var{stil}, but translated by @var{offset}"
51 " (a pair of numbers).")
53 Stencil *s = unsmob_stencil (stil);
54 LY_ASSERT_SMOB (Stencil, stil, 1);
55 LY_ASSERT_TYPE (is_number_pair, offset, 2);
56 Offset o = ly_scm2offset (offset);
58 SCM new_s = s->smobbed_copy ();
59 Stencil *q = unsmob_stencil (new_s);
64 LY_DEFINE (ly_stencil_expr, "ly:stencil-expr",
66 "Return the expression of @var{stil}.")
68 Stencil *s = unsmob_stencil (stil);
69 LY_ASSERT_SMOB (Stencil, stil, 1);
73 LY_DEFINE (ly_stencil_extent, "ly:stencil-extent",
74 2, 0, 0, (SCM stil, SCM axis),
75 "Return a pair of numbers signifying the extent of @var{stil} in"
76 " @var{axis} direction (@code{0} or @code{1} for x and"
77 " y@tie{}axis, respectively).")
79 Stencil *s = unsmob_stencil (stil);
80 LY_ASSERT_SMOB (Stencil, stil, 1);
81 LY_ASSERT_TYPE (is_axis, axis, 2);
83 return ly_interval2scm (s->extent (Axis (scm_to_int (axis))));
86 LY_DEFINE (ly_stencil_empty_p, "ly:stencil-empty?",
87 1, 1, 0, (SCM stil, SCM axis),
88 "Return whether @var{stil} is empty. If an optional"
89 " @var{axis} is supplied, the emptiness check is"
90 " restricted to that axis.")
92 Stencil *s = unsmob_stencil (stil);
93 LY_ASSERT_SMOB (Stencil, stil, 1);
94 if (SCM_UNBNDP (axis))
95 return scm_from_bool (s->is_empty ());
96 LY_ASSERT_TYPE (is_axis, axis, 2);
97 return scm_from_bool (s->is_empty (Axis (scm_to_int (axis))));
100 LY_DEFINE (ly_stencil_combine_at_edge, "ly:stencil-combine-at-edge",
101 4, 1, 0, (SCM first, SCM axis, SCM direction,
104 "Construct a stencil by putting @var{second} next to @var{first}."
105 " @var{axis} can be 0 (x-axis) or@tie{}1 (y-axis)."
106 " @var{direction} can be -1 (left or down) or@tie{}1 (right or"
107 " up). The stencils are juxtaposed with @var{padding} as extra"
108 " space. @var{first} and @var{second} may also be @code{'()} or"
111 Stencil *s1 = unsmob_stencil (first);
112 Stencil *s2 = unsmob_stencil (second);
115 SCM_ASSERT_TYPE (s1 || first == SCM_BOOL_F || first == SCM_EOL,
116 first, SCM_ARG1, __FUNCTION__, "Stencil, #f or ()");
117 SCM_ASSERT_TYPE (s2 || second == SCM_BOOL_F || second == SCM_EOL,
118 second, SCM_ARG4, __FUNCTION__, "Stencil, #f or ()");
119 LY_ASSERT_TYPE (is_axis, axis, 2);
120 LY_ASSERT_TYPE (is_direction, direction, 3);
123 if (padding != SCM_UNDEFINED)
125 LY_ASSERT_TYPE (scm_is_number, padding, 5);
126 p = scm_to_double (padding);
133 result.add_at_edge (Axis (scm_to_int (axis)),
134 Direction (scm_to_int (direction)), *s2, p);
136 return result.smobbed_copy ();
139 LY_DEFINE (ly_stencil_add, "ly:stencil-add",
141 "Combine stencils. Takes any number of arguments.")
143 #define FUNC_NAME __FUNCTION__
144 SCM_VALIDATE_REST_ARGUMENT (args);
147 SCM cs = ly_symbol2scm ("combine-stencil");
152 while (!SCM_NULLP (args))
154 Stencil *s = unsmob_stencil (scm_car (args));
156 SCM_ASSERT_TYPE (s, scm_car (args), SCM_ARGn, __FUNCTION__, "Stencil");
158 extent.unite (s->extent_box ());
159 if (scm_is_pair (s->expr ()) && scm_is_eq (cs, s->expr ()))
161 expr = scm_reverse_x (scm_list_copy (scm_cdr (s->expr ())),
165 expr = scm_cons (s->expr (), expr);
167 args = scm_cdr (args);
170 expr = scm_cons (cs, scm_reverse_x (expr, SCM_EOL));
171 return Stencil (extent, expr).smobbed_copy ();
174 LY_DEFINE (ly_make_stencil, "ly:make-stencil",
175 1, 2, 0, (SCM expr, SCM xext, SCM yext),
176 "Stencils are device independent output expressions."
177 " They carry two pieces of information:\n"
181 "A specification of how to print this object."
182 " This specification is processed by the output backends,"
183 " for example @file{scm/output-ps.scm}.\n"
186 "The vertical and horizontal extents of the object, given as"
187 " pairs. If an extent is unspecified (or if you use"
188 " @code{empty-interval} as its value), it is taken to be empty.\n"
191 SCM_ASSERT_TYPE (!scm_is_pair (expr)
192 || is_stencil_head (scm_car (expr)),
193 expr, SCM_ARG1, __FUNCTION__, "registered stencil expression");
196 if (xext != SCM_UNDEFINED)
198 LY_ASSERT_TYPE (is_number_pair, xext, 2);
199 x = ly_scm2interval (xext);
203 if (yext != SCM_UNDEFINED)
205 LY_ASSERT_TYPE (is_number_pair, yext, 3);
206 y = ly_scm2interval (yext);
211 return s.smobbed_copy ();
214 LY_DEFINE (ly_stencil_aligned_to, "ly:stencil-aligned-to",
215 3, 0, 0, (SCM stil, SCM axis, SCM dir),
216 "Align @var{stil} using its own extents. @var{dir} is a number."
217 " @w{@code{-1}} and @code{1} are left and right, respectively."
218 " Other values are interpolated (so @code{0} means the center).")
220 LY_ASSERT_SMOB (Stencil, stil, 1);
221 LY_ASSERT_TYPE (is_axis, axis, 2);
222 LY_ASSERT_TYPE (scm_is_number, dir, 3);
224 Stencil target = *unsmob_stencil (stil);
226 target.align_to ((Axis)scm_to_int (axis),
227 scm_to_double (dir));
228 return target.smobbed_copy ();
231 LY_DEFINE (ly_stencil_fonts, "ly:stencil-fonts",
233 "Analyze @var{s}, and return a list of fonts used"
236 LY_ASSERT_SMOB (Stencil, s, 1);
237 Stencil *stil = unsmob_stencil (s);
238 return find_expression_fonts (stil->expr ());
241 LY_DEFINE (ly_stencil_in_color, "ly:stencil-in-color",
242 4, 0, 0, (SCM stc, SCM r, SCM g, SCM b),
243 "Put @var{stc} in a different color.")
245 LY_ASSERT_SMOB (Stencil, stc, 1);
246 Stencil *stil = unsmob_stencil (stc);
247 return Stencil (stil->extent_box (),
248 scm_list_3 (ly_symbol2scm ("color"),
249 scm_list_3 (r, g, b),
250 stil->expr ())).smobbed_copy ();
253 struct Stencil_interpret_arguments
259 void stencil_interpret_in_scm (void *p, SCM expr)
261 Stencil_interpret_arguments *ap = (Stencil_interpret_arguments *) p;
262 scm_call_2 (ap->func, ap->arg1, expr);
265 LY_DEFINE (ly_interpret_stencil_expression, "ly:interpret-stencil-expression",
266 4, 0, 0, (SCM expr, SCM func, SCM arg1, SCM offset),
267 "Parse @var{expr}, feed bits to @var{func} with first arg"
268 " @var{arg1} having offset @var{offset}.")
270 LY_ASSERT_TYPE (ly_is_procedure, func, 2);
272 Stencil_interpret_arguments a;
275 Offset o = ly_scm2offset (offset);
277 interpret_stencil_expression (expr, stencil_interpret_in_scm, (void *) & a, o);
279 return SCM_UNSPECIFIED;
282 LY_DEFINE (ly_bracket, "ly:bracket",
284 (SCM a, SCM iv, SCM t, SCM p),
285 "Make a bracket in direction@tie{}@var{a}. The extent of the"
286 " bracket is given by @var{iv}. The wings protrude by an amount"
287 " of@tie{}@var{p}, which may be negative. The thickness is given"
290 LY_ASSERT_TYPE (is_axis, a, 1);
291 LY_ASSERT_TYPE (is_number_pair, iv, 2);
292 LY_ASSERT_TYPE (scm_is_number, t, 3);
293 LY_ASSERT_TYPE (scm_is_number, p, 4);
295 return Lookup::bracket ((Axis)scm_to_int (a), ly_scm2interval (iv),
298 0.95 * scm_to_double (t)).smobbed_copy ();
301 LY_DEFINE (ly_stencil_rotate, "ly:stencil-rotate",
302 4, 0, 0, (SCM stil, SCM angle, SCM x, SCM y),
303 "Return a stencil @var{stil} rotated @var{angle} degrees around"
304 " the relative offset (@var{x}, @var{y}). E.g., an offset of"
305 " (-1, 1) will rotate the stencil around the left upper corner.")
307 Stencil *s = unsmob_stencil (stil);
308 LY_ASSERT_SMOB (Stencil, stil, 1);
309 LY_ASSERT_TYPE (scm_is_number, angle, 2);
310 LY_ASSERT_TYPE (scm_is_number, x, 3);
311 LY_ASSERT_TYPE (scm_is_number, y, 4);
312 Real a = scm_to_double (angle);
313 Real x_off = scm_to_double (x);
314 Real y_off = scm_to_double (y);
316 SCM new_s = s->smobbed_copy ();
317 Stencil *q = unsmob_stencil (new_s);
318 q->rotate_degrees (a, Offset (x_off, y_off));
322 LY_DEFINE (ly_stencil_rotate_absolute, "ly:stencil-rotate-absolute",
323 4, 0, 0, (SCM stil, SCM angle, SCM x, SCM y),
324 "Return a stencil @var{stil} rotated @var{angle} degrees around"
325 " point (@var{x}, @var{y}), given in absolute coordinates.")
327 Stencil *s = unsmob_stencil (stil);
328 LY_ASSERT_SMOB (Stencil, stil, 1);
329 LY_ASSERT_TYPE (scm_is_number, angle, 2);
330 LY_ASSERT_TYPE (scm_is_number, x, 3);
331 LY_ASSERT_TYPE (scm_is_number, y, 4);
332 Real a = scm_to_double (angle);
333 Real x_off = scm_to_double (x);
334 Real y_off = scm_to_double (y);
336 SCM new_s = s->smobbed_copy ();
337 Stencil *q = unsmob_stencil (new_s);
338 q->rotate_degrees_absolute (a, Offset (x_off, y_off));
342 LY_DEFINE (ly_round_filled_box, "ly:round-filled-box",
344 (SCM xext, SCM yext, SCM blot),
345 "Make a @code{Stencil} object that prints a black box of"
346 " dimensions @var{xext}, @var{yext} and roundness @var{blot}.")
348 LY_ASSERT_TYPE (is_number_pair, xext, 1);
349 LY_ASSERT_TYPE (is_number_pair, yext, 2);
350 LY_ASSERT_TYPE (scm_is_number, blot, 3);
352 return Lookup::round_filled_box (Box (ly_scm2interval (xext), ly_scm2interval (yext)),
353 scm_to_double (blot)).smobbed_copy ();
356 LY_DEFINE (ly_round_filled_polygon, "ly:round-filled-polygon",
358 (SCM points, SCM blot),
359 "Make a @code{Stencil} object that prints a black polygon with"
360 " corners at the points defined by @var{points} (list of coordinate"
361 " pairs) and roundness @var{blot}.")
363 SCM_ASSERT_TYPE (scm_ilength (points) > 0, points, SCM_ARG1, __FUNCTION__, "list of coordinate pairs");
364 LY_ASSERT_TYPE (scm_is_number, blot, 2);
366 for (SCM p = points; scm_is_pair (p); p = scm_cdr (p))
368 SCM scm_pt = scm_car (p);
369 if (scm_is_pair (scm_pt))
371 pts.push_back (ly_scm2offset (scm_pt));
375 // TODO: Print out warning
378 return Lookup::round_filled_polygon (pts, scm_to_double (blot)).smobbed_copy ();
381 LY_DEFINE (ly_register_stencil_expression, "ly:register-stencil-expression",
384 "Add @var{symbol} as head of a stencil expression.")
386 LY_ASSERT_TYPE (ly_is_symbol, symbol, 1);
387 register_stencil_head (symbol);
388 return SCM_UNSPECIFIED;
391 LY_DEFINE (ly_all_stencil_expressions, "ly:all-stencil-expressions",
394 "Return all symbols recognized as stencil expressions.")
396 return all_stencil_heads ();
399 LY_DEFINE (ly_stencil_scale, "ly:stencil-scale",
400 3, 0, 0, (SCM stil, SCM x, SCM y),
401 "Scale @var{stil} using the horizontal and vertical scaling"
402 " factors @var{x} and @var{y}.")
404 Stencil *s = unsmob_stencil (stil);
405 LY_ASSERT_SMOB (Stencil, stil, 1);
406 LY_ASSERT_TYPE (scm_is_number, x, 2);
407 LY_ASSERT_TYPE (scm_is_number, y, 3);
409 SCM new_s = s->smobbed_copy ();
410 Stencil *q = unsmob_stencil (new_s);
412 q->scale (scm_to_double (x), scm_to_double (y));