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 l *= s->paper_l ()->get_var ("staffspace");
206 r *= s->paper_l ()->get_var ("staffspace");
207 return ly_interval2scm (Interval (l, r));
210 return ly_interval2scm ( Interval ());
216 Score_element::paper_l () const
218 return pscore_l_ ? pscore_l_->paper_l_ : 0;
222 Score_element::calculate_dependencies (int final, int busy, SCM funcname)
224 assert (status_i_ >=0);
226 if (status_i_ >= final)
229 if (status_i_== busy)
231 programming_error ("Element is busy, come back later");
237 for (SCM d= get_elt_property ("dependencies"); gh_pair_p (d); d = gh_cdr (d))
239 unsmob_element (gh_car (d))
240 ->calculate_dependencies (final, busy, funcname);
244 String s = ly_symbol2string (funcname);
245 SCM proc = get_elt_property (s.ch_C());
246 if (gh_procedure_p (proc))
247 gh_call1 (proc, this->self_scm ());
254 Score_element::get_molecule () const
256 SCM proc = get_elt_property ("molecule-callback");
259 if (gh_procedure_p (proc))
260 mol = gh_apply (proc, gh_list (this->self_scm (), SCM_UNDEFINED));
263 SCM origin =get_elt_property ("origin");
264 if (!unsmob_input (origin))
265 origin =ly_symbol2scm ("no-origin");
270 mol = gh_cons (gh_list (origin, gh_car (mol), SCM_UNDEFINED), gh_cdr (mol));
274 Molecule m (create_molecule (mol));
277 transparent retains dimensions of element.
279 if (to_boolean (get_elt_property ("transparent")))
280 m = Molecule (m.extent_box (), SCM_EOL);
292 Score_element::do_break_processing()
302 Score_element::line_l() const
308 Score_element::add_dependency (Score_element*e)
312 Pointer_group_interface ::add_element (this, "dependencies",e);
316 programming_error ("Null dependency added");
323 Do break substitution in S, using CRITERION. Return new value.
324 CRITERION is either a SMOB pointer to the desired line, or a number
325 representing the break direction. Do not modify SRC.
328 Score_element::handle_broken_smobs (SCM src, SCM criterion)
331 Score_element *sc = unsmob_element (src);
334 if (gh_number_p (criterion))
336 Item * i = dynamic_cast<Item*> (sc);
337 Direction d = to_dir (criterion);
338 if (i && i->break_status_dir () != d)
340 Item *br = i->find_prebroken_piece (d);
341 return (br) ? br->self_scm () : SCM_UNDEFINED;
347 = dynamic_cast<Line_of_score*> (unsmob_element (criterion));
348 if (sc->line_l () != line)
350 sc = sc->find_broken_piece (line);
354 /* now: !sc || (sc && sc->line_l () == line) */
356 return SCM_UNDEFINED;
358 /* now: sc && sc->line_l () == line */
360 || (sc->common_refpoint (line, X_AXIS)
361 && sc->common_refpoint (line, Y_AXIS)))
363 return sc->self_scm ();
365 return SCM_UNDEFINED;
368 else if (gh_pair_p (src))
370 SCM oldcar =gh_car (src);
372 UGH! breaks on circular lists.
374 SCM newcar = handle_broken_smobs (oldcar, criterion);
375 SCM oldcdr = gh_cdr (src);
377 if (newcar == SCM_UNDEFINED
378 && (gh_pair_p (oldcdr) || oldcdr == SCM_EOL))
381 This is tail-recursion, ie.
383 return handle_broken_smobs (cdr, criterion);
385 We don't want to rely on the compiler to do this. Without
386 tail-recursion, this easily crashes with a stack overflow. */
391 SCM newcdr = handle_broken_smobs (oldcdr, criterion);
392 return gh_cons (newcar, newcdr);
401 Score_element::handle_broken_dependencies()
403 Spanner * s= dynamic_cast<Spanner*> (this);
404 if (original_l_ && s)
409 for (int i = 0; i< s->broken_into_l_arr_ .size (); i++)
411 Score_element * sc = s->broken_into_l_arr_[i];
412 Line_of_score * l = sc->line_l ();
413 sc->mutable_property_alist_ =
414 handle_broken_smobs (mutable_property_alist_,
415 l ? l->self_scm () : SCM_UNDEFINED);
420 Line_of_score *line = line_l();
422 if (line && common_refpoint (line, X_AXIS) && common_refpoint (line, Y_AXIS))
424 mutable_property_alist_
425 = handle_broken_smobs (mutable_property_alist_,
426 line ? line->self_scm () : SCM_UNDEFINED);
428 else if (dynamic_cast <Line_of_score*> (this))
430 mutable_property_alist_ = handle_broken_smobs (mutable_property_alist_,
436 This element is `invalid'; it has been removed from all
437 dependencies, so let's junk the element itself.
439 do not do this for Line_of_score, since that would remove
440 references to the originals of score-elts, which get then GC'd
448 Note that we still want references to this element to be
449 rearranged, and not silently thrown away, so we keep pointers
450 like {broken_into_{drul,array}, original}
453 Score_element::suicide ()
455 mutable_property_alist_ = SCM_EOL;
456 immutable_property_alist_ = SCM_EOL;
458 set_extent_callback (SCM_EOL, Y_AXIS);
459 set_extent_callback (SCM_EOL, X_AXIS);
461 for (int a= X_AXIS; a <= Y_AXIS; a++)
463 dim_cache_[a].offset_callbacks_ = SCM_EOL;
464 dim_cache_[a].offsets_left_ = 0;
469 Score_element::handle_prebroken_dependencies()
474 Score_element::find_broken_piece (Line_of_score*) const
480 Score_element::translate_axis (Real y, Axis a)
482 if (isinf (y) || isnan (y))
483 programming_error (_(INFINITY_MSG));
486 dim_cache_[a].offset_ += y;
491 Score_element::relative_coordinate (Score_element const*refp, Axis a) const
497 We catch PARENT_L_ == nil case with this, but we crash if we did
498 not ask for the absolute coordinate (ie. REFP == nil.)
501 if (refp == dim_cache_[a].parent_l_)
502 return get_offset (a);
504 return get_offset (a) + dim_cache_[a].parent_l_->relative_coordinate (refp, a);
508 Score_element::get_offset (Axis a) const
510 Score_element *me = (Score_element*) this;
511 while (dim_cache_[a].offsets_left_)
513 int l = --me->dim_cache_[a].offsets_left_;
514 SCM cb = scm_list_ref (dim_cache_[a].offset_callbacks_, gh_int2scm (l));
515 SCM retval = gh_call2 (cb, self_scm (), gh_int2scm (a));
517 Real r = gh_scm2double (retval);
518 if (isinf (r) || isnan (r))
520 programming_error (INFINITY_MSG);
523 me->dim_cache_[a].offset_ +=r;
525 return dim_cache_[a].offset_;
529 MAKE_SCHEME_CALLBACK(Score_element,point_dimension_callback,2);
531 Score_element::point_dimension_callback (SCM , SCM )
533 return ly_interval2scm ( Interval (0,0));
537 Score_element::empty_b (Axis a)const
539 return ! (gh_pair_p (dim_cache_[a].dimension_ ) ||
540 gh_procedure_p (dim_cache_[a].dimension_ ));
546 Score_element *refpoint
551 Score_element::extent (Score_element * refp, Axis a) const
553 Real x = relative_coordinate (refp, a);
556 Dimension_cache * d = (Dimension_cache *)&dim_cache_[a];
558 if (gh_pair_p (d->dimension_))
560 else if (gh_procedure_p (d->dimension_))
563 FIXME: add doco on types, and should typecheck maybe?
565 d->dimension_= gh_call2 (d->dimension_, self_scm(), gh_int2scm (a));
570 if (!gh_pair_p (d->dimension_))
573 ext = ly_scm2interval (d->dimension_);
575 SCM extra = get_elt_property (a == X_AXIS
582 Real s = paper_l ()->get_var ("staffspace");
583 if (gh_pair_p (extra))
585 ext[BIGGER] += s * gh_scm2double (gh_cdr (extra));
586 ext[SMALLER] += s * gh_scm2double (gh_car (extra));
589 extra = get_elt_property (a == X_AXIS
591 : "minimum-extent-Y");
592 if (gh_pair_p (extra))
594 ext.unite (Interval (s * gh_scm2double (gh_car (extra)),
595 s * gh_scm2double (gh_cdr (extra))));
605 Score_element::parent_l (Axis a) const
607 return dim_cache_[a].parent_l_;
611 Score_element::common_refpoint (Score_element const* s, Axis a) const
614 I don't like the quadratic aspect of this code, but I see no other
615 way. The largest chain of parents might be 10 high or so, so
616 it shouldn't be a real issue. */
617 for (Score_element const *c = this; c; c = c->dim_cache_[a].parent_l_)
618 for (Score_element const * d = s; d; d = d->dim_cache_[a].parent_l_)
620 return (Score_element*)d;
627 Score_element::common_refpoint (SCM elist, Axis a) const
629 Score_element * common = (Score_element*) this;
630 for (; gh_pair_p (elist); elist = gh_cdr (elist))
632 Score_element * s = unsmob_element (gh_car (elist));
634 common = common->common_refpoint (s, a);
641 Score_element::name () const
643 SCM meta = get_elt_property ("meta");
644 SCM nm = scm_assoc (ly_symbol2scm ("name"), meta);
645 nm = (gh_pair_p (nm)) ? gh_cdr (nm) : SCM_EOL;
646 return gh_string_p (nm) ?ly_scm2string (nm) : classname (this);
650 Score_element::add_offset_callback (SCM cb, Axis a)
652 if (!has_offset_callback_b (cb, a))
654 dim_cache_[a].offset_callbacks_ = gh_cons (cb, dim_cache_[a].offset_callbacks_ );
655 dim_cache_[a].offsets_left_ ++;
660 Score_element::has_extent_callback_b (SCM cb, Axis a)const
662 return scm_equal_p (cb, dim_cache_[a].dimension_);
667 Score_element::has_extent_callback_b (Axis a) const
669 return gh_procedure_p (dim_cache_[a].dimension_);
673 Score_element::has_offset_callback_b (SCM cb, Axis a)const
675 return scm_memq (cb, dim_cache_[a].offset_callbacks_) != SCM_BOOL_F;
679 Score_element::set_extent_callback (SCM dc, Axis a)
681 dim_cache_[a].dimension_ =dc;
685 Score_element::set_parent (Score_element *g, Axis a)
687 dim_cache_[a].parent_l_ = g;
690 MAKE_SCHEME_CALLBACK(Score_element,fixup_refpoint,1);
692 Score_element::fixup_refpoint (SCM smob)
694 Score_element *me = unsmob_element (smob);
695 for (int a = X_AXIS; a < NO_AXES; a ++)
698 Score_element * parent = me->parent_l (ax);
703 if (parent->line_l () != me->line_l () && me->line_l ())
705 Score_element * newparent = parent->find_broken_piece (me->line_l ());
706 me->set_parent (newparent, ax);
709 if (Item * i = dynamic_cast<Item*> (me))
711 Item *parenti = dynamic_cast<Item*> (parent);
715 Direction my_dir = i->break_status_dir () ;
716 if (my_dir!= parenti->break_status_dir())
718 Item *newparent = parenti->find_prebroken_piece (my_dir);
719 me->set_parent (newparent, ax);
729 /****************************************************
731 ****************************************************/
734 IMPLEMENT_UNSMOB(Score_element, element);
735 IMPLEMENT_SMOBS(Score_element);
736 IMPLEMENT_DEFAULT_EQUAL_P(Score_element);
739 Score_element::mark_smob (SCM ses)
741 Score_element * s = (Score_element*) SCM_CELL_WORD_1(ses);
742 scm_gc_mark (s->immutable_property_alist_);
743 scm_gc_mark (s->mutable_property_alist_);
745 for (int a =0 ; a < 2; a++)
747 scm_gc_mark (s->dim_cache_[a].offset_callbacks_);
748 scm_gc_mark (s->dim_cache_[a].dimension_);
751 if (s->parent_l (Y_AXIS))
752 scm_gc_mark (s->parent_l (Y_AXIS)->self_scm ());
753 if (s->parent_l (X_AXIS))
754 scm_gc_mark (s->parent_l (X_AXIS)->self_scm ());
757 scm_gc_mark (s->original_l_->self_scm ());
758 return s->do_derived_mark ();
762 Score_element::print_smob (SCM s, SCM port, scm_print_state *)
764 Score_element *sc = (Score_element *) gh_cdr (s);
766 scm_puts ("#<Score_element ", port);
767 scm_puts ((char *)sc->name ().ch_C(), port);
770 don't try to print properties, that is too much hassle.
772 scm_puts (" >", port);
777 Score_element::do_derived_mark ()
784 ly_set_elt_property (SCM elt, SCM sym, SCM val)
786 Score_element * sc = unsmob_element (elt);
788 if (!gh_symbol_p (sym))
790 error ("Not a symbol");
791 ly_display_scm (sym);
792 return SCM_UNSPECIFIED;
797 sc->set_elt_property (sym, val);
801 error ("Not a score element");
802 ly_display_scm (elt);
805 return SCM_UNSPECIFIED;
810 ly_get_elt_property (SCM elt, SCM sym)
812 Score_element * sc = unsmob_element (elt);
816 return sc->get_elt_property (sym);
820 error ("Not a score element");
821 ly_display_scm (elt);
823 return SCM_UNSPECIFIED;
828 Score_element::discretionary_processing()
835 spanner_get_bound (SCM slur, SCM dir)
837 return dynamic_cast<Spanner*> (unsmob_element (slur))->get_bound (to_dir (dir))->self_scm ();
842 static SCM interfaces_sym;
846 interfaces_sym = scm_permanent_object (ly_symbol2scm ("interfaces"));
848 scm_make_gsubr ("ly-get-elt-property", 2, 0, 0, (Scheme_function_unknown)ly_get_elt_property);
849 scm_make_gsubr ("ly-set-elt-property", 3, 0, 0, (Scheme_function_unknown)ly_set_elt_property);
850 scm_make_gsubr ("ly-get-spanner-bound", 2 , 0, 0, (Scheme_function_unknown) spanner_get_bound);
854 Score_element::has_interface (SCM k)
856 SCM ifs = get_elt_property (interfaces_sym);
858 return scm_memq (k, ifs) != SCM_BOOL_F;
862 Score_element::set_interface (SCM k)
864 if (has_interface (k))
868 set_elt_property (interfaces_sym,
869 gh_cons (k, get_elt_property (interfaces_sym)));
874 ADD_SCM_INIT_FUNC(scoreelt, init_functions);
875 IMPLEMENT_TYPE_P(Score_element, "ly-element?");