Fixes Issue 1504, allowing feather beam line breaking.
[lilypond/patrick.git] / lily / grob-scheme.cc
blob9554883464f4d5c5d74d47cac737a286e42e41c4
1 /*
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 "warn.hh" // error ()
22 #include "item.hh"
23 #include "output-def.hh"
24 #include "system.hh"
25 #include "font-interface.hh"
26 #include "paper-score.hh"
27 #include "grob-array.hh"
29 LY_DEFINE (ly_grob_property_data, "ly:grob-property-data",
30 2, 0, 0, (SCM grob, SCM sym),
31 "Return the value for property @var{sym} of @var{grob},"
32 " but do not process callbacks.")
34 Grob *sc = unsmob_grob (grob);
36 LY_ASSERT_SMOB (Grob, grob, 1);
37 LY_ASSERT_TYPE (ly_is_symbol, sym, 2);
39 return sc->get_property_data (sym);
42 LY_DEFINE (ly_grob_set_property_x, "ly:grob-set-property!",
43 3, 0, 0, (SCM grob, SCM sym, SCM val),
44 "Set @var{sym} in grob @var{grob} to value @var{val}.")
46 Grob *sc = unsmob_grob (grob);
48 LY_ASSERT_SMOB (Grob, grob, 1);
49 LY_ASSERT_TYPE (ly_is_symbol, sym, 2);
51 if (!ly_is_procedure (val)
52 && !type_check_assignment (sym, val, ly_symbol2scm ("backend-type?")))
53 error ("typecheck failed");
55 sc->set_property (sym, val);
56 return SCM_UNSPECIFIED;
59 LY_DEFINE (ly_grob_set_nested_property_x, "ly:grob-set-nested-property!",
60 3, 0, 0, (SCM grob, SCM symlist, SCM val),
61 "Set nested property @var{symlist} in grob @var{grob} to value @var{val}.")
63 Grob *sc = unsmob_grob (grob);
65 LY_ASSERT_SMOB (Grob, grob, 1);
67 bool type_ok = ly_cheap_is_list (symlist);
69 if (type_ok)
70 for (SCM s = symlist; scm_is_pair (s) && type_ok; s = scm_cdr (s))
71 type_ok &= ly_is_symbol (scm_car (s));
73 SCM_ASSERT_TYPE (type_ok, symlist, SCM_ARG2, __FUNCTION__, "list of symbols");
75 set_nested_property (sc, symlist, val);
76 return SCM_UNSPECIFIED;
80 LY_DEFINE (ly_grob_property, "ly:grob-property",
81 2, 1, 0, (SCM grob, SCM sym, SCM val),
82 "Return the value for property @var{sym} of @var{grob}."
83 " If no value is found, return @var{val} or @code{'()}"
84 " if @var{val} is not specified.")
86 Grob *sc = unsmob_grob (grob);
88 LY_ASSERT_SMOB (Grob, grob, 1);
89 LY_ASSERT_TYPE (ly_is_symbol, sym, 2);
90 if (val == SCM_UNDEFINED)
91 val = SCM_EOL;
93 SCM retval = sc->internal_get_property (sym);
94 if (retval == SCM_EOL)
95 retval = val;
97 return retval;
101 LY_DEFINE (ly_grob_interfaces, "ly:grob-interfaces",
102 1, 0, 0, (SCM grob),
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);
127 LY_DEFINE (ly_grob_set_object_x, "ly:grob-set-object!",
128 3, 0, 0, (SCM grob, SCM sym, SCM val),
129 "Set @var{sym} in grob @var{grob} to value @var{val}.")
131 Grob *sc = unsmob_grob (grob);
133 LY_ASSERT_SMOB (Grob, grob, 1);
134 LY_ASSERT_TYPE (ly_is_symbol, sym, 2);
136 sc->set_object (sym, val);
137 return SCM_UNSPECIFIED;
140 /* TODO: make difference between scaled and unscalead variable in
141 calling (i.e different funcs.) */
142 LY_DEFINE (ly_grob_layout, "ly:grob-layout",
143 1, 0, 0, (SCM grob),
144 "Get @code{\\layout} definition from grob @var{grob}.")
146 Grob *sc = unsmob_grob (grob);
148 LY_ASSERT_SMOB (Grob, grob, 1);
150 return sc->layout ()->self_scm ();
153 LY_DEFINE (ly_grob_alist_chain, "ly:grob-alist-chain",
154 1, 1, 0, (SCM grob, SCM global),
155 "Get an alist chain for grob @var{grob}, with @var{global} as"
156 " the global default. If unspecified, @code{font-defaults}"
157 " from the layout block is taken.")
159 Grob *sc = unsmob_grob (grob);
161 LY_ASSERT_SMOB (Grob, grob, 1);
163 if (global == SCM_UNDEFINED)
165 global = sc->layout ()->lookup_variable (ly_symbol2scm ("font-defaults"));
166 if (global == SCM_UNDEFINED)
167 global = SCM_EOL;
170 return sc->get_property_alist_chain (global);
173 LY_DEFINE (ly_grob_extent, "ly:grob-extent",
174 3, 0, 0, (SCM grob, SCM refp, SCM axis),
175 "Get the extent in @var{axis} direction of @var{grob} relative to"
176 " the grob @var{refp}.")
178 Grob *sc = unsmob_grob (grob);
179 Grob *ref = unsmob_grob (refp);
182 LY_ASSERT_SMOB (Grob, grob, 1);
183 LY_ASSERT_SMOB (Grob, refp, 2);
184 LY_ASSERT_TYPE (is_axis, axis, 3);
186 Axis a = Axis (scm_to_int (axis));
189 if (ref->common_refpoint (sc, a) != ref)
191 // ugh. should use other error message
192 SCM_ASSERT_TYPE (false, refp, SCM_ARG2, __FUNCTION__, "common refpoint");
194 return ly_interval2scm (sc->extent (ref, a));
197 LY_DEFINE (ly_grob_robust_relative_extent, "ly:grob-robust-relative-extent",
198 3, 0, 0, (SCM grob, SCM refp, SCM axis),
199 "Get the extent in @var{axis} direction of @var{grob} relative to"
200 " the grob @var{refp}, or @code{(0,0)} if empty.")
202 Grob *sc = unsmob_grob (grob);
203 Grob *ref = unsmob_grob (refp);
206 LY_ASSERT_SMOB (Grob, grob, 1);
207 LY_ASSERT_SMOB (Grob, refp, 2);
208 LY_ASSERT_TYPE (is_axis, axis, 3);
210 Axis a = Axis (scm_to_int (axis));
212 if (ref->common_refpoint (sc, a) != ref)
214 // ugh. should use other error message
215 SCM_ASSERT_TYPE (false, refp, SCM_ARG2, __FUNCTION__, "common refpoint");
218 return ly_interval2scm (robust_relative_extent (sc, ref, a));
221 LY_DEFINE (ly_grob_relative_coordinate, "ly:grob-relative-coordinate",
222 3, 0, 0, (SCM grob, SCM refp, SCM axis),
223 "Get the coordinate in @var{axis} direction of @var{grob} relative"
224 " to the grob @var{refp}.")
226 Grob *sc = unsmob_grob (grob);
227 Grob *ref = unsmob_grob (refp);
230 LY_ASSERT_SMOB (Grob, grob, 1);
231 LY_ASSERT_SMOB (Grob, refp, 2);
232 LY_ASSERT_TYPE (is_axis, axis, 3);
234 Axis a = Axis (scm_to_int (axis));
237 if (ref->common_refpoint (sc, a) != ref)
239 // ugh. should use other error message
240 SCM_ASSERT_TYPE (false, refp, SCM_ARG2, __FUNCTION__, "common refpoint");
243 return scm_from_double (sc->relative_coordinate (ref, a));
247 LY_DEFINE (ly_grob_parent, "ly:grob-parent",
248 2, 0, 0, (SCM grob, SCM axis),
249 "Get the parent of @var{grob}. @var{axis} is 0 for the X-axis,"
250 " 1@tie{}for the Y-axis.")
252 Grob *sc = unsmob_grob (grob);
254 LY_ASSERT_SMOB (Grob, grob, 1);
255 LY_ASSERT_TYPE (is_axis, axis, 2);
257 Grob *par = sc->get_parent (Axis (scm_to_int (axis)));
258 return par ? par->self_scm () : SCM_EOL;
261 LY_DEFINE (ly_grob_set_parent_x, "ly:grob-set-parent!",
262 3, 0, 0, (SCM grob, SCM axis, SCM parent_grob),
263 "Set @var{parent-grob} the parent of grob @var{grob} in axis @var{axis}.")
265 Grob *gr = unsmob_grob (grob);
266 Grob *parent = unsmob_grob (parent_grob);
268 LY_ASSERT_SMOB (Grob, grob, 1);
269 LY_ASSERT_TYPE (is_axis, axis, 2);
270 LY_ASSERT_SMOB (Grob, parent_grob, 3);
272 Axis a = Axis (scm_to_int (axis));
273 gr->set_parent (parent, a);
274 return SCM_UNSPECIFIED;
277 LY_DEFINE (ly_grob_properties, "ly:grob-properties",
278 1, 0, 0, (SCM grob),
279 "Get the mutable properties of @var{grob}.")
281 Grob *g = unsmob_grob (grob);
283 LY_ASSERT_SMOB (Grob, grob, 1);
285 /* FIXME: uhg? copy/read only? */
286 return g->mutable_property_alist_;
289 LY_DEFINE (ly_grob_basic_properties, "ly:grob-basic-properties",
290 1, 0, 0, (SCM grob),
291 "Get the immutable properties of @var{grob}.")
293 Grob *g = unsmob_grob (grob);
295 LY_ASSERT_SMOB (Grob, grob, 1);
297 /* FIXME: uhg? copy/read only? */
298 return g->immutable_property_alist_;
301 LY_DEFINE (ly_grob_system, "ly:grob-system",
302 1, 0, 0, (SCM grob),
303 "Return the system grob of @var{grob}.")
305 Grob *me = unsmob_grob (grob);
307 LY_ASSERT_SMOB (Grob, grob, 1);
309 if (System *g = me->get_system ())
310 return g->self_scm ();
312 return SCM_EOL;
315 LY_DEFINE (ly_grob_original, "ly:grob-original",
316 1, 0, 0, (SCM grob),
317 "Return the unbroken original grob of @var{grob}.")
319 Grob *me = unsmob_grob (grob);
321 LY_ASSERT_SMOB (Grob, grob, 1);
322 return me->original () ? me->original ()->self_scm () : me->self_scm ();
326 LY_DEFINE (ly_grob_suicide_x, "ly:grob-suicide!",
327 1, 0, 0, (SCM grob),
328 "Kill @var{grob}.")
330 Grob *me = unsmob_grob (grob);
332 LY_ASSERT_SMOB (Grob, grob, 1);
334 me->suicide ();
335 return SCM_UNSPECIFIED;
338 LY_DEFINE (ly_grob_translate_axis_x, "ly:grob-translate-axis!",
339 3, 0, 0, (SCM grob, SCM d, SCM a),
340 "Translate @var{grob} on axis@tie{}@var{a} over"
341 " distance@tie{}@var{d}.")
343 Grob *me = unsmob_grob (grob);
345 LY_ASSERT_SMOB (Grob, grob, 1);
346 LY_ASSERT_TYPE (scm_is_number, d, 2);
347 LY_ASSERT_TYPE (is_axis, a, 3);
349 me->translate_axis (scm_to_double (d), Axis (scm_to_int (a)));
350 return SCM_UNSPECIFIED;
353 LY_DEFINE (ly_grob_default_font, "ly:grob-default-font",
354 1, 0, 0, (SCM grob),
355 "Return the default font for grob @var{grob}.")
357 Grob *gr = unsmob_grob (grob);
359 LY_ASSERT_SMOB (Grob, grob, 1);
361 return Font_interface::get_default_font (gr)->self_scm ();
366 TODO: consider swapping order, so we can do
368 (grob-common-refpoint a b c d e)
370 LY_DEFINE (ly_grob_common_refpoint, "ly:grob-common-refpoint",
371 3, 0, 0, (SCM grob, SCM other, SCM axis),
372 "Find the common refpoint of @var{grob} and @var{other}"
373 " for @var{axis}.")
376 Grob *gr = unsmob_grob (grob);
378 LY_ASSERT_SMOB (Grob, grob, 1);
379 LY_ASSERT_SMOB (Grob, other, 2);
381 Grob *o = unsmob_grob (other);
383 LY_ASSERT_TYPE (is_axis, axis, 3);
385 Grob *refp = gr->common_refpoint (o, Axis (scm_to_int (axis)));
386 return refp ? refp->self_scm () : SCM_BOOL_F;
389 LY_DEFINE (ly_grob_common_refpoint_of_array, "ly:grob-common-refpoint-of-array",
390 3, 0, 0, (SCM grob, SCM others, SCM axis),
391 "Find the common refpoint of @var{grob} and @var{others}"
392 " (a grob-array) for @var{axis}.")
394 Grob *gr = unsmob_grob (grob);
396 LY_ASSERT_SMOB (Grob, grob, 1);
397 LY_ASSERT_SMOB (Grob_array, others, 2);
399 Grob_array *ga = unsmob_grob_array (others);
400 LY_ASSERT_TYPE (is_axis, axis, 3);
402 Grob *refp = common_refpoint_of_array (ga->array (), gr, Axis (scm_to_int (axis)));
403 return refp ? refp->self_scm () : SCM_BOOL_F;
406 LY_DEFINE (ly_grob_chain_callback, "ly:grob-chain-callback",
407 3, 0, 0, (SCM grob, SCM proc, SCM sym),
408 "Find the callback that is stored as property"
409 " @var{sym} of grob @var{grob} and chain @var{proc}"
410 " to the head of this, meaning that it is called"
411 " using @var{grob} and the previous callback's result.")
413 Grob *gr = unsmob_grob (grob);
415 LY_ASSERT_SMOB (Grob, grob, 1);
416 LY_ASSERT_TYPE (ly_is_procedure, proc, 2);
417 LY_ASSERT_TYPE (ly_is_symbol, sym, 3);
419 chain_callback (gr, proc, sym);
420 return SCM_UNSPECIFIED;