2 grob-scheme.cc -- Scheme entry points for the grob datatype
4 source file of the GNU LilyPond music typesetter
6 (c) 1998--2009 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_SMOB (Grob, grob, 1);
25 LY_ASSERT_TYPE (ly_is_symbol, sym, 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_SMOB (Grob, grob, 1);
37 LY_ASSERT_TYPE (ly_is_symbol, sym, 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 val),
49 "Return the value for property @var{sym} of @var{grob}."
50 " If no value is found, return @var{val} or @code{'()}"
51 " if @var{val} is not specified.")
53 Grob *sc = unsmob_grob (grob);
55 LY_ASSERT_SMOB (Grob, grob, 1);
56 LY_ASSERT_TYPE (ly_is_symbol, sym, 2);
57 if (val == SCM_UNDEFINED)
60 SCM retval = sc->internal_get_property (sym);
61 if (retval == SCM_EOL)
68 LY_DEFINE (ly_grob_interfaces, "ly:grob-interfaces",
70 "Return the interfaces list of grob @var{grob}.")
72 Grob *sc = unsmob_grob (grob);
74 LY_ASSERT_SMOB (Grob, grob, 1);
76 return sc->interfaces ();
79 LY_DEFINE (ly_grob_object, "ly:grob-object",
80 2, 0, 0, (SCM grob, SCM sym),
81 "Return the value of a pointer in grob@tie{}@var{g} of property"
82 " @var{sym}. It returns @code{'()} (end-of-list) if @var{sym}"
83 " is undefined in@tie{}@var{g}.")
85 Grob *sc = unsmob_grob (grob);
87 LY_ASSERT_SMOB (Grob, grob, 1);
88 LY_ASSERT_TYPE (ly_is_symbol, sym, 2);
90 return sc->internal_get_object (sym);
95 /* TODO: make difference between scaled and unscalead variable in
96 calling (i.e different funcs.) */
97 LY_DEFINE (ly_grob_layout, "ly:grob-layout",
99 "Get @code{\\layout} definition from grob @var{grob}.")
101 Grob *sc = unsmob_grob (grob);
103 LY_ASSERT_SMOB (Grob, grob, 1);
105 return sc->layout ()->self_scm ();
108 LY_DEFINE (ly_grob_alist_chain, "ly:grob-alist-chain",
109 1, 1, 0, (SCM grob, SCM global),
110 "Get an alist chain for grob @var{grob}, with @var{global} as"
111 " the global default. If unspecified, @code{font-defaults}"
112 " from the layout block is taken.")
114 Grob *sc = unsmob_grob (grob);
116 LY_ASSERT_SMOB (Grob, grob, 1);
118 if (global == SCM_UNDEFINED)
120 global = sc->layout ()->lookup_variable (ly_symbol2scm ("font-defaults"));
121 if (global == SCM_UNDEFINED)
125 return sc->get_property_alist_chain (global);
128 LY_DEFINE (ly_grob_extent, "ly:grob-extent",
129 3, 0, 0, (SCM grob, SCM refp, SCM axis),
130 "Get the extent in @var{axis} direction of @var{grob} relative to"
131 " the grob @var{refp}.")
133 Grob *sc = unsmob_grob (grob);
134 Grob *ref = unsmob_grob (refp);
137 LY_ASSERT_SMOB (Grob, grob, 1);
138 LY_ASSERT_SMOB (Grob, refp, 2);
139 LY_ASSERT_TYPE (is_axis, axis, 3);
141 Axis a = Axis (scm_to_int (axis));
144 if (ref->common_refpoint (sc, a) != ref)
146 // ugh. should use other error message
147 SCM_ASSERT_TYPE (false, refp, SCM_ARG2, __FUNCTION__, "common refpoint");
149 return ly_interval2scm (sc->extent (ref, a));
152 LY_DEFINE (ly_grob_robust_relative_extent, "ly:grob-robust-relative-extent",
153 3, 0, 0, (SCM grob, SCM refp, SCM axis),
154 "Get the extent in @var{axis} direction of @var{grob} relative to"
155 " the grob @var{refp}, or @code{(0,0)} if empty.")
157 Grob *sc = unsmob_grob (grob);
158 Grob *ref = unsmob_grob (refp);
161 LY_ASSERT_SMOB (Grob, grob, 1);
162 LY_ASSERT_SMOB (Grob, refp, 2);
163 LY_ASSERT_TYPE (is_axis, axis, 3);
165 Axis a = Axis (scm_to_int (axis));
167 if (ref->common_refpoint (sc, a) != ref)
169 // ugh. should use other error message
170 SCM_ASSERT_TYPE (false, refp, SCM_ARG2, __FUNCTION__, "common refpoint");
173 return ly_interval2scm (robust_relative_extent (sc, ref, a));
176 LY_DEFINE (ly_grob_relative_coordinate, "ly:grob-relative-coordinate",
177 3, 0, 0, (SCM grob, SCM refp, SCM axis),
178 "Get the coordinate in @var{axis} direction of @var{grob} relative"
179 " to the grob @var{refp}.")
181 Grob *sc = unsmob_grob (grob);
182 Grob *ref = unsmob_grob (refp);
185 LY_ASSERT_SMOB (Grob, grob, 1);
186 LY_ASSERT_SMOB (Grob, refp, 2);
187 LY_ASSERT_TYPE (is_axis, axis, 3);
189 Axis a = Axis (scm_to_int (axis));
192 if (ref->common_refpoint (sc, a) != ref)
194 // ugh. should use other error message
195 SCM_ASSERT_TYPE (false, refp, SCM_ARG2, __FUNCTION__, "common refpoint");
198 return scm_from_double (sc->relative_coordinate (ref, a));
202 LY_DEFINE (ly_grob_parent, "ly:grob-parent",
203 2, 0, 0, (SCM grob, SCM axis),
204 "Get the parent of @var{grob}. @var{axis} is 0 for the X-axis,"
205 " 1@tie{}for the Y-axis.")
207 Grob *sc = unsmob_grob (grob);
209 LY_ASSERT_SMOB (Grob, grob, 1);
210 LY_ASSERT_TYPE (is_axis, axis, 2);
212 Grob *par = sc->get_parent (Axis (scm_to_int (axis)));
213 return par ? par->self_scm () : SCM_EOL;
216 LY_DEFINE (ly_grob_properties, "ly:grob-properties",
218 "Get the mutable properties of @var{grob}.")
220 Grob *g = unsmob_grob (grob);
222 LY_ASSERT_SMOB (Grob, grob, 1);
224 /* FIXME: uhg? copy/read only? */
225 return g->mutable_property_alist_;
228 LY_DEFINE (ly_grob_basic_properties, "ly:grob-basic-properties",
230 "Get the immutable properties of @var{grob}.")
232 Grob *g = unsmob_grob (grob);
234 LY_ASSERT_SMOB (Grob, grob, 1);
236 /* FIXME: uhg? copy/read only? */
237 return g->immutable_property_alist_;
240 LY_DEFINE (ly_grob_system, "ly:grob-system",
242 "Return the system grob of @var{grob}.")
244 Grob *me = unsmob_grob (grob);
246 LY_ASSERT_SMOB (Grob, grob, 1);
248 if (System *g = me->get_system ())
249 return g->self_scm ();
254 LY_DEFINE (ly_grob_original, "ly:grob-original",
256 "Return the unbroken original grob of @var{grob}.")
258 Grob *me = unsmob_grob (grob);
260 LY_ASSERT_SMOB (Grob, grob, 1);
261 return me->original () ? me->original ()->self_scm () : me->self_scm ();
265 LY_DEFINE (ly_grob_suicide_x, "ly:grob-suicide!",
269 Grob *me = unsmob_grob (grob);
271 LY_ASSERT_SMOB (Grob, grob, 1);
274 return SCM_UNSPECIFIED;
277 LY_DEFINE (ly_grob_translate_axis_x, "ly:grob-translate-axis!",
278 3, 0, 0, (SCM grob, SCM d, SCM a),
279 "Translate @var{g} on axis@tie{}@var{a} over"
280 " distance@tie{}@var{d}.")
282 Grob *me = unsmob_grob (grob);
284 LY_ASSERT_SMOB (Grob, grob, 1);
285 LY_ASSERT_TYPE (scm_is_number, d, 2);
286 LY_ASSERT_TYPE (is_axis, a, 3);
288 me->translate_axis (scm_to_double (d), Axis (scm_to_int (a)));
289 return SCM_UNSPECIFIED;
292 LY_DEFINE (ly_grob_default_font, "ly:grob-default-font",
294 "Return the default font for grob @var{gr}.")
296 Grob *gr = unsmob_grob (grob);
298 LY_ASSERT_SMOB (Grob, grob, 1);
300 return Font_interface::get_default_font (gr)->self_scm ();
305 TODO: consider swapping order, so we can do
307 (grob-common-refpoint a b c d e)
309 LY_DEFINE (ly_grob_common_refpoint, "ly:grob-common-refpoint",
310 3, 0, 0, (SCM grob, SCM other, SCM axis),
311 "Find the common refpoint of @var{grob} and @var{other}"
315 Grob *gr = unsmob_grob (grob);
317 LY_ASSERT_SMOB (Grob, grob, 1);
318 LY_ASSERT_SMOB (Grob, other, 2);
320 Grob *o = unsmob_grob (other);
322 LY_ASSERT_TYPE (is_axis, axis, 3);
324 Grob *refp = gr->common_refpoint (o, Axis (scm_to_int (axis)));
325 return refp ? refp->self_scm () : SCM_BOOL_F;
328 LY_DEFINE (ly_grob_common_refpoint_of_array, "ly:grob-common-refpoint-of-array",
329 3, 0, 0, (SCM grob, SCM others, SCM axis),
330 "Find the common refpoint of @var{grob} and @var{others}"
331 " (a grob-array) for @var{axis}.")
333 Grob *gr = unsmob_grob (grob);
335 LY_ASSERT_SMOB (Grob, grob, 1);
336 LY_ASSERT_SMOB (Grob_array, others, 2);
338 Grob_array *ga = unsmob_grob_array (others);
339 LY_ASSERT_TYPE (is_axis, axis, 3);
341 Grob *refp = common_refpoint_of_array (ga->array (), gr, Axis (scm_to_int (axis)));
342 return refp ? refp->self_scm () : SCM_BOOL_F;