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"
31 Score_element::Score_element()
34 dim_cache_[X_AXIS] = new Dimension_cache;
35 dim_cache_[Y_AXIS] = new Dimension_cache;
36 dim_cache_[X_AXIS]->elt_l_ = dim_cache_[Y_AXIS]->elt_l_ = this;
40 dim_cache_[X_AXIS]->set_callback (molecule_extent);
41 dim_cache_[Y_AXIS]->set_callback (molecule_extent);
48 element_property_alist_ = SCM_EOL;
53 SCM ly_deep_copy (SCM);
60 return gh_cons (ly_deep_copy (gh_car (l)), ly_deep_copy (gh_cdr (l)));
67 Score_element::Score_element (Score_element const&s)
69 dim_cache_[X_AXIS] = new Dimension_cache (*s.dim_cache_[X_AXIS]);
70 dim_cache_[Y_AXIS] = new Dimension_cache (*s.dim_cache_[Y_AXIS]);
71 dim_cache_[X_AXIS]->elt_l_ = dim_cache_[Y_AXIS]->elt_l_ = this;
75 original_l_ =(Score_element*) &s;
78 should protect because smobify_self () might trigger GC.
80 element_property_alist_ = scm_protect_object (ly_deep_copy (s.element_property_alist_));
81 dependency_arr_ = s.dependency_arr_;
83 status_i_ = s.status_i_;
84 lookup_l_ = s.lookup_l_;
85 pscore_l_ = s.pscore_l_;
92 Score_element::~Score_element()
95 assert (status_i_ >=0);
98 delete dim_cache_[X_AXIS];
99 delete dim_cache_[Y_AXIS];
103 Score_element::dependency (int i) const
105 return dependency_arr_ [i];
109 Score_element::dependency_size () const
111 return dependency_arr_.size ();
114 // should also have one that takes SCM arg.
116 Score_element::get_elt_property (String nm) const
118 SCM sym = ly_symbol2scm (nm.ch_C());
119 SCM s = scm_assq(sym, element_property_alist_);
126 SCM sym2 = ly_symbol2scm ((name () + ("::" + nm)).ch_C());
129 // should probably check for Type::sym as well.
130 Paper_def * p= pscore_l_->paper_l_;
131 if (p->default_properties_.try_retrieve (sym2, &val))
133 else if (p->default_properties_.try_retrieve (sym, &val))
137 return SCM_UNDEFINED;
141 Score_element::remove_elt_property (String key)
143 SCM s = get_elt_property (key);
144 SCM sym = ly_symbol2scm (key.ch_C());
145 element_property_alist_ = scm_assq_remove_x (element_property_alist_, sym);
153 Score_element::set_elt_property (String k, SCM v)
155 SCM s = ly_symbol2scm (k.ch_C( ));
156 element_property_alist_ = scm_assoc_set_x (element_property_alist_, s, v);
160 Score_element::molecule_extent(Dimension_cache const *c)
162 Score_element *s = dynamic_cast<Score_element*>(c->element_l());
163 Molecule*m = s->do_brew_molecule_p();
164 return m->extent()[c->axis ()];
169 Score_element::print() const
172 DEBUG_OUT << classname(this) << "{\n";
175 if (flower_dstream && !flower_dstream->silent_b ("Score_element"))
176 ly_display_scm (element_property_alist_);
178 DEBUG_OUT << "dependencies: " << dependency_size();
180 DEBUG_OUT << "Copy ";
188 Score_element::paper_l () const
190 return pscore_l_->paper_l_;
194 Score_element::lookup_l () const
198 Score_element * urg = (Score_element*)this;
199 SCM sz = urg->remove_elt_property ("fontsize");
200 int i = (sz != SCM_UNDEFINED)
204 urg->lookup_l_ = (Lookup*)pscore_l_->paper_l_->lookup_l (i);
210 Score_element::add_processing()
212 assert (status_i_ >=0);
221 if (get_elt_property ("self-alignment-X") != SCM_UNDEFINED
222 && !dim_cache_[X_AXIS]->off_callback_l_)
224 dim_cache_[X_AXIS]->off_callbacks_.push (Side_position_interface::self_alignment);
227 if (get_elt_property ("self-alignment-Y") != SCM_UNDEFINED
228 && !dim_cache_[X_AXIS]->off_callback_l_)
231 dim_cache_[Y_AXIS]->set_offset_callback (Side_position_interface::self_alignment);
239 Score_element::calculate_dependencies (int final, int busy,
240 Score_element_method_pointer funcptr)
242 assert (status_i_ >=0);
244 if (status_i_ >= final)
247 assert (status_i_!= busy);
250 for (int i=0; i < dependency_arr_.size(); i++)
251 dependency_arr_[i]->calculate_dependencies (final, busy, funcptr);
253 Link_array<Score_element> extra (get_extra_dependencies());
254 for (int i=0; i < extra.size(); i++)
255 extra[i]->calculate_dependencies (final, busy, funcptr);
262 Score_element::output_processing ()
264 if (get_elt_property ("transparent") != SCM_UNDEFINED)
267 // we're being silly here.
271 output_p_ = do_brew_molecule_p ();
272 Offset o (relative_coordinate (0, X_AXIS), relative_coordinate (0, Y_AXIS));
274 SCM s = get_elt_property ("extra-offset");
277 Real il = paper_l ()->get_var ("interline");
278 o[X_AXIS] += il * gh_scm2double (gh_car (s));
279 o[Y_AXIS] += il * gh_scm2double (gh_cdr (s));
282 pscore_l_->outputter_l_->output_molecule (output_p_,
296 Score_element::do_break_processing()
298 handle_broken_dependencies();
302 Score_element::do_post_processing()
307 Score_element::do_breakable_col_processing()
309 handle_prebroken_dependencies();
313 Score_element::do_pre_processing()
318 Score_element::do_space_processing ()
323 Score_element::do_add_processing()
328 Score_element::do_substitute_element_pointer (Score_element*,Score_element*)
334 Score_element::do_brew_molecule_p() const
338 Molecule a (lookup_l ()->fill (Box (emp,emp)));
339 return new Molecule (a);
344 Score_element::line_l() const
350 Score_element::add_dependency (Score_element*e)
354 dependency_arr_.push (e);
358 programming_error ("Null dependency added");
362 Score_element::substitute_dependency (Score_element* old, Score_element* new_l)
364 do_substitute_element_pointer (old,new_l);
365 old->do_substitute_element_pointer (this, 0);
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 if (SMOB_IS_TYPE_B (Score_element, s))
378 Score_element *sc = SMOB_TO_TYPE (Score_element, s);
380 if (criterion == SCM_UNDEFINED)
381 return SCM_UNDEFINED;
382 else if (gh_number_p (criterion))
384 Item * i = dynamic_cast<Item*> (sc);
385 Direction d = to_dir (criterion);
386 if (i && i->break_status_dir () != d)
388 Item *br = i->find_broken_piece (d);
389 return (br) ? br->self_scm_ : SCM_UNDEFINED;
394 Score_element * ln = SMOB_TO_TYPE (Score_element, criterion);
395 Line_of_score * line = dynamic_cast<Line_of_score*> (ln);
396 Score_element * br =0;
397 Line_of_score * dep_line = sc->line_l ();
398 if (dep_line != line)
400 br = sc->find_broken_piece (line);
401 return (br) ? br->self_scm_ : SCM_UNDEFINED;
404 return SCM_UNDEFINED;
407 else if (gh_pair_p (s))
410 UGH! breaks on circular lists.
412 gh_set_car_x (s, handle_broken_smobs (gh_car (s), criterion));
413 gh_set_cdr_x (s, handle_broken_smobs (gh_cdr (s), criterion));
415 if (gh_car (s) == SCM_UNDEFINED)
422 Score_element::handle_broken_dependencies()
424 Line_of_score *line = line_l();
425 element_property_alist_ = handle_broken_smobs (element_property_alist_,
426 line ? line->self_scm_ : SCM_UNDEFINED);
431 Link_array<Score_element> new_deps;
433 for (int i=0; i < dependency_size(); i++)
435 Score_element * elt = dependency (i);
436 if (elt->line_l() != line)
438 Score_element * broken = elt->find_broken_piece (line);
439 substitute_dependency (elt, broken);
445 dependency_arr_ = new_deps;
453 Score_element::handle_prebroken_dependencies()
455 if (Item*i =dynamic_cast<Item*> (this))
457 element_property_alist_
458 = handle_broken_smobs (element_property_alist_,
459 gh_int2scm (i->break_status_dir ()));
462 Link_array<Score_element> old_arr, new_arr;
464 for (int i=0; i < dependency_size(); i++)
466 Score_element * elt = dependency (i);
467 Item *it_l = dynamic_cast <Item *> (elt);
468 if (it_l && it_l->broken_original_b ())
469 if (Item *me = dynamic_cast<Item*> (this) )
471 Score_element *new_l = it_l->find_broken_piece (me->break_status_dir ());
474 new_arr.push (new_l);
483 new_arr.push (it_l->find_broken_piece (d));
484 } while (flip(&d)!= LEFT);
488 for (int i=0; i < old_arr.size(); i++)
490 substitute_dependency (old_arr[i], new_arr[i]);
495 Score_element::handle_prebroken_dependents()
500 Score_element::handle_broken_dependents()
507 Link_array<Score_element>
508 Score_element::get_extra_dependencies() const
510 Link_array<Score_element> empty;
515 Score_element::linked_b() const
521 Score_element::do_print () const
526 Score_element::find_broken_piece (Line_of_score*) const
532 Score_element::mark_smob (SCM ses)
534 void * mp = (void*) SCM_CDR(ses);
535 Score_element * s = (Score_element*) mp;
537 assert (s->self_scm_ == ses);
538 return s->element_property_alist_;
543 Score_element::print_smob (SCM s, SCM port, scm_print_state *)
545 Score_element *sc = (Score_element *) SCM_CDR (s);
547 scm_puts ("#<Score_element ", port);
548 scm_puts ((char *)sc->name (), port);
550 for (SCM s = sc->element_property_alist_; gh_pair_p (s); s = gh_cdr (s))
552 scm_display (gh_caar(s), port);
553 SCM val = gh_cdar(s);
554 if (SMOB_IS_TYPE_B (Score_element, val))
556 scm_puts ("#<:", port);
557 scm_puts ((SMOB_TO_TYPE(Score_element,val))->name(), port);
558 scm_puts (">", port);
561 scm_display (val, port);
564 // scm_puts (" properties = ", port);
565 // scm_display (sc->element_property_alist_, port);
566 scm_puts (" >", port);
571 Score_element::do_smobify_self ()
573 scm_unprotect_object (element_property_alist_); // ugh
575 #include "ly-smobs.icc"
576 IMPLEMENT_SMOBS(Score_element);
579 Score_element::equal_p (SCM a, SCM b)
581 return SCM_CDR(a) == SCM_CDR(b) ? SCM_BOOL_T : SCM_BOOL_F;
585 Score_element::translate_axis (Real y, Axis a)
587 dim_cache_[a]->translate (y);
591 Score_element::relative_coordinate (Score_element const*e, Axis a) const
593 return dim_cache_[a]->relative_coordinate (e ? e->dim_cache_[a] : 0);
597 Score_element::common_refpoint (Score_element const* s, Axis a) const
599 Dimension_cache *dim = dim_cache_[a]->common_refpoint (s->dim_cache_[a]);
601 programming_error ("No common reference point");
602 return dim ? dim->element_l () : 0;
606 Score_element::set_empty (Axis a)
608 dim_cache_[a]->callback_l_ =0;
612 Score_element::empty_b (Axis a)const
614 return !dim_cache_[a]->callback_l_;
618 Score_element::extent (Axis a) const
620 Dimension_cache const * d = dim_cache_[a];
622 return d->get_dim ();
632 Score_element::invalidate_cache (Axis a)
634 dim_cache_[a]->invalidate ();
638 Score_element::parent_l (Axis a) const
640 Dimension_cache*d= dim_cache_[a]->parent_l_;
641 return d ? d->elt_l_ : 0;
645 Score_element::common_refpoint (Link_array<Score_element> gs, Axis a) const
647 Dimension_cache * common = dim_cache_[a];
648 for (int i=0; i < gs.size (); i++)
650 common = common->common_refpoint (gs[i]->dim_cache_[a]);
653 return common->element_l ();
657 Score_element::name () const
659 return classname (this);
664 Score_element::set_parent (Score_element *g, Axis a)
666 dim_cache_[a]->parent_l_ = g ? g->dim_cache_[a]: 0;
670 Score_element::fixup_refpoint ()
672 for (int a = X_AXIS; a < NO_AXES; a ++)
675 Score_element * par = parent_l (ax);
680 if (par->line_l () != line_l ())
682 Score_element * newpar = par->find_broken_piece (line_l ());
683 set_parent (newpar, ax);
686 if (Item * i = dynamic_cast<Item*> (this))
688 Item *pari = dynamic_cast<Item*> (par);
692 Direction my_dir = i->break_status_dir () ;
693 if (my_dir!= pari->break_status_dir())
695 Item *newpar = pari->find_broken_piece (my_dir);
696 set_parent (newpar, ax);