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;
88 SCM meta = get_elt_property ("meta");
89 SCM ifs = scm_assoc (ly_symbol2scm ("interfaces"), meta);
91 set_elt_property ("interfaces",gh_cdr (ifs));
95 Score_element::Score_element (Score_element const&s)
96 : dim_cache_ (s.dim_cache_)
98 original_l_ =(Score_element*) &s;
99 immutable_property_alist_ = s.immutable_property_alist_;
100 mutable_property_alist_ = SCM_EOL;
102 status_i_ = s.status_i_;
103 lookup_l_ = s.lookup_l_;
104 pscore_l_ = s.pscore_l_;
109 Score_element::~Score_element()
112 do nothing scm-ish and no unprotecting here.
118 Score_element::get_elt_property (const char *nm) const
120 SCM sym = ly_symbol2scm (nm);
121 return get_elt_property (sym);
125 Score_element::get_elt_property (SCM sym) const
127 SCM s = scm_sloppy_assq(sym, mutable_property_alist_);
131 s = scm_sloppy_assq (sym, immutable_property_alist_);
132 return (s == SCM_BOOL_F) ? SCM_EOL : gh_cdr (s);
136 Remove the value associated with KEY, and return it. The result is
137 that a next call will yield SCM_UNDEFINED (and not the underlying
141 Score_element::remove_elt_property (const char* key)
143 SCM val = get_elt_property (key);
145 set_elt_property (key, SCM_EOL);
150 Score_element::set_elt_property (const char* k, SCM v)
152 SCM s = ly_symbol2scm (k);
153 set_elt_property (s, v);
157 Puts the k, v in the immutable_property_alist_, which is convenient for
158 storing variables that are needed during the breaking process. (eg.
159 Line_of_score::rank : int )
162 Score_element::set_immutable_elt_property (const char*k, SCM v)
164 SCM s = ly_symbol2scm (k);
165 set_immutable_elt_property (s, v);
169 Score_element::set_immutable_elt_property (SCM s, SCM v)
171 immutable_property_alist_ = gh_cons (gh_cons (s,v), mutable_property_alist_);
172 mutable_property_alist_ = scm_assq_remove_x (mutable_property_alist_, s);
175 Score_element::set_elt_property (SCM s, SCM v)
177 mutable_property_alist_ = scm_assq_set_x (mutable_property_alist_, s, v);
181 MAKE_SCHEME_CALLBACK(Score_element,molecule_extent,2);
183 Score_element::molecule_extent (SCM element_smob, SCM scm_axis)
185 Score_element *s = unsmob_element (element_smob);
186 Axis a = (Axis) gh_scm2int (scm_axis);
188 Molecule m = s->get_molecule ();
189 return ly_interval2scm ( m.extent(a));
192 MAKE_SCHEME_CALLBACK(Score_element,preset_extent,2);
195 Score_element::preset_extent (SCM element_smob, SCM scm_axis)
197 Score_element *s = unsmob_element (element_smob);
198 Axis a = (Axis) gh_scm2int (scm_axis);
200 SCM ext = s->get_elt_property ((a == X_AXIS)
206 Real l = gh_scm2double (gh_car (ext));
207 Real r = gh_scm2double (gh_cdr (ext));
208 l *= s->paper_l ()->get_var ("staffspace");
209 r *= s->paper_l ()->get_var ("staffspace");
210 return ly_interval2scm (Interval (l, r));
213 return ly_interval2scm ( Interval ());
219 Score_element::paper_l () const
221 return pscore_l_ ? pscore_l_->paper_l_ : 0;
225 Score_element::lookup_l () const
228 URG junkthis, caching is clumsy.
232 Score_element * urg = (Score_element*)this;
233 SCM sz = urg->remove_elt_property ("font-size");
234 int i = (gh_number_p (sz))
238 urg->lookup_l_ = (Lookup*)pscore_l_->paper_l_->lookup_l (i);
244 Score_element::calculate_dependencies (int final, int busy, SCM funcname)
246 assert (status_i_ >=0);
248 if (status_i_ >= final)
251 if (status_i_== busy)
253 programming_error ("Element is busy, come back later");
259 for (SCM d= get_elt_property ("dependencies"); gh_pair_p (d); d = gh_cdr (d))
261 unsmob_element (gh_car (d))
262 ->calculate_dependencies (final, busy, funcname);
266 String s = ly_symbol2string (funcname);
267 SCM proc = get_elt_property (s.ch_C());
268 if (gh_procedure_p (proc))
269 gh_call1 (proc, this->self_scm ());
276 Score_element::get_molecule () const
278 SCM proc = get_elt_property ("molecule-callback");
281 if (gh_procedure_p (proc))
282 mol = gh_apply (proc, gh_list (this->self_scm (), SCM_UNDEFINED));
285 SCM origin =get_elt_property ("origin");
286 if (!unsmob_input (origin))
287 origin =ly_symbol2scm ("no-origin");
292 mol = gh_cons (gh_list (origin, gh_car (mol), SCM_UNDEFINED), gh_cdr (mol));
296 Molecule m (create_molecule (mol));
299 This is almost the same as setting molecule-callback to #f, but
300 this retains the dimensions of this element, which means that you
301 can erase elements individually. */
302 if (to_boolean (get_elt_property ("transparent")))
303 m = Molecule (m.extent_box (), SCM_EOL);
315 Score_element::do_break_processing()
323 MAKE_SCHEME_CALLBACK(Score_element,brew_molecule,1)
329 Score_element::brew_molecule (SCM smob)
331 Score_element * sc = unsmob_element (smob);
332 SCM glyph = sc->get_elt_property ("glyph");
333 if (gh_string_p (glyph))
335 return sc->lookup_l ()->afm_find (String (ly_scm2string (glyph))).create_scheme ();
345 Score_element::line_l() const
351 Score_element::add_dependency (Score_element*e)
355 Pointer_group_interface ::add_element (this, "dependencies",e);
359 programming_error ("Null dependency added");
366 Do break substitution in S, using CRITERION. Return new value.
367 CRITERION is either a SMOB pointer to the desired line, or a number
368 representing the break direction. Do not modify SRC.
371 Score_element::handle_broken_smobs (SCM src, SCM criterion)
374 Score_element *sc = unsmob_element (src);
377 if (gh_number_p (criterion))
379 Item * i = dynamic_cast<Item*> (sc);
380 Direction d = to_dir (criterion);
381 if (i && i->break_status_dir () != d)
383 Item *br = i->find_prebroken_piece (d);
384 return (br) ? br->self_scm () : SCM_UNDEFINED;
390 = dynamic_cast<Line_of_score*> (unsmob_element (criterion));
391 if (sc->line_l () != line)
393 sc = sc->find_broken_piece (line);
397 /* now: !sc || (sc && sc->line_l () == line) */
399 return SCM_UNDEFINED;
401 /* now: sc && sc->line_l () == line */
403 || (sc->common_refpoint (line, X_AXIS)
404 && sc->common_refpoint (line, Y_AXIS)))
406 return sc->self_scm ();
408 return SCM_UNDEFINED;
411 else if (gh_pair_p (src))
413 SCM oldcar =gh_car (src);
415 UGH! breaks on circular lists.
417 SCM newcar = handle_broken_smobs (oldcar, criterion);
418 SCM oldcdr = gh_cdr (src);
420 if (newcar == SCM_UNDEFINED
421 && (gh_pair_p (oldcdr) || oldcdr == SCM_EOL))
424 This is tail-recursion, ie.
426 return handle_broken_smobs (cdr, criterion);
428 We don't want to rely on the compiler to do this. Without
429 tail-recursion, this easily crashes with a stack overflow. */
434 SCM newcdr = handle_broken_smobs (oldcdr, criterion);
435 return gh_cons (newcar, newcdr);
444 Score_element::handle_broken_dependencies()
446 Spanner * s= dynamic_cast<Spanner*> (this);
447 if (original_l_ && s)
452 for (int i = 0; i< s->broken_into_l_arr_ .size (); i++)
454 Score_element * sc = s->broken_into_l_arr_[i];
455 Line_of_score * l = sc->line_l ();
456 sc->mutable_property_alist_ =
457 handle_broken_smobs (mutable_property_alist_,
458 l ? l->self_scm () : SCM_UNDEFINED);
463 Line_of_score *line = line_l();
465 if (line && common_refpoint (line, X_AXIS) && common_refpoint (line, Y_AXIS))
467 mutable_property_alist_
468 = handle_broken_smobs (mutable_property_alist_,
469 line ? line->self_scm () : SCM_UNDEFINED);
471 else if (dynamic_cast <Line_of_score*> (this))
473 mutable_property_alist_ = handle_broken_smobs (mutable_property_alist_,
479 This element is `invalid'; it has been removed from all
480 dependencies, so let's junk the element itself.
482 do not do this for Line_of_score, since that would remove
483 references to the originals of score-elts, which get then GC'd
491 Note that we still want references to this element to be
492 rearranged, and not silently thrown away, so we keep pointers
493 like {broken_into_{drul,array}, original}
496 Score_element::suicide ()
498 mutable_property_alist_ = SCM_EOL;
499 immutable_property_alist_ = SCM_EOL;
501 set_extent_callback (SCM_EOL, Y_AXIS);
502 set_extent_callback (SCM_EOL, X_AXIS);
504 for (int a= X_AXIS; a <= Y_AXIS; a++)
506 dim_cache_[a].offset_callbacks_ = SCM_EOL;
507 dim_cache_[a].offsets_left_ = 0;
512 Score_element::handle_prebroken_dependencies()
517 Score_element::find_broken_piece (Line_of_score*) const
523 Score_element::translate_axis (Real y, Axis a)
525 if (isinf (y) || isnan (y))
526 programming_error (_(INFINITY_MSG));
529 dim_cache_[a].offset_ += y;
534 Score_element::relative_coordinate (Score_element const*refp, Axis a) const
540 We catch PARENT_L_ == nil case with this, but we crash if we did
541 not ask for the absolute coordinate (ie. REFP == nil.)
544 if (refp == dim_cache_[a].parent_l_)
545 return get_offset (a);
547 return get_offset (a) + dim_cache_[a].parent_l_->relative_coordinate (refp, a);
551 Score_element::get_offset (Axis a) const
553 Score_element *me = (Score_element*) this;
554 while (dim_cache_[a].offsets_left_)
556 int l = --me->dim_cache_[a].offsets_left_;
557 SCM cb = scm_list_ref (dim_cache_[a].offset_callbacks_, gh_int2scm (l));
558 SCM retval = gh_call2 (cb, self_scm (), gh_int2scm (a));
560 Real r = gh_scm2double (retval);
561 if (isinf (r) || isnan (r))
563 programming_error (INFINITY_MSG);
566 me->dim_cache_[a].offset_ +=r;
568 return dim_cache_[a].offset_;
572 MAKE_SCHEME_CALLBACK(Score_element,point_dimension_callback,2);
574 Score_element::point_dimension_callback (SCM , SCM )
576 return ly_interval2scm ( Interval (0,0));
580 Score_element::empty_b (Axis a)const
582 return ! (gh_pair_p (dim_cache_[a].dimension_ ) ||
583 gh_procedure_p (dim_cache_[a].dimension_ ));
589 Score_element *refpoint
594 Score_element::extent (Score_element * refp, Axis a) const
596 Real x = relative_coordinate (refp, a);
599 Dimension_cache * d = (Dimension_cache *)&dim_cache_[a];
601 if (gh_pair_p (d->dimension_))
603 else if (gh_procedure_p (d->dimension_))
606 FIXME: add doco on types, and should typecheck maybe?
608 d->dimension_= gh_call2 (d->dimension_, self_scm(), gh_int2scm (a));
613 if (!gh_pair_p (d->dimension_))
616 ext = ly_scm2interval (d->dimension_);
618 SCM extra = get_elt_property (a == X_AXIS
625 Real s = paper_l ()->get_var ("staffspace");
626 if (gh_pair_p (extra))
628 ext[BIGGER] += s * gh_scm2double (gh_cdr (extra));
629 ext[SMALLER] += s * gh_scm2double (gh_car (extra));
632 extra = get_elt_property (a == X_AXIS
634 : "minimum-extent-Y");
635 if (gh_pair_p (extra))
637 ext.unite (Interval (s * gh_scm2double (gh_car (extra)),
638 s * gh_scm2double (gh_cdr (extra))));
648 Score_element::parent_l (Axis a) const
650 return dim_cache_[a].parent_l_;
654 Score_element::common_refpoint (Score_element const* s, Axis a) const
657 I don't like the quadratic aspect of this code, but I see no other
658 way. The largest chain of parents might be 10 high or so, so
659 it shouldn't be a real issue. */
660 for (Score_element const *c = this; c; c = c->dim_cache_[a].parent_l_)
661 for (Score_element const * d = s; d; d = d->dim_cache_[a].parent_l_)
663 return (Score_element*)d;
670 Score_element::common_refpoint (SCM elist, Axis a) const
672 Score_element * common = (Score_element*) this;
673 for (; gh_pair_p (elist); elist = gh_cdr (elist))
675 Score_element * s = unsmob_element (gh_car (elist));
677 common = common->common_refpoint (s, a);
684 Score_element::name () const
686 SCM meta = get_elt_property ("meta");
687 SCM nm = scm_assoc (ly_symbol2scm ("name"), meta);
688 nm = (gh_pair_p (nm)) ? gh_cdr (nm) : SCM_EOL;
689 return gh_string_p (nm) ?ly_scm2string (nm) : classname (this);
693 Score_element::add_offset_callback (SCM cb, Axis a)
695 if (!has_offset_callback_b (cb, a))
697 dim_cache_[a].offset_callbacks_ = gh_cons (cb, dim_cache_[a].offset_callbacks_ );
698 dim_cache_[a].offsets_left_ ++;
703 Score_element::has_extent_callback_b (SCM cb, Axis a)const
705 return scm_equal_p (cb, dim_cache_[a].dimension_);
710 Score_element::has_extent_callback_b (Axis a) const
712 return gh_procedure_p (dim_cache_[a].dimension_);
716 Score_element::has_offset_callback_b (SCM cb, Axis a)const
718 return scm_memq (cb, dim_cache_[a].offset_callbacks_) != SCM_BOOL_F;
722 Score_element::set_extent_callback (SCM dc, Axis a)
724 dim_cache_[a].dimension_ =dc;
728 Score_element::set_parent (Score_element *g, Axis a)
730 dim_cache_[a].parent_l_ = g;
733 MAKE_SCHEME_CALLBACK(Score_element,fixup_refpoint,1);
735 Score_element::fixup_refpoint (SCM smob)
737 Score_element *me = unsmob_element (smob);
738 for (int a = X_AXIS; a < NO_AXES; a ++)
741 Score_element * parent = me->parent_l (ax);
746 if (parent->line_l () != me->line_l () && me->line_l ())
748 Score_element * newparent = parent->find_broken_piece (me->line_l ());
749 me->set_parent (newparent, ax);
752 if (Item * i = dynamic_cast<Item*> (me))
754 Item *parenti = dynamic_cast<Item*> (parent);
758 Direction my_dir = i->break_status_dir () ;
759 if (my_dir!= parenti->break_status_dir())
761 Item *newparent = parenti->find_prebroken_piece (my_dir);
762 me->set_parent (newparent, ax);
772 /****************************************************
774 ****************************************************/
777 IMPLEMENT_UNSMOB(Score_element, element);
778 IMPLEMENT_SMOBS(Score_element);
779 IMPLEMENT_DEFAULT_EQUAL_P(Score_element);
782 Score_element::mark_smob (SCM ses)
784 Score_element * s = (Score_element*) SCM_CELL_WORD_1(ses);
785 scm_gc_mark (s->immutable_property_alist_);
786 scm_gc_mark (s->mutable_property_alist_);
788 for (int a =0 ; a < 2; a++)
790 scm_gc_mark (s->dim_cache_[a].offset_callbacks_);
791 scm_gc_mark (s->dim_cache_[a].dimension_);
794 if (s->parent_l (Y_AXIS))
795 scm_gc_mark (s->parent_l (Y_AXIS)->self_scm ());
796 if (s->parent_l (X_AXIS))
797 scm_gc_mark (s->parent_l (X_AXIS)->self_scm ());
800 scm_gc_mark (s->original_l_->self_scm ());
801 return s->do_derived_mark ();
805 Score_element::print_smob (SCM s, SCM port, scm_print_state *)
807 Score_element *sc = (Score_element *) gh_cdr (s);
809 scm_puts ("#<Score_element ", port);
810 scm_puts ((char *)sc->name ().ch_C(), port);
813 don't try to print properties, that is too much hassle.
815 scm_puts (" >", port);
820 Score_element::do_derived_mark ()
827 ly_set_elt_property (SCM elt, SCM sym, SCM val)
829 Score_element * sc = unsmob_element (elt);
831 if (!gh_symbol_p (sym))
833 error ("Not a symbol");
834 ly_display_scm (sym);
835 return SCM_UNSPECIFIED;
840 sc->set_elt_property (sym, val);
844 error ("Not a score element");
845 ly_display_scm (elt);
848 return SCM_UNSPECIFIED;
853 ly_get_elt_property (SCM elt, SCM sym)
855 Score_element * sc = unsmob_element (elt);
859 return sc->get_elt_property (sym);
863 error ("Not a score element");
864 ly_display_scm (elt);
866 return SCM_UNSPECIFIED;
871 Score_element::discretionary_processing()
878 spanner_get_bound (SCM slur, SCM dir)
880 return dynamic_cast<Spanner*> (unsmob_element (slur))->get_bound (to_dir (dir))->self_scm ();
885 static SCM interfaces_sym;
889 interfaces_sym = scm_permanent_object (ly_symbol2scm ("interfaces"));
891 scm_make_gsubr ("ly-get-elt-property", 2, 0, 0, (Scheme_function_unknown)ly_get_elt_property);
892 scm_make_gsubr ("ly-set-elt-property", 3, 0, 0, (Scheme_function_unknown)ly_set_elt_property);
893 scm_make_gsubr ("ly-get-spanner-bound", 2 , 0, 0, (Scheme_function_unknown) spanner_get_bound);
897 Score_element::has_interface (SCM k)
899 SCM ifs = get_elt_property (interfaces_sym);
901 return scm_memq (k, ifs) != SCM_BOOL_F;
905 Score_element::set_interface (SCM k)
907 if (has_interface (k))
911 set_elt_property (interfaces_sym,
912 gh_cons (k, get_elt_property (interfaces_sym)));
917 ADD_SCM_INIT_FUNC(scoreelt, init_functions);
918 IMPLEMENT_TYPE_P(Score_element, "ly-element?");