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()
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 set_elt_property ("dependencies", SCM_EOL);
54 set_elt_property ("interfaces", SCM_EOL);
57 SCM ly_deep_copy (SCM);
64 return gh_cons (ly_deep_copy (gh_car (l)), ly_deep_copy (gh_cdr (l)));
71 Score_element::Score_element (Score_element const&s)
73 dim_cache_[X_AXIS] = new Dimension_cache (*s.dim_cache_[X_AXIS]);
74 dim_cache_[Y_AXIS] = new Dimension_cache (*s.dim_cache_[Y_AXIS]);
75 dim_cache_[X_AXIS]->elt_l_ = dim_cache_[Y_AXIS]->elt_l_ = this;
79 original_l_ =(Score_element*) &s;
82 should protect because smobify_self () might trigger GC.
84 SCM onstack = ly_deep_copy (s.element_property_alist_);
85 element_property_alist_ = onstack;
88 status_i_ = s.status_i_;
89 lookup_l_ = s.lookup_l_;
90 pscore_l_ = s.pscore_l_;
95 Score_element::~Score_element()
98 assert (status_i_ >=0);
101 delete dim_cache_[X_AXIS];
102 delete dim_cache_[Y_AXIS];
107 Score_element::get_real (String s) const
109 return gh_scm2double (get_elt_property (s));
113 Score_element::set_real (String s, Real r)
115 set_elt_property (s, gh_double2scm (r));
118 // should also have one that takes SCM arg.
120 Score_element::get_elt_property (String nm) const
122 SCM sym = ly_symbol2scm (nm.ch_C());
123 SCM s = scm_assq(sym, element_property_alist_);
130 SCM sym2 = ly_symbol2scm ((name () + ("::" + nm)).ch_C());
133 // should probably check for Type::sym as well.
134 Paper_def * p= pscore_l_->paper_l_;
135 if (p->default_properties_.try_retrieve (sym2, &val))
137 else if (p->default_properties_.try_retrieve (sym, &val))
141 return SCM_UNDEFINED;
145 Score_element::remove_elt_property (String key)
147 SCM s = get_elt_property (key);
148 SCM sym = ly_symbol2scm (key.ch_C());
149 element_property_alist_ = scm_assq_remove_x (element_property_alist_, sym);
157 Score_element::set_elt_property (String k, SCM v)
159 SCM s = ly_symbol2scm (k.ch_C( ));
160 element_property_alist_ = scm_assoc_set_x (element_property_alist_, s, v);
164 Score_element::molecule_extent(Dimension_cache const *c)
166 Score_element *s = dynamic_cast<Score_element*>(c->element_l());
167 Molecule*m = s->do_brew_molecule_p();
169 Interval iv = m->extent()[c->axis ()];
177 Score_element::print() const
180 DEBUG_OUT << classname(this) << "{\n";
182 if (flower_dstream && !flower_dstream->silent_b ("Score_element"))
183 ly_display_scm (element_property_alist_);
186 DEBUG_OUT << "Copy ";
194 Score_element::paper_l () const
196 return pscore_l_ ? pscore_l_->paper_l_ : 0;
200 Score_element::lookup_l () const
204 Score_element * urg = (Score_element*)this;
205 SCM sz = urg->remove_elt_property ("fontsize");
206 int i = (gh_number_p (sz))
210 urg->lookup_l_ = (Lookup*)pscore_l_->paper_l_->lookup_l (i);
216 Score_element::add_processing()
218 assert (status_i_ >=0);
227 if (get_elt_property ("self-alignment-X") != SCM_UNDEFINED
228 && !dim_cache_[X_AXIS]->off_callback_l_)
230 dim_cache_[X_AXIS]->off_callbacks_.push (Side_position_interface::aligned_on_self);
233 if (get_elt_property ("self-alignment-Y") != SCM_UNDEFINED
234 && !dim_cache_[X_AXIS]->off_callback_l_)
237 dim_cache_[Y_AXIS]->set_offset_callback (Side_position_interface::aligned_on_self);
245 Score_element::calculate_dependencies (int final, int busy,
246 Score_element_method_pointer funcptr)
248 assert (status_i_ >=0);
250 if (status_i_ >= final)
253 assert (status_i_!= busy);
256 Link_array<Score_element> dependency_arr =
257 Group_interface__extract_elements (this, (Score_element*)0, "dependencies");
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 (to_boolean (get_elt_property ("transparent")))
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()
310 Score_element::do_post_processing()
315 Score_element::do_breakable_col_processing()
317 handle_prebroken_dependencies();
321 Score_element::do_pre_processing()
326 Score_element::do_space_processing ()
331 Score_element::do_add_processing()
338 Score_element::do_brew_molecule_p() const
340 SCM glyph = get_elt_property ("glyph");
341 if (gh_string_p (glyph))
343 Molecule*output = new Molecule (lookup_l ()->afm_find (String (ly_scm2string (glyph))));
351 Molecule a (lookup_l ()->fill (Box (emp,emp)));
352 return new Molecule (a);
358 Score_element::line_l() const
364 Score_element::add_dependency (Score_element*e)
368 Group_interface gi (this, "dependencies");
372 programming_error ("Null dependency added");
379 Do break substitution in S, using CRITERION. Return new value.
380 CRITERION is either a SMOB pointer to the desired line, or a number
381 representing the break direction. */
383 Score_element::handle_broken_smobs (SCM s, SCM criterion)
388 Score_element *sc = unsmob_element ( s);
391 if (criterion == SCM_UNDEFINED)
392 return SCM_UNDEFINED;
393 else if (gh_number_p (criterion))
395 Item * i = dynamic_cast<Item*> (sc);
396 Direction d = to_dir (criterion);
397 if (i && i->break_status_dir () != d)
399 Item *br = i->find_broken_piece (d);
400 return (br) ? br->self_scm_ : SCM_UNDEFINED;
405 Score_element * ln = unsmob_element ( criterion);
406 Line_of_score * line = dynamic_cast<Line_of_score*> (ln);
407 Score_element * br =0;
408 Line_of_score * dep_line = sc->line_l ();
409 if (dep_line != line)
411 br = sc->find_broken_piece (line);
412 return (br) ? br->self_scm_ : SCM_UNDEFINED;
415 return SCM_UNDEFINED;
418 else if (gh_pair_p (s))
421 UGH! breaks on circular lists.
423 SCM car = handle_broken_smobs (gh_car (s), criterion);
424 SCM cdr = gh_cdr (s);
426 if (car == SCM_UNDEFINED
427 && (gh_pair_p (cdr) || cdr == SCM_EOL))
430 This is tail-recursion, ie.
432 return handle_broken_smobs (cdr, criterion);
434 We don't want to rely on the compiler to do this. */
439 gh_set_car_x (s, car);
440 gh_set_cdr_x (s, handle_broken_smobs (cdr, criterion));
447 Score_element::handle_broken_dependencies()
449 Line_of_score *line = line_l();
450 element_property_alist_ = handle_broken_smobs (element_property_alist_,
451 line ? line->self_scm_ : SCM_UNDEFINED);
462 Score_element::handle_prebroken_dependencies()
464 if (Item*i =dynamic_cast<Item*> (this))
466 element_property_alist_
467 = handle_broken_smobs (element_property_alist_,
468 gh_int2scm (i->break_status_dir ()));
476 Link_array<Score_element>
477 Score_element::get_extra_dependencies() const
479 Link_array<Score_element> empty;
484 Score_element::linked_b() const
490 Score_element::do_print () const
495 Score_element::find_broken_piece (Line_of_score*) const
503 Score_element::translate_axis (Real y, Axis a)
505 dim_cache_[a]->translate (y);
509 Score_element::relative_coordinate (Score_element const*e, Axis a) const
511 return dim_cache_[a]->relative_coordinate (e ? e->dim_cache_[a] : 0);
515 Score_element::common_refpoint (Score_element const* s, Axis a) const
517 Dimension_cache *dim = dim_cache_[a]->common_refpoint (s->dim_cache_[a]);
518 return dim ? dim->element_l () : 0;
522 Score_element::set_empty (Axis a)
524 dim_cache_[a]->callback_l_ =0;
528 Score_element::empty_b (Axis a)const
530 return !dim_cache_[a]->callback_l_;
534 Score_element::extent (Axis a) const
536 Dimension_cache const * d = dim_cache_[a];
538 return d->get_dim ();
543 Score_element::parent_l (Axis a) const
545 Dimension_cache*d= dim_cache_[a]->parent_l_;
546 return d ? d->elt_l_ : 0;
550 Score_element::common_refpoint (Link_array<Score_element> gs, Axis a) const
552 Dimension_cache * common = dim_cache_[a];
553 for (int i=0; i < gs.size (); i++)
555 common = common->common_refpoint (gs[i]->dim_cache_[a]);
558 return common->element_l ();
562 Score_element::name () const
564 return classname (this);
569 Score_element::set_parent (Score_element *g, Axis a)
571 dim_cache_[a]->parent_l_ = g ? g->dim_cache_[a]: 0;
575 Score_element::fixup_refpoint ()
577 for (int a = X_AXIS; a < NO_AXES; a ++)
580 Score_element * par = parent_l (ax);
585 if (par->line_l () != line_l ())
587 Score_element * newpar = par->find_broken_piece (line_l ());
588 set_parent (newpar, ax);
591 if (Item * i = dynamic_cast<Item*> (this))
593 Item *pari = dynamic_cast<Item*> (par);
597 Direction my_dir = i->break_status_dir () ;
598 if (my_dir!= pari->break_status_dir())
600 Item *newpar = pari->find_broken_piece (my_dir);
601 set_parent (newpar, ax);
610 /****************************************************
612 ****************************************************/
615 #include "ly-smobs.icc"
617 IMPLEMENT_SMOBS(Score_element);
618 IMPLEMENT_UNSMOB(Score_element, element);
620 Score_element::mark_smob (SCM ses)
622 Score_element * s = SMOB_TO_TYPE (Score_element, ses);
623 if (s->self_scm_ != ses)
625 programming_error ("SMOB marking gone awry");
628 return s->element_property_alist_;
632 Score_element::print_smob (SCM s, SCM port, scm_print_state *)
634 Score_element *sc = (Score_element *) gh_cdr (s);
636 scm_puts ("#<Score_element ", port);
637 scm_puts ((char *)sc->name (), port);
639 // scm_puts (" properties = ", port);
640 // scm_display (sc->element_property_alist_, port);
641 scm_puts (" >", port);
646 Score_element::do_smobify_self ()
651 Score_element::equal_p (SCM a, SCM b)
653 return gh_cdr(a) == gh_cdr(b) ? SCM_BOOL_T : SCM_BOOL_F;
658 Score_element::ly_set_elt_property (SCM elt, SCM sym, SCM val)
660 Score_element * sc = unsmob_element (elt);
662 if (!gh_symbol_p (sym))
664 error ("Not a symbol");
665 ly_display_scm (sym);
666 return SCM_UNDEFINED;
671 sc->element_property_alist_ = scm_assoc_set_x (sc->element_property_alist_, sym, val);
675 error ("Not a score element");
676 ly_display_scm (elt);
679 return SCM_UNDEFINED;
684 Score_element::ly_get_elt_property (SCM elt, SCM sym)
686 Score_element * sc = unsmob_element (elt);
690 SCM s = scm_assq(sym, sc->element_property_alist_);
695 return SCM_UNDEFINED;
699 error ("Not a score element");
700 ly_display_scm (elt);
702 return SCM_UNDEFINED;
709 scm_make_gsubr ("ly-get-elt-property", 2, 0, 0, (SCM(*)(...))Score_element::ly_get_elt_property);
710 scm_make_gsubr ("ly-set-elt-property", 3, 0, 0, (SCM(*)(...))Score_element::ly_set_elt_property);
713 ADD_SCM_INIT_FUNC(scoreelt, init_functions);