2 This file is part of LilyPond, the GNU music typesetter.
4 Copyright (C) 1997--2015 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"
28 TODO: naming add/combine.
31 LY_DEFINE (ly_stencil_translate_axis, "ly:stencil-translate-axis",
32 3, 0, 0, (SCM stil, SCM amount, SCM axis),
33 "Return a copy of @var{stil} but translated by @var{amount}"
34 " in @var{axis} direction.")
36 Stencil *s = unsmob<Stencil> (stil);
37 LY_ASSERT_SMOB (Stencil, stil, 1);
38 LY_ASSERT_TYPE (scm_is_number, amount, 2);
40 LY_ASSERT_TYPE (is_axis, axis, 3);
42 Real real_amount = scm_to_double (amount);
44 SCM new_s = s->smobbed_copy ();
45 scm_remember_upto_here_1 (stil);
47 Stencil *q = unsmob<Stencil> (new_s);
48 q->translate_axis (real_amount, Axis (scm_to_int (axis)));
52 LY_DEFINE (ly_stencil_translate, "ly:stencil-translate",
53 2, 0, 0, (SCM stil, SCM offset),
54 "Return a @var{stil}, but translated by @var{offset}"
55 " (a pair of numbers).")
57 Stencil *s = unsmob<Stencil> (stil);
58 LY_ASSERT_SMOB (Stencil, stil, 1);
59 LY_ASSERT_TYPE (is_number_pair, offset, 2);
60 Offset o = ly_scm2offset (offset);
62 SCM new_s = s->smobbed_copy ();
63 scm_remember_upto_here_1 (stil);
65 Stencil *q = unsmob<Stencil> (new_s);
70 LY_DEFINE (ly_stencil_expr, "ly:stencil-expr",
72 "Return the expression of @var{stil}.")
74 Stencil *s = unsmob<Stencil> (stil);
75 LY_ASSERT_SMOB (Stencil, stil, 1);
79 LY_DEFINE (ly_stencil_extent, "ly:stencil-extent",
80 2, 0, 0, (SCM stil, SCM axis),
81 "Return a pair of numbers signifying the extent of @var{stil} in"
82 " @var{axis} direction (@code{0} or @code{1} for x and"
83 " y@tie{}axis, respectively).")
85 Stencil *s = unsmob<Stencil> (stil);
86 LY_ASSERT_SMOB (Stencil, stil, 1);
87 LY_ASSERT_TYPE (is_axis, axis, 2);
89 return ly_interval2scm (s->extent (Axis (scm_to_int (axis))));
92 LY_DEFINE (ly_stencil_empty_p, "ly:stencil-empty?",
93 1, 1, 0, (SCM stil, SCM axis),
94 "Return whether @var{stil} is empty. If an optional"
95 " @var{axis} is supplied, the emptiness check is"
96 " restricted to that axis.")
98 Stencil *s = unsmob<Stencil> (stil);
99 LY_ASSERT_SMOB (Stencil, stil, 1);
100 if (SCM_UNBNDP (axis))
101 return scm_from_bool (s->is_empty ());
102 LY_ASSERT_TYPE (is_axis, axis, 2);
103 return scm_from_bool (s->is_empty (Axis (scm_to_int (axis))));
106 LY_DEFINE (ly_stencil_combine_at_edge, "ly:stencil-combine-at-edge",
107 4, 1, 0, (SCM first, SCM axis, SCM direction,
110 "Construct a stencil by putting @var{second} next to @var{first}."
111 " @var{axis} can be 0 (x-axis) or@tie{}1 (y-axis)."
112 " @var{direction} can be -1 (left or down) or@tie{}1 (right or"
113 " up). The stencils are juxtaposed with @var{padding} as extra"
114 " space. @var{first} and @var{second} may also be @code{'()} or"
117 Stencil *s1 = unsmob<Stencil> (first);
118 Stencil *s2 = unsmob<Stencil> (second);
121 SCM_ASSERT_TYPE (s1 || scm_is_false (first) || scm_is_null (first),
122 first, SCM_ARG1, __FUNCTION__, "Stencil, #f or ()");
123 SCM_ASSERT_TYPE (s2 || scm_is_false (second) || scm_is_null (second),
124 second, SCM_ARG4, __FUNCTION__, "Stencil, #f or ()");
125 LY_ASSERT_TYPE (is_axis, axis, 2);
126 LY_ASSERT_TYPE (is_direction, direction, 3);
129 if (!SCM_UNBNDP (padding))
131 LY_ASSERT_TYPE (scm_is_number, padding, 5);
132 p = scm_to_double (padding);
139 result.add_at_edge (Axis (scm_to_int (axis)),
140 Direction (scm_to_int (direction)), *s2, p);
142 scm_remember_upto_here_2 (first, second);
144 return result.smobbed_copy ();
147 LY_DEFINE (ly_stencil_stack, "ly:stencil-stack",
148 4, 2, 0, (SCM first, SCM axis, SCM direction,
152 "Construct a stencil by stacking @var{second} next to @var{first}."
153 " @var{axis} can be 0 (x-axis) or@tie{}1 (y-axis)."
154 " @var{direction} can be -1 (left or down) or@tie{}1 (right or"
155 " up). The stencils are juxtaposed with @var{padding} as extra"
156 " space. @var{first} and @var{second} may also be @code{'()} or"
157 " @code{#f}. As opposed to @code{ly:stencil-combine-at-edge},"
158 " metrics are suited for successively accumulating lines of"
159 " stencils. Also, @var{second} stencil is drawn last.\n\n"
160 "If @var{mindist} is specified, reference points are placed"
161 " apart at least by this distance. If either of the stencils"
162 " is spacing, @var{padding} and @var{mindist} do not apply.")
164 Stencil *s1 = unsmob<Stencil> (first);
165 Stencil *s2 = unsmob<Stencil> (second);
168 SCM_ASSERT_TYPE (s1 || scm_is_false (first) || scm_is_null (first),
169 first, SCM_ARG1, __FUNCTION__, "Stencil, #f or ()");
170 SCM_ASSERT_TYPE (s2 || scm_is_false (second) || scm_is_null (second),
171 second, SCM_ARG4, __FUNCTION__, "Stencil, #f or ()");
172 LY_ASSERT_TYPE (is_axis, axis, 2);
173 LY_ASSERT_TYPE (is_direction, direction, 3);
176 if (!SCM_UNBNDP (padding))
178 LY_ASSERT_TYPE (scm_is_number, padding, 5);
179 p = scm_to_double (padding);
181 Real d = -infinity_f;
182 if (!SCM_UNBNDP (mindist))
184 LY_ASSERT_TYPE (scm_is_number, mindist, 6);
185 d = scm_to_double (mindist);
192 result.stack (Axis (scm_to_int (axis)),
193 Direction (scm_to_int (direction)), *s2, p, d);
195 scm_remember_upto_here_2 (first, second);
197 return result.smobbed_copy ();
200 LY_DEFINE (ly_stencil_add, "ly:stencil-add",
202 "Combine stencils. Takes any number of arguments.")
204 #define FUNC_NAME __FUNCTION__
205 SCM_VALIDATE_REST_ARGUMENT (args);
208 SCM cs = ly_symbol2scm ("combine-stencil");
213 while (!SCM_NULLP (args))
215 Stencil *s = unsmob<Stencil> (scm_car (args));
217 SCM_ASSERT_TYPE (s, scm_car (args), SCM_ARGn, __FUNCTION__, "Stencil");
219 extent.unite (s->extent_box ());
220 if (scm_is_pair (s->expr ()) && scm_is_eq (cs, s->expr ()))
222 expr = scm_reverse_x (scm_list_copy (scm_cdr (s->expr ())),
226 expr = scm_cons (s->expr (), expr);
228 args = scm_cdr (args);
231 expr = scm_cons (cs, scm_reverse_x (expr, SCM_EOL));
232 return Stencil (extent, expr).smobbed_copy ();
235 LY_DEFINE (ly_make_stencil, "ly:make-stencil",
236 1, 2, 0, (SCM expr, SCM xext, SCM yext),
237 "Stencils are device independent output expressions."
238 " They carry two pieces of information:\n"
242 "A specification of how to print this object."
243 " This specification is processed by the output backends,"
244 " for example @file{scm/output-ps.scm}.\n"
247 "The vertical and horizontal extents of the object, given as"
248 " pairs. If an extent is unspecified (or if you use"
249 " @code{empty-interval} as its value), it is taken to be empty.\n"
252 SCM_ASSERT_TYPE (!scm_is_pair (expr)
253 || is_stencil_head (scm_car (expr)),
254 expr, SCM_ARG1, __FUNCTION__, "registered stencil expression");
257 if (!SCM_UNBNDP (xext))
259 LY_ASSERT_TYPE (is_number_pair, xext, 2);
260 x = ly_scm2interval (xext);
264 if (!SCM_UNBNDP (yext))
266 LY_ASSERT_TYPE (is_number_pair, yext, 3);
267 y = ly_scm2interval (yext);
272 return s.smobbed_copy ();
275 LY_DEFINE (ly_stencil_aligned_to, "ly:stencil-aligned-to",
276 3, 0, 0, (SCM stil, SCM axis, SCM dir),
277 "Align @var{stil} using its own extents. @var{dir} is a number."
278 " @w{@code{-1}} and @code{1} are left and right, respectively."
279 " Other values are interpolated (so @code{0} means the center).")
281 LY_ASSERT_SMOB (Stencil, stil, 1);
282 LY_ASSERT_TYPE (is_axis, axis, 2);
283 LY_ASSERT_TYPE (scm_is_number, dir, 3);
285 Stencil target = *unsmob<Stencil> (stil);
287 target.align_to ((Axis)scm_to_int (axis),
288 scm_to_double (dir));
289 return target.smobbed_copy ();
292 LY_DEFINE (ly_stencil_fonts, "ly:stencil-fonts",
294 "Analyze @var{s}, and return a list of fonts used"
297 LY_ASSERT_SMOB (Stencil, s, 1);
298 Stencil *stil = unsmob<Stencil> (s);
299 return find_expression_fonts (stil->expr ());
302 LY_DEFINE (ly_stencil_in_color, "ly:stencil-in-color",
303 4, 0, 0, (SCM stc, SCM r, SCM g, SCM b),
304 "Put @var{stc} in a different color.")
306 LY_ASSERT_SMOB (Stencil, stc, 1);
307 Stencil *stil = unsmob<Stencil> (stc);
308 return Stencil (stil->extent_box (),
309 scm_list_3 (ly_symbol2scm ("color"),
310 scm_list_3 (r, g, b),
311 stil->expr ())).smobbed_copy ();
314 struct Stencil_interpret_arguments
320 SCM stencil_interpret_in_scm (void *p, SCM expr)
322 Stencil_interpret_arguments *ap = (Stencil_interpret_arguments *) p;
323 return scm_call_2 (ap->func, ap->arg1, expr);
326 LY_DEFINE (ly_interpret_stencil_expression, "ly:interpret-stencil-expression",
327 4, 0, 0, (SCM expr, SCM func, SCM arg1, SCM offset),
328 "Parse @var{expr}, feed bits to @var{func} with first arg"
329 " @var{arg1} having offset @var{offset}.")
331 LY_ASSERT_TYPE (ly_is_procedure, func, 2);
333 Stencil_interpret_arguments a;
336 Offset o = ly_scm2offset (offset);
338 interpret_stencil_expression (expr, stencil_interpret_in_scm, (void *) & a, o);
340 return SCM_UNSPECIFIED;
343 LY_DEFINE (ly_bracket, "ly:bracket",
345 (SCM a, SCM iv, SCM t, SCM p),
346 "Make a bracket in direction@tie{}@var{a}. The extent of the"
347 " bracket is given by @var{iv}. The wings protrude by an amount"
348 " of@tie{}@var{p}, which may be negative. The thickness is given"
351 LY_ASSERT_TYPE (is_axis, a, 1);
352 LY_ASSERT_TYPE (is_number_pair, iv, 2);
353 LY_ASSERT_TYPE (scm_is_number, t, 3);
354 LY_ASSERT_TYPE (scm_is_number, p, 4);
356 return Lookup::bracket ((Axis)scm_to_int (a), ly_scm2interval (iv),
359 0.95 * scm_to_double (t)).smobbed_copy ();
362 LY_DEFINE (ly_stencil_rotate, "ly:stencil-rotate",
363 4, 0, 0, (SCM stil, SCM angle, SCM x, SCM y),
364 "Return a stencil @var{stil} rotated @var{angle} degrees around"
365 " the relative offset (@var{x}, @var{y}). E.g., an offset of"
366 " (-1, 1) will rotate the stencil around the left upper corner.")
368 Stencil *s = unsmob<Stencil> (stil);
369 LY_ASSERT_SMOB (Stencil, stil, 1);
370 LY_ASSERT_TYPE (scm_is_number, angle, 2);
371 LY_ASSERT_TYPE (scm_is_number, x, 3);
372 LY_ASSERT_TYPE (scm_is_number, y, 4);
373 Real a = scm_to_double (angle);
374 Real x_off = scm_to_double (x);
375 Real y_off = scm_to_double (y);
377 SCM new_s = s->smobbed_copy ();
378 Stencil *q = unsmob<Stencil> (new_s);
379 q->rotate_degrees (a, Offset (x_off, y_off));
383 LY_DEFINE (ly_stencil_rotate_absolute, "ly:stencil-rotate-absolute",
384 4, 0, 0, (SCM stil, SCM angle, SCM x, SCM y),
385 "Return a stencil @var{stil} rotated @var{angle} degrees around"
386 " point (@var{x}, @var{y}), given in absolute coordinates.")
388 Stencil *s = unsmob<Stencil> (stil);
389 LY_ASSERT_SMOB (Stencil, stil, 1);
390 LY_ASSERT_TYPE (scm_is_number, angle, 2);
391 LY_ASSERT_TYPE (scm_is_number, x, 3);
392 LY_ASSERT_TYPE (scm_is_number, y, 4);
393 Real a = scm_to_double (angle);
394 Real x_off = scm_to_double (x);
395 Real y_off = scm_to_double (y);
397 SCM new_s = s->smobbed_copy ();
398 Stencil *q = unsmob<Stencil> (new_s);
399 q->rotate_degrees_absolute (a, Offset (x_off, y_off));
403 LY_DEFINE (ly_round_filled_box, "ly:round-filled-box",
405 (SCM xext, SCM yext, SCM blot),
406 "Make a @code{Stencil} object that prints a black box of"
407 " dimensions @var{xext}, @var{yext} and roundness @var{blot}.")
409 LY_ASSERT_TYPE (is_number_pair, xext, 1);
410 LY_ASSERT_TYPE (is_number_pair, yext, 2);
411 LY_ASSERT_TYPE (scm_is_number, blot, 3);
413 return Lookup::round_filled_box (Box (ly_scm2interval (xext), ly_scm2interval (yext)),
414 scm_to_double (blot)).smobbed_copy ();
417 LY_DEFINE (ly_round_filled_polygon, "ly:round-filled-polygon",
419 (SCM points, SCM blot),
420 "Make a @code{Stencil} object that prints a black polygon with"
421 " corners at the points defined by @var{points} (list of coordinate"
422 " pairs) and roundness @var{blot}.")
424 SCM_ASSERT_TYPE (scm_ilength (points) > 0, points, SCM_ARG1, __FUNCTION__, "list of coordinate pairs");
425 LY_ASSERT_TYPE (scm_is_number, blot, 2);
427 for (SCM p = points; scm_is_pair (p); p = scm_cdr (p))
429 SCM scm_pt = scm_car (p);
430 if (scm_is_pair (scm_pt))
432 pts.push_back (ly_scm2offset (scm_pt));
436 // TODO: Print out warning
439 return Lookup::round_filled_polygon (pts, scm_to_double (blot)).smobbed_copy ();
442 LY_DEFINE (ly_register_stencil_expression, "ly:register-stencil-expression",
445 "Add @var{symbol} as head of a stencil expression.")
447 LY_ASSERT_TYPE (ly_is_symbol, symbol, 1);
448 register_stencil_head (symbol);
449 return SCM_UNSPECIFIED;
452 LY_DEFINE (ly_all_stencil_expressions, "ly:all-stencil-expressions",
455 "Return all symbols recognized as stencil expressions.")
457 return all_stencil_heads ();
460 LY_DEFINE (ly_stencil_scale, "ly:stencil-scale",
461 3, 0, 0, (SCM stil, SCM x, SCM y),
462 "Scale stencil @var{stil} using the horizontal and vertical"
463 " scaling factors @var{x} and @var{y}. Negative values will"
464 " flip or mirror @var{stil} without changing its origin;"
465 " this may result in collisions unless it is repositioned.")
467 Stencil *s = unsmob<Stencil> (stil);
468 LY_ASSERT_SMOB (Stencil, stil, 1);
469 LY_ASSERT_TYPE (scm_is_number, x, 2);
470 LY_ASSERT_TYPE (scm_is_number, y, 3);
472 SCM new_s = s->smobbed_copy ();
473 Stencil *q = unsmob<Stencil> (new_s);
475 q->scale (scm_to_double (x), scm_to_double (y));