]> 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--2010 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 "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"
28
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.")
33 {
34   Grob *sc = unsmob_grob (grob);
35
36   LY_ASSERT_SMOB (Grob, grob, 1);
37   LY_ASSERT_TYPE (ly_is_symbol, sym, 2);
38
39   return sc->get_property_data (sym);
40 }
41
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}.")
45 {
46   Grob *sc = unsmob_grob (grob);
47  
48   LY_ASSERT_SMOB (Grob, grob, 1);
49   LY_ASSERT_TYPE (ly_is_symbol, sym, 2);
50
51   if (!ly_is_procedure (val)
52       && !type_check_assignment (sym, val, ly_symbol2scm ("backend-type?")))
53     error ("typecheck failed");
54
55   sc->set_property (sym, val);
56   return SCM_UNSPECIFIED;
57 }
58
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}.")
62 {
63   Grob *sc = unsmob_grob (grob);
64
65   LY_ASSERT_SMOB (Grob, grob, 1);
66
67   bool type_ok = ly_cheap_is_list (symlist);
68
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));
72
73   SCM_ASSERT_TYPE (type_ok, symlist, SCM_ARG2, __FUNCTION__, "list of symbols");
74
75   set_nested_property (sc, symlist, val);
76   return SCM_UNSPECIFIED;
77 }
78
79
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.")
85 {
86   Grob *sc = unsmob_grob (grob);
87
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;
92
93   SCM retval = sc->internal_get_property (sym);
94   if (retval == SCM_EOL)
95     retval = val;
96   
97   return retval;
98 }
99
100
101 LY_DEFINE (ly_grob_interfaces, "ly:grob-interfaces",
102            1, 0, 0, (SCM grob),
103            "Return the interfaces list of grob @var{grob}.")
104 {
105   Grob *sc = unsmob_grob (grob);
106    
107   LY_ASSERT_SMOB (Grob, grob, 1);
108
109   return sc->interfaces ();
110 }
111
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@tie{}@var{g} of property"
115            " @var{sym}.  It returns @code{'()} (end-of-list) if @var{sym}"
116            " is undefined in@tie{}@var{g}.")
117 {
118   Grob *sc = unsmob_grob (grob);
119    
120   LY_ASSERT_SMOB (Grob, grob, 1);
121   LY_ASSERT_TYPE (ly_is_symbol, sym, 2);
122
123   return sc->internal_get_object (sym);
124 }
125
126
127
128 /* TODO: make difference between scaled and unscalead variable in
129    calling (i.e different funcs.) */
130 LY_DEFINE (ly_grob_layout, "ly:grob-layout",
131            1, 0, 0, (SCM grob),
132            "Get @code{\\layout} definition from grob @var{grob}.")
133 {
134   Grob *sc = unsmob_grob (grob);
135    
136   LY_ASSERT_SMOB (Grob, grob, 1);
137
138   return sc->layout ()->self_scm ();
139 }
140
141 LY_DEFINE (ly_grob_alist_chain, "ly:grob-alist-chain",
142            1, 1, 0, (SCM grob, SCM global),
143            "Get an alist chain for grob @var{grob}, with @var{global} as"
144            " the global default.  If unspecified, @code{font-defaults}"
145            " from the layout block is taken.")
146 {
147   Grob *sc = unsmob_grob (grob);
148    
149   LY_ASSERT_SMOB (Grob, grob, 1);
150
151   if (global == SCM_UNDEFINED)
152     {
153       global = sc->layout ()->lookup_variable (ly_symbol2scm ("font-defaults"));
154       if (global == SCM_UNDEFINED)
155         global = SCM_EOL;
156     }
157
158   return sc->get_property_alist_chain (global);
159 }
160
161 LY_DEFINE (ly_grob_extent, "ly:grob-extent",
162            3, 0, 0, (SCM grob, SCM refp, SCM axis),
163            "Get the extent in @var{axis} direction of @var{grob} relative to"
164            " the grob @var{refp}.")
165 {
166   Grob *sc = unsmob_grob (grob);
167   Grob *ref = unsmob_grob (refp);
168   
169    
170   LY_ASSERT_SMOB (Grob, grob, 1);
171   LY_ASSERT_SMOB (Grob, refp, 2);
172   LY_ASSERT_TYPE (is_axis, axis, 3);
173
174   Axis a = Axis (scm_to_int (axis));
175
176     
177   if (ref->common_refpoint (sc, a) != ref)
178     {
179       // ugh. should use other error message
180       SCM_ASSERT_TYPE (false, refp, SCM_ARG2, __FUNCTION__, "common refpoint");
181     }
182   return ly_interval2scm (sc->extent (ref, a));
183 }
184
185 LY_DEFINE (ly_grob_robust_relative_extent, "ly:grob-robust-relative-extent",
186            3, 0, 0, (SCM grob, SCM refp, SCM axis),
187            "Get the extent in @var{axis} direction of @var{grob} relative to"
188            " the grob @var{refp}, or @code{(0,0)} if empty.")
189 {
190   Grob *sc = unsmob_grob (grob);
191   Grob *ref = unsmob_grob (refp);
192   
193    
194   LY_ASSERT_SMOB (Grob, grob, 1);
195   LY_ASSERT_SMOB (Grob, refp, 2);
196   LY_ASSERT_TYPE (is_axis, axis, 3);
197
198   Axis a = Axis (scm_to_int (axis));
199     
200   if (ref->common_refpoint (sc, a) != ref)
201     {
202       // ugh. should use other error message
203       SCM_ASSERT_TYPE (false, refp, SCM_ARG2, __FUNCTION__, "common refpoint");
204     }
205
206   return ly_interval2scm (robust_relative_extent (sc, ref, a));
207 }
208
209 LY_DEFINE (ly_grob_relative_coordinate, "ly:grob-relative-coordinate",
210            3, 0, 0, (SCM grob, SCM refp, SCM axis),
211            "Get the coordinate in @var{axis} direction of @var{grob} relative"
212            " to the grob @var{refp}.")
213 {
214   Grob *sc = unsmob_grob (grob);
215   Grob *ref = unsmob_grob (refp);
216   
217    
218   LY_ASSERT_SMOB (Grob, grob, 1);
219   LY_ASSERT_SMOB (Grob, refp, 2);
220   LY_ASSERT_TYPE (is_axis, axis, 3);
221
222   Axis a = Axis (scm_to_int (axis));
223
224     
225   if (ref->common_refpoint (sc, a) != ref)
226     {
227       // ugh. should use other error message
228       SCM_ASSERT_TYPE (false, refp, SCM_ARG2, __FUNCTION__, "common refpoint");
229     }
230
231   return scm_from_double (sc->relative_coordinate (ref, a));
232 }
233
234
235 LY_DEFINE (ly_grob_parent, "ly:grob-parent",
236            2, 0, 0, (SCM grob, SCM axis),
237            "Get the parent of @var{grob}.  @var{axis} is 0 for the X-axis,"
238            " 1@tie{}for the Y-axis.")
239 {
240   Grob *sc = unsmob_grob (grob);
241    
242   LY_ASSERT_SMOB (Grob, grob, 1);
243   LY_ASSERT_TYPE (is_axis, axis, 2);
244
245   Grob *par = sc->get_parent (Axis (scm_to_int (axis)));
246   return par ? par->self_scm () : SCM_EOL;
247 }
248
249 LY_DEFINE (ly_grob_properties, "ly:grob-properties",
250            1, 0, 0, (SCM grob),
251            "Get the mutable properties of @var{grob}.")
252 {
253   Grob *g = unsmob_grob (grob);
254    
255   LY_ASSERT_SMOB (Grob, grob, 1);
256
257   /* FIXME: uhg? copy/read only? */
258   return g->mutable_property_alist_;
259 }
260
261 LY_DEFINE (ly_grob_basic_properties, "ly:grob-basic-properties",
262            1, 0, 0, (SCM grob),
263            "Get the immutable properties of @var{grob}.")
264 {
265   Grob *g = unsmob_grob (grob);
266    
267   LY_ASSERT_SMOB (Grob, grob, 1);
268
269   /* FIXME: uhg? copy/read only? */
270   return g->immutable_property_alist_;
271 }
272
273 LY_DEFINE (ly_grob_system, "ly:grob-system",
274            1, 0, 0, (SCM grob),
275            "Return the system grob of @var{grob}.")
276 {
277   Grob *me = unsmob_grob (grob);
278    
279   LY_ASSERT_SMOB (Grob, grob, 1);
280
281   if (System *g = me->get_system ())
282     return g->self_scm ();
283
284   return SCM_EOL;
285 }
286
287 LY_DEFINE (ly_grob_original, "ly:grob-original",
288            1, 0, 0, (SCM grob),
289            "Return the unbroken original grob of @var{grob}.")
290 {
291   Grob *me = unsmob_grob (grob);
292    
293   LY_ASSERT_SMOB (Grob, grob, 1);
294   return me->original () ? me->original ()->self_scm () : me->self_scm ();
295 }
296
297
298 LY_DEFINE (ly_grob_suicide_x, "ly:grob-suicide!",
299            1, 0, 0, (SCM grob),
300            "Kill @var{grob}.")
301 {
302   Grob *me = unsmob_grob (grob);
303    
304   LY_ASSERT_SMOB (Grob, grob, 1);
305
306   me->suicide ();
307   return SCM_UNSPECIFIED;
308 }
309
310 LY_DEFINE (ly_grob_translate_axis_x, "ly:grob-translate-axis!",
311            3, 0, 0, (SCM grob, SCM d, SCM a),
312            "Translate @var{g} on axis@tie{}@var{a} over"
313            " distance@tie{}@var{d}.")
314 {
315   Grob *me = unsmob_grob (grob);
316    
317   LY_ASSERT_SMOB (Grob, grob, 1);
318   LY_ASSERT_TYPE (scm_is_number, d, 2);
319   LY_ASSERT_TYPE (is_axis, a, 3);
320
321   me->translate_axis (scm_to_double (d), Axis (scm_to_int (a)));
322   return SCM_UNSPECIFIED;
323 }
324
325 LY_DEFINE (ly_grob_default_font, "ly:grob-default-font",
326            1, 0, 0, (SCM grob),
327            "Return the default font for grob @var{gr}.")
328 {
329   Grob *gr = unsmob_grob (grob);
330    
331   LY_ASSERT_SMOB (Grob, grob, 1);
332
333   return Font_interface::get_default_font (gr)->self_scm ();
334 }
335
336
337 /*
338   TODO: consider swapping order, so we can do
339
340   (grob-common-refpoint a b c d e)
341  */
342 LY_DEFINE (ly_grob_common_refpoint, "ly:grob-common-refpoint",
343            3, 0, 0,  (SCM grob, SCM other, SCM axis),
344            "Find the common refpoint of @var{grob} and @var{other}"
345            " for @var{axis}.")
346 {
347   
348   Grob *gr = unsmob_grob (grob);
349    
350   LY_ASSERT_SMOB (Grob, grob, 1);
351   LY_ASSERT_SMOB (Grob, other, 2);
352
353   Grob *o = unsmob_grob (other);
354
355   LY_ASSERT_TYPE (is_axis, axis, 3);
356
357   Grob *refp = gr->common_refpoint (o,  Axis (scm_to_int (axis)));
358   return refp ? refp->self_scm () : SCM_BOOL_F;
359 }
360
361 LY_DEFINE (ly_grob_common_refpoint_of_array, "ly:grob-common-refpoint-of-array",
362            3, 0, 0,  (SCM grob, SCM others, SCM axis),
363            "Find the common refpoint of @var{grob} and @var{others}"
364            " (a grob-array) for @var{axis}.")
365 {
366   Grob *gr = unsmob_grob (grob);
367    
368   LY_ASSERT_SMOB (Grob, grob, 1);
369   LY_ASSERT_SMOB (Grob_array, others, 2);
370
371   Grob_array *ga = unsmob_grob_array (others);
372   LY_ASSERT_TYPE (is_axis, axis, 3);
373
374   Grob *refp = common_refpoint_of_array (ga->array (), gr, Axis (scm_to_int (axis)));
375   return refp ? refp->self_scm () : SCM_BOOL_F;
376 }