]> git.donarmstrong.com Git - lilypond.git/blob - lily/grob-scheme.cc
Web-ja: update introduction
[lilypond.git] / lily / grob-scheme.cc
1 /*
2   This file is part of LilyPond, the GNU music typesetter.
3
4   Copyright (C) 1998--2015 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 "system.hh"
27 #include "unpure-pure-container.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       && !type_check_assignment (sym, val, ly_symbol2scm ("backend-type?")))
54     error ("typecheck failed");
55
56   sc->set_property (sym, val);
57   return SCM_UNSPECIFIED;
58 }
59
60 LY_DEFINE (ly_grob_set_nested_property_x, "ly:grob-set-nested-property!",
61            3, 0, 0, (SCM grob, SCM symlist, SCM val),
62            "Set nested property @var{symlist} in grob @var{grob} to value @var{val}.")
63 {
64   Grob *sc = unsmob<Grob> (grob);
65
66   LY_ASSERT_SMOB (Grob, grob, 1);
67
68   bool type_ok = scm_is_pair (symlist);
69
70   if (type_ok)
71     for (SCM s = symlist; scm_is_pair (s) && type_ok; s = scm_cdr (s))
72       type_ok &= ly_is_symbol (scm_car (s));
73
74   SCM_ASSERT_TYPE (type_ok, symlist, SCM_ARG2, __FUNCTION__, "list of symbols");
75
76   if (scm_is_pair (scm_cdr (symlist)))
77     set_nested_property (sc, symlist, val);
78   else
79     ly_grob_set_property_x (grob, scm_car (symlist), val);
80   return SCM_UNSPECIFIED;
81 }
82
83 LY_DEFINE (ly_grob_pure_property, "ly:grob-pure-property",
84            4, 1, 0, (SCM grob, SCM sym, SCM beg, SCM end, SCM val),
85            "Return the pure value for property @var{sym} of @var{grob}."
86            "  If no value is found, return @var{val} or @code{'()}"
87            " if @var{val} is not specified.")
88 {
89   Grob *sc = unsmob<Grob> (grob);
90
91   LY_ASSERT_SMOB (Grob, grob, 1);
92   LY_ASSERT_TYPE (ly_is_symbol, sym, 2);
93   LY_ASSERT_TYPE (scm_is_integer, beg, 3);
94   LY_ASSERT_TYPE (scm_is_integer, end, 4);
95   if (SCM_UNBNDP (val))
96     val = SCM_EOL;
97
98   SCM retval = sc->internal_get_pure_property (sym, scm_to_int (beg), scm_to_int (end));
99   if (scm_is_null (retval))
100     retval = val;
101
102   return retval;
103 }
104
105 LY_DEFINE (ly_grob_pure_height, "ly:grob-pure-height",
106            4, 1, 0, (SCM grob, SCM refp, SCM beg, SCM end, SCM val),
107            "Return the pure height of @var{grob} given refpoint @var{refp}."
108            "  If no value is found, return @var{val} or @code{'()}"
109            " if @var{val} is not specified.")
110 {
111   Grob *sc = unsmob<Grob> (grob);
112   Grob *ref = unsmob<Grob> (refp);
113
114   LY_ASSERT_SMOB (Grob, grob, 1);
115   LY_ASSERT_SMOB (Grob, refp, 2);
116   LY_ASSERT_TYPE (scm_is_integer, beg, 3);
117   LY_ASSERT_TYPE (scm_is_integer, end, 4);
118   if (SCM_UNBNDP (val))
119     val = SCM_EOL;
120
121   Interval retval = sc->pure_y_extent (ref, scm_to_int (beg), scm_to_int (end));
122
123   return ly_interval2scm (retval);
124 }
125
126 LY_DEFINE (ly_grob_property, "ly:grob-property",
127            2, 1, 0, (SCM grob, SCM sym, SCM val),
128            "Return the value for property @var{sym} of @var{grob}."
129            "  If no value is found, return @var{val} or @code{'()}"
130            " if @var{val} is not specified.")
131 {
132   Grob *sc = unsmob<Grob> (grob);
133
134   LY_ASSERT_SMOB (Grob, grob, 1);
135   LY_ASSERT_TYPE (ly_is_symbol, sym, 2);
136   if (SCM_UNBNDP (val))
137     val = SCM_EOL;
138
139   SCM retval = sc->get_property (sym);
140   if (scm_is_null (retval))
141     retval = val;
142
143   return retval;
144 }
145
146 LY_DEFINE (ly_grob_interfaces, "ly:grob-interfaces",
147            1, 0, 0, (SCM grob),
148            "Return the interfaces list of grob @var{grob}.")
149 {
150   Grob *sc = unsmob<Grob> (grob);
151
152   LY_ASSERT_SMOB (Grob, grob, 1);
153
154   return sc->interfaces ();
155 }
156
157 LY_DEFINE (ly_grob_object, "ly:grob-object",
158            2, 0, 0, (SCM grob, SCM sym),
159            "Return the value of a pointer in grob @var{grob} of property"
160            " @var{sym}.  It returns @code{'()} (end-of-list) if @var{sym}"
161            " is undefined in @var{grob}.")
162 {
163   Grob *sc = unsmob<Grob> (grob);
164
165   LY_ASSERT_SMOB (Grob, grob, 1);
166   LY_ASSERT_TYPE (ly_is_symbol, sym, 2);
167
168   return sc->internal_get_object (sym);
169 }
170
171 LY_DEFINE (ly_grob_set_object_x, "ly:grob-set-object!",
172            3, 0, 0, (SCM grob, SCM sym, SCM val),
173            "Set @var{sym} in grob @var{grob} to value @var{val}.")
174 {
175   Grob *sc = unsmob<Grob> (grob);
176
177   LY_ASSERT_SMOB (Grob, grob, 1);
178   LY_ASSERT_TYPE (ly_is_symbol, sym, 2);
179
180   sc->set_object (sym, val);
181   return SCM_UNSPECIFIED;
182 }
183
184 /* TODO: make difference between scaled and unscalead variable in
185    calling (i.e different funcs.) */
186 LY_DEFINE (ly_grob_layout, "ly:grob-layout",
187            1, 0, 0, (SCM grob),
188            "Get @code{\\layout} definition from grob @var{grob}.")
189 {
190   Grob *sc = unsmob<Grob> (grob);
191
192   LY_ASSERT_SMOB (Grob, grob, 1);
193
194   return sc->layout ()->self_scm ();
195 }
196
197 LY_DEFINE (ly_grob_alist_chain, "ly:grob-alist-chain",
198            1, 1, 0, (SCM grob, SCM global),
199            "Get an alist chain for grob @var{grob}, with @var{global} as"
200            " the global default.  If unspecified, @code{font-defaults}"
201            " from the layout block is taken.")
202 {
203   Grob *sc = unsmob<Grob> (grob);
204
205   LY_ASSERT_SMOB (Grob, grob, 1);
206
207   if (SCM_UNBNDP (global))
208     {
209       global = sc->layout ()->lookup_variable (ly_symbol2scm ("font-defaults"));
210       if (SCM_UNBNDP (global))
211         global = SCM_EOL;
212     }
213
214   return sc->get_property_alist_chain (global);
215 }
216
217 LY_DEFINE (ly_grob_extent, "ly:grob-extent",
218            3, 0, 0, (SCM grob, SCM refp, SCM axis),
219            "Get the extent in @var{axis} direction of @var{grob} relative to"
220            " the grob @var{refp}.")
221 {
222   Grob *sc = unsmob<Grob> (grob);
223   Grob *ref = unsmob<Grob> (refp);
224
225   LY_ASSERT_SMOB (Grob, grob, 1);
226   LY_ASSERT_SMOB (Grob, refp, 2);
227   LY_ASSERT_TYPE (is_axis, axis, 3);
228
229   Axis a = Axis (scm_to_int (axis));
230
231   if (ref->common_refpoint (sc, a) != ref)
232     {
233       // ugh. should use other error message
234       SCM_ASSERT_TYPE (false, refp, SCM_ARG2, __FUNCTION__, "common refpoint");
235     }
236   return ly_interval2scm (sc->extent (ref, a));
237 }
238
239 LY_DEFINE (ly_grob_robust_relative_extent, "ly:grob-robust-relative-extent",
240            3, 0, 0, (SCM grob, SCM refp, SCM axis),
241            "Get the extent in @var{axis} direction of @var{grob} relative to"
242            " the grob @var{refp}, or @code{(0,0)} if empty.")
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 ly_interval2scm (robust_relative_extent (sc, ref, a));
260 }
261
262 LY_DEFINE (ly_grob_relative_coordinate, "ly:grob-relative-coordinate",
263            3, 0, 0, (SCM grob, SCM refp, SCM axis),
264            "Get the coordinate in @var{axis} direction of @var{grob} relative"
265            " to the grob @var{refp}.")
266 {
267   Grob *sc = unsmob<Grob> (grob);
268   Grob *ref = unsmob<Grob> (refp);
269
270   LY_ASSERT_SMOB (Grob, grob, 1);
271   LY_ASSERT_SMOB (Grob, refp, 2);
272   LY_ASSERT_TYPE (is_axis, axis, 3);
273
274   Axis a = Axis (scm_to_int (axis));
275
276   if (ref->common_refpoint (sc, a) != ref)
277     {
278       // ugh. should use other error message
279       SCM_ASSERT_TYPE (false, refp, SCM_ARG2, __FUNCTION__, "common refpoint");
280     }
281
282   return scm_from_double (sc->relative_coordinate (ref, a));
283 }
284
285 LY_DEFINE (ly_grob_parent, "ly:grob-parent",
286            2, 0, 0, (SCM grob, SCM axis),
287            "Get the parent of @var{grob}.  @var{axis} is 0 for the X-axis,"
288            " 1@tie{}for the Y-axis.")
289 {
290   Grob *sc = unsmob<Grob> (grob);
291
292   LY_ASSERT_SMOB (Grob, grob, 1);
293   LY_ASSERT_TYPE (is_axis, axis, 2);
294
295   Grob *par = sc->get_parent (Axis (scm_to_int (axis)));
296   return par ? par->self_scm () : SCM_EOL;
297 }
298
299 LY_DEFINE (ly_grob_set_parent_x, "ly:grob-set-parent!",
300            3, 0, 0, (SCM grob, SCM axis, SCM parent_grob),
301            "Set @var{parent-grob} the parent of grob @var{grob} in axis @var{axis}.")
302 {
303   Grob *gr = unsmob<Grob> (grob);
304   Grob *parent = unsmob<Grob> (parent_grob);
305
306   LY_ASSERT_SMOB (Grob, grob, 1);
307   LY_ASSERT_TYPE (is_axis, axis, 2);
308   LY_ASSERT_SMOB (Grob, parent_grob, 3);
309
310   Axis a = Axis (scm_to_int (axis));
311   gr->set_parent (parent, a);
312   return SCM_UNSPECIFIED;
313 }
314
315 LY_DEFINE (ly_grob_properties, "ly:grob-properties",
316            1, 0, 0, (SCM grob),
317            "Get the mutable properties of @var{grob}.")
318 {
319   Grob *g = unsmob<Grob> (grob);
320
321   LY_ASSERT_SMOB (Grob, grob, 1);
322
323   /* FIXME: uhg? copy/read only? */
324   return g->mutable_property_alist_;
325 }
326
327 LY_DEFINE (ly_grob_basic_properties, "ly:grob-basic-properties",
328            1, 0, 0, (SCM grob),
329            "Get the immutable properties of @var{grob}.")
330 {
331   Grob *g = unsmob<Grob> (grob);
332
333   LY_ASSERT_SMOB (Grob, grob, 1);
334
335   /* FIXME: uhg? copy/read only? */
336   return g->immutable_property_alist_;
337 }
338
339 LY_DEFINE (ly_grob_system, "ly:grob-system",
340            1, 0, 0, (SCM grob),
341            "Return the system grob of @var{grob}.")
342 {
343   Grob *me = unsmob<Grob> (grob);
344
345   LY_ASSERT_SMOB (Grob, grob, 1);
346
347   if (System *g = me->get_system ())
348     return g->self_scm ();
349
350   return SCM_EOL;
351 }
352
353 LY_DEFINE (ly_grob_original, "ly:grob-original",
354            1, 0, 0, (SCM grob),
355            "Return the unbroken original grob of @var{grob}.")
356 {
357   Grob *me = unsmob<Grob> (grob);
358
359   LY_ASSERT_SMOB (Grob, grob, 1);
360   return me->original () ? me->original ()->self_scm () : me->self_scm ();
361 }
362
363 LY_DEFINE (ly_grob_suicide_x, "ly:grob-suicide!",
364            1, 0, 0, (SCM grob),
365            "Kill @var{grob}.")
366 {
367   Grob *me = unsmob<Grob> (grob);
368
369   LY_ASSERT_SMOB (Grob, grob, 1);
370
371   me->suicide ();
372   return SCM_UNSPECIFIED;
373 }
374
375 LY_DEFINE (ly_grob_translate_axis_x, "ly:grob-translate-axis!",
376            3, 0, 0, (SCM grob, SCM d, SCM a),
377            "Translate @var{grob} on axis@tie{}@var{a} over"
378            " distance@tie{}@var{d}.")
379 {
380   Grob *me = unsmob<Grob> (grob);
381
382   LY_ASSERT_SMOB (Grob, grob, 1);
383   LY_ASSERT_TYPE (scm_is_number, d, 2);
384   LY_ASSERT_TYPE (is_axis, a, 3);
385
386   me->translate_axis (scm_to_double (d), Axis (scm_to_int (a)));
387   return SCM_UNSPECIFIED;
388 }
389
390 LY_DEFINE (ly_grob_default_font, "ly:grob-default-font",
391            1, 0, 0, (SCM grob),
392            "Return the default font for grob @var{grob}.")
393 {
394   Grob *gr = unsmob<Grob> (grob);
395
396   LY_ASSERT_SMOB (Grob, grob, 1);
397
398   return Font_interface::get_default_font (gr)->self_scm ();
399 }
400
401 /*
402   TODO: consider swapping order, so we can do
403
404   (grob-common-refpoint a b c d e)
405  */
406 LY_DEFINE (ly_grob_common_refpoint, "ly:grob-common-refpoint",
407            3, 0, 0, (SCM grob, SCM other, SCM axis),
408            "Find the common refpoint of @var{grob} and @var{other}"
409            " for @var{axis}.")
410 {
411
412   Grob *gr = unsmob<Grob> (grob);
413
414   LY_ASSERT_SMOB (Grob, grob, 1);
415   LY_ASSERT_SMOB (Grob, other, 2);
416
417   Grob *o = unsmob<Grob> (other);
418
419   LY_ASSERT_TYPE (is_axis, axis, 3);
420
421   Grob *refp = gr->common_refpoint (o, Axis (scm_to_int (axis)));
422   return refp ? refp->self_scm () : SCM_BOOL_F;
423 }
424
425 LY_DEFINE (ly_grob_common_refpoint_of_array, "ly:grob-common-refpoint-of-array",
426            3, 0, 0, (SCM grob, SCM others, SCM axis),
427            "Find the common refpoint of @var{grob} and @var{others}"
428            " (a grob-array) for @var{axis}.")
429 {
430   Grob *gr = unsmob<Grob> (grob);
431
432   LY_ASSERT_SMOB (Grob, grob, 1);
433   LY_ASSERT_SMOB (Grob_array, others, 2);
434
435   Grob_array *ga = unsmob<Grob_array> (others);
436   LY_ASSERT_TYPE (is_axis, axis, 3);
437
438   Grob *refp = common_refpoint_of_array (ga->array (), gr, Axis (scm_to_int (axis)));
439   return refp ? refp->self_scm () : SCM_BOOL_F;
440 }
441
442 LY_DEFINE (ly_grob_chain_callback, "ly:grob-chain-callback",
443            3, 0, 0, (SCM grob, SCM proc, SCM sym),
444            "Find the callback that is stored as property"
445            " @var{sym} of grob @var{grob} and chain @var{proc}"
446            " to the head of this, meaning that it is called"
447            " using @var{grob} and the previous callback's result.")
448 {
449   Grob *gr = unsmob<Grob> (grob);
450
451   LY_ASSERT_SMOB (Grob, grob, 1);
452   SCM_ASSERT_TYPE (ly_is_procedure (proc) || unsmob<Unpure_pure_container> (proc), proc, SCM_ARG2, __FUNCTION__, "procedure or unpure pure container");
453   LY_ASSERT_TYPE (ly_is_symbol, sym, 3);
454
455   chain_callback (gr, proc, sym);
456   return SCM_UNSPECIFIED;
457 }
458
459 LY_DEFINE (ly_grob_vertical_less_p, "ly:grob-vertical<?",
460            2, 0, 0, (SCM a, SCM b),
461            "Does @var{a} lie above @var{b} on the page?")
462 {
463   LY_ASSERT_SMOB (Grob, a, 1);
464   LY_ASSERT_SMOB (Grob, b, 2);
465
466   Grob *ga = unsmob<Grob> (a);
467   Grob *gb = unsmob<Grob> (b);
468
469   return ly_bool2scm (Grob::vertical_less (ga, gb));
470 }
471
472 LY_DEFINE (ly_grob_get_vertical_axis_group_index, "ly:grob-get-vertical-axis-group-index",
473            1, 0, 0, (SCM grob),
474            "Get the index of the vertical axis group the grob @var{grob} belongs to;"
475            " return @code{-1} if none is found.")
476 {
477   Grob *gr = unsmob<Grob> (grob);
478
479   LY_ASSERT_SMOB (Grob, grob, 1);
480
481   return scm_from_int (Grob::get_vertical_axis_group_index (gr));
482 }
483
484 LY_DEFINE (ly_grob_spanned_rank_interval, "ly:grob-spanned-rank-interval",
485            1, 0, 0, (SCM grob),
486            "Returns a pair with the @code{rank} of the furthest left"
487            " column and the @code{rank} of the furthest right column"
488            " spanned by @code{grob}.")
489 {
490   Grob *gr = unsmob<Grob> (grob);
491
492   LY_ASSERT_SMOB (Grob, grob, 1);
493
494   Interval_t<int> iv = gr->spanned_rank_interval ();
495
496   return scm_cons (scm_from_int(iv[LEFT]), scm_from_int(iv[RIGHT]));
497 }