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