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");
95 SCM ifs = scm_assoc (ly_symbol2scm ("interfaces"), meta);
97 set_grob_property ("interfaces",ly_cdr (ifs));
101 Grob::Grob (Grob const&s)
102 : dim_cache_ (s.dim_cache_)
104 original_l_ = (Grob*) &s;
105 immutable_property_alist_ = s.immutable_property_alist_;
106 mutable_property_alist_ = SCM_EOL;
108 status_c_ = s.status_c_;
109 pscore_l_ = s.pscore_l_;
117 do nothing scm-ish and no unprotecting here.
123 Grob::get_grob_property (const char *nm) const
125 SCM sym = ly_symbol2scm (nm);
126 return get_grob_property (sym);
130 Grob::get_grob_property (SCM sym) const
132 SCM s = scm_sloppy_assq (sym, mutable_property_alist_);
136 s = scm_sloppy_assq (sym, immutable_property_alist_);
137 return (s == SCM_BOOL_F) ? SCM_EOL : ly_cdr (s);
141 Remove the value associated with KEY, and return it. The result is
142 that a next call will yield SCM_UNDEFINED (and not the underlying
146 Grob::remove_grob_property (const char* key)
148 SCM val = get_grob_property (key);
150 set_grob_property (key, SCM_EOL);
155 Grob::set_grob_property (const char* k, SCM v)
157 SCM s = ly_symbol2scm (k);
158 set_grob_property (s, v);
162 Puts the k, v in the immutable_property_alist_, which is convenient for
163 storing variables that are needed during the breaking process. (eg.
164 Line_of_score::rank : int)
167 Grob::set_immutable_grob_property (const char*k, SCM v)
169 SCM s = ly_symbol2scm (k);
170 set_immutable_grob_property (s, v);
174 Grob::set_immutable_grob_property (SCM s, SCM v)
176 immutable_property_alist_ = gh_cons (gh_cons (s,v), mutable_property_alist_);
177 mutable_property_alist_ = scm_assq_remove_x (mutable_property_alist_, s);
180 Grob::set_grob_property (SCM s, SCM v)
182 mutable_property_alist_ = scm_assq_set_x (mutable_property_alist_, s, v);
186 MAKE_SCHEME_CALLBACK (Grob,molecule_extent,2);
188 Grob::molecule_extent (SCM element_smob, SCM scm_axis)
190 Grob *s = unsmob_grob (element_smob);
191 Axis a = (Axis) gh_scm2int (scm_axis);
193 Molecule *m = s->get_molecule ();
197 return ly_interval2scm (e);
200 MAKE_SCHEME_CALLBACK (Grob,preset_extent,2);
203 Grob::preset_extent (SCM element_smob, SCM scm_axis)
205 Grob *s = unsmob_grob (element_smob);
206 Axis a = (Axis) gh_scm2int (scm_axis);
208 SCM ext = s->get_grob_property ((a == X_AXIS)
214 Real l = gh_scm2double (ly_car (ext));
215 Real r = gh_scm2double (ly_cdr (ext));
216 return ly_interval2scm (Interval (l, r));
219 return ly_interval2scm (Interval ());
225 Grob::paper_l () const
227 return pscore_l_ ? pscore_l_->paper_l_ : 0;
231 Grob::calculate_dependencies (int final, int busy, SCM funcname)
233 if (status_c_ >= final)
236 if (status_c_== busy)
238 programming_error ("Element is busy, come back later");
244 for (SCM d= get_grob_property ("dependencies"); gh_pair_p (d); d = ly_cdr (d))
246 unsmob_grob (ly_car (d))
247 ->calculate_dependencies (final, busy, funcname);
251 String s = ly_symbol2string (funcname);
252 SCM proc = get_grob_property (s.ch_C ());
253 if (gh_procedure_p (proc))
254 gh_call1 (proc, this->self_scm ());
261 Grob::get_molecule () const
263 SCM mol = get_grob_property ("molecule");
264 if (unsmob_molecule (mol))
265 return unsmob_molecule (mol);
267 mol = get_uncached_molecule ();
269 Grob *me = (Grob*)this;
270 me->set_grob_property ("molecule", mol);
272 return unsmob_molecule (mol);
275 Grob::get_uncached_molecule ()const
277 SCM proc = get_grob_property ("molecule-callback");
280 if (gh_procedure_p (proc))
281 mol = gh_apply (proc, scm_list_n (this->self_scm (), SCM_UNDEFINED));
284 Molecule *m = unsmob_molecule (mol);
286 if (unsmob_molecule (mol))
289 TODO: add option for not copying origin info.
291 SCM origin =get_grob_property ("origin");
292 if (!unsmob_input (origin))
293 origin =ly_symbol2scm ("no-origin");
298 mol = Molecule (m->extent_box (),
299 scm_list_n (origin, m->get_expr (), SCM_UNDEFINED)
302 m = unsmob_molecule (mol);
306 transparent retains dimensions of element.
308 if (m && to_boolean (get_grob_property ("transparent")))
309 mol = Molecule (m->extent_box (), SCM_EOL).smobbed_copy ();
320 Grob::do_break_processing ()
330 Grob::line_l () const
336 Grob::add_dependency (Grob*e)
340 Pointer_group_interface ::add_element (this, "dependencies",e);
344 programming_error ("Null dependency added");
351 Do break substitution in S, using CRITERION. Return new value.
352 CRITERION is either a SMOB pointer to the desired line, or a number
353 representing the break direction. Do not modify SRC.
355 It is rather tightly coded, since it takes a lot of time.
358 Grob::handle_broken_grobs (SCM src, SCM criterion)
361 Grob *sc = unsmob_grob (src);
364 if (gh_number_p (criterion))
366 Item * i = dynamic_cast<Item*> (sc);
367 Direction d = to_dir (criterion);
368 if (i && i->break_status_dir () != d)
370 Item *br = i->find_prebroken_piece (d);
371 return (br) ? br->self_scm () : SCM_UNDEFINED;
377 = dynamic_cast<Line_of_score*> (unsmob_grob (criterion));
378 if (sc->line_l () != line)
380 sc = sc->find_broken_piece (line);
384 /* now: !sc || (sc && sc->line_l () == line) */
386 return SCM_UNDEFINED;
388 /* now: sc && sc->line_l () == line */
391 This was introduced in 1.3.49 as a measure to prevent
392 programming errors. It looks expensive (?). TODO:
393 benchmark , document when (what kind of programming
394 errors) this happens.
396 || (sc->common_refpoint (line, X_AXIS)
397 && sc->common_refpoint (line, Y_AXIS)))
399 return sc->self_scm ();
401 return SCM_UNDEFINED;
404 else if (gh_pair_p (src))
406 SCM oldcar =ly_car (src);
408 UGH! breaks on circular lists.
410 SCM newcar = handle_broken_grobs (oldcar, criterion);
411 SCM oldcdr = ly_cdr (src);
413 if (newcar == SCM_UNDEFINED
414 && (gh_pair_p (oldcdr) || oldcdr == SCM_EOL))
417 This is tail-recursion, ie.
419 return handle_broken_grobs (cdr, criterion);
421 We don't want to rely on the compiler to do this. Without
422 tail-recursion, this easily crashes with a stack overflow. */
427 SCM newcdr = handle_broken_grobs (oldcdr, criterion);
428 return gh_cons (newcar, newcdr);
437 Grob::handle_broken_dependencies ()
439 Spanner * s= dynamic_cast<Spanner*> (this);
440 if (original_l_ && s)
445 for (int i = 0; i< s->broken_into_l_arr_ .size (); i++)
447 Grob * sc = s->broken_into_l_arr_[i];
448 Line_of_score * l = sc->line_l ();
449 sc->mutable_property_alist_ =
450 handle_broken_grobs (mutable_property_alist_,
451 l ? l->self_scm () : SCM_UNDEFINED);
456 Line_of_score *line = line_l ();
458 if (line && common_refpoint (line, X_AXIS) && common_refpoint (line, Y_AXIS))
460 mutable_property_alist_
461 = handle_broken_grobs (mutable_property_alist_,
462 line ? line->self_scm () : SCM_UNDEFINED);
464 else if (dynamic_cast <Line_of_score*> (this))
466 mutable_property_alist_ = handle_broken_grobs (mutable_property_alist_,
472 This element is `invalid'; it has been removed from all
473 dependencies, so let's junk the element itself.
475 do not do this for Line_of_score, since that would remove
476 references to the originals of score-elts, which get then GC'd
484 Note that we still want references to this element to be
485 rearranged, and not silently thrown away, so we keep pointers
486 like {broken_into_{drul,array}, original}
491 mutable_property_alist_ = SCM_EOL;
492 immutable_property_alist_ = SCM_EOL;
494 set_extent_callback (SCM_EOL, Y_AXIS);
495 set_extent_callback (SCM_EOL, X_AXIS);
497 for (int a= X_AXIS; a <= Y_AXIS; a++)
499 dim_cache_[a].offset_callbacks_ = SCM_EOL;
500 dim_cache_[a].offsets_left_ = 0;
505 Grob::handle_prebroken_dependencies ()
510 Grob::find_broken_piece (Line_of_score*) const
516 Grob::translate_axis (Real y, Axis a)
518 if (isinf (y) || isnan (y))
519 programming_error (_ (INFINITY_MSG));
522 dim_cache_[a].offset_ += y;
527 Grob::relative_coordinate (Grob const*refp, Axis a) const
533 We catch PARENT_L_ == nil case with this, but we crash if we did
534 not ask for the absolute coordinate (ie. REFP == nil.)
537 if (refp == dim_cache_[a].parent_l_)
538 return get_offset (a);
540 return get_offset (a) + dim_cache_[a].parent_l_->relative_coordinate (refp, a);
544 Grob::get_offset (Axis a) const
546 Grob *me = (Grob*) this;
547 while (dim_cache_[a].offsets_left_)
549 int l = --me->dim_cache_[a].offsets_left_;
550 SCM cb = scm_list_ref (dim_cache_[a].offset_callbacks_, gh_int2scm (l));
551 SCM retval = gh_call2 (cb, self_scm (), gh_int2scm (a));
553 Real r = gh_scm2double (retval);
554 if (isinf (r) || isnan (r))
556 programming_error (INFINITY_MSG);
559 me->dim_cache_[a].offset_ +=r;
561 return dim_cache_[a].offset_;
565 MAKE_SCHEME_CALLBACK (Grob,point_dimension_callback,2);
567 Grob::point_dimension_callback (SCM , SCM)
569 return ly_interval2scm (Interval (0,0));
573 Grob::empty_b (Axis a)const
575 return ! (gh_pair_p (dim_cache_[a].dimension_) ||
576 gh_procedure_p (dim_cache_[a].dimension_));
587 Grob::extent (Grob * refp, Axis a) const
589 Real x = relative_coordinate (refp, a);
592 Dimension_cache * d = (Dimension_cache *)&dim_cache_[a];
594 if (gh_pair_p (d->dimension_))
596 else if (gh_procedure_p (d->dimension_))
599 FIXME: add doco on types, and should typecheck maybe?
601 d->dimension_= gh_call2 (d->dimension_, self_scm (), gh_int2scm (a));
606 if (!gh_pair_p (d->dimension_))
609 ext = ly_scm2interval (d->dimension_);
611 SCM extra = get_grob_property (a == X_AXIS
618 if (gh_pair_p (extra))
620 ext[BIGGER] += gh_scm2double (ly_cdr (extra));
621 ext[SMALLER] += gh_scm2double (ly_car (extra));
624 extra = get_grob_property (a == X_AXIS
626 : "minimum-extent-Y");
627 if (gh_pair_p (extra))
629 ext.unite (Interval (gh_scm2double (ly_car (extra)),
630 gh_scm2double (ly_cdr (extra))));
640 Grob::parent_l (Axis a) const
642 return dim_cache_[a].parent_l_;
646 Grob::common_refpoint (Grob const* s, Axis a) const
649 I don't like the quadratic aspect of this code, but I see no other
650 way. The largest chain of parents might be 10 high or so, so
651 it shouldn't be a real issue. */
652 for (Grob const *c = this; c; c = c->dim_cache_[a].parent_l_)
653 for (Grob const * d = s; d; d = d->dim_cache_[a].parent_l_)
662 Grob::common_refpoint (SCM elist, Axis a) const
664 Grob * common = (Grob*) this;
665 for (; gh_pair_p (elist); elist = ly_cdr (elist))
667 Grob * s = unsmob_grob (ly_car (elist));
669 common = common->common_refpoint (s, a);
678 SCM meta = get_grob_property ("meta");
679 SCM nm = scm_assoc (ly_symbol2scm ("name"), meta);
680 nm = (gh_pair_p (nm)) ? ly_cdr (nm) : SCM_EOL;
681 return gh_symbol_p (nm) ? ly_symbol2string (nm) : classname (this);
685 Grob::add_offset_callback (SCM cb, Axis a)
687 if (!has_offset_callback_b (cb, a))
689 dim_cache_[a].offset_callbacks_ = gh_cons (cb, dim_cache_[a].offset_callbacks_);
690 dim_cache_[a].offsets_left_ ++;
695 Grob::has_extent_callback_b (SCM cb, Axis a)const
697 return scm_equal_p (cb, dim_cache_[a].dimension_) == SCM_BOOL_T;
702 Grob::has_extent_callback_b (Axis a) const
704 return gh_procedure_p (dim_cache_[a].dimension_);
708 Grob::has_offset_callback_b (SCM cb, Axis a)const
710 return scm_memq (cb, dim_cache_[a].offset_callbacks_) != SCM_BOOL_F;
714 Grob::set_extent_callback (SCM dc, Axis a)
716 dim_cache_[a].dimension_ =dc;
720 Grob::set_parent (Grob *g, Axis a)
722 dim_cache_[a].parent_l_ = g;
725 MAKE_SCHEME_CALLBACK (Grob,fixup_refpoint,1);
727 Grob::fixup_refpoint (SCM smob)
729 Grob *me = unsmob_grob (smob);
730 for (int a = X_AXIS; a < NO_AXES; a ++)
733 Grob * parent = me->parent_l (ax);
738 if (parent->line_l () != me->line_l () && me->line_l ())
740 Grob * newparent = parent->find_broken_piece (me->line_l ());
741 me->set_parent (newparent, ax);
744 if (Item * i = dynamic_cast<Item*> (me))
746 Item *parenti = dynamic_cast<Item*> (parent);
750 Direction my_dir = i->break_status_dir () ;
751 if (my_dir!= parenti->break_status_dir ())
753 Item *newparent = parenti->find_prebroken_piece (my_dir);
754 me->set_parent (newparent, ax);
764 /****************************************************
766 ****************************************************/
769 IMPLEMENT_UNSMOB (Grob, grob);
770 IMPLEMENT_SMOBS (Grob);
771 IMPLEMENT_DEFAULT_EQUAL_P (Grob);
774 Grob::mark_smob (SCM ses)
776 Grob * s = (Grob*) SCM_CELL_WORD_1 (ses);
777 scm_gc_mark (s->immutable_property_alist_);
778 scm_gc_mark (s->mutable_property_alist_);
780 for (int a =0 ; a < 2; a++)
782 scm_gc_mark (s->dim_cache_[a].offset_callbacks_);
783 scm_gc_mark (s->dim_cache_[a].dimension_);
786 if (s->parent_l (Y_AXIS))
787 scm_gc_mark (s->parent_l (Y_AXIS)->self_scm ());
788 if (s->parent_l (X_AXIS))
789 scm_gc_mark (s->parent_l (X_AXIS)->self_scm ());
792 scm_gc_mark (s->original_l_->self_scm ());
793 return s->do_derived_mark ();
797 Grob::print_smob (SCM s, SCM port, scm_print_state *)
799 Grob *sc = (Grob *) ly_cdr (s);
801 scm_puts ("#<Grob ", port);
802 scm_puts ((char *)sc->name ().ch_C (), port);
805 don't try to print properties, that is too much hassle.
807 scm_puts (" >", port);
812 Grob::do_derived_mark ()
819 ly_set_grob_property (SCM elt, SCM sym, SCM val)
821 Grob * sc = unsmob_grob (elt);
823 if (!gh_symbol_p (sym))
825 error ("Not a symbol");
826 ly_display_scm (sym);
827 return SCM_UNSPECIFIED;
832 sc->set_grob_property (sym, val);
836 error ("Not a score element");
837 ly_display_scm (elt);
840 return SCM_UNSPECIFIED;
845 ly_get_grob_property (SCM elt, SCM sym)
847 Grob * sc = unsmob_grob (elt);
851 return sc->get_grob_property (sym);
855 error ("Not a score element");
856 ly_display_scm (elt);
858 return SCM_UNSPECIFIED;
863 Grob::discretionary_processing ()
870 spanner_get_bound (SCM slur, SCM dir)
872 return dynamic_cast<Spanner*> (unsmob_grob (slur))->get_bound (to_dir (dir))->self_scm ();
877 static SCM interfaces_sym;
881 interfaces_sym = scm_permanent_object (ly_symbol2scm ("interfaces"));
883 scm_c_define_gsubr ("ly-get-grob-property", 2, 0, 0,
884 (Scheme_function_unknown)ly_get_grob_property);
885 scm_c_define_gsubr ("ly-set-grob-property", 3, 0, 0,
886 (Scheme_function_unknown)ly_set_grob_property);
887 scm_c_define_gsubr ("ly-get-spanner-bound", 2 , 0, 0,
888 (Scheme_function_unknown) spanner_get_bound);
892 Grob::has_interface (SCM k)
894 SCM ifs = get_grob_property (interfaces_sym);
896 return scm_memq (k, ifs) != SCM_BOOL_F;
900 Grob::set_interface (SCM k)
902 if (has_interface (k))
906 set_grob_property (interfaces_sym,
907 gh_cons (k, get_grob_property (interfaces_sym)));
912 ADD_SCM_INIT_FUNC (scoreelt, init_functions);
913 IMPLEMENT_TYPE_P (Grob, "ly-grob?");