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"
37 remove dynamic_cast<Spanner,Item> and put this code into respective
42 #define INFINITY_MSG "Infinity or NaN encountered"
44 Score_element::Score_element(SCM basicprops)
46 set_extent_callback (molecule_extent, X_AXIS);
47 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_)
67 original_l_ =(Score_element*) &s;
68 immutable_property_alist_ = s.immutable_property_alist_;
69 mutable_property_alist_ = SCM_EOL;
71 status_i_ = s.status_i_;
72 lookup_l_ = s.lookup_l_;
73 pscore_l_ = s.pscore_l_;
78 Score_element::~Score_element()
81 do nothing scm-ish and no unprotecting here.
87 Score_element::get_elt_property (const char *nm) const
89 SCM sym = ly_symbol2scm (nm);
90 return get_elt_property (sym);
94 Score_element::get_elt_property (SCM sym) const
96 SCM s = scm_sloppy_assq(sym, mutable_property_alist_);
100 s = scm_sloppy_assq (sym, immutable_property_alist_);
101 return (s == SCM_BOOL_F) ? SCM_EOL : gh_cdr (s);
105 Remove the value associated with KEY, and return it. The result is
106 that a next call will yield SCM_UNDEFINED (and not the underlying
110 Score_element::remove_elt_property (const char* key)
112 SCM val = get_elt_property (key);
114 set_elt_property (key, SCM_EOL);
119 Score_element::set_elt_property (const char* k, SCM v)
121 SCM s = ly_symbol2scm (k);
122 set_elt_property (s, v);
126 Puts the k, v in the immutable_property_alist_, which is convenient for
127 storing variables that are needed during the breaking process. (eg.
128 Line_of_score::rank : int )
131 Score_element::set_immutable_elt_property (const char*k, SCM v)
133 SCM s = ly_symbol2scm (k);
134 set_immutable_elt_property (s, v);
138 Score_element::set_immutable_elt_property (SCM s, SCM v)
140 immutable_property_alist_ = gh_cons (gh_cons (s,v), mutable_property_alist_);
141 mutable_property_alist_ = scm_assq_remove_x (mutable_property_alist_, s);
144 Score_element::set_elt_property (SCM s, SCM v)
146 mutable_property_alist_ = scm_assq_set_x (mutable_property_alist_, s, v);
151 Score_element::molecule_extent (Score_element *s, Axis a)
153 Molecule m = s->get_molecule ();
158 Score_element::preset_extent (Score_element *s , Axis a)
160 SCM ext = s->get_elt_property ((a == X_AXIS)
166 Real l = gh_scm2double (gh_car (ext));
167 Real r = gh_scm2double (gh_cdr (ext));
168 l *= s->paper_l ()->get_var ("staffspace");
169 r *= s->paper_l ()->get_var ("staffspace");
170 return Interval (l, r);
179 Score_element::paper_l () const
181 return pscore_l_ ? pscore_l_->paper_l_ : 0;
185 Score_element::lookup_l () const
189 Score_element * urg = (Score_element*)this;
190 SCM sz = urg->remove_elt_property ("fontsize");
191 int i = (gh_number_p (sz))
195 urg->lookup_l_ = (Lookup*)pscore_l_->paper_l_->lookup_l (i);
201 Score_element::add_processing()
203 assert (status_i_ >=0);
212 Score_element::calculate_dependencies (int final, int busy, SCM funcname)
214 assert (status_i_ >=0);
216 if (status_i_ >= final)
219 if (status_i_== busy)
221 programming_error ("Element is busy, come back later");
227 for (SCM d= get_elt_property ("dependencies"); gh_pair_p (d); d = gh_cdr (d))
229 unsmob_element (gh_car (d))
230 ->calculate_dependencies (final, busy, funcname);
234 String s = ly_symbol2string (funcname);
235 SCM proc = get_elt_property (s.ch_C());
236 if (gh_procedure_p (proc))
237 gh_call1 (proc, this->self_scm ());
244 Score_element::get_molecule () const
246 SCM proc = get_elt_property ("molecule-callback");
249 if (gh_procedure_p (proc))
250 mol = gh_apply (proc, gh_list (this->self_scm (), SCM_UNDEFINED));
253 SCM origin =get_elt_property ("origin");
254 if (!unsmob_input (origin))
255 origin =ly_symbol2scm ("no-origin");
260 mol = gh_cons (gh_list (origin, gh_car (mol), SCM_UNDEFINED), gh_cdr (mol));
264 return create_molecule (mol);
274 Score_element::do_break_processing()
280 Score_element::do_add_processing()
285 MAKE_SCHEME_CALLBACK(Score_element,brew_molecule)
291 Score_element::brew_molecule (SCM smob)
293 Score_element * sc = unsmob_element (smob);
294 SCM glyph = sc->get_elt_property ("glyph");
295 if (gh_string_p (glyph))
297 return sc->lookup_l ()->afm_find (String (ly_scm2string (glyph))).create_scheme ();
307 Score_element::line_l() const
313 Score_element::add_dependency (Score_element*e)
317 Pointer_group_interface gi (this, "dependencies");
321 programming_error ("Null dependency added");
328 Do break substitution in S, using CRITERION. Return new value.
329 CRITERION is either a SMOB pointer to the desired line, or a number
330 representing the break direction. Do not modify SRC.
333 Score_element::handle_broken_smobs (SCM src, SCM criterion)
336 Score_element *sc = unsmob_element (src);
339 if (gh_number_p (criterion))
341 Item * i = dynamic_cast<Item*> (sc);
342 Direction d = to_dir (criterion);
343 if (i && i->break_status_dir () != d)
345 Item *br = i->find_prebroken_piece (d);
346 return (br) ? br->self_scm () : SCM_UNDEFINED;
352 = dynamic_cast<Line_of_score*> (unsmob_element (criterion));
353 if (sc->line_l () != line)
355 sc = sc->find_broken_piece (line);
359 /* now: !sc || (sc && sc->line_l () == line) */
361 return SCM_UNDEFINED;
363 /* now: sc && sc->line_l () == line */
365 || (sc->common_refpoint (line, X_AXIS)
366 && sc->common_refpoint (line, Y_AXIS)))
368 return sc->self_scm ();
370 return SCM_UNDEFINED;
373 else if (gh_pair_p (src))
375 SCM oldcar =gh_car (src);
377 UGH! breaks on circular lists.
379 SCM newcar = handle_broken_smobs (oldcar, criterion);
380 SCM oldcdr = gh_cdr (src);
382 if (newcar == SCM_UNDEFINED
383 && (gh_pair_p (oldcdr) || oldcdr == SCM_EOL))
386 This is tail-recursion, ie.
388 return handle_broken_smobs (cdr, criterion);
390 We don't want to rely on the compiler to do this. Without
391 tail-recursion, this easily crashes with a stack overflow. */
396 SCM newcdr = handle_broken_smobs (oldcdr, criterion);
397 return gh_cons (newcar, newcdr);
406 Score_element::handle_broken_dependencies()
408 Spanner * s= dynamic_cast<Spanner*> (this);
409 if (original_l_ && s)
414 for (int i = 0; i< s->broken_into_l_arr_ .size (); i++)
416 Score_element * sc = s->broken_into_l_arr_[i];
417 Line_of_score * l = sc->line_l ();
418 sc->mutable_property_alist_ =
419 handle_broken_smobs (mutable_property_alist_,
420 l ? l->self_scm () : SCM_UNDEFINED);
425 Line_of_score *line = line_l();
427 if (line && common_refpoint (line, X_AXIS) && common_refpoint (line, Y_AXIS))
429 mutable_property_alist_
430 = handle_broken_smobs (mutable_property_alist_,
431 line ? line->self_scm () : SCM_UNDEFINED);
433 else if (dynamic_cast <Line_of_score*> (this))
435 mutable_property_alist_ = handle_broken_smobs (mutable_property_alist_,
441 This element is `invalid'; it has been removed from all
442 dependencies, so let's junk the element itself.
444 do not do this for Line_of_score, since that would remove
445 references to the originals of score-elts, which get then GC'd
453 Note that we still want references to this element to be
454 rearranged, and not silently thrown away, so we keep pointers
455 like {broken_into_{drul,array}, original}
458 Score_element::suicide ()
460 mutable_property_alist_ = SCM_EOL;
461 immutable_property_alist_ = SCM_EOL;
462 set_extent_callback (0, Y_AXIS);
463 set_extent_callback (0, X_AXIS);
465 for (int a= X_AXIS; a <= Y_AXIS; a++)
467 dim_cache_[a].off_callbacks_.clear ();
472 Score_element::handle_prebroken_dependencies()
477 Score_element::find_broken_piece (Line_of_score*) const
483 Score_element::translate_axis (Real y, Axis a)
485 if (isinf (y) || isnan (y))
486 programming_error (_(INFINITY_MSG));
489 dim_cache_[a].offset_ += y;
494 Score_element::relative_coordinate (Score_element const*refp, Axis a) const
500 We catch PARENT_L_ == nil case with this, but we crash if we did
501 not ask for the absolute coordinate (ie. REFP == nil.)
504 if (refp == dim_cache_[a].parent_l_)
505 return get_offset (a);
507 return get_offset (a) + dim_cache_[a].parent_l_->relative_coordinate (refp, a);
511 Score_element::get_offset (Axis a) const
513 Score_element *me = (Score_element*) this;
514 while (dim_cache_[a].off_callbacks_.size ())
516 Offset_callback c = dim_cache_[a].off_callbacks_[0];
517 me->dim_cache_[a].off_callbacks_.del (0);
518 Real r = (*c) (me,a );
519 if (isinf (r) || isnan (r))
522 programming_error (INFINITY_MSG);
524 me->dim_cache_[a].offset_ +=r;
526 return dim_cache_[a].offset_;
531 Score_element::point_dimension_callback (Score_element* , Axis)
533 return Interval (0,0);
537 Score_element::empty_b (Axis a)const
539 return !dim_cache_[a].extent_callback_l_;
543 Score_element::extent (Axis a) const
545 Dimension_cache * d = (Dimension_cache *)&dim_cache_[a];
546 if (!d->extent_callback_l_)
548 d->dim_.set_empty ();
550 else if (!d->valid_b_)
552 d->dim_= (*d->extent_callback_l_ ) ((Score_element*)this, a);
556 Interval ext = d->dim_;
561 SCM extra = get_elt_property (a == X_AXIS
568 Real s = paper_l ()->get_var ("staffspace");
569 if (gh_pair_p (extra))
571 ext[BIGGER] += s * gh_scm2double (gh_cdr (extra));
572 ext[SMALLER] += s * gh_scm2double (gh_car (extra));
575 extra = get_elt_property (a == X_AXIS
577 : "minimum-extent-Y");
578 if (gh_pair_p (extra))
580 ext.unite (Interval (s * gh_scm2double (gh_car (extra)),
581 s * gh_scm2double (gh_cdr (extra))));
589 Score_element::parent_l (Axis a) const
591 return dim_cache_[a].parent_l_;
595 Score_element::common_refpoint (Score_element const* s, Axis a) const
598 I don't like the quadratic aspect of this code, but I see no other
599 way. The largest chain of parents might be 10 high or so, so
600 it shouldn't be a real issue. */
601 for (Score_element const *c = this; c; c = c->dim_cache_[a].parent_l_)
602 for (Score_element const * d = s; d; d = d->dim_cache_[a].parent_l_)
604 return (Score_element*)d;
611 Score_element::common_refpoint (SCM elist, Axis a) const
613 Score_element * common = (Score_element*) this;
614 for (; gh_pair_p (elist); elist = gh_cdr (elist))
616 Score_element * s = unsmob_element (gh_car (elist));
618 common = common->common_refpoint (s, a);
625 Score_element::name () const
627 return classname (this);
631 Score_element::add_offset_callback (Offset_callback cb, Axis a)
633 dim_cache_[a].off_callbacks_.push (cb);
637 Score_element::has_extent_callback_b (Extent_callback cb, Axis a)const
639 return cb == dim_cache_[a].extent_callback_l_;
643 Score_element::has_offset_callback_b (Offset_callback cb, Axis a)const
645 for (int i= dim_cache_[a].off_callbacks_.size (); i--;)
647 if (dim_cache_[a].off_callbacks_[i] == cb)
654 Score_element::set_extent_callback (Dim_cache_callback dc, Axis a)
656 dim_cache_[a].extent_callback_l_ = dc ;
661 Score_element::set_parent (Score_element *g, Axis a)
663 dim_cache_[a].parent_l_ = g;
666 MAKE_SCHEME_CALLBACK(Score_element,fixup_refpoint);
668 Score_element::fixup_refpoint (SCM smob)
670 Score_element *me = unsmob_element (smob);
671 for (int a = X_AXIS; a < NO_AXES; a ++)
674 Score_element * parent = me->parent_l (ax);
679 if (parent->line_l () != me->line_l () && me->line_l ())
681 Score_element * newparent = parent->find_broken_piece (me->line_l ());
682 me->set_parent (newparent, ax);
685 if (Item * i = dynamic_cast<Item*> (me))
687 Item *parenti = dynamic_cast<Item*> (parent);
691 Direction my_dir = i->break_status_dir () ;
692 if (my_dir!= parenti->break_status_dir())
694 Item *newparent = parenti->find_prebroken_piece (my_dir);
695 me->set_parent (newparent, ax);
705 /****************************************************
707 ****************************************************/
709 #include "ly-smobs.icc"
711 IMPLEMENT_UNSMOB(Score_element, element);
712 IMPLEMENT_SMOBS(Score_element);
713 IMPLEMENT_DEFAULT_EQUAL_P(Score_element);
716 Score_element::mark_smob (SCM ses)
718 Score_element * s = (Score_element*) SCM_CELL_WORD_1(ses);
719 scm_gc_mark (s->immutable_property_alist_);
720 scm_gc_mark (s->mutable_property_alist_);
722 if (s->parent_l (Y_AXIS))
723 scm_gc_mark (s->parent_l (Y_AXIS)->self_scm ());
724 if (s->parent_l (X_AXIS))
725 scm_gc_mark (s->parent_l (X_AXIS)->self_scm ());
728 scm_gc_mark (s->original_l_->self_scm ());
729 return s->do_derived_mark ();
733 Score_element::print_smob (SCM s, SCM port, scm_print_state *)
735 Score_element *sc = (Score_element *) gh_cdr (s);
737 scm_puts ("#<Score_element ", port);
738 scm_puts ((char *)sc->name (), port);
741 don't try to print properties, that is too much hassle.
743 scm_puts (" >", port);
748 Score_element::do_derived_mark ()
755 ly_set_elt_property (SCM elt, SCM sym, SCM val)
757 Score_element * sc = unsmob_element (elt);
759 if (!gh_symbol_p (sym))
761 error ("Not a symbol");
762 ly_display_scm (sym);
763 return SCM_UNSPECIFIED;
768 sc->set_elt_property (sym, val);
772 error ("Not a score element");
773 ly_display_scm (elt);
776 return SCM_UNSPECIFIED;
781 ly_get_elt_property (SCM elt, SCM sym)
783 Score_element * sc = unsmob_element (elt);
787 return sc->get_elt_property (sym);
791 error ("Not a score element");
792 ly_display_scm (elt);
794 return SCM_UNSPECIFIED;
799 Score_element::discretionary_processing()
806 spanner_get_bound (SCM slur, SCM dir)
808 return dynamic_cast<Spanner*> (unsmob_element (slur))->get_bound (to_dir (dir))->self_scm ();
813 static SCM interfaces_sym;
818 interfaces_sym = scm_permanent_object (ly_symbol2scm ("interfaces"));
820 scm_make_gsubr ("ly-get-elt-property", 2, 0, 0, (SCM(*)(...))ly_get_elt_property);
821 scm_make_gsubr ("ly-set-elt-property", 3, 0, 0, (SCM(*)(...))ly_set_elt_property);
822 scm_make_gsubr ("ly-get-spanner-bound", 2 , 0, 0, (SCM(*)(...)) spanner_get_bound);
826 Score_element::has_interface (SCM k)
828 if (mutable_property_alist_ == SCM_EOL)
831 SCM ifs = get_elt_property (interfaces_sym);
833 return scm_memq (k, ifs) != SCM_BOOL_F;
837 Score_element::set_interface (SCM k)
839 if (has_interface (k))
843 set_elt_property (interfaces_sym,
844 gh_cons (k, get_elt_property (interfaces_sym)));
849 ADD_SCM_INIT_FUNC(scoreelt, init_functions);