]> git.donarmstrong.com Git - lilypond.git/blob - lily/grob-scheme.cc
Issue 2976: Allow ly:grob-set-nested-property! to set a single (unnested) property
[lilypond.git] / lily / grob-scheme.cc
1 /*
2   This file is part of LilyPond, the GNU music typesetter.
3
4   Copyright (C) 1998--2012 Jan Nieuwenhuizen <janneke@gnu.org>
5   Han-Wen Nienhuys <hanwen@xs4all.nl>
6
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.
11
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.
16
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/>.
19 */
20
21 #include "font-interface.hh"
22 #include "grob-array.hh"
23 #include "item.hh"
24 #include "output-def.hh"
25 #include "paper-score.hh"
26 #include "simple-closure.hh"
27 #include "system.hh"
28 #include "warn.hh"              // error ()
29
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.")
34 {
35   Grob *sc = unsmob_grob (grob);
36
37   LY_ASSERT_SMOB (Grob, grob, 1);
38   LY_ASSERT_TYPE (ly_is_symbol, sym, 2);
39
40   return sc->get_property_data (sym);
41 }
42
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}.")
46 {
47   Grob *sc = unsmob_grob (grob);
48
49   LY_ASSERT_SMOB (Grob, grob, 1);
50   LY_ASSERT_TYPE (ly_is_symbol, sym, 2);
51
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");
56
57   sc->set_property (sym, val);
58   return SCM_UNSPECIFIED;
59 }
60
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}.")
64 {
65   Grob *sc = unsmob_grob (grob);
66
67   LY_ASSERT_SMOB (Grob, grob, 1);
68
69   bool type_ok = scm_is_pair (symlist);
70
71   if (type_ok)
72     for (SCM s = symlist; scm_is_pair (s) && type_ok; s = scm_cdr (s))
73       type_ok &= ly_is_symbol (scm_car (s));
74
75   SCM_ASSERT_TYPE (type_ok, symlist, SCM_ARG2, __FUNCTION__, "list of symbols");
76
77   if (scm_is_pair (scm_cdr (symlist)))
78     set_nested_property (sc, symlist, val);
79   else
80     ly_grob_set_property_x (grob, scm_car (symlist), val);
81   return SCM_UNSPECIFIED;
82 }
83
84 LY_DEFINE (ly_grob_pure_property, "ly:grob-pure-property",
85            4, 1, 0, (SCM grob, SCM sym, SCM beg, SCM end, SCM val),
86            "Return the pure value for property @var{sym} of @var{grob}."
87            "  If no value is found, return @var{val} or @code{'()}"
88            " if @var{val} is not specified.")
89 {
90   Grob *sc = unsmob_grob (grob);
91
92   LY_ASSERT_SMOB (Grob, grob, 1);
93   LY_ASSERT_TYPE (ly_is_symbol, sym, 2);
94   LY_ASSERT_TYPE (scm_is_integer, beg, 3);
95   LY_ASSERT_TYPE (scm_is_integer, end, 4);
96   if (val == SCM_UNDEFINED)
97     val = SCM_EOL;
98
99   SCM retval = sc->internal_get_pure_property (sym, scm_to_int (beg), scm_to_int (end));
100   if (retval == SCM_EOL)
101     retval = val;
102
103   return retval;
104 }
105
106 LY_DEFINE (ly_grob_pure_height, "ly:grob-pure-height",
107            4, 1, 0, (SCM grob, SCM refp, SCM beg, SCM end, SCM val),
108            "Return the pure height of @var{grob} given refpoint @var{refp}."
109            "  If no value is found, return @var{val} or @code{'()}"
110            " if @var{val} is not specified.")
111 {
112   Grob *sc = unsmob_grob (grob);
113   Grob *ref = unsmob_grob (refp);
114
115   LY_ASSERT_SMOB (Grob, grob, 1);
116   LY_ASSERT_SMOB (Grob, refp, 2);
117   LY_ASSERT_TYPE (scm_is_integer, beg, 3);
118   LY_ASSERT_TYPE (scm_is_integer, end, 4);
119   if (val == SCM_UNDEFINED)
120     val = SCM_EOL;
121
122   Interval retval = sc->pure_height (ref, scm_to_int (beg), scm_to_int (end));
123
124   return ly_interval2scm (retval);
125 }
126
127 LY_DEFINE (ly_grob_property, "ly:grob-property",
128            2, 1, 0, (SCM grob, SCM sym, SCM val),
129            "Return the value for property @var{sym} of @var{grob}."
130            "  If no value is found, return @var{val} or @code{'()}"
131            " if @var{val} is not specified.")
132 {
133   Grob *sc = unsmob_grob (grob);
134
135   LY_ASSERT_SMOB (Grob, grob, 1);
136   LY_ASSERT_TYPE (ly_is_symbol, sym, 2);
137   if (val == SCM_UNDEFINED)
138     val = SCM_EOL;
139
140   SCM retval = sc->internal_get_property (sym);
141   if (retval == SCM_EOL)
142     retval = val;
143
144   return retval;
145 }
146
147 LY_DEFINE (ly_grob_interfaces, "ly:grob-interfaces",
148            1, 0, 0, (SCM grob),
149            "Return the interfaces list of grob @var{grob}.")
150 {
151   Grob *sc = unsmob_grob (grob);
152
153   LY_ASSERT_SMOB (Grob, grob, 1);
154
155   return sc->interfaces ();
156 }
157
158 LY_DEFINE (ly_grob_object, "ly:grob-object",
159            2, 0, 0, (SCM grob, SCM sym),
160            "Return the value of a pointer in grob @var{grob} of property"
161            " @var{sym}.  It returns @code{'()} (end-of-list) if @var{sym}"
162            " is undefined in @var{grob}.")
163 {
164   Grob *sc = unsmob_grob (grob);
165
166   LY_ASSERT_SMOB (Grob, grob, 1);
167   LY_ASSERT_TYPE (ly_is_symbol, sym, 2);
168
169   return sc->internal_get_object (sym);
170 }
171
172 LY_DEFINE (ly_grob_set_object_x, "ly:grob-set-object!",
173            3, 0, 0, (SCM grob, SCM sym, SCM val),
174            "Set @var{sym} in grob @var{grob} to value @var{val}.")
175 {
176   Grob *sc = unsmob_grob (grob);
177
178   LY_ASSERT_SMOB (Grob, grob, 1);
179   LY_ASSERT_TYPE (ly_is_symbol, sym, 2);
180
181   sc->set_object (sym, val);
182   return SCM_UNSPECIFIED;
183 }
184
185 /* TODO: make difference between scaled and unscalead variable in
186    calling (i.e different funcs.) */
187 LY_DEFINE (ly_grob_layout, "ly:grob-layout",
188            1, 0, 0, (SCM grob),
189            "Get @code{\\layout} definition from grob @var{grob}.")
190 {
191   Grob *sc = unsmob_grob (grob);
192
193   LY_ASSERT_SMOB (Grob, grob, 1);
194
195   return sc->layout ()->self_scm ();
196 }
197
198 LY_DEFINE (ly_grob_alist_chain, "ly:grob-alist-chain",
199            1, 1, 0, (SCM grob, SCM global),
200            "Get an alist chain for grob @var{grob}, with @var{global} as"
201            " the global default.  If unspecified, @code{font-defaults}"
202            " from the layout block is taken.")
203 {
204   Grob *sc = unsmob_grob (grob);
205
206   LY_ASSERT_SMOB (Grob, grob, 1);
207
208   if (global == SCM_UNDEFINED)
209     {
210       global = sc->layout ()->lookup_variable (ly_symbol2scm ("font-defaults"));
211       if (global == SCM_UNDEFINED)
212         global = SCM_EOL;
213     }
214
215   return sc->get_property_alist_chain (global);
216 }
217
218 LY_DEFINE (ly_grob_extent, "ly:grob-extent",
219            3, 0, 0, (SCM grob, SCM refp, SCM axis),
220            "Get the extent in @var{axis} direction of @var{grob} relative to"
221            " the grob @var{refp}.")
222 {
223   Grob *sc = unsmob_grob (grob);
224   Grob *ref = unsmob_grob (refp);
225
226   LY_ASSERT_SMOB (Grob, grob, 1);
227   LY_ASSERT_SMOB (Grob, refp, 2);
228   LY_ASSERT_TYPE (is_axis, axis, 3);
229
230   Axis a = Axis (scm_to_int (axis));
231
232   if (ref->common_refpoint (sc, a) != ref)
233     {
234       // ugh. should use other error message
235       SCM_ASSERT_TYPE (false, refp, SCM_ARG2, __FUNCTION__, "common refpoint");
236     }
237   return ly_interval2scm (sc->extent (ref, a));
238 }
239
240 LY_DEFINE (ly_grob_robust_relative_extent, "ly:grob-robust-relative-extent",
241            3, 0, 0, (SCM grob, SCM refp, SCM axis),
242            "Get the extent in @var{axis} direction of @var{grob} relative to"
243            " the grob @var{refp}, or @code{(0,0)} if empty.")
244 {
245   Grob *sc = unsmob_grob (grob);
246   Grob *ref = unsmob_grob (refp);
247
248   LY_ASSERT_SMOB (Grob, grob, 1);
249   LY_ASSERT_SMOB (Grob, refp, 2);
250   LY_ASSERT_TYPE (is_axis, axis, 3);
251
252   Axis a = Axis (scm_to_int (axis));
253
254   if (ref->common_refpoint (sc, a) != ref)
255     {
256       // ugh. should use other error message
257       SCM_ASSERT_TYPE (false, refp, SCM_ARG2, __FUNCTION__, "common refpoint");
258     }
259
260   return ly_interval2scm (robust_relative_extent (sc, ref, a));
261 }
262
263 LY_DEFINE (ly_grob_relative_coordinate, "ly:grob-relative-coordinate",
264            3, 0, 0, (SCM grob, SCM refp, SCM axis),
265            "Get the coordinate in @var{axis} direction of @var{grob} relative"
266            " to the grob @var{refp}.")
267 {
268   Grob *sc = unsmob_grob (grob);
269   Grob *ref = unsmob_grob (refp);
270
271   LY_ASSERT_SMOB (Grob, grob, 1);
272   LY_ASSERT_SMOB (Grob, refp, 2);
273   LY_ASSERT_TYPE (is_axis, axis, 3);
274
275   Axis a = Axis (scm_to_int (axis));
276
277   if (ref->common_refpoint (sc, a) != ref)
278     {
279       // ugh. should use other error message
280       SCM_ASSERT_TYPE (false, refp, SCM_ARG2, __FUNCTION__, "common refpoint");
281     }
282
283   return scm_from_double (sc->relative_coordinate (ref, a));
284 }
285
286 LY_DEFINE (ly_grob_parent, "ly:grob-parent",
287            2, 0, 0, (SCM grob, SCM axis),
288            "Get the parent of @var{grob}.  @var{axis} is 0 for the X-axis,"
289            " 1@tie{}for the Y-axis.")
290 {
291   Grob *sc = unsmob_grob (grob);
292
293   LY_ASSERT_SMOB (Grob, grob, 1);
294   LY_ASSERT_TYPE (is_axis, axis, 2);
295
296   Grob *par = sc->get_parent (Axis (scm_to_int (axis)));
297   return par ? par->self_scm () : SCM_EOL;
298 }
299
300 LY_DEFINE (ly_grob_set_parent_x, "ly:grob-set-parent!",
301            3, 0, 0, (SCM grob, SCM axis, SCM parent_grob),
302            "Set @var{parent-grob} the parent of grob @var{grob} in axis @var{axis}.")
303 {
304   Grob *gr = unsmob_grob (grob);
305   Grob *parent = unsmob_grob (parent_grob);
306
307   LY_ASSERT_SMOB (Grob, grob, 1);
308   LY_ASSERT_TYPE (is_axis, axis, 2);
309   LY_ASSERT_SMOB (Grob, parent_grob, 3);
310
311   Axis a = Axis (scm_to_int (axis));
312   gr->set_parent (parent, a);
313   return SCM_UNSPECIFIED;
314 }
315
316 LY_DEFINE (ly_grob_properties, "ly:grob-properties",
317            1, 0, 0, (SCM grob),
318            "Get the mutable properties of @var{grob}.")
319 {
320   Grob *g = unsmob_grob (grob);
321
322   LY_ASSERT_SMOB (Grob, grob, 1);
323
324   /* FIXME: uhg? copy/read only? */
325   return g->mutable_property_alist_;
326 }
327
328 LY_DEFINE (ly_grob_basic_properties, "ly:grob-basic-properties",
329            1, 0, 0, (SCM grob),
330            "Get the immutable properties of @var{grob}.")
331 {
332   Grob *g = unsmob_grob (grob);
333
334   LY_ASSERT_SMOB (Grob, grob, 1);
335
336   /* FIXME: uhg? copy/read only? */
337   return g->immutable_property_alist_;
338 }
339
340 LY_DEFINE (ly_grob_system, "ly:grob-system",
341            1, 0, 0, (SCM grob),
342            "Return the system grob of @var{grob}.")
343 {
344   Grob *me = unsmob_grob (grob);
345
346   LY_ASSERT_SMOB (Grob, grob, 1);
347
348   if (System *g = me->get_system ())
349     return g->self_scm ();
350
351   return SCM_EOL;
352 }
353
354 LY_DEFINE (ly_grob_original, "ly:grob-original",
355            1, 0, 0, (SCM grob),
356            "Return the unbroken original grob of @var{grob}.")
357 {
358   Grob *me = unsmob_grob (grob);
359
360   LY_ASSERT_SMOB (Grob, grob, 1);
361   return me->original () ? me->original ()->self_scm () : me->self_scm ();
362 }
363
364 LY_DEFINE (ly_grob_suicide_x, "ly:grob-suicide!",
365            1, 0, 0, (SCM grob),
366            "Kill @var{grob}.")
367 {
368   Grob *me = unsmob_grob (grob);
369
370   LY_ASSERT_SMOB (Grob, grob, 1);
371
372   me->suicide ();
373   return SCM_UNSPECIFIED;
374 }
375
376 LY_DEFINE (ly_grob_translate_axis_x, "ly:grob-translate-axis!",
377            3, 0, 0, (SCM grob, SCM d, SCM a),
378            "Translate @var{grob} on axis@tie{}@var{a} over"
379            " distance@tie{}@var{d}.")
380 {
381   Grob *me = unsmob_grob (grob);
382
383   LY_ASSERT_SMOB (Grob, grob, 1);
384   LY_ASSERT_TYPE (scm_is_number, d, 2);
385   LY_ASSERT_TYPE (is_axis, a, 3);
386
387   me->translate_axis (scm_to_double (d), Axis (scm_to_int (a)));
388   return SCM_UNSPECIFIED;
389 }
390
391 LY_DEFINE (ly_grob_default_font, "ly:grob-default-font",
392            1, 0, 0, (SCM grob),
393            "Return the default font for grob @var{grob}.")
394 {
395   Grob *gr = unsmob_grob (grob);
396
397   LY_ASSERT_SMOB (Grob, grob, 1);
398
399   return Font_interface::get_default_font (gr)->self_scm ();
400 }
401
402 /*
403   TODO: consider swapping order, so we can do
404
405   (grob-common-refpoint a b c d e)
406  */
407 LY_DEFINE (ly_grob_common_refpoint, "ly:grob-common-refpoint",
408            3, 0, 0, (SCM grob, SCM other, SCM axis),
409            "Find the common refpoint of @var{grob} and @var{other}"
410            " for @var{axis}.")
411 {
412
413   Grob *gr = unsmob_grob (grob);
414
415   LY_ASSERT_SMOB (Grob, grob, 1);
416   LY_ASSERT_SMOB (Grob, other, 2);
417
418   Grob *o = unsmob_grob (other);
419
420   LY_ASSERT_TYPE (is_axis, axis, 3);
421
422   Grob *refp = gr->common_refpoint (o, Axis (scm_to_int (axis)));
423   return refp ? refp->self_scm () : SCM_BOOL_F;
424 }
425
426 LY_DEFINE (ly_grob_common_refpoint_of_array, "ly:grob-common-refpoint-of-array",
427            3, 0, 0, (SCM grob, SCM others, SCM axis),
428            "Find the common refpoint of @var{grob} and @var{others}"
429            " (a grob-array) for @var{axis}.")
430 {
431   Grob *gr = unsmob_grob (grob);
432
433   LY_ASSERT_SMOB (Grob, grob, 1);
434   LY_ASSERT_SMOB (Grob_array, others, 2);
435
436   Grob_array *ga = unsmob_grob_array (others);
437   LY_ASSERT_TYPE (is_axis, axis, 3);
438
439   Grob *refp = common_refpoint_of_array (ga->array (), gr, Axis (scm_to_int (axis)));
440   return refp ? refp->self_scm () : SCM_BOOL_F;
441 }
442
443 LY_DEFINE (ly_grob_chain_callback, "ly:grob-chain-callback",
444            3, 0, 0, (SCM grob, SCM proc, SCM sym),
445            "Find the callback that is stored as property"
446            " @var{sym} of grob @var{grob} and chain @var{proc}"
447            " to the head of this, meaning that it is called"
448            " using @var{grob} and the previous callback's result.")
449 {
450   Grob *gr = unsmob_grob (grob);
451
452   LY_ASSERT_SMOB (Grob, grob, 1);
453   LY_ASSERT_TYPE (ly_is_procedure, proc, 2);
454   LY_ASSERT_TYPE (ly_is_symbol, sym, 3);
455
456   chain_callback (gr, proc, sym);
457   return SCM_UNSPECIFIED;
458 }
459
460 LY_DEFINE (ly_grob_vertical_less_p, "ly:grob-vertical<?",
461            2, 0, 0, (SCM a, SCM b),
462            "Does @var{a} lie above @var{b} on the page?")
463 {
464   LY_ASSERT_SMOB (Grob, a, 1);
465   LY_ASSERT_SMOB (Grob, b, 2);
466
467   Grob *ga = unsmob_grob (a);
468   Grob *gb = unsmob_grob (b);
469
470   return ly_bool2scm (Grob::vertical_less (ga, gb));
471 }
472
473 LY_DEFINE (ly_grob_get_vertical_axis_group_index, "ly:grob-get-vertical-axis-group-index",
474            1, 0, 0, (SCM grob),
475            "Get the index of the vertical axis group the grob @var{grob} belongs to;"
476            " return @code{-1} if none is found.")
477 {
478   Grob *gr = unsmob_grob (grob);
479
480   LY_ASSERT_SMOB (Grob, grob, 1);
481
482   return scm_from_int (Grob::get_vertical_axis_group_index (gr));
483 }