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>
13 #include "input-smob.hh"
14 #include "libc-extension.hh"
15 #include "group-interface.hh"
17 #include "paper-score.hh"
18 #include "paper-def.hh"
19 #include "molecule.hh"
20 #include "score-element.hh"
23 #include "line-of-score.hh"
25 #include "paper-column.hh"
26 #include "molecule.hh"
28 #include "paper-outputter.hh"
29 #include "dimension-cache.hh"
30 #include "side-position-interface.hh"
33 #include "ly-smobs.icc"
38 remove dynamic_cast<Spanner,Item> and put this code into respective
43 #define INFINITY_MSG "Infinity or NaN encountered"
45 Score_element::Score_element(SCM basicprops
)
48 fixme: default should be no callback.
54 immutable_property_alist_
= basicprops
;
55 mutable_property_alist_
= SCM_EOL
;
59 char const*onames
[] = {"X-offset-callbacks", "Y-offset-callbacks"};
60 char const*enames
[] = {"X-extent-callback", "Y-extent-callback"};
62 for (int a
= X_AXIS
; a
<= Y_AXIS
; a
++){
63 SCM l
= get_elt_property (onames
[a
]);
65 if (scm_ilength (l
) >=0)
67 dim_cache_
[a
].offset_callbacks_
= l
;
68 dim_cache_
[a
].offsets_left_
= scm_ilength (l
);
72 programming_error ("[XY]-offset-callbacks must be a list");
75 SCM cb
= get_elt_property (enames
[a
]);
78 Should change default to be empty?
80 if (!gh_procedure_p (cb
) && !gh_pair_p (cb
))
81 cb
= molecule_extent_proc
;
83 dim_cache_
[a
].dimension_
= cb
;
86 SCM meta
= get_elt_property ("meta");
87 SCM ifs
= scm_assoc (ly_symbol2scm ("interfaces"), meta
);
89 set_elt_property ("interfaces",gh_cdr (ifs
));
93 Score_element::Score_element (Score_element
const&s
)
94 : dim_cache_ (s
.dim_cache_
)
96 original_l_
=(Score_element
*) &s
;
97 immutable_property_alist_
= s
.immutable_property_alist_
;
98 mutable_property_alist_
= SCM_EOL
;
100 status_i_
= s
.status_i_
;
101 pscore_l_
= s
.pscore_l_
;
106 Score_element::~Score_element()
109 do nothing scm-ish and no unprotecting here.
115 Score_element::get_elt_property (const char *nm
) const
117 SCM sym
= ly_symbol2scm (nm
);
118 return get_elt_property (sym
);
122 Score_element::get_elt_property (SCM sym
) const
124 SCM s
= scm_sloppy_assq(sym
, mutable_property_alist_
);
128 s
= scm_sloppy_assq (sym
, immutable_property_alist_
);
129 return (s
== SCM_BOOL_F
) ? SCM_EOL
: gh_cdr (s
);
133 Remove the value associated with KEY, and return it. The result is
134 that a next call will yield SCM_UNDEFINED (and not the underlying
138 Score_element::remove_elt_property (const char* key
)
140 SCM val
= get_elt_property (key
);
142 set_elt_property (key
, SCM_EOL
);
147 Score_element::set_elt_property (const char* k
, SCM v
)
149 SCM s
= ly_symbol2scm (k
);
150 set_elt_property (s
, v
);
154 Puts the k, v in the immutable_property_alist_, which is convenient for
155 storing variables that are needed during the breaking process. (eg.
156 Line_of_score::rank : int )
159 Score_element::set_immutable_elt_property (const char*k
, SCM v
)
161 SCM s
= ly_symbol2scm (k
);
162 set_immutable_elt_property (s
, v
);
166 Score_element::set_immutable_elt_property (SCM s
, SCM v
)
168 immutable_property_alist_
= gh_cons (gh_cons (s
,v
), mutable_property_alist_
);
169 mutable_property_alist_
= scm_assq_remove_x (mutable_property_alist_
, s
);
172 Score_element::set_elt_property (SCM s
, SCM v
)
174 mutable_property_alist_
= scm_assq_set_x (mutable_property_alist_
, s
, v
);
178 MAKE_SCHEME_CALLBACK(Score_element
,molecule_extent
,2);
180 Score_element::molecule_extent (SCM element_smob
, SCM scm_axis
)
182 Score_element
*s
= unsmob_element (element_smob
);
183 Axis a
= (Axis
) gh_scm2int (scm_axis
);
185 Molecule
*m
= s
->get_molecule ();
189 return ly_interval2scm ( e
);
192 MAKE_SCHEME_CALLBACK(Score_element
,preset_extent
,2);
195 Score_element::preset_extent (SCM element_smob
, SCM scm_axis
)
197 Score_element
*s
= unsmob_element (element_smob
);
198 Axis a
= (Axis
) gh_scm2int (scm_axis
);
200 SCM ext
= s
->get_elt_property ((a
== X_AXIS
)
206 Real l
= gh_scm2double (gh_car (ext
));
207 Real r
= gh_scm2double (gh_cdr (ext
));
208 return ly_interval2scm (Interval (l
, r
));
211 return ly_interval2scm ( Interval ());
217 Score_element::paper_l () const
219 return pscore_l_
? pscore_l_
->paper_l_
: 0;
223 Score_element::calculate_dependencies (int final
, int busy
, SCM funcname
)
225 assert (status_i_
>=0);
227 if (status_i_
>= final
)
230 if (status_i_
== busy
)
232 programming_error ("Element is busy, come back later");
238 for (SCM d
= get_elt_property ("dependencies"); gh_pair_p (d
); d
= gh_cdr (d
))
240 unsmob_element (gh_car (d
))
241 ->calculate_dependencies (final
, busy
, funcname
);
245 String s
= ly_symbol2string (funcname
);
246 SCM proc
= get_elt_property (s
.ch_C());
247 if (gh_procedure_p (proc
))
248 gh_call1 (proc
, this->self_scm ());
255 Score_element::get_molecule () const
257 SCM mol
= get_elt_property ("molecule");
258 if (unsmob_molecule (mol
))
259 return unsmob_molecule (mol
);
261 SCM proc
= get_elt_property ("molecule-callback");
264 if (gh_procedure_p (proc
))
265 mol
= gh_apply (proc
, gh_list (this->self_scm (), SCM_UNDEFINED
));
269 TODO: add option for not copying origin info.
271 SCM origin
=get_elt_property ("origin");
272 if (!unsmob_input (origin
))
273 origin
=ly_symbol2scm ("no-origin");
278 mol
= gh_cons (gh_list (origin
, gh_car (mol
), SCM_UNDEFINED
), gh_cdr (mol
));
281 Molecule
*m
= unsmob_molecule (mol
);
285 transparent retains dimensions of element.
287 if (m
&& to_boolean (get_elt_property ("transparent")))
288 mol
= Molecule (m
->extent_box (), SCM_EOL
).smobbed_copy ();
290 Score_element
*me
= (Score_element
*)this;
291 me
->set_elt_property ("molecule", mol
);
293 m
= unsmob_molecule (mol
);
304 Score_element::do_break_processing()
314 Score_element::line_l() const
320 Score_element::add_dependency (Score_element
*e
)
324 Pointer_group_interface ::add_element (this, "dependencies",e
);
328 programming_error ("Null dependency added");
335 Do break substitution in S, using CRITERION. Return new value.
336 CRITERION is either a SMOB pointer to the desired line, or a number
337 representing the break direction. Do not modify SRC.
340 Score_element::handle_broken_smobs (SCM src
, SCM criterion
)
343 Score_element
*sc
= unsmob_element (src
);
346 if (gh_number_p (criterion
))
348 Item
* i
= dynamic_cast<Item
*> (sc
);
349 Direction d
= to_dir (criterion
);
350 if (i
&& i
->break_status_dir () != d
)
352 Item
*br
= i
->find_prebroken_piece (d
);
353 return (br
) ? br
->self_scm () : SCM_UNDEFINED
;
359 = dynamic_cast<Line_of_score
*> (unsmob_element (criterion
));
360 if (sc
->line_l () != line
)
362 sc
= sc
->find_broken_piece (line
);
366 /* now: !sc || (sc && sc->line_l () == line) */
368 return SCM_UNDEFINED
;
370 /* now: sc && sc->line_l () == line */
372 || (sc
->common_refpoint (line
, X_AXIS
)
373 && sc
->common_refpoint (line
, Y_AXIS
)))
375 return sc
->self_scm ();
377 return SCM_UNDEFINED
;
380 else if (gh_pair_p (src
))
382 SCM oldcar
=gh_car (src
);
384 UGH! breaks on circular lists.
386 SCM newcar
= handle_broken_smobs (oldcar
, criterion
);
387 SCM oldcdr
= gh_cdr (src
);
389 if (newcar
== SCM_UNDEFINED
390 && (gh_pair_p (oldcdr
) || oldcdr
== SCM_EOL
))
393 This is tail-recursion, ie.
395 return handle_broken_smobs (cdr, criterion);
397 We don't want to rely on the compiler to do this. Without
398 tail-recursion, this easily crashes with a stack overflow. */
403 SCM newcdr
= handle_broken_smobs (oldcdr
, criterion
);
404 return gh_cons (newcar
, newcdr
);
413 Score_element::handle_broken_dependencies()
415 Spanner
* s
= dynamic_cast<Spanner
*> (this);
416 if (original_l_
&& s
)
421 for (int i
= 0; i
< s
->broken_into_l_arr_
.size (); i
++)
423 Score_element
* sc
= s
->broken_into_l_arr_
[i
];
424 Line_of_score
* l
= sc
->line_l ();
425 sc
->mutable_property_alist_
=
426 handle_broken_smobs (mutable_property_alist_
,
427 l
? l
->self_scm () : SCM_UNDEFINED
);
432 Line_of_score
*line
= line_l();
434 if (line
&& common_refpoint (line
, X_AXIS
) && common_refpoint (line
, Y_AXIS
))
436 mutable_property_alist_
437 = handle_broken_smobs (mutable_property_alist_
,
438 line
? line
->self_scm () : SCM_UNDEFINED
);
440 else if (dynamic_cast <Line_of_score
*> (this))
442 mutable_property_alist_
= handle_broken_smobs (mutable_property_alist_
,
448 This element is `invalid'; it has been removed from all
449 dependencies, so let's junk the element itself.
451 do not do this for Line_of_score, since that would remove
452 references to the originals of score-elts, which get then GC'd
460 Note that we still want references to this element to be
461 rearranged, and not silently thrown away, so we keep pointers
462 like {broken_into_{drul,array}, original}
465 Score_element::suicide ()
467 mutable_property_alist_
= SCM_EOL
;
468 immutable_property_alist_
= SCM_EOL
;
470 set_extent_callback (SCM_EOL
, Y_AXIS
);
471 set_extent_callback (SCM_EOL
, X_AXIS
);
473 for (int a
= X_AXIS
; a
<= Y_AXIS
; a
++)
475 dim_cache_
[a
].offset_callbacks_
= SCM_EOL
;
476 dim_cache_
[a
].offsets_left_
= 0;
481 Score_element::handle_prebroken_dependencies()
486 Score_element::find_broken_piece (Line_of_score
*) const
492 Score_element::translate_axis (Real y
, Axis a
)
494 if (isinf (y
) || isnan (y
))
495 programming_error (_(INFINITY_MSG
));
498 dim_cache_
[a
].offset_
+= y
;
503 Score_element::relative_coordinate (Score_element
const*refp
, Axis a
) const
509 We catch PARENT_L_ == nil case with this, but we crash if we did
510 not ask for the absolute coordinate (ie. REFP == nil.)
513 if (refp
== dim_cache_
[a
].parent_l_
)
514 return get_offset (a
);
516 return get_offset (a
) + dim_cache_
[a
].parent_l_
->relative_coordinate (refp
, a
);
520 Score_element::get_offset (Axis a
) const
522 Score_element
*me
= (Score_element
*) this;
523 while (dim_cache_
[a
].offsets_left_
)
525 int l
= --me
->dim_cache_
[a
].offsets_left_
;
526 SCM cb
= scm_list_ref (dim_cache_
[a
].offset_callbacks_
, gh_int2scm (l
));
527 SCM retval
= gh_call2 (cb
, self_scm (), gh_int2scm (a
));
529 Real r
= gh_scm2double (retval
);
530 if (isinf (r
) || isnan (r
))
532 programming_error (INFINITY_MSG
);
535 me
->dim_cache_
[a
].offset_
+=r
;
537 return dim_cache_
[a
].offset_
;
541 MAKE_SCHEME_CALLBACK(Score_element
,point_dimension_callback
,2);
543 Score_element::point_dimension_callback (SCM
, SCM
)
545 return ly_interval2scm ( Interval (0,0));
549 Score_element::empty_b (Axis a
)const
551 return ! (gh_pair_p (dim_cache_
[a
].dimension_
) ||
552 gh_procedure_p (dim_cache_
[a
].dimension_
));
558 Score_element *refpoint
563 Score_element::extent (Score_element
* refp
, Axis a
) const
565 Real x
= relative_coordinate (refp
, a
);
568 Dimension_cache
* d
= (Dimension_cache
*)&dim_cache_
[a
];
570 if (gh_pair_p (d
->dimension_
))
572 else if (gh_procedure_p (d
->dimension_
))
575 FIXME: add doco on types, and should typecheck maybe?
577 d
->dimension_
= gh_call2 (d
->dimension_
, self_scm(), gh_int2scm (a
));
582 if (!gh_pair_p (d
->dimension_
))
585 ext
= ly_scm2interval (d
->dimension_
);
587 SCM extra
= get_elt_property (a
== X_AXIS
594 if (gh_pair_p (extra
))
596 ext
[BIGGER
] += gh_scm2double (gh_cdr (extra
));
597 ext
[SMALLER
] += gh_scm2double (gh_car (extra
));
600 extra
= get_elt_property (a
== X_AXIS
602 : "minimum-extent-Y");
603 if (gh_pair_p (extra
))
605 ext
.unite (Interval (gh_scm2double (gh_car (extra
)),
606 gh_scm2double (gh_cdr (extra
))));
616 Score_element::parent_l (Axis a
) const
618 return dim_cache_
[a
].parent_l_
;
622 Score_element::common_refpoint (Score_element
const* s
, Axis a
) const
625 I don't like the quadratic aspect of this code, but I see no other
626 way. The largest chain of parents might be 10 high or so, so
627 it shouldn't be a real issue. */
628 for (Score_element
const *c
= this; c
; c
= c
->dim_cache_
[a
].parent_l_
)
629 for (Score_element
const * d
= s
; d
; d
= d
->dim_cache_
[a
].parent_l_
)
631 return (Score_element
*)d
;
638 Score_element::common_refpoint (SCM elist
, Axis a
) const
640 Score_element
* common
= (Score_element
*) this;
641 for (; gh_pair_p (elist
); elist
= gh_cdr (elist
))
643 Score_element
* s
= unsmob_element (gh_car (elist
));
645 common
= common
->common_refpoint (s
, a
);
652 Score_element::name () const
654 SCM meta
= get_elt_property ("meta");
655 SCM nm
= scm_assoc (ly_symbol2scm ("name"), meta
);
656 nm
= (gh_pair_p (nm
)) ? gh_cdr (nm
) : SCM_EOL
;
657 return gh_string_p (nm
) ?ly_scm2string (nm
) : classname (this);
661 Score_element::add_offset_callback (SCM cb
, Axis a
)
663 if (!has_offset_callback_b (cb
, a
))
665 dim_cache_
[a
].offset_callbacks_
= gh_cons (cb
, dim_cache_
[a
].offset_callbacks_
);
666 dim_cache_
[a
].offsets_left_
++;
671 Score_element::has_extent_callback_b (SCM cb
, Axis a
)const
673 return scm_equal_p (cb
, dim_cache_
[a
].dimension_
);
678 Score_element::has_extent_callback_b (Axis a
) const
680 return gh_procedure_p (dim_cache_
[a
].dimension_
);
684 Score_element::has_offset_callback_b (SCM cb
, Axis a
)const
686 return scm_memq (cb
, dim_cache_
[a
].offset_callbacks_
) != SCM_BOOL_F
;
690 Score_element::set_extent_callback (SCM dc
, Axis a
)
692 dim_cache_
[a
].dimension_
=dc
;
696 Score_element::set_parent (Score_element
*g
, Axis a
)
698 dim_cache_
[a
].parent_l_
= g
;
701 MAKE_SCHEME_CALLBACK(Score_element
,fixup_refpoint
,1);
703 Score_element::fixup_refpoint (SCM smob
)
705 Score_element
*me
= unsmob_element (smob
);
706 for (int a
= X_AXIS
; a
< NO_AXES
; a
++)
709 Score_element
* parent
= me
->parent_l (ax
);
714 if (parent
->line_l () != me
->line_l () && me
->line_l ())
716 Score_element
* newparent
= parent
->find_broken_piece (me
->line_l ());
717 me
->set_parent (newparent
, ax
);
720 if (Item
* i
= dynamic_cast<Item
*> (me
))
722 Item
*parenti
= dynamic_cast<Item
*> (parent
);
726 Direction my_dir
= i
->break_status_dir () ;
727 if (my_dir
!= parenti
->break_status_dir())
729 Item
*newparent
= parenti
->find_prebroken_piece (my_dir
);
730 me
->set_parent (newparent
, ax
);
740 /****************************************************
742 ****************************************************/
745 IMPLEMENT_UNSMOB(Score_element
, element
);
746 IMPLEMENT_SMOBS(Score_element
);
747 IMPLEMENT_DEFAULT_EQUAL_P(Score_element
);
750 Score_element::mark_smob (SCM ses
)
752 Score_element
* s
= (Score_element
*) SCM_CELL_WORD_1(ses
);
753 scm_gc_mark (s
->immutable_property_alist_
);
754 scm_gc_mark (s
->mutable_property_alist_
);
756 for (int a
=0 ; a
< 2; a
++)
758 scm_gc_mark (s
->dim_cache_
[a
].offset_callbacks_
);
759 scm_gc_mark (s
->dim_cache_
[a
].dimension_
);
762 if (s
->parent_l (Y_AXIS
))
763 scm_gc_mark (s
->parent_l (Y_AXIS
)->self_scm ());
764 if (s
->parent_l (X_AXIS
))
765 scm_gc_mark (s
->parent_l (X_AXIS
)->self_scm ());
768 scm_gc_mark (s
->original_l_
->self_scm ());
769 return s
->do_derived_mark ();
773 Score_element::print_smob (SCM s
, SCM port
, scm_print_state
*)
775 Score_element
*sc
= (Score_element
*) gh_cdr (s
);
777 scm_puts ("#<Score_element ", port
);
778 scm_puts ((char *)sc
->name ().ch_C(), port
);
781 don't try to print properties, that is too much hassle.
783 scm_puts (" >", port
);
788 Score_element::do_derived_mark ()
795 ly_set_elt_property (SCM elt
, SCM sym
, SCM val
)
797 Score_element
* sc
= unsmob_element (elt
);
799 if (!gh_symbol_p (sym
))
801 error ("Not a symbol");
802 ly_display_scm (sym
);
803 return SCM_UNSPECIFIED
;
808 sc
->set_elt_property (sym
, val
);
812 error ("Not a score element");
813 ly_display_scm (elt
);
816 return SCM_UNSPECIFIED
;
821 ly_get_elt_property (SCM elt
, SCM sym
)
823 Score_element
* sc
= unsmob_element (elt
);
827 return sc
->get_elt_property (sym
);
831 error ("Not a score element");
832 ly_display_scm (elt
);
834 return SCM_UNSPECIFIED
;
839 Score_element::discretionary_processing()
846 spanner_get_bound (SCM slur
, SCM dir
)
848 return dynamic_cast<Spanner
*> (unsmob_element (slur
))->get_bound (to_dir (dir
))->self_scm ();
853 static SCM interfaces_sym
;
857 interfaces_sym
= scm_permanent_object (ly_symbol2scm ("interfaces"));
859 scm_make_gsubr ("ly-get-elt-property", 2, 0, 0, (Scheme_function_unknown
)ly_get_elt_property
);
860 scm_make_gsubr ("ly-set-elt-property", 3, 0, 0, (Scheme_function_unknown
)ly_set_elt_property
);
861 scm_make_gsubr ("ly-get-spanner-bound", 2 , 0, 0, (Scheme_function_unknown
) spanner_get_bound
);
865 Score_element::has_interface (SCM k
)
867 SCM ifs
= get_elt_property (interfaces_sym
);
869 return scm_memq (k
, ifs
) != SCM_BOOL_F
;
873 Score_element::set_interface (SCM k
)
875 if (has_interface (k
))
879 set_elt_property (interfaces_sym
,
880 gh_cons (k
, get_elt_property (interfaces_sym
)));
885 ADD_SCM_INIT_FUNC(scoreelt
, init_functions
);
886 IMPLEMENT_TYPE_P(Score_element
, "ly-element?");