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 "libc-extension.hh"
14 #include "group-interface.hh"
16 #include "paper-score.hh"
17 #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"
36 remove dynamic_cast<Spanner,Item> and put this code into respective
41 #define INFINITY_MSG "Infinity or NaN encountered"
43 Score_element::Score_element(SCM basicprops)
45 set_extent_callback (molecule_extent, X_AXIS);
46 set_extent_callback (molecule_extent, Y_AXIS);
53 immutable_property_alist_ = basicprops;
54 mutable_property_alist_ = SCM_EOL;
57 set_elt_property ("dependencies", SCM_EOL);
59 if (get_elt_property ("interfaces") == SCM_UNDEFINED)
60 set_elt_property ("interfaces", SCM_EOL);
64 Score_element::Score_element (Score_element const&s)
65 : 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()
81 // this goes awry when score-elements are copied...
84 Kijk goed naar hoe smobify en unsmobify werken. unsmobify_self
85 is te gebruiken wanneer C++ geheugen beheer weer overneemt van
91 Score_element::get_elt_property (const char *nm) const
93 SCM sym = ly_symbol2scm (nm);
94 return get_elt_property (sym);
98 Score_element::get_elt_property (SCM sym) const
100 SCM s = scm_sloppy_assq(sym, mutable_property_alist_);
104 s = scm_sloppy_assq (sym, immutable_property_alist_);
105 return (s == SCM_BOOL_F) ? SCM_UNDEFINED : gh_cdr (s);
109 Remove the value associated with KEY, and return it. The result is
110 that a next call will yield SCM_UNDEFINED (and not the underlying
114 Score_element::remove_elt_property (const char* key)
116 SCM val = get_elt_property (key);
117 if (val != SCM_UNDEFINED)
118 set_elt_property (key, SCM_UNDEFINED);
123 Score_element::set_elt_property (const char* k, SCM v)
125 SCM s = ly_symbol2scm (k);
126 set_elt_property (s, v);
130 Puts the k, v in the immutable_property_alist_, which is convenient for
131 storing variables that are needed during the breaking process. (eg.
132 Line_of_score::rank : int )
135 Score_element::set_immutable_elt_property (const char*k, SCM v)
137 SCM s = ly_symbol2scm (k);
138 set_immutable_elt_property (s, v);
142 Score_element::set_immutable_elt_property (SCM s, SCM v)
144 immutable_property_alist_ = gh_cons (gh_cons (s,v), mutable_property_alist_);
145 mutable_property_alist_ = scm_assq_remove_x (mutable_property_alist_, s);
148 Score_element::set_elt_property (SCM s, SCM v)
150 mutable_property_alist_ = scm_assq_set_x (mutable_property_alist_, s, v);
155 Score_element::molecule_extent (Score_element *s, Axis a)
157 Molecule m = s->get_molecule ();
162 Score_element::preset_extent (Score_element *s , Axis a)
164 SCM ext = s->get_elt_property ((a == X_AXIS)
170 Real l = gh_scm2double (gh_car (ext));
171 Real r = gh_scm2double (gh_cdr (ext));
172 l *= s->paper_l ()->get_var ("staffspace");
173 r *= s->paper_l ()->get_var ("staffspace");
174 return Interval (l, r);
183 Score_element::paper_l () const
185 return pscore_l_ ? pscore_l_->paper_l_ : 0;
189 Score_element::lookup_l () const
193 Score_element * urg = (Score_element*)this;
194 SCM sz = urg->remove_elt_property ("fontsize");
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");
251 if (gh_procedure_p (proc))
252 return create_molecule (gh_apply (proc, gh_list (this->self_scm_, SCM_UNDEFINED)));
264 Score_element::do_break_processing()
270 Score_element::do_add_processing()
275 MAKE_SCHEME_CALLBACK(Score_element,brew_molecule)
281 Score_element::brew_molecule (SCM smob)
283 Score_element * sc = unsmob_element (smob);
284 SCM glyph = sc->get_elt_property ("glyph");
285 if (gh_string_p (glyph))
287 return sc->lookup_l ()->afm_find (String (ly_scm2string (glyph))).create_scheme ();
297 Score_element::line_l() const
303 Score_element::add_dependency (Score_element*e)
307 Pointer_group_interface gi (this, "dependencies");
311 programming_error ("Null dependency added");
318 Do break substitution in S, using CRITERION. Return new value.
319 CRITERION is either a SMOB pointer to the desired line, or a number
320 representing the break direction. Do not modify SRC.
323 Score_element::handle_broken_smobs (SCM src, SCM criterion)
326 Score_element *sc = unsmob_element (src);
329 if (gh_number_p (criterion))
331 Item * i = dynamic_cast<Item*> (sc);
332 Direction d = to_dir (criterion);
333 if (i && i->break_status_dir () != d)
335 Item *br = i->find_prebroken_piece (d);
336 return (br) ? br->self_scm_ : SCM_UNDEFINED;
342 = dynamic_cast<Line_of_score*> (unsmob_element (criterion));
343 if (sc->line_l () != line)
345 sc = sc->find_broken_piece (line);
349 /* now: !sc || (sc && sc->line_l () == line) */
351 return SCM_UNDEFINED;
353 /* now: sc && sc->line_l () == line */
355 || (sc->common_refpoint (line, X_AXIS)
356 && sc->common_refpoint (line, Y_AXIS)))
358 return sc->self_scm_;
360 return SCM_UNDEFINED;
363 else if (gh_pair_p (src))
365 SCM oldcar =gh_car (src);
367 UGH! breaks on circular lists.
369 SCM newcar = handle_broken_smobs (oldcar, criterion);
370 SCM oldcdr = gh_cdr (src);
372 if (newcar == SCM_UNDEFINED
373 && (gh_pair_p (oldcdr) || oldcdr == SCM_EOL))
376 This is tail-recursion, ie.
378 return handle_broken_smobs (cdr, criterion);
380 We don't want to rely on the compiler to do this. Without
381 tail-recursion, this easily crashes with a stack overflow. */
386 SCM newcdr = handle_broken_smobs (oldcdr, criterion);
387 return gh_cons (newcar, newcdr);
396 Score_element::handle_broken_dependencies()
398 Spanner * s= dynamic_cast<Spanner*> (this);
399 if (original_l_ && s)
404 for (int i = 0; i< s->broken_into_l_arr_ .size (); i++)
406 Score_element * sc = s->broken_into_l_arr_[i];
407 Line_of_score * l = sc->line_l ();
408 sc->mutable_property_alist_ =
409 handle_broken_smobs (mutable_property_alist_,
410 l ? l->self_scm_ : SCM_UNDEFINED);
415 Line_of_score *line = line_l();
417 if (line && common_refpoint (line, X_AXIS) && common_refpoint (line, Y_AXIS))
419 mutable_property_alist_
420 = handle_broken_smobs (mutable_property_alist_,
421 line ? line->self_scm_ : SCM_UNDEFINED);
423 else if (dynamic_cast <Line_of_score*> (this))
425 mutable_property_alist_ = handle_broken_smobs (mutable_property_alist_,
431 This element is `invalid'; it has been removed from all
432 dependencies, so let's junk the element itself.
434 do not do this for Line_of_score, since that would remove
435 references to the originals of score-elts, which get then GC'd
443 Note that we still want references to this element to be
444 rearranged, and not silently thrown away, so we keep pointers
445 like {broken_into_{drul,array}, original}
448 Score_element::suicide ()
450 mutable_property_alist_ = SCM_EOL;
451 immutable_property_alist_ = SCM_EOL;
452 set_extent_callback (0, Y_AXIS);
453 set_extent_callback (0, X_AXIS);
455 for (int a= X_AXIS; a <= Y_AXIS; a++)
457 dim_cache_[a].off_callbacks_.clear ();
462 Score_element::handle_prebroken_dependencies()
467 Score_element::find_broken_piece (Line_of_score*) const
473 Score_element::translate_axis (Real y, Axis a)
475 if (isinf (y) || isnan (y))
476 programming_error (_(INFINITY_MSG));
479 dim_cache_[a].offset_ += y;
484 Score_element::relative_coordinate (Score_element const*refp, Axis a) const
490 We catch PARENT_L_ == nil case with this, but we crash if we did
491 not ask for the absolute coordinate (ie. REFP == nil.)
494 if (refp == dim_cache_[a].parent_l_)
495 return get_offset (a);
497 return get_offset (a) + dim_cache_[a].parent_l_->relative_coordinate (refp, a);
501 Score_element::get_offset (Axis a) const
503 Score_element *me = (Score_element*) this;
504 while (dim_cache_[a].off_callbacks_.size ())
506 Offset_callback c = dim_cache_[a].off_callbacks_[0];
507 me->dim_cache_[a].off_callbacks_.del (0);
508 Real r = (*c) (me,a );
509 if (isinf (r) || isnan (r))
512 programming_error (INFINITY_MSG);
514 me->dim_cache_[a].offset_ +=r;
516 return dim_cache_[a].offset_;
521 Score_element::point_dimension_callback (Score_element* , Axis)
523 return Interval (0,0);
527 Score_element::empty_b (Axis a)const
529 return !dim_cache_[a].extent_callback_l_;
533 Score_element::extent (Axis a) const
535 Dimension_cache * d = (Dimension_cache *)&dim_cache_[a];
536 if (!d->extent_callback_l_)
538 d->dim_.set_empty ();
540 else if (!d->valid_b_)
542 d->dim_= (*d->extent_callback_l_ ) ((Score_element*)this, a);
546 Interval ext = d->dim_;
551 SCM extra = get_elt_property (a == X_AXIS
558 Real s = paper_l ()->get_var ("staffspace");
559 if (gh_pair_p (extra))
561 ext[BIGGER] += s * gh_scm2double (gh_cdr (extra));
562 ext[SMALLER] += s * gh_scm2double (gh_car (extra));
565 extra = get_elt_property (a == X_AXIS
567 : "minimum-extent-Y");
568 if (gh_pair_p (extra))
570 ext.unite (Interval (s * gh_scm2double (gh_car (extra)),
571 s * gh_scm2double (gh_cdr (extra))));
579 Score_element::parent_l (Axis a) const
581 return dim_cache_[a].parent_l_;
585 Score_element::common_refpoint (Score_element const* s, Axis a) const
588 I don't like the quadratic aspect of this code, but I see no other
589 way. The largest chain of parents might be 10 high or so, so
590 it shouldn't be a real issue. */
591 for (Score_element const *c = this; c; c = c->dim_cache_[a].parent_l_)
592 for (Score_element const * d = s; d; d = d->dim_cache_[a].parent_l_)
594 return (Score_element*)d;
601 Score_element::common_refpoint (SCM elist, Axis a) const
603 Score_element * common = (Score_element*) this;
604 for (; gh_pair_p (elist); elist = gh_cdr (elist))
606 Score_element * s = unsmob_element (gh_car (elist));
608 common = common->common_refpoint (s, a);
615 Score_element::name () const
617 return classname (this);
621 Score_element::add_offset_callback (Offset_callback cb, Axis a)
623 dim_cache_[a].off_callbacks_.push (cb);
627 Score_element::has_extent_callback_b (Extent_callback cb, Axis a)const
629 return cb == dim_cache_[a].extent_callback_l_;
633 Score_element::has_offset_callback_b (Offset_callback cb, Axis a)const
635 for (int i= dim_cache_[a].off_callbacks_.size (); i--;)
637 if (dim_cache_[a].off_callbacks_[i] == cb)
644 Score_element::set_extent_callback (Dim_cache_callback dc, Axis a)
646 dim_cache_[a].extent_callback_l_ = dc ;
651 Score_element::set_parent (Score_element *g, Axis a)
653 dim_cache_[a].parent_l_ = g;
656 MAKE_SCHEME_CALLBACK(Score_element,fixup_refpoint);
658 Score_element::fixup_refpoint (SCM smob)
660 Score_element *me = unsmob_element (smob);
661 for (int a = X_AXIS; a < NO_AXES; a ++)
664 Score_element * parent = me->parent_l (ax);
669 if (parent->line_l () != me->line_l () && me->line_l ())
671 Score_element * newparent = parent->find_broken_piece (me->line_l ());
672 me->set_parent (newparent, ax);
675 if (Item * i = dynamic_cast<Item*> (me))
677 Item *parenti = dynamic_cast<Item*> (parent);
681 Direction my_dir = i->break_status_dir () ;
682 if (my_dir!= parenti->break_status_dir())
684 Item *newparent = parenti->find_prebroken_piece (my_dir);
685 me->set_parent (newparent, ax);
695 /****************************************************
697 ****************************************************/
699 #include "ly-smobs.icc"
701 IMPLEMENT_UNSMOB(Score_element, element);
702 IMPLEMENT_SMOBS(Score_element);
705 Score_element::mark_smob (SCM ses)
707 Score_element * s = SMOB_TO_TYPE (Score_element, ses);
708 if (s->self_scm_ != ses)
710 programming_error ("SMOB marking gone awry");
713 scm_gc_mark ( s->immutable_property_alist_);
715 s->do_derived_mark ();
716 if (s->parent_l (Y_AXIS))
717 scm_gc_mark (s->parent_l (Y_AXIS)->self_scm_);
718 if (s->parent_l (X_AXIS))
719 scm_gc_mark (s->parent_l (X_AXIS)->self_scm_);
721 return s->mutable_property_alist_;
725 Score_element::print_smob (SCM s, SCM port, scm_print_state *)
727 Score_element *sc = (Score_element *) gh_cdr (s);
729 scm_puts ("#<Score_element ", port);
730 scm_puts ((char *)sc->name (), port);
733 don't try to print properties, that is too much hassle.
735 scm_puts (" >", port);
740 Score_element::do_derived_mark ()
745 Score_element::do_smobify_self ()
750 Score_element::equal_p (SCM a, SCM b)
752 return gh_cdr(a) == gh_cdr(b) ? SCM_BOOL_T : SCM_BOOL_F;
757 ly_set_elt_property (SCM elt, SCM sym, SCM val)
759 Score_element * sc = unsmob_element (elt);
761 if (!gh_symbol_p (sym))
763 error ("Not a symbol");
764 ly_display_scm (sym);
765 return SCM_UNDEFINED;
770 sc->set_elt_property (sym, val);
774 error ("Not a score element");
775 ly_display_scm (elt);
778 return SCM_UNDEFINED;
783 ly_get_elt_property (SCM elt, SCM sym)
785 Score_element * sc = unsmob_element (elt);
789 return sc->get_elt_property (sym);
793 error ("Not a score element");
794 ly_display_scm (elt);
796 return SCM_UNDEFINED;
801 Score_element::discretionary_processing()
808 spanner_get_bound (SCM slur, SCM dir)
810 return dynamic_cast<Spanner*> (unsmob_element (slur))->get_bound (to_dir (dir))->self_scm_;
815 static SCM interfaces_sym;
820 interfaces_sym = scm_permanent_object (ly_symbol2scm ("interfaces"));
822 scm_make_gsubr ("ly-get-elt-property", 2, 0, 0, (SCM(*)(...))ly_get_elt_property);
823 scm_make_gsubr ("ly-set-elt-property", 3, 0, 0, (SCM(*)(...))ly_set_elt_property);
824 scm_make_gsubr ("ly-get-spanner-bound", 2 , 0, 0, (SCM(*)(...)) spanner_get_bound);
828 Score_element::has_interface (SCM k)
830 if (mutable_property_alist_ == SCM_EOL)
833 SCM ifs = get_elt_property (interfaces_sym);
835 return scm_memq (k, ifs) != SCM_BOOL_F;
839 Score_element::set_interface (SCM k)
841 if (has_interface (k))
845 set_elt_property (interfaces_sym,
846 gh_cons (k, get_elt_property (interfaces_sym)));
851 ADD_SCM_INIT_FUNC(scoreelt, init_functions);