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 if (get_elt_property ("self-alignment-X") != SCM_UNDEFINED
217 && !dim_cache_[X_AXIS]->off_callback_l_)
219 add_offset_callback (Side_position_interface::aligned_on_self,X_AXIS);
222 if (get_elt_property ("self-alignment-Y") != SCM_UNDEFINED
223 && !dim_cache_[X_AXIS]->off_callback_l_)
226 add_offset_callback (Side_position_interface::aligned_on_self, Y_AXIS);
234 Score_element::calculate_dependencies (int final, int busy,
235 Score_element_method_pointer funcptr)
237 assert (status_i_ >=0);
239 if (status_i_ >= final)
242 assert (status_i_!= busy);
245 for (SCM d= get_elt_property ("dependencies"); d != SCM_EOL; d = gh_cdr (d))
247 unsmob_element (gh_car (d))
248 ->calculate_dependencies (final, busy, funcptr);
256 Score_element::output_processing ()
258 if (to_boolean (get_elt_property ("transparent")))
262 Molecule m (do_brew_molecule ());
263 Offset o (relative_coordinate (0, X_AXIS), relative_coordinate (0, Y_AXIS));
265 SCM s = get_elt_property ("extra-offset");
268 Real il = paper_l ()->get_var ("interline");
269 o[X_AXIS] += il * gh_scm2double (gh_car (s));
270 o[Y_AXIS] += il * gh_scm2double (gh_cdr (s));
273 pscore_l_->outputter_l_->output_molecule (m.expr_, o, classname(this));
282 Score_element::do_break_processing()
287 Score_element::after_line_breaking ()
293 Score_element::before_line_breaking ()
298 Score_element::do_space_processing ()
303 Score_element::do_add_processing()
310 Score_element::do_brew_molecule() const
312 SCM glyph = get_elt_property ("glyph");
313 if (gh_string_p (glyph))
315 return lookup_l ()->afm_find (String (ly_scm2string (glyph)));
328 Score_element::line_l() const
334 Score_element::add_dependency (Score_element*e)
338 Group_interface gi (this, "dependencies");
342 programming_error ("Null dependency added");
349 Do break substitution in S, using CRITERION. Return new value.
350 CRITERION is either a SMOB pointer to the desired line, or a number
351 representing the break direction. Do not modify SRC.
354 Score_element::handle_broken_smobs (SCM src, SCM criterion)
359 Score_element *sc = unsmob_element (src);
362 if (criterion == SCM_UNDEFINED)
363 return SCM_UNDEFINED;
364 else if (gh_number_p (criterion))
366 Item * i = dynamic_cast<Item*> (sc);
367 Direction d = to_dir (criterion);
368 if (i && i->break_status_dir () != d)
370 Item *br = i->find_broken_piece (d);
371 return (br) ? br->self_scm_ : SCM_UNDEFINED;
376 Score_element * ln = unsmob_element ( criterion);
377 Line_of_score * line = dynamic_cast<Line_of_score*> (ln);
378 Score_element * br =0;
379 Line_of_score * dep_line = sc->line_l ();
380 if (dep_line != line)
382 br = sc->find_broken_piece (line);
383 return (br) ? br->self_scm_ : SCM_UNDEFINED;
386 return SCM_UNDEFINED;
389 else if (gh_pair_p (src))
392 UGH! breaks on circular lists.
394 SCM car = handle_broken_smobs (gh_car (src), criterion);
395 SCM cdr = gh_cdr (src);
397 if (car == SCM_UNDEFINED
398 && (gh_pair_p (cdr) || cdr == SCM_EOL))
401 This is tail-recursion, ie.
403 return handle_broken_smobs (cdr, criterion);
405 We don't want to rely on the compiler to do this. */
410 return gh_cons (car, handle_broken_smobs (cdr, criterion));
419 Score_element::handle_broken_dependencies()
421 Spanner * s= dynamic_cast<Spanner*> (this);
422 if (original_l_ && s)
427 for (int i = 0; i< s->broken_into_l_arr_ .size (); i++)
429 Score_element * sc = s->broken_into_l_arr_[i];
430 Line_of_score * l = sc->line_l ();
431 s->broken_into_l_arr_[i]->element_property_alist_ =
432 handle_broken_smobs (element_property_alist_,
433 l ? l->self_scm_ : SCM_UNDEFINED);
437 Line_of_score *line = line_l();
438 element_property_alist_
439 = handle_broken_smobs (element_property_alist_,
440 line ? line->self_scm_ : SCM_UNDEFINED);
448 Score_element::handle_prebroken_dependencies()
450 if (Item*i =dynamic_cast<Item*> (this))
454 element_property_alist_
455 = handle_broken_smobs (original_l_->element_property_alist_,
456 gh_int2scm (i->break_status_dir ()));
462 Score_element::linked_b() const
468 Score_element::do_print () const
473 Score_element::find_broken_piece (Line_of_score*) const
479 Score_element::translate_axis (Real y, Axis a)
481 dim_cache_[a]->translate (y);
485 Score_element::relative_coordinate (Score_element const*e, Axis a) const
487 return dim_cache_[a]->relative_coordinate (e ? e->dim_cache_[a] : 0);
491 Score_element::common_refpoint (Score_element const* s, Axis a) const
493 Dimension_cache *dim = dim_cache_[a]->common_refpoint (s->dim_cache_[a]);
494 return dim ? dim->element_l () : 0;
498 Score_element::set_empty (Axis a)
500 dim_cache_[a]->extent_callback_l_ =0;
504 Score_element::empty_b (Axis a)const
506 return !dim_cache_[a]->extent_callback_l_;
510 Score_element::extent (Axis a) const
512 Dimension_cache const * d = dim_cache_[a];
513 Interval ext = d->get_dim ();
516 SCM extra = get_elt_property (a == X_AXIS ? "extra-extent-X"
523 Real s = paper_l ()->get_var ("staffspace");
524 if (gh_pair_p (extra))
526 ext[BIGGER] += s * gh_scm2double (gh_cdr (extra));
527 ext[SMALLER] += s * gh_scm2double (gh_car (extra));
530 extra = get_elt_property (a == X_AXIS
532 : "minimum-extent-Y");
533 if (gh_pair_p (extra))
535 ext.unite (Interval (s * gh_scm2double (gh_car (extra)),
536 s * gh_scm2double (gh_cdr (extra))));
545 Score_element::parent_l (Axis a) const
547 Dimension_cache*d= dim_cache_[a]->parent_l_;
548 return d ? d->elt_l_ : 0;
552 Score_element::common_refpoint (Link_array<Score_element> gs, Axis a) const
554 Dimension_cache * common = dim_cache_[a];
555 for (int i=0; i < gs.size (); i++)
557 common = common->common_refpoint (gs[i]->dim_cache_[a]);
560 return common->element_l ();
564 Score_element::name () const
566 return classname (this);
570 Score_element::add_offset_callback (Offset_cache_callback cb, Axis a)
572 dim_cache_[a]->off_callbacks_.push (cb);
576 Score_element::has_offset_callback_b (Offset_cache_callback cb, Axis a)const
578 for (int i= dim_cache_[a]->off_callbacks_.size (); i--;)
580 if (dim_cache_[a]->off_callbacks_[i] == cb)
587 Score_element::set_parent (Score_element *g, Axis a)
589 dim_cache_[a]->parent_l_ = g ? g->dim_cache_[a]: 0;
593 Score_element::fixup_refpoint ()
595 for (int a = X_AXIS; a < NO_AXES; a ++)
598 Score_element * parent = parent_l (ax);
603 if (parent->line_l () != line_l () && line_l ())
605 Score_element * newparent = parent->find_broken_piece (line_l ());
606 set_parent (newparent, ax);
609 programming_error ("Orphaned score-element.");
613 if (Item * i = dynamic_cast<Item*> (this))
615 Item *parenti = dynamic_cast<Item*> (parent);
619 Direction my_dir = i->break_status_dir () ;
620 if (my_dir!= parenti->break_status_dir())
622 Item *newparent = parenti->find_broken_piece (my_dir);
623 set_parent (newparent, ax);
632 /****************************************************
634 ****************************************************/
637 #include "ly-smobs.icc"
639 IMPLEMENT_UNSMOB(Score_element, element);
640 IMPLEMENT_SMOBS(Score_element);
642 Score_element::mark_smob (SCM ses)
644 Score_element * s = SMOB_TO_TYPE (Score_element, ses);
645 if (s->self_scm_ != ses)
647 programming_error ("SMOB marking gone awry");
650 return s->element_property_alist_;
654 Score_element::print_smob (SCM s, SCM port, scm_print_state *)
656 Score_element *sc = (Score_element *) gh_cdr (s);
658 scm_puts ("#<Score_element ", port);
659 scm_puts ((char *)sc->name (), port);
662 don't try to print properties, that is too much hassle.
664 scm_puts (" >", port);
669 Score_element::do_smobify_self ()
674 Score_element::equal_p (SCM a, SCM b)
676 return gh_cdr(a) == gh_cdr(b) ? SCM_BOOL_T : SCM_BOOL_F;
681 Score_element::ly_set_elt_property (SCM elt, SCM sym, SCM val)
683 Score_element * sc = unsmob_element (elt);
685 if (!gh_symbol_p (sym))
687 error ("Not a symbol");
688 ly_display_scm (sym);
689 return SCM_UNDEFINED;
694 sc->element_property_alist_ = scm_assoc_set_x (sc->element_property_alist_, sym, val);
698 error ("Not a score element");
699 ly_display_scm (elt);
702 return SCM_UNDEFINED;
707 Score_element::ly_get_elt_property (SCM elt, SCM sym)
709 Score_element * sc = unsmob_element (elt);
713 SCM s = scm_assq(sym, sc->element_property_alist_);
718 return SCM_UNDEFINED;
722 error ("Not a score element");
723 ly_display_scm (elt);
725 return SCM_UNDEFINED;
732 scm_make_gsubr ("ly-get-elt-property", 2, 0, 0, (SCM(*)(...))Score_element::ly_get_elt_property);
733 scm_make_gsubr ("ly-set-elt-property", 3, 0, 0, (SCM(*)(...))Score_element::ly_set_elt_property);
736 ADD_SCM_INIT_FUNC(scoreelt, init_functions);
739 Score_element::do_breakable_col_processing ()