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];
86 Score_element::get_real (String s) const
88 return gh_scm2double (get_elt_property (s));
92 Score_element::set_real (String s, Real r)
94 set_elt_property (s, gh_double2scm (r));
97 // should also have one that takes SCM arg.
99 Score_element::get_elt_property (String nm) const
101 SCM sym = ly_symbol2scm (nm.ch_C());
102 SCM s = scm_assq(sym, element_property_alist_);
109 SCM sym2 = ly_symbol2scm ((name () + ("::" + nm)).ch_C());
112 // should probably check for Type::sym as well.
113 Paper_def * p= pscore_l_->paper_l_;
114 if (p->default_properties_.try_retrieve (sym2, &val))
116 else if (p->default_properties_.try_retrieve (sym, &val))
120 return SCM_UNDEFINED;
124 Score_element::remove_elt_property (String key)
126 SCM s = get_elt_property (key);
127 SCM sym = ly_symbol2scm (key.ch_C());
128 element_property_alist_ = scm_assq_remove_x (element_property_alist_, sym);
136 Score_element::set_elt_property (String k, SCM v)
138 SCM s = ly_symbol2scm (k.ch_C( ));
139 element_property_alist_ = scm_assoc_set_x (element_property_alist_, s, v);
143 Score_element::molecule_extent(Dimension_cache const *c)
145 Score_element *s = dynamic_cast<Score_element*>(c->element_l());
146 Molecule m = s->do_brew_molecule();
147 return m.extent()[c->axis ()];
152 Score_element::print() const
155 DEBUG_OUT << classname(this) << "{\n";
157 if (flower_dstream && !flower_dstream->silent_b ("Score_element"))
158 ly_display_scm (element_property_alist_);
161 DEBUG_OUT << "Copy ";
169 Score_element::paper_l () const
171 return pscore_l_ ? pscore_l_->paper_l_ : 0;
175 Score_element::lookup_l () const
179 Score_element * urg = (Score_element*)this;
180 SCM sz = urg->remove_elt_property ("fontsize");
181 int i = (gh_number_p (sz))
185 urg->lookup_l_ = (Lookup*)pscore_l_->paper_l_->lookup_l (i);
191 Score_element::add_processing()
193 assert (status_i_ >=0);
202 if (get_elt_property ("self-alignment-X") != SCM_UNDEFINED
203 && !dim_cache_[X_AXIS]->off_callback_l_)
205 dim_cache_[X_AXIS]->off_callbacks_.push (Side_position_interface::aligned_on_self);
208 if (get_elt_property ("self-alignment-Y") != SCM_UNDEFINED
209 && !dim_cache_[X_AXIS]->off_callback_l_)
212 dim_cache_[Y_AXIS]->set_offset_callback (Side_position_interface::aligned_on_self);
220 Score_element::calculate_dependencies (int final, int busy,
221 Score_element_method_pointer funcptr)
223 assert (status_i_ >=0);
225 if (status_i_ >= final)
228 assert (status_i_!= busy);
231 Link_array<Score_element> dependency_arr =
232 Group_interface__extract_elements (this, (Score_element*)0, "dependencies");
234 for (int i=0; i < dependency_arr.size(); i++)
235 dependency_arr[i]->calculate_dependencies (final, busy, funcptr);
237 Link_array<Score_element> extra (get_extra_dependencies());
238 for (int i=0; i < extra.size(); i++)
239 extra[i]->calculate_dependencies (final, busy, funcptr);
246 Score_element::output_processing ()
248 if (to_boolean (get_elt_property ("transparent")))
252 Molecule m (do_brew_molecule ());
253 Offset o (relative_coordinate (0, X_AXIS), relative_coordinate (0, Y_AXIS));
255 SCM s = get_elt_property ("extra-offset");
258 Real il = paper_l ()->get_var ("interline");
259 o[X_AXIS] += il * gh_scm2double (gh_car (s));
260 o[Y_AXIS] += il * gh_scm2double (gh_cdr (s));
263 pscore_l_->outputter_l_->output_molecule (m.expr_, o, classname(this));
272 Score_element::do_break_processing()
277 Score_element::do_post_processing()
282 Score_element::do_breakable_col_processing()
284 handle_prebroken_dependencies();
288 Score_element::do_pre_processing()
293 Score_element::do_space_processing ()
298 Score_element::do_add_processing()
305 Score_element::do_brew_molecule() const
307 SCM glyph = get_elt_property ("glyph");
308 if (gh_string_p (glyph))
310 return lookup_l ()->afm_find (String (ly_scm2string (glyph)));
317 return lookup_l ()->fill (Box (emp,emp));
323 Score_element::line_l() const
329 Score_element::add_dependency (Score_element*e)
333 Group_interface gi (this, "dependencies");
337 programming_error ("Null dependency added");
344 Do break substitution in S, using CRITERION. Return new value.
345 CRITERION is either a SMOB pointer to the desired line, or a number
346 representing the break direction. Do not modify SRC.
349 Score_element::handle_broken_smobs (SCM src, SCM criterion)
354 Score_element *sc = unsmob_element (src);
357 if (criterion == SCM_UNDEFINED)
358 return SCM_UNDEFINED;
359 else if (gh_number_p (criterion))
361 Item * i = dynamic_cast<Item*> (sc);
362 Direction d = to_dir (criterion);
363 if (i && i->break_status_dir () != d)
365 Item *br = i->find_broken_piece (d);
366 return (br) ? br->self_scm_ : SCM_UNDEFINED;
371 Score_element * ln = unsmob_element ( criterion);
372 Line_of_score * line = dynamic_cast<Line_of_score*> (ln);
373 Score_element * br =0;
374 Line_of_score * dep_line = sc->line_l ();
375 if (dep_line != line)
377 br = sc->find_broken_piece (line);
378 return (br) ? br->self_scm_ : SCM_UNDEFINED;
381 return SCM_UNDEFINED;
384 else if (gh_pair_p (src))
387 UGH! breaks on circular lists.
389 SCM car = handle_broken_smobs (gh_car (src), criterion);
390 SCM cdr = gh_cdr (src);
392 if (car == SCM_UNDEFINED
393 && (gh_pair_p (cdr) || cdr == SCM_EOL))
396 This is tail-recursion, ie.
398 return handle_broken_smobs (cdr, criterion);
400 We don't want to rely on the compiler to do this. */
405 return gh_cons (car, handle_broken_smobs (cdr, criterion));
414 Score_element::handle_broken_dependencies()
416 Spanner * s= dynamic_cast<Spanner*> (this);
417 if (original_l_ && s)
422 for (int i = 0; i< s->broken_into_l_arr_ .size (); i++)
424 Score_element * sc = s->broken_into_l_arr_[i];
425 Line_of_score * l = sc->line_l ();
426 s->broken_into_l_arr_[i]->element_property_alist_ =
427 handle_broken_smobs (element_property_alist_,
428 l ? l->self_scm_ : SCM_UNDEFINED);
432 Line_of_score *line = line_l();
433 element_property_alist_
434 = handle_broken_smobs (element_property_alist_,
435 line ? line->self_scm_ : SCM_UNDEFINED);
443 Score_element::handle_prebroken_dependencies()
445 if (Item*i =dynamic_cast<Item*> (this))
449 element_property_alist_
450 = handle_broken_smobs (original_l_->element_property_alist_,
451 gh_int2scm (i->break_status_dir ()));
456 Link_array<Score_element>
457 Score_element::get_extra_dependencies() const
459 Link_array<Score_element> empty;
464 Score_element::linked_b() const
470 Score_element::do_print () const
475 Score_element::find_broken_piece (Line_of_score*) const
481 Score_element::translate_axis (Real y, Axis a)
483 dim_cache_[a]->translate (y);
487 Score_element::relative_coordinate (Score_element const*e, Axis a) const
489 return dim_cache_[a]->relative_coordinate (e ? e->dim_cache_[a] : 0);
493 Score_element::common_refpoint (Score_element const* s, Axis a) const
495 Dimension_cache *dim = dim_cache_[a]->common_refpoint (s->dim_cache_[a]);
496 return dim ? dim->element_l () : 0;
500 Score_element::set_empty (Axis a)
502 dim_cache_[a]->callback_l_ =0;
506 Score_element::empty_b (Axis a)const
508 return !dim_cache_[a]->callback_l_;
512 Score_element::extent (Axis a) const
514 Dimension_cache const * d = dim_cache_[a];
516 return d->get_dim ();
521 Score_element::parent_l (Axis a) const
523 Dimension_cache*d= dim_cache_[a]->parent_l_;
524 return d ? d->elt_l_ : 0;
528 Score_element::common_refpoint (Link_array<Score_element> gs, Axis a) const
530 Dimension_cache * common = dim_cache_[a];
531 for (int i=0; i < gs.size (); i++)
533 common = common->common_refpoint (gs[i]->dim_cache_[a]);
536 return common->element_l ();
540 Score_element::name () const
542 return classname (this);
547 Score_element::set_parent (Score_element *g, Axis a)
549 dim_cache_[a]->parent_l_ = g ? g->dim_cache_[a]: 0;
553 Score_element::fixup_refpoint ()
555 for (int a = X_AXIS; a < NO_AXES; a ++)
558 Score_element * par = parent_l (ax);
563 if (par->line_l () != line_l ())
565 Score_element * newpar = par->find_broken_piece (line_l ());
566 set_parent (newpar, ax);
569 if (Item * i = dynamic_cast<Item*> (this))
571 Item *pari = dynamic_cast<Item*> (par);
575 Direction my_dir = i->break_status_dir () ;
576 if (my_dir!= pari->break_status_dir())
578 Item *newpar = pari->find_broken_piece (my_dir);
579 set_parent (newpar, ax);
588 /****************************************************
590 ****************************************************/
593 #include "ly-smobs.icc"
595 IMPLEMENT_UNSMOB(Score_element, element);
596 IMPLEMENT_SMOBS(Score_element);
598 Score_element::mark_smob (SCM ses)
600 Score_element * s = SMOB_TO_TYPE (Score_element, ses);
601 if (s->self_scm_ != ses)
603 programming_error ("SMOB marking gone awry");
606 return s->element_property_alist_;
610 Score_element::print_smob (SCM s, SCM port, scm_print_state *)
612 Score_element *sc = (Score_element *) gh_cdr (s);
614 scm_puts ("#<Score_element ", port);
615 scm_puts ((char *)sc->name (), port);
618 don't try to print properties, that is too much hassle.
620 scm_puts (" >", port);
625 Score_element::do_smobify_self ()
630 Score_element::equal_p (SCM a, SCM b)
632 return gh_cdr(a) == gh_cdr(b) ? SCM_BOOL_T : SCM_BOOL_F;
637 Score_element::ly_set_elt_property (SCM elt, SCM sym, SCM val)
639 Score_element * sc = unsmob_element (elt);
641 if (!gh_symbol_p (sym))
643 error ("Not a symbol");
644 ly_display_scm (sym);
645 return SCM_UNDEFINED;
650 sc->element_property_alist_ = scm_assoc_set_x (sc->element_property_alist_, sym, val);
654 error ("Not a score element");
655 ly_display_scm (elt);
658 return SCM_UNDEFINED;
663 Score_element::ly_get_elt_property (SCM elt, SCM sym)
665 Score_element * sc = unsmob_element (elt);
669 SCM s = scm_assq(sym, sc->element_property_alist_);
674 return SCM_UNDEFINED;
678 error ("Not a score element");
679 ly_display_scm (elt);
681 return SCM_UNDEFINED;
688 scm_make_gsubr ("ly-get-elt-property", 2, 0, 0, (SCM(*)(...))Score_element::ly_get_elt_property);
689 scm_make_gsubr ("ly-set-elt-property", 3, 0, 0, (SCM(*)(...))Score_element::ly_set_elt_property);
692 ADD_SCM_INIT_FUNC(scoreelt, init_functions);