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"
31 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 set_elt_property ("dependencies", SCM_EOL);
53 set_elt_property ("interfaces", SCM_EOL);
57 Score_element::Score_element (Score_element const&s)
59 dim_cache_[X_AXIS] = new Dimension_cache (*s.dim_cache_[X_AXIS]);
60 dim_cache_[Y_AXIS] = new Dimension_cache (*s.dim_cache_[Y_AXIS]);
61 dim_cache_[X_AXIS]->elt_l_ = dim_cache_[Y_AXIS]->elt_l_ = this;
65 original_l_ =(Score_element*) &s;
66 element_property_alist_ = SCM_EOL; // onstack;
68 status_i_ = s.status_i_;
69 lookup_l_ = s.lookup_l_;
70 pscore_l_ = s.pscore_l_;
75 Score_element::~Score_element()
77 assert (status_i_ >=0);
80 delete dim_cache_[X_AXIS];
81 delete dim_cache_[Y_AXIS];
84 // should also have one that takes SCM arg.
86 Score_element::get_elt_property (String nm) const
88 SCM sym = ly_symbol2scm (nm.ch_C());
89 SCM s = scm_assq(sym, element_property_alist_);
96 SCM sym2 = ly_symbol2scm ((name () + ("::" + nm)).ch_C());
99 // should probably check for Type::sym as well.
100 Paper_def * p= pscore_l_->paper_l_;
101 if (p->default_properties_.try_retrieve (sym2, &val))
103 else if (p->default_properties_.try_retrieve (sym, &val))
107 return SCM_UNDEFINED;
111 Score_element::remove_elt_property (String key)
113 SCM s = get_elt_property (key);
114 SCM sym = ly_symbol2scm (key.ch_C());
115 element_property_alist_ = scm_assq_remove_x (element_property_alist_, sym);
123 Score_element::set_elt_property (String k, SCM v)
125 SCM s = ly_symbol2scm (k.ch_C( ));
126 element_property_alist_ = scm_assoc_set_x (element_property_alist_, s, v);
130 Score_element::molecule_extent(Dimension_cache const *c)
132 Score_element *s = dynamic_cast<Score_element*>(c->element_l());
133 Molecule m = s->do_brew_molecule();
134 return m.extent()[c->axis ()];
139 Score_element::print() const
142 DEBUG_OUT << classname(this) << "{\n";
144 if (flower_dstream && !flower_dstream->silent_b ("Score_element"))
145 ly_display_scm (element_property_alist_);
148 DEBUG_OUT << "Copy ";
156 Score_element::paper_l () const
158 return pscore_l_ ? pscore_l_->paper_l_ : 0;
162 Score_element::lookup_l () const
166 Score_element * urg = (Score_element*)this;
167 SCM sz = urg->remove_elt_property ("fontsize");
168 int i = (gh_number_p (sz))
172 urg->lookup_l_ = (Lookup*)pscore_l_->paper_l_->lookup_l (i);
178 Score_element::add_processing()
180 assert (status_i_ >=0);
189 if (get_elt_property ("self-alignment-X") != SCM_UNDEFINED
190 && !dim_cache_[X_AXIS]->off_callback_l_)
192 add_offset_callback (Side_position_interface::aligned_on_self,X_AXIS);
195 if (get_elt_property ("self-alignment-Y") != SCM_UNDEFINED
196 && !dim_cache_[X_AXIS]->off_callback_l_)
199 add_offset_callback (Side_position_interface::aligned_on_self, Y_AXIS);
207 Score_element::calculate_dependencies (int final, int busy,
208 Score_element_method_pointer funcptr)
210 assert (status_i_ >=0);
212 if (status_i_ >= final)
215 assert (status_i_!= busy);
218 for (SCM d= get_elt_property ("dependencies"); d != SCM_EOL; d = gh_cdr (d))
220 unsmob_element (gh_car (d))
221 ->calculate_dependencies (final, busy, funcptr);
229 Score_element::output_processing ()
231 if (to_boolean (get_elt_property ("transparent")))
235 Molecule m (do_brew_molecule ());
236 Offset o (relative_coordinate (0, X_AXIS), relative_coordinate (0, Y_AXIS));
238 SCM s = get_elt_property ("extra-offset");
241 Real il = paper_l ()->get_var ("interline");
242 o[X_AXIS] += il * gh_scm2double (gh_car (s));
243 o[Y_AXIS] += il * gh_scm2double (gh_cdr (s));
246 pscore_l_->outputter_l_->output_molecule (m.expr_, o, classname(this));
255 Score_element::do_break_processing()
260 Score_element::after_line_breaking ()
265 Score_element::do_breakable_col_processing()
267 handle_prebroken_dependencies();
271 Score_element::before_line_breaking ()
276 Score_element::do_space_processing ()
281 Score_element::do_add_processing()
288 Score_element::do_brew_molecule() const
290 SCM glyph = get_elt_property ("glyph");
291 if (gh_string_p (glyph))
293 return lookup_l ()->afm_find (String (ly_scm2string (glyph)));
306 Score_element::line_l() const
312 Score_element::add_dependency (Score_element*e)
316 Group_interface gi (this, "dependencies");
320 programming_error ("Null dependency added");
327 Do break substitution in S, using CRITERION. Return new value.
328 CRITERION is either a SMOB pointer to the desired line, or a number
329 representing the break direction. Do not modify SRC.
332 Score_element::handle_broken_smobs (SCM src, SCM criterion)
337 Score_element *sc = unsmob_element (src);
340 if (criterion == SCM_UNDEFINED)
341 return SCM_UNDEFINED;
342 else if (gh_number_p (criterion))
344 Item * i = dynamic_cast<Item*> (sc);
345 Direction d = to_dir (criterion);
346 if (i && i->break_status_dir () != d)
348 Item *br = i->find_broken_piece (d);
349 return (br) ? br->self_scm_ : SCM_UNDEFINED;
354 Score_element * ln = unsmob_element ( criterion);
355 Line_of_score * line = dynamic_cast<Line_of_score*> (ln);
356 Score_element * br =0;
357 Line_of_score * dep_line = sc->line_l ();
358 if (dep_line != line)
360 br = sc->find_broken_piece (line);
361 return (br) ? br->self_scm_ : SCM_UNDEFINED;
364 return SCM_UNDEFINED;
367 else if (gh_pair_p (src))
370 UGH! breaks on circular lists.
372 SCM car = handle_broken_smobs (gh_car (src), criterion);
373 SCM cdr = gh_cdr (src);
375 if (car == SCM_UNDEFINED
376 && (gh_pair_p (cdr) || cdr == SCM_EOL))
379 This is tail-recursion, ie.
381 return handle_broken_smobs (cdr, criterion);
383 We don't want to rely on the compiler to do this. */
388 return gh_cons (car, handle_broken_smobs (cdr, criterion));
397 Score_element::handle_broken_dependencies()
399 Spanner * s= dynamic_cast<Spanner*> (this);
400 if (original_l_ && s)
405 for (int i = 0; i< s->broken_into_l_arr_ .size (); i++)
407 Score_element * sc = s->broken_into_l_arr_[i];
408 Line_of_score * l = sc->line_l ();
409 s->broken_into_l_arr_[i]->element_property_alist_ =
410 handle_broken_smobs (element_property_alist_,
411 l ? l->self_scm_ : SCM_UNDEFINED);
415 Line_of_score *line = line_l();
416 element_property_alist_
417 = handle_broken_smobs (element_property_alist_,
418 line ? line->self_scm_ : SCM_UNDEFINED);
426 Score_element::handle_prebroken_dependencies()
428 if (Item*i =dynamic_cast<Item*> (this))
432 element_property_alist_
433 = handle_broken_smobs (original_l_->element_property_alist_,
434 gh_int2scm (i->break_status_dir ()));
440 Score_element::linked_b() const
446 Score_element::do_print () const
451 Score_element::find_broken_piece (Line_of_score*) const
457 Score_element::translate_axis (Real y, Axis a)
459 dim_cache_[a]->translate (y);
463 Score_element::relative_coordinate (Score_element const*e, Axis a) const
465 return dim_cache_[a]->relative_coordinate (e ? e->dim_cache_[a] : 0);
469 Score_element::common_refpoint (Score_element const* s, Axis a) const
471 Dimension_cache *dim = dim_cache_[a]->common_refpoint (s->dim_cache_[a]);
472 return dim ? dim->element_l () : 0;
476 Score_element::set_empty (Axis a)
478 dim_cache_[a]->extent_callback_l_ =0;
482 Score_element::empty_b (Axis a)const
484 return !dim_cache_[a]->extent_callback_l_;
488 Score_element::extent (Axis a) const
490 Dimension_cache const * d = dim_cache_[a];
492 return d->get_dim ();
497 Score_element::parent_l (Axis a) const
499 Dimension_cache*d= dim_cache_[a]->parent_l_;
500 return d ? d->elt_l_ : 0;
504 Score_element::common_refpoint (Link_array<Score_element> gs, Axis a) const
506 Dimension_cache * common = dim_cache_[a];
507 for (int i=0; i < gs.size (); i++)
509 common = common->common_refpoint (gs[i]->dim_cache_[a]);
512 return common->element_l ();
516 Score_element::name () const
518 return classname (this);
522 Score_element::add_offset_callback (Offset_cache_callback cb, Axis a)
524 dim_cache_[a]->off_callbacks_.push (cb);
528 Score_element::has_offset_callback_b (Offset_cache_callback cb, Axis a)const
530 for (int i= dim_cache_[a]->off_callbacks_.size (); i--;)
532 if (dim_cache_[a]->off_callbacks_[i] == cb)
539 Score_element::set_parent (Score_element *g, Axis a)
541 dim_cache_[a]->parent_l_ = g ? g->dim_cache_[a]: 0;
545 Score_element::fixup_refpoint ()
547 for (int a = X_AXIS; a < NO_AXES; a ++)
550 Score_element * parent = parent_l (ax);
555 if (parent->line_l () != line_l ())
557 Score_element * newparent = parent->find_broken_piece (line_l ());
558 set_parent (newparent, ax);
561 programming_error ("Orphaned score-element.");
565 if (Item * i = dynamic_cast<Item*> (this))
567 Item *parenti = dynamic_cast<Item*> (parent);
571 Direction my_dir = i->break_status_dir () ;
572 if (my_dir!= parenti->break_status_dir())
574 Item *newparent = parenti->find_broken_piece (my_dir);
575 set_parent (newparent, ax);
584 /****************************************************
586 ****************************************************/
589 #include "ly-smobs.icc"
591 IMPLEMENT_UNSMOB(Score_element, element);
592 IMPLEMENT_SMOBS(Score_element);
594 Score_element::mark_smob (SCM ses)
596 Score_element * s = SMOB_TO_TYPE (Score_element, ses);
597 if (s->self_scm_ != ses)
599 programming_error ("SMOB marking gone awry");
602 return s->element_property_alist_;
606 Score_element::print_smob (SCM s, SCM port, scm_print_state *)
608 Score_element *sc = (Score_element *) gh_cdr (s);
610 scm_puts ("#<Score_element ", port);
611 scm_puts ((char *)sc->name (), port);
614 don't try to print properties, that is too much hassle.
616 scm_puts (" >", port);
621 Score_element::do_smobify_self ()
626 Score_element::equal_p (SCM a, SCM b)
628 return gh_cdr(a) == gh_cdr(b) ? SCM_BOOL_T : SCM_BOOL_F;
633 Score_element::ly_set_elt_property (SCM elt, SCM sym, SCM val)
635 Score_element * sc = unsmob_element (elt);
637 if (!gh_symbol_p (sym))
639 error ("Not a symbol");
640 ly_display_scm (sym);
641 return SCM_UNDEFINED;
646 sc->element_property_alist_ = scm_assoc_set_x (sc->element_property_alist_, sym, val);
650 error ("Not a score element");
651 ly_display_scm (elt);
654 return SCM_UNDEFINED;
659 Score_element::ly_get_elt_property (SCM elt, SCM sym)
661 Score_element * sc = unsmob_element (elt);
665 SCM s = scm_assq(sym, sc->element_property_alist_);
670 return SCM_UNDEFINED;
674 error ("Not a score element");
675 ly_display_scm (elt);
677 return SCM_UNDEFINED;
684 scm_make_gsubr ("ly-get-elt-property", 2, 0, 0, (SCM(*)(...))Score_element::ly_get_elt_property);
685 scm_make_gsubr ("ly-set-elt-property", 3, 0, 0, (SCM(*)(...))Score_element::ly_set_elt_property);
688 ADD_SCM_INIT_FUNC(scoreelt, init_functions);