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;
90 Score_element::Score_element (Score_element const&s)
91 : dim_cache_ (s.dim_cache_)
93 original_l_ =(Score_element*) &s;
94 immutable_property_alist_ = s.immutable_property_alist_;
95 mutable_property_alist_ = SCM_EOL;
97 status_i_ = s.status_i_;
98 lookup_l_ = s.lookup_l_;
99 pscore_l_ = s.pscore_l_;
104 Score_element::~Score_element()
107 do nothing scm-ish and no unprotecting here.
113 Score_element::get_elt_property (const char *nm) const
115 SCM sym = ly_symbol2scm (nm);
116 return get_elt_property (sym);
120 Score_element::get_elt_property (SCM sym) const
122 SCM s = scm_sloppy_assq(sym, mutable_property_alist_);
126 s = scm_sloppy_assq (sym, immutable_property_alist_);
127 return (s == SCM_BOOL_F) ? SCM_EOL : gh_cdr (s);
131 Remove the value associated with KEY, and return it. The result is
132 that a next call will yield SCM_UNDEFINED (and not the underlying
136 Score_element::remove_elt_property (const char* key)
138 SCM val = get_elt_property (key);
140 set_elt_property (key, SCM_EOL);
145 Score_element::set_elt_property (const char* k, SCM v)
147 SCM s = ly_symbol2scm (k);
148 set_elt_property (s, v);
152 Puts the k, v in the immutable_property_alist_, which is convenient for
153 storing variables that are needed during the breaking process. (eg.
154 Line_of_score::rank : int )
157 Score_element::set_immutable_elt_property (const char*k, SCM v)
159 SCM s = ly_symbol2scm (k);
160 set_immutable_elt_property (s, v);
164 Score_element::set_immutable_elt_property (SCM s, SCM v)
166 immutable_property_alist_ = gh_cons (gh_cons (s,v), mutable_property_alist_);
167 mutable_property_alist_ = scm_assq_remove_x (mutable_property_alist_, s);
170 Score_element::set_elt_property (SCM s, SCM v)
172 mutable_property_alist_ = scm_assq_set_x (mutable_property_alist_, s, v);
176 MAKE_SCHEME_CALLBACK(Score_element,molecule_extent,2);
178 Score_element::molecule_extent (SCM element_smob, SCM scm_axis)
180 Score_element *s = unsmob_element (element_smob);
181 Axis a = (Axis) gh_scm2int (scm_axis);
183 Molecule m = s->get_molecule ();
184 return ly_interval2scm ( m.extent(a));
187 MAKE_SCHEME_CALLBACK(Score_element,preset_extent,2);
190 Score_element::preset_extent (SCM element_smob, SCM scm_axis)
192 Score_element *s = unsmob_element (element_smob);
193 Axis a = (Axis) gh_scm2int (scm_axis);
195 SCM ext = s->get_elt_property ((a == X_AXIS)
201 Real l = gh_scm2double (gh_car (ext));
202 Real r = gh_scm2double (gh_cdr (ext));
203 l *= s->paper_l ()->get_var ("staffspace");
204 r *= s->paper_l ()->get_var ("staffspace");
205 return ly_interval2scm (Interval (l, r));
208 return ly_interval2scm ( Interval ());
214 Score_element::paper_l () const
216 return pscore_l_ ? pscore_l_->paper_l_ : 0;
220 Score_element::lookup_l () const
223 URG junkthis, caching is clumsy.
227 Score_element * urg = (Score_element*)this;
228 SCM sz = urg->remove_elt_property ("font-size");
229 int i = (gh_number_p (sz))
233 urg->lookup_l_ = (Lookup*)pscore_l_->paper_l_->lookup_l (i);
239 Score_element::calculate_dependencies (int final, int busy, SCM funcname)
241 assert (status_i_ >=0);
243 if (status_i_ >= final)
246 if (status_i_== busy)
248 programming_error ("Element is busy, come back later");
254 for (SCM d= get_elt_property ("dependencies"); gh_pair_p (d); d = gh_cdr (d))
256 unsmob_element (gh_car (d))
257 ->calculate_dependencies (final, busy, funcname);
261 String s = ly_symbol2string (funcname);
262 SCM proc = get_elt_property (s.ch_C());
263 if (gh_procedure_p (proc))
264 gh_call1 (proc, this->self_scm ());
271 Score_element::get_molecule () const
273 SCM proc = get_elt_property ("molecule-callback");
276 if (gh_procedure_p (proc))
277 mol = gh_apply (proc, gh_list (this->self_scm (), SCM_UNDEFINED));
280 SCM origin =get_elt_property ("origin");
281 if (!unsmob_input (origin))
282 origin =ly_symbol2scm ("no-origin");
287 mol = gh_cons (gh_list (origin, gh_car (mol), SCM_UNDEFINED), gh_cdr (mol));
291 Molecule m (create_molecule (mol));
294 This is almost the same as setting molecule-callback to #f, but
295 this retains the dimensions of this element, which means that you
296 can erase elements individually. */
297 if (to_boolean (get_elt_property ("transparent")))
298 m = Molecule (m.extent_box (), SCM_EOL);
310 Score_element::do_break_processing()
318 MAKE_SCHEME_CALLBACK(Score_element,brew_molecule,1)
324 Score_element::brew_molecule (SCM smob)
326 Score_element * sc = unsmob_element (smob);
327 SCM glyph = sc->get_elt_property ("glyph");
328 if (gh_string_p (glyph))
330 return sc->lookup_l ()->afm_find (String (ly_scm2string (glyph))).create_scheme ();
340 Score_element::line_l() const
346 Score_element::add_dependency (Score_element*e)
350 Pointer_group_interface ::add_element (this, "dependencies",e);
354 programming_error ("Null dependency added");
361 Do break substitution in S, using CRITERION. Return new value.
362 CRITERION is either a SMOB pointer to the desired line, or a number
363 representing the break direction. Do not modify SRC.
366 Score_element::handle_broken_smobs (SCM src, SCM criterion)
369 Score_element *sc = unsmob_element (src);
372 if (gh_number_p (criterion))
374 Item * i = dynamic_cast<Item*> (sc);
375 Direction d = to_dir (criterion);
376 if (i && i->break_status_dir () != d)
378 Item *br = i->find_prebroken_piece (d);
379 return (br) ? br->self_scm () : SCM_UNDEFINED;
385 = dynamic_cast<Line_of_score*> (unsmob_element (criterion));
386 if (sc->line_l () != line)
388 sc = sc->find_broken_piece (line);
392 /* now: !sc || (sc && sc->line_l () == line) */
394 return SCM_UNDEFINED;
396 /* now: sc && sc->line_l () == line */
398 || (sc->common_refpoint (line, X_AXIS)
399 && sc->common_refpoint (line, Y_AXIS)))
401 return sc->self_scm ();
403 return SCM_UNDEFINED;
406 else if (gh_pair_p (src))
408 SCM oldcar =gh_car (src);
410 UGH! breaks on circular lists.
412 SCM newcar = handle_broken_smobs (oldcar, criterion);
413 SCM oldcdr = gh_cdr (src);
415 if (newcar == SCM_UNDEFINED
416 && (gh_pair_p (oldcdr) || oldcdr == SCM_EOL))
419 This is tail-recursion, ie.
421 return handle_broken_smobs (cdr, criterion);
423 We don't want to rely on the compiler to do this. Without
424 tail-recursion, this easily crashes with a stack overflow. */
429 SCM newcdr = handle_broken_smobs (oldcdr, criterion);
430 return gh_cons (newcar, newcdr);
439 Score_element::handle_broken_dependencies()
441 Spanner * s= dynamic_cast<Spanner*> (this);
442 if (original_l_ && s)
447 for (int i = 0; i< s->broken_into_l_arr_ .size (); i++)
449 Score_element * sc = s->broken_into_l_arr_[i];
450 Line_of_score * l = sc->line_l ();
451 sc->mutable_property_alist_ =
452 handle_broken_smobs (mutable_property_alist_,
453 l ? l->self_scm () : SCM_UNDEFINED);
458 Line_of_score *line = line_l();
460 if (line && common_refpoint (line, X_AXIS) && common_refpoint (line, Y_AXIS))
462 mutable_property_alist_
463 = handle_broken_smobs (mutable_property_alist_,
464 line ? line->self_scm () : SCM_UNDEFINED);
466 else if (dynamic_cast <Line_of_score*> (this))
468 mutable_property_alist_ = handle_broken_smobs (mutable_property_alist_,
474 This element is `invalid'; it has been removed from all
475 dependencies, so let's junk the element itself.
477 do not do this for Line_of_score, since that would remove
478 references to the originals of score-elts, which get then GC'd
486 Note that we still want references to this element to be
487 rearranged, and not silently thrown away, so we keep pointers
488 like {broken_into_{drul,array}, original}
491 Score_element::suicide ()
493 mutable_property_alist_ = SCM_EOL;
494 immutable_property_alist_ = SCM_EOL;
496 set_extent_callback (SCM_EOL, Y_AXIS);
497 set_extent_callback (SCM_EOL, X_AXIS);
499 for (int a= X_AXIS; a <= Y_AXIS; a++)
501 dim_cache_[a].offset_callbacks_ = SCM_EOL;
502 dim_cache_[a].offsets_left_ = 0;
507 Score_element::handle_prebroken_dependencies()
512 Score_element::find_broken_piece (Line_of_score*) const
518 Score_element::translate_axis (Real y, Axis a)
520 if (isinf (y) || isnan (y))
521 programming_error (_(INFINITY_MSG));
524 dim_cache_[a].offset_ += y;
529 Score_element::relative_coordinate (Score_element const*refp, Axis a) const
535 We catch PARENT_L_ == nil case with this, but we crash if we did
536 not ask for the absolute coordinate (ie. REFP == nil.)
539 if (refp == dim_cache_[a].parent_l_)
540 return get_offset (a);
542 return get_offset (a) + dim_cache_[a].parent_l_->relative_coordinate (refp, a);
546 Score_element::get_offset (Axis a) const
548 Score_element *me = (Score_element*) this;
549 while (dim_cache_[a].offsets_left_)
551 int l = --me->dim_cache_[a].offsets_left_;
552 SCM cb = scm_list_ref (dim_cache_[a].offset_callbacks_, gh_int2scm (l));
553 SCM retval = gh_call2 (cb, self_scm (), gh_int2scm (a));
555 Real r = gh_scm2double (retval);
556 if (isinf (r) || isnan (r))
558 programming_error (INFINITY_MSG);
561 me->dim_cache_[a].offset_ +=r;
563 return dim_cache_[a].offset_;
567 MAKE_SCHEME_CALLBACK(Score_element,point_dimension_callback,2);
569 Score_element::point_dimension_callback (SCM , SCM )
571 return ly_interval2scm ( Interval (0,0));
575 Score_element::empty_b (Axis a)const
577 return ! (gh_pair_p (dim_cache_[a].dimension_ ) ||
578 gh_procedure_p (dim_cache_[a].dimension_ ));
584 Score_element *refpoint
589 Score_element::extent (Score_element * refp, Axis a) const
591 Real x = relative_coordinate (refp, a);
594 Dimension_cache * d = (Dimension_cache *)&dim_cache_[a];
596 if (gh_pair_p (d->dimension_))
598 else if (gh_procedure_p (d->dimension_))
601 FIXME: add doco on types, and should typecheck maybe?
603 d->dimension_= gh_call2 (d->dimension_, self_scm(), gh_int2scm (a));
608 ext = ly_scm2interval (d->dimension_);
610 SCM extra = get_elt_property (a == X_AXIS
617 Real s = paper_l ()->get_var ("staffspace");
618 if (gh_pair_p (extra))
620 ext[BIGGER] += s * gh_scm2double (gh_cdr (extra));
621 ext[SMALLER] += s * gh_scm2double (gh_car (extra));
624 extra = get_elt_property (a == X_AXIS
626 : "minimum-extent-Y");
627 if (gh_pair_p (extra))
629 ext.unite (Interval (s * gh_scm2double (gh_car (extra)),
630 s * gh_scm2double (gh_cdr (extra))));
640 Score_element::parent_l (Axis a) const
642 return dim_cache_[a].parent_l_;
646 Score_element::common_refpoint (Score_element const* s, Axis a) const
649 I don't like the quadratic aspect of this code, but I see no other
650 way. The largest chain of parents might be 10 high or so, so
651 it shouldn't be a real issue. */
652 for (Score_element const *c = this; c; c = c->dim_cache_[a].parent_l_)
653 for (Score_element const * d = s; d; d = d->dim_cache_[a].parent_l_)
655 return (Score_element*)d;
662 Score_element::common_refpoint (SCM elist, Axis a) const
664 Score_element * common = (Score_element*) this;
665 for (; gh_pair_p (elist); elist = gh_cdr (elist))
667 Score_element * s = unsmob_element (gh_car (elist));
669 common = common->common_refpoint (s, a);
676 Score_element::name () const
678 SCM nm = get_elt_property ("name");
680 return nm == SCM_EOL ? classname (this) :ly_scm2string (nm) ;
684 Score_element::add_offset_callback (SCM cb, Axis a)
686 if (!has_offset_callback_b (cb, a))
688 dim_cache_[a].offset_callbacks_ = gh_cons (cb, dim_cache_[a].offset_callbacks_ );
689 dim_cache_[a].offsets_left_ ++;
694 Score_element::has_extent_callback_b (SCM cb, Axis a)const
696 return scm_equal_p (cb, dim_cache_[a].dimension_);
701 Score_element::has_extent_callback_b (Axis a) const
703 return gh_procedure_p (dim_cache_[a].dimension_);
707 Score_element::has_offset_callback_b (SCM cb, Axis a)const
709 return scm_memq (cb, dim_cache_[a].offset_callbacks_) != SCM_BOOL_F;
713 Score_element::set_extent_callback (SCM dc, Axis a)
715 dim_cache_[a].dimension_ =dc;
719 Score_element::set_parent (Score_element *g, Axis a)
721 dim_cache_[a].parent_l_ = g;
724 MAKE_SCHEME_CALLBACK(Score_element,fixup_refpoint,1);
726 Score_element::fixup_refpoint (SCM smob)
728 Score_element *me = unsmob_element (smob);
729 for (int a = X_AXIS; a < NO_AXES; a ++)
732 Score_element * parent = me->parent_l (ax);
737 if (parent->line_l () != me->line_l () && me->line_l ())
739 Score_element * newparent = parent->find_broken_piece (me->line_l ());
740 me->set_parent (newparent, ax);
743 if (Item * i = dynamic_cast<Item*> (me))
745 Item *parenti = dynamic_cast<Item*> (parent);
749 Direction my_dir = i->break_status_dir () ;
750 if (my_dir!= parenti->break_status_dir())
752 Item *newparent = parenti->find_prebroken_piece (my_dir);
753 me->set_parent (newparent, ax);
763 /****************************************************
765 ****************************************************/
768 IMPLEMENT_UNSMOB(Score_element, element);
769 IMPLEMENT_SMOBS(Score_element);
770 IMPLEMENT_DEFAULT_EQUAL_P(Score_element);
773 Score_element::mark_smob (SCM ses)
775 Score_element * s = (Score_element*) SCM_CELL_WORD_1(ses);
776 scm_gc_mark (s->immutable_property_alist_);
777 scm_gc_mark (s->mutable_property_alist_);
779 for (int a =0 ; a < 2; a++)
781 scm_gc_mark (s->dim_cache_[a].offset_callbacks_);
782 scm_gc_mark (s->dim_cache_[a].dimension_);
785 if (s->parent_l (Y_AXIS))
786 scm_gc_mark (s->parent_l (Y_AXIS)->self_scm ());
787 if (s->parent_l (X_AXIS))
788 scm_gc_mark (s->parent_l (X_AXIS)->self_scm ());
791 scm_gc_mark (s->original_l_->self_scm ());
792 return s->do_derived_mark ();
796 Score_element::print_smob (SCM s, SCM port, scm_print_state *)
798 Score_element *sc = (Score_element *) gh_cdr (s);
800 scm_puts ("#<Score_element ", port);
801 scm_puts ((char *)sc->name ().ch_C(), port);
804 don't try to print properties, that is too much hassle.
806 scm_puts (" >", port);
811 Score_element::do_derived_mark ()
818 ly_set_elt_property (SCM elt, SCM sym, SCM val)
820 Score_element * sc = unsmob_element (elt);
822 if (!gh_symbol_p (sym))
824 error ("Not a symbol");
825 ly_display_scm (sym);
826 return SCM_UNSPECIFIED;
831 sc->set_elt_property (sym, val);
835 error ("Not a score element");
836 ly_display_scm (elt);
839 return SCM_UNSPECIFIED;
844 ly_get_elt_property (SCM elt, SCM sym)
846 Score_element * sc = unsmob_element (elt);
850 return sc->get_elt_property (sym);
854 error ("Not a score element");
855 ly_display_scm (elt);
857 return SCM_UNSPECIFIED;
862 Score_element::discretionary_processing()
869 spanner_get_bound (SCM slur, SCM dir)
871 return dynamic_cast<Spanner*> (unsmob_element (slur))->get_bound (to_dir (dir))->self_scm ();
876 static SCM interfaces_sym;
880 interfaces_sym = scm_permanent_object (ly_symbol2scm ("interfaces"));
882 scm_make_gsubr ("ly-get-elt-property", 2, 0, 0, (Scheme_function_unknown)ly_get_elt_property);
883 scm_make_gsubr ("ly-set-elt-property", 3, 0, 0, (Scheme_function_unknown)ly_set_elt_property);
884 scm_make_gsubr ("ly-get-spanner-bound", 2 , 0, 0, (Scheme_function_unknown) spanner_get_bound);
888 Score_element::has_interface (SCM k)
890 SCM ifs = get_elt_property (interfaces_sym);
892 return scm_memq (k, ifs) != SCM_BOOL_F;
896 Score_element::set_interface (SCM k)
898 if (has_interface (k))
902 set_elt_property (interfaces_sym,
903 gh_cons (k, get_elt_property (interfaces_sym)));
908 ADD_SCM_INIT_FUNC(scoreelt, init_functions);