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;
81 LY_DEFINE (ly_grob_property, "ly:grob-property",
82 2, 1, 0, (SCM grob, SCM sym, SCM val),
83 "Return the value for property @var{sym} of @var{grob}."
84 " If no value is found, return @var{val} or @code{'()}"
85 " if @var{val} is not specified.")
87 Grob *sc = unsmob_grob (grob);
89 LY_ASSERT_SMOB (Grob, grob, 1);
90 LY_ASSERT_TYPE (ly_is_symbol, sym, 2);
91 if (val == SCM_UNDEFINED)
94 SCM retval = sc->internal_get_property (sym);
95 if (retval == SCM_EOL)
101 LY_DEFINE (ly_grob_interfaces, "ly:grob-interfaces",
103 "Return the interfaces list of grob @var{grob}.")
105 Grob *sc = unsmob_grob (grob);
107 LY_ASSERT_SMOB (Grob, grob, 1);
109 return sc->interfaces ();
112 LY_DEFINE (ly_grob_object, "ly:grob-object",
113 2, 0, 0, (SCM grob, SCM sym),
114 "Return the value of a pointer in grob @var{grob} of property"
115 " @var{sym}. It returns @code{'()} (end-of-list) if @var{sym}"
116 " is undefined in @var{grob}.")
118 Grob *sc = unsmob_grob (grob);
120 LY_ASSERT_SMOB (Grob, grob, 1);
121 LY_ASSERT_TYPE (ly_is_symbol, sym, 2);
123 return sc->internal_get_object (sym);
126 LY_DEFINE (ly_grob_set_object_x, "ly:grob-set-object!",
127 3, 0, 0, (SCM grob, SCM sym, SCM val),
128 "Set @var{sym} in grob @var{grob} to value @var{val}.")
130 Grob *sc = unsmob_grob (grob);
132 LY_ASSERT_SMOB (Grob, grob, 1);
133 LY_ASSERT_TYPE (ly_is_symbol, sym, 2);
135 sc->set_object (sym, val);
136 return SCM_UNSPECIFIED;
139 /* TODO: make difference between scaled and unscalead variable in
140 calling (i.e different funcs.) */
141 LY_DEFINE (ly_grob_layout, "ly:grob-layout",
143 "Get @code{\\layout} definition from grob @var{grob}.")
145 Grob *sc = unsmob_grob (grob);
147 LY_ASSERT_SMOB (Grob, grob, 1);
149 return sc->layout ()->self_scm ();
152 LY_DEFINE (ly_grob_alist_chain, "ly:grob-alist-chain",
153 1, 1, 0, (SCM grob, SCM global),
154 "Get an alist chain for grob @var{grob}, with @var{global} as"
155 " the global default. If unspecified, @code{font-defaults}"
156 " from the layout block is taken.")
158 Grob *sc = unsmob_grob (grob);
160 LY_ASSERT_SMOB (Grob, grob, 1);
162 if (global == SCM_UNDEFINED)
164 global = sc->layout ()->lookup_variable (ly_symbol2scm ("font-defaults"));
165 if (global == SCM_UNDEFINED)
169 return sc->get_property_alist_chain (global);
172 LY_DEFINE (ly_grob_extent, "ly:grob-extent",
173 3, 0, 0, (SCM grob, SCM refp, SCM axis),
174 "Get the extent in @var{axis} direction of @var{grob} relative to"
175 " the grob @var{refp}.")
177 Grob *sc = unsmob_grob (grob);
178 Grob *ref = unsmob_grob (refp);
180 LY_ASSERT_SMOB (Grob, grob, 1);
181 LY_ASSERT_SMOB (Grob, refp, 2);
182 LY_ASSERT_TYPE (is_axis, axis, 3);
184 Axis a = Axis (scm_to_int (axis));
186 if (ref->common_refpoint (sc, a) != ref)
188 // ugh. should use other error message
189 SCM_ASSERT_TYPE (false, refp, SCM_ARG2, __FUNCTION__, "common refpoint");
191 return ly_interval2scm (sc->extent (ref, a));
194 LY_DEFINE (ly_grob_robust_relative_extent, "ly:grob-robust-relative-extent",
195 3, 0, 0, (SCM grob, SCM refp, SCM axis),
196 "Get the extent in @var{axis} direction of @var{grob} relative to"
197 " the grob @var{refp}, or @code{(0,0)} if empty.")
199 Grob *sc = unsmob_grob (grob);
200 Grob *ref = unsmob_grob (refp);
202 LY_ASSERT_SMOB (Grob, grob, 1);
203 LY_ASSERT_SMOB (Grob, refp, 2);
204 LY_ASSERT_TYPE (is_axis, axis, 3);
206 Axis a = Axis (scm_to_int (axis));
208 if (ref->common_refpoint (sc, a) != ref)
210 // ugh. should use other error message
211 SCM_ASSERT_TYPE (false, refp, SCM_ARG2, __FUNCTION__, "common refpoint");
214 return ly_interval2scm (robust_relative_extent (sc, ref, a));
217 LY_DEFINE (ly_grob_relative_coordinate, "ly:grob-relative-coordinate",
218 3, 0, 0, (SCM grob, SCM refp, SCM axis),
219 "Get the coordinate in @var{axis} direction of @var{grob} relative"
220 " to 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");
237 return scm_from_double (sc->relative_coordinate (ref, a));
240 LY_DEFINE (ly_grob_parent, "ly:grob-parent",
241 2, 0, 0, (SCM grob, SCM axis),
242 "Get the parent of @var{grob}. @var{axis} is 0 for the X-axis,"
243 " 1@tie{}for the Y-axis.")
245 Grob *sc = unsmob_grob (grob);
247 LY_ASSERT_SMOB (Grob, grob, 1);
248 LY_ASSERT_TYPE (is_axis, axis, 2);
250 Grob *par = sc->get_parent (Axis (scm_to_int (axis)));
251 return par ? par->self_scm () : SCM_EOL;
254 LY_DEFINE (ly_grob_set_parent_x, "ly:grob-set-parent!",
255 3, 0, 0, (SCM grob, SCM axis, SCM parent_grob),
256 "Set @var{parent-grob} the parent of grob @var{grob} in axis @var{axis}.")
258 Grob *gr = unsmob_grob (grob);
259 Grob *parent = unsmob_grob (parent_grob);
261 LY_ASSERT_SMOB (Grob, grob, 1);
262 LY_ASSERT_TYPE (is_axis, axis, 2);
263 LY_ASSERT_SMOB (Grob, parent_grob, 3);
265 Axis a = Axis (scm_to_int (axis));
266 gr->set_parent (parent, a);
267 return SCM_UNSPECIFIED;
270 LY_DEFINE (ly_grob_properties, "ly:grob-properties",
272 "Get the mutable properties of @var{grob}.")
274 Grob *g = unsmob_grob (grob);
276 LY_ASSERT_SMOB (Grob, grob, 1);
278 /* FIXME: uhg? copy/read only? */
279 return g->mutable_property_alist_;
282 LY_DEFINE (ly_grob_basic_properties, "ly:grob-basic-properties",
284 "Get the immutable properties of @var{grob}.")
286 Grob *g = unsmob_grob (grob);
288 LY_ASSERT_SMOB (Grob, grob, 1);
290 /* FIXME: uhg? copy/read only? */
291 return g->immutable_property_alist_;
294 LY_DEFINE (ly_grob_system, "ly:grob-system",
296 "Return the system grob of @var{grob}.")
298 Grob *me = unsmob_grob (grob);
300 LY_ASSERT_SMOB (Grob, grob, 1);
302 if (System *g = me->get_system ())
303 return g->self_scm ();
308 LY_DEFINE (ly_grob_original, "ly:grob-original",
310 "Return the unbroken original grob of @var{grob}.")
312 Grob *me = unsmob_grob (grob);
314 LY_ASSERT_SMOB (Grob, grob, 1);
315 return me->original () ? me->original ()->self_scm () : me->self_scm ();
318 LY_DEFINE (ly_grob_suicide_x, "ly:grob-suicide!",
322 Grob *me = unsmob_grob (grob);
324 LY_ASSERT_SMOB (Grob, grob, 1);
327 return SCM_UNSPECIFIED;
330 LY_DEFINE (ly_grob_translate_axis_x, "ly:grob-translate-axis!",
331 3, 0, 0, (SCM grob, SCM d, SCM a),
332 "Translate @var{grob} on axis@tie{}@var{a} over"
333 " distance@tie{}@var{d}.")
335 Grob *me = unsmob_grob (grob);
337 LY_ASSERT_SMOB (Grob, grob, 1);
338 LY_ASSERT_TYPE (scm_is_number, d, 2);
339 LY_ASSERT_TYPE (is_axis, a, 3);
341 me->translate_axis (scm_to_double (d), Axis (scm_to_int (a)));
342 return SCM_UNSPECIFIED;
345 LY_DEFINE (ly_grob_default_font, "ly:grob-default-font",
347 "Return the default font for grob @var{grob}.")
349 Grob *gr = unsmob_grob (grob);
351 LY_ASSERT_SMOB (Grob, grob, 1);
353 return Font_interface::get_default_font (gr)->self_scm ();
357 TODO: consider swapping order, so we can do
359 (grob-common-refpoint a b c d e)
361 LY_DEFINE (ly_grob_common_refpoint, "ly:grob-common-refpoint",
362 3, 0, 0, (SCM grob, SCM other, SCM axis),
363 "Find the common refpoint of @var{grob} and @var{other}"
367 Grob *gr = unsmob_grob (grob);
369 LY_ASSERT_SMOB (Grob, grob, 1);
370 LY_ASSERT_SMOB (Grob, other, 2);
372 Grob *o = unsmob_grob (other);
374 LY_ASSERT_TYPE (is_axis, axis, 3);
376 Grob *refp = gr->common_refpoint (o, Axis (scm_to_int (axis)));
377 return refp ? refp->self_scm () : SCM_BOOL_F;
380 LY_DEFINE (ly_grob_common_refpoint_of_array, "ly:grob-common-refpoint-of-array",
381 3, 0, 0, (SCM grob, SCM others, SCM axis),
382 "Find the common refpoint of @var{grob} and @var{others}"
383 " (a grob-array) for @var{axis}.")
385 Grob *gr = unsmob_grob (grob);
387 LY_ASSERT_SMOB (Grob, grob, 1);
388 LY_ASSERT_SMOB (Grob_array, others, 2);
390 Grob_array *ga = unsmob_grob_array (others);
391 LY_ASSERT_TYPE (is_axis, axis, 3);
393 Grob *refp = common_refpoint_of_array (ga->array (), gr, Axis (scm_to_int (axis)));
394 return refp ? refp->self_scm () : SCM_BOOL_F;
397 LY_DEFINE (ly_grob_chain_callback, "ly:grob-chain-callback",
398 3, 0, 0, (SCM grob, SCM proc, SCM sym),
399 "Find the callback that is stored as property"
400 " @var{sym} of grob @var{grob} and chain @var{proc}"
401 " to the head of this, meaning that it is called"
402 " using @var{grob} and the previous callback's result.")
404 Grob *gr = unsmob_grob (grob);
406 LY_ASSERT_SMOB (Grob, grob, 1);
407 LY_ASSERT_TYPE (ly_is_procedure, proc, 2);
408 LY_ASSERT_TYPE (ly_is_symbol, sym, 3);
410 chain_callback (gr, proc, sym);
411 return SCM_UNSPECIFIED;