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"
27 * A few general helpers in degrees
30 LY_DEFINE (ly_angle, "ly:angle",
31 1, 1, 0, (SCM x, SCM y),
32 "Calculates angle in degrees of given vector. With one argument,"
33 " @var{x} is a number pair indicating the vector. With two"
34 " arguments, @var{x} and @var{y} specify the respective coordinates.")
38 LY_ASSERT_TYPE (is_number_pair, x, 1);
39 off = ly_scm2offset (x);
41 LY_ASSERT_TYPE (scm_is_number, x, 1);
42 LY_ASSERT_TYPE (scm_is_number, y, 2);
43 off = Offset (scm_to_double (x), scm_to_double (y));
45 return scm_from_double (off.angle_degrees ());
48 LY_DEFINE (ly_length, "ly:length",
49 1, 1, 0, (SCM x, SCM y),
50 "Calculates magnitude of given vector. With one argument,"
51 " @var{x} is a number pair indicating the vector. With two"
52 " arguments, @var{x} and @var{y} specify the respective coordinates.")
56 LY_ASSERT_TYPE (is_number_pair, x, 1);
57 off = ly_scm2offset (x);
59 LY_ASSERT_TYPE (scm_is_number, x, 1);
60 LY_ASSERT_TYPE (scm_is_number, y, 2);
61 off = Offset (scm_to_double (x), scm_to_double (y));
63 return scm_from_double (off.length ());
66 LY_DEFINE (ly_directed, "ly:directed",
67 1, 1, 0, (SCM direction, SCM magnitude),
68 "Calculates an @code{(x . y)} pair with optional @var{magnitude}"
69 " (defaulting to @code{1.0}) and @var{direction} specified either"
70 " as an angle in degrees or a coordinate pair giving the direction. "
71 " If @var{magnitude} is a pair, the respective coordinates are"
72 " scaled independently, useful for ellipse drawings.")
75 if (scm_is_pair (direction))
77 LY_ASSERT_TYPE (is_number_pair, direction, 1);
78 res = ly_scm2offset (direction).direction ();
82 LY_ASSERT_TYPE (scm_is_number, direction, 1);
83 res = offset_directed (scm_to_double (direction));
85 if (SCM_UNBNDP (magnitude))
86 return ly_offset2scm (res);
87 if (scm_is_pair (magnitude))
89 LY_ASSERT_TYPE (is_number_pair, magnitude, 2);
90 return ly_offset2scm (res.scale (ly_scm2offset (magnitude)));
92 LY_ASSERT_TYPE (scm_is_number, magnitude, 2);
93 return ly_offset2scm (scm_to_double (magnitude) * res);
98 TODO: naming add/combine.
101 LY_DEFINE (ly_stencil_translate_axis, "ly:stencil-translate-axis",
102 3, 0, 0, (SCM stil, SCM amount, SCM axis),
103 "Return a copy of @var{stil} but translated by @var{amount}"
104 " in @var{axis} direction.")
106 Stencil *s = unsmob<Stencil> (stil);
107 LY_ASSERT_SMOB (Stencil, stil, 1);
108 LY_ASSERT_TYPE (scm_is_number, amount, 2);
110 LY_ASSERT_TYPE (is_axis, axis, 3);
112 Real real_amount = scm_to_double (amount);
114 SCM new_s = s->smobbed_copy ();
115 scm_remember_upto_here_1 (stil);
117 Stencil *q = unsmob<Stencil> (new_s);
118 q->translate_axis (real_amount, Axis (scm_to_int (axis)));
122 LY_DEFINE (ly_stencil_translate, "ly:stencil-translate",
123 2, 0, 0, (SCM stil, SCM offset),
124 "Return a @var{stil}, but translated by @var{offset}"
125 " (a pair of numbers).")
127 Stencil *s = unsmob<Stencil> (stil);
128 LY_ASSERT_SMOB (Stencil, stil, 1);
129 LY_ASSERT_TYPE (is_number_pair, offset, 2);
130 Offset o = ly_scm2offset (offset);
132 SCM new_s = s->smobbed_copy ();
133 scm_remember_upto_here_1 (stil);
135 Stencil *q = unsmob<Stencil> (new_s);
140 LY_DEFINE (ly_stencil_expr, "ly:stencil-expr",
142 "Return the expression of @var{stil}.")
144 Stencil *s = unsmob<Stencil> (stil);
145 LY_ASSERT_SMOB (Stencil, stil, 1);
149 LY_DEFINE (ly_stencil_extent, "ly:stencil-extent",
150 2, 0, 0, (SCM stil, SCM axis),
151 "Return a pair of numbers signifying the extent of @var{stil} in"
152 " @var{axis} direction (@code{0} or @code{1} for x and"
153 " y@tie{}axis, respectively).")
155 Stencil *s = unsmob<Stencil> (stil);
156 LY_ASSERT_SMOB (Stencil, stil, 1);
157 LY_ASSERT_TYPE (is_axis, axis, 2);
159 return ly_interval2scm (s->extent (Axis (scm_to_int (axis))));
162 LY_DEFINE (ly_stencil_empty_p, "ly:stencil-empty?",
163 1, 1, 0, (SCM stil, SCM axis),
164 "Return whether @var{stil} is empty. If an optional"
165 " @var{axis} is supplied, the emptiness check is"
166 " restricted to that axis.")
168 Stencil *s = unsmob<Stencil> (stil);
169 LY_ASSERT_SMOB (Stencil, stil, 1);
170 if (SCM_UNBNDP (axis))
171 return scm_from_bool (s->is_empty ());
172 LY_ASSERT_TYPE (is_axis, axis, 2);
173 return scm_from_bool (s->is_empty (Axis (scm_to_int (axis))));
176 LY_DEFINE (ly_stencil_combine_at_edge, "ly:stencil-combine-at-edge",
177 4, 1, 0, (SCM first, SCM axis, SCM direction,
180 "Construct a stencil by putting @var{second} next to @var{first}."
181 " @var{axis} can be 0 (x-axis) or@tie{}1 (y-axis)."
182 " @var{direction} can be -1 (left or down) or@tie{}1 (right or"
183 " up). The stencils are juxtaposed with @var{padding} as extra"
184 " space. @var{first} and @var{second} may also be @code{'()} or"
187 Stencil *s1 = unsmob<Stencil> (first);
188 Stencil *s2 = unsmob<Stencil> (second);
191 SCM_ASSERT_TYPE (s1 || scm_is_false (first) || scm_is_null (first),
192 first, SCM_ARG1, __FUNCTION__, "Stencil, #f or ()");
193 SCM_ASSERT_TYPE (s2 || scm_is_false (second) || scm_is_null (second),
194 second, SCM_ARG4, __FUNCTION__, "Stencil, #f or ()");
195 LY_ASSERT_TYPE (is_axis, axis, 2);
196 LY_ASSERT_TYPE (is_direction, direction, 3);
199 if (!SCM_UNBNDP (padding))
201 LY_ASSERT_TYPE (scm_is_number, padding, 5);
202 p = scm_to_double (padding);
209 result.add_at_edge (Axis (scm_to_int (axis)),
210 Direction (scm_to_int (direction)), *s2, p);
212 scm_remember_upto_here_2 (first, second);
214 return result.smobbed_copy ();
217 LY_DEFINE (ly_stencil_stack, "ly:stencil-stack",
218 4, 2, 0, (SCM first, SCM axis, SCM direction,
222 "Construct a stencil by stacking @var{second} next to @var{first}."
223 " @var{axis} can be 0 (x-axis) or@tie{}1 (y-axis)."
224 " @var{direction} can be -1 (left or down) or@tie{}1 (right or"
225 " up). The stencils are juxtaposed with @var{padding} as extra"
226 " space. @var{first} and @var{second} may also be @code{'()} or"
227 " @code{#f}. As opposed to @code{ly:stencil-combine-at-edge},"
228 " metrics are suited for successively accumulating lines of"
229 " stencils. Also, @var{second} stencil is drawn last.\n\n"
230 "If @var{mindist} is specified, reference points are placed"
231 " apart at least by this distance. If either of the stencils"
232 " is spacing, @var{padding} and @var{mindist} do not apply.")
234 Stencil *s1 = unsmob<Stencil> (first);
235 Stencil *s2 = unsmob<Stencil> (second);
238 SCM_ASSERT_TYPE (s1 || scm_is_false (first) || scm_is_null (first),
239 first, SCM_ARG1, __FUNCTION__, "Stencil, #f or ()");
240 SCM_ASSERT_TYPE (s2 || scm_is_false (second) || scm_is_null (second),
241 second, SCM_ARG4, __FUNCTION__, "Stencil, #f or ()");
242 LY_ASSERT_TYPE (is_axis, axis, 2);
243 LY_ASSERT_TYPE (is_direction, direction, 3);
246 if (!SCM_UNBNDP (padding))
248 LY_ASSERT_TYPE (scm_is_number, padding, 5);
249 p = scm_to_double (padding);
251 Real d = -infinity_f;
252 if (!SCM_UNBNDP (mindist))
254 LY_ASSERT_TYPE (scm_is_number, mindist, 6);
255 d = scm_to_double (mindist);
262 result.stack (Axis (scm_to_int (axis)),
263 Direction (scm_to_int (direction)), *s2, p, d);
265 scm_remember_upto_here_2 (first, second);
267 return result.smobbed_copy ();
270 LY_DEFINE (ly_stencil_add, "ly:stencil-add",
272 "Combine stencils. Takes any number of arguments.")
274 #define FUNC_NAME __FUNCTION__
275 SCM_VALIDATE_REST_ARGUMENT (args);
278 SCM cs = ly_symbol2scm ("combine-stencil");
283 while (!SCM_NULLP (args))
285 Stencil *s = unsmob<Stencil> (scm_car (args));
287 SCM_ASSERT_TYPE (s, scm_car (args), SCM_ARGn, __FUNCTION__, "Stencil");
289 extent.unite (s->extent_box ());
290 if (scm_is_pair (s->expr ()) && scm_is_eq (cs, s->expr ()))
292 expr = scm_reverse_x (scm_list_copy (scm_cdr (s->expr ())),
296 expr = scm_cons (s->expr (), expr);
298 args = scm_cdr (args);
301 expr = scm_cons (cs, scm_reverse_x (expr, SCM_EOL));
302 return Stencil (extent, expr).smobbed_copy ();
305 LY_DEFINE (ly_make_stencil, "ly:make-stencil",
306 1, 2, 0, (SCM expr, SCM xext, SCM yext),
307 "Stencils are device independent output expressions."
308 " They carry two pieces of information:\n"
312 "A specification of how to print this object."
313 " This specification is processed by the output backends,"
314 " for example @file{scm/output-ps.scm}.\n"
317 "The vertical and horizontal extents of the object, given as"
318 " pairs. If an extent is unspecified (or if you use"
319 " @code{empty-interval} as its value), it is taken to be empty.\n"
322 SCM_ASSERT_TYPE (!scm_is_pair (expr)
323 || is_stencil_head (scm_car (expr)),
324 expr, SCM_ARG1, __FUNCTION__, "registered stencil expression");
327 if (!SCM_UNBNDP (xext))
329 LY_ASSERT_TYPE (is_number_pair, xext, 2);
330 x = ly_scm2interval (xext);
334 if (!SCM_UNBNDP (yext))
336 LY_ASSERT_TYPE (is_number_pair, yext, 3);
337 y = ly_scm2interval (yext);
342 return s.smobbed_copy ();
345 LY_DEFINE (ly_stencil_aligned_to, "ly:stencil-aligned-to",
346 3, 0, 0, (SCM stil, SCM axis, SCM dir),
347 "Align @var{stil} using its own extents. @var{dir} is a number."
348 " @w{@code{-1}} and @code{1} are left and right, respectively."
349 " Other values are interpolated (so @code{0} means the center).")
351 LY_ASSERT_SMOB (Stencil, stil, 1);
352 LY_ASSERT_TYPE (is_axis, axis, 2);
353 LY_ASSERT_TYPE (scm_is_number, dir, 3);
355 Stencil target = *unsmob<Stencil> (stil);
357 target.align_to ((Axis)scm_to_int (axis),
358 scm_to_double (dir));
359 return target.smobbed_copy ();
362 LY_DEFINE (ly_stencil_fonts, "ly:stencil-fonts",
364 "Analyze @var{s}, and return a list of fonts used"
367 LY_ASSERT_SMOB (Stencil, s, 1);
368 Stencil *stil = unsmob<Stencil> (s);
369 return find_expression_fonts (stil->expr ());
372 LY_DEFINE (ly_stencil_in_color, "ly:stencil-in-color",
373 4, 0, 0, (SCM stc, SCM r, SCM g, SCM b),
374 "Put @var{stc} in a different color.")
376 LY_ASSERT_SMOB (Stencil, stc, 1);
377 Stencil *stil = unsmob<Stencil> (stc);
378 return Stencil (stil->extent_box (),
379 scm_list_3 (ly_symbol2scm ("color"),
380 scm_list_3 (r, g, b),
381 stil->expr ())).smobbed_copy ();
384 struct Stencil_interpret_arguments
390 SCM stencil_interpret_in_scm (void *p, SCM expr)
392 Stencil_interpret_arguments *ap = (Stencil_interpret_arguments *) p;
393 return scm_call_2 (ap->func, ap->arg1, expr);
396 LY_DEFINE (ly_interpret_stencil_expression, "ly:interpret-stencil-expression",
397 4, 0, 0, (SCM expr, SCM func, SCM arg1, SCM offset),
398 "Parse @var{expr}, feed bits to @var{func} with first arg"
399 " @var{arg1} having offset @var{offset}.")
401 LY_ASSERT_TYPE (ly_is_procedure, func, 2);
403 Stencil_interpret_arguments a;
406 Offset o = ly_scm2offset (offset);
408 interpret_stencil_expression (expr, stencil_interpret_in_scm, (void *) & a, o);
410 return SCM_UNSPECIFIED;
413 LY_DEFINE (ly_bracket, "ly:bracket",
415 (SCM a, SCM iv, SCM t, SCM p),
416 "Make a bracket in direction@tie{}@var{a}. The extent of the"
417 " bracket is given by @var{iv}. The wings protrude by an amount"
418 " of@tie{}@var{p}, which may be negative. The thickness is given"
421 LY_ASSERT_TYPE (is_axis, a, 1);
422 LY_ASSERT_TYPE (is_number_pair, iv, 2);
423 LY_ASSERT_TYPE (scm_is_number, t, 3);
424 LY_ASSERT_TYPE (scm_is_number, p, 4);
426 return Lookup::bracket ((Axis)scm_to_int (a), ly_scm2interval (iv),
429 0.95 * scm_to_double (t)).smobbed_copy ();
432 LY_DEFINE (ly_stencil_rotate, "ly:stencil-rotate",
433 4, 0, 0, (SCM stil, SCM angle, SCM x, SCM y),
434 "Return a stencil @var{stil} rotated @var{angle} degrees around"
435 " the relative offset (@var{x}, @var{y}). E.g., an offset of"
436 " (-1, 1) will rotate the stencil around the left upper corner.")
438 Stencil *s = unsmob<Stencil> (stil);
439 LY_ASSERT_SMOB (Stencil, stil, 1);
440 LY_ASSERT_TYPE (scm_is_number, angle, 2);
441 LY_ASSERT_TYPE (scm_is_number, x, 3);
442 LY_ASSERT_TYPE (scm_is_number, y, 4);
443 Real a = scm_to_double (angle);
444 Real x_off = scm_to_double (x);
445 Real y_off = scm_to_double (y);
447 SCM new_s = s->smobbed_copy ();
448 Stencil *q = unsmob<Stencil> (new_s);
449 q->rotate_degrees (a, Offset (x_off, y_off));
453 LY_DEFINE (ly_stencil_rotate_absolute, "ly:stencil-rotate-absolute",
454 4, 0, 0, (SCM stil, SCM angle, SCM x, SCM y),
455 "Return a stencil @var{stil} rotated @var{angle} degrees around"
456 " point (@var{x}, @var{y}), given in absolute coordinates.")
458 Stencil *s = unsmob<Stencil> (stil);
459 LY_ASSERT_SMOB (Stencil, stil, 1);
460 LY_ASSERT_TYPE (scm_is_number, angle, 2);
461 LY_ASSERT_TYPE (scm_is_number, x, 3);
462 LY_ASSERT_TYPE (scm_is_number, y, 4);
463 Real a = scm_to_double (angle);
464 Real x_off = scm_to_double (x);
465 Real y_off = scm_to_double (y);
467 SCM new_s = s->smobbed_copy ();
468 Stencil *q = unsmob<Stencil> (new_s);
469 q->rotate_degrees_absolute (a, Offset (x_off, y_off));
473 LY_DEFINE (ly_round_filled_box, "ly:round-filled-box",
475 (SCM xext, SCM yext, SCM blot),
476 "Make a @code{Stencil} object that prints a black box of"
477 " dimensions @var{xext}, @var{yext} and roundness @var{blot}.")
479 LY_ASSERT_TYPE (is_number_pair, xext, 1);
480 LY_ASSERT_TYPE (is_number_pair, yext, 2);
481 LY_ASSERT_TYPE (scm_is_number, blot, 3);
483 return Lookup::round_filled_box (Box (ly_scm2interval (xext), ly_scm2interval (yext)),
484 scm_to_double (blot)).smobbed_copy ();
487 LY_DEFINE (ly_round_filled_polygon, "ly:round-filled-polygon",
489 (SCM points, SCM blot, SCM extroversion),
490 "Make a @code{Stencil} object that prints a black polygon with"
491 " corners at the points defined by @var{points} (list of coordinate"
492 " pairs) and roundness @var{blot}. Optional"
493 "@var{extroversion} shifts the outline outward, with the"
494 "default of@tie{}@code{-1.0} keeping the outer boundary of"
495 "the outline just inside of the polygon.")
497 SCM_ASSERT_TYPE (scm_ilength (points) > 0, points, SCM_ARG1, __FUNCTION__, "list of coordinate pairs");
498 LY_ASSERT_TYPE (scm_is_number, blot, 2);
500 if (!SCM_UNBNDP (extroversion))
502 LY_ASSERT_TYPE (scm_is_number, extroversion, 3);
503 ext = scm_to_double (extroversion);
506 for (SCM p = points; scm_is_pair (p); p = scm_cdr (p))
508 SCM scm_pt = scm_car (p);
509 if (scm_is_pair (scm_pt))
511 pts.push_back (ly_scm2offset (scm_pt));
515 // TODO: Print out warning
518 return Lookup::round_filled_polygon (pts, scm_to_double (blot), ext)
522 LY_DEFINE (ly_register_stencil_expression, "ly:register-stencil-expression",
525 "Add @var{symbol} as head of a stencil expression.")
527 LY_ASSERT_TYPE (ly_is_symbol, symbol, 1);
528 register_stencil_head (symbol);
529 return SCM_UNSPECIFIED;
532 LY_DEFINE (ly_all_stencil_expressions, "ly:all-stencil-expressions",
535 "Return all symbols recognized as stencil expressions.")
537 return all_stencil_heads ();
540 LY_DEFINE (ly_stencil_scale, "ly:stencil-scale",
541 3, 0, 0, (SCM stil, SCM x, SCM y),
542 "Scale stencil @var{stil} using the horizontal and vertical"
543 " scaling factors @var{x} and @var{y}. Negative values will"
544 " flip or mirror @var{stil} without changing its origin;"
545 " this may result in collisions unless it is repositioned.")
547 Stencil *s = unsmob<Stencil> (stil);
548 LY_ASSERT_SMOB (Stencil, stil, 1);
549 LY_ASSERT_TYPE (scm_is_number, x, 2);
550 LY_ASSERT_TYPE (scm_is_number, y, 3);
552 SCM new_s = s->smobbed_copy ();
553 Stencil *q = unsmob<Stencil> (new_s);
555 q->scale (scm_to_double (x), scm_to_double (y));
559 LY_DEFINE (ly_stencil_outline, "ly:stencil-outline",
560 2, 0, 0, (SCM stil, SCM outline),
561 "Return a stencil with the stencil expression (inking)"
562 " of stencil @var{stil} but with outline and dimensions"
563 " from stencil @var{outline}.")
565 Stencil s = *LY_ASSERT_SMOB (Stencil, stil, 1);
566 Stencil o = *LY_ASSERT_SMOB (Stencil, outline, 2);
567 return s.with_outline (o).smobbed_copy ();