2 This file is part of LilyPond, the GNU music typesetter.
4 Copyright (C) 1998--2011 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"
26 #include "simple-closure.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 && !is_simple_closure (val)
54 && !type_check_assignment (sym, val, ly_symbol2scm ("backend-type?")))
55 error ("typecheck failed");
57 sc->set_property (sym, val);
58 return SCM_UNSPECIFIED;
61 LY_DEFINE (ly_grob_set_nested_property_x, "ly:grob-set-nested-property!",
62 3, 0, 0, (SCM grob, SCM symlist, SCM val),
63 "Set nested property @var{symlist} in grob @var{grob} to value @var{val}.")
65 Grob *sc = unsmob_grob (grob);
67 LY_ASSERT_SMOB (Grob, grob, 1);
69 bool type_ok = ly_cheap_is_list (symlist);
72 for (SCM s = symlist; scm_is_pair (s) && type_ok; s = scm_cdr (s))
73 type_ok &= ly_is_symbol (scm_car (s));
75 SCM_ASSERT_TYPE (type_ok, symlist, SCM_ARG2, __FUNCTION__, "list of symbols");
77 set_nested_property (sc, symlist, val);
78 return SCM_UNSPECIFIED;
82 LY_DEFINE (ly_grob_property, "ly:grob-property",
83 2, 1, 0, (SCM grob, SCM sym, SCM val),
84 "Return the value for property @var{sym} of @var{grob}."
85 " If no value is found, return @var{val} or @code{'()}"
86 " if @var{val} is not specified.")
88 Grob *sc = unsmob_grob (grob);
90 LY_ASSERT_SMOB (Grob, grob, 1);
91 LY_ASSERT_TYPE (ly_is_symbol, sym, 2);
92 if (val == SCM_UNDEFINED)
95 SCM retval = sc->internal_get_property (sym);
96 if (retval == SCM_EOL)
103 LY_DEFINE (ly_grob_interfaces, "ly:grob-interfaces",
105 "Return the interfaces list of grob @var{grob}.")
107 Grob *sc = unsmob_grob (grob);
109 LY_ASSERT_SMOB (Grob, grob, 1);
111 return sc->interfaces ();
114 LY_DEFINE (ly_grob_object, "ly:grob-object",
115 2, 0, 0, (SCM grob, SCM sym),
116 "Return the value of a pointer in grob @var{grob} of property"
117 " @var{sym}. It returns @code{'()} (end-of-list) if @var{sym}"
118 " is undefined in @var{grob}.")
120 Grob *sc = unsmob_grob (grob);
122 LY_ASSERT_SMOB (Grob, grob, 1);
123 LY_ASSERT_TYPE (ly_is_symbol, sym, 2);
125 return sc->internal_get_object (sym);
129 LY_DEFINE (ly_grob_set_object_x, "ly:grob-set-object!",
130 3, 0, 0, (SCM grob, SCM sym, SCM val),
131 "Set @var{sym} in grob @var{grob} to value @var{val}.")
133 Grob *sc = unsmob_grob (grob);
135 LY_ASSERT_SMOB (Grob, grob, 1);
136 LY_ASSERT_TYPE (ly_is_symbol, sym, 2);
138 sc->set_object (sym, val);
139 return SCM_UNSPECIFIED;
142 /* TODO: make difference between scaled and unscalead variable in
143 calling (i.e different funcs.) */
144 LY_DEFINE (ly_grob_layout, "ly:grob-layout",
146 "Get @code{\\layout} definition from grob @var{grob}.")
148 Grob *sc = unsmob_grob (grob);
150 LY_ASSERT_SMOB (Grob, grob, 1);
152 return sc->layout ()->self_scm ();
155 LY_DEFINE (ly_grob_alist_chain, "ly:grob-alist-chain",
156 1, 1, 0, (SCM grob, SCM global),
157 "Get an alist chain for grob @var{grob}, with @var{global} as"
158 " the global default. If unspecified, @code{font-defaults}"
159 " from the layout block is taken.")
161 Grob *sc = unsmob_grob (grob);
163 LY_ASSERT_SMOB (Grob, grob, 1);
165 if (global == SCM_UNDEFINED)
167 global = sc->layout ()->lookup_variable (ly_symbol2scm ("font-defaults"));
168 if (global == SCM_UNDEFINED)
172 return sc->get_property_alist_chain (global);
175 LY_DEFINE (ly_grob_extent, "ly:grob-extent",
176 3, 0, 0, (SCM grob, SCM refp, SCM axis),
177 "Get the extent in @var{axis} direction of @var{grob} relative to"
178 " the grob @var{refp}.")
180 Grob *sc = unsmob_grob (grob);
181 Grob *ref = unsmob_grob (refp);
184 LY_ASSERT_SMOB (Grob, grob, 1);
185 LY_ASSERT_SMOB (Grob, refp, 2);
186 LY_ASSERT_TYPE (is_axis, axis, 3);
188 Axis a = Axis (scm_to_int (axis));
191 if (ref->common_refpoint (sc, a) != ref)
193 // ugh. should use other error message
194 SCM_ASSERT_TYPE (false, refp, SCM_ARG2, __FUNCTION__, "common refpoint");
196 return ly_interval2scm (sc->extent (ref, a));
199 LY_DEFINE (ly_grob_robust_relative_extent, "ly:grob-robust-relative-extent",
200 3, 0, 0, (SCM grob, SCM refp, SCM axis),
201 "Get the extent in @var{axis} direction of @var{grob} relative to"
202 " the grob @var{refp}, or @code{(0,0)} if empty.")
204 Grob *sc = unsmob_grob (grob);
205 Grob *ref = unsmob_grob (refp);
208 LY_ASSERT_SMOB (Grob, grob, 1);
209 LY_ASSERT_SMOB (Grob, refp, 2);
210 LY_ASSERT_TYPE (is_axis, axis, 3);
212 Axis a = Axis (scm_to_int (axis));
214 if (ref->common_refpoint (sc, a) != ref)
216 // ugh. should use other error message
217 SCM_ASSERT_TYPE (false, refp, SCM_ARG2, __FUNCTION__, "common refpoint");
220 return ly_interval2scm (robust_relative_extent (sc, ref, a));
223 LY_DEFINE (ly_grob_relative_coordinate, "ly:grob-relative-coordinate",
224 3, 0, 0, (SCM grob, SCM refp, SCM axis),
225 "Get the coordinate in @var{axis} direction of @var{grob} relative"
226 " to the grob @var{refp}.")
228 Grob *sc = unsmob_grob (grob);
229 Grob *ref = unsmob_grob (refp);
232 LY_ASSERT_SMOB (Grob, grob, 1);
233 LY_ASSERT_SMOB (Grob, refp, 2);
234 LY_ASSERT_TYPE (is_axis, axis, 3);
236 Axis a = Axis (scm_to_int (axis));
239 if (ref->common_refpoint (sc, a) != ref)
241 // ugh. should use other error message
242 SCM_ASSERT_TYPE (false, refp, SCM_ARG2, __FUNCTION__, "common refpoint");
245 return scm_from_double (sc->relative_coordinate (ref, a));
249 LY_DEFINE (ly_grob_parent, "ly:grob-parent",
250 2, 0, 0, (SCM grob, SCM axis),
251 "Get the parent of @var{grob}. @var{axis} is 0 for the X-axis,"
252 " 1@tie{}for the Y-axis.")
254 Grob *sc = unsmob_grob (grob);
256 LY_ASSERT_SMOB (Grob, grob, 1);
257 LY_ASSERT_TYPE (is_axis, axis, 2);
259 Grob *par = sc->get_parent (Axis (scm_to_int (axis)));
260 return par ? par->self_scm () : SCM_EOL;
263 LY_DEFINE (ly_grob_set_parent_x, "ly:grob-set-parent!",
264 3, 0, 0, (SCM grob, SCM axis, SCM parent_grob),
265 "Set @var{parent-grob} the parent of grob @var{grob} in axis @var{axis}.")
267 Grob *gr = unsmob_grob (grob);
268 Grob *parent = unsmob_grob (parent_grob);
270 LY_ASSERT_SMOB (Grob, grob, 1);
271 LY_ASSERT_TYPE (is_axis, axis, 2);
272 LY_ASSERT_SMOB (Grob, parent_grob, 3);
274 Axis a = Axis (scm_to_int (axis));
275 gr->set_parent (parent, a);
276 return SCM_UNSPECIFIED;
279 LY_DEFINE (ly_grob_properties, "ly:grob-properties",
281 "Get the mutable properties of @var{grob}.")
283 Grob *g = unsmob_grob (grob);
285 LY_ASSERT_SMOB (Grob, grob, 1);
287 /* FIXME: uhg? copy/read only? */
288 return g->mutable_property_alist_;
291 LY_DEFINE (ly_grob_basic_properties, "ly:grob-basic-properties",
293 "Get the immutable properties of @var{grob}.")
295 Grob *g = unsmob_grob (grob);
297 LY_ASSERT_SMOB (Grob, grob, 1);
299 /* FIXME: uhg? copy/read only? */
300 return g->immutable_property_alist_;
303 LY_DEFINE (ly_grob_system, "ly:grob-system",
305 "Return the system grob of @var{grob}.")
307 Grob *me = unsmob_grob (grob);
309 LY_ASSERT_SMOB (Grob, grob, 1);
311 if (System *g = me->get_system ())
312 return g->self_scm ();
317 LY_DEFINE (ly_grob_original, "ly:grob-original",
319 "Return the unbroken original grob of @var{grob}.")
321 Grob *me = unsmob_grob (grob);
323 LY_ASSERT_SMOB (Grob, grob, 1);
324 return me->original () ? me->original ()->self_scm () : me->self_scm ();
328 LY_DEFINE (ly_grob_suicide_x, "ly:grob-suicide!",
332 Grob *me = unsmob_grob (grob);
334 LY_ASSERT_SMOB (Grob, grob, 1);
337 return SCM_UNSPECIFIED;
340 LY_DEFINE (ly_grob_translate_axis_x, "ly:grob-translate-axis!",
341 3, 0, 0, (SCM grob, SCM d, SCM a),
342 "Translate @var{grob} on axis@tie{}@var{a} over"
343 " distance@tie{}@var{d}.")
345 Grob *me = unsmob_grob (grob);
347 LY_ASSERT_SMOB (Grob, grob, 1);
348 LY_ASSERT_TYPE (scm_is_number, d, 2);
349 LY_ASSERT_TYPE (is_axis, a, 3);
351 me->translate_axis (scm_to_double (d), Axis (scm_to_int (a)));
352 return SCM_UNSPECIFIED;
355 LY_DEFINE (ly_grob_default_font, "ly:grob-default-font",
357 "Return the default font for grob @var{grob}.")
359 Grob *gr = unsmob_grob (grob);
361 LY_ASSERT_SMOB (Grob, grob, 1);
363 return Font_interface::get_default_font (gr)->self_scm ();
368 TODO: consider swapping order, so we can do
370 (grob-common-refpoint a b c d e)
372 LY_DEFINE (ly_grob_common_refpoint, "ly:grob-common-refpoint",
373 3, 0, 0, (SCM grob, SCM other, SCM axis),
374 "Find the common refpoint of @var{grob} and @var{other}"
378 Grob *gr = unsmob_grob (grob);
380 LY_ASSERT_SMOB (Grob, grob, 1);
381 LY_ASSERT_SMOB (Grob, other, 2);
383 Grob *o = unsmob_grob (other);
385 LY_ASSERT_TYPE (is_axis, axis, 3);
387 Grob *refp = gr->common_refpoint (o, Axis (scm_to_int (axis)));
388 return refp ? refp->self_scm () : SCM_BOOL_F;
391 LY_DEFINE (ly_grob_common_refpoint_of_array, "ly:grob-common-refpoint-of-array",
392 3, 0, 0, (SCM grob, SCM others, SCM axis),
393 "Find the common refpoint of @var{grob} and @var{others}"
394 " (a grob-array) for @var{axis}.")
396 Grob *gr = unsmob_grob (grob);
398 LY_ASSERT_SMOB (Grob, grob, 1);
399 LY_ASSERT_SMOB (Grob_array, others, 2);
401 Grob_array *ga = unsmob_grob_array (others);
402 LY_ASSERT_TYPE (is_axis, axis, 3);
404 Grob *refp = common_refpoint_of_array (ga->array (), gr, Axis (scm_to_int (axis)));
405 return refp ? refp->self_scm () : SCM_BOOL_F;
408 LY_DEFINE (ly_grob_chain_callback, "ly:grob-chain-callback",
409 3, 0, 0, (SCM grob, SCM proc, SCM sym),
410 "Find the callback that is stored as property"
411 " @var{sym} of grob @var{grob} and chain @var{proc}"
412 " to the head of this, meaning that it is called"
413 " using @var{grob} and the previous callback's result.")
415 Grob *gr = unsmob_grob (grob);
417 LY_ASSERT_SMOB (Grob, grob, 1);
418 LY_ASSERT_TYPE (ly_is_procedure, proc, 2);
419 LY_ASSERT_TYPE (ly_is_symbol, sym, 3);
421 chain_callback (gr, proc, sym);
422 return SCM_UNSPECIFIED;