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