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>
12 #include "group-interface.hh"
14 #include "paper-score.hh"
15 #include "paper-def.hh"
17 #include "molecule.hh"
18 #include "score-element.hh"
21 #include "line-of-score.hh"
23 #include "paper-column.hh"
24 #include "molecule.hh"
26 #include "paper-outputter.hh"
27 #include "dimension-cache.hh"
28 #include "side-position-interface.hh"
34 remove dynamic_cast<Spanner,Item> and put this code into respective
38 Score_element::Score_element()
40 dim_cache_[X_AXIS] = new Dimension_cache;
41 dim_cache_[Y_AXIS] = new Dimension_cache;
42 dim_cache_[X_AXIS]->elt_l_ = dim_cache_[Y_AXIS]->elt_l_ = this;
47 dim_cache_[X_AXIS]->set_extent_callback (molecule_extent);
48 dim_cache_[Y_AXIS]->set_extent_callback (molecule_extent);
55 element_property_alist_ = SCM_EOL;
60 set_elt_property ("dependencies", SCM_EOL);
61 set_elt_property ("interfaces", SCM_EOL);
65 Score_element::Score_element (Score_element const&s)
67 dim_cache_[X_AXIS] = new Dimension_cache (*s.dim_cache_[X_AXIS]);
68 dim_cache_[Y_AXIS] = new Dimension_cache (*s.dim_cache_[Y_AXIS]);
69 dim_cache_[X_AXIS]->elt_l_ = dim_cache_[Y_AXIS]->elt_l_ = this;
73 original_l_ =(Score_element*) &s;
74 element_property_alist_ = SCM_EOL; // onstack;
76 status_i_ = s.status_i_;
77 lookup_l_ = s.lookup_l_;
78 pscore_l_ = s.pscore_l_;
83 Score_element::~Score_element()
85 assert (status_i_ >=0);
88 delete dim_cache_[X_AXIS];
89 delete dim_cache_[Y_AXIS];
92 // should also have one that takes SCM arg.
94 Score_element::get_elt_property (String nm) const
96 SCM sym = ly_symbol2scm (nm.ch_C());
97 SCM s = scm_assq(sym, element_property_alist_);
104 SCM sym2 = ly_symbol2scm ((name () + ("::" + nm)).ch_C());
107 // should probably check for Type::sym as well.
108 Paper_def * p= pscore_l_->paper_l_;
109 if (p->default_properties_.try_retrieve (sym2, &val))
111 else if (p->default_properties_.try_retrieve (sym, &val))
115 return SCM_UNDEFINED;
119 Score_element::remove_elt_property (String key)
121 SCM s = get_elt_property (key);
122 SCM sym = ly_symbol2scm (key.ch_C());
123 element_property_alist_ = scm_assq_remove_x (element_property_alist_, sym);
131 Score_element::set_elt_property (String k, SCM v)
133 SCM s = ly_symbol2scm (k.ch_C( ));
134 element_property_alist_ = scm_assoc_set_x (element_property_alist_, s, v);
138 Score_element::molecule_extent (Dimension_cache const *c)
140 Score_element *s = dynamic_cast<Score_element*>(c->element_l());
141 Molecule m = s->do_brew_molecule();
142 return m.extent()[c->axis ()];
146 Score_element::preset_extent (Dimension_cache const *c)
148 Score_element *s = dynamic_cast<Score_element*>(c->element_l());
149 SCM ext = s->get_elt_property ((c->axis () == X_AXIS)
155 Real l = gh_scm2double (gh_car (ext));
156 Real r = gh_scm2double (gh_cdr (ext));
157 l *= s->paper_l ()->get_var ("staffspace");
158 r *= s->paper_l ()->get_var ("staffspace");
159 return Interval (l, r);
167 Score_element::print() const
170 DEBUG_OUT << classname(this) << "{\n";
172 if (flower_dstream && !flower_dstream->silent_b ("Score_element"))
173 ly_display_scm (element_property_alist_);
176 DEBUG_OUT << "Copy ";
184 Score_element::paper_l () const
186 return pscore_l_ ? pscore_l_->paper_l_ : 0;
190 Score_element::lookup_l () const
194 Score_element * urg = (Score_element*)this;
195 SCM sz = urg->remove_elt_property ("fontsize");
196 int i = (gh_number_p (sz))
200 urg->lookup_l_ = (Lookup*)pscore_l_->paper_l_->lookup_l (i);
206 Score_element::add_processing()
208 assert (status_i_ >=0);
217 Score_element::calculate_dependencies (int final, int busy,
218 Score_element_method_pointer funcptr)
220 assert (status_i_ >=0);
222 if (status_i_ >= final)
225 if (status_i_== busy)
227 programming_error ("Element is busy, come back later");
233 for (SCM d= get_elt_property ("dependencies"); gh_pair_p (d); d = gh_cdr (d))
235 unsmob_element (gh_car (d))
236 ->calculate_dependencies (final, busy, funcptr);
244 Score_element::output_processing ()
246 if (to_boolean (get_elt_property ("transparent")))
249 Molecule m (do_brew_molecule ());
250 Offset o (relative_coordinate (0, X_AXIS), relative_coordinate (0, Y_AXIS));
252 SCM s = get_elt_property ("extra-offset");
255 Real il = paper_l ()->get_var ("interline");
256 o[X_AXIS] += il * gh_scm2double (gh_car (s));
257 o[Y_AXIS] += il * gh_scm2double (gh_cdr (s));
260 line_l ()->output_molecule (m.expr_, o);
269 Score_element::do_break_processing()
274 Score_element::after_line_breaking ()
280 Score_element::before_line_breaking ()
285 Score_element::do_space_processing ()
290 Score_element::do_add_processing()
299 Score_element::do_brew_molecule() const
301 SCM glyph = get_elt_property ("glyph");
302 if (gh_string_p (glyph))
304 return lookup_l ()->afm_find (String (ly_scm2string (glyph)));
317 Score_element::line_l() const
323 Score_element::add_dependency (Score_element*e)
327 Group_interface gi (this, "dependencies");
331 programming_error ("Null dependency added");
338 Do break substitution in S, using CRITERION. Return new value.
339 CRITERION is either a SMOB pointer to the desired line, or a number
340 representing the break direction. Do not modify SRC.
343 Score_element::handle_broken_smobs (SCM src, SCM criterion)
346 Score_element *sc = unsmob_element (src);
349 if (gh_number_p (criterion))
351 Item * i = dynamic_cast<Item*> (sc);
352 Direction d = to_dir (criterion);
353 if (i && i->break_status_dir () != d)
355 Item *br = i->find_prebroken_piece (d);
356 return (br) ? br->self_scm_ : SCM_UNDEFINED;
361 Line_of_score * line = dynamic_cast<Line_of_score*> (unsmob_element ( criterion));
362 Line_of_score * dep_line = sc->line_l ();
363 if (dep_line != line)
365 Score_element * br = sc->find_broken_piece (line);
366 return (br) ? br->self_scm_ : SCM_UNDEFINED;
370 || !sc->common_refpoint (line, X_AXIS)
371 || !sc->common_refpoint (line, Y_AXIS)))
373 return SCM_UNDEFINED;
377 else if (gh_pair_p (src))
379 SCM oldcar =gh_car (src);
381 UGH! breaks on circular lists.
383 SCM newcar = handle_broken_smobs (oldcar, criterion);
384 SCM oldcdr = gh_cdr (src);
386 if (newcar == SCM_UNDEFINED
387 && (gh_pair_p (oldcdr) || oldcdr == SCM_EOL))
390 This is tail-recursion, ie.
392 return handle_broken_smobs (cdr, criterion);
394 We don't want to rely on the compiler to do this. Without
395 tail-recursion, this easily crashes with a stack overflow. */
400 SCM newcdr = handle_broken_smobs (oldcdr, criterion);
401 return gh_cons (newcar, newcdr);
410 Score_element::handle_broken_dependencies()
412 Spanner * s= dynamic_cast<Spanner*> (this);
413 if (original_l_ && s)
418 for (int i = 0; i< s->broken_into_l_arr_ .size (); i++)
420 Score_element * sc = s->broken_into_l_arr_[i];
421 Line_of_score * l = sc->line_l ();
422 s->broken_into_l_arr_[i]->element_property_alist_ =
423 handle_broken_smobs (element_property_alist_,
424 l ? l->self_scm_ : SCM_UNDEFINED);
429 Line_of_score *line = line_l();
431 if (line && common_refpoint (line, X_AXIS) && common_refpoint (line, Y_AXIS))
433 element_property_alist_
434 = handle_broken_smobs (element_property_alist_,
435 line ? line->self_scm_ : SCM_UNDEFINED);
437 else if (!dynamic_cast <Line_of_score*> (this))
440 This element is `invalid'; it has been removed from all
441 dependencies, so let's junk the element itself.
443 do not do this for Line_of_score , since that would free
444 up originals of score-elts (a bad thing.)
447 element_property_alist_ = SCM_EOL;
448 set_extent_callback (0, Y_AXIS);
449 set_extent_callback (0, X_AXIS);
454 Score_element::handle_prebroken_dependencies()
460 Score_element::linked_b() const
466 Score_element::do_print () const
471 Score_element::find_broken_piece (Line_of_score*) const
477 Score_element::translate_axis (Real y, Axis a)
479 dim_cache_[a]->translate (y);
483 Score_element::relative_coordinate (Score_element const*e, Axis a) const
485 return dim_cache_[a]->relative_coordinate (e ? e->dim_cache_[a] : 0);
489 Score_element::common_refpoint (Score_element const* s, Axis a) const
491 Dimension_cache *dim = dim_cache_[a]->common_refpoint (s->dim_cache_[a]);
492 return dim ? dim->element_l () : 0;
496 Score_element::empty_b (Axis a)const
498 return !dim_cache_[a]->extent_callback_l_;
502 Score_element::extent (Axis a) const
504 Dimension_cache const * d = dim_cache_[a];
505 Interval ext = d->get_dim ();
510 SCM extra = get_elt_property (a == X_AXIS ? "extra-extent-X"
516 Real s = paper_l ()->get_var ("staffspace");
517 if (gh_pair_p (extra))
519 ext[BIGGER] += s * gh_scm2double (gh_cdr (extra));
520 ext[SMALLER] += s * gh_scm2double (gh_car (extra));
523 extra = get_elt_property (a == X_AXIS
525 : "minimum-extent-Y");
526 if (gh_pair_p (extra))
528 ext.unite (Interval (s * gh_scm2double (gh_car (extra)),
529 s * gh_scm2double (gh_cdr (extra))));
537 Score_element::parent_l (Axis a) const
539 Dimension_cache*d= dim_cache_[a]->parent_l_;
540 return d ? d->elt_l_ : 0;
544 Score_element::common_refpoint (Link_array<Score_element> gs, Axis a) const
546 Dimension_cache * common = dim_cache_[a];
547 for (int i=0; i < gs.size (); i++)
549 common = common->common_refpoint (gs[i]->dim_cache_[a]);
552 return common->element_l ();
556 Score_element::name () const
558 return classname (this);
562 Score_element::add_offset_callback (Offset_cache_callback cb, Axis a)
564 dim_cache_[a]->off_callbacks_.push (cb);
568 Score_element::has_offset_callback_b (Offset_cache_callback cb, Axis a)const
570 for (int i= dim_cache_[a]->off_callbacks_.size (); i--;)
572 if (dim_cache_[a]->off_callbacks_[i] == cb)
579 Score_element::set_extent_callback (Dim_cache_callback dc, Axis a)
581 dim_cache_[a]->extent_callback_l_ = dc ;
586 Score_element::set_parent (Score_element *g, Axis a)
588 dim_cache_[a]->parent_l_ = g ? g->dim_cache_[a]: 0;
592 Score_element::fixup_refpoint ()
594 for (int a = X_AXIS; a < NO_AXES; a ++)
597 Score_element * parent = parent_l (ax);
602 if (parent->line_l () != line_l () && line_l ())
604 Score_element * newparent = parent->find_broken_piece (line_l ());
605 set_parent (newparent, ax);
608 if (Item * i = dynamic_cast<Item*> (this))
610 Item *parenti = dynamic_cast<Item*> (parent);
614 Direction my_dir = i->break_status_dir () ;
615 if (my_dir!= parenti->break_status_dir())
617 Item *newparent = parenti->find_prebroken_piece (my_dir);
618 set_parent (newparent, ax);
627 /****************************************************
629 ****************************************************/
631 #include "ly-smobs.icc"
633 IMPLEMENT_UNSMOB(Score_element, element);
634 IMPLEMENT_SMOBS(Score_element);
637 Score_element::mark_smob (SCM ses)
639 Score_element * s = SMOB_TO_TYPE (Score_element, ses);
640 if (s->self_scm_ != ses)
642 programming_error ("SMOB marking gone awry");
645 return s->element_property_alist_;
649 Score_element::print_smob (SCM s, SCM port, scm_print_state *)
651 Score_element *sc = (Score_element *) gh_cdr (s);
653 scm_puts ("#<Score_element ", port);
654 scm_puts ((char *)sc->name (), port);
657 don't try to print properties, that is too much hassle.
659 scm_puts (" >", port);
664 Score_element::do_smobify_self ()
669 Score_element::equal_p (SCM a, SCM b)
671 return gh_cdr(a) == gh_cdr(b) ? SCM_BOOL_T : SCM_BOOL_F;
676 Score_element::ly_set_elt_property (SCM elt, SCM sym, SCM val)
678 Score_element * sc = unsmob_element (elt);
680 if (!gh_symbol_p (sym))
682 error ("Not a symbol");
683 ly_display_scm (sym);
684 return SCM_UNDEFINED;
689 sc->element_property_alist_ = scm_assoc_set_x (sc->element_property_alist_, sym, val);
693 error ("Not a score element");
694 ly_display_scm (elt);
697 return SCM_UNDEFINED;
702 Score_element::ly_get_elt_property (SCM elt, SCM sym)
704 Score_element * sc = unsmob_element (elt);
708 SCM s = scm_assq(sym, sc->element_property_alist_);
713 return SCM_UNDEFINED;
717 error ("Not a score element");
718 ly_display_scm (elt);
720 return SCM_UNDEFINED;
727 scm_make_gsubr ("ly-get-elt-property", 2, 0, 0, (SCM(*)(...))Score_element::ly_get_elt_property);
728 scm_make_gsubr ("ly-set-elt-property", 3, 0, 0, (SCM(*)(...))Score_element::ly_set_elt_property);
731 ADD_SCM_INIT_FUNC(scoreelt, init_functions);
734 Score_element::do_breakable_col_processing ()