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.
51 set_extent_callback (molecule_extent, X_AXIS);
52 set_extent_callback (molecule_extent, Y_AXIS);
58 immutable_property_alist_ = basicprops;
59 mutable_property_alist_ = SCM_EOL;
65 Score_element::Score_element (Score_element const&s)
66 : dim_cache_ (s.dim_cache_)
68 original_l_ =(Score_element*) &s;
69 immutable_property_alist_ = s.immutable_property_alist_;
70 mutable_property_alist_ = SCM_EOL;
72 status_i_ = s.status_i_;
73 lookup_l_ = s.lookup_l_;
74 pscore_l_ = s.pscore_l_;
79 Score_element::~Score_element()
82 do nothing scm-ish and no unprotecting here.
88 Score_element::get_elt_property (const char *nm) const
90 SCM sym = ly_symbol2scm (nm);
91 return get_elt_property (sym);
95 Score_element::get_elt_property (SCM sym) const
97 SCM s = scm_sloppy_assq(sym, mutable_property_alist_);
101 s = scm_sloppy_assq (sym, immutable_property_alist_);
102 return (s == SCM_BOOL_F) ? SCM_EOL : gh_cdr (s);
106 Remove the value associated with KEY, and return it. The result is
107 that a next call will yield SCM_UNDEFINED (and not the underlying
111 Score_element::remove_elt_property (const char* key)
113 SCM val = get_elt_property (key);
115 set_elt_property (key, SCM_EOL);
120 Score_element::set_elt_property (const char* k, SCM v)
122 SCM s = ly_symbol2scm (k);
123 set_elt_property (s, v);
127 Puts the k, v in the immutable_property_alist_, which is convenient for
128 storing variables that are needed during the breaking process. (eg.
129 Line_of_score::rank : int )
132 Score_element::set_immutable_elt_property (const char*k, SCM v)
134 SCM s = ly_symbol2scm (k);
135 set_immutable_elt_property (s, v);
139 Score_element::set_immutable_elt_property (SCM s, SCM v)
141 immutable_property_alist_ = gh_cons (gh_cons (s,v), mutable_property_alist_);
142 mutable_property_alist_ = scm_assq_remove_x (mutable_property_alist_, s);
145 Score_element::set_elt_property (SCM s, SCM v)
147 mutable_property_alist_ = scm_assq_set_x (mutable_property_alist_, s, v);
152 Score_element::molecule_extent (Score_element *s, Axis a)
154 Molecule m = s->get_molecule ();
159 Score_element::preset_extent (Score_element *s , Axis a)
161 SCM ext = s->get_elt_property ((a == X_AXIS)
167 Real l = gh_scm2double (gh_car (ext));
168 Real r = gh_scm2double (gh_cdr (ext));
169 l *= s->paper_l ()->get_var ("staffspace");
170 r *= s->paper_l ()->get_var ("staffspace");
171 return Interval (l, r);
180 Score_element::paper_l () const
182 return pscore_l_ ? pscore_l_->paper_l_ : 0;
186 Score_element::lookup_l () const
189 URG junkthis, caching is clumsy.
193 Score_element * urg = (Score_element*)this;
194 SCM sz = urg->remove_elt_property ("font-size");
195 int i = (gh_number_p (sz))
199 urg->lookup_l_ = (Lookup*)pscore_l_->paper_l_->lookup_l (i);
205 Score_element::add_processing()
207 assert (status_i_ >=0);
216 Score_element::calculate_dependencies (int final, int busy, SCM funcname)
218 assert (status_i_ >=0);
220 if (status_i_ >= final)
223 if (status_i_== busy)
225 programming_error ("Element is busy, come back later");
231 for (SCM d= get_elt_property ("dependencies"); gh_pair_p (d); d = gh_cdr (d))
233 unsmob_element (gh_car (d))
234 ->calculate_dependencies (final, busy, funcname);
238 String s = ly_symbol2string (funcname);
239 SCM proc = get_elt_property (s.ch_C());
240 if (gh_procedure_p (proc))
241 gh_call1 (proc, this->self_scm ());
248 Score_element::get_molecule () const
250 SCM proc = get_elt_property ("molecule-callback");
253 if (gh_procedure_p (proc))
254 mol = gh_apply (proc, gh_list (this->self_scm (), SCM_UNDEFINED));
257 SCM origin =get_elt_property ("origin");
258 if (!unsmob_input (origin))
259 origin =ly_symbol2scm ("no-origin");
264 mol = gh_cons (gh_list (origin, gh_car (mol), SCM_UNDEFINED), gh_cdr (mol));
268 Molecule m (create_molecule (mol));
271 This is almost the same as setting molecule-callback to #f, but
272 this retains the dimensions of this element, which means that you
273 can erase elements individually. */
274 if (to_boolean (get_elt_property ("transparent")))
275 m = Molecule (m.extent_box (), SCM_EOL);
287 Score_element::do_break_processing()
293 Score_element::do_add_processing()
298 MAKE_SCHEME_CALLBACK(Score_element,brew_molecule)
304 Score_element::brew_molecule (SCM smob)
306 Score_element * sc = unsmob_element (smob);
307 SCM glyph = sc->get_elt_property ("glyph");
308 if (gh_string_p (glyph))
310 return sc->lookup_l ()->afm_find (String (ly_scm2string (glyph))).create_scheme ();
320 Score_element::line_l() const
326 Score_element::add_dependency (Score_element*e)
330 Pointer_group_interface gi (this, "dependencies");
334 programming_error ("Null dependency added");
341 Do break substitution in S, using CRITERION. Return new value.
342 CRITERION is either a SMOB pointer to the desired line, or a number
343 representing the break direction. Do not modify SRC.
346 Score_element::handle_broken_smobs (SCM src, SCM criterion)
349 Score_element *sc = unsmob_element (src);
352 if (gh_number_p (criterion))
354 Item * i = dynamic_cast<Item*> (sc);
355 Direction d = to_dir (criterion);
356 if (i && i->break_status_dir () != d)
358 Item *br = i->find_prebroken_piece (d);
359 return (br) ? br->self_scm () : SCM_UNDEFINED;
365 = dynamic_cast<Line_of_score*> (unsmob_element (criterion));
366 if (sc->line_l () != line)
368 sc = sc->find_broken_piece (line);
372 /* now: !sc || (sc && sc->line_l () == line) */
374 return SCM_UNDEFINED;
376 /* now: sc && sc->line_l () == line */
378 || (sc->common_refpoint (line, X_AXIS)
379 && sc->common_refpoint (line, Y_AXIS)))
381 return sc->self_scm ();
383 return SCM_UNDEFINED;
386 else if (gh_pair_p (src))
388 SCM oldcar =gh_car (src);
390 UGH! breaks on circular lists.
392 SCM newcar = handle_broken_smobs (oldcar, criterion);
393 SCM oldcdr = gh_cdr (src);
395 if (newcar == SCM_UNDEFINED
396 && (gh_pair_p (oldcdr) || oldcdr == SCM_EOL))
399 This is tail-recursion, ie.
401 return handle_broken_smobs (cdr, criterion);
403 We don't want to rely on the compiler to do this. Without
404 tail-recursion, this easily crashes with a stack overflow. */
409 SCM newcdr = handle_broken_smobs (oldcdr, criterion);
410 return gh_cons (newcar, newcdr);
419 Score_element::handle_broken_dependencies()
421 Spanner * s= dynamic_cast<Spanner*> (this);
422 if (original_l_ && s)
427 for (int i = 0; i< s->broken_into_l_arr_ .size (); i++)
429 Score_element * sc = s->broken_into_l_arr_[i];
430 Line_of_score * l = sc->line_l ();
431 sc->mutable_property_alist_ =
432 handle_broken_smobs (mutable_property_alist_,
433 l ? l->self_scm () : SCM_UNDEFINED);
438 Line_of_score *line = line_l();
440 if (line && common_refpoint (line, X_AXIS) && common_refpoint (line, Y_AXIS))
442 mutable_property_alist_
443 = handle_broken_smobs (mutable_property_alist_,
444 line ? line->self_scm () : SCM_UNDEFINED);
446 else if (dynamic_cast <Line_of_score*> (this))
448 mutable_property_alist_ = handle_broken_smobs (mutable_property_alist_,
454 This element is `invalid'; it has been removed from all
455 dependencies, so let's junk the element itself.
457 do not do this for Line_of_score, since that would remove
458 references to the originals of score-elts, which get then GC'd
466 Note that we still want references to this element to be
467 rearranged, and not silently thrown away, so we keep pointers
468 like {broken_into_{drul,array}, original}
471 Score_element::suicide ()
473 mutable_property_alist_ = SCM_EOL;
474 immutable_property_alist_ = SCM_EOL;
475 set_extent_callback (0, Y_AXIS);
476 set_extent_callback (0, X_AXIS);
478 for (int a= X_AXIS; a <= Y_AXIS; a++)
480 dim_cache_[a].off_callbacks_.clear ();
485 Score_element::handle_prebroken_dependencies()
490 Score_element::find_broken_piece (Line_of_score*) const
496 Score_element::translate_axis (Real y, Axis a)
498 if (isinf (y) || isnan (y))
499 programming_error (_(INFINITY_MSG));
502 dim_cache_[a].offset_ += y;
507 Score_element::relative_coordinate (Score_element const*refp, Axis a) const
513 We catch PARENT_L_ == nil case with this, but we crash if we did
514 not ask for the absolute coordinate (ie. REFP == nil.)
517 if (refp == dim_cache_[a].parent_l_)
518 return get_offset (a);
520 return get_offset (a) + dim_cache_[a].parent_l_->relative_coordinate (refp, a);
524 Score_element::get_offset (Axis a) const
526 Score_element *me = (Score_element*) this;
527 while (dim_cache_[a].off_callbacks_.size ())
529 Offset_callback c = dim_cache_[a].off_callbacks_[0];
530 me->dim_cache_[a].off_callbacks_.del (0);
531 Real r = (*c) (me,a );
532 if (isinf (r) || isnan (r))
534 programming_error (INFINITY_MSG);
537 me->dim_cache_[a].offset_ +=r;
539 return dim_cache_[a].offset_;
544 Score_element::point_dimension_callback (Score_element* , Axis)
546 return Interval (0,0);
550 Score_element::empty_b (Axis a)const
552 return !dim_cache_[a].extent_callback_l_;
556 Score_element::extent (Axis a) const
558 Dimension_cache * d = (Dimension_cache *)&dim_cache_[a];
559 if (!d->extent_callback_l_)
561 d->dim_.set_empty ();
563 else if (!d->valid_b_)
565 d->dim_= (*d->extent_callback_l_ ) ((Score_element*)this, a);
569 Interval ext = d->dim_;
574 SCM extra = get_elt_property (a == X_AXIS
581 Real s = paper_l ()->get_var ("staffspace");
582 if (gh_pair_p (extra))
584 ext[BIGGER] += s * gh_scm2double (gh_cdr (extra));
585 ext[SMALLER] += s * gh_scm2double (gh_car (extra));
588 extra = get_elt_property (a == X_AXIS
590 : "minimum-extent-Y");
591 if (gh_pair_p (extra))
593 ext.unite (Interval (s * gh_scm2double (gh_car (extra)),
594 s * 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 nm = get_elt_property ("name");
642 return nm == SCM_EOL ? classname (this) :ly_scm2string (nm) ;
646 Score_element::add_offset_callback (Offset_callback cb, Axis a)
648 dim_cache_[a].off_callbacks_.push (cb);
652 Score_element::has_extent_callback_b (Extent_callback cb, Axis a)const
654 return cb == dim_cache_[a].extent_callback_l_;
659 Score_element::has_extent_callback_b (Axis a) const
661 return dim_cache_[a].extent_callback_l_;
665 Score_element::has_offset_callback_b (Offset_callback cb, Axis a)const
667 for (int i= dim_cache_[a].off_callbacks_.size (); i--;)
669 if (dim_cache_[a].off_callbacks_[i] == cb)
676 Score_element::set_extent_callback (Dim_cache_callback dc, Axis a)
678 dim_cache_[a].extent_callback_l_ = dc ;
683 Score_element::set_parent (Score_element *g, Axis a)
685 dim_cache_[a].parent_l_ = g;
688 MAKE_SCHEME_CALLBACK(Score_element,fixup_refpoint);
690 Score_element::fixup_refpoint (SCM smob)
692 Score_element *me = unsmob_element (smob);
693 for (int a = X_AXIS; a < NO_AXES; a ++)
696 Score_element * parent = me->parent_l (ax);
701 if (parent->line_l () != me->line_l () && me->line_l ())
703 Score_element * newparent = parent->find_broken_piece (me->line_l ());
704 me->set_parent (newparent, ax);
707 if (Item * i = dynamic_cast<Item*> (me))
709 Item *parenti = dynamic_cast<Item*> (parent);
713 Direction my_dir = i->break_status_dir () ;
714 if (my_dir!= parenti->break_status_dir())
716 Item *newparent = parenti->find_prebroken_piece (my_dir);
717 me->set_parent (newparent, ax);
727 /****************************************************
729 ****************************************************/
732 IMPLEMENT_UNSMOB(Score_element, element);
733 IMPLEMENT_SMOBS(Score_element);
734 IMPLEMENT_DEFAULT_EQUAL_P(Score_element);
737 Score_element::mark_smob (SCM ses)
739 Score_element * s = (Score_element*) SCM_CELL_WORD_1(ses);
740 scm_gc_mark (s->immutable_property_alist_);
741 scm_gc_mark (s->mutable_property_alist_);
743 if (s->parent_l (Y_AXIS))
744 scm_gc_mark (s->parent_l (Y_AXIS)->self_scm ());
745 if (s->parent_l (X_AXIS))
746 scm_gc_mark (s->parent_l (X_AXIS)->self_scm ());
749 scm_gc_mark (s->original_l_->self_scm ());
750 return s->do_derived_mark ();
754 Score_element::print_smob (SCM s, SCM port, scm_print_state *)
756 Score_element *sc = (Score_element *) gh_cdr (s);
758 scm_puts ("#<Score_element ", port);
759 scm_puts ((char *)sc->name ().ch_C(), port);
762 don't try to print properties, that is too much hassle.
764 scm_puts (" >", port);
769 Score_element::do_derived_mark ()
776 ly_set_elt_property (SCM elt, SCM sym, SCM val)
778 Score_element * sc = unsmob_element (elt);
780 if (!gh_symbol_p (sym))
782 error ("Not a symbol");
783 ly_display_scm (sym);
784 return SCM_UNSPECIFIED;
789 sc->set_elt_property (sym, val);
793 error ("Not a score element");
794 ly_display_scm (elt);
797 return SCM_UNSPECIFIED;
802 ly_get_elt_property (SCM elt, SCM sym)
804 Score_element * sc = unsmob_element (elt);
808 return sc->get_elt_property (sym);
812 error ("Not a score element");
813 ly_display_scm (elt);
815 return SCM_UNSPECIFIED;
820 Score_element::discretionary_processing()
827 spanner_get_bound (SCM slur, SCM dir)
829 return dynamic_cast<Spanner*> (unsmob_element (slur))->get_bound (to_dir (dir))->self_scm ();
834 static SCM interfaces_sym;
838 interfaces_sym = scm_permanent_object (ly_symbol2scm ("interfaces"));
840 scm_make_gsubr ("ly-get-elt-property", 2, 0, 0, (Scheme_function_unknown)ly_get_elt_property);
841 scm_make_gsubr ("ly-set-elt-property", 3, 0, 0, (Scheme_function_unknown)ly_set_elt_property);
842 scm_make_gsubr ("ly-get-spanner-bound", 2 , 0, 0, (Scheme_function_unknown) spanner_get_bound);
846 Score_element::has_interface (SCM k)
848 // if (mutable_property_alist_ == SCM_EOL)
851 SCM ifs = get_elt_property (interfaces_sym);
853 return scm_memq (k, ifs) != SCM_BOOL_F;
857 Score_element::set_interface (SCM k)
859 if (has_interface (k))
863 set_elt_property (interfaces_sym,
864 gh_cons (k, get_elt_property (interfaces_sym)));
869 ADD_SCM_INIT_FUNC(scoreelt, init_functions);