2 grob.cc -- implement Grob
4 source file of the GNU LilyPond music typesetter
6 (c) 1997--2002 Han-Wen Nienhuys <hanwen@cs.uu.nl>
14 #include "input-smob.hh"
16 #include "group-interface.hh"
18 #include "paper-score.hh"
19 #include "paper-def.hh"
20 #include "molecule.hh"
24 #include "line-of-score.hh"
26 #include "paper-column.hh"
27 #include "molecule.hh"
29 #include "paper-outputter.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);
172 Grob::internal_set_grob_property (SCM s, SCM v)
175 if (internal_type_checking_global_b)
176 assert (type_check_assignment (s, v, ly_symbol2scm ("backend-type?")));
180 mutable_property_alist_ = scm_assq_set_x (mutable_property_alist_, s, v);
184 MAKE_SCHEME_CALLBACK (Grob,molecule_extent,2);
186 Grob::molecule_extent (SCM element_smob, SCM scm_axis)
188 Grob *s = unsmob_grob (element_smob);
189 Axis a = (Axis) gh_scm2int (scm_axis);
191 Molecule *m = s->get_molecule ();
195 return ly_interval2scm (e);
198 MAKE_SCHEME_CALLBACK (Grob,preset_extent,2);
201 Grob::preset_extent (SCM element_smob, SCM scm_axis)
203 Grob *s = unsmob_grob (element_smob);
204 Axis a = (Axis) gh_scm2int (scm_axis);
206 SCM ext = s->get_grob_property ((a == X_AXIS)
212 Real l = gh_scm2double (ly_car (ext));
213 Real r = gh_scm2double (ly_cdr (ext));
214 return ly_interval2scm (Interval (l, r));
217 return ly_interval2scm (Interval ());
223 Grob::paper_l () const
225 return pscore_l_ ? pscore_l_->paper_l_ : 0;
229 Grob::calculate_dependencies (int final, int busy, SCM funcname)
231 if (status_c_ >= final)
234 if (status_c_== busy)
236 programming_error ("Element is busy, come back later");
242 for (SCM d= get_grob_property ("dependencies"); gh_pair_p (d); d = ly_cdr (d))
244 unsmob_grob (ly_car (d))
245 ->calculate_dependencies (final, busy, funcname);
249 String s = ly_symbol2string (funcname);
250 SCM proc = get_grob_property (s.ch_C ());
251 if (gh_procedure_p (proc))
252 gh_call1 (proc, this->self_scm ());
259 Grob::get_molecule () const
261 SCM mol = get_grob_property ("molecule");
262 if (unsmob_molecule (mol))
263 return unsmob_molecule (mol);
265 mol = get_uncached_molecule ();
267 Grob *me = (Grob*)this;
268 me->set_grob_property ("molecule", mol);
270 return unsmob_molecule (mol);
273 Grob::get_uncached_molecule ()const
275 SCM proc = get_grob_property ("molecule-callback");
278 if (gh_procedure_p (proc))
279 mol = gh_apply (proc, scm_list_n (this->self_scm (), SCM_UNDEFINED));
282 Molecule *m = unsmob_molecule (mol);
284 if (unsmob_molecule (mol))
286 SCM origin = ly_symbol2scm ("no-origin");
288 if (store_locations_global_b){
289 SCM cause = get_grob_property ("cause");
290 if (Music*m = unsmob_music (cause))
292 SCM music_origin = m->get_mus_property ("origin");
293 if (unsmob_input (music_origin))
294 origin = music_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_grob (this, ly_symbol2scm ("dependencies"),e);
345 programming_error ("Null dependency added");
352 Do break substitution in S, using CRITERION. Return new value.
353 CRITERION is either a SMOB pointer to the desired line, or a number
354 representing the break direction. Do not modify SRC.
356 It is rather tightly coded, since it takes a lot of time; it is
357 one of the top functions in the profile.
361 Grob::handle_broken_grobs (SCM src, SCM criterion)
364 Grob *sc = unsmob_grob (src);
367 if (SCM_INUMP (criterion))
369 Item * i = dynamic_cast<Item*> (sc);
370 Direction d = to_dir (criterion);
371 if (i && i->break_status_dir () != d)
373 Item *br = i->find_prebroken_piece (d);
374 return (br) ? br->self_scm () : SCM_UNDEFINED;
380 = dynamic_cast<Line_of_score*> (unsmob_grob (criterion));
381 if (sc->line_l () != line)
383 sc = sc->find_broken_piece (line);
387 /* now: !sc || (sc && sc->line_l () == line) */
389 return SCM_UNDEFINED;
391 /* now: sc && sc->line_l () == line */
393 return sc->self_scm();
395 This was introduced in 1.3.49 as a measure to prevent
396 programming errors. It looks expensive (?). TODO:
397 benchmark , document when (what kind of programming
398 errors) this happens.
400 if (sc->common_refpoint (line, X_AXIS)
401 && sc->common_refpoint (line, Y_AXIS))
403 return sc->self_scm ();
405 return SCM_UNDEFINED;
408 else if (ly_pair_p (src)) // SCM_CONSP (src)) // huh?
410 SCM oldcar =ly_car (src);
412 UGH! breaks on circular lists.
414 SCM newcar = handle_broken_grobs (oldcar, criterion);
415 SCM oldcdr = ly_cdr (src);
417 if (newcar == SCM_UNDEFINED
418 && (gh_pair_p (oldcdr) || oldcdr == SCM_EOL))
421 This is tail-recursion, ie.
423 return handle_broken_grobs (cdr, criterion);
425 We don't want to rely on the compiler to do this. Without
426 tail-recursion, this easily crashes with a stack overflow. */
431 SCM newcdr = handle_broken_grobs (oldcdr, criterion);
432 return scm_cons (newcar, newcdr);
441 Grob::handle_broken_dependencies ()
443 Spanner * s= dynamic_cast<Spanner*> (this);
444 if (original_l_ && s)
449 for (int i = 0; i< s->broken_into_l_arr_ .size (); i++)
451 Grob * sc = s->broken_into_l_arr_[i];
452 Line_of_score * l = sc->line_l ();
453 sc->mutable_property_alist_ =
454 handle_broken_grobs (mutable_property_alist_,
455 l ? l->self_scm () : SCM_UNDEFINED);
460 Line_of_score *line = line_l ();
462 if (line && common_refpoint (line, X_AXIS) && common_refpoint (line, Y_AXIS))
464 mutable_property_alist_
465 = handle_broken_grobs (mutable_property_alist_,
466 line ? line->self_scm () : SCM_UNDEFINED);
468 else if (dynamic_cast <Line_of_score*> (this))
470 mutable_property_alist_ = handle_broken_grobs (mutable_property_alist_,
476 This element is `invalid'; it has been removed from all
477 dependencies, so let's junk the element itself.
479 do not do this for Line_of_score, since that would remove
480 references to the originals of score-elts, which get then GC'd
488 Note that we still want references to this element to be
489 rearranged, and not silently thrown away, so we keep pointers
490 like {broken_into_{drul,array}, original}
495 mutable_property_alist_ = SCM_EOL;
496 immutable_property_alist_ = SCM_EOL;
498 set_extent_callback (SCM_EOL, Y_AXIS);
499 set_extent_callback (SCM_EOL, X_AXIS);
501 for (int a= X_AXIS; a <= Y_AXIS; a++)
503 dim_cache_[a].offset_callbacks_ = SCM_EOL;
504 dim_cache_[a].offsets_left_ = 0;
509 Grob::handle_prebroken_dependencies ()
514 Grob::find_broken_piece (Line_of_score*) const
520 Grob::translate_axis (Real y, Axis a)
522 if (isinf (y) || isnan (y))
523 programming_error (_ (INFINITY_MSG));
526 dim_cache_[a].offset_ += y;
531 Grob::relative_coordinate (Grob const*refp, Axis a) const
537 We catch PARENT_L_ == nil case with this, but we crash if we did
538 not ask for the absolute coordinate (ie. REFP == nil.)
541 if (refp == dim_cache_[a].parent_l_)
542 return get_offset (a);
544 return get_offset (a) + dim_cache_[a].parent_l_->relative_coordinate (refp, a);
548 Grob::get_offset (Axis a) const
550 Grob *me = (Grob*) this;
551 while (dim_cache_[a].offsets_left_)
553 int l = --me->dim_cache_[a].offsets_left_;
554 SCM cb = scm_list_ref (dim_cache_[a].offset_callbacks_, gh_int2scm (l));
555 SCM retval = gh_call2 (cb, self_scm (), gh_int2scm (a));
557 Real r = gh_scm2double (retval);
558 if (isinf (r) || isnan (r))
560 programming_error (INFINITY_MSG);
563 me->dim_cache_[a].offset_ +=r;
565 return dim_cache_[a].offset_;
569 MAKE_SCHEME_CALLBACK (Grob,point_dimension_callback,2);
571 Grob::point_dimension_callback (SCM , SCM)
573 return ly_interval2scm (Interval (0,0));
577 Grob::empty_b (Axis a)const
579 return ! (gh_pair_p (dim_cache_[a].dimension_) ||
580 gh_procedure_p (dim_cache_[a].dimension_));
591 Grob::extent (Grob * refp, Axis a) const
593 Real x = relative_coordinate (refp, a);
596 Dimension_cache * d = (Dimension_cache *)&dim_cache_[a];
598 if (gh_pair_p (d->dimension_))
600 else if (gh_procedure_p (d->dimension_))
603 FIXME: add doco on types, and should typecheck maybe?
605 d->dimension_= gh_call2 (d->dimension_, self_scm (), gh_int2scm (a));
610 if (!gh_pair_p (d->dimension_))
613 ext = ly_scm2interval (d->dimension_);
615 SCM extra = get_grob_property (a == X_AXIS
622 if (gh_pair_p (extra))
624 ext[BIGGER] += gh_scm2double (ly_cdr (extra));
625 ext[SMALLER] += gh_scm2double (ly_car (extra));
628 extra = get_grob_property (a == X_AXIS
630 : "minimum-extent-Y");
631 if (gh_pair_p (extra))
633 ext.unite (Interval (gh_scm2double (ly_car (extra)),
634 gh_scm2double (ly_cdr (extra))));
643 Grob::common_refpoint (Grob const* s, Axis a) const
646 I don't like the quadratic aspect of this code, but I see no other
647 way. The largest chain of parents might be 10 high or so, so
648 it shouldn't be a real issue. */
649 for (Grob const *c = this; c; c = c->dim_cache_[a].parent_l_)
650 for (Grob const * d = s; d; d = d->dim_cache_[a].parent_l_)
659 Grob::common_refpoint (SCM elist, Axis a) const
661 Grob * common = (Grob*) this;
662 for (; gh_pair_p (elist); elist = ly_cdr (elist))
664 Grob * s = unsmob_grob (ly_car (elist));
666 common = common->common_refpoint (s, a);
675 SCM meta = get_grob_property ("meta");
676 SCM nm = scm_assoc (ly_symbol2scm ("name"), meta);
677 nm = (gh_pair_p (nm)) ? ly_cdr (nm) : SCM_EOL;
678 return gh_symbol_p (nm) ? ly_symbol2string (nm) : classname (this);
682 Grob::add_offset_callback (SCM cb, Axis a)
684 if (!has_offset_callback_b (cb, a))
686 dim_cache_[a].offset_callbacks_ = gh_cons (cb, dim_cache_[a].offset_callbacks_);
687 dim_cache_[a].offsets_left_ ++;
692 Grob::has_extent_callback_b (SCM cb, Axis a)const
694 return scm_equal_p (cb, dim_cache_[a].dimension_) == SCM_BOOL_T;
699 Grob::has_extent_callback_b (Axis a) const
701 return gh_procedure_p (dim_cache_[a].dimension_);
705 Grob::has_offset_callback_b (SCM cb, Axis a)const
707 return scm_memq (cb, dim_cache_[a].offset_callbacks_) != SCM_BOOL_F;
711 Grob::set_extent_callback (SCM dc, Axis a)
713 dim_cache_[a].dimension_ =dc;
717 Grob::set_parent (Grob *g, Axis a)
719 dim_cache_[a].parent_l_ = g;
722 MAKE_SCHEME_CALLBACK (Grob,fixup_refpoint,1);
724 Grob::fixup_refpoint (SCM smob)
726 Grob *me = unsmob_grob (smob);
727 for (int a = X_AXIS; a < NO_AXES; a ++)
730 Grob * parent = me->get_parent (ax);
735 if (parent->line_l () != me->line_l () && me->line_l ())
737 Grob * newparent = parent->find_broken_piece (me->line_l ());
738 me->set_parent (newparent, ax);
741 if (Item * i = dynamic_cast<Item*> (me))
743 Item *parenti = dynamic_cast<Item*> (parent);
747 Direction my_dir = i->break_status_dir () ;
748 if (my_dir!= parenti->break_status_dir ())
750 Item *newparent = parenti->find_prebroken_piece (my_dir);
751 me->set_parent (newparent, ax);
761 /****************************************************
763 ****************************************************/
767 IMPLEMENT_SMOBS (Grob);
768 IMPLEMENT_DEFAULT_EQUAL_P (Grob);
771 Grob::mark_smob (SCM ses)
773 Grob * s = (Grob*) SCM_CELL_WORD_1 (ses);
774 scm_gc_mark (s->immutable_property_alist_);
775 scm_gc_mark (s->mutable_property_alist_);
777 for (int a =0 ; a < 2; a++)
779 scm_gc_mark (s->dim_cache_[a].offset_callbacks_);
780 scm_gc_mark (s->dim_cache_[a].dimension_);
781 Grob *p = s->get_parent (Y_AXIS);
783 scm_gc_mark (p->self_scm ());
787 scm_gc_mark (s->original_l_->self_scm ());
789 return s->do_derived_mark ();
793 Grob::print_smob (SCM s, SCM port, scm_print_state *)
795 Grob *sc = (Grob *) ly_cdr (s);
797 scm_puts ("#<Grob ", port);
798 scm_puts ((char *)sc->name ().ch_C (), port);
801 don't try to print properties, that is too much hassle.
803 scm_puts (" >", port);
808 Grob::do_derived_mark ()
815 ly_set_grob_property (SCM elt, SCM sym, SCM val)
817 Grob * sc = unsmob_grob (elt);
819 if (!gh_symbol_p (sym))
821 error ("Not a symbol");
822 ly_display_scm (sym);
823 return SCM_UNSPECIFIED;
826 if (!type_check_assignment (sym, val, ly_symbol2scm ("backend-type?")))
827 return SCM_UNSPECIFIED;
831 sc->internal_set_grob_property (sym, val);
835 error ("Not a score element");
836 ly_display_scm (elt);
839 return SCM_UNSPECIFIED;
844 ly_get_grob_property (SCM elt, SCM sym)
846 Grob * sc = unsmob_grob (elt);
850 return sc->internal_get_grob_property (sym);
854 error ("Not a score element");
855 ly_display_scm (elt);
857 return SCM_UNSPECIFIED;
862 Grob::discretionary_processing ()
869 spanner_get_bound (SCM slur, SCM dir)
871 return dynamic_cast<Spanner*> (unsmob_grob (slur))->get_bound (to_dir (dir))->self_scm ();
880 scm_c_define_gsubr ("ly-get-grob-property", 2, 0, 0,
881 (Scheme_function_unknown)ly_get_grob_property);
882 scm_c_define_gsubr ("ly-set-grob-property", 3, 0, 0,
883 (Scheme_function_unknown)ly_set_grob_property);
884 scm_c_define_gsubr ("ly-get-spanner-bound", 2 , 0, 0,
885 (Scheme_function_unknown) spanner_get_bound);
889 Grob::has_interface (SCM k)
891 SCM ifs = get_grob_property ("interfaces");
893 return scm_memq (k, ifs) != SCM_BOOL_F;
897 Grob::set_interface (SCM k)
899 if (has_interface (k))
903 set_grob_property ("interfaces",
904 gh_cons (k, get_grob_property ("interfaces")));
909 ADD_SCM_INIT_FUNC (scoreelt, init_functions);
910 IMPLEMENT_TYPE_P (Grob, "ly-grob?");