]> git.donarmstrong.com Git - lilypond.git/blob - lily/grob-scheme.cc
Docs: IR: improve docstring for ly:grob-property
[lilypond.git] / lily / grob-scheme.cc
1 /*
2   grob-scheme.cc -- Scheme entry points for the grob datatype
3
4   source file of the GNU LilyPond music typesetter
5
6   (c) 1998--2009 Jan Nieuwenhuizen <janneke@gnu.org>
7   Han-Wen Nienhuys <hanwen@xs4all.nl>
8 */
9
10 #include "warn.hh"              // error ()
11 #include "item.hh"
12 #include "output-def.hh"
13 #include "system.hh"
14 #include "font-interface.hh"
15 #include "paper-score.hh"
16 #include "grob-array.hh"
17
18 LY_DEFINE (ly_grob_property_data, "ly:grob-property-data",
19            2, 0, 0, (SCM grob, SCM sym),
20            "Retrieve @var{sym} for @var{grob} but don't process callbacks.")
21 {
22   Grob *sc = unsmob_grob (grob);
23
24   LY_ASSERT_SMOB (Grob, grob, 1);
25   LY_ASSERT_TYPE (ly_is_symbol, sym, 2);
26
27   return sc->get_property_data (sym);
28 }
29
30 LY_DEFINE (ly_grob_set_property_x, "ly:grob-set-property!",
31            3, 0, 0, (SCM grob, SCM sym, SCM val),
32            "Set @var{sym} in grob @var{grob} to value @var{val}.")
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   if (!ly_is_procedure (val)
40       && !type_check_assignment (sym, val, ly_symbol2scm ("backend-type?")))
41     error ("typecheck failed");
42
43   sc->set_property (sym, val);
44   return SCM_UNSPECIFIED;
45 }
46
47 LY_DEFINE (ly_grob_property, "ly:grob-property",
48            2, 1, 0, (SCM grob, SCM sym, SCM val),
49            "Return the value for property @var{sym} of @var{grob}."
50            "  If no value is found, return @var{val} or @code{'()}"
51            " if @var{val} is not specified.")
52 {
53   Grob *sc = unsmob_grob (grob);
54
55   LY_ASSERT_SMOB (Grob, grob, 1);
56   LY_ASSERT_TYPE (ly_is_symbol, sym, 2);
57   if (val == SCM_UNDEFINED)
58     val = SCM_EOL;
59
60   SCM retval = sc->internal_get_property (sym);
61   if (retval == SCM_EOL)
62     retval = val;
63   
64   return retval;
65 }
66
67
68 LY_DEFINE (ly_grob_interfaces, "ly:grob-interfaces",
69            1, 0, 0, (SCM grob),
70            "Return the interfaces list of grob @var{grob}.")
71 {
72   Grob *sc = unsmob_grob (grob);
73    
74   LY_ASSERT_SMOB (Grob, grob, 1);
75
76   return sc->interfaces ();
77 }
78
79 LY_DEFINE (ly_grob_object, "ly:grob-object",
80            2, 0, 0, (SCM grob, SCM sym),
81            "Return the value of a pointer in grob@tie{}@var{g} of property"
82            " @var{sym}.  It returns @code{'()} (end-of-list) if @var{sym}"
83            " is undefined in@tie{}@var{g}.")
84 {
85   Grob *sc = unsmob_grob (grob);
86    
87   LY_ASSERT_SMOB (Grob, grob, 1);
88   LY_ASSERT_TYPE (ly_is_symbol, sym, 2);
89
90   return sc->internal_get_object (sym);
91 }
92
93
94
95 /* TODO: make difference between scaled and unscalead variable in
96    calling (i.e different funcs.) */
97 LY_DEFINE (ly_grob_layout, "ly:grob-layout",
98            1, 0, 0, (SCM grob),
99            "Get @code{\\layout} definition from grob @var{grob}.")
100 {
101   Grob *sc = unsmob_grob (grob);
102    
103   LY_ASSERT_SMOB (Grob, grob, 1);
104
105   return sc->layout ()->self_scm ();
106 }
107
108 LY_DEFINE (ly_grob_alist_chain, "ly:grob-alist-chain",
109            1, 1, 0, (SCM grob, SCM global),
110            "Get an alist chain for grob @var{grob}, with @var{global} as"
111            " the global default.  If unspecified, @code{font-defaults}"
112            " from the layout block is taken.")
113 {
114   Grob *sc = unsmob_grob (grob);
115    
116   LY_ASSERT_SMOB (Grob, grob, 1);
117
118   if (global == SCM_UNDEFINED)
119     {
120       global = sc->layout ()->lookup_variable (ly_symbol2scm ("font-defaults"));
121       if (global == SCM_UNDEFINED)
122         global = SCM_EOL;
123     }
124
125   return sc->get_property_alist_chain (global);
126 }
127
128 LY_DEFINE (ly_grob_extent, "ly:grob-extent",
129            3, 0, 0, (SCM grob, SCM refp, SCM axis),
130            "Get the extent in @var{axis} direction of @var{grob} relative to"
131            " the grob @var{refp}.")
132 {
133   Grob *sc = unsmob_grob (grob);
134   Grob *ref = unsmob_grob (refp);
135   
136    
137   LY_ASSERT_SMOB (Grob, grob, 1);
138   LY_ASSERT_SMOB (Grob, refp, 2);
139   LY_ASSERT_TYPE (is_axis, axis, 3);
140
141   Axis a = Axis (scm_to_int (axis));
142
143     
144   if (ref->common_refpoint (sc, a) != ref)
145     {
146       // ugh. should use other error message
147       SCM_ASSERT_TYPE (false, refp, SCM_ARG2, __FUNCTION__, "common refpoint");
148     }
149   return ly_interval2scm (sc->extent (ref, a));
150 }
151
152 LY_DEFINE (ly_grob_robust_relative_extent, "ly:grob-robust-relative-extent",
153            3, 0, 0, (SCM grob, SCM refp, SCM axis),
154            "Get the extent in @var{axis} direction of @var{grob} relative to"
155            " the grob @var{refp}, or @code{(0,0)} if empty.")
156 {
157   Grob *sc = unsmob_grob (grob);
158   Grob *ref = unsmob_grob (refp);
159   
160    
161   LY_ASSERT_SMOB (Grob, grob, 1);
162   LY_ASSERT_SMOB (Grob, refp, 2);
163   LY_ASSERT_TYPE (is_axis, axis, 3);
164
165   Axis a = Axis (scm_to_int (axis));
166     
167   if (ref->common_refpoint (sc, a) != ref)
168     {
169       // ugh. should use other error message
170       SCM_ASSERT_TYPE (false, refp, SCM_ARG2, __FUNCTION__, "common refpoint");
171     }
172
173   return ly_interval2scm (robust_relative_extent (sc, ref, a));
174 }
175
176 LY_DEFINE (ly_grob_relative_coordinate, "ly:grob-relative-coordinate",
177            3, 0, 0, (SCM grob, SCM refp, SCM axis),
178            "Get the coordinate in @var{axis} direction of @var{grob} relative"
179            " to the grob @var{refp}.")
180 {
181   Grob *sc = unsmob_grob (grob);
182   Grob *ref = unsmob_grob (refp);
183   
184    
185   LY_ASSERT_SMOB (Grob, grob, 1);
186   LY_ASSERT_SMOB (Grob, refp, 2);
187   LY_ASSERT_TYPE (is_axis, axis, 3);
188
189   Axis a = Axis (scm_to_int (axis));
190
191     
192   if (ref->common_refpoint (sc, a) != ref)
193     {
194       // ugh. should use other error message
195       SCM_ASSERT_TYPE (false, refp, SCM_ARG2, __FUNCTION__, "common refpoint");
196     }
197
198   return scm_from_double (sc->relative_coordinate (ref, a));
199 }
200
201
202 LY_DEFINE (ly_grob_parent, "ly:grob-parent",
203            2, 0, 0, (SCM grob, SCM axis),
204            "Get the parent of @var{grob}.  @var{axis} is 0 for the X-axis,"
205            " 1@tie{}for the Y-axis.")
206 {
207   Grob *sc = unsmob_grob (grob);
208    
209   LY_ASSERT_SMOB (Grob, grob, 1);
210   LY_ASSERT_TYPE (is_axis, axis, 2);
211
212   Grob *par = sc->get_parent (Axis (scm_to_int (axis)));
213   return par ? par->self_scm () : SCM_EOL;
214 }
215
216 LY_DEFINE (ly_grob_properties, "ly:grob-properties",
217            1, 0, 0, (SCM grob),
218            "Get the mutable properties of @var{grob}.")
219 {
220   Grob *g = unsmob_grob (grob);
221    
222   LY_ASSERT_SMOB (Grob, grob, 1);
223
224   /* FIXME: uhg? copy/read only? */
225   return g->mutable_property_alist_;
226 }
227
228 LY_DEFINE (ly_grob_basic_properties, "ly:grob-basic-properties",
229            1, 0, 0, (SCM grob),
230            "Get the immutable 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->immutable_property_alist_;
238 }
239
240 LY_DEFINE (ly_grob_system, "ly:grob-system",
241            1, 0, 0, (SCM grob),
242            "Return the system grob of @var{grob}.")
243 {
244   Grob *me = unsmob_grob (grob);
245    
246   LY_ASSERT_SMOB (Grob, grob, 1);
247
248   if (System *g = me->get_system ())
249     return g->self_scm ();
250
251   return SCM_EOL;
252 }
253
254 LY_DEFINE (ly_grob_original, "ly:grob-original",
255            1, 0, 0, (SCM grob),
256            "Return the unbroken original grob of @var{grob}.")
257 {
258   Grob *me = unsmob_grob (grob);
259    
260   LY_ASSERT_SMOB (Grob, grob, 1);
261   return me->original () ? me->original ()->self_scm () : me->self_scm ();
262 }
263
264
265 LY_DEFINE (ly_grob_suicide_x, "ly:grob-suicide!",
266            1, 0, 0, (SCM grob),
267            "Kill @var{grob}.")
268 {
269   Grob *me = unsmob_grob (grob);
270    
271   LY_ASSERT_SMOB (Grob, grob, 1);
272
273   me->suicide ();
274   return SCM_UNSPECIFIED;
275 }
276
277 LY_DEFINE (ly_grob_translate_axis_x, "ly:grob-translate-axis!",
278            3, 0, 0, (SCM grob, SCM d, SCM a),
279            "Translate @var{g} on axis@tie{}@var{a} over"
280            " distance@tie{}@var{d}.")
281 {
282   Grob *me = unsmob_grob (grob);
283    
284   LY_ASSERT_SMOB (Grob, grob, 1);
285   LY_ASSERT_TYPE (scm_is_number, d, 2);
286   LY_ASSERT_TYPE (is_axis, a, 3);
287
288   me->translate_axis (scm_to_double (d), Axis (scm_to_int (a)));
289   return SCM_UNSPECIFIED;
290 }
291
292 LY_DEFINE (ly_grob_default_font, "ly:grob-default-font",
293            1, 0, 0, (SCM grob),
294            "Return the default font for grob @var{gr}.")
295 {
296   Grob *gr = unsmob_grob (grob);
297    
298   LY_ASSERT_SMOB (Grob, grob, 1);
299
300   return Font_interface::get_default_font (gr)->self_scm ();
301 }
302
303
304 /*
305   TODO: consider swapping order, so we can do
306
307   (grob-common-refpoint a b c d e)
308  */
309 LY_DEFINE (ly_grob_common_refpoint, "ly:grob-common-refpoint",
310            3, 0, 0,  (SCM grob, SCM other, SCM axis),
311            "Find the common refpoint of @var{grob} and @var{other}"
312            " for @var{axis}.")
313 {
314   
315   Grob *gr = unsmob_grob (grob);
316    
317   LY_ASSERT_SMOB (Grob, grob, 1);
318   LY_ASSERT_SMOB (Grob, other, 2);
319
320   Grob *o = unsmob_grob (other);
321
322   LY_ASSERT_TYPE (is_axis, axis, 3);
323
324   Grob *refp = gr->common_refpoint (o,  Axis (scm_to_int (axis)));
325   return refp ? refp->self_scm () : SCM_BOOL_F;
326 }
327
328 LY_DEFINE (ly_grob_common_refpoint_of_array, "ly:grob-common-refpoint-of-array",
329            3, 0, 0,  (SCM grob, SCM others, SCM axis),
330            "Find the common refpoint of @var{grob} and @var{others}"
331            " (a grob-array) for @var{axis}.")
332 {
333   Grob *gr = unsmob_grob (grob);
334    
335   LY_ASSERT_SMOB (Grob, grob, 1);
336   LY_ASSERT_SMOB (Grob_array, others, 2);
337
338   Grob_array *ga = unsmob_grob_array (others);
339   LY_ASSERT_TYPE (is_axis, axis, 3);
340
341   Grob *refp = common_refpoint_of_array (ga->array (), gr, Axis (scm_to_int (axis)));
342   return refp ? refp->self_scm () : SCM_BOOL_F;
343 }