2 grob-scheme.cc -- Scheme entry points for the grob datatype
4 source file of the GNU LilyPond music typesetter
6 (c) 1998--2007 Jan Nieuwenhuizen <janneke@gnu.org>
7 Han-Wen Nienhuys <hanwen@xs4all.nl>
10 #include "warn.hh" // error()
12 #include "output-def.hh"
14 #include "font-interface.hh"
15 #include "paper-score.hh"
16 #include "grob-array.hh"
18 LY_DEFINE (ly_grob_property_data, "ly:grob-property-data",
19 2, 0, 0, (SCM grob, SCM sym),
20 "Retrieve @var{sym} for @var{grob} but don't process callbacks.")
22 Grob *sc = unsmob_grob (grob);
24 LY_ASSERT_FIRST_SMOB(Grob, grob);
25 LY_ASSERT_TYPE (ly_is_symbol, 2);
27 return sc->get_property_data (sym);
30 LY_DEFINE (ly_grob_set_property_x, "ly:grob-set-property!",
31 3, 0, 0, (SCM grob, SCM sym, SCM val),
32 "Set @var{sym} in grob @var{grob} to value @var{val}")
34 Grob *sc = unsmob_grob (grob);
36 LY_ASSERT_FIRST_SMOB(Grob, grob);
37 LY_ASSERT_TYPE (ly_is_symbol, 2);
39 if (!ly_is_procedure (val)
40 && !type_check_assignment (sym, val, ly_symbol2scm ("backend-type?")))
41 error ("typecheck failed");
43 sc->set_property (sym, val);
44 return SCM_UNSPECIFIED;
47 LY_DEFINE (ly_grob_property, "ly:grob-property",
48 2, 1, 0, (SCM grob, SCM sym, SCM deflt),
49 "Return the value of a value in grob @var{g} of property @var{sym}. "
50 "It will return @code{'()} or @var{deflt} (if specified) "
51 "if @var{sym} is undefined in @var{g}."
54 Grob *sc = unsmob_grob (grob);
56 LY_ASSERT_FIRST_SMOB(Grob, grob);
57 LY_ASSERT_TYPE (ly_is_symbol, 2);
58 if (deflt == SCM_UNDEFINED)
61 SCM retval = sc->internal_get_property (sym);
62 if (retval == SCM_EOL)
69 LY_DEFINE (ly_grob_interfaces, "ly:grob-interfaces",
71 "Return the interfaces list of grob @var{grob}.")
73 Grob *sc = unsmob_grob (grob);
75 LY_ASSERT_FIRST_SMOB(Grob, grob);
77 return sc->interfaces ();
80 LY_DEFINE (ly_grob_object, "ly:grob-object",
81 2, 0, 0, (SCM grob, SCM sym),
82 "Return the value of a pointer in grob @var{g} of property @var{sym}. "
83 "It will return @code{' ()} (end-of-list) "
84 "if @var{sym} is undefined in @var{g}."
87 Grob *sc = unsmob_grob (grob);
89 LY_ASSERT_FIRST_SMOB(Grob, grob);
90 LY_ASSERT_TYPE (ly_is_symbol, 2);
92 return sc->internal_get_object (sym);
97 /* TODO: make difference between scaled and unscalead variable in
98 calling (i.e different funcs.) */
99 LY_DEFINE (ly_grob_layout, "ly:grob-layout",
101 "Get @code{\\layout} definition from grob @var{grob}.")
103 Grob *sc = unsmob_grob (grob);
105 LY_ASSERT_FIRST_SMOB(Grob, grob);
107 return sc->layout ()->self_scm ();
110 LY_DEFINE (ly_grob_alist_chain, "ly:grob-alist-chain",
111 1, 1, 0, (SCM grob, SCM global),
112 "Get an alist chain for grob @var{grob}, with @var{global} as the "
113 "global default. If unspecified, @code{font-defaults} "
114 "from the layout block is taken. ")
116 Grob *sc = unsmob_grob (grob);
118 LY_ASSERT_FIRST_SMOB(Grob, grob);
120 if (global == SCM_UNDEFINED)
122 global = sc->layout ()->lookup_variable (ly_symbol2scm ("font-defaults"));
123 if (global == SCM_UNDEFINED)
127 return sc->get_property_alist_chain (global);
130 LY_DEFINE (ly_grob_extent, "ly:grob-extent",
131 3, 0, 0, (SCM grob, SCM refp, SCM axis),
132 "Get the extent in @var{axis} direction of @var{grob} relative to "
133 "the grob @var{refp}")
135 Grob *sc = unsmob_grob (grob);
136 Grob *ref = unsmob_grob (refp);
139 LY_ASSERT_FIRST_SMOB(Grob, grob);
140 LY_ASSERT_SMOB(Grob, 2);
141 LY_ASSERT_TYPE(is_axis, 3);
143 Axis a = Axis (scm_to_int (axis));
146 if (ref->common_refpoint (sc, a) != ref)
148 // ugh. should use other error message
149 SCM_ASSERT_TYPE (false, refp, SCM_ARG2, __FUNCTION__, "common refpoint");
151 return ly_interval2scm (sc->extent (ref, a));
154 LY_DEFINE (ly_grob_robust_relative_extent, "ly:grob-robust-relative-extent",
155 3, 0, 0, (SCM grob, SCM refp, SCM axis),
156 "Get the extent in @var{axis} direction of @var{grob} relative to "
157 "the grob @var{refp}, or (0,0) if empty")
159 Grob *sc = unsmob_grob (grob);
160 Grob *ref = unsmob_grob (refp);
163 LY_ASSERT_FIRST_SMOB(Grob, grob);
164 LY_ASSERT_SMOB(Grob, 2);
165 LY_ASSERT_TYPE(is_axis, 3);
167 Axis a = Axis (scm_to_int (axis));
169 if (ref->common_refpoint (sc, a) != ref)
171 // ugh. should use other error message
172 SCM_ASSERT_TYPE (false, refp, SCM_ARG2, __FUNCTION__, "common refpoint");
175 return ly_interval2scm (robust_relative_extent (sc, ref, a));
178 LY_DEFINE (ly_grob_relative_coordinate, "ly:grob-relative-coordinate",
179 3, 0, 0, (SCM grob, SCM refp, SCM axis),
180 "Get the coordinate in @var{axis} direction of @var{grob} relative to "
181 "the grob @var{refp}")
183 Grob *sc = unsmob_grob (grob);
184 Grob *ref = unsmob_grob (refp);
187 LY_ASSERT_FIRST_SMOB(Grob, grob);
188 LY_ASSERT_SMOB(Grob, 2);
189 LY_ASSERT_TYPE(is_axis, 3);
191 Axis a = Axis (scm_to_int (axis));
194 if (ref->common_refpoint (sc, a) != ref)
196 // ugh. should use other error message
197 SCM_ASSERT_TYPE (false, refp, SCM_ARG2, __FUNCTION__, "common refpoint");
200 return scm_from_double (sc->relative_coordinate (ref,a));
204 LY_DEFINE (ly_grob_parent, "ly:grob-parent",
205 2, 0, 0, (SCM grob, SCM axis),
206 "Get the parent of @var{grob}. @var{axis} is 0 for the X-axis, "
209 Grob *sc = unsmob_grob (grob);
211 LY_ASSERT_FIRST_SMOB(Grob, grob);
212 LY_ASSERT_TYPE(is_axis, 2);
214 Grob *par = sc->get_parent (Axis (scm_to_int (axis)));
215 return par ? par->self_scm () : SCM_EOL;
218 LY_DEFINE (ly_grob_properties, "ly:grob-properties",
220 "Get the mutable proprerties of @var{grob}.")
222 Grob *g = unsmob_grob (grob);
224 LY_ASSERT_FIRST_SMOB(Grob, grob);
226 /* FIXME: uhg? copy/read only? */
227 return g->mutable_property_alist_;
230 LY_DEFINE (ly_grob_basic_properties, "ly:grob-basic-properties",
232 "Get the immutable properties of @var{grob}.")
234 Grob *g = unsmob_grob (grob);
236 LY_ASSERT_FIRST_SMOB(Grob, grob);
238 /* FIXME: uhg? copy/read only? */
239 return g->immutable_property_alist_;
242 LY_DEFINE (ly_grob_system, "ly:grob-system",
244 "Return the System Grob of @var{grob}.")
246 Grob *me = unsmob_grob (grob);
248 LY_ASSERT_FIRST_SMOB(Grob, grob);
250 if (System *g = me->get_system ())
251 return g->self_scm ();
256 LY_DEFINE (ly_grob_original, "ly:grob-original",
258 "Return the unbroken original Grob of @var{grob}.")
260 Grob *me = unsmob_grob (grob);
262 LY_ASSERT_FIRST_SMOB(Grob, grob);
263 return me->original () ? me->original ()->self_scm () : me->self_scm ();
267 LY_DEFINE (ly_grob_suicide_x, "ly:grob-suicide!",
271 Grob *me = unsmob_grob (grob);
273 LY_ASSERT_FIRST_SMOB(Grob, grob);
276 return SCM_UNSPECIFIED;
279 LY_DEFINE (ly_grob_translate_axis_x, "ly:grob-translate-axis!",
280 3, 0, 0, (SCM grob, SCM d, SCM a),
281 "Translate @var{g} on axis @var{a} over distance @var{d}.")
283 Grob *me = unsmob_grob (grob);
285 LY_ASSERT_FIRST_SMOB(Grob, grob);
286 LY_ASSERT_TYPE(scm_is_number, 2);
287 LY_ASSERT_TYPE(is_axis, 3);
289 me->translate_axis (scm_to_double (d), Axis (scm_to_int (a)));
290 return SCM_UNSPECIFIED;
293 LY_DEFINE (ly_grob_default_font, "ly:grob-default-font",
295 "Return the default font for grob @var{gr}.")
297 Grob *gr = unsmob_grob (grob);
299 LY_ASSERT_FIRST_SMOB(Grob, grob);
301 return Font_interface::get_default_font (gr)->self_scm ();
306 TODO: consider swapping order, so we can do
308 (grob-common-refpoint a b c d e)
310 LY_DEFINE (ly_grob_common_refpoint, "ly:grob-common-refpoint",
311 3, 0, 0, (SCM grob, SCM other, SCM axis),
312 "Find the common refpoint of @var{grob} and @var{other} for @var{axis}."
316 Grob *gr = unsmob_grob (grob);
318 LY_ASSERT_FIRST_SMOB(Grob, grob);
319 LY_ASSERT_SMOB(Grob, 2);
321 Grob *o = unsmob_grob (other);
323 LY_ASSERT_TYPE(is_axis, 3);
325 Grob *refp = gr->common_refpoint (o, Axis (scm_to_int (axis)));
326 return refp ? refp->self_scm () : SCM_BOOL_F;
329 LY_DEFINE (ly_grob_common_refpoint_of_array, "ly:grob-common-refpoint-of-array",
330 3, 0, 0, (SCM grob, SCM others, SCM axis),
331 "Find the common refpoint of @var{grob} and @var{others} "
332 "(a grob-array) for @var{axis}."
335 Grob *gr = unsmob_grob (grob);
337 LY_ASSERT_FIRST_SMOB(Grob, grob);
338 LY_ASSERT_SMOB(Grob_array, 2);
340 Grob_array *ga = unsmob_grob_array (others);
341 LY_ASSERT_TYPE(is_axis, 3);
343 Grob *refp = common_refpoint_of_array (ga->array (), gr, Axis (scm_to_int (axis)));
344 return refp ? refp->self_scm () : SCM_BOOL_F;