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"
20 #include "molecule.hh"
21 #include "score-element.hh"
24 #include "line-of-score.hh"
26 #include "paper-column.hh"
27 #include "molecule.hh"
29 #include "paper-outputter.hh"
30 #include "dimension-cache.hh"
31 #include "side-position-interface.hh"
34 #include "ly-smobs.icc"
39 remove dynamic_cast<Spanner,Item> and put this code into respective
44 #define INFINITY_MSG "Infinity or NaN encountered"
46 Score_element::Score_element(SCM basicprops)
49 fixme: default should be no callback.
56 immutable_property_alist_ = basicprops;
57 mutable_property_alist_ = SCM_EOL;
61 char const*onames[] = {"X-offset-callbacks", "Y-offset-callbacks"};
62 char const*enames[] = {"X-extent-callback", "Y-extent-callback"};
64 for (int a = X_AXIS; a <= Y_AXIS; a++){
65 SCM l = get_elt_property (onames[a]);
67 if (scm_ilength (l) >=0)
69 dim_cache_[a].offset_callbacks_ = l;
70 dim_cache_[a].offsets_left_ = scm_ilength (l);
74 programming_error ("[XY]-offset-callbacks must be a list");
77 SCM cb = get_elt_property (enames[a]);
80 Should change default to be empty?
82 if (!gh_procedure_p (cb) && !gh_pair_p (cb))
83 cb = molecule_extent_proc;
85 dim_cache_[a].dimension_ = cb;
88 SCM meta = get_elt_property ("meta");
89 SCM ifs = scm_assoc (ly_symbol2scm ("interfaces"), meta);
91 set_elt_property ("interfaces",gh_cdr (ifs));
95 Score_element::Score_element (Score_element const&s)
96 : dim_cache_ (s.dim_cache_)
98 original_l_ =(Score_element*) &s;
99 immutable_property_alist_ = s.immutable_property_alist_;
100 mutable_property_alist_ = SCM_EOL;
102 status_i_ = s.status_i_;
103 lookup_l_ = s.lookup_l_;
104 pscore_l_ = s.pscore_l_;
109 Score_element::~Score_element()
112 do nothing scm-ish and no unprotecting here.
118 Score_element::get_elt_property (const char *nm) const
120 SCM sym = ly_symbol2scm (nm);
121 return get_elt_property (sym);
125 Score_element::get_elt_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 : gh_cdr (s);
136 Remove the value associated with KEY, and return it. The result is
137 that a next call will yield SCM_UNDEFINED (and not the underlying
141 Score_element::remove_elt_property (const char* key)
143 SCM val = get_elt_property (key);
145 set_elt_property (key, SCM_EOL);
150 Score_element::set_elt_property (const char* k, SCM v)
152 SCM s = ly_symbol2scm (k);
153 set_elt_property (s, v);
157 Puts the k, v in the immutable_property_alist_, which is convenient for
158 storing variables that are needed during the breaking process. (eg.
159 Line_of_score::rank : int )
162 Score_element::set_immutable_elt_property (const char*k, SCM v)
164 SCM s = ly_symbol2scm (k);
165 set_immutable_elt_property (s, v);
169 Score_element::set_immutable_elt_property (SCM s, SCM v)
171 immutable_property_alist_ = gh_cons (gh_cons (s,v), mutable_property_alist_);
172 mutable_property_alist_ = scm_assq_remove_x (mutable_property_alist_, s);
175 Score_element::set_elt_property (SCM s, SCM v)
177 mutable_property_alist_ = scm_assq_set_x (mutable_property_alist_, s, v);
181 MAKE_SCHEME_CALLBACK(Score_element,molecule_extent,2);
183 Score_element::molecule_extent (SCM element_smob, SCM scm_axis)
185 Score_element *s = unsmob_element (element_smob);
186 Axis a = (Axis) gh_scm2int (scm_axis);
188 Molecule m = s->get_molecule ();
189 return ly_interval2scm ( m.extent(a));
192 MAKE_SCHEME_CALLBACK(Score_element,preset_extent,2);
195 Score_element::preset_extent (SCM element_smob, SCM scm_axis)
197 Score_element *s = unsmob_element (element_smob);
198 Axis a = (Axis) gh_scm2int (scm_axis);
200 SCM ext = s->get_elt_property ((a == X_AXIS)
206 Real l = gh_scm2double (gh_car (ext));
207 Real r = gh_scm2double (gh_cdr (ext));
208 l *= s->paper_l ()->get_var ("staffspace");
209 r *= s->paper_l ()->get_var ("staffspace");
210 return ly_interval2scm (Interval (l, r));
213 return ly_interval2scm ( Interval ());
219 Score_element::paper_l () const
221 return pscore_l_ ? pscore_l_->paper_l_ : 0;
225 Score_element::lookup_l () const
228 URG junkthis, caching is clumsy.
232 Score_element * urg = (Score_element*)this;
233 SCM sz = urg->remove_elt_property ("font-size");
234 int i = (gh_number_p (sz))
238 urg->lookup_l_ = (Lookup*)pscore_l_->paper_l_->lookup_l (i);
244 Score_element::calculate_dependencies (int final, int busy, SCM funcname)
246 assert (status_i_ >=0);
248 if (status_i_ >= final)
251 if (status_i_== busy)
253 programming_error ("Element is busy, come back later");
259 for (SCM d= get_elt_property ("dependencies"); gh_pair_p (d); d = gh_cdr (d))
261 unsmob_element (gh_car (d))
262 ->calculate_dependencies (final, busy, funcname);
266 String s = ly_symbol2string (funcname);
267 SCM proc = get_elt_property (s.ch_C());
268 if (gh_procedure_p (proc))
269 gh_call1 (proc, this->self_scm ());
276 Score_element::get_molecule () const
278 SCM proc = get_elt_property ("molecule-callback");
281 if (gh_procedure_p (proc))
282 mol = gh_apply (proc, gh_list (this->self_scm (), SCM_UNDEFINED));
285 SCM origin =get_elt_property ("origin");
286 if (!unsmob_input (origin))
287 origin =ly_symbol2scm ("no-origin");
292 mol = gh_cons (gh_list (origin, gh_car (mol), SCM_UNDEFINED), gh_cdr (mol));
296 Molecule m (create_molecule (mol));
299 transparent retains dimensions of element.
301 if (to_boolean (get_elt_property ("transparent")))
302 m = Molecule (m.extent_box (), SCM_EOL);
314 Score_element::do_break_processing()
324 Score_element::line_l() const
330 Score_element::add_dependency (Score_element*e)
334 Pointer_group_interface ::add_element (this, "dependencies",e);
338 programming_error ("Null dependency added");
345 Do break substitution in S, using CRITERION. Return new value.
346 CRITERION is either a SMOB pointer to the desired line, or a number
347 representing the break direction. Do not modify SRC.
350 Score_element::handle_broken_smobs (SCM src, SCM criterion)
353 Score_element *sc = unsmob_element (src);
356 if (gh_number_p (criterion))
358 Item * i = dynamic_cast<Item*> (sc);
359 Direction d = to_dir (criterion);
360 if (i && i->break_status_dir () != d)
362 Item *br = i->find_prebroken_piece (d);
363 return (br) ? br->self_scm () : SCM_UNDEFINED;
369 = dynamic_cast<Line_of_score*> (unsmob_element (criterion));
370 if (sc->line_l () != line)
372 sc = sc->find_broken_piece (line);
376 /* now: !sc || (sc && sc->line_l () == line) */
378 return SCM_UNDEFINED;
380 /* now: sc && sc->line_l () == line */
382 || (sc->common_refpoint (line, X_AXIS)
383 && sc->common_refpoint (line, Y_AXIS)))
385 return sc->self_scm ();
387 return SCM_UNDEFINED;
390 else if (gh_pair_p (src))
392 SCM oldcar =gh_car (src);
394 UGH! breaks on circular lists.
396 SCM newcar = handle_broken_smobs (oldcar, criterion);
397 SCM oldcdr = gh_cdr (src);
399 if (newcar == SCM_UNDEFINED
400 && (gh_pair_p (oldcdr) || oldcdr == SCM_EOL))
403 This is tail-recursion, ie.
405 return handle_broken_smobs (cdr, criterion);
407 We don't want to rely on the compiler to do this. Without
408 tail-recursion, this easily crashes with a stack overflow. */
413 SCM newcdr = handle_broken_smobs (oldcdr, criterion);
414 return gh_cons (newcar, newcdr);
423 Score_element::handle_broken_dependencies()
425 Spanner * s= dynamic_cast<Spanner*> (this);
426 if (original_l_ && s)
431 for (int i = 0; i< s->broken_into_l_arr_ .size (); i++)
433 Score_element * sc = s->broken_into_l_arr_[i];
434 Line_of_score * l = sc->line_l ();
435 sc->mutable_property_alist_ =
436 handle_broken_smobs (mutable_property_alist_,
437 l ? l->self_scm () : SCM_UNDEFINED);
442 Line_of_score *line = line_l();
444 if (line && common_refpoint (line, X_AXIS) && common_refpoint (line, Y_AXIS))
446 mutable_property_alist_
447 = handle_broken_smobs (mutable_property_alist_,
448 line ? line->self_scm () : SCM_UNDEFINED);
450 else if (dynamic_cast <Line_of_score*> (this))
452 mutable_property_alist_ = handle_broken_smobs (mutable_property_alist_,
458 This element is `invalid'; it has been removed from all
459 dependencies, so let's junk the element itself.
461 do not do this for Line_of_score, since that would remove
462 references to the originals of score-elts, which get then GC'd
470 Note that we still want references to this element to be
471 rearranged, and not silently thrown away, so we keep pointers
472 like {broken_into_{drul,array}, original}
475 Score_element::suicide ()
477 mutable_property_alist_ = SCM_EOL;
478 immutable_property_alist_ = SCM_EOL;
480 set_extent_callback (SCM_EOL, Y_AXIS);
481 set_extent_callback (SCM_EOL, X_AXIS);
483 for (int a= X_AXIS; a <= Y_AXIS; a++)
485 dim_cache_[a].offset_callbacks_ = SCM_EOL;
486 dim_cache_[a].offsets_left_ = 0;
491 Score_element::handle_prebroken_dependencies()
496 Score_element::find_broken_piece (Line_of_score*) const
502 Score_element::translate_axis (Real y, Axis a)
504 if (isinf (y) || isnan (y))
505 programming_error (_(INFINITY_MSG));
508 dim_cache_[a].offset_ += y;
513 Score_element::relative_coordinate (Score_element const*refp, Axis a) const
519 We catch PARENT_L_ == nil case with this, but we crash if we did
520 not ask for the absolute coordinate (ie. REFP == nil.)
523 if (refp == dim_cache_[a].parent_l_)
524 return get_offset (a);
526 return get_offset (a) + dim_cache_[a].parent_l_->relative_coordinate (refp, a);
530 Score_element::get_offset (Axis a) const
532 Score_element *me = (Score_element*) this;
533 while (dim_cache_[a].offsets_left_)
535 int l = --me->dim_cache_[a].offsets_left_;
536 SCM cb = scm_list_ref (dim_cache_[a].offset_callbacks_, gh_int2scm (l));
537 SCM retval = gh_call2 (cb, self_scm (), gh_int2scm (a));
539 Real r = gh_scm2double (retval);
540 if (isinf (r) || isnan (r))
542 programming_error (INFINITY_MSG);
545 me->dim_cache_[a].offset_ +=r;
547 return dim_cache_[a].offset_;
551 MAKE_SCHEME_CALLBACK(Score_element,point_dimension_callback,2);
553 Score_element::point_dimension_callback (SCM , SCM )
555 return ly_interval2scm ( Interval (0,0));
559 Score_element::empty_b (Axis a)const
561 return ! (gh_pair_p (dim_cache_[a].dimension_ ) ||
562 gh_procedure_p (dim_cache_[a].dimension_ ));
568 Score_element *refpoint
573 Score_element::extent (Score_element * refp, Axis a) const
575 Real x = relative_coordinate (refp, a);
578 Dimension_cache * d = (Dimension_cache *)&dim_cache_[a];
580 if (gh_pair_p (d->dimension_))
582 else if (gh_procedure_p (d->dimension_))
585 FIXME: add doco on types, and should typecheck maybe?
587 d->dimension_= gh_call2 (d->dimension_, self_scm(), gh_int2scm (a));
592 if (!gh_pair_p (d->dimension_))
595 ext = ly_scm2interval (d->dimension_);
597 SCM extra = get_elt_property (a == X_AXIS
604 Real s = paper_l ()->get_var ("staffspace");
605 if (gh_pair_p (extra))
607 ext[BIGGER] += s * gh_scm2double (gh_cdr (extra));
608 ext[SMALLER] += s * gh_scm2double (gh_car (extra));
611 extra = get_elt_property (a == X_AXIS
613 : "minimum-extent-Y");
614 if (gh_pair_p (extra))
616 ext.unite (Interval (s * gh_scm2double (gh_car (extra)),
617 s * gh_scm2double (gh_cdr (extra))));
627 Score_element::parent_l (Axis a) const
629 return dim_cache_[a].parent_l_;
633 Score_element::common_refpoint (Score_element const* s, Axis a) const
636 I don't like the quadratic aspect of this code, but I see no other
637 way. The largest chain of parents might be 10 high or so, so
638 it shouldn't be a real issue. */
639 for (Score_element const *c = this; c; c = c->dim_cache_[a].parent_l_)
640 for (Score_element const * d = s; d; d = d->dim_cache_[a].parent_l_)
642 return (Score_element*)d;
649 Score_element::common_refpoint (SCM elist, Axis a) const
651 Score_element * common = (Score_element*) this;
652 for (; gh_pair_p (elist); elist = gh_cdr (elist))
654 Score_element * s = unsmob_element (gh_car (elist));
656 common = common->common_refpoint (s, a);
663 Score_element::name () const
665 SCM meta = get_elt_property ("meta");
666 SCM nm = scm_assoc (ly_symbol2scm ("name"), meta);
667 nm = (gh_pair_p (nm)) ? gh_cdr (nm) : SCM_EOL;
668 return gh_string_p (nm) ?ly_scm2string (nm) : classname (this);
672 Score_element::add_offset_callback (SCM cb, Axis a)
674 if (!has_offset_callback_b (cb, a))
676 dim_cache_[a].offset_callbacks_ = gh_cons (cb, dim_cache_[a].offset_callbacks_ );
677 dim_cache_[a].offsets_left_ ++;
682 Score_element::has_extent_callback_b (SCM cb, Axis a)const
684 return scm_equal_p (cb, dim_cache_[a].dimension_);
689 Score_element::has_extent_callback_b (Axis a) const
691 return gh_procedure_p (dim_cache_[a].dimension_);
695 Score_element::has_offset_callback_b (SCM cb, Axis a)const
697 return scm_memq (cb, dim_cache_[a].offset_callbacks_) != SCM_BOOL_F;
701 Score_element::set_extent_callback (SCM dc, Axis a)
703 dim_cache_[a].dimension_ =dc;
707 Score_element::set_parent (Score_element *g, Axis a)
709 dim_cache_[a].parent_l_ = g;
712 MAKE_SCHEME_CALLBACK(Score_element,fixup_refpoint,1);
714 Score_element::fixup_refpoint (SCM smob)
716 Score_element *me = unsmob_element (smob);
717 for (int a = X_AXIS; a < NO_AXES; a ++)
720 Score_element * parent = me->parent_l (ax);
725 if (parent->line_l () != me->line_l () && me->line_l ())
727 Score_element * newparent = parent->find_broken_piece (me->line_l ());
728 me->set_parent (newparent, ax);
731 if (Item * i = dynamic_cast<Item*> (me))
733 Item *parenti = dynamic_cast<Item*> (parent);
737 Direction my_dir = i->break_status_dir () ;
738 if (my_dir!= parenti->break_status_dir())
740 Item *newparent = parenti->find_prebroken_piece (my_dir);
741 me->set_parent (newparent, ax);
751 /****************************************************
753 ****************************************************/
756 IMPLEMENT_UNSMOB(Score_element, element);
757 IMPLEMENT_SMOBS(Score_element);
758 IMPLEMENT_DEFAULT_EQUAL_P(Score_element);
761 Score_element::mark_smob (SCM ses)
763 Score_element * s = (Score_element*) SCM_CELL_WORD_1(ses);
764 scm_gc_mark (s->immutable_property_alist_);
765 scm_gc_mark (s->mutable_property_alist_);
767 for (int a =0 ; a < 2; a++)
769 scm_gc_mark (s->dim_cache_[a].offset_callbacks_);
770 scm_gc_mark (s->dim_cache_[a].dimension_);
773 if (s->parent_l (Y_AXIS))
774 scm_gc_mark (s->parent_l (Y_AXIS)->self_scm ());
775 if (s->parent_l (X_AXIS))
776 scm_gc_mark (s->parent_l (X_AXIS)->self_scm ());
779 scm_gc_mark (s->original_l_->self_scm ());
780 return s->do_derived_mark ();
784 Score_element::print_smob (SCM s, SCM port, scm_print_state *)
786 Score_element *sc = (Score_element *) gh_cdr (s);
788 scm_puts ("#<Score_element ", port);
789 scm_puts ((char *)sc->name ().ch_C(), port);
792 don't try to print properties, that is too much hassle.
794 scm_puts (" >", port);
799 Score_element::do_derived_mark ()
806 ly_set_elt_property (SCM elt, SCM sym, SCM val)
808 Score_element * sc = unsmob_element (elt);
810 if (!gh_symbol_p (sym))
812 error ("Not a symbol");
813 ly_display_scm (sym);
814 return SCM_UNSPECIFIED;
819 sc->set_elt_property (sym, val);
823 error ("Not a score element");
824 ly_display_scm (elt);
827 return SCM_UNSPECIFIED;
832 ly_get_elt_property (SCM elt, SCM sym)
834 Score_element * sc = unsmob_element (elt);
838 return sc->get_elt_property (sym);
842 error ("Not a score element");
843 ly_display_scm (elt);
845 return SCM_UNSPECIFIED;
850 Score_element::discretionary_processing()
857 spanner_get_bound (SCM slur, SCM dir)
859 return dynamic_cast<Spanner*> (unsmob_element (slur))->get_bound (to_dir (dir))->self_scm ();
864 static SCM interfaces_sym;
868 interfaces_sym = scm_permanent_object (ly_symbol2scm ("interfaces"));
870 scm_make_gsubr ("ly-get-elt-property", 2, 0, 0, (Scheme_function_unknown)ly_get_elt_property);
871 scm_make_gsubr ("ly-set-elt-property", 3, 0, 0, (Scheme_function_unknown)ly_set_elt_property);
872 scm_make_gsubr ("ly-get-spanner-bound", 2 , 0, 0, (Scheme_function_unknown) spanner_get_bound);
876 Score_element::has_interface (SCM k)
878 SCM ifs = get_elt_property (interfaces_sym);
880 return scm_memq (k, ifs) != SCM_BOOL_F;
884 Score_element::set_interface (SCM k)
886 if (has_interface (k))
890 set_elt_property (interfaces_sym,
891 gh_cons (k, get_elt_property (interfaces_sym)));
896 ADD_SCM_INIT_FUNC(scoreelt, init_functions);
897 IMPLEMENT_TYPE_P(Score_element, "ly-element?");