]> git.donarmstrong.com Git - lilypond.git/blob - lily/grob-scheme.cc
ly:grob-set-property!: Don't crash if set to a simple closure.
[lilypond.git] / lily / grob-scheme.cc
1 /*
2   This file is part of LilyPond, the GNU music typesetter.
3
4   Copyright (C) 1998--2011 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 = ly_cheap_is_list (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   set_nested_property (sc, symlist, val);
78   return SCM_UNSPECIFIED;
79 }
80
81
82 LY_DEFINE (ly_grob_property, "ly:grob-property",
83            2, 1, 0, (SCM grob, SCM sym, SCM val),
84            "Return the value for property @var{sym} of @var{grob}."
85            "  If no value is found, return @var{val} or @code{'()}"
86            " if @var{val} is not specified.")
87 {
88   Grob *sc = unsmob_grob (grob);
89
90   LY_ASSERT_SMOB (Grob, grob, 1);
91   LY_ASSERT_TYPE (ly_is_symbol, sym, 2);
92   if (val == SCM_UNDEFINED)
93     val = SCM_EOL;
94
95   SCM retval = sc->internal_get_property (sym);
96   if (retval == SCM_EOL)
97     retval = val;
98   
99   return retval;
100 }
101
102
103 LY_DEFINE (ly_grob_interfaces, "ly:grob-interfaces",
104            1, 0, 0, (SCM grob),
105            "Return the interfaces list of grob @var{grob}.")
106 {
107   Grob *sc = unsmob_grob (grob);
108    
109   LY_ASSERT_SMOB (Grob, grob, 1);
110
111   return sc->interfaces ();
112 }
113
114 LY_DEFINE (ly_grob_object, "ly:grob-object",
115            2, 0, 0, (SCM grob, SCM sym),
116            "Return the value of a pointer in grob @var{grob} of property"
117            " @var{sym}.  It returns @code{'()} (end-of-list) if @var{sym}"
118            " is undefined in @var{grob}.")
119 {
120   Grob *sc = unsmob_grob (grob);
121    
122   LY_ASSERT_SMOB (Grob, grob, 1);
123   LY_ASSERT_TYPE (ly_is_symbol, sym, 2);
124
125   return sc->internal_get_object (sym);
126 }
127
128
129 LY_DEFINE (ly_grob_set_object_x, "ly:grob-set-object!",
130            3, 0, 0, (SCM grob, SCM sym, SCM val),
131            "Set @var{sym} in grob @var{grob} to value @var{val}.")
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
138   sc->set_object (sym, val);
139   return SCM_UNSPECIFIED;
140 }
141
142 /* TODO: make difference between scaled and unscalead variable in
143    calling (i.e different funcs.) */
144 LY_DEFINE (ly_grob_layout, "ly:grob-layout",
145            1, 0, 0, (SCM grob),
146            "Get @code{\\layout} definition from grob @var{grob}.")
147 {
148   Grob *sc = unsmob_grob (grob);
149    
150   LY_ASSERT_SMOB (Grob, grob, 1);
151
152   return sc->layout ()->self_scm ();
153 }
154
155 LY_DEFINE (ly_grob_alist_chain, "ly:grob-alist-chain",
156            1, 1, 0, (SCM grob, SCM global),
157            "Get an alist chain for grob @var{grob}, with @var{global} as"
158            " the global default.  If unspecified, @code{font-defaults}"
159            " from the layout block is taken.")
160 {
161   Grob *sc = unsmob_grob (grob);
162    
163   LY_ASSERT_SMOB (Grob, grob, 1);
164
165   if (global == SCM_UNDEFINED)
166     {
167       global = sc->layout ()->lookup_variable (ly_symbol2scm ("font-defaults"));
168       if (global == SCM_UNDEFINED)
169         global = SCM_EOL;
170     }
171
172   return sc->get_property_alist_chain (global);
173 }
174
175 LY_DEFINE (ly_grob_extent, "ly:grob-extent",
176            3, 0, 0, (SCM grob, SCM refp, SCM axis),
177            "Get the extent in @var{axis} direction of @var{grob} relative to"
178            " the grob @var{refp}.")
179 {
180   Grob *sc = unsmob_grob (grob);
181   Grob *ref = unsmob_grob (refp);
182   
183    
184   LY_ASSERT_SMOB (Grob, grob, 1);
185   LY_ASSERT_SMOB (Grob, refp, 2);
186   LY_ASSERT_TYPE (is_axis, axis, 3);
187
188   Axis a = Axis (scm_to_int (axis));
189
190     
191   if (ref->common_refpoint (sc, a) != ref)
192     {
193       // ugh. should use other error message
194       SCM_ASSERT_TYPE (false, refp, SCM_ARG2, __FUNCTION__, "common refpoint");
195     }
196   return ly_interval2scm (sc->extent (ref, a));
197 }
198
199 LY_DEFINE (ly_grob_robust_relative_extent, "ly:grob-robust-relative-extent",
200            3, 0, 0, (SCM grob, SCM refp, SCM axis),
201            "Get the extent in @var{axis} direction of @var{grob} relative to"
202            " the grob @var{refp}, or @code{(0,0)} if empty.")
203 {
204   Grob *sc = unsmob_grob (grob);
205   Grob *ref = unsmob_grob (refp);
206   
207    
208   LY_ASSERT_SMOB (Grob, grob, 1);
209   LY_ASSERT_SMOB (Grob, refp, 2);
210   LY_ASSERT_TYPE (is_axis, axis, 3);
211
212   Axis a = Axis (scm_to_int (axis));
213     
214   if (ref->common_refpoint (sc, a) != ref)
215     {
216       // ugh. should use other error message
217       SCM_ASSERT_TYPE (false, refp, SCM_ARG2, __FUNCTION__, "common refpoint");
218     }
219
220   return ly_interval2scm (robust_relative_extent (sc, ref, a));
221 }
222
223 LY_DEFINE (ly_grob_relative_coordinate, "ly:grob-relative-coordinate",
224            3, 0, 0, (SCM grob, SCM refp, SCM axis),
225            "Get the coordinate in @var{axis} direction of @var{grob} relative"
226            " to the grob @var{refp}.")
227 {
228   Grob *sc = unsmob_grob (grob);
229   Grob *ref = unsmob_grob (refp);
230   
231    
232   LY_ASSERT_SMOB (Grob, grob, 1);
233   LY_ASSERT_SMOB (Grob, refp, 2);
234   LY_ASSERT_TYPE (is_axis, axis, 3);
235
236   Axis a = Axis (scm_to_int (axis));
237
238     
239   if (ref->common_refpoint (sc, a) != ref)
240     {
241       // ugh. should use other error message
242       SCM_ASSERT_TYPE (false, refp, SCM_ARG2, __FUNCTION__, "common refpoint");
243     }
244
245   return scm_from_double (sc->relative_coordinate (ref, a));
246 }
247
248
249 LY_DEFINE (ly_grob_parent, "ly:grob-parent",
250            2, 0, 0, (SCM grob, SCM axis),
251            "Get the parent of @var{grob}.  @var{axis} is 0 for the X-axis,"
252            " 1@tie{}for the Y-axis.")
253 {
254   Grob *sc = unsmob_grob (grob);
255    
256   LY_ASSERT_SMOB (Grob, grob, 1);
257   LY_ASSERT_TYPE (is_axis, axis, 2);
258
259   Grob *par = sc->get_parent (Axis (scm_to_int (axis)));
260   return par ? par->self_scm () : SCM_EOL;
261 }
262
263 LY_DEFINE (ly_grob_set_parent_x, "ly:grob-set-parent!",
264            3, 0, 0, (SCM grob, SCM axis, SCM parent_grob),
265            "Set @var{parent-grob} the parent of grob @var{grob} in axis @var{axis}.")
266 {
267   Grob *gr = unsmob_grob (grob);
268   Grob *parent = unsmob_grob (parent_grob);
269
270   LY_ASSERT_SMOB (Grob, grob, 1);
271   LY_ASSERT_TYPE (is_axis, axis, 2);
272   LY_ASSERT_SMOB (Grob, parent_grob, 3);
273
274   Axis a = Axis (scm_to_int (axis));
275   gr->set_parent (parent, a);
276   return SCM_UNSPECIFIED;
277 }
278
279 LY_DEFINE (ly_grob_properties, "ly:grob-properties",
280            1, 0, 0, (SCM grob),
281            "Get the mutable properties of @var{grob}.")
282 {
283   Grob *g = unsmob_grob (grob);
284    
285   LY_ASSERT_SMOB (Grob, grob, 1);
286
287   /* FIXME: uhg? copy/read only? */
288   return g->mutable_property_alist_;
289 }
290
291 LY_DEFINE (ly_grob_basic_properties, "ly:grob-basic-properties",
292            1, 0, 0, (SCM grob),
293            "Get the immutable properties of @var{grob}.")
294 {
295   Grob *g = unsmob_grob (grob);
296    
297   LY_ASSERT_SMOB (Grob, grob, 1);
298
299   /* FIXME: uhg? copy/read only? */
300   return g->immutable_property_alist_;
301 }
302
303 LY_DEFINE (ly_grob_system, "ly:grob-system",
304            1, 0, 0, (SCM grob),
305            "Return the system grob of @var{grob}.")
306 {
307   Grob *me = unsmob_grob (grob);
308    
309   LY_ASSERT_SMOB (Grob, grob, 1);
310
311   if (System *g = me->get_system ())
312     return g->self_scm ();
313
314   return SCM_EOL;
315 }
316
317 LY_DEFINE (ly_grob_original, "ly:grob-original",
318            1, 0, 0, (SCM grob),
319            "Return the unbroken original grob of @var{grob}.")
320 {
321   Grob *me = unsmob_grob (grob);
322    
323   LY_ASSERT_SMOB (Grob, grob, 1);
324   return me->original () ? me->original ()->self_scm () : me->self_scm ();
325 }
326
327
328 LY_DEFINE (ly_grob_suicide_x, "ly:grob-suicide!",
329            1, 0, 0, (SCM grob),
330            "Kill @var{grob}.")
331 {
332   Grob *me = unsmob_grob (grob);
333    
334   LY_ASSERT_SMOB (Grob, grob, 1);
335
336   me->suicide ();
337   return SCM_UNSPECIFIED;
338 }
339
340 LY_DEFINE (ly_grob_translate_axis_x, "ly:grob-translate-axis!",
341            3, 0, 0, (SCM grob, SCM d, SCM a),
342            "Translate @var{grob} on axis@tie{}@var{a} over"
343            " distance@tie{}@var{d}.")
344 {
345   Grob *me = unsmob_grob (grob);
346    
347   LY_ASSERT_SMOB (Grob, grob, 1);
348   LY_ASSERT_TYPE (scm_is_number, d, 2);
349   LY_ASSERT_TYPE (is_axis, a, 3);
350
351   me->translate_axis (scm_to_double (d), Axis (scm_to_int (a)));
352   return SCM_UNSPECIFIED;
353 }
354
355 LY_DEFINE (ly_grob_default_font, "ly:grob-default-font",
356            1, 0, 0, (SCM grob),
357            "Return the default font for grob @var{grob}.")
358 {
359   Grob *gr = unsmob_grob (grob);
360    
361   LY_ASSERT_SMOB (Grob, grob, 1);
362
363   return Font_interface::get_default_font (gr)->self_scm ();
364 }
365
366
367 /*
368   TODO: consider swapping order, so we can do
369
370   (grob-common-refpoint a b c d e)
371  */
372 LY_DEFINE (ly_grob_common_refpoint, "ly:grob-common-refpoint",
373            3, 0, 0,  (SCM grob, SCM other, SCM axis),
374            "Find the common refpoint of @var{grob} and @var{other}"
375            " for @var{axis}.")
376 {
377   
378   Grob *gr = unsmob_grob (grob);
379    
380   LY_ASSERT_SMOB (Grob, grob, 1);
381   LY_ASSERT_SMOB (Grob, other, 2);
382
383   Grob *o = unsmob_grob (other);
384
385   LY_ASSERT_TYPE (is_axis, axis, 3);
386
387   Grob *refp = gr->common_refpoint (o,  Axis (scm_to_int (axis)));
388   return refp ? refp->self_scm () : SCM_BOOL_F;
389 }
390
391 LY_DEFINE (ly_grob_common_refpoint_of_array, "ly:grob-common-refpoint-of-array",
392            3, 0, 0,  (SCM grob, SCM others, SCM axis),
393            "Find the common refpoint of @var{grob} and @var{others}"
394            " (a grob-array) for @var{axis}.")
395 {
396   Grob *gr = unsmob_grob (grob);
397    
398   LY_ASSERT_SMOB (Grob, grob, 1);
399   LY_ASSERT_SMOB (Grob_array, others, 2);
400
401   Grob_array *ga = unsmob_grob_array (others);
402   LY_ASSERT_TYPE (is_axis, axis, 3);
403
404   Grob *refp = common_refpoint_of_array (ga->array (), gr, Axis (scm_to_int (axis)));
405   return refp ? refp->self_scm () : SCM_BOOL_F;
406 }
407
408 LY_DEFINE (ly_grob_chain_callback, "ly:grob-chain-callback",
409            3, 0, 0, (SCM grob, SCM proc, SCM sym),
410            "Find the callback that is stored as property"
411            " @var{sym} of grob @var{grob} and chain @var{proc}"
412            " to the head of this, meaning that it is called"
413            " using @var{grob} and the previous callback's result.")
414 {
415   Grob *gr = unsmob_grob (grob);
416
417   LY_ASSERT_SMOB (Grob, grob, 1);
418   LY_ASSERT_TYPE (ly_is_procedure, proc, 2);
419   LY_ASSERT_TYPE (ly_is_symbol, sym, 3);
420
421   chain_callback (gr, proc, sym);
422   return SCM_UNSPECIFIED;
423 }