2 score-elem.cc -- implement Score_element
4 source file of the GNU LilyPond music typesetter
6 (c) 1997--1999 Han-Wen Nienhuys <hanwen@cs.uu.nl>
13 #include "paper-score.hh"
14 #include "paper-def.hh"
16 #include "molecule.hh"
17 #include "score-element.hh"
20 #include "line-of-score.hh"
22 #include "paper-column.hh"
23 #include "molecule.hh"
25 #include "paper-outputter.hh"
26 #include "dimension-cache.hh"
27 #include "staff-side.hh"
30 Score_element::Score_element()
33 dim_cache_[X_AXIS] = new Dimension_cache;
34 dim_cache_[Y_AXIS] = new Dimension_cache;
35 dim_cache_[X_AXIS]->elt_l_ = dim_cache_[Y_AXIS]->elt_l_ = this;
39 dim_cache_[X_AXIS]->set_callback (molecule_extent);
40 dim_cache_[Y_AXIS]->set_callback (molecule_extent);
47 element_property_alist_ = SCM_EOL;
52 SCM ly_deep_copy (SCM);
59 return gh_cons (ly_deep_copy (gh_car (l)), ly_deep_copy (gh_cdr (l)));
66 Score_element::Score_element (Score_element const&s)
68 dim_cache_[X_AXIS] = new Dimension_cache (*s.dim_cache_[X_AXIS]);
69 dim_cache_[Y_AXIS] = new Dimension_cache (*s.dim_cache_[Y_AXIS]);
70 dim_cache_[X_AXIS]->elt_l_ = dim_cache_[Y_AXIS]->elt_l_ = this;
74 original_l_ =(Score_element*) &s;
77 should protect because smobify_self () might trigger GC.
79 element_property_alist_ = scm_protect_object (ly_deep_copy (s.element_property_alist_));
80 dependency_arr_ = s.dependency_arr_;
82 status_i_ = s.status_i_;
83 lookup_l_ = s.lookup_l_;
84 pscore_l_ = s.pscore_l_;
89 Score_element::~Score_element()
92 assert (status_i_ >=0);
95 delete dim_cache_[X_AXIS];
96 delete dim_cache_[Y_AXIS];
100 Score_element::dependency (int i) const
102 return dependency_arr_ [i];
106 Score_element::dependency_size () const
108 return dependency_arr_.size ();
112 Score_element::get_real (String s) const
114 return gh_scm2double (get_elt_property (s));
118 Score_element::set_real (String s, Real r)
120 set_elt_property (s, gh_double2scm (r));
123 // should also have one that takes SCM arg.
125 Score_element::get_elt_property (String nm) const
127 SCM sym = ly_symbol2scm (nm.ch_C());
128 SCM s = scm_assq(sym, element_property_alist_);
135 SCM sym2 = ly_symbol2scm ((name () + ("::" + nm)).ch_C());
138 // should probably check for Type::sym as well.
139 Paper_def * p= pscore_l_->paper_l_;
140 if (p->default_properties_.try_retrieve (sym2, &val))
142 else if (p->default_properties_.try_retrieve (sym, &val))
146 return SCM_UNDEFINED;
150 Score_element::remove_elt_property (String key)
152 SCM s = get_elt_property (key);
153 SCM sym = ly_symbol2scm (key.ch_C());
154 element_property_alist_ = scm_assq_remove_x (element_property_alist_, sym);
162 Score_element::set_elt_property (String k, SCM v)
164 SCM s = ly_symbol2scm (k.ch_C( ));
165 element_property_alist_ = scm_assoc_set_x (element_property_alist_, s, v);
169 Score_element::molecule_extent(Dimension_cache const *c)
171 Score_element *s = dynamic_cast<Score_element*>(c->element_l());
172 Molecule*m = s->do_brew_molecule_p();
173 return m->extent()[c->axis ()];
178 Score_element::print() const
181 DEBUG_OUT << classname(this) << "{\n";
184 if (flower_dstream && !flower_dstream->silent_b ("Score_element"))
185 ly_display_scm (element_property_alist_);
187 DEBUG_OUT << "dependencies: " << dependency_size();
189 DEBUG_OUT << "Copy ";
197 Score_element::paper_l () const
199 return pscore_l_->paper_l_;
203 Score_element::lookup_l () const
207 Score_element * urg = (Score_element*)this;
208 SCM sz = urg->remove_elt_property ("fontsize");
209 int i = (sz != SCM_UNDEFINED)
213 urg->lookup_l_ = (Lookup*)pscore_l_->paper_l_->lookup_l (i);
219 Score_element::add_processing()
221 assert (status_i_ >=0);
230 if (get_elt_property ("self-alignment-X") != SCM_UNDEFINED
231 && !dim_cache_[X_AXIS]->off_callback_l_)
233 dim_cache_[X_AXIS]->off_callbacks_.push (Side_position_interface::self_alignment);
236 if (get_elt_property ("self-alignment-Y") != SCM_UNDEFINED
237 && !dim_cache_[X_AXIS]->off_callback_l_)
240 dim_cache_[Y_AXIS]->set_offset_callback (Side_position_interface::self_alignment);
248 Score_element::calculate_dependencies (int final, int busy,
249 Score_element_method_pointer funcptr)
251 assert (status_i_ >=0);
253 if (status_i_ >= final)
256 assert (status_i_!= busy);
259 for (int i=0; i < dependency_arr_.size(); i++)
260 dependency_arr_[i]->calculate_dependencies (final, busy, funcptr);
262 Link_array<Score_element> extra (get_extra_dependencies());
263 for (int i=0; i < extra.size(); i++)
264 extra[i]->calculate_dependencies (final, busy, funcptr);
271 Score_element::output_processing ()
273 if (get_elt_property ("transparent") != SCM_UNDEFINED)
276 // we're being silly here.
280 output_p_ = do_brew_molecule_p ();
281 Offset o (relative_coordinate (0, X_AXIS), relative_coordinate (0, Y_AXIS));
283 SCM s = get_elt_property ("extra-offset");
286 Real il = paper_l ()->get_var ("interline");
287 o[X_AXIS] += il * gh_scm2double (gh_car (s));
288 o[Y_AXIS] += il * gh_scm2double (gh_cdr (s));
291 pscore_l_->outputter_l_->output_molecule (output_p_,
305 Score_element::do_break_processing()
307 handle_broken_dependencies();
311 Score_element::do_post_processing()
316 Score_element::do_breakable_col_processing()
318 handle_prebroken_dependencies();
322 Score_element::do_pre_processing()
327 Score_element::do_space_processing ()
332 Score_element::do_add_processing()
339 Score_element::do_brew_molecule_p() const
343 Molecule a (lookup_l ()->fill (Box (emp,emp)));
344 return new Molecule (a);
349 Score_element::line_l() const
355 Score_element::add_dependency (Score_element*e)
359 dependency_arr_.push (e);
363 programming_error ("Null dependency added");
370 Do break substitution in S, using CRITERION. Return new value.
371 CRITERION is either a SMOB pointer to the desired line, or a number
372 representing the break direction. */
374 Score_element::handle_broken_smobs (SCM s, SCM criterion)
376 Score_element *sc = unsmob_element ( s);
379 if (criterion == SCM_UNDEFINED)
380 return SCM_UNDEFINED;
381 else if (gh_number_p (criterion))
383 Item * i = dynamic_cast<Item*> (sc);
384 Direction d = to_dir (criterion);
385 if (i && i->break_status_dir () != d)
387 Item *br = i->find_broken_piece (d);
388 return (br) ? br->self_scm_ : SCM_UNDEFINED;
393 Score_element * ln = unsmob_element ( criterion);
394 Line_of_score * line = dynamic_cast<Line_of_score*> (ln);
395 Score_element * br =0;
396 Line_of_score * dep_line = sc->line_l ();
397 if (dep_line != line)
399 br = sc->find_broken_piece (line);
400 return (br) ? br->self_scm_ : SCM_UNDEFINED;
403 return SCM_UNDEFINED;
406 else if (gh_pair_p (s))
409 UGH! breaks on circular lists.
411 gh_set_car_x (s, handle_broken_smobs (gh_car (s), criterion));
412 gh_set_cdr_x (s, handle_broken_smobs (gh_cdr (s), criterion));
414 if (gh_car (s) == SCM_UNDEFINED && gh_list_p (gh_cdr(s)))
421 Score_element::recurse_into_smobs (SCM s, void (Score_element::*meth_ptr)())
423 Score_element * sc = unsmob_element ( s);
428 else if (gh_pair_p (s))
430 recurse_into_smobs (gh_car (s), meth_ptr);
431 recurse_into_smobs (gh_cdr (s), meth_ptr);
436 Score_element::handle_broken_dependencies()
438 Line_of_score *line = line_l();
440 SCM rec = get_elt_property ("handle-broken-deps");
441 if (gh_boolean_p (rec) && gh_scm2bool (rec))
444 set_elt_property ("handle-broken-deps", SCM_BOOL_T);
445 element_property_alist_ = handle_broken_smobs (element_property_alist_,
446 line ? line->self_scm_ : SCM_UNDEFINED);
449 recurse_into_smobs (element_property_alist_,
450 &Score_element::handle_broken_dependencies);
456 Link_array<Score_element> new_deps;
458 for (int i=0; i < dependency_size(); i++)
460 Score_element * elt = dependency (i);
461 if (elt->line_l() != line)
463 Score_element * broken = elt->find_broken_piece (line);
469 dependency_arr_ = new_deps;
477 Score_element::handle_prebroken_dependencies()
479 if (Item*i =dynamic_cast<Item*> (this))
481 element_property_alist_
482 = handle_broken_smobs (element_property_alist_,
483 gh_int2scm (i->break_status_dir ()));
491 Link_array<Score_element>
492 Score_element::get_extra_dependencies() const
494 Link_array<Score_element> empty;
499 Score_element::linked_b() const
505 Score_element::do_print () const
510 Score_element::find_broken_piece (Line_of_score*) const
516 Score_element::mark_smob (SCM ses)
518 void * mp = (void*) SCM_CDR(ses);
519 Score_element * s = (Score_element*) mp;
521 assert (s->self_scm_ == ses);
522 return s->element_property_alist_;
527 Score_element::print_smob (SCM s, SCM port, scm_print_state *)
529 Score_element *sc = (Score_element *) SCM_CDR (s);
531 scm_puts ("#<Score_element ", port);
532 scm_puts ((char *)sc->name (), port);
534 // scm_puts (" properties = ", port);
535 // scm_display (sc->element_property_alist_, port);
536 scm_puts (" >", port);
541 Score_element::do_smobify_self ()
543 scm_unprotect_object (element_property_alist_); // ugh
545 #include "ly-smobs.icc"
546 IMPLEMENT_SMOBS(Score_element);
549 Score_element::equal_p (SCM a, SCM b)
551 return SCM_CDR(a) == SCM_CDR(b) ? SCM_BOOL_T : SCM_BOOL_F;
555 Score_element::translate_axis (Real y, Axis a)
557 dim_cache_[a]->translate (y);
561 Score_element::relative_coordinate (Score_element const*e, Axis a) const
563 return dim_cache_[a]->relative_coordinate (e ? e->dim_cache_[a] : 0);
567 Score_element::common_refpoint (Score_element const* s, Axis a) const
569 Dimension_cache *dim = dim_cache_[a]->common_refpoint (s->dim_cache_[a]);
571 programming_error ("No common reference point");
572 return dim ? dim->element_l () : 0;
576 Score_element::set_empty (Axis a)
578 dim_cache_[a]->callback_l_ =0;
582 Score_element::empty_b (Axis a)const
584 return !dim_cache_[a]->callback_l_;
588 Score_element::extent (Axis a) const
590 Dimension_cache const * d = dim_cache_[a];
592 return d->get_dim ();
596 unsmob_element (SCM s)
598 if (SMOB_IS_TYPE_B (Score_element, s))
599 return SMOB_TO_TYPE(Score_element,s);
609 Score_element::invalidate_cache (Axis a)
611 dim_cache_[a]->invalidate ();
615 Score_element::parent_l (Axis a) const
617 Dimension_cache*d= dim_cache_[a]->parent_l_;
618 return d ? d->elt_l_ : 0;
622 Score_element::common_refpoint (Link_array<Score_element> gs, Axis a) const
624 Dimension_cache * common = dim_cache_[a];
625 for (int i=0; i < gs.size (); i++)
627 common = common->common_refpoint (gs[i]->dim_cache_[a]);
630 return common->element_l ();
634 Score_element::name () const
636 return classname (this);
641 Score_element::set_parent (Score_element *g, Axis a)
643 dim_cache_[a]->parent_l_ = g ? g->dim_cache_[a]: 0;
647 Score_element::fixup_refpoint ()
649 for (int a = X_AXIS; a < NO_AXES; a ++)
652 Score_element * par = parent_l (ax);
657 if (par->line_l () != line_l ())
659 Score_element * newpar = par->find_broken_piece (line_l ());
660 set_parent (newpar, ax);
663 if (Item * i = dynamic_cast<Item*> (this))
665 Item *pari = dynamic_cast<Item*> (par);
669 Direction my_dir = i->break_status_dir () ;
670 if (my_dir!= pari->break_status_dir())
672 Item *newpar = pari->find_broken_piece (my_dir);
673 set_parent (newpar, ax);