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