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::internal_get_grob_property (SCM sym) const
127 SCM s = scm_sloppy_assq (sym, mutable_property_alist_);
131 s = scm_sloppy_assq (sym, immutable_property_alist_);
132 return (s == SCM_BOOL_F) ? SCM_EOL : ly_cdr (s);
136 Remove the value associated with KEY, and return it. The result is
137 that a next call will yield SCM_EOL (and not the underlying
141 Grob::remove_grob_property (const char* key)
143 SCM val = get_grob_property (key);
145 set_grob_property (key, SCM_EOL);
152 Puts the k, v in the immutable_property_alist_, which is convenient for
153 storing variables that are needed during the breaking process. (eg.
154 Line_of_score::rank : int)
157 Grob::set_immutable_grob_property (const char*k, SCM v)
159 SCM s = ly_symbol2scm (k);
160 set_immutable_grob_property (s, v);
164 Grob::set_immutable_grob_property (SCM s, SCM v)
166 immutable_property_alist_ = gh_cons (gh_cons (s,v), mutable_property_alist_);
167 mutable_property_alist_ = scm_assq_remove_x (mutable_property_alist_, s);
173 Grob::internal_set_grob_property (SCM s, SCM v)
175 mutable_property_alist_ = scm_assq_set_x (mutable_property_alist_, s, v);
179 MAKE_SCHEME_CALLBACK (Grob,molecule_extent,2);
181 Grob::molecule_extent (SCM element_smob, SCM scm_axis)
183 Grob *s = unsmob_grob (element_smob);
184 Axis a = (Axis) gh_scm2int (scm_axis);
186 Molecule *m = s->get_molecule ();
190 return ly_interval2scm (e);
193 MAKE_SCHEME_CALLBACK (Grob,preset_extent,2);
196 Grob::preset_extent (SCM element_smob, SCM scm_axis)
198 Grob *s = unsmob_grob (element_smob);
199 Axis a = (Axis) gh_scm2int (scm_axis);
201 SCM ext = s->get_grob_property ((a == X_AXIS)
207 Real l = gh_scm2double (ly_car (ext));
208 Real r = gh_scm2double (ly_cdr (ext));
209 return ly_interval2scm (Interval (l, r));
212 return ly_interval2scm (Interval ());
218 Grob::paper_l () const
220 return pscore_l_ ? pscore_l_->paper_l_ : 0;
224 Grob::calculate_dependencies (int final, int busy, SCM funcname)
226 if (status_c_ >= final)
229 if (status_c_== busy)
231 programming_error ("Element is busy, come back later");
237 for (SCM d= get_grob_property ("dependencies"); gh_pair_p (d); d = ly_cdr (d))
239 unsmob_grob (ly_car (d))
240 ->calculate_dependencies (final, busy, funcname);
244 String s = ly_symbol2string (funcname);
245 SCM proc = get_grob_property (s.ch_C ());
246 if (gh_procedure_p (proc))
247 gh_call1 (proc, this->self_scm ());
254 Grob::get_molecule () const
256 SCM mol = get_grob_property ("molecule");
257 if (unsmob_molecule (mol))
258 return unsmob_molecule (mol);
260 mol = get_uncached_molecule ();
262 Grob *me = (Grob*)this;
263 me->set_grob_property ("molecule", mol);
265 return unsmob_molecule (mol);
268 Grob::get_uncached_molecule ()const
270 SCM proc = get_grob_property ("molecule-callback");
273 if (gh_procedure_p (proc))
274 mol = gh_apply (proc, scm_list_n (this->self_scm (), SCM_UNDEFINED));
277 Molecule *m = unsmob_molecule (mol);
279 if (unsmob_molecule (mol))
282 TODO: add option for not copying origin info.
284 SCM origin =get_grob_property ("origin");
285 if (!unsmob_input (origin))
286 origin =ly_symbol2scm ("no-origin");
291 mol = Molecule (m->extent_box (),
292 scm_list_n (origin, m->get_expr (), SCM_UNDEFINED)
295 m = unsmob_molecule (mol);
299 transparent retains dimensions of element.
301 if (m && to_boolean (get_grob_property ("transparent")))
302 mol = Molecule (m->extent_box (), SCM_EOL).smobbed_copy ();
313 Grob::do_break_processing ()
323 Grob::line_l () const
329 Grob::add_dependency (Grob*e)
333 Pointer_group_interface ::add_element (this, "dependencies",e);
337 programming_error ("Null dependency added");
344 Do break substitution in S, using CRITERION. Return new value.
345 CRITERION is either a SMOB pointer to the desired line, or a number
346 representing the break direction. Do not modify SRC.
348 It is rather tightly coded, since it takes a lot of time; it is
349 one of the top functions in the profile.
353 Grob::handle_broken_grobs (SCM src, SCM criterion)
356 Grob *sc = unsmob_grob (src);
359 if (SCM_INUMP (criterion))
361 Item * i = dynamic_cast<Item*> (sc);
362 Direction d = to_dir (criterion);
363 if (i && i->break_status_dir () != d)
365 Item *br = i->find_prebroken_piece (d);
366 return (br) ? br->self_scm () : SCM_UNDEFINED;
372 = dynamic_cast<Line_of_score*> (unsmob_grob (criterion));
373 if (sc->line_l () != line)
375 sc = sc->find_broken_piece (line);
379 /* now: !sc || (sc && sc->line_l () == line) */
381 return SCM_UNDEFINED;
383 /* now: sc && sc->line_l () == line */
385 return sc->self_scm();
387 This was introduced in 1.3.49 as a measure to prevent
388 programming errors. It looks expensive (?). TODO:
389 benchmark , document when (what kind of programming
390 errors) this happens.
392 if (sc->common_refpoint (line, X_AXIS)
393 && sc->common_refpoint (line, Y_AXIS))
395 return sc->self_scm ();
397 return SCM_UNDEFINED;
400 else if (ly_pair_p (src)) // SCM_CONSP (src)) // huh?
402 SCM oldcar =ly_car (src);
404 UGH! breaks on circular lists.
406 SCM newcar = handle_broken_grobs (oldcar, criterion);
407 SCM oldcdr = ly_cdr (src);
409 if (newcar == SCM_UNDEFINED
410 && (gh_pair_p (oldcdr) || oldcdr == SCM_EOL))
413 This is tail-recursion, ie.
415 return handle_broken_grobs (cdr, criterion);
417 We don't want to rely on the compiler to do this. Without
418 tail-recursion, this easily crashes with a stack overflow. */
423 SCM newcdr = handle_broken_grobs (oldcdr, criterion);
424 return scm_cons (newcar, newcdr);
433 Grob::handle_broken_dependencies ()
435 Spanner * s= dynamic_cast<Spanner*> (this);
436 if (original_l_ && s)
441 for (int i = 0; i< s->broken_into_l_arr_ .size (); i++)
443 Grob * sc = s->broken_into_l_arr_[i];
444 Line_of_score * l = sc->line_l ();
445 sc->mutable_property_alist_ =
446 handle_broken_grobs (mutable_property_alist_,
447 l ? l->self_scm () : SCM_UNDEFINED);
452 Line_of_score *line = line_l ();
454 if (line && common_refpoint (line, X_AXIS) && common_refpoint (line, Y_AXIS))
456 mutable_property_alist_
457 = handle_broken_grobs (mutable_property_alist_,
458 line ? line->self_scm () : SCM_UNDEFINED);
460 else if (dynamic_cast <Line_of_score*> (this))
462 mutable_property_alist_ = handle_broken_grobs (mutable_property_alist_,
468 This element is `invalid'; it has been removed from all
469 dependencies, so let's junk the element itself.
471 do not do this for Line_of_score, since that would remove
472 references to the originals of score-elts, which get then GC'd
480 Note that we still want references to this element to be
481 rearranged, and not silently thrown away, so we keep pointers
482 like {broken_into_{drul,array}, original}
487 mutable_property_alist_ = SCM_EOL;
488 immutable_property_alist_ = SCM_EOL;
490 set_extent_callback (SCM_EOL, Y_AXIS);
491 set_extent_callback (SCM_EOL, X_AXIS);
493 for (int a= X_AXIS; a <= Y_AXIS; a++)
495 dim_cache_[a].offset_callbacks_ = SCM_EOL;
496 dim_cache_[a].offsets_left_ = 0;
501 Grob::handle_prebroken_dependencies ()
506 Grob::find_broken_piece (Line_of_score*) const
512 Grob::translate_axis (Real y, Axis a)
514 if (isinf (y) || isnan (y))
515 programming_error (_ (INFINITY_MSG));
518 dim_cache_[a].offset_ += y;
523 Grob::relative_coordinate (Grob const*refp, Axis a) const
529 We catch PARENT_L_ == nil case with this, but we crash if we did
530 not ask for the absolute coordinate (ie. REFP == nil.)
533 if (refp == dim_cache_[a].parent_l_)
534 return get_offset (a);
536 return get_offset (a) + dim_cache_[a].parent_l_->relative_coordinate (refp, a);
540 Grob::get_offset (Axis a) const
542 Grob *me = (Grob*) this;
543 while (dim_cache_[a].offsets_left_)
545 int l = --me->dim_cache_[a].offsets_left_;
546 SCM cb = scm_list_ref (dim_cache_[a].offset_callbacks_, gh_int2scm (l));
547 SCM retval = gh_call2 (cb, self_scm (), gh_int2scm (a));
549 Real r = gh_scm2double (retval);
550 if (isinf (r) || isnan (r))
552 programming_error (INFINITY_MSG);
555 me->dim_cache_[a].offset_ +=r;
557 return dim_cache_[a].offset_;
561 MAKE_SCHEME_CALLBACK (Grob,point_dimension_callback,2);
563 Grob::point_dimension_callback (SCM , SCM)
565 return ly_interval2scm (Interval (0,0));
569 Grob::empty_b (Axis a)const
571 return ! (gh_pair_p (dim_cache_[a].dimension_) ||
572 gh_procedure_p (dim_cache_[a].dimension_));
583 Grob::extent (Grob * refp, Axis a) const
585 Real x = relative_coordinate (refp, a);
588 Dimension_cache * d = (Dimension_cache *)&dim_cache_[a];
590 if (gh_pair_p (d->dimension_))
592 else if (gh_procedure_p (d->dimension_))
595 FIXME: add doco on types, and should typecheck maybe?
597 d->dimension_= gh_call2 (d->dimension_, self_scm (), gh_int2scm (a));
602 if (!gh_pair_p (d->dimension_))
605 ext = ly_scm2interval (d->dimension_);
607 SCM extra = get_grob_property (a == X_AXIS
614 if (gh_pair_p (extra))
616 ext[BIGGER] += gh_scm2double (ly_cdr (extra));
617 ext[SMALLER] += gh_scm2double (ly_car (extra));
620 extra = get_grob_property (a == X_AXIS
622 : "minimum-extent-Y");
623 if (gh_pair_p (extra))
625 ext.unite (Interval (gh_scm2double (ly_car (extra)),
626 gh_scm2double (ly_cdr (extra))));
635 Grob::common_refpoint (Grob const* s, Axis a) const
638 I don't like the quadratic aspect of this code, but I see no other
639 way. The largest chain of parents might be 10 high or so, so
640 it shouldn't be a real issue. */
641 for (Grob const *c = this; c; c = c->dim_cache_[a].parent_l_)
642 for (Grob const * d = s; d; d = d->dim_cache_[a].parent_l_)
651 Grob::common_refpoint (SCM elist, Axis a) const
653 Grob * common = (Grob*) this;
654 for (; gh_pair_p (elist); elist = ly_cdr (elist))
656 Grob * s = unsmob_grob (ly_car (elist));
658 common = common->common_refpoint (s, a);
667 SCM meta = get_grob_property ("meta");
668 SCM nm = scm_assoc (ly_symbol2scm ("name"), meta);
669 nm = (gh_pair_p (nm)) ? ly_cdr (nm) : SCM_EOL;
670 return gh_symbol_p (nm) ? ly_symbol2string (nm) : classname (this);
674 Grob::add_offset_callback (SCM cb, Axis a)
676 if (!has_offset_callback_b (cb, a))
678 dim_cache_[a].offset_callbacks_ = gh_cons (cb, dim_cache_[a].offset_callbacks_);
679 dim_cache_[a].offsets_left_ ++;
684 Grob::has_extent_callback_b (SCM cb, Axis a)const
686 return scm_equal_p (cb, dim_cache_[a].dimension_) == SCM_BOOL_T;
691 Grob::has_extent_callback_b (Axis a) const
693 return gh_procedure_p (dim_cache_[a].dimension_);
697 Grob::has_offset_callback_b (SCM cb, Axis a)const
699 return scm_memq (cb, dim_cache_[a].offset_callbacks_) != SCM_BOOL_F;
703 Grob::set_extent_callback (SCM dc, Axis a)
705 dim_cache_[a].dimension_ =dc;
709 Grob::set_parent (Grob *g, Axis a)
711 dim_cache_[a].parent_l_ = g;
714 MAKE_SCHEME_CALLBACK (Grob,fixup_refpoint,1);
716 Grob::fixup_refpoint (SCM smob)
718 Grob *me = unsmob_grob (smob);
719 for (int a = X_AXIS; a < NO_AXES; a ++)
722 Grob * parent = me->parent_l (ax);
727 if (parent->line_l () != me->line_l () && me->line_l ())
729 Grob * newparent = parent->find_broken_piece (me->line_l ());
730 me->set_parent (newparent, ax);
733 if (Item * i = dynamic_cast<Item*> (me))
735 Item *parenti = dynamic_cast<Item*> (parent);
739 Direction my_dir = i->break_status_dir () ;
740 if (my_dir!= parenti->break_status_dir ())
742 Item *newparent = parenti->find_prebroken_piece (my_dir);
743 me->set_parent (newparent, ax);
753 /****************************************************
755 ****************************************************/
759 IMPLEMENT_SMOBS (Grob);
760 IMPLEMENT_DEFAULT_EQUAL_P (Grob);
763 Grob::mark_smob (SCM ses)
765 Grob * s = (Grob*) SCM_CELL_WORD_1 (ses);
766 scm_gc_mark (s->immutable_property_alist_);
767 scm_gc_mark (s->mutable_property_alist_);
769 for (int a =0 ; a < 2; a++)
771 scm_gc_mark (s->dim_cache_[a].offset_callbacks_);
772 scm_gc_mark (s->dim_cache_[a].dimension_);
773 Grob *p = s->parent_l (Y_AXIS);
775 scm_gc_mark (p->self_scm ());
779 scm_gc_mark (s->original_l_->self_scm ());
781 return s->do_derived_mark ();
785 Grob::print_smob (SCM s, SCM port, scm_print_state *)
787 Grob *sc = (Grob *) ly_cdr (s);
789 scm_puts ("#<Grob ", port);
790 scm_puts ((char *)sc->name ().ch_C (), port);
793 don't try to print properties, that is too much hassle.
795 scm_puts (" >", port);
800 Grob::do_derived_mark ()
807 ly_set_grob_property (SCM elt, SCM sym, SCM val)
809 Grob * sc = unsmob_grob (elt);
811 if (!gh_symbol_p (sym))
813 error ("Not a symbol");
814 ly_display_scm (sym);
815 return SCM_UNSPECIFIED;
820 sc->internal_set_grob_property (sym, val);
824 error ("Not a score element");
825 ly_display_scm (elt);
828 return SCM_UNSPECIFIED;
833 ly_get_grob_property (SCM elt, SCM sym)
835 Grob * sc = unsmob_grob (elt);
839 return sc->internal_get_grob_property (sym);
843 error ("Not a score element");
844 ly_display_scm (elt);
846 return SCM_UNSPECIFIED;
851 Grob::discretionary_processing ()
858 spanner_get_bound (SCM slur, SCM dir)
860 return dynamic_cast<Spanner*> (unsmob_grob (slur))->get_bound (to_dir (dir))->self_scm ();
869 scm_c_define_gsubr ("ly-get-grob-property", 2, 0, 0,
870 (Scheme_function_unknown)ly_get_grob_property);
871 scm_c_define_gsubr ("ly-set-grob-property", 3, 0, 0,
872 (Scheme_function_unknown)ly_set_grob_property);
873 scm_c_define_gsubr ("ly-get-spanner-bound", 2 , 0, 0,
874 (Scheme_function_unknown) spanner_get_bound);
878 Grob::has_interface (SCM k)
880 SCM ifs = get_grob_property ("interfaces");
882 return scm_memq (k, ifs) != SCM_BOOL_F;
886 Grob::set_interface (SCM k)
888 if (has_interface (k))
892 set_grob_property ("interfaces",
893 gh_cons (k, get_grob_property ("interfaces")));
898 ADD_SCM_INIT_FUNC (scoreelt, init_functions);
899 IMPLEMENT_TYPE_P (Grob, "ly-grob?");