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 property_alist_ = basicprops;
54 pointer_alist_ = SCM_EOL;
57 set_elt_pointer ("dependencies", SCM_EOL);
58 set_elt_property ("interfaces", SCM_EOL);
62 Score_element::Score_element (Score_element const&s)
63 : dim_cache_ (s.dim_cache_)
66 original_l_ =(Score_element*) &s;
67 property_alist_ = s.property_alist_;
69 pointer_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()
80 // this goes awry when score-elements are copied...
83 Kijk goed naar hoe smobify en unsmobify werken. unsmobify_self
84 is te gebruiken wanneer C++ geheugen beheer weer overneemt van
90 Score_element::get_elt_pointer (const char *nm) const
92 SCM sym = ly_symbol2scm (nm);
93 SCM s = scm_assq(sym, pointer_alist_);
95 return (s == SCM_BOOL_F) ? SCM_UNDEFINED : gh_cdr (s);
98 // should also have one that takes SCM arg.
100 Score_element::get_elt_property (String nm) const
102 SCM sym = ly_symbol2scm (nm.ch_C());
103 SCM s = scm_assq(sym, property_alist_);
108 return SCM_UNDEFINED;
112 Remove the value associated with KEY, and return it. The result is
113 that a next call will yield SCM_UNDEFINED (and not the underlying
117 Score_element::remove_elt_property (const char* key)
119 SCM val = get_elt_property (key);
120 if (val != SCM_UNDEFINED)
121 set_elt_property (key, SCM_UNDEFINED);
126 Score_element::set_elt_property (String k, SCM val)
128 SCM sym = ly_symbol2scm (k.ch_C ());
129 property_alist_ = gh_cons (gh_cons (sym, val), property_alist_);
133 Score_element::set_elt_pointer (const char* k, SCM v)
135 SCM s = ly_symbol2scm (k);
136 pointer_alist_ = scm_assq_set_x (pointer_alist_, s, v);
141 Score_element::molecule_extent (Score_element const *s, Axis a )
143 Molecule m = s->get_molecule ();
148 Score_element::preset_extent (Score_element const *s , Axis a )
150 SCM ext = s->get_elt_property ((a == X_AXIS)
156 Real l = gh_scm2double (gh_car (ext));
157 Real r = gh_scm2double (gh_cdr (ext));
158 l *= s->paper_l ()->get_var ("staffspace");
159 r *= s->paper_l ()->get_var ("staffspace");
160 return Interval (l, r);
169 Score_element::paper_l () const
171 return pscore_l_ ? pscore_l_->paper_l_ : 0;
175 Score_element::lookup_l () const
179 Score_element * urg = (Score_element*)this;
180 SCM sz = urg->remove_elt_property ("fontsize");
181 int i = (gh_number_p (sz))
185 urg->lookup_l_ = (Lookup*)pscore_l_->paper_l_->lookup_l (i);
191 Score_element::add_processing()
193 assert (status_i_ >=0);
202 Score_element::calculate_dependencies (int final, int busy,
203 Score_element_method_pointer funcptr)
205 assert (status_i_ >=0);
207 if (status_i_ >= final)
210 if (status_i_== busy)
212 programming_error ("Element is busy, come back later");
218 for (SCM d= get_elt_pointer ("dependencies"); gh_pair_p (d); d = gh_cdr (d))
220 unsmob_element (gh_car (d))
221 ->calculate_dependencies (final, busy, funcptr);
229 Score_element::get_molecule () const
231 SCM proc = get_elt_property ("molecule-callback");
232 if (gh_procedure_p (proc))
233 return create_molecule (gh_apply (proc, gh_list (this->self_scm_, SCM_UNDEFINED)));
245 Score_element::do_break_processing()
250 Score_element::after_line_breaking ()
256 Score_element::before_line_breaking ()
261 Score_element::do_space_processing ()
266 Score_element::do_add_processing()
271 MAKE_SCHEME_SCORE_ELEMENT_NON_DEFAULT_CALLBACKS(Score_element)
277 Score_element::scheme_molecule (SCM smob)
279 Score_element * sc = unsmob_element (smob);
280 SCM glyph = sc->get_elt_property ("glyph");
281 if (gh_string_p (glyph))
283 return sc->lookup_l ()->afm_find (String (ly_scm2string (glyph))).create_scheme ();
293 Score_element::line_l() const
299 Score_element::add_dependency (Score_element*e)
303 Pointer_group_interface gi (this, "dependencies");
307 programming_error ("Null dependency added");
314 Do break substitution in S, using CRITERION. Return new value.
315 CRITERION is either a SMOB pointer to the desired line, or a number
316 representing the break direction. Do not modify SRC.
319 Score_element::handle_broken_smobs (SCM src, SCM criterion)
322 Score_element *sc = unsmob_element (src);
325 if (gh_number_p (criterion))
327 Item * i = dynamic_cast<Item*> (sc);
328 Direction d = to_dir (criterion);
329 if (i && i->break_status_dir () != d)
331 Item *br = i->find_prebroken_piece (d);
332 return (br) ? br->self_scm_ : SCM_UNDEFINED;
338 = dynamic_cast<Line_of_score*> (unsmob_element (criterion));
339 if (sc->line_l () != line)
341 sc = sc->find_broken_piece (line);
345 /* now: !sc || (sc && sc->line_l () == line) */
347 return SCM_UNDEFINED;
349 /* now: sc && sc->line_l () == line */
351 || (sc->common_refpoint (line, X_AXIS)
352 && sc->common_refpoint (line, Y_AXIS)))
354 return sc->self_scm_;
356 return SCM_UNDEFINED;
359 else if (gh_pair_p (src))
361 SCM oldcar =gh_car (src);
363 UGH! breaks on circular lists.
365 SCM newcar = handle_broken_smobs (oldcar, criterion);
366 SCM oldcdr = gh_cdr (src);
368 if (newcar == SCM_UNDEFINED
369 && (gh_pair_p (oldcdr) || oldcdr == SCM_EOL))
372 This is tail-recursion, ie.
374 return handle_broken_smobs (cdr, criterion);
376 We don't want to rely on the compiler to do this. Without
377 tail-recursion, this easily crashes with a stack overflow. */
382 SCM newcdr = handle_broken_smobs (oldcdr, criterion);
383 return gh_cons (newcar, newcdr);
392 Score_element::handle_broken_dependencies()
394 Spanner * s= dynamic_cast<Spanner*> (this);
395 if (original_l_ && s)
400 for (int i = 0; i< s->broken_into_l_arr_ .size (); i++)
402 Score_element * sc = s->broken_into_l_arr_[i];
403 Line_of_score * l = sc->line_l ();
405 handle_broken_smobs (pointer_alist_,
406 l ? l->self_scm_ : SCM_UNDEFINED);
411 Line_of_score *line = line_l();
413 if (line && common_refpoint (line, X_AXIS) && common_refpoint (line, Y_AXIS))
416 = handle_broken_smobs (pointer_alist_,
417 line ? line->self_scm_ : SCM_UNDEFINED);
419 else if (dynamic_cast <Line_of_score*> (this))
421 pointer_alist_ = handle_broken_smobs (pointer_alist_,
427 This element is `invalid'; it has been removed from all
428 dependencies, so let's junk the element itself.
430 do not do this for Line_of_score, since that would remove
431 references to the originals of score-elts, which get then GC'd
439 Note that we still want references to this element to be
440 rearranged, and not silently thrown away, so we keep pointers
441 like {broken_into_{drul,array}, original}
444 Score_element::suicide ()
446 property_alist_ = SCM_EOL;
447 pointer_alist_ = SCM_EOL;
448 set_extent_callback (0, Y_AXIS);
449 set_extent_callback (0, X_AXIS);
451 for (int a= X_AXIS; a <= Y_AXIS; a++)
453 dim_cache_[a].off_callbacks_.clear ();
458 Score_element::handle_prebroken_dependencies()
463 Score_element::find_broken_piece (Line_of_score*) const
469 Score_element::translate_axis (Real y, Axis a)
471 if (isinf (y) || isnan (y))
472 programming_error (_(INFINITY_MSG));
475 dim_cache_[a].offset_ += y;
480 Score_element::relative_coordinate (Score_element const*refp, Axis a) const
486 We catch PARENT_L_ == nil case with this, but we crash if we did
487 not ask for the absolute coordinate (ie. REFP == nil.)
490 if (refp == dim_cache_[a].parent_l_)
491 return get_offset (a);
493 return get_offset (a) + dim_cache_[a].parent_l_->relative_coordinate (refp, a);
497 Score_element::get_offset (Axis a) const
499 Score_element *me = (Score_element*) this;
500 while (dim_cache_[a].off_callbacks_.size ())
502 Offset_callback c = dim_cache_[a].off_callbacks_[0];
503 me->dim_cache_[a].off_callbacks_.del (0);
504 Real r = (*c) (me,a );
505 if (isinf (r) || isnan (r))
508 programming_error (INFINITY_MSG);
510 me->dim_cache_[a].offset_ +=r;
512 return dim_cache_[a].offset_;
517 Score_element::point_dimension_callback (Score_element const* , Axis)
519 return Interval (0,0);
523 Score_element::empty_b (Axis a)const
525 return !dim_cache_[a].extent_callback_l_;
529 Score_element::extent (Axis a) const
531 Dimension_cache * d = (Dimension_cache *)&dim_cache_[a];
532 if (!d->extent_callback_l_)
534 d->dim_.set_empty ();
536 else if (!d->valid_b_)
538 d->dim_= (*d->extent_callback_l_ ) (this, a);
542 Interval ext = d->dim_;
547 SCM extra = get_elt_property (a == X_AXIS
554 Real s = paper_l ()->get_var ("staffspace");
555 if (gh_pair_p (extra))
557 ext[BIGGER] += s * gh_scm2double (gh_cdr (extra));
558 ext[SMALLER] += s * gh_scm2double (gh_car (extra));
561 extra = get_elt_property (a == X_AXIS
563 : "minimum-extent-Y");
564 if (gh_pair_p (extra))
566 ext.unite (Interval (s * gh_scm2double (gh_car (extra)),
567 s * gh_scm2double (gh_cdr (extra))));
575 Score_element::parent_l (Axis a) const
577 return dim_cache_[a].parent_l_;
581 Score_element::common_refpoint (Score_element const* s, Axis a) const
584 I don't like the quadratic aspect of this code, but I see no other
585 way. The largest chain of parents might be 10 high or so, so
586 it shouldn't be a real issue. */
587 for (Score_element const *c = this; c; c = c->dim_cache_[a].parent_l_)
588 for (Score_element const * d = s; d; d = d->dim_cache_[a].parent_l_)
590 return (Score_element*)d;
597 Score_element::common_refpoint (SCM elist, Axis a) const
599 Score_element * common = (Score_element*) this;
600 for (; gh_pair_p (elist); elist = gh_cdr (elist))
602 Score_element * s = unsmob_element (gh_car (elist));
604 common = common->common_refpoint (s, a);
611 Score_element::name () const
613 return classname (this);
617 Score_element::add_offset_callback (Offset_callback cb, Axis a)
619 dim_cache_[a].off_callbacks_.push (cb);
623 Score_element::has_extent_callback_b (Extent_callback cb, Axis a)const
625 return cb == dim_cache_[a].extent_callback_l_;
629 Score_element::has_offset_callback_b (Offset_callback cb, Axis a)const
631 for (int i= dim_cache_[a].off_callbacks_.size (); i--;)
633 if (dim_cache_[a].off_callbacks_[i] == cb)
640 Score_element::set_extent_callback (Dim_cache_callback dc, Axis a)
642 dim_cache_[a].extent_callback_l_ = dc ;
647 Score_element::set_parent (Score_element *g, Axis a)
649 dim_cache_[a].parent_l_ = g;
653 Score_element::fixup_refpoint ()
655 for (int a = X_AXIS; a < NO_AXES; a ++)
658 Score_element * parent = parent_l (ax);
663 if (parent->line_l () != line_l () && line_l ())
665 Score_element * newparent = parent->find_broken_piece (line_l ());
666 set_parent (newparent, ax);
669 if (Item * i = dynamic_cast<Item*> (this))
671 Item *parenti = dynamic_cast<Item*> (parent);
675 Direction my_dir = i->break_status_dir () ;
676 if (my_dir!= parenti->break_status_dir())
678 Item *newparent = parenti->find_prebroken_piece (my_dir);
679 set_parent (newparent, ax);
688 /****************************************************
690 ****************************************************/
692 #include "ly-smobs.icc"
694 IMPLEMENT_UNSMOB(Score_element, element);
695 IMPLEMENT_SMOBS(Score_element);
698 Score_element::mark_smob (SCM ses)
700 Score_element * s = SMOB_TO_TYPE (Score_element, ses);
701 if (s->self_scm_ != ses)
703 programming_error ("SMOB marking gone awry");
706 scm_gc_mark (s->pointer_alist_);
708 s->do_derived_mark ();
709 if (s->parent_l (Y_AXIS))
710 scm_gc_mark (s->parent_l (Y_AXIS)->self_scm_);
711 if (s->parent_l (X_AXIS))
712 scm_gc_mark (s->parent_l (X_AXIS)->self_scm_);
714 return s->property_alist_;
718 Score_element::print_smob (SCM s, SCM port, scm_print_state *)
720 Score_element *sc = (Score_element *) gh_cdr (s);
722 scm_puts ("#<Score_element ", port);
723 scm_puts ((char *)sc->name (), port);
726 don't try to print properties, that is too much hassle.
728 scm_puts (" >", port);
733 Score_element::do_derived_mark ()
738 Score_element::do_smobify_self ()
743 Score_element::equal_p (SCM a, SCM b)
745 return gh_cdr(a) == gh_cdr(b) ? SCM_BOOL_T : SCM_BOOL_F;
750 Score_element::ly_set_elt_property (SCM elt, SCM sym, SCM val)
752 Score_element * sc = unsmob_element (elt);
754 if (!gh_symbol_p (sym))
756 error ("Not a symbol");
757 ly_display_scm (sym);
758 return SCM_UNDEFINED;
763 sc->property_alist_ = scm_assq_set_x (sc->property_alist_, sym, val);
767 error ("Not a score element");
768 ly_display_scm (elt);
771 return SCM_UNDEFINED;
776 Score_element::ly_get_elt_property (SCM elt, SCM sym)
778 Score_element * sc = unsmob_element (elt);
782 SCM s = scm_assq(sym, sc->property_alist_);
787 return SCM_UNDEFINED;
791 error ("Not a score element");
792 ly_display_scm (elt);
794 return SCM_UNDEFINED;
801 scm_make_gsubr ("ly-get-elt-property", 2, 0, 0, (SCM(*)(...))Score_element::ly_get_elt_property);
802 scm_make_gsubr ("ly-set-elt-property", 3, 0, 0, (SCM(*)(...))Score_element::ly_set_elt_property);
805 ADD_SCM_INIT_FUNC(scoreelt, init_functions);
808 Score_element::discretionary_processing()