2 This file is part of LilyPond, the GNU music typesetter.
4 Copyright (C) 1998--2015 Jan Nieuwenhuizen <janneke@gnu.org>
5 Han-Wen Nienhuys <hanwen@xs4all.nl>
7 LilyPond is free software: you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
12 LilyPond is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with LilyPond. If not, see <http://www.gnu.org/licenses/>.
21 #include "font-interface.hh"
22 #include "grob-array.hh"
24 #include "output-def.hh"
25 #include "paper-score.hh"
27 #include "unpure-pure-container.hh"
28 #include "warn.hh" // error ()
30 LY_DEFINE (ly_grob_property_data, "ly:grob-property-data",
31 2, 0, 0, (SCM grob, SCM sym),
32 "Return the value for property @var{sym} of @var{grob},"
33 " but do not process callbacks.")
35 Grob *sc = unsmob<Grob> (grob);
37 LY_ASSERT_SMOB (Grob, grob, 1);
38 LY_ASSERT_TYPE (ly_is_symbol, sym, 2);
40 return sc->get_property_data (sym);
43 LY_DEFINE (ly_grob_set_property_x, "ly:grob-set-property!",
44 3, 0, 0, (SCM grob, SCM sym, SCM val),
45 "Set @var{sym} in grob @var{grob} to value @var{val}.")
47 Grob *sc = unsmob<Grob> (grob);
49 LY_ASSERT_SMOB (Grob, grob, 1);
50 LY_ASSERT_TYPE (ly_is_symbol, sym, 2);
52 if (!ly_is_procedure (val)
53 && !type_check_assignment (sym, val, ly_symbol2scm ("backend-type?")))
54 error ("typecheck failed");
56 sc->set_property (sym, val);
57 return SCM_UNSPECIFIED;
60 LY_DEFINE (ly_grob_set_nested_property_x, "ly:grob-set-nested-property!",
61 3, 0, 0, (SCM grob, SCM symlist, SCM val),
62 "Set nested property @var{symlist} in grob @var{grob} to value @var{val}.")
64 Grob *sc = unsmob<Grob> (grob);
66 LY_ASSERT_SMOB (Grob, grob, 1);
68 bool type_ok = scm_is_pair (symlist);
71 for (SCM s = symlist; scm_is_pair (s) && type_ok; s = scm_cdr (s))
72 type_ok &= ly_is_symbol (scm_car (s));
74 SCM_ASSERT_TYPE (type_ok, symlist, SCM_ARG2, __FUNCTION__, "list of symbols");
76 if (scm_is_pair (scm_cdr (symlist)))
77 set_nested_property (sc, symlist, val);
79 ly_grob_set_property_x (grob, scm_car (symlist), val);
80 return SCM_UNSPECIFIED;
83 LY_DEFINE (ly_grob_pure_property, "ly:grob-pure-property",
84 4, 1, 0, (SCM grob, SCM sym, SCM beg, SCM end, SCM val),
85 "Return the pure value for property @var{sym} of @var{grob}."
86 " If no value is found, return @var{val} or @code{'()}"
87 " if @var{val} is not specified.")
89 Grob *sc = unsmob<Grob> (grob);
91 LY_ASSERT_SMOB (Grob, grob, 1);
92 LY_ASSERT_TYPE (ly_is_symbol, sym, 2);
93 LY_ASSERT_TYPE (scm_is_integer, beg, 3);
94 LY_ASSERT_TYPE (scm_is_integer, end, 4);
98 SCM retval = sc->internal_get_pure_property (sym, scm_to_int (beg), scm_to_int (end));
99 if (scm_is_null (retval))
105 LY_DEFINE (ly_grob_pure_height, "ly:grob-pure-height",
106 4, 1, 0, (SCM grob, SCM refp, SCM beg, SCM end, SCM val),
107 "Return the pure height of @var{grob} given refpoint @var{refp}."
108 " If no value is found, return @var{val} or @code{'()}"
109 " if @var{val} is not specified.")
111 Grob *sc = unsmob<Grob> (grob);
112 Grob *ref = unsmob<Grob> (refp);
114 LY_ASSERT_SMOB (Grob, grob, 1);
115 LY_ASSERT_SMOB (Grob, refp, 2);
116 LY_ASSERT_TYPE (scm_is_integer, beg, 3);
117 LY_ASSERT_TYPE (scm_is_integer, end, 4);
118 if (SCM_UNBNDP (val))
121 Interval retval = sc->pure_y_extent (ref, scm_to_int (beg), scm_to_int (end));
123 return ly_interval2scm (retval);
126 LY_DEFINE (ly_grob_property, "ly:grob-property",
127 2, 1, 0, (SCM grob, SCM sym, SCM val),
128 "Return the value for property @var{sym} of @var{grob}."
129 " If no value is found, return @var{val} or @code{'()}"
130 " if @var{val} is not specified.")
132 Grob *sc = unsmob<Grob> (grob);
134 LY_ASSERT_SMOB (Grob, grob, 1);
135 LY_ASSERT_TYPE (ly_is_symbol, sym, 2);
136 if (SCM_UNBNDP (val))
139 SCM retval = sc->get_property (sym);
140 if (scm_is_null (retval))
146 LY_DEFINE (ly_grob_interfaces, "ly:grob-interfaces",
148 "Return the interfaces list of grob @var{grob}.")
150 Grob *sc = unsmob<Grob> (grob);
152 LY_ASSERT_SMOB (Grob, grob, 1);
154 return sc->interfaces ();
157 LY_DEFINE (ly_grob_object, "ly:grob-object",
158 2, 0, 0, (SCM grob, SCM sym),
159 "Return the value of a pointer in grob @var{grob} of property"
160 " @var{sym}. It returns @code{'()} (end-of-list) if @var{sym}"
161 " is undefined in @var{grob}.")
163 Grob *sc = unsmob<Grob> (grob);
165 LY_ASSERT_SMOB (Grob, grob, 1);
166 LY_ASSERT_TYPE (ly_is_symbol, sym, 2);
168 return sc->internal_get_object (sym);
171 LY_DEFINE (ly_grob_set_object_x, "ly:grob-set-object!",
172 3, 0, 0, (SCM grob, SCM sym, SCM val),
173 "Set @var{sym} in grob @var{grob} to value @var{val}.")
175 Grob *sc = unsmob<Grob> (grob);
177 LY_ASSERT_SMOB (Grob, grob, 1);
178 LY_ASSERT_TYPE (ly_is_symbol, sym, 2);
180 sc->set_object (sym, val);
181 return SCM_UNSPECIFIED;
184 /* TODO: make difference between scaled and unscalead variable in
185 calling (i.e different funcs.) */
186 LY_DEFINE (ly_grob_layout, "ly:grob-layout",
188 "Get @code{\\layout} definition from grob @var{grob}.")
190 Grob *sc = unsmob<Grob> (grob);
192 LY_ASSERT_SMOB (Grob, grob, 1);
194 return sc->layout ()->self_scm ();
197 LY_DEFINE (ly_grob_alist_chain, "ly:grob-alist-chain",
198 1, 1, 0, (SCM grob, SCM global),
199 "Get an alist chain for grob @var{grob}, with @var{global} as"
200 " the global default. If unspecified, @code{font-defaults}"
201 " from the layout block is taken.")
203 Grob *sc = unsmob<Grob> (grob);
205 LY_ASSERT_SMOB (Grob, grob, 1);
207 if (SCM_UNBNDP (global))
209 global = sc->layout ()->lookup_variable (ly_symbol2scm ("font-defaults"));
210 if (SCM_UNBNDP (global))
214 return sc->get_property_alist_chain (global);
217 LY_DEFINE (ly_grob_extent, "ly:grob-extent",
218 3, 0, 0, (SCM grob, SCM refp, SCM axis),
219 "Get the extent in @var{axis} direction of @var{grob} relative to"
220 " the grob @var{refp}.")
222 Grob *sc = unsmob<Grob> (grob);
223 Grob *ref = unsmob<Grob> (refp);
225 LY_ASSERT_SMOB (Grob, grob, 1);
226 LY_ASSERT_SMOB (Grob, refp, 2);
227 LY_ASSERT_TYPE (is_axis, axis, 3);
229 Axis a = Axis (scm_to_int (axis));
231 if (ref->common_refpoint (sc, a) != ref)
233 // ugh. should use other error message
234 SCM_ASSERT_TYPE (false, refp, SCM_ARG2, __FUNCTION__, "common refpoint");
236 return ly_interval2scm (sc->extent (ref, a));
239 LY_DEFINE (ly_grob_robust_relative_extent, "ly:grob-robust-relative-extent",
240 3, 0, 0, (SCM grob, SCM refp, SCM axis),
241 "Get the extent in @var{axis} direction of @var{grob} relative to"
242 " the grob @var{refp}, or @code{(0,0)} if empty.")
244 Grob *sc = unsmob<Grob> (grob);
245 Grob *ref = unsmob<Grob> (refp);
247 LY_ASSERT_SMOB (Grob, grob, 1);
248 LY_ASSERT_SMOB (Grob, refp, 2);
249 LY_ASSERT_TYPE (is_axis, axis, 3);
251 Axis a = Axis (scm_to_int (axis));
253 if (ref->common_refpoint (sc, a) != ref)
255 // ugh. should use other error message
256 SCM_ASSERT_TYPE (false, refp, SCM_ARG2, __FUNCTION__, "common refpoint");
259 return ly_interval2scm (robust_relative_extent (sc, ref, a));
262 LY_DEFINE (ly_grob_relative_coordinate, "ly:grob-relative-coordinate",
263 3, 0, 0, (SCM grob, SCM refp, SCM axis),
264 "Get the coordinate in @var{axis} direction of @var{grob} relative"
265 " to the grob @var{refp}.")
267 Grob *sc = unsmob<Grob> (grob);
268 Grob *ref = unsmob<Grob> (refp);
270 LY_ASSERT_SMOB (Grob, grob, 1);
271 LY_ASSERT_SMOB (Grob, refp, 2);
272 LY_ASSERT_TYPE (is_axis, axis, 3);
274 Axis a = Axis (scm_to_int (axis));
276 if (ref->common_refpoint (sc, a) != ref)
278 // ugh. should use other error message
279 SCM_ASSERT_TYPE (false, refp, SCM_ARG2, __FUNCTION__, "common refpoint");
282 return scm_from_double (sc->relative_coordinate (ref, a));
285 LY_DEFINE (ly_grob_parent, "ly:grob-parent",
286 2, 0, 0, (SCM grob, SCM axis),
287 "Get the parent of @var{grob}. @var{axis} is 0 for the X-axis,"
288 " 1@tie{}for the Y-axis.")
290 Grob *sc = unsmob<Grob> (grob);
292 LY_ASSERT_SMOB (Grob, grob, 1);
293 LY_ASSERT_TYPE (is_axis, axis, 2);
295 Grob *par = sc->get_parent (Axis (scm_to_int (axis)));
296 return par ? par->self_scm () : SCM_EOL;
299 LY_DEFINE (ly_grob_set_parent_x, "ly:grob-set-parent!",
300 3, 0, 0, (SCM grob, SCM axis, SCM parent_grob),
301 "Set @var{parent-grob} the parent of grob @var{grob} in axis @var{axis}.")
303 Grob *gr = unsmob<Grob> (grob);
304 Grob *parent = unsmob<Grob> (parent_grob);
306 LY_ASSERT_SMOB (Grob, grob, 1);
307 LY_ASSERT_TYPE (is_axis, axis, 2);
308 LY_ASSERT_SMOB (Grob, parent_grob, 3);
310 Axis a = Axis (scm_to_int (axis));
311 gr->set_parent (parent, a);
312 return SCM_UNSPECIFIED;
315 LY_DEFINE (ly_grob_properties, "ly:grob-properties",
317 "Get the mutable properties of @var{grob}.")
319 Grob *g = unsmob<Grob> (grob);
321 LY_ASSERT_SMOB (Grob, grob, 1);
323 /* FIXME: uhg? copy/read only? */
324 return g->mutable_property_alist_;
327 LY_DEFINE (ly_grob_basic_properties, "ly:grob-basic-properties",
329 "Get the immutable properties of @var{grob}.")
331 Grob *g = unsmob<Grob> (grob);
333 LY_ASSERT_SMOB (Grob, grob, 1);
335 /* FIXME: uhg? copy/read only? */
336 return g->immutable_property_alist_;
339 LY_DEFINE (ly_grob_system, "ly:grob-system",
341 "Return the system grob of @var{grob}.")
343 Grob *me = unsmob<Grob> (grob);
345 LY_ASSERT_SMOB (Grob, grob, 1);
347 if (System *g = me->get_system ())
348 return g->self_scm ();
353 LY_DEFINE (ly_grob_original, "ly:grob-original",
355 "Return the unbroken original grob of @var{grob}.")
357 Grob *me = unsmob<Grob> (grob);
359 LY_ASSERT_SMOB (Grob, grob, 1);
360 return me->original () ? me->original ()->self_scm () : me->self_scm ();
363 LY_DEFINE (ly_grob_suicide_x, "ly:grob-suicide!",
367 Grob *me = unsmob<Grob> (grob);
369 LY_ASSERT_SMOB (Grob, grob, 1);
372 return SCM_UNSPECIFIED;
375 LY_DEFINE (ly_grob_translate_axis_x, "ly:grob-translate-axis!",
376 3, 0, 0, (SCM grob, SCM d, SCM a),
377 "Translate @var{grob} on axis@tie{}@var{a} over"
378 " distance@tie{}@var{d}.")
380 Grob *me = unsmob<Grob> (grob);
382 LY_ASSERT_SMOB (Grob, grob, 1);
383 LY_ASSERT_TYPE (scm_is_number, d, 2);
384 LY_ASSERT_TYPE (is_axis, a, 3);
386 me->translate_axis (scm_to_double (d), Axis (scm_to_int (a)));
387 return SCM_UNSPECIFIED;
390 LY_DEFINE (ly_grob_default_font, "ly:grob-default-font",
392 "Return the default font for grob @var{grob}.")
394 Grob *gr = unsmob<Grob> (grob);
396 LY_ASSERT_SMOB (Grob, grob, 1);
398 return Font_interface::get_default_font (gr)->self_scm ();
402 TODO: consider swapping order, so we can do
404 (grob-common-refpoint a b c d e)
406 LY_DEFINE (ly_grob_common_refpoint, "ly:grob-common-refpoint",
407 3, 0, 0, (SCM grob, SCM other, SCM axis),
408 "Find the common refpoint of @var{grob} and @var{other}"
412 Grob *gr = unsmob<Grob> (grob);
414 LY_ASSERT_SMOB (Grob, grob, 1);
415 LY_ASSERT_SMOB (Grob, other, 2);
417 Grob *o = unsmob<Grob> (other);
419 LY_ASSERT_TYPE (is_axis, axis, 3);
421 Grob *refp = gr->common_refpoint (o, Axis (scm_to_int (axis)));
422 return refp ? refp->self_scm () : SCM_BOOL_F;
425 LY_DEFINE (ly_grob_common_refpoint_of_array, "ly:grob-common-refpoint-of-array",
426 3, 0, 0, (SCM grob, SCM others, SCM axis),
427 "Find the common refpoint of @var{grob} and @var{others}"
428 " (a grob-array) for @var{axis}.")
430 Grob *gr = unsmob<Grob> (grob);
432 LY_ASSERT_SMOB (Grob, grob, 1);
433 LY_ASSERT_SMOB (Grob_array, others, 2);
435 Grob_array *ga = unsmob<Grob_array> (others);
436 LY_ASSERT_TYPE (is_axis, axis, 3);
438 Grob *refp = common_refpoint_of_array (ga->array (), gr, Axis (scm_to_int (axis)));
439 return refp ? refp->self_scm () : SCM_BOOL_F;
442 LY_DEFINE (ly_grob_chain_callback, "ly:grob-chain-callback",
443 3, 0, 0, (SCM grob, SCM proc, SCM sym),
444 "Find the callback that is stored as property"
445 " @var{sym} of grob @var{grob} and chain @var{proc}"
446 " to the head of this, meaning that it is called"
447 " using @var{grob} and the previous callback's result.")
449 Grob *gr = unsmob<Grob> (grob);
451 LY_ASSERT_SMOB (Grob, grob, 1);
452 SCM_ASSERT_TYPE (ly_is_procedure (proc) || unsmob<Unpure_pure_container> (proc), proc, SCM_ARG2, __FUNCTION__, "procedure or unpure pure container");
453 LY_ASSERT_TYPE (ly_is_symbol, sym, 3);
455 chain_callback (gr, proc, sym);
456 return SCM_UNSPECIFIED;
459 LY_DEFINE (ly_grob_vertical_less_p, "ly:grob-vertical<?",
460 2, 0, 0, (SCM a, SCM b),
461 "Does @var{a} lie above @var{b} on the page?")
463 LY_ASSERT_SMOB (Grob, a, 1);
464 LY_ASSERT_SMOB (Grob, b, 2);
466 Grob *ga = unsmob<Grob> (a);
467 Grob *gb = unsmob<Grob> (b);
469 return ly_bool2scm (Grob::vertical_less (ga, gb));
472 LY_DEFINE (ly_grob_get_vertical_axis_group_index, "ly:grob-get-vertical-axis-group-index",
474 "Get the index of the vertical axis group the grob @var{grob} belongs to;"
475 " return @code{-1} if none is found.")
477 Grob *gr = unsmob<Grob> (grob);
479 LY_ASSERT_SMOB (Grob, grob, 1);
481 return scm_from_int (Grob::get_vertical_axis_group_index (gr));
484 LY_DEFINE (ly_grob_spanned_rank_interval, "ly:grob-spanned-rank-interval",
486 "Returns a pair with the @code{rank} of the furthest left"
487 " column and the @code{rank} of the furthest right column"
488 " spanned by @code{grob}.")
490 Grob *gr = unsmob<Grob> (grob);
492 LY_ASSERT_SMOB (Grob, grob, 1);
494 Interval_t<int> iv = gr->spanned_rank_interval ();
496 return scm_cons (scm_from_int(iv[LEFT]), scm_from_int(iv[RIGHT]));