2 score-elem.cc -- implement Grob
4 source file of the GNU LilyPond music typesetter
6 (c) 1997--2001 Han-Wen Nienhuys <hanwen@cs.uu.nl>
13 #include "input-smob.hh"
14 #include "libc-extension.hh"
15 #include "group-interface.hh"
17 #include "paper-score.hh"
18 #include "paper-def.hh"
19 #include "molecule.hh"
23 #include "line-of-score.hh"
25 #include "paper-column.hh"
26 #include "molecule.hh"
28 #include "paper-outputter.hh"
29 #include "dimension-cache.hh"
30 #include "side-position-interface.hh"
33 #include "ly-smobs.icc"
38 remove dynamic_cast<Spanner,Item> and put this code into respective
43 #define INFINITY_MSG "Infinity or NaN encountered"
45 Grob::Grob (SCM basicprops)
48 fixme: default should be no callback.
54 immutable_property_alist_ = basicprops;
55 mutable_property_alist_ = SCM_EOL;
62 destill this into a function, so we can re-init the immutable
63 properties with a new BASICPROPS value after creation. Convenient
64 eg. when using \override with StaffSymbol. */
66 char const*onames[] = {"X-offset-callbacks", "Y-offset-callbacks"};
67 char const*enames[] = {"X-extent-callback", "Y-extent-callback"};
69 for (int a = X_AXIS; a <= Y_AXIS; a++)
71 SCM l = get_grob_property (onames[a]);
73 if (scm_ilength (l) >=0)
75 dim_cache_[a].offset_callbacks_ = l;
76 dim_cache_[a].offsets_left_ = scm_ilength (l);
80 programming_error ("[XY]-offset-callbacks must be a list");
83 SCM cb = get_grob_property (enames[a]);
86 Should change default to be empty?
88 if (cb != SCM_BOOL_F && !gh_procedure_p (cb) && !gh_pair_p (cb))
89 cb = molecule_extent_proc;
91 dim_cache_[a].dimension_ = cb;
94 SCM meta = get_grob_property ("meta");
97 SCM ifs = scm_assoc (ly_symbol2scm ("interfaces"), meta);
99 set_grob_property ("interfaces",ly_cdr (ifs));
103 Grob::Grob (Grob const&s)
104 : dim_cache_ (s.dim_cache_)
106 original_l_ = (Grob*) &s;
107 immutable_property_alist_ = s.immutable_property_alist_;
108 mutable_property_alist_ = SCM_EOL;
110 status_c_ = s.status_c_;
111 pscore_l_ = s.pscore_l_;
119 do nothing scm-ish and no unprotecting here.
125 Grob::get_grob_property (const char *nm) const
127 SCM sym = ly_symbol2scm (nm);
128 return get_grob_property (sym);
132 Grob::get_grob_property (SCM sym) const
134 SCM s = scm_sloppy_assq (sym, mutable_property_alist_);
138 s = scm_sloppy_assq (sym, immutable_property_alist_);
139 return (s == SCM_BOOL_F) ? SCM_EOL : ly_cdr (s);
143 Remove the value associated with KEY, and return it. The result is
144 that a next call will yield SCM_EOL (and not the underlying
148 Grob::remove_grob_property (const char* key)
150 SCM val = get_grob_property (key);
152 set_grob_property (key, SCM_EOL);
157 Grob::set_grob_property (const char* k, SCM v)
159 SCM s = ly_symbol2scm (k);
160 set_grob_property (s, v);
164 Puts the k, v in the immutable_property_alist_, which is convenient for
165 storing variables that are needed during the breaking process. (eg.
166 Line_of_score::rank : int)
169 Grob::set_immutable_grob_property (const char*k, SCM v)
171 SCM s = ly_symbol2scm (k);
172 set_immutable_grob_property (s, v);
176 Grob::set_immutable_grob_property (SCM s, SCM v)
178 immutable_property_alist_ = gh_cons (gh_cons (s,v), mutable_property_alist_);
179 mutable_property_alist_ = scm_assq_remove_x (mutable_property_alist_, s);
182 Grob::set_grob_property (SCM s, SCM v)
184 mutable_property_alist_ = scm_assq_set_x (mutable_property_alist_, s, v);
188 MAKE_SCHEME_CALLBACK (Grob,molecule_extent,2);
190 Grob::molecule_extent (SCM element_smob, SCM scm_axis)
192 Grob *s = unsmob_grob (element_smob);
193 Axis a = (Axis) gh_scm2int (scm_axis);
195 Molecule *m = s->get_molecule ();
199 return ly_interval2scm (e);
202 MAKE_SCHEME_CALLBACK (Grob,preset_extent,2);
205 Grob::preset_extent (SCM element_smob, SCM scm_axis)
207 Grob *s = unsmob_grob (element_smob);
208 Axis a = (Axis) gh_scm2int (scm_axis);
210 SCM ext = s->get_grob_property ((a == X_AXIS)
216 Real l = gh_scm2double (ly_car (ext));
217 Real r = gh_scm2double (ly_cdr (ext));
218 return ly_interval2scm (Interval (l, r));
221 return ly_interval2scm (Interval ());
227 Grob::paper_l () const
229 return pscore_l_ ? pscore_l_->paper_l_ : 0;
233 Grob::calculate_dependencies (int final, int busy, SCM funcname)
235 if (status_c_ >= final)
238 if (status_c_== busy)
240 programming_error ("Element is busy, come back later");
246 for (SCM d= get_grob_property ("dependencies"); gh_pair_p (d); d = ly_cdr (d))
248 unsmob_grob (ly_car (d))
249 ->calculate_dependencies (final, busy, funcname);
253 String s = ly_symbol2string (funcname);
254 SCM proc = get_grob_property (s.ch_C ());
255 if (gh_procedure_p (proc))
256 gh_call1 (proc, this->self_scm ());
263 Grob::get_molecule () const
265 SCM mol = get_grob_property ("molecule");
266 if (unsmob_molecule (mol))
267 return unsmob_molecule (mol);
269 mol = get_uncached_molecule ();
271 Grob *me = (Grob*)this;
272 me->set_grob_property ("molecule", mol);
274 return unsmob_molecule (mol);
277 Grob::get_uncached_molecule ()const
279 SCM proc = get_grob_property ("molecule-callback");
282 if (gh_procedure_p (proc))
283 mol = gh_apply (proc, scm_list_n (this->self_scm (), SCM_UNDEFINED));
286 Molecule *m = unsmob_molecule (mol);
288 if (unsmob_molecule (mol))
291 TODO: add option for not copying origin info.
293 SCM origin =get_grob_property ("origin");
294 if (!unsmob_input (origin))
295 origin =ly_symbol2scm ("no-origin");
300 mol = Molecule (m->extent_box (),
301 scm_list_n (origin, m->get_expr (), SCM_UNDEFINED)
304 m = unsmob_molecule (mol);
308 transparent retains dimensions of element.
310 if (m && to_boolean (get_grob_property ("transparent")))
311 mol = Molecule (m->extent_box (), SCM_EOL).smobbed_copy ();
322 Grob::do_break_processing ()
332 Grob::line_l () const
338 Grob::add_dependency (Grob*e)
342 Pointer_group_interface ::add_element (this, "dependencies",e);
346 programming_error ("Null dependency added");
353 Do break substitution in S, using CRITERION. Return new value.
354 CRITERION is either a SMOB pointer to the desired line, or a number
355 representing the break direction. Do not modify SRC.
357 It is rather tightly coded, since it takes a lot of time; it is
358 one of the top functions in the profile.
362 Grob::handle_broken_grobs (SCM src, SCM criterion)
365 Grob *sc = unsmob_grob (src);
368 if (SCM_INUMP (criterion))
370 Item * i = dynamic_cast<Item*> (sc);
371 Direction d = to_dir (criterion);
372 if (i && i->break_status_dir () != d)
374 Item *br = i->find_prebroken_piece (d);
375 return (br) ? br->self_scm () : SCM_UNDEFINED;
381 = dynamic_cast<Line_of_score*> (unsmob_grob (criterion));
382 if (sc->line_l () != line)
384 sc = sc->find_broken_piece (line);
388 /* now: !sc || (sc && sc->line_l () == line) */
390 return SCM_UNDEFINED;
392 /* now: sc && sc->line_l () == line */
394 return sc->self_scm();
396 This was introduced in 1.3.49 as a measure to prevent
397 programming errors. It looks expensive (?). TODO:
398 benchmark , document when (what kind of programming
399 errors) this happens.
401 if (sc->common_refpoint (line, X_AXIS)
402 && sc->common_refpoint (line, Y_AXIS))
404 return sc->self_scm ();
406 return SCM_UNDEFINED;
409 else if (SCM_CONSP (src))
411 SCM oldcar =ly_car (src);
413 UGH! breaks on circular lists.
415 SCM newcar = handle_broken_grobs (oldcar, criterion);
416 SCM oldcdr = ly_cdr (src);
418 if (newcar == SCM_UNDEFINED
419 && (gh_pair_p (oldcdr) || oldcdr == SCM_EOL))
422 This is tail-recursion, ie.
424 return handle_broken_grobs (cdr, criterion);
426 We don't want to rely on the compiler to do this. Without
427 tail-recursion, this easily crashes with a stack overflow. */
432 SCM newcdr = handle_broken_grobs (oldcdr, criterion);
433 return scm_cons (newcar, newcdr);
442 Grob::handle_broken_dependencies ()
444 Spanner * s= dynamic_cast<Spanner*> (this);
445 if (original_l_ && s)
450 for (int i = 0; i< s->broken_into_l_arr_ .size (); i++)
452 Grob * sc = s->broken_into_l_arr_[i];
453 Line_of_score * l = sc->line_l ();
454 sc->mutable_property_alist_ =
455 handle_broken_grobs (mutable_property_alist_,
456 l ? l->self_scm () : SCM_UNDEFINED);
461 Line_of_score *line = line_l ();
463 if (line && common_refpoint (line, X_AXIS) && common_refpoint (line, Y_AXIS))
465 mutable_property_alist_
466 = handle_broken_grobs (mutable_property_alist_,
467 line ? line->self_scm () : SCM_UNDEFINED);
469 else if (dynamic_cast <Line_of_score*> (this))
471 mutable_property_alist_ = handle_broken_grobs (mutable_property_alist_,
477 This element is `invalid'; it has been removed from all
478 dependencies, so let's junk the element itself.
480 do not do this for Line_of_score, since that would remove
481 references to the originals of score-elts, which get then GC'd
489 Note that we still want references to this element to be
490 rearranged, and not silently thrown away, so we keep pointers
491 like {broken_into_{drul,array}, original}
496 mutable_property_alist_ = SCM_EOL;
497 immutable_property_alist_ = SCM_EOL;
499 set_extent_callback (SCM_EOL, Y_AXIS);
500 set_extent_callback (SCM_EOL, X_AXIS);
502 for (int a= X_AXIS; a <= Y_AXIS; a++)
504 dim_cache_[a].offset_callbacks_ = SCM_EOL;
505 dim_cache_[a].offsets_left_ = 0;
510 Grob::handle_prebroken_dependencies ()
515 Grob::find_broken_piece (Line_of_score*) const
521 Grob::translate_axis (Real y, Axis a)
523 if (isinf (y) || isnan (y))
524 programming_error (_ (INFINITY_MSG));
527 dim_cache_[a].offset_ += y;
532 Grob::relative_coordinate (Grob const*refp, Axis a) const
538 We catch PARENT_L_ == nil case with this, but we crash if we did
539 not ask for the absolute coordinate (ie. REFP == nil.)
542 if (refp == dim_cache_[a].parent_l_)
543 return get_offset (a);
545 return get_offset (a) + dim_cache_[a].parent_l_->relative_coordinate (refp, a);
549 Grob::get_offset (Axis a) const
551 Grob *me = (Grob*) this;
552 while (dim_cache_[a].offsets_left_)
554 int l = --me->dim_cache_[a].offsets_left_;
555 SCM cb = scm_list_ref (dim_cache_[a].offset_callbacks_, gh_int2scm (l));
556 SCM retval = gh_call2 (cb, self_scm (), gh_int2scm (a));
558 Real r = gh_scm2double (retval);
559 if (isinf (r) || isnan (r))
561 programming_error (INFINITY_MSG);
564 me->dim_cache_[a].offset_ +=r;
566 return dim_cache_[a].offset_;
570 MAKE_SCHEME_CALLBACK (Grob,point_dimension_callback,2);
572 Grob::point_dimension_callback (SCM , SCM)
574 return ly_interval2scm (Interval (0,0));
578 Grob::empty_b (Axis a)const
580 return ! (gh_pair_p (dim_cache_[a].dimension_) ||
581 gh_procedure_p (dim_cache_[a].dimension_));
592 Grob::extent (Grob * refp, Axis a) const
594 Real x = relative_coordinate (refp, a);
597 Dimension_cache * d = (Dimension_cache *)&dim_cache_[a];
599 if (gh_pair_p (d->dimension_))
601 else if (gh_procedure_p (d->dimension_))
604 FIXME: add doco on types, and should typecheck maybe?
606 d->dimension_= gh_call2 (d->dimension_, self_scm (), gh_int2scm (a));
611 if (!gh_pair_p (d->dimension_))
614 ext = ly_scm2interval (d->dimension_);
616 SCM extra = get_grob_property (a == X_AXIS
623 if (gh_pair_p (extra))
625 ext[BIGGER] += gh_scm2double (ly_cdr (extra));
626 ext[SMALLER] += gh_scm2double (ly_car (extra));
629 extra = get_grob_property (a == X_AXIS
631 : "minimum-extent-Y");
632 if (gh_pair_p (extra))
634 ext.unite (Interval (gh_scm2double (ly_car (extra)),
635 gh_scm2double (ly_cdr (extra))));
645 Grob::parent_l (Axis a) const
647 return dim_cache_[a].parent_l_;
651 Grob::common_refpoint (Grob const* s, Axis a) const
654 I don't like the quadratic aspect of this code, but I see no other
655 way. The largest chain of parents might be 10 high or so, so
656 it shouldn't be a real issue. */
657 for (Grob const *c = this; c; c = c->dim_cache_[a].parent_l_)
658 for (Grob const * d = s; d; d = d->dim_cache_[a].parent_l_)
667 Grob::common_refpoint (SCM elist, Axis a) const
669 Grob * common = (Grob*) this;
670 for (; gh_pair_p (elist); elist = ly_cdr (elist))
672 Grob * s = unsmob_grob (ly_car (elist));
674 common = common->common_refpoint (s, a);
683 SCM meta = get_grob_property ("meta");
684 SCM nm = scm_assoc (ly_symbol2scm ("name"), meta);
685 nm = (gh_pair_p (nm)) ? ly_cdr (nm) : SCM_EOL;
686 return gh_symbol_p (nm) ? ly_symbol2string (nm) : classname (this);
690 Grob::add_offset_callback (SCM cb, Axis a)
692 if (!has_offset_callback_b (cb, a))
694 dim_cache_[a].offset_callbacks_ = gh_cons (cb, dim_cache_[a].offset_callbacks_);
695 dim_cache_[a].offsets_left_ ++;
700 Grob::has_extent_callback_b (SCM cb, Axis a)const
702 return scm_equal_p (cb, dim_cache_[a].dimension_) == SCM_BOOL_T;
707 Grob::has_extent_callback_b (Axis a) const
709 return gh_procedure_p (dim_cache_[a].dimension_);
713 Grob::has_offset_callback_b (SCM cb, Axis a)const
715 return scm_memq (cb, dim_cache_[a].offset_callbacks_) != SCM_BOOL_F;
719 Grob::set_extent_callback (SCM dc, Axis a)
721 dim_cache_[a].dimension_ =dc;
725 Grob::set_parent (Grob *g, Axis a)
727 dim_cache_[a].parent_l_ = g;
730 MAKE_SCHEME_CALLBACK (Grob,fixup_refpoint,1);
732 Grob::fixup_refpoint (SCM smob)
734 Grob *me = unsmob_grob (smob);
735 for (int a = X_AXIS; a < NO_AXES; a ++)
738 Grob * parent = me->parent_l (ax);
743 if (parent->line_l () != me->line_l () && me->line_l ())
745 Grob * newparent = parent->find_broken_piece (me->line_l ());
746 me->set_parent (newparent, ax);
749 if (Item * i = dynamic_cast<Item*> (me))
751 Item *parenti = dynamic_cast<Item*> (parent);
755 Direction my_dir = i->break_status_dir () ;
756 if (my_dir!= parenti->break_status_dir ())
758 Item *newparent = parenti->find_prebroken_piece (my_dir);
759 me->set_parent (newparent, ax);
769 /****************************************************
771 ****************************************************/
775 IMPLEMENT_SMOBS (Grob);
776 IMPLEMENT_DEFAULT_EQUAL_P (Grob);
779 Grob::mark_smob (SCM ses)
781 Grob * s = (Grob*) SCM_CELL_WORD_1 (ses);
782 scm_gc_mark (s->immutable_property_alist_);
783 scm_gc_mark (s->mutable_property_alist_);
785 for (int a =0 ; a < 2; a++)
787 scm_gc_mark (s->dim_cache_[a].offset_callbacks_);
788 scm_gc_mark (s->dim_cache_[a].dimension_);
791 if (s->parent_l (Y_AXIS))
792 scm_gc_mark (s->parent_l (Y_AXIS)->self_scm ());
793 if (s->parent_l (X_AXIS))
794 scm_gc_mark (s->parent_l (X_AXIS)->self_scm ());
797 scm_gc_mark (s->original_l_->self_scm ());
798 return s->do_derived_mark ();
802 Grob::print_smob (SCM s, SCM port, scm_print_state *)
804 Grob *sc = (Grob *) ly_cdr (s);
806 scm_puts ("#<Grob ", port);
807 scm_puts ((char *)sc->name ().ch_C (), port);
810 don't try to print properties, that is too much hassle.
812 scm_puts (" >", port);
817 Grob::do_derived_mark ()
824 ly_set_grob_property (SCM elt, SCM sym, SCM val)
826 Grob * sc = unsmob_grob (elt);
828 if (!gh_symbol_p (sym))
830 error ("Not a symbol");
831 ly_display_scm (sym);
832 return SCM_UNSPECIFIED;
837 sc->set_grob_property (sym, val);
841 error ("Not a score element");
842 ly_display_scm (elt);
845 return SCM_UNSPECIFIED;
850 ly_get_grob_property (SCM elt, SCM sym)
852 Grob * sc = unsmob_grob (elt);
856 return sc->get_grob_property (sym);
860 error ("Not a score element");
861 ly_display_scm (elt);
863 return SCM_UNSPECIFIED;
868 Grob::discretionary_processing ()
875 spanner_get_bound (SCM slur, SCM dir)
877 return dynamic_cast<Spanner*> (unsmob_grob (slur))->get_bound (to_dir (dir))->self_scm ();
882 static SCM interfaces_sym;
886 interfaces_sym = scm_permanent_object (ly_symbol2scm ("interfaces"));
888 scm_c_define_gsubr ("ly-get-grob-property", 2, 0, 0,
889 (Scheme_function_unknown)ly_get_grob_property);
890 scm_c_define_gsubr ("ly-set-grob-property", 3, 0, 0,
891 (Scheme_function_unknown)ly_set_grob_property);
892 scm_c_define_gsubr ("ly-get-spanner-bound", 2 , 0, 0,
893 (Scheme_function_unknown) spanner_get_bound);
897 Grob::has_interface (SCM k)
899 SCM ifs = get_grob_property (interfaces_sym);
901 return scm_memq (k, ifs) != SCM_BOOL_F;
905 Grob::set_interface (SCM k)
907 if (has_interface (k))
911 set_grob_property (interfaces_sym,
912 gh_cons (k, get_grob_property (interfaces_sym)));
917 ADD_SCM_INIT_FUNC (scoreelt, init_functions);
918 IMPLEMENT_TYPE_P (Grob, "ly-grob?");