X-Git-Url: https://git.donarmstrong.com/?a=blobdiff_plain;f=lily%2Fgrob-scheme.cc;h=4d6ed8f69b1524236b81b8f32291382cf75846db;hb=5b4b0d6e9a197e8f9eb085b7c2ad78b8be3e5cfc;hp=3242db1603645dcecf4709a58b938a425e6d7ce6;hpb=8b956db3cbebf3bb1bcb5fd1cf9b21f6fd767fc7;p=lilypond.git diff --git a/lily/grob-scheme.cc b/lily/grob-scheme.cc index 3242db1603..4d6ed8f69b 100644 --- a/lily/grob-scheme.cc +++ b/lily/grob-scheme.cc @@ -1,154 +1,344 @@ -#include "grob.hh" -#include "warn.hh" -#include "spanner.hh" +/* + grob-scheme.cc -- Scheme entry points for the grob datatype + + source file of the GNU LilyPond music typesetter + + (c) 1998--2008 Jan Nieuwenhuizen + Han-Wen Nienhuys +*/ + +#include "warn.hh" // error () #include "item.hh" -#include "paper-def.hh" +#include "output-def.hh" +#include "system.hh" +#include "font-interface.hh" +#include "paper-score.hh" +#include "grob-array.hh" + +LY_DEFINE (ly_grob_property_data, "ly:grob-property-data", + 2, 0, 0, (SCM grob, SCM sym), + "Retrieve @var{sym} for @var{grob} but don't process callbacks.") +{ + Grob *sc = unsmob_grob (grob); -LY_DEFINE(ly_set_grob_property,"ly-set-grob-property!", 3, 0, 0, - (SCM grob, SCM sym, SCM val), - "Set @var{sym} in grob @var{grob} to value @var{val}") + LY_ASSERT_SMOB (Grob, grob, 1); + LY_ASSERT_TYPE (ly_is_symbol, sym, 2); + + return sc->get_property_data (sym); +} + +LY_DEFINE (ly_grob_set_property_x, "ly:grob-set-property!", + 3, 0, 0, (SCM grob, SCM sym, SCM val), + "Set @var{sym} in grob @var{grob} to value @var{val}.") { - Grob * sc = unsmob_grob (grob); - SCM_ASSERT_TYPE(sc, grob, SCM_ARG1, __FUNCTION__, "grob"); - SCM_ASSERT_TYPE(gh_symbol_p (sym), sym, SCM_ARG2, __FUNCTION__, "symbol"); + Grob *sc = unsmob_grob (grob); + + LY_ASSERT_SMOB (Grob, grob, 1); + LY_ASSERT_TYPE (ly_is_symbol, sym, 2); - if (!type_check_assignment (sym, val, ly_symbol2scm ("backend-type?"))) + if (!ly_is_procedure (val) + && !type_check_assignment (sym, val, ly_symbol2scm ("backend-type?"))) error ("typecheck failed"); - - sc->internal_set_grob_property (sym, val); + + sc->set_property (sym, val); return SCM_UNSPECIFIED; } -LY_DEFINE(ly_get_grob_property, - "ly-get-grob-property", 2, 0, 0, (SCM grob, SCM sym), - "Get the value of a value in grob @var{g} of property @var{sym}. It -will return @code{'()} (end-of-list) if @var{g} doesn't have @var{sym} set. +LY_DEFINE (ly_grob_property, "ly:grob-property", + 2, 1, 0, (SCM grob, SCM sym, SCM deflt), + "Return the value of a value in grob@tie{}@var{g} of property" + " @var{sym}. It returns @code{'()} (end-of-list) or" + " @var{deflt} (if specified) if @var{sym} is undefined" + " in@tie{}@var{g}.") +{ + Grob *sc = unsmob_grob (grob); + + LY_ASSERT_SMOB (Grob, grob, 1); + LY_ASSERT_TYPE (ly_is_symbol, sym, 2); + if (deflt == SCM_UNDEFINED) + deflt = SCM_EOL; + + SCM retval = sc->internal_get_property (sym); + if (retval == SCM_EOL) + retval = deflt; + + return retval; +} -Grob properties are stored as GUILE association lists, with symbols as -keys. All lookup functions identify undefined properties with -end-of-list (i.e. @code{'()} in Scheme or @code{SCM_EOL} in C) -Properties are stored in two ways: -@itemize @bullet -@item mutable properties. -Grob properties that change from object to object. The storage of -these are private to a grob. For example pointers to other grobs are -always stored in the mutable properties. +LY_DEFINE (ly_grob_interfaces, "ly:grob-interfaces", + 1, 0, 0, (SCM grob), + "Return the interfaces list of grob @var{grob}.") +{ + Grob *sc = unsmob_grob (grob); + + LY_ASSERT_SMOB (Grob, grob, 1); -@item immutable properties. -Grob properties that are shared across different grobs of the same -type. The storage is shared, and hence it is read-only. Typically, this -is used to store function callbacks, and default settings. They are -initially read from @file{scm/grob-description.scm}. -@end itemize + return sc->interfaces (); +} -") +LY_DEFINE (ly_grob_object, "ly:grob-object", + 2, 0, 0, (SCM grob, SCM sym), + "Return the value of a pointer in grob@tie{}@var{g} of property" + " @var{sym}. It returns @code{'()} (end-of-list) if @var{sym}" + " is undefined in@tie{}@var{g}.") { - Grob * sc = unsmob_grob (grob); - SCM_ASSERT_TYPE(sc, grob, SCM_ARG1, __FUNCTION__, "grob"); - SCM_ASSERT_TYPE(gh_symbol_p (sym), sym, SCM_ARG2, __FUNCTION__, "symbol"); + Grob *sc = unsmob_grob (grob); + + LY_ASSERT_SMOB (Grob, grob, 1); + LY_ASSERT_TYPE (ly_is_symbol, sym, 2); - return sc->internal_get_grob_property (sym); + return sc->internal_get_object (sym); } -LY_DEFINE(spanner_get_bound, "ly-get-spanner-bound", 2 , 0, 0, - (SCM slur, SCM dir), - "Get one of the bounds of @var{spanner}. @var{dir} may be @code{-1} for -left, and @code{1} for right. -") + + +/* TODO: make difference between scaled and unscalead variable in + calling (i.e different funcs.) */ +LY_DEFINE (ly_grob_layout, "ly:grob-layout", + 1, 0, 0, (SCM grob), + "Get @code{\\layout} definition from grob @var{grob}.") { - Spanner * sl = dynamic_cast (unsmob_grob (slur)); - SCM_ASSERT_TYPE(sl, slur, SCM_ARG1, __FUNCTION__, "spanner grob"); - SCM_ASSERT_TYPE(ly_dir_p (dir), slur, SCM_ARG2, __FUNCTION__, "dir"); - return sl->get_bound (to_dir (dir))->self_scm (); + Grob *sc = unsmob_grob (grob); + + LY_ASSERT_SMOB (Grob, grob, 1); + + return sc->layout ()->self_scm (); } -LY_DEFINE(ly_get_paper_var,"ly-get-paper-variable", 2, 0, 0, - (SCM grob, SCM sym), - "Get a variable from the \\paper block.") +LY_DEFINE (ly_grob_alist_chain, "ly:grob-alist-chain", + 1, 1, 0, (SCM grob, SCM global), + "Get an alist chain for grob @var{grob}, with @var{global} as" + " the global default. If unspecified, @code{font-defaults}" + " from the layout block is taken.") { - Grob * sc = unsmob_grob (grob); - SCM_ASSERT_TYPE(sc, grob, SCM_ARG1, __FUNCTION__, "grob"); - SCM_ASSERT_TYPE(gh_symbol_p (sym), sym, SCM_ARG2, __FUNCTION__, "symbol"); + Grob *sc = unsmob_grob (grob); + + LY_ASSERT_SMOB (Grob, grob, 1); - return sc->get_paper () ->get_scmvar_scm (sym); + if (global == SCM_UNDEFINED) + { + global = sc->layout ()->lookup_variable (ly_symbol2scm ("font-defaults")); + if (global == SCM_UNDEFINED) + global = SCM_EOL; + } + + return sc->get_property_alist_chain (global); } +LY_DEFINE (ly_grob_extent, "ly:grob-extent", + 3, 0, 0, (SCM grob, SCM refp, SCM axis), + "Get the extent in @var{axis} direction of @var{grob} relative to" + " the grob @var{refp}.") +{ + Grob *sc = unsmob_grob (grob); + Grob *ref = unsmob_grob (refp); + + + LY_ASSERT_SMOB (Grob, grob, 1); + LY_ASSERT_SMOB (Grob, refp, 2); + LY_ASSERT_TYPE (is_axis, axis, 3); + Axis a = Axis (scm_to_int (axis)); + + + if (ref->common_refpoint (sc, a) != ref) + { + // ugh. should use other error message + SCM_ASSERT_TYPE (false, refp, SCM_ARG2, __FUNCTION__, "common refpoint"); + } + return ly_interval2scm (sc->extent (ref, a)); +} -LY_DEFINE(ly_get_extent, "ly-get-extent", 3, 0, 0, - (SCM grob, SCM refp, SCM axis), - "Get the extent in @var{axis} direction of @var{grob} relative to the -grob @var{refp}") +LY_DEFINE (ly_grob_robust_relative_extent, "ly:grob-robust-relative-extent", + 3, 0, 0, (SCM grob, SCM refp, SCM axis), + "Get the extent in @var{axis} direction of @var{grob} relative to" + " the grob @var{refp}, or @code{(0,0)} if empty.") { - Grob * sc = unsmob_grob (grob); - Grob * ref = unsmob_grob (refp); - SCM_ASSERT_TYPE(sc, grob, SCM_ARG1, __FUNCTION__, "grob"); - SCM_ASSERT_TYPE(ref, refp, SCM_ARG2, __FUNCTION__, "grob"); + Grob *sc = unsmob_grob (grob); + Grob *ref = unsmob_grob (refp); - SCM_ASSERT_TYPE(ly_axis_p (axis), axis, SCM_ARG3, __FUNCTION__, "axis"); + + LY_ASSERT_SMOB (Grob, grob, 1); + LY_ASSERT_SMOB (Grob, refp, 2); + LY_ASSERT_TYPE (is_axis, axis, 3); + + Axis a = Axis (scm_to_int (axis)); + + if (ref->common_refpoint (sc, a) != ref) + { + // ugh. should use other error message + SCM_ASSERT_TYPE (false, refp, SCM_ARG2, __FUNCTION__, "common refpoint"); + } + + return ly_interval2scm (robust_relative_extent (sc, ref, a)); +} + +LY_DEFINE (ly_grob_relative_coordinate, "ly:grob-relative-coordinate", + 3, 0, 0, (SCM grob, SCM refp, SCM axis), + "Get the coordinate in @var{axis} direction of @var{grob} relative" + " to the grob @var{refp}.") +{ + Grob *sc = unsmob_grob (grob); + Grob *ref = unsmob_grob (refp); + + + LY_ASSERT_SMOB (Grob, grob, 1); + LY_ASSERT_SMOB (Grob, refp, 2); + LY_ASSERT_TYPE (is_axis, axis, 3); + + Axis a = Axis (scm_to_int (axis)); + + + if (ref->common_refpoint (sc, a) != ref) + { + // ugh. should use other error message + SCM_ASSERT_TYPE (false, refp, SCM_ARG2, __FUNCTION__, "common refpoint"); + } + + return scm_from_double (sc->relative_coordinate (ref, a)); +} + - return ly_interval2scm ( sc->extent (ref, Axis (gh_scm2int (axis)))); +LY_DEFINE (ly_grob_parent, "ly:grob-parent", + 2, 0, 0, (SCM grob, SCM axis), + "Get the parent of @var{grob}. @var{axis} is 0 for the X-axis," + " 1@tie{}for the Y-axis.") +{ + Grob *sc = unsmob_grob (grob); + + LY_ASSERT_SMOB (Grob, grob, 1); + LY_ASSERT_TYPE (is_axis, axis, 2); + + Grob *par = sc->get_parent (Axis (scm_to_int (axis))); + return par ? par->self_scm () : SCM_EOL; +} + +LY_DEFINE (ly_grob_properties, "ly:grob-properties", + 1, 0, 0, (SCM grob), + "Get the mutable properties of @var{grob}.") +{ + Grob *g = unsmob_grob (grob); + + LY_ASSERT_SMOB (Grob, grob, 1); + + /* FIXME: uhg? copy/read only? */ + return g->mutable_property_alist_; } -LY_DEFINE (ly_get_parent, "ly-get-parent", 2, 0, 0, (SCM grob, SCM axis), - "Get the parent of @var{grob}. @var{axis} can be 0 for the X-axis, 1 -for the Y-axis.") +LY_DEFINE (ly_grob_basic_properties, "ly:grob-basic-properties", + 1, 0, 0, (SCM grob), + "Get the immutable properties of @var{grob}.") { - Grob * sc = unsmob_grob (grob); - SCM_ASSERT_TYPE(sc, grob, SCM_ARG1, __FUNCTION__, "grob"); - SCM_ASSERT_TYPE(ly_axis_p (axis), axis, SCM_ARG2, __FUNCTION__, "axis"); + Grob *g = unsmob_grob (grob); + + LY_ASSERT_SMOB (Grob, grob, 1); - Grob * par = sc->get_parent (Axis (gh_scm2int (axis))); - return par ? par->self_scm() : SCM_EOL; + /* FIXME: uhg? copy/read only? */ + return g->immutable_property_alist_; } -/* ly prefix? */ -LY_DEFINE (get_system, - "get-system", +LY_DEFINE (ly_grob_system, "ly:grob-system", 1, 0, 0, (SCM grob), - " -Return the System Grob of @var{grob}. -") + "Return the system grob of @var{grob}.") { Grob *me = unsmob_grob (grob); - SCM_ASSERT_TYPE (me, grob, SCM_ARG1, __FUNCTION__, "grob"); - - if (Grob *g = me->get_system ()) + + LY_ASSERT_SMOB (Grob, grob, 1); + + if (System *g = me->get_system ()) return g->self_scm (); - + return SCM_EOL; } -/* ly prefix? */ -LY_DEFINE (get_original, - "get-original", +LY_DEFINE (ly_grob_original, "ly:grob-original", 1, 0, 0, (SCM grob), - " -Return the original Grob of @var{grob} -") + "Return the unbroken original grob of @var{grob}.") { Grob *me = unsmob_grob (grob); - SCM_ASSERT_TYPE (me, grob, SCM_ARG1, __FUNCTION__, "grob"); - return me->original_ ? me->original_->self_scm () : me->self_scm (); + + LY_ASSERT_SMOB (Grob, grob, 1); + return me->original () ? me->original ()->self_scm () : me->self_scm (); } -/* ly prefix? spanner in name? */ -LY_DEFINE (get_broken_into, - "get-broken-into", 1, 0, 0, (SCM spanner), - " -Return broken-into list for @var{spanner}. -" -) +LY_DEFINE (ly_grob_suicide_x, "ly:grob-suicide!", + 1, 0, 0, (SCM grob), + "Kill @var{grob}.") { - /// Spanner *me = unsmob_spanner (spanner); - Spanner *me = dynamic_cast (unsmob_grob (spanner)); - SCM_ASSERT_TYPE (me, spanner, SCM_ARG1, __FUNCTION__, "spanner"); + Grob *me = unsmob_grob (grob); + + LY_ASSERT_SMOB (Grob, grob, 1); - SCM s = SCM_EOL; - for (int i = me->broken_intos_.size (); i; i--) - s = gh_cons (me->broken_intos_[i-1]->self_scm (), s); - return s; + me->suicide (); + return SCM_UNSPECIFIED; } +LY_DEFINE (ly_grob_translate_axis_x, "ly:grob-translate-axis!", + 3, 0, 0, (SCM grob, SCM d, SCM a), + "Translate @var{g} on axis@tie{}@var{a} over" + " distance@tie{}@var{d}.") +{ + Grob *me = unsmob_grob (grob); + + LY_ASSERT_SMOB (Grob, grob, 1); + LY_ASSERT_TYPE (scm_is_number, d, 2); + LY_ASSERT_TYPE (is_axis, a, 3); + + me->translate_axis (scm_to_double (d), Axis (scm_to_int (a))); + return SCM_UNSPECIFIED; +} + +LY_DEFINE (ly_grob_default_font, "ly:grob-default-font", + 1, 0, 0, (SCM grob), + "Return the default font for grob @var{gr}.") +{ + Grob *gr = unsmob_grob (grob); + + LY_ASSERT_SMOB (Grob, grob, 1); + + return Font_interface::get_default_font (gr)->self_scm (); +} + + +/* + TODO: consider swapping order, so we can do + + (grob-common-refpoint a b c d e) + */ +LY_DEFINE (ly_grob_common_refpoint, "ly:grob-common-refpoint", + 3, 0, 0, (SCM grob, SCM other, SCM axis), + "Find the common refpoint of @var{grob} and @var{other}" + " for @var{axis}.") +{ + + Grob *gr = unsmob_grob (grob); + + LY_ASSERT_SMOB (Grob, grob, 1); + LY_ASSERT_SMOB (Grob, other, 2); + + Grob *o = unsmob_grob (other); + + LY_ASSERT_TYPE (is_axis, axis, 3); + + Grob *refp = gr->common_refpoint (o, Axis (scm_to_int (axis))); + return refp ? refp->self_scm () : SCM_BOOL_F; +} + +LY_DEFINE (ly_grob_common_refpoint_of_array, "ly:grob-common-refpoint-of-array", + 3, 0, 0, (SCM grob, SCM others, SCM axis), + "Find the common refpoint of @var{grob} and @var{others}" + " (a grob-array) for @var{axis}.") +{ + Grob *gr = unsmob_grob (grob); + + LY_ASSERT_SMOB (Grob, grob, 1); + LY_ASSERT_SMOB (Grob_array, others, 2); + + Grob_array *ga = unsmob_grob_array (others); + LY_ASSERT_TYPE (is_axis, axis, 3); + + Grob *refp = common_refpoint_of_array (ga->array (), gr, Axis (scm_to_int (axis))); + return refp ? refp->self_scm () : SCM_BOOL_F; +}