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"
33 remove dynamic_cast<Spanner,Item> and put this code into respective
37 Score_element::Score_element()
39 dim_cache_[X_AXIS] = new Dimension_cache;
40 dim_cache_[Y_AXIS] = new Dimension_cache;
41 dim_cache_[X_AXIS]->elt_l_ = dim_cache_[Y_AXIS]->elt_l_ = this;
46 dim_cache_[X_AXIS]->set_extent_callback (molecule_extent);
47 dim_cache_[Y_AXIS]->set_extent_callback (molecule_extent);
54 element_property_alist_ = SCM_EOL;
59 set_elt_property ("dependencies", SCM_EOL);
60 set_elt_property ("interfaces", SCM_EOL);
64 Score_element::Score_element (Score_element const&s)
66 dim_cache_[X_AXIS] = new Dimension_cache (*s.dim_cache_[X_AXIS]);
67 dim_cache_[Y_AXIS] = new Dimension_cache (*s.dim_cache_[Y_AXIS]);
68 dim_cache_[X_AXIS]->elt_l_ = dim_cache_[Y_AXIS]->elt_l_ = this;
72 original_l_ =(Score_element*) &s;
73 element_property_alist_ = SCM_EOL; // onstack;
75 status_i_ = s.status_i_;
76 lookup_l_ = s.lookup_l_;
77 pscore_l_ = s.pscore_l_;
82 Score_element::~Score_element()
84 assert (status_i_ >=0);
87 delete dim_cache_[X_AXIS];
88 delete dim_cache_[Y_AXIS];
91 // should also have one that takes SCM arg.
93 Score_element::get_elt_property (String nm) const
95 SCM sym = ly_symbol2scm (nm.ch_C());
96 SCM s = scm_assq(sym, element_property_alist_);
103 SCM sym2 = ly_symbol2scm ((name () + ("::" + nm)).ch_C());
106 // should probably check for Type::sym as well.
107 Paper_def * p= pscore_l_->paper_l_;
108 if (p->default_properties_.try_retrieve (sym2, &val))
110 else if (p->default_properties_.try_retrieve (sym, &val))
114 return SCM_UNDEFINED;
118 Score_element::remove_elt_property (String key)
120 SCM s = get_elt_property (key);
121 SCM sym = ly_symbol2scm (key.ch_C());
122 element_property_alist_ = scm_assq_remove_x (element_property_alist_, sym);
130 Score_element::set_elt_property (String k, SCM v)
132 SCM s = ly_symbol2scm (k.ch_C( ));
133 element_property_alist_ = scm_assoc_set_x (element_property_alist_, s, v);
137 Score_element::molecule_extent (Dimension_cache const *c)
139 Score_element *s = dynamic_cast<Score_element*>(c->element_l());
140 Molecule m = s->do_brew_molecule();
141 return m.extent()[c->axis ()];
145 Score_element::preset_extent (Dimension_cache const *c)
147 Score_element *s = dynamic_cast<Score_element*>(c->element_l());
148 SCM ext = s->get_elt_property ((c->axis () == X_AXIS)
154 Real l = gh_scm2double (gh_car (ext));
155 Real r = gh_scm2double (gh_cdr (ext));
156 l *= s->paper_l ()->get_var ("staffspace");
157 r *= s->paper_l ()->get_var ("staffspace");
158 return Interval (l, r);
166 Score_element::print() const
169 DEBUG_OUT << classname(this) << "{\n";
171 if (flower_dstream && !flower_dstream->silent_b ("Score_element"))
172 ly_display_scm (element_property_alist_);
175 DEBUG_OUT << "Copy ";
183 Score_element::paper_l () const
185 return pscore_l_ ? pscore_l_->paper_l_ : 0;
189 Score_element::lookup_l () const
193 Score_element * urg = (Score_element*)this;
194 SCM sz = urg->remove_elt_property ("fontsize");
195 int i = (gh_number_p (sz))
199 urg->lookup_l_ = (Lookup*)pscore_l_->paper_l_->lookup_l (i);
205 Score_element::add_processing()
207 assert (status_i_ >=0);
216 Score_element::calculate_dependencies (int final, int busy,
217 Score_element_method_pointer funcptr)
219 assert (status_i_ >=0);
221 if (status_i_ >= final)
224 if (status_i_== busy)
226 programming_error ("Element is busy, come back later");
232 for (SCM d= get_elt_property ("dependencies"); gh_pair_p (d); d = gh_cdr (d))
234 unsmob_element (gh_car (d))
235 ->calculate_dependencies (final, busy, funcptr);
243 Score_element::output_processing ()
245 if (to_boolean (get_elt_property ("transparent")))
248 Molecule m (do_brew_molecule ());
249 Offset o (relative_coordinate (0, X_AXIS), relative_coordinate (0, Y_AXIS));
251 SCM s = get_elt_property ("extra-offset");
254 Real il = paper_l ()->get_var ("interline");
255 o[X_AXIS] += il * gh_scm2double (gh_car (s));
256 o[Y_AXIS] += il * gh_scm2double (gh_cdr (s));
259 line_l ()->output_molecule (m.expr_, o);
268 Score_element::do_break_processing()
273 Score_element::after_line_breaking ()
279 Score_element::before_line_breaking ()
284 Score_element::do_space_processing ()
289 Score_element::do_add_processing()
296 Score_element::do_brew_molecule() const
298 SCM glyph = get_elt_property ("glyph");
299 if (gh_string_p (glyph))
301 return lookup_l ()->afm_find (String (ly_scm2string (glyph)));
314 Score_element::line_l() const
320 Score_element::add_dependency (Score_element*e)
324 Group_interface gi (this, "dependencies");
328 programming_error ("Null dependency added");
335 Do break substitution in S, using CRITERION. Return new value.
336 CRITERION is either a SMOB pointer to the desired line, or a number
337 representing the break direction. Do not modify SRC.
340 Score_element::handle_broken_smobs (SCM src, SCM criterion)
345 Score_element *sc = unsmob_element (src);
348 if (criterion == SCM_UNDEFINED)
349 return SCM_UNDEFINED;
350 else if (gh_number_p (criterion))
352 Item * i = dynamic_cast<Item*> (sc);
353 Direction d = to_dir (criterion);
354 if (i && i->break_status_dir () != d)
356 Item *br = i->find_broken_piece (d);
357 return (br) ? br->self_scm_ : SCM_UNDEFINED;
363 Line_of_score * line = dynamic_cast<Line_of_score*> (unsmob_element ( criterion));
364 Line_of_score * dep_line = sc->line_l ();
365 if (dep_line != line)
367 Score_element * br = sc->find_broken_piece (line);
368 return (br) ? br->self_scm_ : SCM_UNDEFINED;
371 return SCM_UNDEFINED;
373 if (!sc->common_refpoint (line, X_AXIS)
374 || !sc->common_refpoint (line, Y_AXIS))
376 return SCM_UNDEFINED;
380 else if (gh_pair_p (src))
383 UGH! breaks on circular lists.
385 SCM car = handle_broken_smobs (gh_car (src), criterion);
386 SCM cdr = gh_cdr (src);
388 if (car == SCM_UNDEFINED
389 && (gh_pair_p (cdr) || cdr == SCM_EOL))
392 This is tail-recursion, ie.
394 return handle_broken_smobs (cdr, criterion);
396 We don't want to rely on the compiler to do this. */
401 return gh_cons (car, handle_broken_smobs (cdr, criterion));
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);
440 This element is `invalid'; it has been removed from all dependencies, so
441 let's junk the element itself.
444 element_property_alist_ = SCM_EOL;
445 set_extent_callback (0, Y_AXIS);
446 set_extent_callback (0, X_AXIS);
457 Score_element::handle_prebroken_dependencies()
459 if (Item*i =dynamic_cast<Item*> (this))
463 element_property_alist_
464 = handle_broken_smobs (original_l_->element_property_alist_,
465 gh_int2scm (i->break_status_dir ()));
471 Score_element::linked_b() const
477 Score_element::do_print () const
482 Score_element::find_broken_piece (Line_of_score*) const
488 Score_element::translate_axis (Real y, Axis a)
490 dim_cache_[a]->translate (y);
494 Score_element::relative_coordinate (Score_element const*e, Axis a) const
496 return dim_cache_[a]->relative_coordinate (e ? e->dim_cache_[a] : 0);
500 Score_element::common_refpoint (Score_element const* s, Axis a) const
502 Dimension_cache *dim = dim_cache_[a]->common_refpoint (s->dim_cache_[a]);
503 return dim ? dim->element_l () : 0;
507 Score_element::empty_b (Axis a)const
509 return !dim_cache_[a]->extent_callback_l_;
513 Score_element::extent (Axis a) const
515 Dimension_cache const * d = dim_cache_[a];
516 Interval ext = d->get_dim ();
521 SCM extra = get_elt_property (a == X_AXIS ? "extra-extent-X"
527 Real s = paper_l ()->get_var ("staffspace");
528 if (gh_pair_p (extra))
530 ext[BIGGER] += s * gh_scm2double (gh_cdr (extra));
531 ext[SMALLER] += s * gh_scm2double (gh_car (extra));
534 extra = get_elt_property (a == X_AXIS
536 : "minimum-extent-Y");
537 if (gh_pair_p (extra))
539 ext.unite (Interval (s * gh_scm2double (gh_car (extra)),
540 s * gh_scm2double (gh_cdr (extra))));
548 Score_element::parent_l (Axis a) const
550 Dimension_cache*d= dim_cache_[a]->parent_l_;
551 return d ? d->elt_l_ : 0;
555 Score_element::common_refpoint (Link_array<Score_element> gs, Axis a) const
557 Dimension_cache * common = dim_cache_[a];
558 for (int i=0; i < gs.size (); i++)
560 common = common->common_refpoint (gs[i]->dim_cache_[a]);
563 return common->element_l ();
567 Score_element::name () const
569 return classname (this);
573 Score_element::add_offset_callback (Offset_cache_callback cb, Axis a)
575 dim_cache_[a]->off_callbacks_.push (cb);
579 Score_element::has_offset_callback_b (Offset_cache_callback cb, Axis a)const
581 for (int i= dim_cache_[a]->off_callbacks_.size (); i--;)
583 if (dim_cache_[a]->off_callbacks_[i] == cb)
590 Score_element::set_extent_callback (Dim_cache_callback dc, Axis a)
592 dim_cache_[a]->extent_callback_l_ = dc ;
597 Score_element::set_parent (Score_element *g, Axis a)
599 dim_cache_[a]->parent_l_ = g ? g->dim_cache_[a]: 0;
603 Score_element::fixup_refpoint ()
605 for (int a = X_AXIS; a < NO_AXES; a ++)
608 Score_element * parent = parent_l (ax);
613 if (parent->line_l () != line_l () && line_l ())
615 Score_element * newparent = parent->find_broken_piece (line_l ());
616 set_parent (newparent, ax);
619 if (Item * i = dynamic_cast<Item*> (this))
621 Item *parenti = dynamic_cast<Item*> (parent);
625 Direction my_dir = i->break_status_dir () ;
626 if (my_dir!= parenti->break_status_dir())
628 Item *newparent = parenti->find_broken_piece (my_dir);
629 set_parent (newparent, ax);
638 /****************************************************
640 ****************************************************/
643 #include "ly-smobs.icc"
645 IMPLEMENT_UNSMOB(Score_element, element);
646 IMPLEMENT_SMOBS(Score_element);
648 Score_element::mark_smob (SCM ses)
650 Score_element * s = SMOB_TO_TYPE (Score_element, ses);
651 if (s->self_scm_ != ses)
653 programming_error ("SMOB marking gone awry");
656 return s->element_property_alist_;
660 Score_element::print_smob (SCM s, SCM port, scm_print_state *)
662 Score_element *sc = (Score_element *) gh_cdr (s);
664 scm_puts ("#<Score_element ", port);
665 scm_puts ((char *)sc->name (), port);
668 don't try to print properties, that is too much hassle.
670 scm_puts (" >", port);
675 Score_element::do_smobify_self ()
680 Score_element::equal_p (SCM a, SCM b)
682 return gh_cdr(a) == gh_cdr(b) ? SCM_BOOL_T : SCM_BOOL_F;
687 Score_element::ly_set_elt_property (SCM elt, SCM sym, SCM val)
689 Score_element * sc = unsmob_element (elt);
691 if (!gh_symbol_p (sym))
693 error ("Not a symbol");
694 ly_display_scm (sym);
695 return SCM_UNDEFINED;
700 sc->element_property_alist_ = scm_assoc_set_x (sc->element_property_alist_, sym, val);
704 error ("Not a score element");
705 ly_display_scm (elt);
708 return SCM_UNDEFINED;
713 Score_element::ly_get_elt_property (SCM elt, SCM sym)
715 Score_element * sc = unsmob_element (elt);
719 SCM s = scm_assq(sym, sc->element_property_alist_);
724 return SCM_UNDEFINED;
728 error ("Not a score element");
729 ly_display_scm (elt);
731 return SCM_UNDEFINED;
738 scm_make_gsubr ("ly-get-elt-property", 2, 0, 0, (SCM(*)(...))Score_element::ly_get_elt_property);
739 scm_make_gsubr ("ly-set-elt-property", 3, 0, 0, (SCM(*)(...))Score_element::ly_set_elt_property);
742 ADD_SCM_INIT_FUNC(scoreelt, init_functions);
745 Score_element::do_breakable_col_processing ()