2 score-elem.cc -- implement Score_element
4 source file of the GNU LilyPond music typesetter
6 (c) 1997--2000 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"
20 #include "score-element.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 Score_element::Score_element(SCM basicprops)
48 fixme: default should be no callback.
54 immutable_property_alist_ = basicprops;
55 mutable_property_alist_ = SCM_EOL;
59 char const*onames[] = {"X-offset-callbacks", "Y-offset-callbacks"};
60 char const*enames[] = {"X-extent-callback", "Y-extent-callback"};
62 for (int a = X_AXIS; a <= Y_AXIS; a++){
63 SCM l = get_elt_property (onames[a]);
65 if (scm_ilength (l) >=0)
67 dim_cache_[a].offset_callbacks_ = l;
68 dim_cache_[a].offsets_left_ = scm_ilength (l);
72 programming_error ("[XY]-offset-callbacks must be a list");
75 SCM cb = get_elt_property (enames[a]);
78 Should change default to be empty?
80 if (!gh_procedure_p (cb) && !gh_pair_p (cb))
81 cb = molecule_extent_proc;
83 dim_cache_[a].dimension_ = cb;
86 SCM meta = get_elt_property ("meta");
87 SCM ifs = scm_assoc (ly_symbol2scm ("interfaces"), meta);
89 set_elt_property ("interfaces",gh_cdr (ifs));
93 Score_element::Score_element (Score_element const&s)
94 : dim_cache_ (s.dim_cache_)
96 original_l_ =(Score_element*) &s;
97 immutable_property_alist_ = s.immutable_property_alist_;
98 mutable_property_alist_ = SCM_EOL;
100 status_i_ = s.status_i_;
101 pscore_l_ = s.pscore_l_;
106 Score_element::~Score_element()
109 do nothing scm-ish and no unprotecting here.
115 Score_element::get_elt_property (const char *nm) const
117 SCM sym = ly_symbol2scm (nm);
118 return get_elt_property (sym);
122 Score_element::get_elt_property (SCM sym) const
124 SCM s = scm_sloppy_assq(sym, mutable_property_alist_);
128 s = scm_sloppy_assq (sym, immutable_property_alist_);
129 return (s == SCM_BOOL_F) ? SCM_EOL : gh_cdr (s);
133 Remove the value associated with KEY, and return it. The result is
134 that a next call will yield SCM_UNDEFINED (and not the underlying
138 Score_element::remove_elt_property (const char* key)
140 SCM val = get_elt_property (key);
142 set_elt_property (key, SCM_EOL);
147 Score_element::set_elt_property (const char* k, SCM v)
149 SCM s = ly_symbol2scm (k);
150 set_elt_property (s, v);
154 Puts the k, v in the immutable_property_alist_, which is convenient for
155 storing variables that are needed during the breaking process. (eg.
156 Line_of_score::rank : int )
159 Score_element::set_immutable_elt_property (const char*k, SCM v)
161 SCM s = ly_symbol2scm (k);
162 set_immutable_elt_property (s, v);
166 Score_element::set_immutable_elt_property (SCM s, SCM v)
168 immutable_property_alist_ = gh_cons (gh_cons (s,v), mutable_property_alist_);
169 mutable_property_alist_ = scm_assq_remove_x (mutable_property_alist_, s);
172 Score_element::set_elt_property (SCM s, SCM v)
174 mutable_property_alist_ = scm_assq_set_x (mutable_property_alist_, s, v);
178 MAKE_SCHEME_CALLBACK(Score_element,molecule_extent,2);
180 Score_element::molecule_extent (SCM element_smob, SCM scm_axis)
182 Score_element *s = unsmob_element (element_smob);
183 Axis a = (Axis) gh_scm2int (scm_axis);
185 Molecule m = s->get_molecule ();
186 return ly_interval2scm ( m.extent(a));
189 MAKE_SCHEME_CALLBACK(Score_element,preset_extent,2);
192 Score_element::preset_extent (SCM element_smob, SCM scm_axis)
194 Score_element *s = unsmob_element (element_smob);
195 Axis a = (Axis) gh_scm2int (scm_axis);
197 SCM ext = s->get_elt_property ((a == X_AXIS)
203 Real l = gh_scm2double (gh_car (ext));
204 Real r = gh_scm2double (gh_cdr (ext));
205 return ly_interval2scm (Interval (l, r));
208 return ly_interval2scm ( Interval ());
214 Score_element::paper_l () const
216 return pscore_l_ ? pscore_l_->paper_l_ : 0;
220 Score_element::calculate_dependencies (int final, int busy, SCM funcname)
222 assert (status_i_ >=0);
224 if (status_i_ >= final)
227 if (status_i_== busy)
229 programming_error ("Element is busy, come back later");
235 for (SCM d= get_elt_property ("dependencies"); gh_pair_p (d); d = gh_cdr (d))
237 unsmob_element (gh_car (d))
238 ->calculate_dependencies (final, busy, funcname);
242 String s = ly_symbol2string (funcname);
243 SCM proc = get_elt_property (s.ch_C());
244 if (gh_procedure_p (proc))
245 gh_call1 (proc, this->self_scm ());
252 Score_element::get_molecule () const
254 SCM proc = get_elt_property ("molecule-callback");
257 if (gh_procedure_p (proc))
258 mol = gh_apply (proc, gh_list (this->self_scm (), SCM_UNDEFINED));
261 SCM origin =get_elt_property ("origin");
262 if (!unsmob_input (origin))
263 origin =ly_symbol2scm ("no-origin");
268 mol = gh_cons (gh_list (origin, gh_car (mol), SCM_UNDEFINED), gh_cdr (mol));
272 Molecule m (create_molecule (mol));
275 transparent retains dimensions of element.
277 if (to_boolean (get_elt_property ("transparent")))
278 m = Molecule (m.extent_box (), SCM_EOL);
290 Score_element::do_break_processing()
300 Score_element::line_l() const
306 Score_element::add_dependency (Score_element*e)
310 Pointer_group_interface ::add_element (this, "dependencies",e);
314 programming_error ("Null dependency added");
321 Do break substitution in S, using CRITERION. Return new value.
322 CRITERION is either a SMOB pointer to the desired line, or a number
323 representing the break direction. Do not modify SRC.
326 Score_element::handle_broken_smobs (SCM src, SCM criterion)
329 Score_element *sc = unsmob_element (src);
332 if (gh_number_p (criterion))
334 Item * i = dynamic_cast<Item*> (sc);
335 Direction d = to_dir (criterion);
336 if (i && i->break_status_dir () != d)
338 Item *br = i->find_prebroken_piece (d);
339 return (br) ? br->self_scm () : SCM_UNDEFINED;
345 = dynamic_cast<Line_of_score*> (unsmob_element (criterion));
346 if (sc->line_l () != line)
348 sc = sc->find_broken_piece (line);
352 /* now: !sc || (sc && sc->line_l () == line) */
354 return SCM_UNDEFINED;
356 /* now: sc && sc->line_l () == line */
358 || (sc->common_refpoint (line, X_AXIS)
359 && sc->common_refpoint (line, Y_AXIS)))
361 return sc->self_scm ();
363 return SCM_UNDEFINED;
366 else if (gh_pair_p (src))
368 SCM oldcar =gh_car (src);
370 UGH! breaks on circular lists.
372 SCM newcar = handle_broken_smobs (oldcar, criterion);
373 SCM oldcdr = gh_cdr (src);
375 if (newcar == SCM_UNDEFINED
376 && (gh_pair_p (oldcdr) || oldcdr == SCM_EOL))
379 This is tail-recursion, ie.
381 return handle_broken_smobs (cdr, criterion);
383 We don't want to rely on the compiler to do this. Without
384 tail-recursion, this easily crashes with a stack overflow. */
389 SCM newcdr = handle_broken_smobs (oldcdr, criterion);
390 return gh_cons (newcar, newcdr);
399 Score_element::handle_broken_dependencies()
401 Spanner * s= dynamic_cast<Spanner*> (this);
402 if (original_l_ && s)
407 for (int i = 0; i< s->broken_into_l_arr_ .size (); i++)
409 Score_element * sc = s->broken_into_l_arr_[i];
410 Line_of_score * l = sc->line_l ();
411 sc->mutable_property_alist_ =
412 handle_broken_smobs (mutable_property_alist_,
413 l ? l->self_scm () : SCM_UNDEFINED);
418 Line_of_score *line = line_l();
420 if (line && common_refpoint (line, X_AXIS) && common_refpoint (line, Y_AXIS))
422 mutable_property_alist_
423 = handle_broken_smobs (mutable_property_alist_,
424 line ? line->self_scm () : SCM_UNDEFINED);
426 else if (dynamic_cast <Line_of_score*> (this))
428 mutable_property_alist_ = handle_broken_smobs (mutable_property_alist_,
434 This element is `invalid'; it has been removed from all
435 dependencies, so let's junk the element itself.
437 do not do this for Line_of_score, since that would remove
438 references to the originals of score-elts, which get then GC'd
446 Note that we still want references to this element to be
447 rearranged, and not silently thrown away, so we keep pointers
448 like {broken_into_{drul,array}, original}
451 Score_element::suicide ()
453 mutable_property_alist_ = SCM_EOL;
454 immutable_property_alist_ = SCM_EOL;
456 set_extent_callback (SCM_EOL, Y_AXIS);
457 set_extent_callback (SCM_EOL, X_AXIS);
459 for (int a= X_AXIS; a <= Y_AXIS; a++)
461 dim_cache_[a].offset_callbacks_ = SCM_EOL;
462 dim_cache_[a].offsets_left_ = 0;
467 Score_element::handle_prebroken_dependencies()
472 Score_element::find_broken_piece (Line_of_score*) const
478 Score_element::translate_axis (Real y, Axis a)
480 if (isinf (y) || isnan (y))
481 programming_error (_(INFINITY_MSG));
484 dim_cache_[a].offset_ += y;
489 Score_element::relative_coordinate (Score_element const*refp, Axis a) const
495 We catch PARENT_L_ == nil case with this, but we crash if we did
496 not ask for the absolute coordinate (ie. REFP == nil.)
499 if (refp == dim_cache_[a].parent_l_)
500 return get_offset (a);
502 return get_offset (a) + dim_cache_[a].parent_l_->relative_coordinate (refp, a);
506 Score_element::get_offset (Axis a) const
508 Score_element *me = (Score_element*) this;
509 while (dim_cache_[a].offsets_left_)
511 int l = --me->dim_cache_[a].offsets_left_;
512 SCM cb = scm_list_ref (dim_cache_[a].offset_callbacks_, gh_int2scm (l));
513 SCM retval = gh_call2 (cb, self_scm (), gh_int2scm (a));
515 Real r = gh_scm2double (retval);
516 if (isinf (r) || isnan (r))
518 programming_error (INFINITY_MSG);
521 me->dim_cache_[a].offset_ +=r;
523 return dim_cache_[a].offset_;
527 MAKE_SCHEME_CALLBACK(Score_element,point_dimension_callback,2);
529 Score_element::point_dimension_callback (SCM , SCM )
531 return ly_interval2scm ( Interval (0,0));
535 Score_element::empty_b (Axis a)const
537 return ! (gh_pair_p (dim_cache_[a].dimension_ ) ||
538 gh_procedure_p (dim_cache_[a].dimension_ ));
544 Score_element *refpoint
549 Score_element::extent (Score_element * refp, Axis a) const
551 Real x = relative_coordinate (refp, a);
554 Dimension_cache * d = (Dimension_cache *)&dim_cache_[a];
556 if (gh_pair_p (d->dimension_))
558 else if (gh_procedure_p (d->dimension_))
561 FIXME: add doco on types, and should typecheck maybe?
563 d->dimension_= gh_call2 (d->dimension_, self_scm(), gh_int2scm (a));
568 if (!gh_pair_p (d->dimension_))
571 ext = ly_scm2interval (d->dimension_);
573 SCM extra = get_elt_property (a == X_AXIS
580 if (gh_pair_p (extra))
582 ext[BIGGER] += gh_scm2double (gh_cdr (extra));
583 ext[SMALLER] += gh_scm2double (gh_car (extra));
586 extra = get_elt_property (a == X_AXIS
588 : "minimum-extent-Y");
589 if (gh_pair_p (extra))
591 ext.unite (Interval (gh_scm2double (gh_car (extra)),
592 gh_scm2double (gh_cdr (extra))));
602 Score_element::parent_l (Axis a) const
604 return dim_cache_[a].parent_l_;
608 Score_element::common_refpoint (Score_element const* s, Axis a) const
611 I don't like the quadratic aspect of this code, but I see no other
612 way. The largest chain of parents might be 10 high or so, so
613 it shouldn't be a real issue. */
614 for (Score_element const *c = this; c; c = c->dim_cache_[a].parent_l_)
615 for (Score_element const * d = s; d; d = d->dim_cache_[a].parent_l_)
617 return (Score_element*)d;
624 Score_element::common_refpoint (SCM elist, Axis a) const
626 Score_element * common = (Score_element*) this;
627 for (; gh_pair_p (elist); elist = gh_cdr (elist))
629 Score_element * s = unsmob_element (gh_car (elist));
631 common = common->common_refpoint (s, a);
638 Score_element::name () const
640 SCM meta = get_elt_property ("meta");
641 SCM nm = scm_assoc (ly_symbol2scm ("name"), meta);
642 nm = (gh_pair_p (nm)) ? gh_cdr (nm) : SCM_EOL;
643 return gh_string_p (nm) ?ly_scm2string (nm) : classname (this);
647 Score_element::add_offset_callback (SCM cb, Axis a)
649 if (!has_offset_callback_b (cb, a))
651 dim_cache_[a].offset_callbacks_ = gh_cons (cb, dim_cache_[a].offset_callbacks_ );
652 dim_cache_[a].offsets_left_ ++;
657 Score_element::has_extent_callback_b (SCM cb, Axis a)const
659 return scm_equal_p (cb, dim_cache_[a].dimension_);
664 Score_element::has_extent_callback_b (Axis a) const
666 return gh_procedure_p (dim_cache_[a].dimension_);
670 Score_element::has_offset_callback_b (SCM cb, Axis a)const
672 return scm_memq (cb, dim_cache_[a].offset_callbacks_) != SCM_BOOL_F;
676 Score_element::set_extent_callback (SCM dc, Axis a)
678 dim_cache_[a].dimension_ =dc;
682 Score_element::set_parent (Score_element *g, Axis a)
684 dim_cache_[a].parent_l_ = g;
687 MAKE_SCHEME_CALLBACK(Score_element,fixup_refpoint,1);
689 Score_element::fixup_refpoint (SCM smob)
691 Score_element *me = unsmob_element (smob);
692 for (int a = X_AXIS; a < NO_AXES; a ++)
695 Score_element * parent = me->parent_l (ax);
700 if (parent->line_l () != me->line_l () && me->line_l ())
702 Score_element * newparent = parent->find_broken_piece (me->line_l ());
703 me->set_parent (newparent, ax);
706 if (Item * i = dynamic_cast<Item*> (me))
708 Item *parenti = dynamic_cast<Item*> (parent);
712 Direction my_dir = i->break_status_dir () ;
713 if (my_dir!= parenti->break_status_dir())
715 Item *newparent = parenti->find_prebroken_piece (my_dir);
716 me->set_parent (newparent, ax);
726 /****************************************************
728 ****************************************************/
731 IMPLEMENT_UNSMOB(Score_element, element);
732 IMPLEMENT_SMOBS(Score_element);
733 IMPLEMENT_DEFAULT_EQUAL_P(Score_element);
736 Score_element::mark_smob (SCM ses)
738 Score_element * s = (Score_element*) SCM_CELL_WORD_1(ses);
739 scm_gc_mark (s->immutable_property_alist_);
740 scm_gc_mark (s->mutable_property_alist_);
742 for (int a =0 ; a < 2; a++)
744 scm_gc_mark (s->dim_cache_[a].offset_callbacks_);
745 scm_gc_mark (s->dim_cache_[a].dimension_);
748 if (s->parent_l (Y_AXIS))
749 scm_gc_mark (s->parent_l (Y_AXIS)->self_scm ());
750 if (s->parent_l (X_AXIS))
751 scm_gc_mark (s->parent_l (X_AXIS)->self_scm ());
754 scm_gc_mark (s->original_l_->self_scm ());
755 return s->do_derived_mark ();
759 Score_element::print_smob (SCM s, SCM port, scm_print_state *)
761 Score_element *sc = (Score_element *) gh_cdr (s);
763 scm_puts ("#<Score_element ", port);
764 scm_puts ((char *)sc->name ().ch_C(), port);
767 don't try to print properties, that is too much hassle.
769 scm_puts (" >", port);
774 Score_element::do_derived_mark ()
781 ly_set_elt_property (SCM elt, SCM sym, SCM val)
783 Score_element * sc = unsmob_element (elt);
785 if (!gh_symbol_p (sym))
787 error ("Not a symbol");
788 ly_display_scm (sym);
789 return SCM_UNSPECIFIED;
794 sc->set_elt_property (sym, val);
798 error ("Not a score element");
799 ly_display_scm (elt);
802 return SCM_UNSPECIFIED;
807 ly_get_elt_property (SCM elt, SCM sym)
809 Score_element * sc = unsmob_element (elt);
813 return sc->get_elt_property (sym);
817 error ("Not a score element");
818 ly_display_scm (elt);
820 return SCM_UNSPECIFIED;
825 Score_element::discretionary_processing()
832 spanner_get_bound (SCM slur, SCM dir)
834 return dynamic_cast<Spanner*> (unsmob_element (slur))->get_bound (to_dir (dir))->self_scm ();
839 static SCM interfaces_sym;
843 interfaces_sym = scm_permanent_object (ly_symbol2scm ("interfaces"));
845 scm_make_gsubr ("ly-get-elt-property", 2, 0, 0, (Scheme_function_unknown)ly_get_elt_property);
846 scm_make_gsubr ("ly-set-elt-property", 3, 0, 0, (Scheme_function_unknown)ly_set_elt_property);
847 scm_make_gsubr ("ly-get-spanner-bound", 2 , 0, 0, (Scheme_function_unknown) spanner_get_bound);
851 Score_element::has_interface (SCM k)
853 SCM ifs = get_elt_property (interfaces_sym);
855 return scm_memq (k, ifs) != SCM_BOOL_F;
859 Score_element::set_interface (SCM k)
861 if (has_interface (k))
865 set_elt_property (interfaces_sym,
866 gh_cons (k, get_elt_property (interfaces_sym)));
871 ADD_SCM_INIT_FUNC(scoreelt, init_functions);
872 IMPLEMENT_TYPE_P(Score_element, "ly-element?");